diff --git a/examples/SedovBlast_3D/run.sh b/examples/SedovBlast_3D/run.sh index 00d5e5b91c31e64f824a3d2a28c8e1a126684a74..590ed5efa7dfd4ed7677f3eb9627d1139d20fc8b 100755 --- a/examples/SedovBlast_3D/run.sh +++ b/examples/SedovBlast_3D/run.sh @@ -12,6 +12,9 @@ then python makeIC.py fi +# Generate output fields +../create_ouput_fields.py ../../config.h -o output_fields.yml -d ../output_fields_examples/ + # Run SWIFT ../swift -s -t 4 sedov.yml 2>&1 | tee output.log diff --git a/examples/SedovBlast_3D/sedov.yml b/examples/SedovBlast_3D/sedov.yml index 5368c02645af9a170e0401c6772532ed4b689e77..d73f70be0b5b708f0b2bed80122d1cabab6e5d72 100644 --- a/examples/SedovBlast_3D/sedov.yml +++ b/examples/SedovBlast_3D/sedov.yml @@ -19,7 +19,7 @@ Snapshots: time_first: 0. # Time of the first output (in internal units) delta_time: 1e-2 # Time difference between consecutive outputs (in internal units) compression: 4 - FieldsFilename: ../output_fields_example.yml + FieldsFilename: output_fields.yml # Parameters governing the conserved quantities statistics Statistics: diff --git a/examples/create_ouput_fields.py b/examples/create_ouput_fields.py new file mode 100644 index 0000000000000000000000000000000000000000..86ca8a585dfc1ed5af85b8b6b4c0bf9a0cdf9451 --- /dev/null +++ b/examples/create_ouput_fields.py @@ -0,0 +1,291 @@ +#!/usr/bin/env python3 + +from optparse import OptionParser +from regex import search +from os import path, remove + +# fields to do +todo = [ + "chemistry", "cooling", + "gravity", "stars", + "hydro"] + +# number of particle type +N_type = 6 + + +def parseOptions(): + """ + Parse options. + + Returns + ------- + + opt: + Option structure + + args: + Arguments + """ + usage = """usage: %prog [options] args1 + If options are not provided, this script will read the config.h file \ +provided (args1) and use theses values. + """ + + parser = OptionParser(usage=usage) + + parser.add_option("--with-cooling", + dest="cooling", + help="Cooling Type", + default="", + type=str, + metavar="STR") + + parser.add_option("--with-chemistry", + dest="chemistry", + help="Chemistry Type", + default="", + type=str, + metavar="STR") + + parser.add_option("--with-hydro", + dest="hydro", + help="Hydro Type", + default="", + type=str, + metavar="STR") + + parser.add_option("--with-stars", + dest="stars", + help="Stars Type", + default="default", + type=str, + metavar="STR") + + parser.add_option("--with-gravity", + dest="gravity", + help="Gravity Type", + default="default", + type=str, + metavar="STR") + + parser.add_option("-o", "--output", + dest="o", + help="Output filename", + type=str, + default=None, + metavar="STR") + + parser.add_option("-d", "--example_directory", + dest="d", + help="Directory containing the examples", + type=str, + default=None, + metavar="STR") + + return parser.parse_args() + + +def readConfig(filename): + """ + Read the config.h file and parse it to keep + only the definitions. + + Parameters + ---------- + + filename: str + Filename to parse + + Returns + ------- + + options: list + List of options + """ + expression = "#define (.*) 1" + options = [] + with open(filename, "r") as f: + for line in f: + tmp = search(expression, line) + if tmp is not None: + options.append(tmp.group(1)) + + return options + + +def getConfig(opt, swift_options, current): + """ + Parse the config.h options and extract the required + parameter. If not able to find an option, return False + + Parameters + ---------- + + opt: + Option structure + + swift_options: list + Option in config.h + + current: str + Current option + + Returns + ------- + + option: str + name of the option (in lower case) + """ + # if given in options, returns + if getattr(opt, current) != "": + return getattr(opt, current) + + # get current option + caps = current.upper() + expression = "%s_(.*)" % caps + if current == "hydro": + expression = "(.*)_SPH" + for i in swift_options: + tmp = search(expression, i) + if tmp is not None: + return tmp.group(1).lower() + return False + + +def readYamlFile(filename): + """ + Read quickly a yaml file. + + Parameters + ---------- + + filename: str + file to read + + Returns + ------- + + d: dict + key -> section name, value -> list of parameters + """ + d = {} + key = None + section = "(PartType\d+):" + with open(filename, "r") as f: + for line in f: + # check for section + tmp = search(section, line) + if tmp is not None: + key = tmp.group(1) + d[key] = [] + continue + + # skip if not a parameter or outside + # a section + if ":" not in line or key is None: + continue + + # ensure end of line + if "\n" not in line: + line += "\n" + + # write line + d[key].append(line) + + return d + + +def generateFile(opt): + """ + Generate an output fields file from examples. + + Parameters + ---------- + + opt: + Option structure + + Returns + ------- + + d: dict + key -> section name, value -> list of parameters + """ + d = {} + # read all files + for current in todo: + filename = current + "_" + getattr(opt, current) + ".yml" + filename = opt.d + "/" + filename + data = readYamlFile(filename) + + for tpe in range(N_type): + name = "PartType%i" % tpe + if name not in data: + continue + + if name not in d: + d[name] = data[name] + + else: + d[name].extend(data[name]) + return d + + +def writeOutputFields(d, opt): + """ + Write the output. + + Parameters + ---------- + + d: dict + key -> section name, value -> list of parameters + + opt: + Option structure + + """ + with open(opt.o, "w") as f: + for tpe in range(N_type): + name = "PartType%i" % tpe + if name not in d: + continue + f.write("%s:\n" % name) + for i in d[name]: + f.write(i) + + f.write("\n") + + +if __name__ == "__main__": + # parse option + opt, args = parseOptions() + + # check inputs + # arguments + if len(args) != 1: + raise IOError("A file should be provided") + filename = args[0] + + # output file + if opt.o is None: + raise IOError("The output option '-o' is required") + + # example directory + if opt.d is None: + raise IOError("The example directory option '-d' is required") + + # read configuration file + swift_options = readConfig(filename) + + # get correct configuration + for current in todo: + tmp = getConfig(opt, swift_options, current) + if tmp is False: + raise IOError("Unable to get field %s" % current) + setattr(opt, current, tmp) + + # generate and write output_fields file + d = generateFile(opt) + writeOutputFields(d, opt) diff --git a/examples/main.c b/examples/main.c index 605b2d330795543c0dcf4c4ca915798bd75aeb48..ec0787bfd394b00bdf2eb91ed5e559d42f8fb677 100644 --- a/examples/main.c +++ b/examples/main.c @@ -469,11 +469,8 @@ int main(int argc, char *argv[]) { 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); - + parser_read_file(outputFieldsFileName, output_fields); + /* And dump the parameters as used. */ parser_write_params_to_file(output_fields, "used_output_fields.yml"); } diff --git a/examples/output_fields_example.yml b/examples/output_fields_example.yml deleted file mode 100644 index 22dfdc35a91efeabdc96fbdcb92dfa65fbc191b8..0000000000000000000000000000000000000000 --- a/examples/output_fields_example.yml +++ /dev/null @@ -1,27 +0,0 @@ -# Gas -PartType0: - Coordinates: 1 # Request to write output - Masses: 1 - Velocities: 1 - SmoothingLength: 1 - Entropy: 1 - ParticleIDs: 1 - Density: 1 - InternalEnergy: 0 # Request to not write output - Pressure: 0 - Potential: 0 - -# Dark Matter -PartType1: - Coordinates: 1 - Velocities: 1 - Masses: 1 - ParticleIDs: 1 - Potential: 0 - -# Stars -PartType2: - Coordinates: 1 - Velocities: 1 - Masses: 1 - ParticleIDs: 1 \ No newline at end of file diff --git a/examples/output_fields_examples/chemistry_eagle.yml b/examples/output_fields_examples/chemistry_eagle.yml new file mode 100644 index 0000000000000000000000000000000000000000..d62c2b32d2972f233fe7a3962d7b980c9e5007ce --- /dev/null +++ b/examples/output_fields_examples/chemistry_eagle.yml @@ -0,0 +1,14 @@ +# gas +PartType0: + ElementAbundance: 1 + SmoothedElementAbundance: 1 + Metallicity: 1 + SmoothedMetallicity: 1 + TotalMassFromSNIa: 1 + MetalMassFracFromSNIa: 1 + TotalMassFromAGB: 1 + MetalMassFracFromAGB: 1 + TotalMassFromSNII: 1 + MetalMassFracFromSNII: 1 + IronMassFracFromSNIa: 1 + SmoothedIronMassFracFromSNIa: 1 \ No newline at end of file diff --git a/examples/output_fields_examples/chemistry_gear.yml b/examples/output_fields_examples/chemistry_gear.yml new file mode 100644 index 0000000000000000000000000000000000000000..a64dbfb0152a399a163e44c4a954cad7c3f887d8 --- /dev/null +++ b/examples/output_fields_examples/chemistry_gear.yml @@ -0,0 +1,4 @@ +# gas +PartType0: + SmoothedElementAbundance: 1 + ElementAbundance: 1 \ No newline at end of file diff --git a/examples/output_fields_examples/chemistry_none.yml b/examples/output_fields_examples/chemistry_none.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/output_fields_examples/cooling_const_du.yml b/examples/output_fields_examples/cooling_const_du.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/output_fields_examples/cooling_eagle.yml b/examples/output_fields_examples/cooling_eagle.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/output_fields_examples/cooling_grackle.yml b/examples/output_fields_examples/cooling_grackle.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/output_fields_examples/cooling_lambda.yml b/examples/output_fields_examples/cooling_lambda.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/output_fields_examples/cooling_none.yml b/examples/output_fields_examples/cooling_none.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/output_fields_examples/gravity_default.yml b/examples/output_fields_examples/gravity_default.yml new file mode 100644 index 0000000000000000000000000000000000000000..26e54f1fa0ca9b4679c967891c3019fe69e55c6b --- /dev/null +++ b/examples/output_fields_examples/gravity_default.yml @@ -0,0 +1,7 @@ +# Dark Matter +PartType1: + Coordinates: 1 + Velocities: 1 + Masses: 1 + ParticleIDs: 1 + Potential: 1 diff --git a/examples/output_fields_examples/hydro_default.yml b/examples/output_fields_examples/hydro_default.yml new file mode 100644 index 0000000000000000000000000000000000000000..350979d7aa60bb868afd195653599945bdc910c7 --- /dev/null +++ b/examples/output_fields_examples/hydro_default.yml @@ -0,0 +1,10 @@ +# Gas +PartType0: + Coordinates: 1 + Masses: 1 + Velocities: 1 + SmoothingLength: 1 + ParticleIDs: 1 + Density: 1 + InternalEnergy: 1 + Potential: 1 diff --git a/examples/output_fields_examples/hydro_gadget2.yml b/examples/output_fields_examples/hydro_gadget2.yml new file mode 100644 index 0000000000000000000000000000000000000000..9cfcf123df4f8f1a85123069505ca6e6275e0547 --- /dev/null +++ b/examples/output_fields_examples/hydro_gadget2.yml @@ -0,0 +1,17 @@ +# Gas +PartType0: + Coordinates: 1 + Masses: 1 + Velocities: 1 + SmoothingLength: 1 + Entropy: 1 + ParticleIDs: 1 + Density: 1 + InternalEnergy: 1 + Pressure: 1 + Potential: 1 + # debug fields + # Num_ngb_density: 0 + # Num_ngb_force: 0 + # Ids_ngb_density: 0 + # Ids_ngb_force \ No newline at end of file diff --git a/examples/output_fields_examples/hydro_gizmo.yml b/examples/output_fields_examples/hydro_gizmo.yml new file mode 100644 index 0000000000000000000000000000000000000000..17f4231d638ef6eac4ec062f12c6dcaacab5dd80 --- /dev/null +++ b/examples/output_fields_examples/hydro_gizmo.yml @@ -0,0 +1,13 @@ +# Gas +PartType0: + Coordinates: 1 + Masses: 1 + Velocities: 1 + SmoothingLength: 1 + Entropy: 1 + ParticleIDs: 1 + Density: 1 + InternalEnergy: 1 + Pressure: 1 + TotEnergy: 1 + Potential: 1 diff --git a/examples/output_fields_examples/hydro_minimal.yml b/examples/output_fields_examples/hydro_minimal.yml new file mode 100644 index 0000000000000000000000000000000000000000..bbc1ae9371f2cc6d564e293236270f1698e31984 --- /dev/null +++ b/examples/output_fields_examples/hydro_minimal.yml @@ -0,0 +1,12 @@ +# Gas +PartType0: + Coordinates: 1 + Masses: 1 + Velocities: 1 + SmoothingLength: 1 + Entropy: 1 + ParticleIDs: 1 + Density: 1 + InternalEnergy: 1 + Pressure: 1 + Potential: 1 diff --git a/examples/output_fields_examples/hydro_pressureentropy.yml b/examples/output_fields_examples/hydro_pressureentropy.yml new file mode 100644 index 0000000000000000000000000000000000000000..b4ea000c11fb5c23e6034bef4ccf090d935e1261 --- /dev/null +++ b/examples/output_fields_examples/hydro_pressureentropy.yml @@ -0,0 +1,13 @@ +# Gas +PartType0: + Coordinates: 1 + Masses: 1 + Velocities: 1 + SmoothingLength: 1 + Entropy: 1 + ParticleIDs: 1 + Density: 1 + InternalEnergy: 1 + Pressure: 1 + Potential: 1 + WeightedDensity: 1 \ No newline at end of file diff --git a/examples/output_fields_examples/hydro_shadowswift.yml b/examples/output_fields_examples/hydro_shadowswift.yml new file mode 100644 index 0000000000000000000000000000000000000000..edd62d19aea73e80bfb1c4cec663ca4277a38d88 --- /dev/null +++ b/examples/output_fields_examples/hydro_shadowswift.yml @@ -0,0 +1,16 @@ +# Gas +PartType0: + Coordinates: 1 + Masses: 1 + Velocities: 1 + SmoothingLength: 1 + Entropy: 1 + ParticleIDs: 1 + Density: 1 + InternalEnergy: 1 + Pressure: 1 + Potential: 1 + Acceleration: 1 + Volume: 1 + GradDensity: 1 + TotEnergy: 1 \ No newline at end of file diff --git a/examples/output_fields_examples/stars_default.yml b/examples/output_fields_examples/stars_default.yml new file mode 100644 index 0000000000000000000000000000000000000000..bc447626dda7f394f7d6a4be677cd23ad680db85 --- /dev/null +++ b/examples/output_fields_examples/stars_default.yml @@ -0,0 +1,6 @@ +# Stars +PartType2: + Coordinates: 1 + Velocities: 1 + Masses: 1 + ParticleIDs: 1 \ No newline at end of file