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

Commit 53173af4 authored by lhausamm's avatar lhausamm
Browse files

Add selection of output fields

parent 1d2a43c0
......@@ -198,11 +198,12 @@ int main(int argc, char *argv[]) {
char *cmdparams[PARSER_MAX_NO_OF_PARAMS];
char paramFileName[200] = "";
char restart_file[200] = "";
char outputFieldsFileName[200] = "";
unsigned long long cpufreq = 0;
/* Parse the parameters */
int c;
while ((c = getopt(argc, argv, "acCdDef:FgGhMn:P:rsSt:Tv:y:Y:")) != -1)
while ((c = getopt(argc, argv, "acCdDef:FgGhMn:o:P:rsSt:Tv:y:Y:")) != -1)
switch (c) {
case 'a':
#if defined(HAVE_SETAFFINITY) && defined(HAVE_LIBNUMA)
......@@ -259,6 +260,15 @@ int main(int argc, char *argv[]) {
return 1;
}
break;
case 'o':
if (sscanf(optarg, "%s", outputFieldsFileName) != 1) {
if (myrank == 0)
{
printf("Error parsing output fields filename");
print_help_message();
}
}
break;
case 'P':
cmdparams[nparams] = optarg;
nparams++;
......@@ -457,6 +467,25 @@ int main(int argc, char *argv[]) {
MPI_Bcast(params, sizeof(struct swift_params), MPI_BYTE, 0, MPI_COMM_WORLD);
#endif
/* Read output fields */
struct swift_params *output_fields =
(struct swift_params *)malloc(sizeof(struct swift_params));
if (output_fields == NULL) error("Error allocating memory for the output fields file.");
if (myrank == 0) {
message("Reading runtime output fields from file '%s'", outputFieldsFileName);
if (strcmp(outputFieldsFileName, "") != 0)
parser_read_file(outputFieldsFileName, output_fields);
else
parser_init(outputFieldsFileName, output_fields);
/* And dump the parameters as used. */
parser_write_params_to_file(output_fields, "used_output_fields.yml");
}
#ifdef WITH_MPI
/* Broadcast the parameter file */
MPI_Bcast(output_fields, sizeof(struct swift_params), MPI_BYTE, 0, MPI_COMM_WORLD);
#endif
/* Check that we can write the snapshots by testing if the output
* directory exists and is searchable and writable. */
char basename[PARSER_MAX_LINE_SIZE];
......
......@@ -5484,6 +5484,7 @@ void engine_init(struct engine *e, struct space *s,
e->chemistry = chemistry;
e->sourceterms = sourceterms;
e->parameter_file = params;
e->output_fields = output_fields;
#ifdef WITH_MPI
e->cputime_last_step = 0;
e->last_repartition = 0;
......
......@@ -312,6 +312,9 @@ struct engine {
/* The (parsed) parameter file */
const struct swift_params *parameter_file;
/* The (parsed) output fields */
const struct swift_params *output_fields;
/* Temporary struct to hold a group of deferable properties (in MPI mode
* these are reduced together, but may not be required just yet). */
struct collectgroup1 collect_group1;
......
......@@ -106,15 +106,15 @@ void darkmatter_write_particles(const struct gpart* gparts,
/* List what we want to write */
list[0] = io_make_output_field_convert_gpart(
"Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, gparts, convert_gpart_pos);
"Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, gparts, convert_gpart_pos, 1);
list[1] = io_make_output_field_convert_gpart(
"Velocities", FLOAT, 3, UNIT_CONV_SPEED, gparts, convert_gpart_vel);
"Velocities", FLOAT, 3, UNIT_CONV_SPEED, gparts, convert_gpart_vel, 1);
list[2] =
io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, gparts, mass);
io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, gparts, mass, 1);
list[3] = io_make_output_field("ParticleIDs", ULONGLONG, 1,
UNIT_CONV_NO_UNITS, gparts, id_or_neg_offset);
UNIT_CONV_NO_UNITS, gparts, id_or_neg_offset, 1);
list[4] = io_make_output_field("Potential", FLOAT, 1, UNIT_CONV_POTENTIAL,
gparts, potential);
gparts, potential, 1);
}
#endif /* SWIFT_DEFAULT_GRAVITY_IO_H */
......@@ -139,28 +139,28 @@ void hydro_write_particles(const struct part* parts, const struct xpart* xparts,
/* List what we want to write */
list[0] = io_make_output_field_convert_part("Coordinates", DOUBLE, 3,
UNIT_CONV_LENGTH, parts, xparts,
convert_part_pos);
convert_part_pos, 1);
list[1] = io_make_output_field_convert_part(
"Velocities", FLOAT, 3, UNIT_CONV_SPEED, parts, xparts, convert_part_vel);
"Velocities", FLOAT, 3, UNIT_CONV_SPEED, parts, xparts, convert_part_vel, 1);
list[2] =
io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, parts, mass);
io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, parts, mass, 1);
list[3] = io_make_output_field("SmoothingLength", FLOAT, 1, UNIT_CONV_LENGTH,
parts, h);
parts, h, 1);
list[4] = io_make_output_field(
"Entropy", FLOAT, 1, UNIT_CONV_ENTROPY_PER_UNIT_MASS, parts, entropy);
"Entropy", FLOAT, 1, UNIT_CONV_ENTROPY_PER_UNIT_MASS, parts, entropy, 1);
list[5] = io_make_output_field("ParticleIDs", ULONGLONG, 1,
UNIT_CONV_NO_UNITS, parts, id);
UNIT_CONV_NO_UNITS, parts, id, 1);
list[6] =
io_make_output_field("Density", FLOAT, 1, UNIT_CONV_DENSITY, parts, rho);
io_make_output_field("Density", FLOAT, 1, UNIT_CONV_DENSITY, parts, rho, 1);
list[7] = io_make_output_field_convert_part("InternalEnergy", FLOAT, 1,
UNIT_CONV_ENERGY_PER_UNIT_MASS,
parts, xparts, convert_part_u);
parts, xparts, convert_part_u, 1);
list[8] = io_make_output_field_convert_part(
"Pressure", FLOAT, 1, UNIT_CONV_PRESSURE, parts, xparts, convert_part_P);
"Pressure", FLOAT, 1, UNIT_CONV_PRESSURE, parts, xparts, convert_part_P, 1);
list[9] = io_make_output_field_convert_part("Potential", FLOAT, 1,
UNIT_CONV_POTENTIAL, parts,
xparts, convert_part_potential);
xparts, convert_part_potential, 1);
#ifdef DEBUG_INTERACTIONS_SPH
......@@ -168,15 +168,15 @@ void hydro_write_particles(const struct part* parts, const struct xpart* xparts,
*num_fields += 4;
list[0] = io_make_output_field("Num_ngb_density", INT, 1, UNIT_CONV_NO_UNITS,
parts, num_ngb_density);
parts, num_ngb_density, 1);
list[1] = io_make_output_field("Num_ngb_force", INT, 1, UNIT_CONV_NO_UNITS,
parts, num_ngb_force);
parts, num_ngb_force, 1);
list[2] =
io_make_output_field("Ids_ngb_density", LONGLONG, MAX_NUM_OF_NEIGHBOURS,
UNIT_CONV_NO_UNITS, parts, ids_ngbs_density);
UNIT_CONV_NO_UNITS, parts, ids_ngbs_density, 1);
list[3] =
io_make_output_field("Ids_ngb_force", LONGLONG, MAX_NUM_OF_NEIGHBOURS,
UNIT_CONV_NO_UNITS, parts, ids_ngbs_force);
UNIT_CONV_NO_UNITS, parts, ids_ngbs_force, 1);
#endif
}
......
......@@ -97,13 +97,17 @@ struct io_props {
/* Conversion function for gpart */
conversion_func_gpart_float convert_gpart_f;
conversion_func_gpart_double convert_gpart_d;
/* default output behaviour
* 0 means not writing, 1 means writing */
int default_output;
};
/**
* @brief Constructs an #io_props from its parameters
*/
#define io_make_input_field(name, type, dim, importance, units, part, field) \
io_make_input_field_(name, type, dim, importance, units, \
io_make_input_field_(name, type, dim, importance, units, \
(char*)(&(part[0]).field), sizeof(part[0]))
/**
......@@ -139,6 +143,7 @@ INLINE static struct io_props io_make_input_field_(
r.convert_part_d = NULL;
r.convert_gpart_f = NULL;
r.convert_gpart_d = NULL;
r.default_output = 0;
return r;
}
......@@ -146,9 +151,9 @@ INLINE static struct io_props io_make_input_field_(
/**
* @brief Constructs an #io_props from its parameters
*/
#define io_make_output_field(name, type, dim, units, part, field) \
#define io_make_output_field(name, type, dim, units, part, field, default_output) \
io_make_output_field_(name, type, dim, units, (char*)(&(part[0]).field), \
sizeof(part[0]))
sizeof(part[0]), default_output)
/**
* @brief Construct an #io_props from its parameters
......@@ -159,12 +164,14 @@ INLINE static struct io_props io_make_input_field_(
* @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 default_output Default output behaviour (0 -> not write, 1 -> write)
*
* Do not call this function directly. Use the macro defined above.
*/
INLINE static struct io_props io_make_output_field_(
const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
enum unit_conversion_factor units, char* field, size_t partSize) {
enum unit_conversion_factor units, char* field, size_t partSize,
int default_output) {
struct io_props r;
strcpy(r.name, name);
r.type = type;
......@@ -180,6 +187,7 @@ INLINE static struct io_props io_make_output_field_(
r.convert_part_d = NULL;
r.convert_gpart_f = NULL;
r.convert_gpart_d = NULL;
r.default_output = default_output;
return r;
}
......@@ -188,9 +196,9 @@ INLINE static struct io_props io_make_output_field_(
* @brief Constructs an #io_props (with conversion) from its parameters
*/
#define io_make_output_field_convert_part(name, type, dim, units, part, xpart, \
convert) \
io_make_output_field_convert_part_##type( \
name, type, dim, units, sizeof(part[0]), part, xpart, convert)
convert, default_output) \
io_make_output_field_convert_part_##type( \
name, type, dim, units, sizeof(part[0]), part, xpart, convert, default_output)
/**
* @brief Construct an #io_props from its parameters
......@@ -203,6 +211,7 @@ INLINE static struct io_props io_make_output_field_(
* @param parts The particle array
* @param xparts The xparticle array
* @param functionPtr The function used to convert a particle to a float
* @param default_output Default output behaviour (0 -> not write, 1 -> write)
*
* Do not call this function directly. Use the macro defined above.
*/
......@@ -210,7 +219,8 @@ INLINE static struct io_props io_make_output_field_convert_part_FLOAT(
const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
enum unit_conversion_factor units, size_t partSize,
const struct part* parts, const struct xpart* xparts,
conversion_func_part_float functionPtr) {
conversion_func_part_float functionPtr,
int default_output) {
struct io_props r;
strcpy(r.name, name);
......@@ -228,6 +238,7 @@ INLINE static struct io_props io_make_output_field_convert_part_FLOAT(
r.convert_part_d = NULL;
r.convert_gpart_f = NULL;
r.convert_gpart_d = NULL;
r.default_output = default_output;
return r;
}
......@@ -243,6 +254,7 @@ INLINE static struct io_props io_make_output_field_convert_part_FLOAT(
* @param parts The particle array
* @param xparts The xparticle array
* @param functionPtr The function used to convert a particle to a float
* @param default_output Default output behaviour (0 -> not write, 1 -> write)
*
* Do not call this function directly. Use the macro defined above.
*/
......@@ -250,7 +262,7 @@ INLINE static struct io_props io_make_output_field_convert_part_DOUBLE(
const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
enum unit_conversion_factor units, size_t partSize,
const struct part* parts, const struct xpart* xparts,
conversion_func_part_double functionPtr) {
conversion_func_part_double functionPtr, int default_output) {
struct io_props r;
strcpy(r.name, name);
......@@ -268,6 +280,7 @@ INLINE static struct io_props io_make_output_field_convert_part_DOUBLE(
r.convert_part_d = functionPtr;
r.convert_gpart_f = NULL;
r.convert_gpart_d = NULL;
r.default_output = default_output;
return r;
}
......@@ -276,9 +289,10 @@ INLINE static struct io_props io_make_output_field_convert_part_DOUBLE(
* @brief Constructs an #io_props (with conversion) from its parameters
*/
#define io_make_output_field_convert_gpart(name, type, dim, units, gpart, \
convert) \
io_make_output_field_convert_gpart_##type(name, type, dim, units, \
sizeof(gpart[0]), gpart, convert)
convert, default_output) \
io_make_output_field_convert_gpart_##type(name, type, dim, units, \
sizeof(gpart[0]), gpart, convert, \
default_output)
/**
* @brief Construct an #io_props from its parameters
......@@ -290,13 +304,15 @@ INLINE static struct io_props io_make_output_field_convert_part_DOUBLE(
* @param gpartSize The size in byte of the particle
* @param gparts The particle array
* @param functionPtr The function used to convert a g-particle to a float
* @param default_output Default output behaviour (0 -> not write, 1 -> write)
*
* Do not call this function directly. Use the macro defined above.
*/
INLINE static struct io_props io_make_output_field_convert_gpart_FLOAT(
const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
enum unit_conversion_factor units, size_t gpartSize,
const struct gpart* gparts, conversion_func_gpart_float functionPtr) {
const struct gpart* gparts, conversion_func_gpart_float functionPtr,
int default_output) {
struct io_props r;
strcpy(r.name, name);
......@@ -314,6 +330,7 @@ INLINE static struct io_props io_make_output_field_convert_gpart_FLOAT(
r.convert_part_d = NULL;
r.convert_gpart_f = functionPtr;
r.convert_gpart_d = NULL;
r.default_output = default_output;
return r;
}
......@@ -328,13 +345,15 @@ INLINE static struct io_props io_make_output_field_convert_gpart_FLOAT(
* @param gpartSize The size in byte of the particle
* @param gparts The particle array
* @param functionPtr The function used to convert a g-particle to a float
* @param default_output Default output behaviour (0 -> not write, 1 -> write)
*
* Do not call this function directly. Use the macro defined above.
*/
INLINE static struct io_props io_make_output_field_convert_gpart_DOUBLE(
const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
enum unit_conversion_factor units, size_t gpartSize,
const struct gpart* gparts, conversion_func_gpart_double functionPtr) {
const struct gpart* gparts, conversion_func_gpart_double functionPtr,
int default_output) {
struct io_props r;
strcpy(r.name, name);
......@@ -352,6 +371,7 @@ INLINE static struct io_props io_make_output_field_convert_gpart_DOUBLE(
r.convert_part_d = NULL;
r.convert_gpart_f = NULL;
r.convert_gpart_d = functionPtr;
r.default_output = default_output;
return r;
}
......
......@@ -1262,10 +1262,15 @@ void write_output_parallel(struct engine* e, const char* baseName,
}
/* Write everything */
for (int i = 0; i < num_fields; ++i)
writeArray(e, h_grp, fileName, partTypeGroupName, list[i], Nparticles,
N_total[ptype], mpi_rank, offset[ptype], internal_units,
snapshot_units);
for (int i = 0; i < num_fields; ++i) {
char field[200] = "OutputFields:";
strcat(field, list[i].name);
int should_write = parser_get_opt_param_int(output_fields, field, list[i].default_output);
if (should_write)
writeArray(e, h_grp, fileName, partTypeGroupName, list[i], Nparticles,
N_total[ptype], mpi_rank, offset[ptype], internal_units,
snapshot_units);
}
/* Free temporary array */
if (dmparts) {
......
......@@ -57,12 +57,23 @@ static void find_duplicate_section(const struct swift_params *params,
static int lineNumber = 0;
/**
* @brief Reads an input file and stores each parameter in a structure.
* @brief Initialize the parser structure.
*
* @param file_name Name of file to be read
* @param params Structure to be populated from file
*/
void parser_init(const char *file_name, struct swift_params *params) {
params->paramCount = 0;
params->sectionCount = 0;
strcpy(params->fileName, file_name);
}
/**
* @brief Reads an input file and stores each parameter in a structure.
*
* @param file_name Name of file to be read
* @param params Structure to be populated from file
*/
void parser_read_file(const char *file_name, struct swift_params *params) {
/* Open file for reading */
FILE *file = fopen(file_name, "r");
......@@ -71,9 +82,7 @@ void parser_read_file(const char *file_name, struct swift_params *params) {
char line[PARSER_MAX_LINE_SIZE];
/* Initialise parameter count. */
params->paramCount = 0;
params->sectionCount = 0;
strcpy(params->fileName, file_name);
parser_init(file_name, params);
/* Check if parameter file exits. */
if (file == NULL) {
......
......@@ -55,6 +55,7 @@ struct swift_params {
};
/* Public API. */
void parser_init(const char *file_name, struct swift_params *params);
void parser_read_file(const char *file_name, struct swift_params *params);
void parser_print_params(const struct swift_params *params);
void parser_write_params_to_file(const struct swift_params *params,
......
......@@ -1006,10 +1006,15 @@ void write_output_serial(struct engine* e, const char* baseName,
}
/* Write everything */
for (int i = 0; i < num_fields; ++i)
writeArray(e, h_grp, fileName, xmfFile, partTypeGroupName, list[i],
Nparticles, N_total[ptype], mpi_rank, offset[ptype],
internal_units, snapshot_units);
for (int i = 0; i < num_fields; ++i) {
char field[200] = "OutputFields:";
strcat(field, list[i].name);
int should_write = parser_get_opt_param_int(output_fields, field, list[i].default_output);
if (should_write)
writeArray(e, h_grp, fileName, xmfFile, partTypeGroupName, list[i],
Nparticles, N_total[ptype], mpi_rank, offset[ptype],
internal_units, snapshot_units);
}
/* Free temporary array */
if (dmparts) {
......
......@@ -594,6 +594,7 @@ void write_output_single(struct engine* e, const char* baseName,
struct gpart* dmparts = NULL;
const struct spart* sparts = e->s->sparts;
const struct cooling_function_data* cooling = e->cooling_func;
const struct swift_params* output_fields = e->output_fields;
/* Number of unassociated gparts */
const size_t Ndm = Ntot > 0 ? Ntot - (Ngas + Nstars) : 0;
......@@ -825,9 +826,14 @@ void write_output_single(struct engine* e, const char* baseName,
}
/* Write everything */
for (int i = 0; i < num_fields; ++i)
writeArray(e, h_grp, fileName, xmfFile, partTypeGroupName, list[i], N,
internal_units, snapshot_units);
for (int i = 0; i < num_fields; ++i) {
char field[200] = "OutputFields:";
strcat(field, list[i].name);
int should_write = parser_get_opt_param_int(output_fields, field, list[i].default_output);
if (should_write)
writeArray(e, h_grp, fileName, xmfFile, partTypeGroupName, list[i], N,
internal_units, snapshot_units);
}
/* Free temporary array */
if (dmparts) {
......
......@@ -60,13 +60,13 @@ void star_write_particles(const struct spart* sparts, struct io_props* list,
/* List what we want to read */
list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH,
sparts, x);
sparts, x, 1);
list[1] =
io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, sparts, v);
io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, sparts, v, 1);
list[2] =
io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, sparts, mass);
io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, sparts, mass, 1);
list[3] = io_make_output_field("ParticleIDs", LONGLONG, 1, UNIT_CONV_NO_UNITS,
sparts, id);
sparts, id, 1);
}
#endif /* SWIFT_DEFAULT_STAR_IO_H */
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