class TestMPDATA2D: def test_Arabas_et_al_2014_sanity(self, case): case = { "nx": case[0], "ny": case[1], "Cx": case[2], "Cy": case[3], "nt": case[4], "ni": case[5], "input": case[6], "output": case[7] } # Arrange sut = Factories.constant_2d(case["input"].reshape( (case["nx"], case["ny"])), [case["Cx"], case["Cy"]], options=Options(n_iters=case["ni"])) # Act sut.advance(nt=case["nt"]) # Assert np.testing.assert_almost_equal(sut.advectee.get(), case["output"].reshape( case["nx"], case["ny"]), decimal=4)
def test_upwind(shape, ij0, out, C): value = 44 scalar_field_init = np.zeros(shape) scalar_field_init[ij0] = value vector_field_init = (np.full((shape[0] + 1, shape[1]), C[0]), np.full((shape[0], shape[1] + 1), C[1])) options = Options(n_iters=1) advectee = ScalarField(scalar_field_init, halo=options.n_halo, boundary_conditions=(PeriodicBoundaryCondition(), PeriodicBoundaryCondition())) advector = VectorField(vector_field_init, halo=options.n_halo, boundary_conditions=(PeriodicBoundaryCondition(), PeriodicBoundaryCondition())) mpdata = Solver(stepper=Stepper(options=options, grid=shape, n_threads=1), advector=advector, advectee=advectee) mpdata.advance(nt=1) np.testing.assert_array_equal(mpdata.advectee.get(), out)
def test_upwind_1d(): state = np.array([0, 1, 0]) C = 1 mpdata = Factories.constant_1d(state, C, Options(n_iters=1)) nt = 5 conserved = np.sum(mpdata.advectee.get()) mpdata.advance(nt) assert np.sum(mpdata.advectee.get()) == conserved
def test_fig11(dtype: np.floating): # Arrange simulation = Simulation( Setup("rect"), Options(infinite_gauge=True, n_iters=2, dtype=dtype)) # Act simulation.run() psiT = simulation.state # Assert assert psiT.dtype == dtype assert -1.9 < np.amin(psiT) < 2 assert 4 < np.amax(psiT) < 4.2
def test_fig10(dtype: np.floating): # Arrange simulation = Simulation( Setup("cosine"), Options(infinite_gauge=True, n_iters=2, dtype=dtype)) # Act simulation.run() psiT = simulation.state # Assert assert psiT.dtype == dtype assert -.1 < np.amin(psiT) < 0 assert 1.75 < np.amax(psiT) < 1.9
def test_DPDC(n_iters): state = np.array([0, 1, 0]) C = .5 mpdata = Factories.constant_1d( state, C, Options(n_iters=n_iters, DPDC=True, flux_corrected_transport=True)) nt = 1 conserved = np.sum(mpdata.advectee.get()) mpdata.advance(nt) print(mpdata.advectee.get()) assert np.sum(mpdata.advectee.get()) == conserved
def test_fig4(dtype: np.floating): # Arrange simulation = Simulation(Setup("cosine"), Options(n_iters=2, dtype=dtype)) psi0 = simulation.state # Act simulation.run() psiT = simulation.state # Assert epsilon = 1e-20 assert psiT.dtype == dtype assert np.amin(psi0) == 0 assert np.amax(psi0) == 2 assert 0 < np.amin(psiT) < epsilon assert 1.3 < np.amax(psiT) < 1.4
def test_fig3(dtype: np.floating): # Arrange simulation = Simulation(Settings("cosine"), Options(n_iters=1, dtype=dtype)) psi_0 = simulation.state # Act simulation.run() psi_t = simulation.state # Assert epsilon = 1e-20 assert psi_t.dtype == dtype assert np.amin(psi_0) == 0 assert np.amax(psi_0) == 2 assert 0 < np.amin(psi_t) < epsilon assert .45 < np.amax(psi_t) < .5
def test_fig12(dtype: np.floating): # Arrange simulation = Simulation( Setup("rect"), Options(n_iters=2, infinite_gauge=True, flux_corrected_transport=True, dtype=dtype)) # Act simulation.run() psiT = simulation.state # Assert assert psiT.dtype == dtype assert np.amin(psiT) >= 2 assert np.amax(psiT) <= 4 assert np.amax(psiT) > 3
def test_fig12(dtype: np.floating): # Arrange simulation = Simulation( Settings("rect"), Options(n_iters=2, infinite_gauge=True, nonoscillatory=True, dtype=dtype)) # Act simulation.run() psi_t = simulation.state # Assert assert psi_t.dtype == dtype assert np.amin(psi_t) >= 2 assert np.amax(psi_t) <= 4 assert np.amax(psi_t) > 3
def test_init(grid_layout, psi_coord, flux_corrected_transport): # Arrange opts = Options(flux_corrected_transport=flux_corrected_transport) # Act simulation = Simulation(setup, grid_layout=grid_layout, GC_max=default_GC_max, psi_coord=psi_coord, opts=opts) simulation.step(1) # Asserts for array shapes assert simulation.n_of_r.shape[0] == setup.nr # Asserts for Jacobian G_with_halo = simulation.solver.g_factor.data assert np.isfinite(G_with_halo).all() if type(psi_coord) == type(grid_layout): np.testing.assert_array_almost_equal(np.diff(G_with_halo), 0) else: assert (np.diff(G_with_halo) >= 0).all() or (np.diff(G_with_halo) <= 0).all()
def test_init(grid_layout, psi_coord, nonoscillatory): # Arrange opts = Options(nonoscillatory=nonoscillatory) # Act simulation = Simulation(settings, grid_layout=grid_layout, GC_max=default_GC_max, psi_coord=psi_coord, opts=opts) simulation.step(1) # Asserts for array shapes assert simulation.n_of_r.shape[0] == settings.nr # Asserts for Jacobian g_factor_with_halo = simulation.solver.g_factor.data assert np.isfinite(g_factor_with_halo).all() if isinstance(psi_coord, type(grid_layout)): np.testing.assert_array_almost_equal(np.diff(g_factor_with_halo), 0) else: assert (np.diff(g_factor_with_halo) >= 0).all() or (np.diff(g_factor_with_halo) <= 0).all()
def from_pdf_2d(pdf, xrange, yrange, gridsize): z = np.empty(gridsize) dx, dy = (xrange[1] - xrange[0]) / gridsize[0], (yrange[1] - yrange[0]) / gridsize[1] for i in range(gridsize[0]): for j in range(gridsize[1]): z[i, j] = pdf(xrange[0] + dx * (i + .5), yrange[0] + dy * (j + .5)) x = np.linspace(xrange[0] + dx / 2, xrange[1] - dx / 2, gridsize[0]) y = np.linspace(yrange[0] + dy / 2, yrange[1] - dy / 2, gridsize[1]) return x, y, z @pytest.mark.parametrize("options", [ Options(n_iters=1), Options(n_iters=2), Options(n_iters=3, infinite_gauge=True), Options(n_iters=2, infinite_gauge=True, flux_corrected_transport=True) ]) @pytest.mark.parametrize("dtype", (np.float64, )) @pytest.mark.parametrize("grid_static_str", ("static", "dynamic")) @pytest.mark.parametrize("concurrency_str", ("threads", "serial")) def test_timing_2d(benchmark, options, dtype, grid_static_str, concurrency_str, plot=False): if grid_static_str == "static": grid_static = True
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring import time import numba from PyMPDATA.options import Options from PyMPDATA.impl.clock import clock jit_flags = Options().jit_flags class TestClock: @staticmethod def test_clock_python(): clock() @staticmethod def test_clock_numba_jit(): @numba.jit(**jit_flags) def test(): clock() test() @staticmethod def test_clock_numba_njit(): @numba.njit(**jit_flags) def test(): clock() test() @staticmethod
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring import pytest import numpy as np from PyMPDATA_examples.Smolarkiewicz_2006_Figs_3_4_10_11_12.settings import Settings from PyMPDATA_examples.Smolarkiewicz_2006_Figs_3_4_10_11_12.simulation import Simulation from PyMPDATA.options import Options @pytest.mark.parametrize("options", [ Options(n_iters=1), Options(n_iters=2), Options(n_iters=3), Options(n_iters=4), Options(n_iters=2, infinite_gauge=True), Options(n_iters=3, infinite_gauge=True), Options(n_iters=2, nonoscillatory=True), Options(n_iters=3, nonoscillatory=True), Options(n_iters=2, divergent_flow=True), Options(n_iters=3, divergent_flow=True), Options(n_iters=2, third_order_terms=True), Options(n_iters=3, third_order_terms=True) ]) # pylint: disable-next=redefined-outer-name def test_timing_1d(benchmark, options): simulation = Simulation(Settings("cosine"), options) psi0 = simulation.stepper.advectee.get().copy() def set_psi(): simulation.stepper.advectee.get()[:] = psi0 benchmark.pedantic(simulation.run, {},
from PyMPDATA.options import Options from PyMPDATA_examples.Smolarkiewicz_2006_Figs_3_4_10_11_12.setup import Setup from PyMPDATA_examples.Smolarkiewicz_2006_Figs_3_4_10_11_12.simulation import Simulation import pytest import numpy as np @pytest.mark.parametrize("options", [ Options(n_iters=1), Options(n_iters=2), Options(n_iters=3), Options(n_iters=4), Options(n_iters=2, infinite_gauge=True), Options(n_iters=3, infinite_gauge=True), Options(n_iters=2, flux_corrected_transport=True), Options(n_iters=3, flux_corrected_transport=True), Options(n_iters=2, divergent_flow=True), Options(n_iters=3, divergent_flow=True), Options(n_iters=2, third_order_terms=True), Options(n_iters=3, third_order_terms=True) ]) def test_timing_1d(benchmark, options): simulation = Simulation(Setup("cosine"), options) psi0 = simulation.stepper.advectee.get().copy() def set_psi(): simulation.stepper.advectee.get()[:] = psi0