Skip to content

Commit

Permalink
refactor: remove references to backend from freezing dynamic and move…
Browse files Browse the repository at this point in the history
… freezing unit test to tests/unit_tests/dynamics. closes #1154 (#1515)
  • Loading branch information
slayoo authored Mar 7, 2025
1 parent 4db6819 commit ce84f6e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
43 changes: 7 additions & 36 deletions PySDM/dynamics/freezing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
immersion freezing using either singular or time-dependent formulation
"""

from PySDM.backends.impl_common.freezing_attributes import (
SingularAttributes,
TimeDependentAttributes,
)
from PySDM.physics.heterogeneous_ice_nucleation_rate import Null
from PySDM.dynamics.impl import register_dynamic

Expand All @@ -25,22 +21,24 @@ def __init__(self, *, singular=True, record_freezing_temperature=False, thaw=Fal
def register(self, builder):
self.particulator = builder.particulator

assert builder.formulae.particle_shape_and_density.supports_mixed_phase()
assert (
self.particulator.formulae.particle_shape_and_density.supports_mixed_phase()
)

builder.request_attribute("signed water mass")
if self.singular or self.record_freezing_temperature:
builder.request_attribute("freezing temperature")

if not self.singular:
assert not isinstance(
builder.formulae.heterogeneous_ice_nucleation_rate, Null
self.particulator.formulae.heterogeneous_ice_nucleation_rate, Null
)
builder.request_attribute("immersed surface area")
self.rand = self.particulator.Storage.empty(
self.particulator.n_sd, dtype=float
)
self.rng = self.particulator.Random(
self.particulator.n_sd, self.particulator.backend.formulae.seed
self.particulator.n_sd, self.particulator.formulae.seed
)

def __call__(self):
Expand All @@ -54,39 +52,12 @@ def __call__(self):
return

if self.singular:
self.particulator.backend.freeze_singular(
attributes=SingularAttributes(
freezing_temperature=self.particulator.attributes[
"freezing temperature"
],
signed_water_mass=self.particulator.attributes["signed water mass"],
),
temperature=self.particulator.environment["T"],
relative_humidity=self.particulator.environment["RH"],
cell=self.particulator.attributes["cell id"],
thaw=self.thaw,
)
self.particulator.immersion_freezing_singular(thaw=self.thaw)
else:
self.rand.urand(self.rng)
self.particulator.backend.freeze_time_dependent(
self.particulator.immersion_freezing_time_dependent(
rand=self.rand,
attributes=TimeDependentAttributes(
immersed_surface_area=self.particulator.attributes[
"immersed surface area"
],
signed_water_mass=self.particulator.attributes["signed water mass"],
),
timestep=self.particulator.dt,
cell=self.particulator.attributes["cell id"],
a_w_ice=self.particulator.environment["a_w_ice"],
temperature=self.particulator.environment["T"],
relative_humidity=self.particulator.environment["RH"],
record_freezing_temperature=self.record_freezing_temperature,
freezing_temperature=(
self.particulator.attributes["freezing temperature"]
if self.record_freezing_temperature
else None
),
thaw=self.thaw,
)

Expand Down
39 changes: 39 additions & 0 deletions PySDM/particulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import numpy as np

from PySDM.backends.impl_common.backend_methods import BackendMethods
from PySDM.backends.impl_common.freezing_attributes import (
SingularAttributes,
TimeDependentAttributes,
)
from PySDM.backends.impl_common.index import make_Index
from PySDM.backends.impl_common.indexed_storage import make_IndexedStorage
from PySDM.backends.impl_common.pair_indicator import make_PairIndicator
Expand Down Expand Up @@ -479,3 +483,38 @@ def seeding(
self.attributes.mark_updated("multiplicity")
for key in self.attributes.get_extensive_attribute_keys():
self.attributes.mark_updated(key)

def immersion_freezing_time_dependent(
self, *, thaw: bool, record_freezing_temperature: bool, rand: Storage
):
self.backend.freeze_time_dependent(
rand=rand,
attributes=TimeDependentAttributes(
immersed_surface_area=self.attributes["immersed surface area"],
signed_water_mass=self.attributes["signed water mass"],
),
timestep=self.dt,
cell=self.attributes["cell id"],
a_w_ice=self.environment["a_w_ice"],
temperature=self.environment["T"],
relative_humidity=self.environment["RH"],
record_freezing_temperature=record_freezing_temperature,
freezing_temperature=(
self.attributes["freezing temperature"]
if record_freezing_temperature
else None
),
thaw=thaw,
)

def immersion_freezing_singular(self, *, thaw: bool):
self.backend.freeze_singular(
attributes=SingularAttributes(
freezing_temperature=self.attributes["freezing temperature"],
signed_water_mass=self.attributes["signed water mass"],
),
temperature=self.environment["T"],
relative_humidity=self.environment["RH"],
cell=self.attributes["cell id"],
thaw=thaw,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from PySDM.products import IceWaterContent


class TestFreezingMethods:
class TestImmersionFreezing:
# TODO #599
def test_record_freezing_temperature_on_time_dependent_freeze(self):
pass
Expand Down

0 comments on commit ce84f6e

Please sign in to comment.