-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sand.cs
172 lines (137 loc) · 4.98 KB
/
Sand.cs
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
using System;
namespace SandPile
{
public enum EpsionDis { Random, Directed, Elliptical};
public class Sand
{
public int Lx, Ly;
public int[,] massSites;
public Direction[,] eSites;
public EpsionDis epsilonDis;
public int e, m, numFalledGrains;
public int minAdded = 1, maxAdded = 1000, scale = 4000, scalak = 1000;
public int xInit, yInit, topledValueInit;
public int[,] h;
public bool isCompleteAvalanche;
static int[] nnX = new int[] { 1, -1, 0, 0 };
static int[] nnY = new int[] { 0, 0, 1, -1};
Random rand = new Random();
public Sand(int Lx, int Ly, int e, int m, EpsionDis epsilonDis)
{
this.Lx = Lx;
this.Ly = Ly;
this.e = e;
this.m = m;
h = new int[Lx, Ly];
massSites = new int[Lx, Ly];
eSites = new Direction[Lx, Ly];
this.epsilonDis = epsilonDis;
InitEpsilon();
InitMass();
InitHeigths();
isCompleteAvalanche = true;
}
void InitEpsilon()
{
int i, j;
switch (epsilonDis)
{
case EpsionDis.Directed:
for (i = 0; i < Lx; i++)
for (j = 0; j < Ly; j++)
eSites[i, j] = new Direction(e, -e, e, -e);
break;
case EpsionDis.Elliptical:
for (i = 0; i < Lx; i++)
for (j = 0; j < Ly; j++)
eSites[i, j] = new Direction(e, e, -e, -e);
break;
case EpsionDis.Random:
int temp_e;
for (i = 0; i < Lx; i++)
for (j = 0; j < Ly; j++)
{
temp_e = (rand.NextDouble() < 0.5) ? e : -e;
eSites[i, j] = new Direction(temp_e, temp_e, -temp_e, -temp_e);
}
break;
}
}
void InitMass()
{
for (int i = 0; i < Lx; i++)
for (int j = 0; j < Ly; j++)
massSites[i, j] = m;
}
void InitHeigths()
{
for (int i = 0; i < Lx; i++)
for (int j = 0; j < Ly; j++)
h[i, j] = rand.Next(scalak + 1, scale + 1);
}
bool IsInLattice(int x, int y)
{
if (x >= 0 && x < Lx && y >= 0 && y < Ly)
return true;
return false;
}
public void PropagateOneWave()
{
Topple(xInit, yInit, topledValueInit);
if (h[xInit, yInit] > topledValueInit)
isCompleteAvalanche = false;
else
isCompleteAvalanche = true;
}
void Topple(int x, int y, int topledValue)
{
if (h[x, y] <= topledValue)
return;
h[x, y] += -topledValue;
int x2 = x + 1, x1 = x - 1;
int y2 = y + 1, y1 = y - 1;
if (x2 < Lx)
{
h[x2, y] += scalak + eSites[x, y].right;
int topledValueNew = scale + massSites[x2, y];
if ((x2 != xInit || y != yInit) && h[x2, y] > topledValueNew)
Topple(x2, y, topledValueNew);
}
if (x1 >= 0)
{
h[x1, y] += scalak + eSites[x, y].left;
int topledValueNew = scale + massSites[x1, y];
if ((x1 != xInit || y != yInit) && h[x1, y] > topledValueNew)
Topple(x1, y, topledValueNew);
}
if (y2 < Ly)
{
h[x, y2] += scalak + eSites[x, y].up;
int topledValueNew = scale + massSites[x, y2];
if ((x != xInit || y2 != yInit) && h[x, y2] > topledValueNew)
Topple(x, y2, topledValueNew);
}
if (y1 >= 0)
{
h[x, y1] += scalak + eSites[x, y].down;
int topledValueNew = scale + massSites[x, y1];
if ((x != xInit || y1 != yInit) && h[x, y1] > topledValueNew)
Topple(x, y1, topledValueNew);
}
}
public void AddOneGrain()
{
isCompleteAvalanche = true;
numFalledGrains++;
int x0 = rand.Next(Lx);
int y0 = rand.Next(Ly);
h[x0, y0] += rand.Next(minAdded, maxAdded);
int topledValue = scale + massSites[x0, y0];
xInit = x0;
yInit = y0;
topledValueInit = topledValue;
if (h[x0, y0] > topledValue)
PropagateOneWave();
}
}
}