// sw2d.c — 2D shallow-water (linearized) with h,u,v output per frame and rich header. // Discretization: forward Euler in time, centered differences in space. // Boundaries: zero-gradient (Neumann) via clamped neighbor access. // // Default IC (when no --init): circular displaced water column (disk) centered on the grid, // with diameter = 1/4 of the physical domain width (cols * dx). Height inside the disk = --height. // // Binary INIT/PRIOR file (little-endian): // int32 rows, int32 cols, // then either rows*cols doubles (h) or 3*rows*cols doubles (h,u,v) // // Binary MOVIE/OUTPUT file (little-endian): // Header layout (fixed, little-endian) // char magic[4] = "SW2D" // uint32 version = 1 // uint32 flags = 0x7 // bit0: has_h, bit1: has_u, bit2: has_v // int32 rows // int32 cols // int32 nframes (filled-in at end; includes initial frame if saved) // int32 save_interval // double dx, dy, dt, g, H0 // Frames (nframes of): // double h[rows*cols], double u[rows*cols], double v[rows*cols] // // Compile: cc -O3 -march=native -Wall -Wextra -o sw2d sw2d.c -lm // // Example: ./sw2d --rows 200 --cols 200 --steps 5000 --height 0.5 --save-interval 50 --out movie.bin #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif typedef struct { int rows, cols, steps; double dx, dy, dt; double g, H0, cfl; // init condition if no init file double init_height; int init_col; // kept for backward-compat; unused by circular IC // I/O const char *init_file; // optional prior state (h or h,u,v) const char *out_file; // movie output (h,u,v per frame) int save_interval; // save every k steps (0 => disabled) // stats int stats_interval; // print/refresh every k steps int progress_bar; // 1 show progress bar } Params; static double now_sec(void) { struct timeval tv; gettimeofday(&tv, NULL); return (double)tv.tv_sec + 1e-6 * (double)tv.tv_usec; } static inline int idx(int r, int c, int cols) { return r*cols + c; } static inline int clampi(int x, int lo, int hi) { if (x < lo) return lo; if (x > hi) return hi; return x; } static inline double getH(const double *A, int r, int c, int R, int C) { r = clampi(r, 0, R-1); c = clampi(c, 0, C-1); return A[idx(r,c,C)]; } // Slurp file static unsigned char* slurp(const char* path, size_t* out_sz) { FILE* f = fopen(path, "rb"); if (!f) return NULL; if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; } long n = ftell(f); if (n < 0) { fclose(f); return NULL; } rewind(f); unsigned char* buf = (unsigned char*)malloc((size_t)n); if (!buf) { fclose(f); return NULL; } size_t rd = fread(buf, 1, (size_t)n, f); fclose(f); if (rd != (size_t)n) { free(buf); return NULL; } *out_sz = rd; return buf; } // Load init/prior state (allocates h,u,v). Returns 0 on success. static int load_init(const char* path, int* rows, int* cols, double** h, double** u, double** v) { size_t sz = 0; unsigned char* buf = slurp(path, &sz); if (!buf) { fprintf(stderr, "[error] Cannot read init '%s': %s\n", path, strerror(errno)); return -1; } if (sz < 8) { fprintf(stderr, "[error] Init too small.\n"); free(buf); return -1; } const int32_t* i32 = (const int32_t*)buf; int R = i32[0], C = i32[1]; if (R <= 0 || C <= 0) { fprintf(stderr, "[error] Bad dims in init.\n"); free(buf); return -1; } size_t off = 2*sizeof(int32_t); size_t need = (size_t)R*(size_t)C*sizeof(double); size_t rem = sz - off; bool has_all = false; if (rem == need) has_all = false; else if (rem == 3*need) has_all = true; else { fprintf(stderr, "[error] Init payload size mismatch.\n"); free(buf); return -1; } double *H = (double*)malloc(need); double *U = (double*)calloc((size_t)R*(size_t)C, sizeof(double)); double *V = (double*)calloc((size_t)R*(size_t)C, sizeof(double)); if (!H || !U || !V) { fprintf(stderr, "[error] OOM.\n"); free(H); free(U); free(V); free(buf); return -1; } memcpy(H, buf + off, need); if (has_all) { memcpy(U, buf + off + need, need); memcpy(V, buf + off + 2*need, need); } free(buf); *rows = R; *cols = C; *h = H; *u = U; *v = V; return 0; } static void usage(const char* prog) { fprintf(stderr, "Usage: %s [options]\n" " --rows INT grid rows (N) [200]\n" " --cols INT grid cols (M) [200]\n" " --steps INT number of time steps [2000]\n" " --dx DOUBLE cell size x [1.0]\n" " --dy DOUBLE cell size y [1.0]\n" " --dt DOUBLE time step (<=0 => CFL) [auto]\n" " --g DOUBLE gravity [9.81]\n" " --H0 DOUBLE mean depth [1.0]\n" " --cfl DOUBLE CFL number [0.4]\n" " --height DOUBLE displaced column height if no init [0.5]\n" " --col INT (legacy) column index (unused with circular IC)\n" " --init PATH optional binary init/prior (h or h,u,v)\n" " --out PATH output movie filename (h,u,v per frame)\n" " --save-interval INT save every k steps (0 disables) [0]\n" " --stats-interval INT stats update every k steps [100]\n" " --no-progress disable progress bar\n" " --help\n", prog); } // Progress bar static void draw_progress(int step, int steps, double ema_upd, double ema_GBps) { const int width = 40; double frac = steps>0? (double)step/(double)steps : 1.0; if (frac < 0) frac = 0; if (frac > 1) frac = 1; int filled = (int)llround(frac * width); fprintf(stderr, "\r["); for (int i=0;i radius = width/8 const double radius = (C * P.dx) / 8.0; const double r2_max = radius * radius; for (int r = 0; r < R; ++r) { const double y = r * P.dy; for (int c = 0; c < C; ++c) { const double x = c * P.dx; const double dx = x - cx; const double dy = y - cy; const double d2 = dx*dx + dy*dy; if (d2 <= r2_max) { h[idx(r,c,C)] = P.init_height; // flat top inside the disk } else { h[idx(r,c,C)] = 0.0; // flat (zero perturbation) elsewhere } } } // u,v start at 0 } double *h_new = (double*)malloc(N*sizeof(double)); double *u_new = (double*)malloc(N*sizeof(double)); double *v_new = (double*)malloc(N*sizeof(double)); if (!h_new||!u_new||!v_new) { fprintf(stderr,"[error] OOM\n"); return 1; } // Time step from CFL if not provided const double wavespeed = sqrt(P.g * P.H0); if (P.dt <= 0.0) { double dmin = (P.dx < P.dy) ? P.dx : P.dy; P.dt = P.cfl * dmin / wavespeed; } // Output FILE* fout=NULL; int32_t nframes = 0; if (P.out_file && P.save_interval > 0) { fout = fopen(P.out_file, "wb"); if (!fout) { fprintf(stderr,"[error] open out '%s': %s\n", P.out_file, strerror(errno)); return 1; } // Header const char magic[4] = {'S','W','2','D'}; uint32_t version = 1u; uint32_t flags = 0x7u; // has h,u,v int32_t rows = R, cols = C; int32_t save_int = P.save_interval; double dxv = P.dx, dyv = P.dy, dtv = P.dt, gv = P.g, H0v = P.H0; fwrite(magic, 1, 4, fout); fwrite(&version, sizeof(uint32_t), 1, fout); fwrite(&flags, sizeof(uint32_t), 1, fout); fwrite(&rows, sizeof(int32_t), 1, fout); fwrite(&cols, sizeof(int32_t), 1, fout); int32_t nframes_placeholder = 0; // fill at end long nframes_pos = ftell(fout); fwrite(&nframes_placeholder, sizeof(int32_t), 1, fout); fwrite(&save_int, sizeof(int32_t), 1, fout); fwrite(&dxv, sizeof(double), 1, fout); fwrite(&dyv, sizeof(double), 1, fout); fwrite(&dtv, sizeof(double), 1, fout); fwrite(&gv, sizeof(double), 1, fout); fwrite(&H0v, sizeof(double), 1, fout); // Save initial frame (step 0) size_t count = (size_t)R*(size_t)C; if (fwrite(h, sizeof(double), count, fout) != count) { fprintf(stderr,"[error] write frame h\n"); return 1; } if (fwrite(u, sizeof(double), count, fout) != count) { fprintf(stderr,"[error] write frame u\n"); return 1; } if (fwrite(v, sizeof(double), count, fout) != count) { fprintf(stderr,"[error] write frame v\n"); return 1; } nframes = 1; (void)nframes_pos; // kept for clarity; we use fixed offset below } // Perf model const double bytes_per_cell_update_est = 3.0 /*h,u,v*/ * (5+1) /*~5 reads + 1 write*/ * 8.0; // rough double t0 = now_sec(), last_t = t0; double ema_updates = 0.0, ema_GBps = 0.0, ema_alpha = 0.2; const double inv2dx = 1.0/(2.0*P.dx), inv2dy = 1.0/(2.0*P.dy); const double H0 = P.H0, g = P.g, dt = P.dt; for (int step=1; step<=P.steps; ++step) { // u,v from h gradients for (int r=0;r0 && (step % P.stats_interval)==0)) { double t1 = now_sec(); double dt_wall = t1 - last_t; last_t = t1; if (dt_wall > 0) { double upd = (double)R*(double)C*(double)((step0)? total_updates/wall : 0.0; double GBps = (wall>0)? (total_updates*bytes_per_cell_update_est)/(wall*1e9) : 0.0; fprintf(stderr, "\nDone. Wall=%.3fs Updates=%.3e Updates/s=%.2e Apparent BW=%.2f GB/s\n", wall, total_updates, upd_s, GBps); free(h); free(u); free(v); free(h_new); free(u_new); free(v_new); return 0; }