def __init__(self, setup): self.particles = Particles(backend=setup.backend, n_sd=setup.n_sd, dt=setup.dt) self.particles.set_mesh_0d() self.particles.set_environment( MoistLagrangianParcelAdiabatic, { "mass_of_dry_air": setup.mass_of_dry_air, "p0": setup.p0, "q0": setup.q0, "T0": setup.T0, "w": setup.w, "z0": setup.z0 }) v_dry = phys.volume(radius=setup.r_dry) r_wet = r_wet_init(setup.r_dry, self.particles.environment, np.zeros_like(setup.n), setup.kappa) v_wet = phys.volume(radius=r_wet) self.particles.create_state_0d(n=setup.n, extensive={ 'dry volume': v_dry, 'volume': v_wet }, intensive={}) self.particles.add_dynamic(Condensation, {"kappa": setup.kappa}) self.n_steps = setup.n_steps
def __init__(self, setup): t_half = setup.z_half / setup.w_avg dt = (2 * t_half) / setup.n_steps self.particles = Particles(backend=setup.backend, n_sd=1, dt=dt) self.particles.set_mesh_0d() self.particles.set_environment( MoistLagrangianParcelAdiabatic, { "mass_of_dry_air": setup.mass_of_dry_air, "p0": setup.p0, "q0": setup.q0, "T0": setup.T0, "w": setup.w }) r_dry = np.array([setup.r_dry]) x_dry = phys.volume(radius=r_dry) n = np.array([setup.n_in_dv], dtype=np.int64) r_wet = r_wet_init(r_dry, self.particles.environment, np.zeros_like(n), setup.kappa) v_wet = phys.volume(radius=r_wet) self.particles.create_state_0d(n=n, extensive={ 'dry volume': x_dry, 'volume': v_wet }, intensive={}) self.particles.add_dynamic(Condensation, {"kappa": setup.kappa}) self.n_steps = setup.n_steps
class Simulation: def __init__(self, setup): self.particles = Particles(backend=setup.backend, n_sd=setup.n_sd, dt=setup.dt) self.particles.set_mesh_0d() self.particles.set_environment( MoistLagrangianParcelAdiabatic, { "mass_of_dry_air": setup.mass_of_dry_air, "p0": setup.p0, "q0": setup.q0, "T0": setup.T0, "w": setup.w, "z0": setup.z0 }) v_dry = phys.volume(radius=setup.r_dry) r_wet = r_wet_init(setup.r_dry, self.particles.environment, np.zeros_like(setup.n), setup.kappa) v_wet = phys.volume(radius=r_wet) self.particles.create_state_0d(n=setup.n, extensive={ 'dry volume': v_dry, 'volume': v_wet }, intensive={}) self.particles.add_dynamic(Condensation, {"kappa": setup.kappa}) self.n_steps = setup.n_steps # TODO: make it common with Arabas_and_Shima_2017 def save(self, output): cell_id = 0 volume = self.particles.state.get_backend_storage('volume') volume = self.particles.backend.to_ndarray(volume) output["r"].append(phys.radius(volume=volume)) output["S"].append(self.particles.environment["RH"][cell_id] - 1) output["qv"].append(self.particles.environment["qv"][cell_id]) output["T"].append(self.particles.environment["T"][cell_id]) output["z"].append(self.particles.environment["z"][cell_id]) output["t"].append(self.particles.environment["t"][cell_id]) def run(self): output = {"r": [], "S": [], "z": [], "t": [], "qv": [], "T": []} self.save(output) for step in range(self.n_steps): self.particles.run(1) self.save(output) return output
class Simulation: def __init__(self, setup): t_half = setup.z_half / setup.w_avg dt = (2 * t_half) / setup.n_steps self.particles = Particles(backend=setup.backend, n_sd=1, dt=dt) self.particles.set_mesh_0d() self.particles.set_environment( MoistLagrangianParcelAdiabatic, { "mass_of_dry_air": setup.mass_of_dry_air, "p0": setup.p0, "q0": setup.q0, "T0": setup.T0, "w": setup.w }) r_dry = np.array([setup.r_dry]) x_dry = phys.volume(radius=r_dry) n = np.array([setup.n_in_dv], dtype=np.int64) r_wet = r_wet_init(r_dry, self.particles.environment, np.zeros_like(n), setup.kappa) v_wet = phys.volume(radius=r_wet) self.particles.create_state_0d(n=n, extensive={ 'dry volume': x_dry, 'volume': v_wet }, intensive={}) self.particles.add_dynamic(Condensation, {"kappa": setup.kappa}) self.n_steps = setup.n_steps # TODO: common with Yang? def save(self, output): cell_id = 0 volume = self.particles.state.get_backend_storage('volume') volume = self.particles.backend.to_ndarray(volume) output["r"].append(phys.radius(volume=volume)) output["S"].append(self.particles.environment["RH"][cell_id] - 1) output["z"].append(self.particles.environment["z"][cell_id]) output["t"].append(self.particles.environment["t"][cell_id]) def run(self): output = {"r": [], "S": [], "z": [], "t": []} self.save(output) for step in range(self.n_steps): self.particles.run(1) self.save(output) return output
def run(self, controller=DummyController()): self.tmp = None # TODO! self.particles = Particles(n_sd=self.setup.n_sd, dt=self.setup.dt, backend=self.setup.backend) self.particles.set_mesh(grid=self.setup.grid, size=self.setup.size) self.particles.set_environment(MoistEulerian2DKinematic, { "stream_function": self.setup.stream_function, "field_values": self.setup.field_values, "rhod_of": self.setup.rhod }) self.particles.create_state_2d( extensive={}, intensive={}, spatial_discretisation=spatial_sampling.pseudorandom, spectral_discretisation=spectral_sampling.constant_multiplicity, spectrum_per_mass_of_dry_air=self.setup.spectrum_per_mass_of_dry_air, r_range=(self.setup.r_min, self.setup.r_max), kappa=self.setup.kappa ) if self.setup.processes["condensation"]: self.particles.add_dynamic(Condensation, {"kappa": self.setup.kappa}) if self.setup.processes["advection"]: self.particles.add_dynamic(EulerianAdvection, {}) self.particles.add_dynamic(Advection, {"scheme": 'FTBS'}) if self.setup.processes["coalescence"]: self.particles.add_dynamic(SDM, {"kernel": self.setup.kernel}) # TODO if self.storage is not None: self.storage.init(self.setup) with controller: for step in self.setup.steps: # TODO: rename output_steps if controller.panic: break for _ in range(step - self.particles.n_steps): self.particles.run(1) self.store(self.particles, step) controller.set_percent(step / self.setup.steps[-1]) return self.particles.stats
def test_set_mesh(self): # Arrange sut = Particles(0, 0, Default) grid = (1, 1) size = (1, 1) # Act & Assert sut.set_mesh(grid, size) try: sut.set_mesh(grid, size) except AssertionError: return assert False
def __init__(self, n_sd, dt, backend, stats=None): self.particles = Particles(n_sd, dt, backend, stats)
def __init__(self, backend, n_sd, dt=None): Particles.__init__(self, n_sd, dt, backend) # super(ParticlesBuilder, self).__init__(n_sd, dt, backend) self.particles = self self.mesh = None self.environment = None
class Simulation: def __init__(self, setup, storage): self.setup = setup self.storage = storage def run(self, controller=DummyController()): self.tmp = None # TODO! self.particles = Particles(n_sd=self.setup.n_sd, dt=self.setup.dt, backend=self.setup.backend) self.particles.set_mesh(grid=self.setup.grid, size=self.setup.size) self.particles.set_environment(MoistEulerian2DKinematic, { "stream_function": self.setup.stream_function, "field_values": self.setup.field_values, "rhod_of": self.setup.rhod }) self.particles.create_state_2d( extensive={}, intensive={}, spatial_discretisation=spatial_sampling.pseudorandom, spectral_discretisation=spectral_sampling.constant_multiplicity, spectrum_per_mass_of_dry_air=self.setup.spectrum_per_mass_of_dry_air, r_range=(self.setup.r_min, self.setup.r_max), kappa=self.setup.kappa ) if self.setup.processes["condensation"]: self.particles.add_dynamic(Condensation, {"kappa": self.setup.kappa}) if self.setup.processes["advection"]: self.particles.add_dynamic(EulerianAdvection, {}) self.particles.add_dynamic(Advection, {"scheme": 'FTBS'}) if self.setup.processes["coalescence"]: self.particles.add_dynamic(SDM, {"kernel": self.setup.kernel}) # TODO if self.storage is not None: self.storage.init(self.setup) with controller: for step in self.setup.steps: # TODO: rename output_steps if controller.panic: break for _ in range(step - self.particles.n_steps): self.particles.run(1) self.store(self.particles, step) controller.set_percent(step / self.setup.steps[-1]) return self.particles.stats def store(self, particles, step): backend = particles.backend eulerian_fields = particles.environment.eulerian_fields # allocations if self.tmp is None: # TODO: move to constructor n_moments = 0 for attr in self.setup.specs: for _ in self.setup.specs[attr]: n_moments += 1 self.moment_0 = backend.array(particles.mesh.n_cell, dtype=int) self.moments = backend.array((n_moments, particles.mesh.n_cell), dtype=float) self.tmp = np.empty(particles.mesh.n_cell) # store moments particles.state.moments(self.moment_0, self.moments, self.setup.specs) # TODO: attr_range backend.download(self.moment_0, self.tmp) self.tmp /= particles.mesh.dv self.storage.save(self.tmp.reshape(self.setup.grid), step, "m0") i = 0 for attr in self.setup.specs: for k in self.setup.specs[attr]: backend.download(self.moments[i], self.tmp) # TODO: [i] will not work self.tmp /= particles.mesh.dv self.storage.save(self.tmp.reshape(self.setup.grid), step, f"{attr}_m{k}") i += 1 # store advected fields for key in eulerian_fields.mpdatas.keys(): self.storage.save(eulerian_fields.mpdatas[key].curr.get(), step, key) # store auxiliary fields backend.download(particles.environment['RH'], self.tmp) self.storage.save(self.tmp.reshape(self.setup.grid), step, "RH")
def run(setup): particles = Particles(n_sd=setup.n_sd, dt=setup.dt, backend=setup.backend) particles.set_mesh_0d(setup.dv) particles.set_environment(Box, {}) v, n = constant_multiplicity(setup.n_sd, setup.spectrum, (setup.x_min, setup.x_max)) particles.create_state_0d(n=n, extensive={'volume': v}, intensive={}) particles.add_dynamic(SDM, {"kernel": setup.kernel}) states = {} for step in setup.steps: particles.run(step - particles.n_steps) setup.check(particles.state, particles.n_steps) states[particles.n_steps] = copy.deepcopy(particles.state) return states, particles.stats
def test_coalescence(): # TODO: np.random.RandomState in backend? # Arrange v_min = 4.186e-15 v_max = 4.186e-12 n_sd = 2**13 steps = [0, 30, 60] X0 = 4 / 3 * 3.14 * 30.531e-6**3 n_part = 2**23 # [m-3] dv = 1e6 # [m3] dt = 1 # [s] norm_factor = n_part * dv kernel = Golovin(b=1.5e3) # [s-1] spectrum = Exponential(norm_factor=norm_factor, scale=X0) particles = Particles(n_sd=n_sd, dt=dt, backend=backend) particles.set_mesh_0d(dv=dv) particles.set_environment(Box, {}) v, n = constant_multiplicity(n_sd, spectrum, (v_min, v_max)) particles.create_state_0d(n=n, extensive={'volume': v}, intensive={}) particles.add_dynamic(SDM, {"kernel": kernel}) states = {} # Act for step in steps: particles.run(step - particles.n_steps) states[particles.n_steps] = copy.deepcopy(particles.state) # Assert x_max = 0 for state in states.values(): assert x_max < state.max('volume') x_max = state.max('volume')