#define _XOPEN_SOURCE 600 #include #include #include #include #include #include // Option to change numerical precision. typedef int64_t int_t; typedef double real_t; // Simulation parameters: size, step count, and how often to save the state. const int_t N = 65536, max_iteration = 100000, snapshot_freq = 500; // Wave equation parameters, time step is derived from the space step. const real_t c = 1.0, dx = 1.0; real_t dt; // Buffers for three time steps, indexed with 2 ghost points for the boundary. real_t *buffers[3] = { NULL, NULL, NULL }; #define U_prv(i) buffers[0][(i)+1] #define U(i) buffers[1][(i)+1] #define U_nxt(i) buffers[2][(i)+1] // Convert 'struct timeval' into seconds in double prec. floating point #define WALLTIME(t) ((double)(t).tv_sec + 1e-6 * (double)(t).tv_usec) // Save the present time step in a numbered file under 'data/'. void domain_save ( int_t step ) { char filename[256]; sprintf ( filename, "data/%.5ld.dat", step ); FILE *out = fopen ( filename, "wb" ); fwrite ( &U(0), sizeof(real_t), N, out ); fclose ( out ); } // Set up our three buffers, fill two with an initial cosine wave, // and set the time step. void domain_initialize ( void ) { buffers[0] = malloc ( (N+2)*sizeof(real_t) ); buffers[1] = malloc ( (N+2)*sizeof(real_t) ); buffers[2] = malloc ( (N+2)*sizeof(real_t) ); for ( int_t i=0; i