def test_fake_numba(): # Arrange sut = DimensionalAnalysis() # Act & Assert assert hasattr(Formulae().saturation_vapour_pressure.pvs_Celsius, "py_func") with sut: assert not hasattr(Formulae().saturation_vapour_pressure.pvs_Celsius, "py_func") assert hasattr(Formulae().saturation_vapour_pressure.pvs_Celsius, "py_func")
def __init__(self): self.formulae = Formulae() self.n_sd = 2**12 self.n_part = 1e4 / si.cm**3 self.theta = 0.33e-9 * si.g / rho_w self.k = 1 self.dv = 0.1 * si.m**3 self.norm_factor = self.n_part * self.dv self.dt = 1 * si.seconds self.adaptive = False self.seed = 44 self._steps = list(range(60)) self.kernel = Golovin(b=2000 * si.cm**3 / si.g / si.s * rho_w) self.coal_effs = [ConstEc(Ec=0.8), ConstEc(Ec=0.9), ConstEc(Ec=1.0)] self.vmin = 1.0 * si.um**3 self.nfmax = 10 self.fragtol = 1e-3 self.fragmentation = Feingold1988Frag( scale=self.k * self.theta, fragtol=self.fragtol, vmin=self.vmin, nfmax=self.nfmax, ) self.break_eff = ConstEb(1.0) self.spectrum = Gamma(norm_factor=self.norm_factor, k=self.k, theta=self.theta) self.rho = rho_w
def test_spin_up(backend_class, fastmath, plot=False): # Arrange settings = Settings(Formulae(fastmath=fastmath)) settings.dt = .5 * si.second settings.grid = (3, 25) settings.simulation_time = 20 * settings.dt settings.output_interval = 1 * settings.dt storage = DummyStorage() simulation = Simulation(settings, storage, SpinUp=SpinUp, backend_class=backend_class) simulation.reinit() # Act simulation.run() # Plot if plot: levels = np.arange(settings.grid[1]) for step, datum in enumerate(storage.profiles): pyplot.plot(datum["qv_env"], levels, label=str(step)) pyplot.legend() pyplot.show() # Assert step_num = len(storage.profiles) - 1 for step in range(step_num): next_profile = storage.profiles[step + 1]["qv_env"] prev_profile = storage.profiles[step]["qv_env"] eps = 1e-3 assert ((prev_profile + eps) >= next_profile).all() assert storage.profiles[step_num]["qv_env"][-1] < 7.1
def test___str__(): # Arrange sut = Formulae() # Act result = str(sut) # Assert assert len(result) > 0
def __init__(self, formulae=None): self.formulae = formulae or Formulae() CollisionsMethods.__init__(self) PairMethods.__init__(self) IndexMethods.__init__(self) PhysicsMethods.__init__(self) CondensationMethods.__init__(self) ChemistryMethods.__init__(self) MomentsMethods.__init__(self) FreezingMethods.__init__(self) DisplacementMethods.__init__(self) TerminalVelocityMethods.__init__(self)
def test_analytic_solution_underflow(x): # Arrange formulae = Formulae() b = 1.5e3 x_0 = formulae.trivia.volume(radius=30.531e-6) N_0 = 2**23 sut = Golovin(b) # Act value = sut.analytic_solution(x=x, t=1200, x_0=x_0, N_0=N_0) # Assert assert np.all(np.isfinite(value))
def test_pvs_ice(opt): with DimensionalAnalysis(): # Arrange formulae = Formulae(saturation_vapour_pressure=opt) si = constants_defaults.si sut = formulae.saturation_vapour_pressure.ice_Celsius T = 250 * si.kelvins # Act pvs = sut(T) # Assert assert pvs.check('[pressure]')
def test_lv(opt): with DimensionalAnalysis(): # Arrange si = constants_defaults.si T = 300 * si.kelvins formulae = Formulae(latent_heat=opt) sut = formulae.latent_heat.lv # Act lv = sut(T) # Assert assert lv.check('[energy]/[mass]')
def test_thermal_conductivity_radius_dependence(opt): with DimensionalAnalysis(): # Arrange si = constants_defaults.si r = 1 * si.um lmbd = .1 * si.um formulae = Formulae(diffusion_kinetics=opt) sut = formulae.diffusion_kinetics.K # Act thermal_conductivity = sut(constants_defaults.K0, r, lmbd) # Assert assert thermal_conductivity.check('[power]/[length]/[temperature]')
def test_thermal_conductivity_temperature_dependence(opt): with DimensionalAnalysis(): # Arrange si = constants_defaults.si T = 300 * si.kelvins p = 1000 * si.hPa formulae = Formulae(diffusion_thermics=opt) sut = formulae.diffusion_thermics.K # Act thermal_conductivity = sut(T, p) # Assert assert thermal_conductivity.check('[power]/[length]/[temperature]')
def test_vapour_diffusivity_radius_dependence(opt): with DimensionalAnalysis(): # Arrange si = constants_defaults.si r = 1 * si.um lmbd = .1 * si.um formulae = Formulae(diffusion_kinetics=opt) sut = formulae.diffusion_kinetics.D # Act vpour_diffusivity = sut(constants_defaults.D0, r, lmbd) # Assert assert vpour_diffusivity.check('[area]/[time]')
def test_vapour_diffusivity_temperature_dependence(opt): with DimensionalAnalysis(): # Arrange si = constants_defaults.si T = 300 * si.kelvins p = 1000 * si.hPa formulae = Formulae(diffusion_thermics=opt) sut = formulae.diffusion_thermics.D # Act vpour_diffusivity = sut(T, p) # Assert assert vpour_diffusivity.check('[area]/[time]')
def test_r_cr(): with DimensionalAnalysis(): # Arrange si = constants_defaults.si formulae = Formulae() sut = formulae.hygroscopicity.r_cr kp = .5 rd = .1 * si.micrometre T = 300 * si.kelvins sgm = constants_defaults.sgm_w # Act r_cr = sut(kp, rd**3, T, sgm) # Assert assert r_cr.to_base_units().units == si.metres
def test_toms748(fun): sut = toms748_solve if 'NUMBA_DISABLE_JIT' in os.environ else toms748_solve.py_func a = -.5 b = .5 rtol = 1e-6 wt = Formulae().trivia.within_tolerance actual, _ = sut(fun, (), ax=a, bx=b, fax=fun(a), fbx=fun(b), max_iter=10, rtol=rtol, within_tolerance=wt) expected = toms748(fun, a, b) np.testing.assert_almost_equal(actual, expected)
def test_coalescence(backend_class, croupier, adaptive): if backend_class == ThrustRTC and croupier == 'local': # TODO #358 return if backend_class == ThrustRTC and adaptive and croupier == 'global': # TODO #329 return # Arrange formulae = Formulae(seed=256) n_sd = 2 ** 14 steps = [0, 100, 200] X0 = formulae.trivia.volume(radius=30.531e-6) n_part = 2 ** 23 / si.metre ** 3 dv = 1e6 * si.metres ** 3 dt = 1 * si.seconds norm_factor = n_part * dv rho = 1000 * si.kilogram / si.metre ** 3 kernel = Golovin(b=1.5e3) # [s-1] spectrum = Exponential(norm_factor=norm_factor, scale=X0) builder = Builder(n_sd=n_sd, backend=backend_class(formulae=formulae)) builder.set_environment(Box(dt=dt, dv=dv)) attributes = {} attributes['volume'], attributes['n'] = ConstantMultiplicity(spectrum).sample(n_sd) builder.add_dynamic(Coalescence(kernel, croupier=croupier, adaptive=adaptive)) particulator = builder.build(attributes) volumes = {} # Act for step in steps: particulator.run(step - particulator.n_steps) check(n_part, dv, n_sd, rho, particulator.attributes, step) volumes[particulator.n_steps] = particulator.attributes['volume'].to_ndarray() # Assert x_max = 0 for volume in volumes.values(): assert x_max < np.amax(volume) x_max = np.amax(volume)
def __init__(self): self.formulae = Formulae() self.n_sd = 2**20 self.n_part = 100 / si.cm**3 self.X0 = self.formulae.trivia.volume(radius=30.531 * si.micrometres) self.dv = 1 * si.m**3 self.norm_factor = self.n_part * self.dv self.rho = 1000 * si.kilogram / si.metre**3 self.dt = 1 * si.seconds self.adaptive = False self.seed = 44 self._steps = [0] self.kernel = Geometric() self.coal_eff = Berry1967() self.fragmentation = ExponFrag(scale=100.0 * si.micrometres) self.break_eff = ConstEb(1.0) # no "bouncing" self.spectrum = Exponential(norm_factor=self.norm_factor, scale=self.X0) self.radius_bins_edges = np.logspace(np.log10(10 * si.um), np.log10(5000 * si.um), num=128, endpoint=True) self.radius_range = [0 * si.um, 1e6 * si.um]
def __init__(self, formulae=None, double_precision=False, debug=False, verbose=False): self.formulae = formulae or Formulae() self._conv_function = trtc.DVDouble if double_precision else trtc.DVFloat self._real_type = "double" if double_precision else "float" self._np_dtype = np.float64 if double_precision else np.float32 self.Storage = make_storage_class(self) CollisionsMethods.__init__(self) PairMethods.__init__(self) IndexMethods.__init__(self) PhysicsMethods.__init__(self) CondensationMethods.__init__(self) MomentsMethods.__init__(self) DisplacementMethods.__init__(self) TerminalVelocityMethods.__init__(self) trtc.Set_Kernel_Debug(debug) trtc.Set_Verbose(verbose) if not ThrustRTC.ENABLE \ and 'CI' not in os.environ: warnings.warn('CUDA is not available, using FakeThrustRTC!')
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring from collections import defaultdict import numpy as np import pytest from chempy import Equilibrium from chempy.equilibria import EqSystem from chempy.chemistry import Species from PySDM.dynamics.impl.chemistry_utils import M, EquilibriumConsts from PySDM.physics.constants import K_H2O from PySDM.formulae import Formulae from PySDM.backends.impl_numba.methods.chemistry_methods import ChemistryMethods, _K, _conc from PySDM.dynamics import aqueous_chemistry FORMULAE = Formulae() EQUILIBRIUM_CONST = EquilibriumConsts(FORMULAE).EQUILIBRIUM_CONST class TestAcidity: @staticmethod def test_equilibrate_pH_pure_water(): # Arrange eqs = {} for key, const in EQUILIBRIUM_CONST.items(): eqs[key] = np.full(1, const.at(FORMULAE.constants.ROOM_TEMP)) # Act result = np.empty(1) ChemistryMethods.equilibrate_H_body( within_tolerance=FORMULAE.trivia.within_tolerance, pH2H=FORMULAE.trivia.pH2H, H2pH=FORMULAE.trivia.H2pH,