46 bool GameStep(vector<bool> *
b[],
int nx,
int ny);
52 bool InitMFEM(vector<bool> &
b,
int nx,
int ny);
54 int main(
int argc,
char *argv[])
64 bool visualization = 1;
67 args.
AddOption(&nx,
"-nx",
"--num-elems-x",
68 "Number of elements in the x direction.");
69 args.
AddOption(&ny,
"-ny",
"--num-elems-y",
70 "Number of elements in the y direction.");
71 args.
AddOption(&r,
"-r",
"--random-fraction",
72 "Fraction of randomly chosen live cells.");
73 args.
AddOption(&rs,
"-rs",
"--random-seed",
74 "Seed for the random number generator.");
75 args.
AddOption(&sketch_pad_params,
"-sp",
"--sketch-pad",
76 "Specify the starting coordinates and values on a grid"
77 " of cells. The values can be 0, 1, or 2. Where 0 and 1"
78 " indicate cells that are off or on and 2 represents a"
79 " newline character.");
80 args.
AddOption(&blinker_params,
"-b",
"--blinker",
81 "Specify the starting coordinates and orientation (0 or 1)"
82 " of the blinker. Multiple blinkers can be specified as "
83 "'x0 y0 o0 x1 y1 o1 ...'.");
84 args.
AddOption(&glider_params,
"-g",
"--glider",
85 "Specify the starting coordinates and "
86 "orientation (0,1,2, or 3) of the glider. "
87 "Multiple gliders can be specified as "
88 "'x0 y0 o0 x1 y1 o1 ...'.");
89 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
91 "Enable or disable GLVis visualization.");
101 Mesh *mesh =
new Mesh(nx, ny, Element::QUADRILATERAL, 0, nx, ny,
false);
113 vector<bool> * vbp[2];
114 vector<bool> vb0(len);
115 vector<bool> vb1(len);
126 seed = (
unsigned int)rand();
130 seed = (
unsigned int)rs;
132 cout <<
"Using random seed: " << seed << endl;
139 for (
int i=0; i<len; i++)
141 double rv = double(rand()) / RAND_MAX;
148 for (
int i=0; i<len; i++)
153 if ( sketch_pad_params.
Size() > 2 )
157 if ( blinker_params.
Size() > 0 && (blinker_params.
Size() % 3 == 0 ) )
161 if ( glider_params.
Size() > 0 && (glider_params.
Size() % 3 == 0 ) )
163 init =
InitGlider(vb0, nx, ny, glider_params);
181 char vishost[] =
"localhost";
183 sol_sock.
open(vishost, visport);
187 cout << endl <<
"Running the Game of Life..." << flush;
190 bool is_stable =
false;
191 while ( is_good && visualization && !is_stable )
197 std::swap(vbp[0], vbp[1]);
200 is_good = sol_sock.good();
202 if (visualization && is_good )
204 sol_sock <<
"solution\n" << *mesh << x << flush;
209 sol_sock <<
"keys Ajlm\n";
210 sol_sock <<
"view 0 0\n";
211 sol_sock <<
"zoom 1.9\n";
212 sol_sock <<
"palette 24\n";
217 sol_sock <<
"valuerange 0 1\n";
222 cout <<
"done." << endl;
226 ofstream mesh_ofs(
"life.mesh");
227 mesh_ofs.precision(8);
228 mesh->
Print(mesh_ofs);
229 ofstream sol_ofs(
"life.gf");
230 sol_ofs.precision(8);
241 inline int index(
int i,
int j,
int nx,
int ny)
243 return ((j + ny) % ny) * nx + ((i + nx) % nx);
248 bool is_stable =
true;
249 for (
int j=0; j<ny; j++)
251 for (
int i=0; i<nx; i++)
254 (int)(*b[0])[
index(i+0,j+0,nx,ny)] +
255 (int)(*b[0])[
index(i+1,j+0,nx,ny)] +
256 (int)(*b[0])[
index(i+1,j+1,nx,ny)] +
257 (int)(*b[0])[
index(i+0,j+1,nx,ny)] +
258 (int)(*b[0])[
index(i-1,j+1,nx,ny)] +
259 (int)(*b[0])[
index(i-1,j+0,nx,ny)] +
260 (int)(*b[0])[
index(i-1,j-1,nx,ny)] +
261 (int)(*b[0])[
index(i+0,j-1,nx,ny)] +
262 (int)(*b[0])[
index(i+1,j-1,nx,ny)];
266 (*b[1])[
index(i,j,nx,ny)] =
true;
269 (*b[1])[
index(i,j,nx,ny)] = (*b[0])[
index(i,j,nx,ny)];
272 (*b[1])[
index(i,j,nx,ny)] =
false;
275 is_stable &= (*b[1])[
index(i,j,nx,ny)] == (*b[0])[
index(i,j,nx,ny)];
283 for (
int i=0; i<n; i++)
291 for (
int i=0; i<params.
Size()/3; i++)
293 int cx = params[3 * i + 0];
294 int cy = params[3 * i + 1];
295 int ornt = params[3 * i + 2];
300 b[
index(cx+0,cy+1,nx,ny)] =
true;
301 b[
index(cx+0,cy+0,nx,ny)] =
true;
302 b[
index(cx+0,cy-1,nx,ny)] =
true;
305 b[
index(cx+1,cy+0,nx,ny)] =
true;
306 b[
index(cx+0,cy+0,nx,ny)] =
true;
307 b[
index(cx-1,cy+0,nx,ny)] =
true;
316 for (
int i=0; i<params.
Size()/3; i++)
318 int cx = params[3 * i + 0];
319 int cy = params[3 * i + 1];
320 int ornt = params[3 * i + 2];
325 b[
index(cx-1,cy+0,nx,ny)] =
true;
326 b[
index(cx+0,cy+1,nx,ny)] =
true;
327 b[
index(cx+1,cy-1,nx,ny)] =
true;
328 b[
index(cx+1,cy+0,nx,ny)] =
true;
329 b[
index(cx+1,cy+1,nx,ny)] =
true;
332 b[
index(cx+0,cy-1,nx,ny)] =
true;
333 b[
index(cx-1,cy+0,nx,ny)] =
true;
334 b[
index(cx-1,cy+1,nx,ny)] =
true;
335 b[
index(cx+0,cy+1,nx,ny)] =
true;
336 b[
index(cx+1,cy+1,nx,ny)] =
true;
339 b[
index(cx+1,cy+0,nx,ny)] =
true;
340 b[
index(cx+0,cy-1,nx,ny)] =
true;
341 b[
index(cx-1,cy-1,nx,ny)] =
true;
342 b[
index(cx-1,cy+0,nx,ny)] =
true;
343 b[
index(cx-1,cy+1,nx,ny)] =
true;
346 b[
index(cx+0,cy+1,nx,ny)] =
true;
347 b[
index(cx+1,cy+0,nx,ny)] =
true;
348 b[
index(cx-1,cy-1,nx,ny)] =
true;
349 b[
index(cx+0,cy-1,nx,ny)] =
true;
350 b[
index(cx+1,cy-1,nx,ny)] =
true;
365 for (
int i=2; i<params.
Size(); i++)
367 if ( params[i]/2 == 1 )
374 b[
index(cx+ox,cy+oy,nx,ny)] = (bool)params[i];
385 int wx = (nx >= 23) ? 23 : 5;
386 int hy = (ny >= 7) ? 7 : 5;
394 for (
int j=0; j<hy; j++)
396 b[
index(ox + 0, oy+j,nx,ny)] =
true;
397 b[
index(ox + 4, oy+j,nx,ny)] =
true;
398 b[
index(ox + 6, oy+j,nx,ny)] =
true;
399 b[
index(ox + 12, oy+j,nx,ny)] =
true;
400 b[
index(ox + 18, oy+j,nx,ny)] =
true;
401 b[
index(ox + 22, oy+j,nx,ny)] =
true;
403 for (
int i=1; i<5; i++)
405 b[
index(ox + 6 + i, oy + hy - 1,nx,ny)] =
true;
406 b[
index(ox + 12 + i, oy + 0,nx,ny)] =
true;
407 b[
index(ox + 12 + i, oy + hy - 1,nx,ny)] =
true;
409 for (
int i=1; i<4; i++)
411 b[
index(ox + 6 + i, oy + hy/2,nx,ny)] =
true;
412 b[
index(ox + 12 + i, oy + hy/2,nx,ny)] =
true;
414 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
415 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
416 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
418 b[
index(ox + 19, oy + hy - 2,nx,ny)] =
true;
419 b[
index(ox + 20, oy + hy - 3,nx,ny)] =
true;
420 b[
index(ox + 21, oy + hy - 2,nx,ny)] =
true;
428 for (
int j=0; j<hy; j++)
430 b[
index(ox + 0, oy+j,nx,ny)] =
true;
431 b[
index(ox + 4, oy+j,nx,ny)] =
true;
433 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
434 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
435 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
440 b[
index(nx/2,ny/2,nx,ny)] =
true;
int Size() const
Logical size of the array.
virtual void Print(std::ostream &out=mfem::out) const
Class for grid function - Vector with associated FE space.
int main(int argc, char *argv[])
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
bool InitSketchPad(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)
bool GameStep(vector< bool > *b[], int nx, int ny)
bool InitMFEM(vector< bool > &b, int nx, int ny)
void PrintUsage(std::ostream &out) const
bool InitBlinker(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
bool InitGlider(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
void ProjectStep(const vector< bool > &b, GridFunction &x, int ns, int s)
int index(int i, int j, int nx, int ny)
void PrintOptions(std::ostream &out) const
int open(const char hostname[], int port)
Arbitrary order "L2-conforming" discontinuous finite elements.