Add filaments postproc, improve units detection, add automatic map rules, add selection

This commit is contained in:
Noe Brucy
2020-10-16 17:44:34 +02:00
parent 013aab911e
commit e05477cb7a
7 changed files with 867 additions and 211 deletions
+66 -41
View File
@@ -88,47 +88,7 @@ class Comparator(Aggregator, HDF5Container):
)
return missing_nums
def _get_units(self, unit, data=None):
"""
Get real units from info files
unit is either:
1. An instance of cst.Unit (pymses unit class)
2. A string beginning by "unit_", referring to a code unit,
available in self.info
3. A dict {unit1 : exp1, unit2: exp2, ...} with unitX as 2.
and expX a float, referring to the compound unit
unit1**exp1 * unit2**exp2
4. A dict {key: unit, ...} where key is a field name (eg. 'time', or 'mass')
and unit the corresponding unit (on one on the above format)
Returns:
1-3. : a cst.Unit instance
4. : a dict {key: unit, ...} with same key as input and unit being cst.Unit instances
"""
if isinstance(unit, cst.Unit):
return unit
if isinstance(unit, str) and unit[:5] == "unit_":
res = self.info[unit]
if unit == "unit_length":
res = res / self.info["boxlen"]
return res
if list(unit)[0][:5] == "unit_":
new_unit = cst.none
for base_unit_str in unit:
expo = unit[base_unit_str]
base_unit = self._get_units(base_unit_str)
new_unit = new_unit * base_unit ** expo
return new_unit
if (not data is None) and isinstance(data, dict) and list(unit)[0] in data:
for key in unit:
unit[key] = self._get_units(unit[key])
return unit
else:
raise ValueError("Invalid unit")
def _save_data(self, name_full, data, description, unit):
unit = self._get_units(unit, data=data)
super(Comparator, self)._save_data(name_full, data, description, unit)
self.save.get_node(name_full)._v_attrs.nums = self.nums
@@ -276,6 +236,24 @@ class Comparator(Aggregator, HDF5Container):
series["sfr"][run].append(sfr)
return series
def _extract_cons_from_log(self, series, log_filename, run):
cmd_grep = "grep 'Main step' {} -A 2".format(log_filename)
content = os.popen(cmd_grep).readlines()
for i in range(0, len(content), 4):
series["time"][run].append(
np.float(content[i + 2].split("=")[2].split()[0])
)
series["step"][run].append(np.int(content[i].split("=")[1].split()[0]))
series["mcons"][run].append(np.float(content[i].split("=")[2].split()[0]))
series["econs"][run].append(np.float(content[i].split("=")[3].split()[0]))
series["epot"][run].append(np.float(content[i].split("=")[4].split()[0]))
series["ekin"][run].append(np.float(content[i].split("=")[5].split()[0]))
series["eint"][run].append(np.float(content[i].split("=")[6].split()[0]))
series["emag"][run].append(
np.float(content[i + 1].split("=")[1].split()[0])
)
return series
def _extract_rms_from_log(self, series, log_filename, run):
cmd_grep = "grep 'turbulent rms' {} -C 1".format(log_filename)
content = os.popen(cmd_grep).readlines()
@@ -334,7 +312,8 @@ class Comparator(Aggregator, HDF5Container):
ssfr = {}
for run in self.runs:
# Surface of the box in pc^2
surface = (self.info["unit_length"].express(cst.pc)) ** 2
info = self.pp[run][self.nums[run][0]].info
surface = (info["unit_length"].express(cst.pc)) ** 2
# WARNING : We do not multiply by boxlen since already done in 'unit_length' (pymses)
time = self.save.get_node("/series/sinks_from_log/time/" + run).read()
@@ -360,6 +339,22 @@ class Comparator(Aggregator, HDF5Container):
return ssfr, {"avg_window": avg_window}
def _surfacic_sink_mass(self):
mass_unit = self.save.get_node("/series/sinks_from_log/mass_sink")._v_attrs.unit
ssm = {}
for run in self.runs:
# Surface of the box in pc^2
info = self.pp[run][self.nums[run][0]].info
surface = (info["unit_length"].express(cst.pc)) ** 2
mass_sink = self.save.get_node(
"/series/sinks_from_log/mass_sink/" + run
).read()
mass_sink = mass_sink * mass_unit.express(cst.Msun)
ssm[run] = mass_sink / surface
return ssm
def _turb_power(self):
turb_power = {}
for run in self.runs:
@@ -475,6 +470,14 @@ class Comparator(Aggregator, HDF5Container):
description="Instantaneous surfacic star formation rate",
dependencies=["sinks_from_log"],
),
"ssm": Rule(
self,
self._surfacic_sink_mass,
group="/series/sinks_from_log",
unit=cst.Msun / cst.pc ** 2,
description="Surfacic sink mass",
dependencies=["sinks_from_log"],
),
"sfr_from_log": Rule(
self,
partial(self._from_log, ["time", "sfr"], self._extract_sfr_from_log),
@@ -510,6 +513,25 @@ class Comparator(Aggregator, HDF5Container):
"turb_energy": "Injected turbulent energy",
},
),
"cons_from_log": Rule(
self,
partial(
self._from_log,
["time", "step", "mcons", "econs", "epot", "ekin", "eint", "emag"],
self._extract_cons_from_log,
),
group="/series",
unit={
"time": "unit_time",
"step": cst.none,
"mcons": cst.none,
"econs": cst.none,
"epot": cst.none, # TODO find unit
"ekin": cst.none,
"eint": cst.none,
"emag": cst.none,
},
),
"turb_power": Rule(
self,
self._turb_power,
@@ -574,6 +596,9 @@ class Comparator(Aggregator, HDF5Container):
self._gen_rule_time_global("mwa_sigma", "time_sigma", unit="unit_velocity")
self._gen_rule_time_global("max_fluct_coldens")
self._gen_rule_time_global(
"mass", unit=self.info["unit_density"] * self.info["unit_length"] ** 3
)
self._gen_rule_time_global("mwa_B_int", unit="unit_mag")
for name in [