Skip to content

Commit

Permalink
Merge pull request #689 from slayoo/freezing
Browse files Browse the repository at this point in the history
product subsystem refactor
  • Loading branch information
slayoo authored Nov 22, 2021
2 parents 2c2302c + 2c3e28e commit 31d9a3e
Show file tree
Hide file tree
Showing 77 changed files with 720 additions and 813 deletions.
8 changes: 8 additions & 0 deletions PySDM/environments/_moist.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def sync(self):
target['rhod'], target['thd'], target['qv'],
target['T'], target['p'], target['RH']
)
if 'a_w_ice' in self.variables:
self.particulator.backend.a_w_ice(
T=target['T'],
p=target['p'],
RH=target['RH'],
qv=target['qv'],
a_w_ice=target['a_w_ice']
)
self._values["predicted"] = target

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions PySDM/exporters/netcdf_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def _create_variables(self, ncdf):
# TODO #340 ParticleVolume var

for name, instance in self.simulator.products.items():
assert name not in self.vars
if name in self.vars:
raise AssertionError(f"product ({name}) has same name as one of netCDF dimensions")

n_dimensions = len(instance.shape)
if n_dimensions == 3:
Expand All @@ -51,7 +52,6 @@ def _create_variables(self, ncdf):
raise NotImplementedError()
self.vars[name] = ncdf.createVariable(name, "f", dimensions)
self.vars[name].units = instance.unit
self.vars[name].long_name = instance.description

def _write_variables(self, i):
self.vars["T"][i] = self.settings.output_steps[i] * self.settings.dt
Expand Down
108 changes: 0 additions & 108 deletions PySDM/impl/product.py

This file was deleted.

9 changes: 0 additions & 9 deletions PySDM/products/PartMC/VolumeFractalDimension.py

This file was deleted.

1 change: 0 additions & 1 deletion PySDM/products/PartMC/__init__.py

This file was deleted.

30 changes: 3 additions & 27 deletions PySDM/products/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@
from .condensation import *
from .displacement import *
from .freezing import *
from .PartMC import *

from .aerosol_specific_concentration import AerosolSpecificConcentration
from .cloud_droplet_effective_radius import CloudDropletEffectiveRadius
from .dry_air_density import DryAirDensity
from .dry_air_potential_temperature import DryAirPotentialTemperature
from .dynamic_wall_time import DynamicWallTime
from .parcel_displacement import ParcelDisplacement
from .particle_mean_radius import ParticleMeanRadius
from .particles_concentration import (
ParticlesConcentration, AerosolConcentration, CloudDropletConcentration, DrizzleConcentration
)
from .particles_size_spectrum import (
ParticlesSizeSpectrum, ParticlesDrySizeSpectrum, ParticlesWetSizeSpectrum
)
from .particles_volume_spectrum import ParticlesVolumeSpectrum
from .pressure import Pressure
from .relative_humidity import RelativeHumidity
from .super_droplet_count import SuperDropletCount
from .temperature import Temperature
from .time import Time
from .timers import CPUTime, WallTime
from .total_dry_mass_mixing_ratio import TotalDryMassMixingRatio
from .total_particle_concentration import TotalParticleConcentration
from .total_particle_specific_concentration import TotalParticleSpecificConcentration
from .water_mixing_ratio import WaterMixingRatio
from .water_vapour_mixing_ratio import WaterVapourMixingRatio
from .ambient_thermodynamics import *
from .housekeeping import *
from .size_spectral import *
25 changes: 0 additions & 25 deletions PySDM/products/aerosol_specific_concentration.py

This file was deleted.

6 changes: 6 additions & 0 deletions PySDM/products/ambient_thermodynamics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .ambient_pressure import AmbientPressure
from .ambient_temperature import AmbientTemperature
from .ambient_relative_humidity import AmbientRelativeHumidity
from .ambient_dry_air_density import AmbientDryAirDensity
from .ambient_dry_air_potential_temperature import AmbientDryAirPotentialTemperature
from .ambient_water_vapour_mixing_ratio import AmbientWaterVapourMixingRatio
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PySDM.products.impl.moist_environment_product import MoistEnvironmentProduct


class AmbientDryAirDensity(MoistEnvironmentProduct):
def __init__(self, name="rhod", unit="kg/m^3", var=None):
super().__init__(name=name, unit=unit, var=var)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PySDM.products.impl.moist_environment_product import MoistEnvironmentProduct


class AmbientDryAirPotentialTemperature(MoistEnvironmentProduct):
def __init__(self, unit='K', name=None, var=None):
super().__init__(unit=unit, name=name, var=var)
6 changes: 6 additions & 0 deletions PySDM/products/ambient_thermodynamics/ambient_pressure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PySDM.products.impl.moist_environment_product import MoistEnvironmentProduct


class AmbientPressure(MoistEnvironmentProduct):
def __init__(self, name=None, unit="Pa", var=None):
super().__init__(name=name, unit=unit, var=var)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PySDM.products.impl.moist_environment_product import MoistEnvironmentProduct


class AmbientRelativeHumidity(MoistEnvironmentProduct):
def __init__(self, name=None, unit='dimensionless', var=None):
super().__init__(name=name, unit=unit, var=var)
6 changes: 6 additions & 0 deletions PySDM/products/ambient_thermodynamics/ambient_temperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PySDM.products.impl.moist_environment_product import MoistEnvironmentProduct


