Skip to content

Commit 95681a8

Browse files
authored
fix how derives are imported when reading output (#320)
previously, if the solver itself did not define derives.py, then we didn't add any derived variables. But compressible uses a lot of inheritance, and only the base compressible has derives.py. Now we loop over the inheritance hierarchy and try importing derives until we find it. Also add machnumber to the compressible derives
1 parent 9089e92 commit 95681a8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pyro/compressible/derives.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def derive_primitives(myd, varnames):
5757
elif var == "soundspeed":
5858
derived_vars.append(np.sqrt(gamma*p/dens))
5959

60+
elif var == "machnumber":
61+
derived_vars.append(np.sqrt(u**2 + v**2) / np.sqrt(gamma*p/dens))
62+
6063
elif var == "vorticity":
6164
derived_vars.append(vort)
6265

pyro/util/io_pyro.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,20 @@ def read(filename):
127127

128128
sim.read_extras(f)
129129

130-
# check if there are derived variables
131-
try:
132-
derives = importlib.import_module(f"pyro.{solver_name}.derives")
133-
sim.cc_data.add_derived(derives.derive_primitives)
134-
135-
except ModuleNotFoundError:
136-
pass
130+
# check if there are derived variables -- since we do a lot of
131+
# inheritance, we want to start with the deepest class and
132+
# work backwards through the inheritance until we find a derives
133+
# module
134+
for mod in [cls.__module__ for cls in type(sim).__mro__ if cls is not object]:
135+
try:
136+
derives = importlib.import_module(f"{mod.replace('simulation', 'derives')}")
137+
sim.cc_data.add_derived(derives.derive_primitives)
138+
139+
except ModuleNotFoundError:
140+
continue
141+
142+
else:
143+
break
137144

138145
if solver_name is not None:
139146
return sim

0 commit comments

Comments
 (0)