-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replacing optional (sic!) normalisation by dv with meaningful specifi…
…c=True/False flag in ParticleSizeSpectrum product (#1343)
- Loading branch information
Showing
9 changed files
with
7,640 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3,614 changes: 3,598 additions & 16 deletions
3,614
examples/PySDM_examples/Lowe_et_al_2019/fig_2.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
3,929 changes: 3,909 additions & 20 deletions
3,929
examples/PySDM_examples/Pyrcel/example_basic_run.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
tests/unit_tests/products/test_particle_size_spectrum.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
""" | ||
tests the ParticleSizeSpectrum product against per-mass/per-volume and dry-/wet-size choices | ||
""" | ||
|
||
import pytest | ||
import numpy as np | ||
from PySDM import Builder | ||
from PySDM.physics import si | ||
from PySDM.environments import Box | ||
from PySDM.products import ( | ||
ParticleSizeSpectrumPerMassOfDryAir, | ||
ParticleSizeSpectrumPerVolume, | ||
) | ||
|
||
|
||
class TestParticleSizeSpectrum: | ||
@staticmethod | ||
@pytest.mark.parametrize( | ||
"product_class, specific, unit", | ||
( | ||
(ParticleSizeSpectrumPerVolume, False, "1.0 / meter ** 4"), | ||
(ParticleSizeSpectrumPerMassOfDryAir, True, "1.0 / kilogram / meter"), | ||
), | ||
) | ||
def test_specific_flag(product_class, specific, unit, backend_instance): | ||
"""checks if the reported concentration is correctly normalised per | ||
volume or mass of air, and per bin size""" | ||
# arrange | ||
dv = 666 * si.m**3 | ||
multiplicity = 1000 | ||
rhod = 44 * si.kg / si.m**3 | ||
min_size = 0 | ||
max_size = 1 * si.mm | ||
|
||
n_sd = 1 | ||
box = Box(dt=np.nan, dv=dv) | ||
builder = Builder(n_sd=n_sd, backend=backend_instance, environment=box) | ||
sut = product_class(radius_bins_edges=(min_size, max_size)) | ||
builder.build( | ||
products=(sut,), | ||
attributes={ | ||
"multiplicity": np.ones(n_sd) * multiplicity, | ||
"water mass": np.ones(n_sd) * si.ug, | ||
}, | ||
) | ||
box["rhod"] = rhod | ||
|
||
# act | ||
actual = sut.get() | ||
|
||
# assert | ||
assert sut.unit == unit | ||
assert sut.specific == specific | ||
np.testing.assert_approx_equal( | ||
actual=actual, | ||
desired=multiplicity | ||
/ dv | ||
/ (max_size - min_size) | ||
/ (rhod if specific else 1), | ||
significant=10, | ||
) | ||
|
||
@staticmethod | ||
@pytest.mark.parametrize( | ||
"product_class", | ||
(ParticleSizeSpectrumPerVolume, ParticleSizeSpectrumPerMassOfDryAir), | ||
) | ||
@pytest.mark.parametrize("dry", (True, False)) | ||
def test_dry_flag(product_class, dry, backend_instance): | ||
"""checks if dry or wet size attribute is correctly picked for moment calculation""" | ||
# arrange | ||
n_sd = 1 | ||
dv = 666 * si.m**3 | ||
min_size = 0 | ||
max_size = 1 * si.mm | ||
multiplicity = 100 | ||
rhod = 44 * si.kg / si.m**3 | ||
wet_volume = 1 * si.um**3 | ||
dry_volume = 0.01 * si.um**3 | ||
|
||
box = Box(dt=np.nan, dv=dv) | ||
builder = Builder(n_sd=n_sd, backend=backend_instance, environment=box) | ||
sut = product_class(radius_bins_edges=(min_size, max_size), dry=dry) | ||
builder.build( | ||
products=(sut,), | ||
attributes={ | ||
"multiplicity": np.ones(n_sd) * multiplicity, | ||
"volume": np.ones(n_sd) * (np.nan if dry else wet_volume), | ||
"dry volume": np.ones(n_sd) * (dry_volume if dry else np.nan), | ||
}, | ||
) | ||
box["rhod"] = rhod | ||
|
||
# act | ||
actual = sut.get() | ||
|
||
# assert | ||
np.testing.assert_approx_equal( | ||
actual=actual, | ||
desired=multiplicity | ||
/ dv | ||
/ (max_size - min_size) | ||
/ (rhod if sut.specific else 1), | ||
significant=10, | ||
) |