PySDM_examples.Niedermeier_et_al_2014.simulation
1import numpy as np 2from PySDM_examples.Ervens_and_Feingold_2012.settings import ( 3 sampled_ccn_diameter_number_concentration_spectrum, 4) 5from PySDM_examples.Niedermeier_et_al_2014.settings import Settings 6from PySDM_examples.utils import BasicSimulation 7 8from PySDM import Builder 9from PySDM.backends import CPU 10from PySDM.dynamics import AmbientThermodynamics, Condensation, Freezing 11from PySDM.environments import Parcel 12from PySDM.initialisation import equilibrate_wet_radii 13from PySDM.products import AmbientTemperature, IceWaterContent, ParcelDisplacement 14 15 16class Simulation(BasicSimulation): 17 def __init__(self, settings: Settings): 18 n_particles = settings.ccn_sampling_n - 1 + settings.in_sampling_n 19 env = Parcel( 20 dt=settings.timestep, 21 p0=settings.p0, 22 T0=settings.T0, 23 initial_water_vapour_mixing_ratio=settings.initial_water_vapour_mixing_ratio, 24 mass_of_dry_air=settings.mass_of_dry_air, 25 w=settings.vertical_velocity, 26 mixed_phase=True, 27 ) 28 builder = Builder( 29 n_sd=n_particles, backend=CPU(settings.formulae), environment=env 30 ) 31 32 builder.add_dynamic(AmbientThermodynamics()) 33 builder.add_dynamic(Condensation()) 34 builder.add_dynamic(Freezing(singular=False)) 35 36 air_volume = settings.mass_of_dry_air / settings.rhod0 37 ( 38 ccn_diameter, 39 ccn_conc_float, 40 ) = sampled_ccn_diameter_number_concentration_spectrum( 41 size_range=settings.ccn_dry_diameter_range, n_sd=settings.ccn_sampling_n 42 ) 43 dry_volume = settings.formulae.trivia.volume(radius=ccn_diameter / 2) 44 45 immersed_surface_area = np.zeros_like(dry_volume) 46 immersed_surface_area[-1] = settings.formulae.trivia.sphere_surface( 47 diameter=ccn_diameter[-1] 48 ) 49 50 attributes = { 51 "multiplicity": ccn_conc_float * air_volume, 52 "dry volume": dry_volume, 53 "kappa times dry volume": settings.kappa * dry_volume, 54 "volume": None, 55 "immersed surface area": immersed_surface_area, 56 } 57 attributes["volume"] = settings.formulae.trivia.volume( 58 radius=equilibrate_wet_radii( 59 r_dry=ccn_diameter / 2, 60 environment=builder.particulator.environment, 61 kappa_times_dry_volume=attributes["kappa times dry volume"], 62 ) 63 ) 64 65 for attribute, data in attributes.items(): 66 attributes[attribute] = np.concatenate( 67 ( 68 data[:-1], 69 np.full( 70 settings.in_sampling_n, 71 ( 72 data[-1] 73 if attribute != "multiplicity" 74 else data[-1] / settings.in_sampling_n 75 ), 76 ), 77 ) 78 ) 79 80 products = ( 81 IceWaterContent(), 82 ParcelDisplacement(name="z"), 83 AmbientTemperature(name="T"), 84 ) 85 super().__init__(builder.build(attributes=attributes, products=products)) 86 self.steps = int( 87 settings.displacement / settings.vertical_velocity / settings.timestep 88 ) 89 90 def run(self): 91 return super()._run(nt=self.steps, steps_per_output_interval=1)
17class Simulation(BasicSimulation): 18 def __init__(self, settings: Settings): 19 n_particles = settings.ccn_sampling_n - 1 + settings.in_sampling_n 20 env = Parcel( 21 dt=settings.timestep, 22 p0=settings.p0, 23 T0=settings.T0, 24 initial_water_vapour_mixing_ratio=settings.initial_water_vapour_mixing_ratio, 25 mass_of_dry_air=settings.mass_of_dry_air, 26 w=settings.vertical_velocity, 27 mixed_phase=True, 28 ) 29 builder = Builder( 30 n_sd=n_particles, backend=CPU(settings.formulae), environment=env 31 ) 32 33 builder.add_dynamic(AmbientThermodynamics()) 34 builder.add_dynamic(Condensation()) 35 builder.add_dynamic(Freezing(singular=False)) 36 37 air_volume = settings.mass_of_dry_air / settings.rhod0 38 ( 39 ccn_diameter, 40 ccn_conc_float, 41 ) = sampled_ccn_diameter_number_concentration_spectrum( 42 size_range=settings.ccn_dry_diameter_range, n_sd=settings.ccn_sampling_n 43 ) 44 dry_volume = settings.formulae.trivia.volume(radius=ccn_diameter / 2) 45 46 immersed_surface_area = np.zeros_like(dry_volume) 47 immersed_surface_area[-1] = settings.formulae.trivia.sphere_surface( 48 diameter=ccn_diameter[-1] 49 ) 50 51 attributes = { 52 "multiplicity": ccn_conc_float * air_volume, 53 "dry volume": dry_volume, 54 "kappa times dry volume": settings.kappa * dry_volume, 55 "volume": None, 56 "immersed surface area": immersed_surface_area, 57 } 58 attributes["volume"] = settings.formulae.trivia.volume( 59 radius=equilibrate_wet_radii( 60 r_dry=ccn_diameter / 2, 61 environment=builder.particulator.environment, 62 kappa_times_dry_volume=attributes["kappa times dry volume"], 63 ) 64 ) 65 66 for attribute, data in attributes.items(): 67 attributes[attribute] = np.concatenate( 68 ( 69 data[:-1], 70 np.full( 71 settings.in_sampling_n, 72 ( 73 data[-1] 74 if attribute != "multiplicity" 75 else data[-1] / settings.in_sampling_n 76 ), 77 ), 78 ) 79 ) 80 81 products = ( 82 IceWaterContent(), 83 ParcelDisplacement(name="z"), 84 AmbientTemperature(name="T"), 85 ) 86 super().__init__(builder.build(attributes=attributes, products=products)) 87 self.steps = int( 88 settings.displacement / settings.vertical_velocity / settings.timestep 89 ) 90 91 def run(self): 92 return super()._run(nt=self.steps, steps_per_output_interval=1)
Simulation(settings: PySDM_examples.Niedermeier_et_al_2014.settings.Settings)
18 def __init__(self, settings: Settings): 19 n_particles = settings.ccn_sampling_n - 1 + settings.in_sampling_n 20 env = Parcel( 21 dt=settings.timestep, 22 p0=settings.p0, 23 T0=settings.T0, 24 initial_water_vapour_mixing_ratio=settings.initial_water_vapour_mixing_ratio, 25 mass_of_dry_air=settings.mass_of_dry_air, 26 w=settings.vertical_velocity, 27 mixed_phase=True, 28 ) 29 builder = Builder( 30 n_sd=n_particles, backend=CPU(settings.formulae), environment=env 31 ) 32 33 builder.add_dynamic(AmbientThermodynamics()) 34 builder.add_dynamic(Condensation()) 35 builder.add_dynamic(Freezing(singular=False)) 36 37 air_volume = settings.mass_of_dry_air / settings.rhod0 38 ( 39 ccn_diameter, 40 ccn_conc_float, 41 ) = sampled_ccn_diameter_number_concentration_spectrum( 42 size_range=settings.ccn_dry_diameter_range, n_sd=settings.ccn_sampling_n 43 ) 44 dry_volume = settings.formulae.trivia.volume(radius=ccn_diameter / 2) 45 46 immersed_surface_area = np.zeros_like(dry_volume) 47 immersed_surface_area[-1] = settings.formulae.trivia.sphere_surface( 48 diameter=ccn_diameter[-1] 49 ) 50 51 attributes = { 52 "multiplicity": ccn_conc_float * air_volume, 53 "dry volume": dry_volume, 54 "kappa times dry volume": settings.kappa * dry_volume, 55 "volume": None, 56 "immersed surface area": immersed_surface_area, 57 } 58 attributes["volume"] = settings.formulae.trivia.volume( 59 radius=equilibrate_wet_radii( 60 r_dry=ccn_diameter / 2, 61 environment=builder.particulator.environment, 62 kappa_times_dry_volume=attributes["kappa times dry volume"], 63 ) 64 ) 65 66 for attribute, data in attributes.items(): 67 attributes[attribute] = np.concatenate( 68 ( 69 data[:-1], 70 np.full( 71 settings.in_sampling_n, 72 ( 73 data[-1] 74 if attribute != "multiplicity" 75 else data[-1] / settings.in_sampling_n 76 ), 77 ), 78 ) 79 ) 80 81 products = ( 82 IceWaterContent(), 83 ParcelDisplacement(name="z"), 84 AmbientTemperature(name="T"), 85 ) 86 super().__init__(builder.build(attributes=attributes, products=products)) 87 self.steps = int( 88 settings.displacement / settings.vertical_velocity / settings.timestep 89 )