twrite slider positions to file, todo: add other params - slidergrid - grid of … | |
git clone git://src.adamsgaard.dk/slidergrid | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 0f79de37df00ace7285e5b445d648a90a27d6def | |
parent bd4fc9c6e32224ee34ffce1e727317cc3b8375f5 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Wed, 16 Mar 2016 16:05:11 -0700 | |
write slider positions to file, todo: add other params | |
Diffstat: | |
M slidergrid/main.c | 24 ++++++++++++++++++++++++ | |
M slidergrid/simulation.c | 20 ++++++++++++++++++++ | |
M slidergrid/simulation.h | 3 +++ | |
3 files changed, 47 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/slidergrid/main.c b/slidergrid/main.c | |
t@@ -1,6 +1,9 @@ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
+#include <sys/types.h> | |
+#include <sys/stat.h> | |
+#include <unistd.h> | |
#include "slider.h" | |
#include "grid.h" | |
#include "simulation.h" | |
t@@ -94,9 +97,19 @@ int main(int argc, char** argv) | |
printf("time step length: dt = %f s\n", sim.dt); | |
} | |
+ // create output file directory with simulation id as name and placed in | |
+ // current folder if it doesn't already exist | |
+ char output_folder[400]; | |
+ sprintf("./%s/", output_folder, sim.id); | |
+ struct stat st = {0}; | |
+ if (stat(output_folder, &st) == -1) | |
+ mkdir(output_folder, 0700); | |
+ | |
// main temporal loop | |
sim.iteration = 0; | |
Float time_since_status = 0.0; | |
+ Float time_since_file = 0.0; | |
+ char filename[1000]; | |
for (sim.time = 0.0; | |
sim.time <= sim.time_end; | |
sim.time += sim.dt) { | |
t@@ -115,6 +128,16 @@ int main(int argc, char** argv) | |
update_kinematics(sim.sliders[i], sim.dt, sim.iteration); | |
} | |
+ if (time_since_file >= sim.file_interval) { | |
+ sprintf(filename, "%s/output%06d.txt", | |
+ output_folder, sim.file_number); | |
+ if (save_simulation_to_file(sim, filename)) { | |
+ fprintf(stderr, "\nFatal error: Could not save to output file " | |
+ "'%s'.\n", filename); | |
+ return EXIT_FAILURE; | |
+ } | |
+ } | |
+ | |
if (verbose) { | |
if (time_since_status > sim.dt*100. || | |
time_since_status >= sim.file_interval) { | |
t@@ -125,6 +148,7 @@ int main(int argc, char** argv) | |
sim.iteration++; | |
time_since_status += sim.dt; | |
+ time_since_file += sim.dt; | |
} | |
print_status(sim); | |
diff --git a/slidergrid/simulation.c b/slidergrid/simulation.c | |
t@@ -14,6 +14,7 @@ simulation create_simulation() | |
sim.bond_length_limit = 0; | |
sim.id = "unnamed"; | |
sim.file_interval = 0.0; | |
+ sim.file_number = 0; | |
sim.verbose = 0; | |
return sim; | |
} | |
t@@ -120,3 +121,22 @@ int save_slider_positions_to_file( | |
fclose(f); | |
return 0; | |
} | |
+ | |
+int save_simulation_to_file(const simulation sim, const char* filename) | |
+{ | |
+ FILE* f = fopen(filename, "w"); | |
+ if (f == NULL) { | |
+ fprintf(stderr, "Could not open output file %s.", filename); | |
+ return 1; | |
+ } | |
+ | |
+ int i; | |
+ for (i=0; i<sim.N; i++) | |
+ printf("%f\t%f\t%f\n", | |
+ sim.sliders[i].pos.x, | |
+ sim.sliders[i].pos.y, | |
+ sim.sliders[i].pos.z); | |
+ | |
+ fclose(f); | |
+ return 0; | |
+} | |
diff --git a/slidergrid/simulation.h b/slidergrid/simulation.h | |
t@@ -13,6 +13,7 @@ typedef struct { | |
Float bond_length_limit; | |
char* id; | |
Float file_interval; | |
+ int file_number; | |
int verbose; | |
} simulation; | |
t@@ -31,6 +32,8 @@ int save_slider_positions_to_file( | |
const int N, | |
const char* filename); | |
+int save_simulation_to_file(const simulation sim, const char* filename); | |
+ | |
// user-defined function which sets up the simulation | |
simulation setup_simulation(); | |