def test_spin_up(fastmath, plot=False): # Arrange settings = Settings(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) 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 = storage.profiles[step + 1]["qv_env"] prev = storage.profiles[step]["qv_env"] eps = 1e-3 assert ((prev + eps) >= next).all() assert storage.profiles[step_num]["qv_env"][-1] < 7.
def test_Arabas_et_al_2015_export(): # Arrange settings = DemoSettings() settings.ui_simulation_time = IntSlider(value=10) settings.ui_dt = IntSlider(value=10) settings.ui_output_options['interval'] = IntSlider( value=settings.ui_dt.value) assert settings.n_steps == 1 assert len(settings.output_steps) == 2 and settings.output_steps[-1] == 1 storage = Storage() simulator = Simulation(settings=settings, storage=storage, backend=CPU) file = TemporaryFile() exporter = NetCDFExporter(storage=storage, settings=settings, simulator=simulator, filename=file.absolute_path) # Act simulator.reinit() simulator.run() exporter.run() # Assert versions = netcdf.netcdf_file(file.absolute_path).versions assert 'PyMPDATA' in str(versions)
def main(): settings = Settings() settings.n_sd_per_gridbox = 25 settings.grid = (25, 25) settings.simulation_time = 5400 * si.second storage = Storage() simulation = Simulation(settings, storage) simulation.reinit() simulation.run() temp_file = TemporaryFile('.nc') exporter = NetCDFExporter(storage, settings, simulation, temp_file.absolute_path) exporter.run()
def test_export(tmp_path): # Arrange settings = Settings() settings.simulation_time = settings.dt settings.output_interval = settings.dt storage = Storage() simulator = Simulation(settings, storage) _, temp_file = tempfile.mkstemp(dir=tmp_path, suffix='.nc') sut = NetCDFExporter(storage, settings, simulator, temp_file) simulator.reinit() simulator.run() # Act sut.run()
def launch(): settings = DemoSettings() storage = Storage() simulator = Simulation(settings, storage) temporary_file = TemporaryFile('.nc') exporter = NetCDFExporter(storage, settings, simulator, temporary_file.absolute_path) viewer = DemoViewer(storage, settings) controller = DemoController(simulator, viewer, exporter, temporary_file) tabs = Tab([VBox([controller.box(), viewer.box()]), settings.box()]) tabs.set_title(1, "Settings") tabs.set_title(0, "Simulation") tabs.observe(controller.reinit, 'selected_index') # https://github.com/googlecolab/colabtools/issues/1302 if 'google.colab' in sys.modules: display( HTML( '''<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> ''' )) display(tabs)
def main(): settings = Settings() settings.grid = (25, 25) settings.simulation_time = settings.dt * 100 settings.output_interval = settings.dt * 10 settings.processes = { "particle advection": True, "fluid advection": True, "coalescence": True, "condensation": False, "sedimentation": True, } n_sd = range(14, 16, 1) times = {} backends = [(CPU, "sync"), (CPU, "async")] if GPU.ENABLE: backends.append((GPU, "async")) for backend, mode in backends: if backend is CPU: PySDM.backends.numba.conf.NUMBA_PARALLEL = mode reload_CPU_backend() key = f"{backend} (mode={mode})" times[key] = [] for sd in n_sd: settings.n_sd_per_gridbox = sd storage = Storage() simulation = Simulation(settings, storage, backend) simulation.reinit(products=[WallTime()]) simulation.run() times[key].append(storage.load('wall_time')[-1]) from matplotlib import pyplot as plt for parallelization, t in times.items(): plt.plot(n_sd, t, label=parallelization) plt.legend() plt.loglog() plt.savefig("benchmark.pdf", format="pdf")
def test_environment(): # Arrange settings = Settings() settings.simulation_time = -1 * settings.dt simulation = Simulation(settings, None) simulation.reinit() # Act simulation.run() rhod = simulation.core.environment["rhod"].to_ndarray().reshape( settings.grid) # Assert - same in all columns for column in range(settings.grid[0]): np.testing.assert_array_equal(rhod[column, :], rhod[0, :]) # Assert - decreasing with altitude rhod_below = 2 * si.kilograms / si.metre**3 for level in range(settings.grid[1]): rhod_above = rhod[0, level] assert rhod_above < rhod_below rhod_below = rhod_above
def test_initialisation(backend, plot=False): settings = Settings() settings.simulation_time = -1 * settings.dt settings.grid = (10, 5) settings.n_sd_per_gridbox = 5000 simulation = Simulation(settings, None, backend) n_levels = settings.grid[1] n_cell = int(np.prod(np.array(settings.grid))) n_moments = 1 r_bins = settings.formulae.trivia.radius(volume=settings.v_bins) histogram_dry = np.empty((len(r_bins) - 1, n_levels)) histogram_wet = np.empty_like(histogram_dry) moment_0 = backend.Storage.empty(n_cell, dtype=int) moments = backend.Storage.empty((n_moments, n_cell), dtype=float) tmp = np.empty(n_cell) simulation.reinit() # Act (moments) simulation.run() particles = simulation.core environment = simulation.core.environment rhod = environment["rhod"].to_ndarray().reshape(settings.grid).mean(axis=0) for i in range(len(histogram_dry)): particles.particles.moments(moment_0, moments, specs={}, attr_name='dry volume', attr_range=(settings.v_bins[i], settings.v_bins[i + 1])) moment_0.download(tmp) histogram_dry[i, :] = tmp.reshape( settings.grid).sum(axis=0) / (particles.mesh.dv * settings.grid[0]) particles.particles.moments(moment_0, moments, specs={}, attr_name='volume', attr_range=(settings.v_bins[i], settings.v_bins[i + 1])) moment_0.download(tmp) histogram_wet[i, :] = tmp.reshape( settings.grid).sum(axis=0) / (particles.mesh.dv * settings.grid[0]) # Plot if plot: for level in range(0, n_levels): color = str(.75 * (level / (n_levels - 1))) pyplot.step(r_bins[:-1] * si.metres / si.micrometres, histogram_dry[:, level] / si.metre**3 * si.centimetre**3, where='post', color=color, label="level " + str(level)) pyplot.step(r_bins[:-1] * si.metres / si.micrometres, histogram_wet[:, level] / si.metre**3 * si.centimetre**3, where='post', color=color, linestyle='--') pyplot.grid() pyplot.xscale('log') pyplot.xlabel('particle radius [µm]') pyplot.ylabel('concentration per bin [cm^{-3}]') pyplot.legend() pyplot.show() # Assert - total number for level in reversed(range(n_levels)): mass_conc_dry = np.sum(histogram_dry[:, level]) / rhod[level] mass_conc_wet = np.sum(histogram_wet[:, level]) / rhod[level] mass_conc_STP = settings.spectrum_per_mass_of_dry_air.norm_factor assert .5 * mass_conc_STP < mass_conc_dry < 1.5 * mass_conc_STP np.testing.assert_approx_equal(mass_conc_dry, mass_conc_wet, significant=5) # Assert - decreasing number density total_above = 0 for level in reversed(range(n_levels)): total_below = np.sum(histogram_dry[:, level]) assert total_below > total_above total_above = total_below