Skip to content

Commit f44d88b

Browse files
authored
complete volume-to-mass transition for base attribute. closes #798 (#1357)
1 parent 3a04584 commit f44d88b

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

PySDM/initialisation/init_fall_momenta.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313

1414
def init_fall_momenta(
15-
volume: np.ndarray,
16-
rho_w: float, # TODO #798 - we plan to use masses instead of volumes soon
15+
water_mass: np.ndarray,
1716
zero: bool = False,
1817
terminal_velocity_approx=GunnKinzer1949, # TODO #1155
1918
):
@@ -24,24 +23,26 @@ def init_fall_momenta(
2423
`PySDM.attributes.physics.relative_fall_velocity.RelativeFallVelocity` attribute)
2524
2625
Parameters:
27-
- volume: a numpy array of superdroplet volumes
28-
- rho_w: the density of water (generally found in formulae.constants)
26+
- water_mass: a numpy array of superdroplet water masses
2927
3028
Returns:
3129
- a numpy array of initial momentum values
3230
"""
3331
if zero:
34-
return np.zeros_like(volume)
32+
return np.zeros_like(water_mass)
3533

3634
particulator = Particulator(0, CPU(Formulae())) # TODO #1155
3735

3836
approximation = terminal_velocity_approx(particulator=particulator)
3937

40-
radii_arr = particulator.formulae.trivia.radius(volume)
38+
volume_arr = particulator.formulae.particle_shape_and_density.mass_to_volume(
39+
water_mass
40+
)
41+
radii_arr = particulator.formulae.trivia.radius(volume=volume_arr)
4142
radii = particulator.Storage.from_ndarray(radii_arr)
4243

43-
output = particulator.Storage.empty((len(volume),), dtype=float)
44+
output = particulator.Storage.empty((len(water_mass),), dtype=float)
4445

4546
approximation(output=output, radius=radii)
4647

47-
return output.to_ndarray() * volume * rho_w # TODO #798 this assumes no ice
48+
return output.to_ndarray() * water_mass

tests/unit_tests/initialisation/test_init_fall_momenta.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,54 @@
1818
pytest.param(
1919
{
2020
"multiplicity": np.array([1, 2, 3, 2]),
21-
"volume": np.array(
21+
"water mass": np.array(
2222
[
23-
1 * si.mm**3,
24-
0.1 * si.mm**3,
25-
1 * si.mm**3,
26-
0.05 * si.mm**3,
23+
1 * si.mg,
24+
0.1 * si.mg,
25+
1 * si.mg,
26+
0.05 * si.mg,
2727
]
2828
),
29-
"rho_w": 1000, # TODO #798 - we plan to use masses instead of volumes soon
3029
},
31-
id="",
3230
),
3331
),
3432
)
3533
def params_fixture(request):
3634
return request.param
3735

3836

39-
def test_init_to_terminal_velocity(params, backend_instance):
40-
"""
41-
Fall momenta correctly initialized to the terminal velocity * mass.
42-
"""
43-
env = Box(dt=1, dv=1)
44-
builder = Builder(
45-
n_sd=len(params["multiplicity"]), backend=backend_instance, environment=env
46-
)
47-
builder.request_attribute("terminal velocity")
48-
particulator = builder.build(
49-
attributes={"multiplicity": params["multiplicity"], "volume": params["volume"]},
50-
products=(),
51-
)
52-
53-
terminal_momentum = (
54-
particulator.attributes["terminal velocity"].to_ndarray()
55-
* params["volume"]
56-
* params["rho_w"]
57-
)
37+
class TestInitFallMomenta:
38+
@staticmethod
39+
def test_init_to_terminal_velocity(params, backend_instance):
40+
"""
41+
Fall momenta correctly initialized to the terminal velocity * mass.
42+
"""
43+
env = Box(dt=1, dv=1)
44+
builder = Builder(
45+
n_sd=len(params["multiplicity"]), backend=backend_instance, environment=env
46+
)
47+
builder.request_attribute("terminal velocity")
48+
particulator = builder.build(
49+
attributes={
50+
"multiplicity": params["multiplicity"],
51+
"water mass": params["water mass"],
52+
},
53+
products=(),
54+
)
5855

59-
assert np.allclose(
60-
init_fall_momenta(params["volume"], params["rho_w"]), terminal_momentum
61-
)
56+
terminal_momentum = (
57+
particulator.attributes["terminal velocity"].to_ndarray()
58+
* params["water mass"]
59+
)
6260

61+
assert np.allclose(init_fall_momenta(params["water mass"]), terminal_momentum)
6362

64-
def test_init_to_zero(params):
65-
"""
66-
Fall momenta correctly initialized to zero.
67-
"""
63+
@staticmethod
64+
def test_init_to_zero(params):
65+
"""
66+
Fall momenta correctly initialized to zero.
67+
"""
6868

69-
fall_momenta = init_fall_momenta(params["volume"], params["rho_w"], zero=True)
69+
fall_momenta = init_fall_momenta(params["water mass"], zero=True)
7070

71-
assert (fall_momenta == np.zeros_like(fall_momenta)).all()
71+
assert (fall_momenta == np.zeros_like(fall_momenta)).all()

0 commit comments

Comments
 (0)