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 = Mesh::MakeCartesian2D(nx, ny, Element::QUADRILATERAL, 0, nx, ny,
114 vector<bool> * vbp[2];
115 vector<bool> vb0(len);
116 vector<bool> vb1(len);
127 seed = (
unsigned int)rand();
131 seed = (
unsigned int)rs;
133 cout <<
"Using random seed: " << seed << endl;
140 for (
int i=0; i<len; i++)
142 double rv = double(rand()) / RAND_MAX;
149 for (
int i=0; i<len; i++)
154 if ( sketch_pad_params.
Size() > 2 )
158 if ( blinker_params.
Size() > 0 && (blinker_params.
Size() % 3 == 0 ) )
162 if ( glider_params.
Size() > 0 && (glider_params.
Size() % 3 == 0 ) )
164 init =
InitGlider(vb0, nx, ny, glider_params);
184 sol_sock.
open(vishost, visport);
188 cout << endl <<
"Running the Game of Life..." << flush;
191 bool is_stable =
false;
192 while ( is_good && visualization && !is_stable )
198 std::swap(vbp[0], vbp[1]);
201 is_good = sol_sock.good();
203 if (visualization && is_good )
205 sol_sock <<
"solution\n" << mesh << x << flush;
210 sol_sock <<
"keys Ajlm\n";
211 sol_sock <<
"view 0 0\n";
212 sol_sock <<
"zoom 1.9\n";
213 sol_sock <<
"palette 24\n";
218 sol_sock <<
"valuerange 0 1\n";
223 cout <<
"done." << endl;
227 ofstream mesh_ofs(
"life.mesh");
228 mesh_ofs.precision(8);
229 mesh.
Print(mesh_ofs);
230 ofstream sol_ofs(
"life.gf");
231 sol_ofs.precision(8);
237 inline int index(
int i,
int j,
int nx,
int ny)
239 return ((j + ny) % ny) * nx + ((i + nx) % nx);
244 bool is_stable =
true;
245 for (
int j=0; j<ny; j++)
247 for (
int i=0; i<nx; i++)
250 (int)(*b[0])[
index(i+0,j+0,nx,ny)] +
251 (int)(*b[0])[
index(i+1,j+0,nx,ny)] +
252 (int)(*b[0])[
index(i+1,j+1,nx,ny)] +
253 (int)(*b[0])[
index(i+0,j+1,nx,ny)] +
254 (int)(*b[0])[
index(i-1,j+1,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)];
262 (*b[1])[
index(i,j,nx,ny)] =
true;
265 (*b[1])[
index(i,j,nx,ny)] = (*b[0])[
index(i,j,nx,ny)];
268 (*b[1])[
index(i,j,nx,ny)] =
false;
271 is_stable &= (*b[1])[
index(i,j,nx,ny)] == (*b[0])[
index(i,j,nx,ny)];
279 for (
int i=0; i<n; i++)
287 for (
int i=0; i<params.
Size()/3; i++)
289 int cx = params[3 * i + 0];
290 int cy = params[3 * i + 1];
291 int ornt = params[3 * i + 2];
296 b[
index(cx+0,cy+1,nx,ny)] =
true;
297 b[
index(cx+0,cy+0,nx,ny)] =
true;
298 b[
index(cx+0,cy-1,nx,ny)] =
true;
301 b[
index(cx+1,cy+0,nx,ny)] =
true;
302 b[
index(cx+0,cy+0,nx,ny)] =
true;
303 b[
index(cx-1,cy+0,nx,ny)] =
true;
312 for (
int i=0; i<params.
Size()/3; i++)
314 int cx = params[3 * i + 0];
315 int cy = params[3 * i + 1];
316 int ornt = params[3 * i + 2];
321 b[
index(cx-1,cy+0,nx,ny)] =
true;
322 b[
index(cx+0,cy+1,nx,ny)] =
true;
323 b[
index(cx+1,cy-1,nx,ny)] =
true;
324 b[
index(cx+1,cy+0,nx,ny)] =
true;
325 b[
index(cx+1,cy+1,nx,ny)] =
true;
328 b[
index(cx+0,cy-1,nx,ny)] =
true;
329 b[
index(cx-1,cy+0,nx,ny)] =
true;
330 b[
index(cx-1,cy+1,nx,ny)] =
true;
331 b[
index(cx+0,cy+1,nx,ny)] =
true;
332 b[
index(cx+1,cy+1,nx,ny)] =
true;
335 b[
index(cx+1,cy+0,nx,ny)] =
true;
336 b[
index(cx+0,cy-1,nx,ny)] =
true;
337 b[
index(cx-1,cy-1,nx,ny)] =
true;
338 b[
index(cx-1,cy+0,nx,ny)] =
true;
339 b[
index(cx-1,cy+1,nx,ny)] =
true;
342 b[
index(cx+0,cy+1,nx,ny)] =
true;
343 b[
index(cx+1,cy+0,nx,ny)] =
true;
344 b[
index(cx-1,cy-1,nx,ny)] =
true;
345 b[
index(cx+0,cy-1,nx,ny)] =
true;
346 b[
index(cx+1,cy-1,nx,ny)] =
true;
361 for (
int i=2; i<params.
Size(); i++)
363 if ( params[i]/2 == 1 )
370 b[
index(cx+ox,cy+oy,nx,ny)] = (bool)params[i];
381 int wx = (nx >= 23) ? 23 : 5;
382 int hy = (ny >= 7) ? 7 : 5;
390 for (
int j=0; j<hy; j++)
392 b[
index(ox + 0, oy+j,nx,ny)] =
true;
393 b[
index(ox + 4, oy+j,nx,ny)] =
true;
394 b[
index(ox + 6, oy+j,nx,ny)] =
true;
395 b[
index(ox + 12, oy+j,nx,ny)] =
true;
396 b[
index(ox + 18, oy+j,nx,ny)] =
true;
397 b[
index(ox + 22, oy+j,nx,ny)] =
true;
399 for (
int i=1; i<5; i++)
401 b[
index(ox + 6 + i, oy + hy - 1,nx,ny)] =
true;
402 b[
index(ox + 12 + i, oy + 0,nx,ny)] =
true;
403 b[
index(ox + 12 + i, oy + hy - 1,nx,ny)] =
true;
405 for (
int i=1; i<4; i++)
407 b[
index(ox + 6 + i, oy + hy/2,nx,ny)] =
true;
408 b[
index(ox + 12 + i, oy + hy/2,nx,ny)] =
true;
410 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
411 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
412 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
414 b[
index(ox + 19, oy + hy - 2,nx,ny)] =
true;
415 b[
index(ox + 20, oy + hy - 3,nx,ny)] =
true;
416 b[
index(ox + 21, oy + hy - 2,nx,ny)] =
true;
424 for (
int j=0; j<hy; j++)
426 b[
index(ox + 0, oy+j,nx,ny)] =
true;
427 b[
index(ox + 4, oy+j,nx,ny)] =
true;
429 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
430 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
431 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
436 b[
index(nx/2,ny/2,nx,ny)] =
true;
int Size() const
Return the logical size of the array.
virtual void Print(std::ostream &out=mfem::out) const
Class for grid function - Vector with associated FE space.
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)
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)
void PrintUsage(std::ostream &out) const
Print the usage message.
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...
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
Print the options.
int open(const char hostname[], int port)
Open the socket stream on 'port' at 'hostname'.
Arbitrary order "L2-conforming" discontinuous finite elements.
bool Good() const
Return true if the command line options were parsed successfully.