-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.cpp
175 lines (141 loc) · 3.95 KB
/
simulation.cpp
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
#include "simulation.h"
#include "display.h"
#include <EEvar.h>
fixed cellsA[2][SIM_DIM][SIM_DIM];
fixed cellsB[2][SIM_DIM][SIM_DIM];
fixed diffA;
fixed feedA;
fixed dripA;
fixed diffB;
fixed killB;
fixed dripB;
const EEstore<float> eeDiffA = 1.0;
const EEstore<float> eeFeedA = 0.055;
const EEstore<float> eeDripA = 0.05;
const EEstore<float> eeDiffB = 0.5;
const EEstore<float> eeKillB = 0.062;
const EEstore<float> eeDripB = 0.01;
void loadParameters() {
float value;
eeDiffA >> value;
diffA = value;
eeFeedA >> value;
feedA = value;
eeDripA >> value;
dripA = value;
eeDiffB >> value;
diffB = value;
eeKillB >> value;
killB = value;
eeDripB >> value;
dripB = value;
}
void saveParameters() {
float value;
value = static_cast<float>(diffA);
eeDiffA << value;
value = static_cast<float>(feedA);
eeFeedA << value;
value = static_cast<float>(dripA);
eeDripA << value;
value = static_cast<float>(diffB);
eeDiffB << value;
value = static_cast<float>(killB);
eeKillB << value;
value = static_cast<float>(dripB);
eeDripB << value;
}
void initCells(uint8_t idx, uint8_t seedCount = 2) {
for (uint8_t i = 0; i < SIM_DIM; i++) {
for (uint8_t j = 0; j < SIM_DIM; j++) {
cellsA[idx][i][j] = 1.0;
cellsB[idx][i][j] = 0.0;
}
}
while (seedCount > 0) {
int si = random(0, SIM_DIM);
int sj = random(0, SIM_DIM);
if (cellsB[idx][si][sj] != 0.0) continue;
Serial.print(F("Seed ")); Serial.print(si); Serial.print(' '); Serial.println(sj);
cellsA[idx][si][sj] = 0.0;
cellsB[idx][si][sj] = 1.0;
seedCount -= 1;
}
}
void dripCells(uint8_t idx) {
if (float(random(0,1024)) / 1023.0 < dripA) {
int di = random(0, SIM_DIM);
int dj = random(0, SIM_DIM);
Serial.print(F("Drip A: ")); Serial.print(di); Serial.print(' '); Serial.println(dj);
cellsA[idx][di][dj] = 1.0;
}
if (float(random(0,1024)) / 1023.0 < dripB) {
int di = random(0, SIM_DIM);
int dj = random(0, SIM_DIM);
Serial.print(F("Drip B: ")); Serial.print(di); Serial.print(' '); Serial.println(dj);
cellsB[idx][di][dj] = 1.0;
}
}
void showCells(uint8_t idx) {
drawPixels(cellsA[idx], cellsB[idx]);
}
//fixed weights[][3] = {{.05, .2, .05}, {.2, 0.0, .2}, {.05, .2, .05}};
const fixed weights[][3] = {{.7, 1., .7}, {1., 0.0, 1.}, {.7, 1., .7}};
fixed laplacian(fixed cells[SIM_DIM][SIM_DIM], uint8_t i, uint8_t j) {
fixed sum = 0.0;
fixed weightSum = 0.0;
for (uint8_t ii = 0; ii < 3; ii++) {
#ifdef WRAP_I
uint8_t ci = (SIM_DIM + i + ii - 1) % SIM_DIM;
#else
int8_t ci = i + ii - 1;
if (ci < 0 || ci >= SIM_DIM) continue;
#endif
for (uint8_t jj = 0; jj < 3; jj++) {
if (ii == 0 && jj == 0) continue;
#ifdef WRAP_J
uint8_t cj = (SIM_DIM + j + jj - 1) % SIM_DIM;
#else
int8_t cj = j + jj - 1;
if (cj < 0 || cj >= SIM_DIM) continue;
#endif
fixed w = weights[ii][jj];
weightSum += w;
sum += w * cells[ci][cj];
}
}
return sum / weightSum - cells[i][j];
}
unsigned long last_time;
uint8_t cur;
void initSimulation() {
Serial.println(F("Init"));
loadParameters();
resetSimulation();
}
void loopSimulation(unsigned long now) {
if (now - last_time >= SIM_STEP) {
uint8_t nxt = (cur + 1) % 2;
fixed dt = float(now - last_time) / 1000.0;
for (uint8_t i = 0; i < SIM_DIM; i++) {
for (uint8_t j = 0; j < SIM_DIM; j++) {
fixed L2A = laplacian(cellsA[cur], i, j);
fixed L2B = laplacian(cellsB[cur], i, j);
fixed A = cellsA[cur][i][j];
fixed B = cellsB[cur][i][j];
cellsA[nxt][i][j] = constrain(A + (diffA * L2A - A*B*B + feedA * (1.0 - A)) * dt, 0.0, 1.0);
cellsB[nxt][i][j] = constrain(B + (diffB * L2B + A*B*B - (killB + feedA) * B) * dt, 0.0, 1.0);
}
}
dripCells(nxt);
showCells(nxt);
last_time = now;
cur = nxt;
}
}
void resetSimulation() {
Serial.println(F("Reset"));
cur = 0;
initCells(cur, random(1,4));
showCells(cur);
}