From c4f17f913246a5b382b79f4eb9ac13e29ffaa222 Mon Sep 17 00:00:00 2001 From: Matthieu Schaller <matthieu.schaller@durham.ac.uk> Date: Thu, 28 Jun 2018 14:47:42 +0100 Subject: [PATCH] Create the infrastructure to stop and restart with the pm_mesh switched on. --- src/engine.c | 5 +++++ src/gravity_properties.c | 2 +- src/gravity_properties.h | 8 +++++--- src/mesh_gravity.c | 36 ++++++++++++++++++++++++++++++++++++ src/mesh_gravity.h | 5 +++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/engine.c b/src/engine.c index d6fb46f9c0..ac43011499 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6295,6 +6295,7 @@ void engine_struct_dump(struct engine *e, FILE *stream) { phys_const_struct_dump(e->physical_constants, stream); hydro_props_struct_dump(e->hydro_properties, stream); gravity_props_struct_dump(e->gravity_properties, stream); + pm_mesh_struct_dump(e->mesh, stream); potential_struct_dump(e->external_potential, stream); cooling_struct_dump(e->cooling_func, stream); chemistry_struct_dump(e->chemistry, stream); @@ -6364,6 +6365,10 @@ void engine_struct_restore(struct engine *e, FILE *stream) { gravity_props_struct_restore(gravity_properties, stream); e->gravity_properties = gravity_properties; + struct pm_mesh *mesh = (struct pm_mesh *)malloc(sizeof(struct pm_mesh)); + pm_mesh_struct_restore(mesh, stream); + e->mesh = mesh; + struct external_potential *external_potential = (struct external_potential *)malloc(sizeof(struct external_potential)); potential_struct_restore(external_potential, stream); diff --git a/src/gravity_properties.c b/src/gravity_properties.c index 5e0fa238a8..84ce90f727 100644 --- a/src/gravity_properties.c +++ b/src/gravity_properties.c @@ -190,7 +190,7 @@ void gravity_props_struct_dump(const struct gravity_props *p, FILE *stream) { * @param p the struct * @param stream the file stream */ -void gravity_props_struct_restore(const struct gravity_props *p, FILE *stream) { +void gravity_props_struct_restore(struct gravity_props *p, FILE *stream) { restart_read_blocks((void *)p, sizeof(struct gravity_props), 1, stream, NULL, "gravity props"); } diff --git a/src/gravity_properties.h b/src/gravity_properties.h index 59491eca21..1d230b8c37 100644 --- a/src/gravity_properties.h +++ b/src/gravity_properties.h @@ -27,10 +27,12 @@ #endif /* Local includes. */ -#include "cosmology.h" -#include "parser.h" #include "restart.h" +/* Forward declarations */ +struct cosmology; +struct swift_params; + /** * @brief Contains all the constants and parameters of the self-gravity scheme */ @@ -96,6 +98,6 @@ void gravity_props_print_snapshot(hid_t h_grpsph, /* Dump/restore. */ void gravity_props_struct_dump(const struct gravity_props *p, FILE *stream); -void gravity_props_struct_restore(const struct gravity_props *p, FILE *stream); +void gravity_props_struct_restore(struct gravity_props *p, FILE *stream); #endif /* SWIFT_GRAVITY_PROPERTIES */ diff --git a/src/mesh_gravity.c b/src/mesh_gravity.c index 4f5ff46354..be38c00cb8 100644 --- a/src/mesh_gravity.c +++ b/src/mesh_gravity.c @@ -509,3 +509,39 @@ void pm_mesh_clean(struct pm_mesh* mesh) { if (mesh->potential) free(mesh->potential); mesh->potential = 0; } + +/** + * @brief Write a #pm_mesh struct to the given FILE as a stream of bytes. + * + * @param p the struct + * @param stream the file stream + */ +void pm_mesh_struct_dump(const struct pm_mesh* mesh, FILE* stream) { + restart_write_blocks((void*)mesh, sizeof(struct pm_mesh), 1, stream, + "gravity", "gravity props"); +} + +/** + * @brief Restore a #pm_mesh struct from the given FILE as a stream of + * bytes. + * + * @param p the struct + * @param stream the file stream + */ +void pm_mesh_struct_restore(struct pm_mesh* mesh, FILE* stream) { + + restart_read_blocks((void*)mesh, sizeof(struct pm_mesh), 1, stream, NULL, + "gravity props"); + + const int N = mesh->N; + + /* Allocate the memory for the combined density and potential array */ + mesh->potential = (double*)fftw_malloc(sizeof(double) * N * N * N); + if (mesh->potential == NULL) + error("Error allocating memory for the long-range gravity mesh."); + +#ifdef HAVE_FFTW +#else + error("No FFTW library found. Cannot compute periodic long-range forces."); +#endif +} diff --git a/src/mesh_gravity.h b/src/mesh_gravity.h index d04ab26e6d..8cc4cd753c 100644 --- a/src/mesh_gravity.h +++ b/src/mesh_gravity.h @@ -24,6 +24,7 @@ /* Local headers */ #include "gravity_properties.h" +#include "restart.h" /* Forward declarations */ struct engine; @@ -71,4 +72,8 @@ void pm_mesh_interpolate_forces(const struct pm_mesh *mesh, int gcount); void pm_mesh_clean(struct pm_mesh *mesh); +/* Dump/restore. */ +void pm_mesh_struct_dump(const struct pm_mesh *p, FILE *stream); +void pm_mesh_struct_restore(struct pm_mesh *p, FILE *stream); + #endif /* SWIFT_MESH_GRAVITY_H */ -- GitLab