Skip to content

Commit

Permalink
reduce boilerplate code in physics by introducing plus-separated Form…
Browse files Browse the repository at this point in the history
…ulae ctor args (#1314)
  • Loading branch information
slayoo authored Apr 11, 2024
1 parent 91d0307 commit ec7af2e
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 89 deletions.
43 changes: 35 additions & 8 deletions PySDM/formulae.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _formula(func, constants, dimensional_analysis, **kw):

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


def _pick(value: str, choices: dict, constants: namedtuple):
"""selects a given physics logic and instantiates it passing the constants catalogue"""
for name, cls in choices.items():
if name == value:
return cls(constants)
raise ValueError(
f"Unknown setting: '{value}'; choices are: {tuple(choices.keys())}"
)
"""
selects a given physics logic and instantiates it passing the constants catalogue;
`value` is expected to be string containing a plus-separated list of class names
"""

obj = None
if "+" not in value:
for name, cls in choices.items():
if name == value:
obj = cls(constants)
else:
parent_classes = []
for name in value.split("+"):
if name not in choices:
parent_classes.clear()
break
parent_classes.append(choices[name])

if len(parent_classes) > 0:

class Cls(*parent_classes): # pylint: disable=too-few-public-methods
def __init__(self, const):
for cls in parent_classes:
cls.__init__(self, const)

obj = Cls(constants)

if obj is None:
raise ValueError(
f"Unknown setting: '{name}'; choices are: {tuple(choices.keys())}"
)

obj.__name__ = value # pylint: disable=attribute-defined-outside-init
return obj


def _choices(module):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
phase equilibrium, defined as ratios alpha of isotopic ratios R """

from .barkan_and_luz_2005 import BarkanAndLuz2005
from .bolot_et_al_2013 import BolotEtAl2013
from .graf_phd_2017 import GrafPhD2017
from .horita_and_wesolowski_1994 import HoritaAndWesolowski1994
from .majoube_1970 import Majoube1970
from .majoube_1971 import Majoube1971
from .merlivat_and_nief_1967 import MerlivatAndNief1967
from .null import Null
from .pierchala_et_al_2022 import PierchalaEtAl2022
from .van_hook_1968 import VanHook1968
from .lamb_et_al_2017 import LambEtAl2017
from .ellehoj_et_al_2013 import EllehojEtAl2013

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
from .barkan_and_luz_2007 import BarkanAndLuz2007
from .dansgaard_1964 import Dansgaard1964
from .null import Null
from .pierchala_et_al_2022 import PierchalaEtAl2022

This file was deleted.

2 changes: 1 addition & 1 deletion examples/PySDM_examples/Bolot_et_al_2013/fig_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"outputs": [],
"source": [
"formulae = Formulae(isotope_equilibrium_fractionation_factors=\"BolotEtAl2013\")\n",
"formulae = Formulae(isotope_equilibrium_fractionation_factors=\"MerlivatAndNief1967+Majoube1970+Majoube1971\")\n",
"alphas = formulae.isotope_equilibrium_fractionation_factors\n",
"const = formulae.constants"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/PySDM_examples/Graf_et_al_2019/Table_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"outputs": [],
"source": [
"formulae = Formulae(\n",
" isotope_equilibrium_fractionation_factors='GrafPhD2017',\n",
" isotope_equilibrium_fractionation_factors='Majoube1970+Majoube1971+MerlivatAndNief1967',\n",
" isotope_meteoric_water_line_excess='Dansgaard1964'\n",
")\n",
"const = formulae.constants\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/PySDM_examples/Pierchala_et_al_2022/fig_3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
"outputs": [],
"source": [
"formulae = Formulae(\n",
" isotope_equilibrium_fractionation_factors='PierchalaEtAl2022',\n",
" isotope_meteoric_water_line_excess='PierchalaEtAl2022',\n",
" isotope_equilibrium_fractionation_factors='BarkanAndLuz2005+HoritaAndWesolowski1994',\n",
" isotope_meteoric_water_line_excess='Dansgaard1964+BarkanAndLuz2007',\n",
" isotope_ratio_evolution='RayleighDistillation'\n",
")\n",
"const = formulae.constants\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/PySDM_examples/Pierchala_et_al_2022/fig_4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"from PySDM_examples.Pierchala_et_al_2022.commons import deltas_0_SMOW, TABLE_1, TABLE_2\n",
"\n",
"formulae = Formulae(\n",
" isotope_equilibrium_fractionation_factors='PierchalaEtAl2022',\n",
" isotope_meteoric_water_line_excess='PierchalaEtAl2022',\n",
" isotope_equilibrium_fractionation_factors='BarkanAndLuz2005+HoritaAndWesolowski1994',\n",
" isotope_meteoric_water_line_excess='Dansgaard1964+BarkanAndLuz2007',\n",
" isotope_ratio_evolution='RayleighDistillation'\n",
")\n",
"const = formulae.constants\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
def test_cracow_water_excesses():
"""checking if d-excess and 17O-excess values match those computed from deltas"""
# arrange
formulae = Formulae(isotope_meteoric_water_line_excess="PierchalaEtAl2022")
formulae = Formulae(
isotope_meteoric_water_line_excess="Dansgaard1964+BarkanAndLuz2007"
)
sut = formulae.isotope_meteoric_water_line_excess

# act/assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)
"""values from Fig. 1 in [Bolot et al. 2013](https://doi.org/10.5194/acp-13-7903-2013)"""

PAPERS = ("BolotEtAl2013", "VanHook1968")
PAPERS = ("MerlivatAndNief1967+Majoube1970+Majoube1971", "VanHook1968")

# TODO #1208: tests for VanHook1968 H2_17O, HOT

Expand Down
12 changes: 12 additions & 0 deletions tests/unit_tests/test_formulae.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,15 @@ def test_get_constant():

# assert
assert sut.get_constant("rho_w") == rho_w

@staticmethod
@pytest.mark.parametrize("arg", ("Dansgaard1964+BarkanAndLuz2007", "Dansgaard1964"))
def test_plus_separated_ctor_arg(arg):
# arrange
sut = formulae.Formulae(isotope_meteoric_water_line_excess=arg)

# act
class_name = sut.isotope_meteoric_water_line_excess.__name__

# assert
assert class_name == arg

0 comments on commit ec7af2e

Please sign in to comment.