Skip to content

Commit

Permalink
complete volume-to-mass transition for base attribute. closes #798 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
slayoo authored Jul 7, 2024
1 parent 3a04584 commit f44d88b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
17 changes: 9 additions & 8 deletions PySDM/initialisation/init_fall_momenta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@


def init_fall_momenta(
volume: np.ndarray,
rho_w: float, # TODO #798 - we plan to use masses instead of volumes soon
water_mass: np.ndarray,
zero: bool = False,
terminal_velocity_approx=GunnKinzer1949, # TODO #1155
):
Expand All @@ -24,24 +23,26 @@ def init_fall_momenta(
`PySDM.attributes.physics.relative_fall_velocity.RelativeFallVelocity` attribute)
Parameters:
- volume: a numpy array of superdroplet volumes
- rho_w: the density of water (generally found in formulae.constants)
- water_mass: a numpy array of superdroplet water masses
Returns:
- a numpy array of initial momentum values
"""
if zero:
return np.zeros_like(volume)
return np.zeros_like(water_mass)

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

approximation = terminal_velocity_approx(particulator=particulator)

radii_arr = particulator.formulae.trivia.radius(volume)
volume_arr = particulator.formulae.particle_shape_and_density.mass_to_volume(
water_mass
)
radii_arr = particulator.formulae.trivia.radius(volume=volume_arr)
radii = particulator.Storage.from_ndarray(radii_arr)

output = particulator.Storage.empty((len(volume),), dtype=float)
output = particulator.Storage.empty((len(water_mass),), dtype=float)

approximation(output=output, radius=radii)

return output.to_ndarray() * volume * rho_w # TODO #798 this assumes no ice
return output.to_ndarray() * water_mass
70 changes: 35 additions & 35 deletions tests/unit_tests/initialisation/test_init_fall_momenta.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,54 @@
pytest.param(
{
"multiplicity": np.array([1, 2, 3, 2]),
"volume": np.array(
"water mass": np.array(
[
1 * si.mm**3,
0.1 * si.mm**3,
1 * si.mm**3,
0.05 * si.mm**3,
1 * si.mg,
0.1 * si.mg,
1 * si.mg,
0.05 * si.mg,
]
),
"rho_w": 1000, # TODO #798 - we plan to use masses instead of volumes soon
},
id="",
),
),
)
def params_fixture(request):
return request.param


def test_init_to_terminal_velocity(params, backend_instance):
"""
Fall momenta correctly initialized to the terminal velocity * mass.
"""
env = Box(dt=1, dv=1)
builder = Builder(
n_sd=len(params["multiplicity"]), backend=backend_instance, environment=env
)
builder.request_attribute("terminal velocity")
particulator = builder.build(
attributes={"multiplicity": params["multiplicity"], "volume": params["volume"]},
products=(),
)

terminal_momentum = (
particulator.attributes["terminal velocity"].to_ndarray()
* params["volume"]
* params["rho_w"]
)
class TestInitFallMomenta:
@staticmethod
def test_init_to_terminal_velocity(params, backend_instance):
"""
Fall momenta correctly initialized to the terminal velocity * mass.
"""
env = Box(dt=1, dv=1)
builder = Builder(
n_sd=len(params["multiplicity"]), backend=backend_instance, environment=env
)
builder.request_attribute("terminal velocity")
particulator = builder.build(
attributes={
"multiplicity": params["multiplicity"],
"water mass": params["water mass"],
},
products=(),
)

assert np.allclose(
init_fall_momenta(params["volume"], params["rho_w"]), terminal_momentum
)
terminal_momentum = (
particulator.attributes["terminal velocity"].to_ndarray()
* params["water mass"]
)

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

def test_init_to_zero(params):
"""
Fall momenta correctly initialized to zero.
"""
@staticmethod
def test_init_to_zero(params):
"""
Fall momenta correctly initialized to zero.
"""

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

assert (fall_momenta == np.zeros_like(fall_momenta)).all()
assert (fall_momenta == np.zeros_like(fall_momenta)).all()

0 comments on commit f44d88b

Please sign in to comment.