Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
btw_sandpile.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11*/
12
18#include <cstddef>
19#include <cstdint>
20#include <filesystem>
21#include <fstream>
22#include <iomanip>
23#include <iostream>
24#include <vector>
25
27
28using namespace Aleph::CA;
29using namespace Aleph::CA::Reproductions;
30
31#ifndef ALEPH_REPRODUCTIONS_SOURCE_DIR
32# define ALEPH_REPRODUCTIONS_SOURCE_DIR "reproductions"
33#endif
34
35int main()
36{
37 constexpr ca_size_t side = 64;
38 constexpr std::size_t warmup_drops = 20000;
39 constexpr std::size_t measured_drops = 100000;
40 constexpr std::uint64_t seed = 0x4254571987ull;
41
43
45 for (std::size_t i = 0; i < warmup_drops; ++i)
46 (void) sandpile.drop_random();
47
48 std::vector<std::size_t> sizes;
49 std::vector<std::size_t> durations;
50 sizes.reserve(measured_drops);
52 for (std::size_t i = 0; i < measured_drops; ++i)
53 {
54 const Avalanche avalanche = sandpile.drop_random();
55 if (avalanche.size != 0)
56 {
57 sizes.push_back(avalanche.size);
58 durations.push_back(avalanche.duration);
59 }
60 }
61
62 const Linear_Fit fit = fit_log_log_histogram(sizes, 4, 2048, 18);
63 const std::filesystem::path root = ALEPH_REPRODUCTIONS_SOURCE_DIR;
64 write_histogram_csv(root / "results" / "btw_avalanche_sizes.csv", sizes);
65 write_histogram_csv(root / "results" / "btw_avalanche_durations.csv", durations);
66
67 std::ofstream summary(root / "results" / "btw_summary.csv");
68 if (not summary)
69 {
70 std::cerr << "Cannot write BTW summary\n";
71 return 1;
72 }
73 summary << "side,warmup_drops,measured_drops,nonzero_avalanches,slope,r_squared,fit_points\n"
74 << side << ',' << warmup_drops << ',' << measured_drops << ','
75 << sizes.size() << ',' << std::setprecision(12) << fit.slope << ','
76 << fit.r_squared << ',' << fit.points << '\n';
77
78 std::cout << "BTW sandpile: slope=" << std::fixed << std::setprecision(4)
79 << fit.slope << " r2=" << fit.r_squared
80 << " nonzero_avalanches=" << sizes.size() << '\n';
81 if (fit.slope < -1.2 or fit.slope > -0.8)
82 {
83 std::cerr << "BTW avalanche-size slope outside [-1.2, -0.8]\n";
84 return 2;
85 }
86 return 0;
87}
#define ALEPH_REPRODUCTIONS_SOURCE_DIR
int main()
Internal helpers shared by the cellular-automata reproductions.
Deterministic open-boundary Bak-Tang-Wiesenfeld sandpile.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4071
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Linear_Fit fit_log_log_histogram(const std::vector< std::size_t > &samples, const std::size_t min_value, const std::size_t max_value, const std::size_t bins, const double sample_weight_power=0.0)
Fit a power-law exponent from logarithmically binned samples.
void write_histogram_csv(const std::filesystem::path &path, const std::vector< std::size_t > &samples)
Write an exact discrete histogram as CSV.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Avalanche measurements for one BTW grain drop.
std::size_t duration
number of non-empty row-major sweeps.
std::size_t size
number of topplings.
Least-squares result for a log-log histogram.
std::size_t points
populated bins included in the fit.
double r_squared
coefficient of determination.
static constexpr bool requires_double_buffer
Operates in place, no second buffer needed.
ValueArg< size_t > seed
Definition testHash.C:53