oblique_mc

Monte Carlo in Single Layer Biological Tissue
git clone anongit@rnpnr.xyz:oblique_mc.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 7a2292b22aa3081f5de0c759bdcc5d9a0d1b55dc
Parent: d4de4a4808aa835e8bcb04a4e989b0070bc4417f
Author: Randy Palamar
Date:   Wed,  3 Apr 2024 10:12:37 -0600

move platform specific code to separate file

code is still compiled as single translation unit I just did this
because I intend on readding win32 support later.

Diffstat:
Mmc.c | 43++++++++++++++++++++++++++++---------------
Aposix.c | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+), 15 deletions(-)

diff --git a/mc.c b/mc.c @@ -57,6 +57,16 @@ typedef struct { static Mat2 Rd_xy; static u64 rand_state[2]; +static void die(const char *, ...); + +#if defined(__unix__) || defined(__APPLE__) +#include "posix.c" +#elif defined(_WIN32) +#error Win32 is currently unsupported! +#else +#error Unsupported Platform! +#endif + static void die(const char *fmt, ...) { @@ -115,36 +125,39 @@ dump_output(s8 pre) s8 rd = s8("_Rd_xy.csv"); s8 cat[2] = { pre, xy }; s8 out = s8concat(cat, 2); + os_file f = os_open(out, OS_WRITE); + + u8 dbuf[4096]; + s8 buf = { .data = dbuf }; - FILE *fd = fopen((char *)out.data, "w"); - if (fd == NULL) - die("rip can't open output file: %s\n", out.data); - fputs("x [cm]\ty [cm]\n", fd); + os_write(f, s8("x [cm]\ty [cm]\n")); for (u32 i = 0; i < gctx.Nx; i++) { f64 x = (i + 0.5) * gctx.dx - gctx.xoff; f64 y = (i + 0.5) * gctx.dy - gctx.yoff; - fprintf(fd, "%e\t%e\n", x, y); + buf.len = snprintf((char *)buf.data, sizeof(dbuf), + "%e\t%e\n", x, y); + os_write(f, buf); } - fclose(fd); + os_close(f); free(out.data); cat[1] = rd; out = s8concat(cat, 2); - - fd = fopen((char *)out.data, "w"); - if (fd == NULL) - die("rip can't open output file: %s\n", out.data); + f = os_open(out, OS_WRITE); f64 scale = gctx.N_photons * gctx.dx * gctx.dy; f64 *b = Rd_xy.b; for (u32 i = 0; i < Rd_xy.Nx; i++) { - for (u32 j = 0; j < Rd_xy.Ny; j++) - fprintf(fd, "%e,", b[j] / scale); - fseek(fd, -1, SEEK_CUR); - fputc('\n', fd); + for (u32 j = 0; j < Rd_xy.Ny; j++) { + buf.len = snprintf((char *)buf.data, sizeof(dbuf), + "%e,", b[j] / scale); + os_write(f, buf); + } + os_seek(f, -1, OS_SEEK_CUR); + os_write(f, s8("\n")); b += Rd_xy.Ny; } - fclose(fd); + os_close(f); free(out.data); } diff --git a/posix.c b/posix.c @@ -0,0 +1,40 @@ +#include <fcntl.h> +#include <unistd.h> + +#define OS_READ O_RDONLY +#define OS_WRITE O_WRONLY +#define OS_RW O_RDWR +#define OS_SEEK_CUR SEEK_CUR +#define OS_SEEK_END SEEK_END +#define OS_SEEK_SET SEEK_SET + +typedef int os_file; + +static os_file +os_open(s8 path, int flags) +{ + os_file f; + f = open((char *)path.data, flags); + if (f == -1) + die("rip can't open output file: %s\n", path.data); + return f; +} + +static void +os_write(os_file f, s8 s) +{ + if (write(f, s.data, s.len) == -1) + die("can't write to file\n"); +} + +static void +os_close(os_file f) +{ + close(f); +} + +static void +os_seek(os_file f, size off, int whence) +{ + lseek(f, off, whence); +}