Skip to content

Commit 09d8e90

Browse files
committed
Fix protodisk Rmin/radius_min rename
1 parent 4a5500b commit 09d8e90

3 files changed

Lines changed: 110 additions & 35 deletions

File tree

src/amuse/ext/protodisk.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy
22
import warnings
3+
from amuse.support.helpers import rename_fn_par
34
from amuse.ext.evrard_test import body_centered_grid_unit_cube
45
from amuse.ext.evrard_test import regular_grid_unit_cube
56
from amuse.ext.evrard_test import uniform_random_unit_cube
@@ -54,45 +55,15 @@ def make_xyz(self):
5455
class ProtoPlanetaryDisk:
5556
def __init__(
5657
self, targetN, convert_nbody=None, discfraction=0.1,
57-
densitypower=1., thermalpower=0.5, radius_min=1, radius_max=100,
58+
densitypower=1., thermalpower=0.5, radius_min=None, radius_max=None,
5859
gamma=1., q_out=2., base_grid=None, Rmin=None, Rmax=None,
5960
):
60-
if Rmin is not None:
61-
warnings.warn(
62-
"Rmin is deprecated, use radius_min instead",
63-
category=FutureWarning,
64-
)
65-
if radius_min is not None and radius_min != Rmin:
66-
raise ValueError(
67-
"Rmin and radius_min have different values, "
68-
"this is only allowed if one of them is None"
69-
)
70-
radius_min = Rmin
71-
72-
if radius_min is None:
73-
raise ValueError("radius_min must be set")
74-
75-
if Rmax is not None:
76-
warnings.warn(
77-
"Rmax is deprecated, use radius_max instead",
78-
category=FutureWarning,
79-
)
80-
if radius_max is not None and radius_max != Rmax:
81-
raise ValueError(
82-
"Rmax and radius_max have different values, "
83-
"this is only allowed if one of them is None"
84-
)
85-
radius_max = Rmax
86-
87-
if radius_max is None:
88-
raise ValueError("radius_max must be set")
89-
9061
self.targetN = targetN
9162
self.convert_nbody = convert_nbody
9263
self.densitypower = densitypower
9364
self.thermalpower = thermalpower
94-
self.Rmin = radius_min
95-
self.Rmax = radius_max
65+
self.Rmin = rename_fn_par("radius_min", radius_min, "Rmin", Rmin, 1)
66+
self.Rmax = rename_fn_par("radius_max", radius_max, "Rmax", Rmax, 100)
9667
self.gamma = gamma
9768
self.q_out = q_out
9869
self.discfraction = discfraction
@@ -101,13 +72,22 @@ def __init__(
10172
self.a2 = self.thermalpower/2
10273
self.g = densitypower
10374
self.g2 = 2 - densitypower
104-
self.k_out = ((1 + discfraction) / Rmax**3)**0.5
75+
self.k_out = ((1 + discfraction) / self.Rmax**3)**0.5
10576
self.sigma_out = self.g2 * discfraction / (
106-
2 * numpy.pi * Rmax**self.g * (Rmax**self.g2 - Rmin**self.g2))
77+
2 * numpy.pi * self.Rmax**self.g *
78+
(self.Rmax**self.g2 - self.Rmin**self.g2))
10779
self.cs_out = self.q_out * numpy.pi * self.sigma_out / self.k_out
10880

10981
self.base_cylinder = uniform_unit_cylinder(targetN, base_grid)
11082

83+
@property
84+
def radius_min(self):
85+
return self.Rmin
86+
87+
@property
88+
def radius_max(self):
89+
return self.Rmax
90+
11191
def sigma(self, r):
11292
return self.sigma_out * (self.Rmax / r)**self.g
11393

src/amuse/support/helpers.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import warnings
2+
3+
4+
def rename_fn_par(new_name, new_value, old_name, old_value, default_value):
5+
"""Get value of a renamed function parameter.
6+
7+
If you have a function f with parameter ``x`` and default value 1, like this:
8+
9+
.. code-block::
10+
11+
def f(x=1):
12+
...
13+
14+
and you want to rename ``x`` to ``y`` while staying backwards compatible, then you
15+
can rename ``x`` to ``y``, add ``x`` back in at the end so that old keyword
16+
arguments still work, give both a default value of ``None``, and use this function
17+
like this:
18+
19+
.. code-block::
20+
21+
def f(y=None, x=None):
22+
value = rename_fn_par("y", y, "x", x, 1)
23+
24+
Any callers using ``x`` explicitly will receive a warning to change their code to
25+
use ``y`` in the future. If both ``x`` and ``y`` are set and the values are
26+
different, then an exception is raised.
27+
28+
Args:
29+
new_name (str): The new name of the variable
30+
new_value: The value passed using the new name
31+
old_name (str): The old name of the variable
32+
old_value: The value passed using the old name
33+
default_value: The default value if neither are set
34+
35+
Returns:
36+
Either new_value or old_value if only one is set or they're set to the same
37+
value, or default_value if neither is set.
38+
39+
Raises:
40+
ValueError: If both new_value and old_value are set, and to different values.
41+
"""
42+
if new_value is not None:
43+
if old_value is not None and old_value != new_value:
44+
raise ValueError(
45+
f"{old_name} and {new_name} have different values,"
46+
" which is not allowed because they represent the same thing.")
47+
return new_value
48+
49+
if old_value is not None:
50+
warnings.warn(
51+
f"{old_name} is deprecated, please use {new_name} instead",
52+
category=FutureWarning)
53+
return old_value
54+
55+
return default_value
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from amuse.support.helpers import rename_fn_par
2+
3+
import pytest
4+
5+
6+
@pytest.mark.filterwarnings("ignore: old is deprecated.*")
7+
def test_rename_fn_par():
8+
assert rename_fn_par("new", None, "old", None, 1) == 1
9+
assert rename_fn_par("new", None, "old", 1, 2) == 1
10+
assert rename_fn_par("new", 1, "old", None, 2) == 1
11+
assert rename_fn_par("new", 1, "old", 1, 2) == 1
12+
13+
with pytest.raises(ValueError):
14+
rename_fn_par("new", 1, "old", 2, 3)
15+
16+
17+
class _RenamedMethod:
18+
def __init__(self, new = None, a = 1, b = "test", old = None):
19+
self.y = rename_fn_par("new", new, "old", old, 42)
20+
21+
22+
@pytest.mark.filterwarnings("ignore: old is deprecated.*")
23+
def test_rename_fn_par_usage():
24+
rm = _RenamedMethod()
25+
assert rm.y == 42
26+
27+
rm = _RenamedMethod(1)
28+
assert rm.y == 1
29+
30+
rm = _RenamedMethod(new=1)
31+
assert rm.y == 1
32+
33+
rm = _RenamedMethod(old=1)
34+
assert rm.y == 1
35+
36+
rm = _RenamedMethod(1, 1, "test", 1)
37+
assert rm.y == 1
38+
39+
with pytest.raises(ValueError):
40+
_RenamedMethod(1, 2, "test", 3)

0 commit comments

Comments
 (0)