-
Notifications
You must be signed in to change notification settings - Fork 0
/
libgrayscott.cc
277 lines (232 loc) · 8.4 KB
/
libgrayscott.cc
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// g++ -shared -o libgrayscott.so libgrayscott.cc `pkg-config --libs --cflags python` -lboost_python -O3 -fPIC
// if numpy is pip installed : -I /usr/local/lib/python2.7/dist-packages/numpy/core/include
#include <boost/python/extract.hpp>
#include <boost/python/numeric.hpp>
#include <boost/python.hpp>
#include <numpy/arrayobject.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
class GrayScott {
private:
std::string param_name;
unsigned int width, height;
double d, dt;
double k, F;
double h,Du, Dv, noise;
double* ut_1, *vt_1, *lut_1, *lvt_1, *ut, *vt;
void compute_laplacian(double* values, double* laplacian) {
// For efficiently computing the laplacian we will move 5 pointers on values
// pointing : on the pixel, on its north, on its east, on its west, on its south
double* v_ptr, *vN_ptr, *vE_ptr, *vS_ptr, *vW_ptr;
double* l_ptr;
// We handle the corners
// #oooooo#
// oooooooo
// oooooooo
// oooooooo
// #oooooo#
laplacian[0] = -(4 * values[0]) + (values[(height-1)*width] + values[1] + values[width] + values[(width-1)]);// top left
laplacian[width-1] = -(4 * values[width-1]) + (values[(height-1)*width + (width-1)] + values[0] + values[width + (width-1)] + values[width-2]); // top right
laplacian[(height-1)*width] = -(4 * values[(height-1)*width]) + (values[(height-2)*width] + values[(height-1)*width + 1] + values[0] + values[(height-1)*width + (width-1)]); // bottom left
laplacian[(height-1)*width + (width-1)] = -(4 * values[(height-1)*width + (width-1)]) + (values[(height-2)*width + (width-1)]+values[(height-1)*width] + values[width-1] + values[(height-1)*width + (width-2)]); // bottom right
// We handle the borders
// o########o
// oooooooooo
// oooooooooo
// oooooooooo
// oooooooooo
v_ptr = values + 1;
vN_ptr = values + (height-1)*width + 1;
vE_ptr = values + 2;
vS_ptr = values + width + 1;
vW_ptr = values;
l_ptr = laplacian + 1;
for(unsigned int i = 1 ; i < width-1 ; ++i, ++v_ptr, ++vN_ptr, ++vE_ptr, ++vS_ptr, ++vW_ptr, ++l_ptr)
*l_ptr = (*vN_ptr + *vE_ptr + *vS_ptr + *vW_ptr) - ((*v_ptr) * 4);
// oooooooooo
// oooooooooo
// oooooooooo
// oooooooooo
// o########o
v_ptr = values + (height-1)*width + 1;
vN_ptr = values + (height-2)*width + 1;
vE_ptr = values + (height-1)*width + 2;
vS_ptr = values + 1 ;
vW_ptr = values + (height-1)*width;
l_ptr = laplacian + (height-1)*width + 1;
for(unsigned int i = 1 ; i < width-1 ; ++i, ++v_ptr, ++vN_ptr, ++vE_ptr, ++vS_ptr, ++vW_ptr, ++l_ptr)
*l_ptr = (*vN_ptr + *vE_ptr + *vS_ptr + *vW_ptr) - ((*v_ptr) * 4);
// oooooooooo
// #ooooooooo
// #ooooooooo
// #ooooooooo
// #ooooooooo
// oooooooooo
v_ptr = values + width;
vN_ptr = values ;
vE_ptr = values + width + 1;
vS_ptr = values + 2*width ;
vW_ptr = values + width + (width-1);
l_ptr = laplacian + width;
for(unsigned int i = 1 ; i < height-1 ; ++i, v_ptr+=width, vN_ptr+=width, vE_ptr+=width, vS_ptr+=width, vW_ptr+=width, l_ptr+=width)
*l_ptr = (*vN_ptr + *vE_ptr + *vS_ptr + *vW_ptr) - ((*v_ptr) * 4);
// oooooooooo
// ooooooooo#
// ooooooooo#
// ooooooooo#
// ooooooooo#
// oooooooooo
//
v_ptr = values + width + (width-1);
vN_ptr = values + (width-1);
vE_ptr = values + width;
vS_ptr = values + 2*width + (width-1) ;
vW_ptr = values + width + (width-2);
l_ptr = laplacian + width + (width-1);
for(unsigned int i = 1 ; i < height-1 ; ++i, v_ptr+=width, vN_ptr+=width, vE_ptr+=width, vS_ptr+=width, vW_ptr+=width, l_ptr+=width)
*l_ptr = (*vN_ptr + *vE_ptr + *vS_ptr + *vW_ptr) - ((*v_ptr) * 4);
// We handle the region inside the array exlucding a border of size 1,
// i.e. the pixels # below
// oooooooooo
// o########o
// o########o
// o########o
// oooooooooo
v_ptr = values + (1*width + 1);
vN_ptr = values + 1;
vE_ptr = values + (1*width + 2);
vS_ptr = values + (2*width + 1);
vW_ptr = values + (1*width + 0);
l_ptr = laplacian + (1*width + 1);
for(unsigned int i = 1; i < height-1; ++i) {
for(unsigned int j = 1 ; j < width-1 ; ++j, ++vN_ptr, ++vE_ptr, ++vS_ptr, ++vW_ptr, ++v_ptr, ++l_ptr)
*l_ptr = (*vN_ptr + *vE_ptr + *vS_ptr + *vW_ptr) - ((*v_ptr) * 4);
// For switching to the next line we must move the pointers forward by 2 pixels
v_ptr += 2;
l_ptr += 2;
vN_ptr += 2;
vE_ptr += 2;
vS_ptr += 2;
vW_ptr += 2;
}
}
public:
GrayScott(std::string param_name, unsigned int width, unsigned int height, double d, double dt) : param_name(param_name), width(width), height(height), dt(dt) {
if(param_name == std::string("solitons")) {
k = 0.056;
F = 0.020;
}
else if(param_name == std::string("worms")) {
k = 0.0630;
F = 0.0580;
}
else if(param_name == std::string("spirals")) {
k = 0.050;
F = 0.018;
}
else if(param_name == std::string("uskate")) {
k = 0.06093;
F = 0.0620;
}
else {
k = 0.040;
F = 0.060;
}
h = d / (double)width;
Du = 2.0 * 1e-5 / (h*h);
Dv = 1.0 * 1e-5 / (h*h);
noise = 0.2;
ut_1 = new double[width*height];
std::fill(ut_1, ut_1 + width*height, 0.0);
vt_1 = new double[width*height];
std::fill(vt_1, vt_1 + width*height, 0.0);
lut_1 = new double[width*height];
std::fill(lut_1, lut_1 + width*height, 0.0);
lvt_1 = new double[width*height];
std::fill(lvt_1, lvt_1 + width*height, 0.0);
ut = new double[width*height];
std::fill(ut, ut + width*height, 0.0);
vt = new double[width*height];
std::fill(vt, vt + width*height, 0.0);
}
~GrayScott(void) {
delete[] ut_1;
delete[] vt_1;
delete[] lut_1;
delete[] lvt_1;
delete[] ut;
delete[] vt;
}
void init(void) {
double *ut_1_ptr, *vt_1_ptr;
std::fill(ut_1, ut_1 + width*height, 1.0);
std::fill(vt_1, vt_1 + width*height, 0.0);
int dN = std::min(width, height)/4;
for(int i = -dN/2 ; i <= dN/2 ; ++i) {
for(int j = -dN/2 ; j <= dN/2; ++j) {
ut_1[(height/2+i)*width + (width/2+j)] = 0.5;
vt_1[(height/2+i)*width + (width/2+j)] = 0.25;
}
}
ut_1_ptr = ut_1;
vt_1_ptr = vt_1;
for(unsigned int i = 0 ; i < width*height; ++i, ++ut_1_ptr, ++vt_1_ptr) {
*ut_1_ptr += noise * (2.0 * (std::rand() / double(RAND_MAX)) - 1.);
if(*ut_1_ptr <= 0.)
*ut_1_ptr = 0.;
*vt_1_ptr += noise * (2.0 * (std::rand() / double(RAND_MAX)) - 1.);
if(*vt_1_ptr <= 0.)
*vt_1_ptr = 0.;
}
}
void step(void) {
// The GrayScott equations read :
// du/dt = Du Laplacian(u) - u * v^2 + F * (1 - u)
// dv/dt = Dv Laplacian(v) + u * v^2 - (F + k) * v
//
// Discretized in time with Forward Euler :
// u(t+dt) = u(t) + dt * (Du Laplacian(u(t)) - u(t) * v(t)^2 + F * (1 - u(t))
// v(t+dt) = v(t) + dt * (Dv Laplacian(v(t)) + u(t) * v(t)^2 - (F + k) * v(t))
//
//
//
compute_laplacian(ut_1, lut_1);
compute_laplacian(vt_1, lvt_1);
double* ut_1_ptr = ut_1;
double* vt_1_ptr = vt_1;
double* ut_ptr = ut;
double* vt_ptr = vt;
double* lut_1_ptr = lut_1;
double* lvt_1_ptr = lvt_1;
//std::cout << dt << "," << Du << "," << Dv << "," << std::endl;
for(unsigned int i = 0 ; i < width*height ; ++i, ++ut_1_ptr, ++vt_1_ptr, ++ut_ptr, ++vt_ptr, ++lut_1_ptr, ++lvt_1_ptr) {
double uvv = (*ut_1_ptr) * (*vt_1_ptr) * (*vt_1_ptr);
*ut_ptr = *ut_1_ptr + dt * ( Du * (*lut_1_ptr) - uvv + F * (1.0 - (*ut_1_ptr)));
*vt_ptr = *vt_1_ptr + dt * ( Dv * (*lvt_1_ptr) + uvv - (F + k) * (*vt_1_ptr));
}
double* tmp;
tmp = ut;
ut = ut_1;
ut_1 = tmp;
tmp = vt;
vt = vt_1;
vt_1 = tmp;
}
PyObject* get_ut(void) {
npy_intp dims[2];
dims[0] = height;
dims[1] = width;
return PyArray_SimpleNewFromData( 2, dims, NPY_DOUBLE, ut );
}
};
BOOST_PYTHON_MODULE(libgrayscott) {
// tell Boost::Python under what name the array is known
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
// initialize the Numpy C API
import_array();
boost::python::class_<GrayScott>("GrayScott", boost::python::init<std::string, unsigned int, unsigned int, double, double>())
.def("init", &GrayScott::init)
.def("step", &GrayScott::step)
.def("get_ut", &GrayScott::get_ut);
}