Skip to content

Commit

Permalink
Merge pull request #635 from slayoo/freezing
Browse files Browse the repository at this point in the history
ABIFM implementation; set/getitem in Box env; T and a_w_ice dependence in J_het
  • Loading branch information
slayoo authored Sep 22, 2021
2 parents c7aa9d2 + cd7d850 commit fc40f68
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 16 deletions.
8 changes: 4 additions & 4 deletions PySDM/backends/numba/impl/freezing_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ def freeze_singular_body(T_fz, v_wet, T, RH, cell):
J_het = self.formulae.heterogeneous_ice_nucleation_rate.J_het

@numba.njit(**{**conf.JIT_FLAGS, 'fastmath': self.formulae.fastmath})
def freeze_time_dependent_body(rand, immersed_surface_area, volume, dt):
def freeze_time_dependent_body(rand, immersed_surface_area, volume, dt, cell, a_w_ice):
n_sd = len(volume)
for i in numba.prange(n_sd):
if _unfrozen(volume, i):
p = 1 - np.exp(-J_het() * immersed_surface_area[i] * dt) # TODO #599: assert if > 1?
p = 1 - np.exp(-J_het(a_w_ice[cell[i]]) * immersed_surface_area[i] * dt) # TODO #599: assert if > 1?
if rand[i] < p:
_freeze(volume, i)
self.freeze_time_dependent_body = freeze_time_dependent_body

def freeze_singular(self, T_fz, v_wet, T, RH, cell):
self.freeze_singular_body(T_fz.data, v_wet.data, T.data, RH.data, cell.data)

def freeze_time_dependent(self, rand, immersed_surface_area, volume, dt):
self.freeze_time_dependent_body(rand.data, immersed_surface_area.data, volume.data, dt)
def freeze_time_dependent(self, rand, immersed_surface_area, volume, dt, cell, a_w_ice):
self.freeze_time_dependent_body(rand.data, immersed_surface_area.data, volume.data, dt, cell.data, a_w_ice.data)
4 changes: 3 additions & 1 deletion PySDM/dynamics/freezing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def __call__(self):
rand=self.rand,
immersed_surface_area=self.particulator.attributes['immersed surface area'],
volume=self.particulator.attributes['volume'],
dt=self.particulator.dt
dt=self.particulator.dt,
cell=self.particulator.attributes['cell id'],
a_w_ice=self.particulator.environment['a_w_ice']
)

self.particulator.attributes.attributes['volume'].mark_updated()
8 changes: 8 additions & 0 deletions PySDM/environments/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from PySDM.state.mesh import Mesh
import numpy as np


class Box:
Expand All @@ -11,6 +12,13 @@ def __init__(self, dt, dv):
self.dt = dt
self.mesh = Mesh.mesh_0d(dv)
self.particulator = None
self._ambient_air = {}

def __getitem__(self, item):
return self._ambient_air[item]

def __setitem__(self, key, value):
self._ambient_air[key] = self.particulator.backend.Storage.from_ndarray(np.array([value]))

def register(self, builder):
self.particulator = builder.particulator
Expand Down
4 changes: 2 additions & 2 deletions PySDM/physics/formulae.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, *,
self.hydrostatics = _magick(hydrostatics, physics.hydrostatics, fastmath)
self.freezing_temperature_spectrum = _magick(freezing_temperature_spectrum, physics.freezing_temperature_spectrum, fastmath)
self.heterogeneous_ice_nucleation_rate = _magick(heterogeneous_ice_nucleation_rate, physics.heterogeneous_ice_nucleation_rate, fastmath)
self.check()
self.__check()

def __str__(self):
description = []
Expand All @@ -128,7 +128,7 @@ def __str__(self):
description.append(f"{attr}: {value}")
return ', '.join(description)

def check(self):
def __check(self):
for attr in dir(self):
if not attr.startswith('__'):
if hasattr(getattr(self, attr), '_check'):
Expand Down
4 changes: 0 additions & 4 deletions PySDM/physics/heterogeneous_ice_nucleation_rate/ABIFM.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .constant import Constant
from .null import Null
from .ABIFM import ABIFM
from .abifm import ABIFM
14 changes: 14 additions & 0 deletions PySDM/physics/heterogeneous_ice_nucleation_rate/abifm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np

m = np.inf
c = np.inf

class ABIFM:
@staticmethod
def _check():
assert np.isfinite(m)
assert np.isfinite(c)

@staticmethod
def J_het(a_w_ice):
return 10**(m * (1 - a_w_ice) + c)
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ def _check():
assert np.isfinite(J_het)

@staticmethod
def J_het():
def J_het(a_w_ice):
return J_het
2 changes: 1 addition & 1 deletion PySDM/physics/heterogeneous_ice_nucleation_rate/null.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Null:
@staticmethod
def J_het():
def J_het(a_w_ice):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ def ice_Celsius(T):
const.FWC_I7 + T *
const.FWC_I8
))))))))

@staticmethod
def a_w_ice(T):
return (
FlatauWalkoCotton.ice_Celsius(T - const.T0)
/
FlatauWalkoCotton.pvs_Celsius(T - const.T0)
)
2 changes: 1 addition & 1 deletion test-time-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ ghapi
pytest

# note: if cloning both PySDM and PySDM examples, consider "pip install -e"
PySDM-examples @ git+git://github.com/slayoo/PySDM-examples@0be0f1e#egg=PySDM-examples
PySDM-examples @ git+git://github.com/slayoo/PySDM-examples@b37041a#egg=PySDM-examples
5 changes: 4 additions & 1 deletion tests/unit_tests/backends/test_freezing_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def test_freeze_time_dependent(plot=False):

formulae = Formulae(heterogeneous_ice_nucleation_rate='Constant')
builder = Builder(n_sd=n_sd, backend=CPU, formulae=formulae)
builder.set_environment(Box(dt=case['dt'], dv=dv))
env = Box(dt=case['dt'], dv=dv)
builder.set_environment(env)
builder.add_dynamic(Freezing(singular=False))
attributes = {
'n': np.full(n_sd, int(case['N'])),
Expand All @@ -67,6 +68,8 @@ def test_freeze_time_dependent(plot=False):
products = (IceWaterContent(specific=False),)
particulator = builder.build(attributes=attributes, products=products)

env['a_w_ice'] = np.nan

cell_id = 0
for i in range(int(total_time / case['dt']) + 1):
particulator.run(0 if i == 0 else 1)
Expand Down

0 comments on commit fc40f68

Please sign in to comment.