tfix sim_add_grain, granular2vtk runs without errors - granular - granular dyna… | |
git clone git://src.adamsgaard.dk/granular | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 9f2040e9cae2ff9dd00420ee7dd234ba97bb8afb | |
parent 7e9eec953717ca34bfb7e4d1a49b93b0d1be1a31 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Thu, 25 Mar 2021 12:51:33 +0100 | |
fix sim_add_grain, granular2vtk runs without errors | |
Diffstat: | |
M Makefile | 1 + | |
M simulation.c | 14 +++++++++++--- | |
M simulation.h | 2 +- | |
3 files changed, 13 insertions(+), 4 deletions(-) | |
--- | |
diff --git a/Makefile b/Makefile | |
t@@ -19,6 +19,7 @@ SRC = ${BIN:=.c} arrays.c grain.c granular.c packing.c simul… | |
HDR = \ | |
arg.h\ | |
arrays.h\ | |
+ compat.h\ | |
grain.h\ | |
granular.h\ | |
packing.h\ | |
diff --git a/simulation.c b/simulation.c | |
t@@ -1,6 +1,7 @@ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <err.h> | |
+#include <string.h> | |
#include "grain.h" | |
#include "simulation.h" | |
#include "arrays.h" | |
t@@ -28,6 +29,7 @@ sim_defaults(struct simulation *sim) | |
sim->file_dt = 1.0; | |
sim->n_file = 0; | |
sim->np = 0; | |
+ sim->grains = NULL; | |
} | |
void | |
t@@ -37,14 +39,20 @@ sim_free(struct simulation *sim) | |
free(sim->origo); | |
free(sim->L); | |
free(sim->grains); | |
+ sim->grains = NULL; | |
+ sim->np = 0; | |
} | |
void | |
-sim_add_grain(struct simulation *sim, const struct grain *g) | |
+sim_add_grain(struct simulation *sim, struct grain *g) | |
{ | |
- if (!(sim->grains = realloc(sim->grains, sim->np * sizeof(struct grain… | |
- errx(1, "%s: sim.grains realloc", __func__); | |
+ struct grain *tmp = NULL; | |
+ if (!(tmp = calloc(sim->np + 1, sizeof(*g)))) | |
+ err(1, "%s: sim.grains calloc", __func__); | |
+ memcpy(tmp, sim->grains, sim->np * sizeof(*g)); | |
+ free(sim->grains); | |
+ sim->grains = tmp; | |
sim->grains[sim->np++] = *g; | |
} | |
diff --git a/simulation.h b/simulation.h | |
t@@ -36,7 +36,7 @@ struct simulation sim_new(void); | |
void sim_defaults(struct simulation *sim); | |
void sim_free(struct simulation *sim); | |
-void sim_add_grain(struct simulation *sim, const struct grain *g); | |
+void sim_add_grain(struct simulation *sim, struct grain *g); | |
void sim_print_grains(FILE *stream, const struct grain *grains, size_t n); | |
void sim_print_grains_vtk(FILE *stream, const struct grain *grains, size_t n); |