tfix sprintf calls, improve error messages - slidergrid - grid of elastic sliders on a frictional surface
 (HTM) git clone git://src.adamsgaard.dk/slidergrid
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 835f19fe0b2edc041905949cdd48466a743ed440
 (DIR) parent 0f79de37df00ace7285e5b445d648a90a27d6def
 (HTM) Author: Anders Damsgaard <anders.damsgaard@geo.au.dk>
       Date:   Thu, 17 Mar 2016 09:27:09 -0700
       
       fix sprintf calls, improve error messages
       
       mkdir is not successful
       
       Diffstat:
         M Makefile                            |       7 ++++++-
         M slidergrid/main.c                   |      19 +++++++++++++++----
         M slidergrid/simulation.c             |      22 ++++++++++++----------
         M slidergrid/simulation.h             |       2 +-
       
       4 files changed, 34 insertions(+), 16 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -1,5 +1,6 @@
        CC=gcc
       -CFLAGS=-Wall -O3 -g -pg
       +#CFLAGS=-Wall -O3 -march=native
       +CFLAGS=-Wall -g -pg
        LDLIBS=-lm
        SRCFOLDER=slidergrid
        ESSENTIALOBJS=$(SRCFOLDER)/main.o \
       t@@ -7,6 +8,7 @@ ESSENTIALOBJS=$(SRCFOLDER)/main.o \
                                  $(SRCFOLDER)/grid.o \
                                  $(SRCFOLDER)/vector_math.o \
                                  $(SRCFOLDER)/simulation.o
       +BIN=test
        
        test: test.o $(ESSENTIALOBJS)
                $(CC) $(LDLIBS) $^ -o $@
       t@@ -15,6 +17,9 @@ profile: $(BIN)
                @gprof $< > $<-profile.txt
                @less $<-profile.txt
        
       +debug: $(BIN)
       +        @gdb $<
       +
        clean:
                @$(RM) $(BIN)
                @$(RM) *.o
 (DIR) diff --git a/slidergrid/main.c b/slidergrid/main.c
       t@@ -100,7 +100,7 @@ int main(int argc, char** argv)
            // 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);
       +    sprintf(output_folder, "./%s/", sim.id);
            struct stat st = {0};
            if (stat(output_folder, &st) == -1)
                mkdir(output_folder, 0700);
       t@@ -129,9 +129,20 @@ int main(int argc, char** argv)
                }
        
                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)) {
       +
       +            // slider parameters
       +            sprintf(filename, "%s/%s.sliders.%06d.txt",
       +                    output_folder, sim.id, sim.file_number);
       +            if (save_slider_positions_to_file(sim.sliders, sim.N, filename)) {
       +                fprintf(stderr, "\nFatal error: Could not save to output file "
       +                        "'%s'.\n", filename);
       +                return EXIT_FAILURE;
       +            }
       +
       +            // other parameters
       +            sprintf(filename, "%s/%s.general.%06d.txt",
       +                    output_folder, sim.id, sim.file_number);
       +            if (save_general_state_to_file(sim, filename)) {
                        fprintf(stderr, "\nFatal error: Could not save to output file "
                                "'%s'.\n", filename);
                        return EXIT_FAILURE;
 (DIR) diff --git a/slidergrid/simulation.c b/slidergrid/simulation.c
       t@@ -109,33 +109,35 @@ int save_slider_positions_to_file(
        {
            FILE* f = fopen(filename, "w");
            if (f == NULL) {
       -        fprintf(stderr, "Could not open output file %s.", filename);
       +        fprintf(stderr, "Error: Could not open output file %s.", filename);
                return 1;
            }
        
            int i;
            for (i=0; i<N; i++)
       -        printf("%f\t%f\t%f\n",
       +        fprintf(f, "%f\t%f\t%f\n",
                        sliders[i].pos.x, sliders[i].pos.y, sliders[i].pos.z);
        
            fclose(f);
            return 0;
        }
        
       -int save_simulation_to_file(const simulation sim, const char* filename)
       +int save_general_state_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);
       +        fprintf(stderr, "Error: 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);
       +    fprintf(f, "id = %s\n", sim.id);
       +    fprintf(f, "N = %d\n", sim.N);
       +    fprintf(f, "time = %f\n", sim.time);
       +    fprintf(f, "time_end = %f\n", sim.time_end);
       +    fprintf(f, "dt = %f\n", sim.dt);
       +    fprintf(f, "file_interval = %f\n", sim.file_interval);
       +    fprintf(f, "iteration = %ld\n", sim.iteration);
       +    fprintf(f, "bond_length_limit = %f\n", sim.bond_length_limit);
        
            fclose(f);
            return 0;
 (DIR) diff --git a/slidergrid/simulation.h b/slidergrid/simulation.h
       t@@ -32,7 +32,7 @@ int save_slider_positions_to_file(
                const int N,
                const char* filename);
        
       -int save_simulation_to_file(const simulation sim, const char* filename);
       +int save_general_state_to_file(const simulation sim, const char* filename);
        
        // user-defined function which sets up the simulation
        simulation setup_simulation();