44 bool GameStep(vector<bool> *
b[],
int nx,
int ny);
50 bool InitMFEM(vector<bool> &
b,
int nx,
int ny);
52 int main(
int argc,
char *argv[])
62 bool visualization = 1;
65 args.
AddOption(&nx,
"-nx",
"--num-elems-x",
66 "Number of elements in the x direction.");
67 args.
AddOption(&ny,
"-ny",
"--num-elems-y",
68 "Number of elements in the y direction.");
69 args.
AddOption(&r,
"-r",
"--random-fraction",
70 "Fraction of randomly chosen live cells.");
71 args.
AddOption(&rs,
"-rs",
"--random-seed",
72 "Seed for the random number generator.");
73 args.
AddOption(&sketch_pad_params,
"-sp",
"--sketch-pad",
74 "Specify the starting coordinates and values on a grid" 75 " of cells. The values can be 0, 1, or 2. Where 0 and 1" 76 " indicate cells that are off or on and 2 represents a" 77 " newline character.");
78 args.
AddOption(&blinker_params,
"-b",
"--blinker",
79 "Specify the starting coordinates and orientation (0 or 1)" 80 " of the blinker. Multiple blinkers can be specified as " 81 "'x0 y0 o0 x1 y1 o1 ...'.");
82 args.
AddOption(&glider_params,
"-g",
"--glider",
83 "Specify the starting coordinates and " 84 "orientation (0,1,2, or 3) of the glider. " 85 "Multiple gliders can be specified as " 86 "'x0 y0 o0 x1 y1 o1 ...'.");
87 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
89 "Enable or disable GLVis visualization.");
99 Mesh mesh = Mesh::MakeCartesian2D(nx, ny, Element::QUADRILATERAL, 0, nx, ny,
112 vector<bool> * vbp[2];
113 vector<bool> vb0(len);
114 vector<bool> vb1(len);
125 seed = (
unsigned int)rand();
129 seed = (
unsigned int)rs;
131 cout <<
"Using random seed: " << seed << endl;
138 for (
int i=0; i<len; i++)
140 double rv = double(rand()) / RAND_MAX;
147 for (
int i=0; i<len; i++)
152 if ( sketch_pad_params.
Size() > 2 )
156 if ( blinker_params.
Size() > 0 && (blinker_params.
Size() % 3 == 0 ) )
160 if ( glider_params.
Size() > 0 && (glider_params.
Size() % 3 == 0 ) )
162 init =
InitGlider(vb0, nx, ny, glider_params);
186 cout << endl <<
"Running the Game of Life..." << flush;
189 bool is_stable =
false;
190 while ( is_good && visualization && !is_stable )
196 std::swap(vbp[0], vbp[1]);
199 is_good = sol_sock.good();
201 if (visualization && is_good )
203 sol_sock <<
"solution\n" << mesh << x << flush;
208 sol_sock <<
"keys Ajlm\n";
209 sol_sock <<
"view 0 0\n";
210 sol_sock <<
"zoom 1.9\n";
211 sol_sock <<
"palette 24\n";
216 sol_sock <<
"valuerange 0 1\n";
221 cout <<
"done." << endl;
225 ofstream mesh_ofs(
"life.mesh");
226 mesh_ofs.precision(8);
227 mesh.
Print(mesh_ofs);
228 ofstream sol_ofs(
"life.gf");
229 sol_ofs.precision(8);
235 inline int index(
int i,
int j,
int nx,
int ny)
237 return ((j + ny) % ny) * nx + ((i + nx) % nx);
242 bool is_stable =
true;
243 for (
int j=0; j<ny; j++)
245 for (
int i=0; i<nx; i++)
248 (int)(*
b[0])[
index(i+0,j+0,nx,ny)] +
249 (int)(*
b[0])[
index(i+1,j+0,nx,ny)] +
250 (int)(*
b[0])[
index(i+1,j+1,nx,ny)] +
251 (int)(*
b[0])[
index(i+0,j+1,nx,ny)] +
252 (int)(*
b[0])[
index(i-1,j+1,nx,ny)] +
253 (int)(*
b[0])[
index(i-1,j+0,nx,ny)] +
254 (int)(*
b[0])[
index(i-1,j-1,nx,ny)] +
255 (int)(*
b[0])[
index(i+0,j-1,nx,ny)] +
256 (int)(*
b[0])[
index(i+1,j-1,nx,ny)];
260 (*
b[1])[
index(i,j,nx,ny)] =
true;
266 (*
b[1])[
index(i,j,nx,ny)] =
false;
269 is_stable &= (*
b[1])[
index(i,j,nx,ny)] == (*
b[0])[
index(i,j,nx,ny)];
277 for (
int i=0; i<n; i++)
285 for (
int i=0; i<params.
Size()/3; i++)
287 int cx = params[3 * i + 0];
288 int cy = params[3 * i + 1];
289 int ornt = params[3 * i + 2];
294 b[
index(cx+0,cy+1,nx,ny)] =
true;
295 b[
index(cx+0,cy+0,nx,ny)] =
true;
296 b[
index(cx+0,cy-1,nx,ny)] =
true;
299 b[
index(cx+1,cy+0,nx,ny)] =
true;
300 b[
index(cx+0,cy+0,nx,ny)] =
true;
301 b[
index(cx-1,cy+0,nx,ny)] =
true;
310 for (
int i=0; i<params.
Size()/3; i++)
312 int cx = params[3 * i + 0];
313 int cy = params[3 * i + 1];
314 int ornt = params[3 * i + 2];
319 b[
index(cx-1,cy+0,nx,ny)] =
true;
320 b[
index(cx+0,cy+1,nx,ny)] =
true;
321 b[
index(cx+1,cy-1,nx,ny)] =
true;
322 b[
index(cx+1,cy+0,nx,ny)] =
true;
323 b[
index(cx+1,cy+1,nx,ny)] =
true;
326 b[
index(cx+0,cy-1,nx,ny)] =
true;
327 b[
index(cx-1,cy+0,nx,ny)] =
true;
328 b[
index(cx-1,cy+1,nx,ny)] =
true;
329 b[
index(cx+0,cy+1,nx,ny)] =
true;
330 b[
index(cx+1,cy+1,nx,ny)] =
true;
333 b[
index(cx+1,cy+0,nx,ny)] =
true;
334 b[
index(cx+0,cy-1,nx,ny)] =
true;
335 b[
index(cx-1,cy-1,nx,ny)] =
true;
336 b[
index(cx-1,cy+0,nx,ny)] =
true;
337 b[
index(cx-1,cy+1,nx,ny)] =
true;
340 b[
index(cx+0,cy+1,nx,ny)] =
true;
341 b[
index(cx+1,cy+0,nx,ny)] =
true;
342 b[
index(cx-1,cy-1,nx,ny)] =
true;
343 b[
index(cx+0,cy-1,nx,ny)] =
true;
344 b[
index(cx+1,cy-1,nx,ny)] =
true;
359 for (
int i=2; i<params.
Size(); i++)
361 if ( params[i]/2 == 1 )
368 b[
index(cx+ox,cy+oy,nx,ny)] = (bool)params[i];
379 int wx = (nx >= 23) ? 23 : 5;
380 int hy = (ny >= 7) ? 7 : 5;
388 for (
int j=0; j<hy; j++)
390 b[
index(ox + 0, oy+j,nx,ny)] =
true;
391 b[
index(ox + 4, oy+j,nx,ny)] =
true;
392 b[
index(ox + 6, oy+j,nx,ny)] =
true;
393 b[
index(ox + 12, oy+j,nx,ny)] =
true;
394 b[
index(ox + 18, oy+j,nx,ny)] =
true;
395 b[
index(ox + 22, oy+j,nx,ny)] =
true;
397 for (
int i=1; i<5; i++)
399 b[
index(ox + 6 + i, oy + hy - 1,nx,ny)] =
true;
400 b[
index(ox + 12 + i, oy + 0,nx,ny)] =
true;
401 b[
index(ox + 12 + i, oy + hy - 1,nx,ny)] =
true;
403 for (
int i=1; i<4; i++)
405 b[
index(ox + 6 + i, oy + hy/2,nx,ny)] =
true;
406 b[
index(ox + 12 + i, oy + hy/2,nx,ny)] =
true;
408 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
409 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
410 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
412 b[
index(ox + 19, oy + hy - 2,nx,ny)] =
true;
413 b[
index(ox + 20, oy + hy - 3,nx,ny)] =
true;
414 b[
index(ox + 21, oy + hy - 2,nx,ny)] =
true;
422 for (
int j=0; j<hy; j++)
424 b[
index(ox + 0, oy+j,nx,ny)] =
true;
425 b[
index(ox + 4, oy+j,nx,ny)] =
true;
427 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
428 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
429 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
434 b[
index(nx/2,ny/2,nx,ny)] =
true;
Class for grid function - Vector with associated FE space.
void ProjectStep(const vector< bool > &b, GridFunction &x, int n)
void PrintOptions(std::ostream &out) const
Print the options.
void PrintUsage(std::ostream &out) const
Print the usage message.
bool Good() const
Return true if the command line options were parsed successfully.
int main(int argc, char *argv[])
bool InitSketchPad(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)
bool GameStep(vector< bool > *b[], int nx, int ny)
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
bool InitMFEM(vector< bool > &b, int nx, int ny)
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)
Add a boolean option and set 'var' to receive the value. Enable/disable tags are used to set the bool...
int index(int i, int j, int nx, int ny)
int open(const char hostname[], int port)
Open the socket stream on 'port' at 'hostname'.
int Size() const
Return the logical size of the array.
virtual void Print(std::ostream &os=mfem::out) const
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Arbitrary order "L2-conforming" discontinuous finite elements.