timplement simulation initialization and dynamic grain memory allocation - gran… | |
git clone git://src.adamsgaard.dk/granular | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 3d5cc041769b612a57de8f5068a244c83b4612f9 | |
parent 512cf441b47308e28c16512418bccf09855cb678 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Fri, 19 Mar 2021 21:56:24 +0100 | |
implement simulation initialization and dynamic grain memory allocation | |
Diffstat: | |
M Makefile | 12 +++++++++--- | |
A granular2vtu.c | 28 ++++++++++++++++++++++++++++ | |
A granularpacking.c | 22 ++++++++++++++++++++++ | |
D packingtest.c | 22 ---------------------- | |
M simulation.c | 30 ++++++++++++++++++++++++++++++ | |
M simulation.h | 15 ++++++++------- | |
6 files changed, 97 insertions(+), 32 deletions(-) | |
--- | |
diff --git a/Makefile b/Makefile | |
t@@ -10,7 +10,8 @@ DOCPREFIX = ${PREFIX}/share/doc/${NAME} | |
BIN = \ | |
granular\ | |
- packingtest | |
+ granular2vtu\ | |
+ granularpacking | |
SRC = ${BIN:=.c} arrays.c grain.c granular.c packing.c simulation.c util.c | |
HDR = \ | |
arg.h\ | |
t@@ -47,9 +48,14 @@ ${OBJ}: ${HDR} | |
.c.o: | |
${CC} ${GRANULAR_CFLAGS} ${GRANULAR_CPPFLAGS} -o $@ -c $< | |
-packingtest: packingtest.o arrays.o grain.o packing.o simulation.o util.o | |
+granularpacking: granularpacking.o arrays.o grain.o packing.o simulation.o uti… | |
${CC}\ | |
- packingtest.o arrays.o grain.o packing.o simulation.o util.o\ | |
+ granularpacking.o arrays.o grain.o packing.o simulation.o util… | |
+ -o $@ ${GRANULAR_LDFLAGS} | |
+ | |
+granular2vtu: granular2vtu.o arrays.o grain.o simulation.o util.o | |
+ ${CC}\ | |
+ granular2vtu.o arrays.o grain.o simulation.o util.o\ | |
-o $@ ${GRANULAR_LDFLAGS} | |
granular: granular.o arrays.o grain.o packing.o simulation.o util.o | |
diff --git a/granular2vtu.c b/granular2vtu.c | |
t@@ -0,0 +1,28 @@ | |
+#include <stdio.h> | |
+#include <unistd.h> | |
+#include "grain.c" | |
+#include "simulation.h" | |
+#include "arrays.h" | |
+#include "util.h" | |
+ | |
+int | |
+main(void) | |
+{ | |
+ char *line = NULL; | |
+ size_t linesize = 0; | |
+ ssize_t linelen; | |
+ struct simulation sim; | |
+ | |
+ init_sim(&sim); | |
+ | |
+ while ((linelen = getline(&line, &linesize, stdin)) > 0) | |
+ add_grain(&sim, grain_read(line)); | |
+ | |
+ /* if (sim.np > 0) */ | |
+ print_grains_vtk(stdout, sim.grains, sim.np); | |
+ | |
+ free_sim(&sim); | |
+ free(line); | |
+ | |
+ return 0; | |
+} | |
diff --git a/granularpacking.c b/granularpacking.c | |
t@@ -0,0 +1,22 @@ | |
+#include <stdio.h> | |
+#include "packing.h" | |
+#include "simulation.h" | |
+#include "arrays.h" | |
+#include "util.h" | |
+ | |
+int | |
+main(void) | |
+{ | |
+ struct simulation sim; | |
+ | |
+ size_t n[3], np = 0; | |
+ n[0] = 50; n[1] = 50; n[2] = 50; | |
+ double *origo = zeros(3); | |
+ | |
+ /* np = rectangular_packing(&sim.grains, n, 1.0, 1.0, random_value_uni… | |
+ np = triangular_packing(&sim.grains, n, 0.2, 1.0, random_value_uniform… | |
+ print_grains(stdout, sim.grains, np); | |
+ | |
+ free(origo); | |
+ free_sim(&sim); | |
+} | |
diff --git a/packingtest.c b/packingtest.c | |
t@@ -1,22 +0,0 @@ | |
-#include <stdio.h> | |
-#include "packing.h" | |
-#include "simulation.h" | |
-#include "arrays.h" | |
-#include "util.h" | |
- | |
-int | |
-main(void) | |
-{ | |
- struct simulation sim; | |
- | |
- size_t n[3], np = 0; | |
- n[0] = 10; n[1] = 7; n[2] = 1; | |
- double *origo = zeros(3); | |
- | |
- /* np = rectangular_packing(&sim.grains, n, 1.0, 1.0, random_value_uni… | |
- np = triangular_packing(&sim.grains, n, 1.0, 1.0, random_value_uniform… | |
- print_grains(stdout, sim.grains, np); | |
- | |
- free(origo); | |
- free_sim(&sim); | |
-} | |
diff --git a/simulation.c b/simulation.c | |
t@@ -1,14 +1,44 @@ | |
#include <stdio.h> | |
+#include <err.h> | |
#include "simulation.h" | |
+#include "arrays.h" | |
#include "util.h" | |
void | |
+init_sim(struct simulation *sim) | |
+{ | |
+ snprintf(sim->name, sizeof(sim->name), DEFAULT_SIMULATION_NAME); | |
+ sim->np = 0; | |
+ sim->gravacc = zeros(ND); | |
+ sim->origo = zeros(ND); | |
+ sim->L = ones(ND); | |
+ sim->t = 0.0; | |
+ sim->t_end = 0.0; | |
+ sim->dt = 0.0; | |
+ sim->file_dt = 1.0; | |
+ sim->n_file = 0; | |
+ sim->np = 0; | |
+} | |
+ | |
+void | |
free_sim(struct simulation *sim) | |
{ | |
+ free(sim->gravacc); | |
+ free(sim->origo); | |
+ free(sim->L); | |
free(sim->grains); | |
} | |
void | |
+add_grain(struct simulation *sim, const struct grain *g) | |
+{ | |
+ if (!(sim->grains = realloc(sim->grains, sim->np * sizeof(struct grain… | |
+ errx(1, "%s: sim.grains realloc", __func__); | |
+ | |
+ sim->grains[sim->np++] = *g; | |
+} | |
+ | |
+void | |
print_grains(FILE *stream, const struct grain *grains, size_t n) | |
{ | |
size_t i; | |
diff --git a/simulation.h b/simulation.h | |
t@@ -16,14 +16,12 @@ struct simulation { | |
char name[255]; | |
/* gravitational acceleration [m/s^2] */ | |
- double gravacc[ND]; | |
+ double *gravacc; | |
/* grain sorting grid */ | |
- size_t nd[ND]; | |
- double origo[ND]; | |
- double L[ND]; | |
- double x[ND]; | |
- double dx; | |
+ size_t *nd; | |
+ double *origo; | |
+ double *L; | |
/* temporal state [s] */ | |
double t; | |
t@@ -33,12 +31,15 @@ struct simulation { | |
size_t n_file; | |
/* grains */ | |
- size_t n; | |
+ size_t np; | |
struct grain *grains; | |
}; | |
+void init_sim(struct simulation *sim); | |
void free_sim(struct simulation *sim); | |
+void add_grain(struct simulation *sim, const struct grain *g); | |
+ | |
void print_grains(FILE *stream, const struct grain *grains, size_t n); | |
void print_grains_vtk(FILE *stream, const struct grain *grains, size_t n); | |