Skip to content

Commit

Permalink
some cleaning in python tests (#719)
Browse files Browse the repository at this point in the history
* some cleaning in python code
  • Loading branch information
nicolasaunai authored Feb 12, 2023
1 parent 1a14417 commit 774fd61
Show file tree
Hide file tree
Showing 19 changed files with 1,008 additions and 649 deletions.
47 changes: 27 additions & 20 deletions tests/initializer/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,41 @@
Simulation(
smallest_patch_size=10,
largest_patch_size=64,
time_step_nbr=1000, # number of time steps (not specified if time_step and final_time provided)
final_time=1., # simulation final time (not specified if time_step and time_step_nbr provided)
boundary_types="periodic", # boundary condition, string or tuple, length == len(cell) == len(dl)
cells=65, # integer or tuple length == dimension
dl=1./65, # mesh size of the root level, float or tuple
refinement_boxes = {"L0":{"B0":[(10,),(50,)]}},
diag_options = {"format":"phareh5", "options": {"dir": "phare_outputs","mode":"overwrite"}}
time_step_nbr=1000, # number of time steps (not specified if time_step and final_time provided)
final_time=1.0, # simulation final time (not specified if time_step and time_step_nbr provided)
boundary_types="periodic", # boundary condition, string or tuple, length == len(cell) == len(dl)
cells=65, # integer or tuple length == dimension
dl=1.0 / 65, # mesh size of the root level, float or tuple
refinement_boxes={"L0": {"B0": [(10,), (50,)]}},
diag_options={
"format": "phareh5",
"options": {"dir": "phare_outputs", "mode": "overwrite"},
},
)

density = lambda x: 2.
density = lambda x: 2.0

bx, by, bz = (lambda x: 1 for i in range(3))
ex, ey, ez = (lambda x: 1 for i in range(3))
vx, vy, vz = (lambda x: 1. for i in range(3))
bx, by, bz = (lambda x: 1 for i in range(3))
ex, ey, ez = (lambda x: 1 for i in range(3))
vx, vy, vz = (lambda x: 1.0 for i in range(3))

vthx, vthy, vthz = (lambda x: 1. for i in range(3))
vthx, vthy, vthz = (lambda x: 1.0 for i in range(3))

vvv = {
"vbulkx":vx, "vbulky":vy, "vbulkz":vz,
"vthx":vthx, "vthy":vthy, "vthz":vthz
"vbulkx": vx,
"vbulky": vy,
"vbulkz": vz,
"vthx": vthx,
"vthy": vthy,
"vthz": vthz,
}

MaxwellianFluidModel(
bx=bx, by=by, bz=bz,
protons={"charge":1, "density":density, **vvv, "init":{"seed":1337}},
alpha={"charge":1, "density":density, **vvv, "init":{"seed":2}},
bx=bx,
by=by,
bz=bz,
protons={"charge": 1, "density": density, **vvv, "init": {"seed": 1337}},
alpha={"charge": 1, "density": density, **vvv, "init": {"seed": 2}},
)

ElectronModel(closure="isothermal",Te = 0.12)


ElectronModel(closure="isothermal", Te=0.12)
8 changes: 4 additions & 4 deletions tests/simulator/advance/test_fields_advance_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box1D, nDBox
from tests.simulator.test_advance import AdvanceTestBase

import matplotlib
from ddt import data, ddt, unpack
from pyphare.core.box import Box1D

from tests.simulator.test_advance import AdvanceTestBase

matplotlib.use("Agg") # for systems without GUI

Expand Down Expand Up @@ -83,4 +84,3 @@ def test_field_level_ghosts_via_subcycles_and_coarser_interpolation(self, interp

if __name__ == "__main__":
unittest.main()

8 changes: 4 additions & 4 deletions tests/simulator/advance/test_fields_advance_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box2D, nDBox
from tests.simulator.test_advance import AdvanceTestBase

import matplotlib
from ddt import data, ddt, unpack
from pyphare.core.box import Box2D

from tests.simulator.test_advance import AdvanceTestBase

matplotlib.use("Agg") # for systems without GUI

Expand Down Expand Up @@ -85,4 +86,3 @@ def test_field_level_ghosts_via_subcycles_and_coarser_interpolation(self, interp

if __name__ == "__main__":
unittest.main()

8 changes: 4 additions & 4 deletions tests/simulator/advance/test_particles_advance_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box1D, nDBox
from tests.simulator.test_advance import AdvanceTestBase

import matplotlib
from ddt import data, ddt, unpack
from pyphare.core.box import Box1D

from tests.simulator.test_advance import AdvanceTestBase

matplotlib.use("Agg") # for systems without GUI

Expand Down Expand Up @@ -52,4 +53,3 @@ def test_domain_particles_on_refined_level(self, interp_order, refinement_boxes)

if __name__ == "__main__":
unittest.main()

8 changes: 4 additions & 4 deletions tests/simulator/advance/test_particles_advance_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box2D, nDBox
from tests.simulator.test_advance import AdvanceTestBase

import matplotlib
from ddt import data, ddt, unpack
from pyphare.core.box import Box2D

from tests.simulator.test_advance import AdvanceTestBase

matplotlib.use("Agg") # for systems without GUI

Expand Down Expand Up @@ -51,4 +52,3 @@ def test_domain_particles_on_refined_level(self, interp_order, refinement_boxes)

if __name__ == "__main__":
unittest.main()

6 changes: 3 additions & 3 deletions tests/simulator/initialize/test_fields_init_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box1D, nDBox
from tests.simulator.test_initialization import InitializationTest

import matplotlib
from ddt import data, ddt

from tests.simulator.test_initialization import InitializationTest

matplotlib.use("Agg") # for systems without GUI

Expand Down
6 changes: 3 additions & 3 deletions tests/simulator/initialize/test_fields_init_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box2D, nDBox
from tests.simulator.test_initialization import InitializationTest

import matplotlib
from ddt import data, ddt

from tests.simulator.test_initialization import InitializationTest

matplotlib.use("Agg") # for systems without GUI

Expand Down
13 changes: 7 additions & 6 deletions tests/simulator/initialize/test_particles_init_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box1D, nDBox
from tests.simulator.test_initialization import InitializationTest

import matplotlib

from ddt import data, ddt, unpack
from pyphare.core.box import Box1D
from pyphare.cpp import cpp_lib

from tests.simulator.test_initialization import InitializationTest

cpp = cpp_lib()

matplotlib.use("Agg") # for systems without GUI
Expand Down Expand Up @@ -82,7 +83,6 @@ def test_no_patch_ghost_on_refined_level_case(self, simInput):
def test_has_patch_ghost_on_refined_level_case(self, simInput):
print(f"{self._testMethodName}_{ndim}d")
from pyphare.pharein.simulation import check_patch_size
diag_outputs=f"phare_overlaped_fields_are_equal_with_min_max_patch_size_of_max_ghosts_{ndim}_{self.ddt_test_id()}"
_, smallest_patch_size = check_patch_size(ndim, **simInput)
simInput["smallest_patch_size"] = smallest_patch_size
simInput["largest_patch_size"] = smallest_patch_size
Expand All @@ -95,7 +95,8 @@ def test_amr_clustering(self, clustering):
interp_order = 1
test_id = self.ddt_test_id()
local_out = f"test_amr_clustering/mpi/{cpp.mpi_size()}/{test_id}"
datahier = self.getHierarchy(interp_order, {"L0": {"B0": [(10, ), (20, )]}}, "particles", clustering=clustering, diag_outputs=local_out)
self.getHierarchy(interp_order, {"L0": {"B0": [(10, ), (20, )]}}, "particles",
clustering=clustering, diag_outputs=local_out)



Expand Down
8 changes: 4 additions & 4 deletions tests/simulator/initialize/test_particles_init_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

import unittest
from ddt import ddt, data, unpack
from pyphare.core.box import Box, Box2D, nDBox
from tests.simulator.test_initialization import InitializationTest

import matplotlib
from ddt import data, ddt, unpack
from pyphare.core.box import Box2D

from tests.simulator.test_initialization import InitializationTest

matplotlib.use("Agg") # for systems without GUI

Expand Down Expand Up @@ -81,7 +82,6 @@ def test_no_patch_ghost_on_refined_level_case(self, simInput):
def test_has_patch_ghost_on_refined_level_case(self, simInput):
print(f"\n{self._testMethodName}_{ndim}d")
from pyphare.pharein.simulation import check_patch_size
diag_outputs=f"phare_overlaped_fields_are_equal_with_min_max_patch_size_of_max_ghosts_{ndim}_{self.ddt_test_id()}"
_, smallest_patch_size = check_patch_size(ndim, **simInput)
simInput["smallest_patch_size"] = smallest_patch_size
simInput["largest_patch_size"] = smallest_patch_size
Expand Down
13 changes: 8 additions & 5 deletions tests/simulator/refined_particle_nbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
# formatted with black

from pyphare.cpp import cpp_lib

cpp = cpp_lib()

import os, sys, unittest, yaml
import os
import sys
import unittest

import numpy as np
import pyphare.pharein as ph
from pyphare.simulator.simulator import Simulator
from tests.simulator import NoOverwriteDict
from tests.simulator import populate_simulation
import yaml
from pyphare.cpp import splitter_type
from pyphare.simulator.simulator import Simulator

from tests.simulator import NoOverwriteDict, populate_simulation
from tests.simulator.config import project_root


Expand Down
27 changes: 10 additions & 17 deletions tests/simulator/refinement/test_2d_10_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
"""


import pyphare.pharein as ph #lgtm [py/import-and-import-from]
from pyphare.pharein import Simulation
from pyphare.pharein import MaxwellianFluidModel
from pyphare.pharein import ElectromagDiagnostics,FluidDiagnostics, ParticleDiagnostics
from pyphare.pharein import ElectronModel
from pyphare.core.box import Box
import matplotlib as mpl
import numpy as np
import pyphare.core.box as boxm
import pyphare.pharein as ph # lgtm [py/import-and-import-from]
from pyphare.pharein import (ElectromagDiagnostics, ElectronModel,
FluidDiagnostics, MaxwellianFluidModel,
Simulation)
from pyphare.simulator.simulator import Simulator, startMPI
from pyphare.pharein import global_vars as gv

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Agg')


Expand All @@ -46,17 +42,12 @@ def config(diag_outputs, model_init={}, refinement_boxes=None):
"mode":"overwrite", "fine_dump_lvl_max": 10}}
)
def density(x, y):
from pyphare.pharein.global_vars import sim
Lx = sim.simulation_domain()[0]
return 1.

def bx(x, y):
return 0.1

def by(x, y):
from pyphare.pharein.global_vars import sim
Lx = sim.simulation_domain()[0]
Ly = sim.simulation_domain()[1]
return 0.2

def bz(x, y):
Expand Down Expand Up @@ -128,8 +119,10 @@ def get_time(path, time=None, datahier = None):
def get_hier(path):
return get_time(path)

from tests.simulator.test_advance import AdvanceTestBase
from pyphare.cpp import cpp_lib

from tests.simulator.test_advance import AdvanceTestBase

cpp = cpp_lib()
test = AdvanceTestBase(rethrow=True) # change to False for debugging images
L0_diags = "phare_outputs/test_x_homo_0"
Expand Down Expand Up @@ -177,7 +170,7 @@ def post_advance(new_time):
def main():
import random
startMPI()
rando = random.randint(0, 1e10)
rando = random.randint(0, int(1e10))
Simulator(config(L0_diags, {"seed": rando})).run().reset()
refinement_boxes={"L0": {"B0": [( 7, 40), ( 20, 60)]}}
sim = config(L0L1_diags, {"seed": rando}, refinement_boxes)
Expand Down
21 changes: 9 additions & 12 deletions tests/simulator/refinement/test_2d_2_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
"""


import pyphare.pharein as ph #lgtm [py/import-and-import-from]
from pyphare.pharein import Simulation
from pyphare.pharein import MaxwellianFluidModel
from pyphare.pharein import ElectromagDiagnostics,FluidDiagnostics, ParticleDiagnostics
from pyphare.pharein import ElectronModel
from pyphare.core.box import Box
import matplotlib as mpl
import numpy as np
import pyphare.core.box as boxm
import pyphare.pharein as ph # lgtm [py/import-and-import-from]
from pyphare.pharein import (ElectromagDiagnostics, ElectronModel,
FluidDiagnostics, MaxwellianFluidModel,
Simulation)
from pyphare.simulator.simulator import Simulator, startMPI
from pyphare.pharein import global_vars as gv

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Agg')


Expand Down Expand Up @@ -143,8 +139,10 @@ def get_time(path, time=None, datahier = None):
def get_hier(path):
return get_time(path)

from tests.simulator.test_advance import AdvanceTestBase
from pyphare.cpp import cpp_lib

from tests.simulator.test_advance import AdvanceTestBase

cpp = cpp_lib()
test = AdvanceTestBase(rethrow=True) # change to False for debugging images
L0_diags = "phare_outputs/test_homo_0"
Expand Down Expand Up @@ -181,4 +179,3 @@ def main():

if __name__=="__main__":
main()

Loading

0 comments on commit 774fd61

Please sign in to comment.