class AmbientTemperature(MoistEnvironmentProduct):
def __init__(self, name=None, unit="K", var=None):
super().__init__(name=name, unit=unit, var=var)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from PySDM.products.impl.moist_environment_product import MoistEnvironmentProduct


class AmbientWaterVapourMixingRatio(MoistEnvironmentProduct):
def __init__(self, name=None, unit="dimensionless", var=None):
super().__init__(unit=unit, name=name, var=var)
3 changes: 2 additions & 1 deletion PySDM/products/aqueous_chemistry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .gaseous_mole_fraction import GaseousMoleFraction
from .aqueous_mole_fraction import AqueousMoleFraction
from .pH import pH
from .acidity import Acidity
from .aqueous_mass_spectrum import AqueousMassSpectrum
from .total_dry_mass_mixing_ratio import TotalDryMassMixingRatio
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
average pH (averaging after or before taking the logarithm in pH definition)
with weighting either by number or volume
"""
from PySDM.impl.product import MomentProduct
import numpy as np
from PySDM.products.impl.moment_product import MomentProduct


class pH(MomentProduct):
def __init__(self, radius_range, weighting='volume', attr='conc_H'):
class Acidity(MomentProduct):
def __init__(self, *,
radius_range=(0, np.inf),
weighting='volume',
attr='conc_H',
unit='dimensionless',
name=None):
assert attr in ('pH', 'moles_H', 'conc_H')
self.attr = attr

Expand All @@ -18,18 +24,14 @@ def __init__(self, radius_range, weighting='volume', attr='conc_H'):
raise NotImplementedError()

self.radius_range = radius_range
super().__init__(
name='pH_' + attr + '_' + weighting + '_weighted',
unit='',
description='number-weighted pH'
)
super().__init__(name=name, unit=unit)

def register(self, builder):
builder.request_attribute('conc_H')
super().register(builder)

def get(self):
self.download_moment_to_buffer(
def _impl(self, **kwargs):
self._download_moment_to_buffer(
self.attr, rank=1,
filter_range=(self.formulae.trivia.volume(self.radius_range[0]),
self.formulae.trivia.volume(self.radius_range[1])),
Expand Down
30 changes: 15 additions & 15 deletions PySDM/products/aqueous_chemistry/aqueous_mass_spectrum.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import numpy as np
from chempy import Substance
from PySDM.physics.constants import convert_to, si
from PySDM.impl.product import SpectrumMomentProduct
from PySDM.physics.constants import si
from PySDM.products.impl.spectrum_moment_product import SpectrumMomentProduct
from PySDM.physics.aqueous_chemistry.support import AQUEOUS_COMPOUNDS


class AqueousMassSpectrum(SpectrumMomentProduct):

def __init__(self, key, dry_radius_bins_edges, specific=False):
super().__init__(
name=f'dm_{key}/dlog_10(dry diameter){"_spec" if specific else ""}',
unit=f'µg / {"kg" if specific else "m3"} / (unit dD/D)',
description=f'... {key} ...'
)
def __init__(self, key, dry_radius_bins_edges, specific=False, name=None, unit='kg/m^3'):
super().__init__(name=name, unit=unit)
self.key = key
self.dry_radius_bins_edges = dry_radius_bins_edges
self.molar_mass = Substance.from_formula(AQUEOUS_COMPOUNDS[key][0]).mass * si.g / si.mole
Expand All @@ -31,24 +26,29 @@ def register(self, builder):

self.shape = (*builder.particulator.mesh.grid, len(self.attr_bins_edges) - 1)

def get(self):
def _impl(self, **kwargs):
vals = np.empty([self.particulator.mesh.n_cell, len(self.attr_bins_edges) - 1])
self.recalculate_spectrum_moment(
self._recalculate_spectrum_moment(
attr=f'moles_{self.key}',
rank=1,
filter_attr='dry volume'
)

for i in range(vals.shape[1]):
self.download_spectrum_moment_to_buffer(rank=1, bin_number=i)
self._download_spectrum_moment_to_buffer(rank=1, bin_number=i)
vals[:, i] = self.buffer.ravel()
self.download_spectrum_moment_to_buffer(rank=0, bin_number=i)
self._download_spectrum_moment_to_buffer(rank=0, bin_number=i)
vals[:, i] *= self.buffer.ravel()
d_log10_diameter = np.diff(np.log10(2 * self.dry_radius_bins_edges))
vals *= self.molar_mass / d_log10_diameter / self.particulator.mesh.dv
convert_to(vals, si.ug)

if self.specific:
self.download_to_buffer(self.particulator.environment['rhod'])
self._download_to_buffer(self.particulator.environment['rhod'])
vals[:] /= self.buffer
return vals


class SpecificAqueousMassSpectrum(AqueousMassSpectrum):
def __init__(self, key, dry_radius_bins_edges, name=None, unit='dimensionless'):
super().__init__(key=key, dry_radius_bins_edges=dry_radius_bins_edges,
name=name, unit=unit, specific=True)
Loading

0 comments on commit 31d9a3e

Please sign in to comment.