gitlab is upgraded to version 13, please report any issues and enjoy

Commit 981ae243 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Allow for conversion of quantities when writing snapshots

parent f53617b2
......@@ -358,8 +358,8 @@ int main(int argc, char *argv[]) {
&periodic, &flag_entropy_ICs, myrank, nr_nodes,
MPI_COMM_WORLD, MPI_INFO_NULL, dry_run);
#else
read_ic_serial(ICfileName, &us, dim, &parts, &gparts, &Ngas, &Ngpart, &periodic,
&flag_entropy_ICs, myrank, nr_nodes, MPI_COMM_WORLD,
read_ic_serial(ICfileName, &us, dim, &parts, &gparts, &Ngas, &Ngpart,
&periodic, &flag_entropy_ICs, myrank, nr_nodes, MPI_COMM_WORLD,
MPI_INFO_NULL, dry_run);
#endif
#else
......
......@@ -120,7 +120,7 @@ size_t sizeOfType(enum DATA_TYPE type) {
* Returns an error if the type is not FLOAT or DOUBLE
*/
int isDoublePrecision(enum DATA_TYPE type) {
switch (type) {
case FLOAT:
return 0;
......
......@@ -17,6 +17,7 @@
*
******************************************************************************/
#include "adiabatic_index.h"
#include "io_properties.h"
#include "kernel_hydro.h"
......@@ -51,6 +52,12 @@ void hydro_read_particles(struct part* parts, struct io_props* list,
UNIT_CONV_DENSITY, parts, rho);
}
float convert_u(struct part* p) {
return p->entropy * pow_gamma_minus_one(p->rho) *
hydro_one_over_gamma_minus_one;
}
/**
* @brief Specifies which particle fields to write to a dataset
*
......@@ -61,7 +68,7 @@ void hydro_read_particles(struct part* parts, struct io_props* list,
void hydro_write_particles(struct part* parts, struct io_props* list,
int* num_fields) {
*num_fields = 8;
*num_fields = 9;
/* List what we want to write */
list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH,
......@@ -80,6 +87,9 @@ void hydro_write_particles(struct part* parts, struct io_props* list,
UNIT_CONV_ACCELERATION, parts, a_hydro);
list[7] =
io_make_output_field("Density", FLOAT, 1, UNIT_CONV_DENSITY, parts, rho);
list[8] = io_make_output_field_convert_part("InternalEnergy", FLOAT, 1,
UNIT_CONV_ENERGY_PER_UNIT_MASS,
parts, rho, convert_u);
}
/**
......@@ -102,4 +112,4 @@ void writeSPHflavour(hid_t h_grpsph) {
*
* @return 1 if entropy is in 'internal energy', 0 otherwise.
*/
int writeEntropyFlag() { return 1; }
int writeEntropyFlag() { return 0; }
......@@ -54,6 +54,16 @@ struct io_props {
/* The size of the particles */
size_t partSize;
/* The particle arrays */
struct part* parts;
struct gpart* gparts;
/* Conversion function for part */
float (*convert_part)(struct part*);
/* Conversion function for part */
float (*convert_gpart)(struct gpart*);
};
/**
......@@ -68,7 +78,7 @@ struct io_props {
*
* @param name Name of the field to read
* @param type The type of the data
* @param dimension Dataset dimension (!D, 3D, ...)
* @param dimension Dataset dimension (1D, 3D, ...)
* @param importance Is this dataset compulsory ?
* @param units The units of the dataset
* @param field Pointer to the field of the first particle
......@@ -89,6 +99,10 @@ struct io_props io_make_input_field_(char name[FIELD_BUFFER_SIZE],
r.units = units;
r.field = field;
r.partSize = partSize;
r.parts = NULL;
r.gparts = NULL;
r.convert_part = NULL;
r.convert_gpart = NULL;
return r;
}
......@@ -105,7 +119,7 @@ struct io_props io_make_input_field_(char name[FIELD_BUFFER_SIZE],
*
* @param name Name of the field to read
* @param type The type of the data
* @param dimension Dataset dimension (!D, 3D, ...)
* @param dimension Dataset dimension (1D, 3D, ...)
* @param units The units of the dataset
* @param field Pointer to the field of the first particle
* @param partSize The size in byte of the particle
......@@ -124,6 +138,100 @@ struct io_props io_make_output_field_(char name[FIELD_BUFFER_SIZE],
r.units = units;
r.field = field;
r.partSize = partSize;
r.parts = NULL;
r.gparts = NULL;
r.convert_part = NULL;
r.convert_gpart = NULL;
return r;
}
/**
* @brief Constructs an #io_props (with conversion) from its parameters
*/
#define io_make_output_field_convert_part(name, type, dim, units, part, field, \
convert) \
io_make_output_field_convert_part_(name, type, dim, units, \
(char*)(&(part[0]).field), \
sizeof(part[0]), part, convert)
/**
* @brief Construct an #io_props from its parameters
*
* @param name Name of the field to read
* @param type The type of the data
* @param dimension Dataset dimension (1D, 3D, ...)
* @param importance Is this dataset compulsory ?
* @param units The units of the dataset
* @param field Pointer to the field of the first particle
* @param partSize The size in byte of the particle
* @param parts The particle array
* @param functionPtr The function used to convert a particle to a float
*
* Do not call this function directly. Use the macro defined above.
*/
struct io_props io_make_output_field_convert_part_(
char name[FIELD_BUFFER_SIZE], enum DATA_TYPE type, int dimension,
enum UnitConversionFactor units, char* field, size_t partSize,
struct part* parts, float (*functionPtr)(struct part*)) {
struct io_props r;
strcpy(r.name, name);
r.type = type;
r.dimension = dimension;
r.importance = 0;
r.units = units;
r.field = field;
r.partSize = partSize;
r.parts = parts;
r.gparts = NULL;
r.convert_part = functionPtr;
r.convert_gpart = NULL;
return r;
}
/**
* @brief Constructs an #io_props (with conversion) from its parameters
*/
#define io_make_output_field_convert_part(name, type, dim, units, part, field, \
convert) \
io_make_output_field_convert_part_(name, type, dim, units, \
(char*)(&(part[0]).field), \
sizeof(part[0]), part, convert)
/**
* @brief Construct an #io_props from its parameters
*
* @param name Name of the field to read
* @param type The type of the data
* @param dimension Dataset dimension (1D, 3D, ...)
* @param importance Is this dataset compulsory ?
* @param units The units of the dataset
* @param field Pointer to the field of the first particle
* @param partSize The size in byte of the particle
* @param parts The particle array
* @param functionPtr The function used to convert a particle to a float
*
* Do not call this function directly. Use the macro defined above.
*/
struct io_props io_make_output_field_convert_gpart_(
char name[FIELD_BUFFER_SIZE], enum DATA_TYPE type, int dimension,
enum UnitConversionFactor units, char* field, size_t partSize,
struct gpart* gparts, float (*functionPtr)(struct gpart*)) {
struct io_props r;
strcpy(r.name, name);
r.type = type;
r.dimension = dimension;
r.importance = 0;
r.units = units;
r.field = field;
r.partSize = partSize;
r.parts = NULL;
r.gparts = gparts;
r.convert_part = NULL;
r.convert_gpart = functionPtr;
return r;
}
......
......@@ -39,9 +39,9 @@
#include "common_io.h"
#include "engine.h"
#include "error.h"
#include "hydro_properties.h"
#include "gravity_io.h"
#include "hydro_io.h"
#include "hydro_properties.h"
#include "io_properties.h"
#include "kernel_hydro.h"
#include "part.h"
......
......@@ -39,9 +39,9 @@
#include "common_io.h"
#include "engine.h"
#include "error.h"
#include "hydro_properties.h"
#include "gravity_io.h"
#include "hydro_io.h"
#include "hydro_properties.h"
#include "io_properties.h"
#include "kernel_hydro.h"
#include "part.h"
......
......@@ -38,9 +38,9 @@
#include "common_io.h"
#include "engine.h"
#include "error.h"
#include "hydro_properties.h"
#include "gravity_io.h"
#include "hydro_io.h"
#include "hydro_properties.h"
#include "io_properties.h"
#include "kernel_hydro.h"
#include "part.h"
......@@ -181,9 +181,20 @@ void writeArray(hid_t grp, char* fileName, FILE* xmfFile,
if (temp == NULL) error("Unable to allocate memory for temporary buffer");
/* Copy particle data to temporary buffer */
char* temp_c = temp;
for (size_t i = 0; i < N; ++i)
memcpy(&temp_c[i * copySize], props.field + i * props.partSize, copySize);
if (props.convert_part == NULL &&
props.convert_gpart == NULL) { /* No conversion */
char* temp_c = temp;
for (size_t i = 0; i < N; ++i)
memcpy(&temp_c[i * copySize], props.field + i * props.partSize, copySize);
} else if (props.convert_part != NULL) { /* conversion (for parts)*/
float* temp_f = temp;
for (size_t i = 0; i < N; ++i)
temp_f[i] = props.convert_part(&props.parts[i]);
} else if (props.convert_part != NULL) { /* conversion (for gparts)*/
float* temp_f = temp;
for (size_t i = 0; i < N; ++i)
temp_f[i] = props.convert_gpart(&props.gparts[i]);
}
/* Unit conversion if necessary */
const double factor =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment