-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
202 lines (164 loc) · 4.07 KB
/
model.cpp
File metadata and controls
202 lines (164 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2013, Robert Rueger <rueger@itp.uni-frankfurt.de>
*
* This file is part of YASS.
*
* YASS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* YASS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YASS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "model.hpp"
#include <assert.h>
#include <algorithm>
#include <iostream>
using namespace std;
SandpileModel::SandpileModel(
unsigned int size_init, bool pbc_init,
topplemode_t tm_init, unsigned int rng_seed )
: size( size_init ), pbc( pbc_init ),
data( vector<unsigned int>( size_init* size_init, 0 ) ),
grain_counter( 0 ), rng( mt19937( rng_seed) ),
tm( tm_init ) { }
unsigned int SandpileModel::operator()( unsigned int x, unsigned int y ) const
{
assert( y < size );
assert( x < size );
return data[ size * y + x ];
}
unsigned int& SandpileModel::operator()( unsigned int x, unsigned int y )
{
assert( y < size );
assert( x < size );
return data[ size * y + x ];
}
void SandpileModel::add_grain() {
data[
uniform_int_distribution<unsigned int>(0, data.size() - 1 )( rng )
] += 1;
++grain_counter;
}
bool SandpileModel::topple()
{
if ( tm == TOPPLE_ASYNC ) {
return topple_async();
} else {
return topple_sync();
}
}
void SandpileModel::topple_site( unsigned int l )
{
assert( data[ l ] >= 4 );
unsigned int x = l % size;
unsigned int y = l / size;
data[ l ] -= 4;
// bottom neighbor
if ( y == 0 ) {
if ( pbc ) {
operator()( x, size - 1 ) += 1;
} else {
--grain_counter;
}
} else {
operator()( x, y - 1 ) += 1;
}
// right neighbor
if ( x == size - 1 ) {
if ( pbc ) {
operator()( 0, y ) += 1;
} else {
--grain_counter;
}
} else {
operator()( x + 1, y ) += 1;
}
// top neighbor
if ( y == size - 1 ) {
if ( pbc ) {
operator()( x, 0 ) += 1;
} else {
--grain_counter;
}
} else {
operator()( x, y + 1 ) += 1;
}
// left neighbor
if ( x == 0 ) {
if ( pbc ) {
operator()( size - 1, y ) += 1;
} else {
--grain_counter;
}
} else {
operator()( x - 1, y ) += 1;
}
}
bool SandpileModel::topple_sync()
{
bool toppling_occurred = false;
vector<unsigned int> toppling_sites;
toppling_sites.reserve( size * size );
// find toppling sites
assert( data.size() = size * size );
for ( unsigned int i = 0; i < data.size(); ++i ) {
if ( data[i] >= 4 ) {
toppling_sites.push_back( i );
toppling_occurred = true;
}
}
// let them topple
for ( auto ts = toppling_sites.begin(); ts != toppling_sites.end(); ++ts ) {
topple_site( *ts );
}
return toppling_occurred;
}
bool SandpileModel::topple_async()
{
bool toppling_occurred = false;
// find toppling sites
assert( data.size() = size * size );
for ( unsigned int i = 0; i < data.size(); ++i ) {
if ( data[i] >= 4 ) {
topple_site( i );
toppling_occurred = true;
}
}
return toppling_occurred;
}
double SandpileModel::get_density() const
{
return
static_cast<double>( grain_counter ) / static_cast<double>( size * size );
}
double SandpileModel::get_active_site_density() const
{
return
static_cast<double>(
count_if(
data.begin(), data.end(),
[]( unsigned int h ) { return h >= 4; }
)
) / static_cast<double>( size * size );
}
unsigned int SandpileModel::get_num_sites() const
{
return size * size;
}
ostream& operator<<( ostream& out, const SandpileModel& m )
{
for ( unsigned int y = 0; y < m.size; ++y ) {
for ( unsigned int x = 0; x < m.size; ++x ) {
out << m( x, y ) << ' ';
}
out << '\b' <<endl;
}
return out;
}