Skip to content

Commit ec7af2e

Browse files
authored
reduce boilerplate code in physics by introducing plus-separated Formulae ctor args (#1314)
1 parent 91d0307 commit ec7af2e

File tree

14 files changed

+57
-89
lines changed

14 files changed

+57
-89
lines changed

PySDM/formulae.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def _formula(func, constants, dimensional_analysis, **kw):
224224

225225
def _boost(obj, fastmath, constants, dimensional_analysis):
226226
"""returns JIT-compiled, `c_inline`-equipped formulae with the constants catalogue attached"""
227-
formulae = {"__name__": obj.__class__.__name__}
227+
formulae = {"__name__": obj.__name__}
228228
for item in dir(obj):
229229
attr = getattr(obj, item)
230230
if item.startswith("__") or not callable(attr):
@@ -288,13 +288,40 @@ def _c_inline(fun, return_type=None, constants=None, **args):
288288

289289

290290
def _pick(value: str, choices: dict, constants: namedtuple):
291-
"""selects a given physics logic and instantiates it passing the constants catalogue"""
292-
for name, cls in choices.items():
293-
if name == value:
294-
return cls(constants)
295-
raise ValueError(
296-
f"Unknown setting: '{value}'; choices are: {tuple(choices.keys())}"
297-
)
291+
"""
292+
selects a given physics logic and instantiates it passing the constants catalogue;
293+
`value` is expected to be string containing a plus-separated list of class names
294+
"""
295+
296+
obj = None
297+
if "+" not in value:
298+
for name, cls in choices.items():
299+
if name == value:
300+
obj = cls(constants)
301+
else:
302+
parent_classes = []
303+
for name in value.split("+"):
304+
if name not in choices:
305+
parent_classes.clear()
306+
break
307+
parent_classes.append(choices[name])
308+
309+
if len(parent_classes) > 0:
310+
311+
class Cls(*parent_classes): # pylint: disable=too-few-public-methods
312+
def __init__(self, const):
313+
for cls in parent_classes:
314+
cls.__init__(self, const)
315+
316+
obj = Cls(constants)
317+
318+
if obj is None:
319+
raise ValueError(
320+
f"Unknown setting: '{name}'; choices are: {tuple(choices.keys())}"
321+
)
322+
323+
obj.__name__ = value # pylint: disable=attribute-defined-outside-init
324+
return obj
298325

299326

300327
def _choices(module):

PySDM/physics/isotope_equilibrium_fractionation_factors/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
phase equilibrium, defined as ratios alpha of isotopic ratios R """
33

44
from .barkan_and_luz_2005 import BarkanAndLuz2005
5-
from .bolot_et_al_2013 import BolotEtAl2013
6-
from .graf_phd_2017 import GrafPhD2017
75
from .horita_and_wesolowski_1994 import HoritaAndWesolowski1994
86
from .majoube_1970 import Majoube1970
97
from .majoube_1971 import Majoube1971
108
from .merlivat_and_nief_1967 import MerlivatAndNief1967
119
from .null import Null
12-
from .pierchala_et_al_2022 import PierchalaEtAl2022
1310
from .van_hook_1968 import VanHook1968
1411
from .lamb_et_al_2017 import LambEtAl2017
1512
from .ellehoj_et_al_2013 import EllehojEtAl2013

PySDM/physics/isotope_equilibrium_fractionation_factors/bolot_et_al_2013.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

PySDM/physics/isotope_equilibrium_fractionation_factors/graf_phd_2017.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

PySDM/physics/isotope_equilibrium_fractionation_factors/pierchala_et_al_2022.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

PySDM/physics/isotope_meteoric_water_line_excess/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
from .barkan_and_luz_2007 import BarkanAndLuz2007
44
from .dansgaard_1964 import Dansgaard1964
55
from .null import Null
6-
from .pierchala_et_al_2022 import PierchalaEtAl2022

PySDM/physics/isotope_meteoric_water_line_excess/pierchala_et_al_2022.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/PySDM_examples/Bolot_et_al_2013/fig_1.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
"outputs": [],
6969
"source": [
70-
"formulae = Formulae(isotope_equilibrium_fractionation_factors=\"BolotEtAl2013\")\n",
70+
"formulae = Formulae(isotope_equilibrium_fractionation_factors=\"MerlivatAndNief1967+Majoube1970+Majoube1971\")\n",
7171
"alphas = formulae.isotope_equilibrium_fractionation_factors\n",
7272
"const = formulae.constants"
7373
]

examples/PySDM_examples/Graf_et_al_2019/Table_1.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"outputs": [],
5858
"source": [
5959
"formulae = Formulae(\n",
60-
" isotope_equilibrium_fractionation_factors='GrafPhD2017',\n",
60+
" isotope_equilibrium_fractionation_factors='Majoube1970+Majoube1971+MerlivatAndNief1967',\n",
6161
" isotope_meteoric_water_line_excess='Dansgaard1964'\n",
6262
")\n",
6363
"const = formulae.constants\n",

examples/PySDM_examples/Pierchala_et_al_2022/fig_3.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
"outputs": [],
7171
"source": [
7272
"formulae = Formulae(\n",
73-
" isotope_equilibrium_fractionation_factors='PierchalaEtAl2022',\n",
74-
" isotope_meteoric_water_line_excess='PierchalaEtAl2022',\n",
73+
" isotope_equilibrium_fractionation_factors='BarkanAndLuz2005+HoritaAndWesolowski1994',\n",
74+
" isotope_meteoric_water_line_excess='Dansgaard1964+BarkanAndLuz2007',\n",
7575
" isotope_ratio_evolution='RayleighDistillation'\n",
7676
")\n",
7777
"const = formulae.constants\n",

0 commit comments

Comments
 (0)