Skip to content

Commit

Permalink
imports/static sim cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Dec 12, 2023
1 parent de6bee3 commit b2ac941
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 249 deletions.
13 changes: 5 additions & 8 deletions src/phare/phare_init.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/usr/bin/env python3


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 ElectronModel
import numpy as np

import pyphare.pharein as ph

# configure the simulation

Simulation(
ph.Simulation(
smallest_patch_size=20,
largest_patch_size=20,
time_step_nbr=30000, # number of time steps (not specified if time_step and final_time provided)
Expand Down Expand Up @@ -85,14 +82,14 @@ def vthz(x):
"vthz": vthz,
}

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

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


sim = ph.global_vars.sim
Expand Down
13 changes: 5 additions & 8 deletions src/phare/phare_init_small.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/usr/bin/env python3


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 ElectronModel
import numpy as np

import pyphare.pharein as ph

# configure the simulation

Simulation(
ph.Simulation(
smallest_patch_size=20,
largest_patch_size=20,
time_step_nbr=300, # number of time steps (not specified if time_step and final_time provided)
Expand Down Expand Up @@ -88,14 +85,14 @@ def vthz(x):
"vthz": vthz,
}

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

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


sim = ph.global_vars.sim
Expand Down
35 changes: 12 additions & 23 deletions tests/functional/alfven_wave/alfven_wave1d.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#!/usr/bin/env python3

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
from pyphare.pharein import ElectronModel
import pyphare.pharein as ph
from pyphare.simulator.simulator import Simulator
from pyphare.pharein import global_vars as gv
from pyphare.pharesee.hierarchy import get_times_from_h5
from tests.diagnostic import all_timestamps
from pyphare.pharesee.run import Run
from pyphare.pharesee.hierarchy import flat_finest_field

from tests.diagnostic import all_timestamps

import matplotlib.pyplot as plt
import matplotlib as mpl
Expand All @@ -28,7 +23,7 @@
def config():
# configure the simulation

Simulation(
sim = ph.Simulation(
smallest_patch_size=50,
largest_patch_size=50,
time_step_nbr=100000, # number of time steps (not specified if time_step and final_time provided)
Expand All @@ -48,14 +43,10 @@ def density(x):
return 1.0

def by(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.01 * np.cos(2 * np.pi * x / L[0])

def bz(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.01 * np.sin(2 * np.pi * x / L[0])

Expand All @@ -66,14 +57,10 @@ def vx(x):
return 0.0

def vy(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.01 * np.cos(2 * np.pi * x / L[0])

def vz(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.01 * np.sin(2 * np.pi * x / L[0])

Expand All @@ -95,28 +82,30 @@ def vthz(x):
"vthz": vthz,
}

MaxwellianFluidModel(
ph.MaxwellianFluidModel(
bx=bx, by=by, bz=bz, protons={"charge": 1, "density": density, **vvv}
)

ElectronModel(closure="isothermal", Te=0.0)
ph.ElectronModel(closure="isothermal", Te=0.0)

timestamps = all_timestamps(gv.sim)
timestamps = all_timestamps(sim)

for quantity in ["E", "B"]:
ElectromagDiagnostics(
ph.ElectromagDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
)

for quantity in ["density", "bulkVelocity"]:
FluidDiagnostics(
ph.FluidDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
)

return sim


####################################################################
# post processing
Expand Down Expand Up @@ -158,8 +147,8 @@ def main():
from pyphare.pharesee.run import Run
from pyphare.pharesee.hierarchy import flat_finest_field

config()
Simulator(gv.sim).run()
sim = config()
Simulator(sim).run()

if cpp.mpi_rank() == 0:
vphi, t, phi, a, k = phase_speed(".", 0.01, 1000)
Expand Down
35 changes: 12 additions & 23 deletions tests/functional/conservation/conserv.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
#!/usr/bin/env python3

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
import pyphare.pharein as ph
from pyphare.simulator.simulator import Simulator
from pyphare.pharein import global_vars as gv

from pyphare.pharesee.run import Run
from pyphare.pharesee.hierarchy import get_times_from_h5

import os
import numpy as np
import pyphare
from pyphare.pharesee.run import Run
from pyphare.pharesee.hierarchy import get_times_from_h5
from glob import glob


import matplotlib.pyplot as plt
import matplotlib as mpl

Expand All @@ -29,7 +21,7 @@
#
#############################################################
def uniform(vth, dl, cells, nbr_steps):
Simulation(
sim = ph.Simulation(
smallest_patch_size=20,
largest_patch_size=20,
time_step_nbr=nbr_steps,
Expand Down Expand Up @@ -82,31 +74,31 @@ def vthz(x):
"vthz": vthz,
}

MaxwellianFluidModel(
ph.MaxwellianFluidModel(
bx=bx, by=by, bz=bz, protons={"charge": 1, "density": density, **vvv}
)

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

sim = ph.global_vars.sim
ph.ElectronModel(closure="isothermal", Te=0.1)

timestamps = np.arange(0, sim.final_time, 50 * sim.time_step)

for quantity in ["B"]:
ElectromagDiagnostics(
ph.ElectromagDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
)

for name in ["domain", "levelGhost", "patchGhost"]:
ParticleDiagnostics(
ph.ParticleDiagnostics(
quantity=name,
compute_timestamps=timestamps,
write_timestamps=timestamps,
population_name="protons",
)

return sim


#############################################################
# POSTPROCESS ROUTINES
Expand Down Expand Up @@ -222,11 +214,8 @@ def main():

for vth in cases:
for dl, nbrcell, nbrdt in zip(dls, nbrcells, nbrdts):
uniform(vth, dl, nbrcell, nbrdt)
simulator = Simulator(gv.sim)
simulator.initialize()
simulator.run()
gv.sim = None
Simulator(uniform(vth, dl, nbrcell, nbrdt)).run()
ph.global_vars.sim = None

paths = glob("*vth*")
runs_vth = {}
Expand Down
44 changes: 15 additions & 29 deletions tests/functional/dispersion/dispersion.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#!/usr/bin/env python3

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
from pyphare.pharein import ElectronModel
import pyphare.pharein as ph
from pyphare.simulator.simulator import Simulator
from pyphare.pharein import global_vars as gv


import matplotlib.pyplot as plt
Expand All @@ -21,7 +16,7 @@ def fromNoise():
# and only eigen modes existing in the simulation noise
# will be visible

Simulation(
sim = ph.Simulation(
smallest_patch_size=20,
largest_patch_size=20,
# the following time step number
Expand Down Expand Up @@ -80,30 +75,32 @@ def vthz(x):
"vthz": vthz,
}

MaxwellianFluidModel(
ph.MaxwellianFluidModel(
bx=bx, by=by, bz=bz, protons={"charge": 1, "density": density, **vvv}
)

ElectronModel(closure="isothermal", Te=0.0)
ph.ElectronModel(closure="isothermal", Te=0.0)

sim = ph.global_vars.sim

timestamps = np.arange(0, sim.final_time + sim.time_step, sim.time_step)

for quantity in ["E", "B"]:
ElectromagDiagnostics(
ph.ElectromagDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
)

return sim


def prescribedModes():
# in this configuration there user prescribed
# wavelength, at which more energy is thus expected
# than in modes naturally present in the simulation noise

Simulation(
sim = ph.Simulation(
smallest_patch_size=20,
largest_patch_size=20,
# the following time step number
Expand All @@ -127,14 +124,10 @@ def density(x):
return 1.0

def by(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.1 * np.cos(2 * np.pi * x / L[0])

def bz(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return -0.1 * np.sin(2 * np.pi * x / L[0])

Expand All @@ -145,14 +138,10 @@ def vx(x):
return 0.0

def vy(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.1 * np.cos(2 * np.pi * x / L[0])

def vz(x):
from pyphare.pharein.global_vars import sim

L = sim.simulation_domain()
return 0.1 * np.sin(2 * np.pi * x / L[0])

Expand All @@ -174,36 +163,33 @@ def vthz(x):
"vthz": vthz,
}

MaxwellianFluidModel(
ph.MaxwellianFluidModel(
bx=bx, by=by, bz=bz, protons={"charge": 1, "density": density, **vvv}
)

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

sim = ph.global_vars.sim
ph.ElectronModel(closure="isothermal", Te=0.0)

timestamps = np.arange(0, sim.final_time + sim.time_step, sim.time_step)

for quantity in ["E", "B"]:
ElectromagDiagnostics(
ph.ElectromagDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
)

for quantity in ["density", "bulkVelocity"]:
FluidDiagnostics(
ph.FluidDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
)

return sim


def main():
fromNoise()
simulator = Simulator(gv.sim)
simulator.initialize()
simulator.run()
Simulator(fromNoise()).run()


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit b2ac941

Please sign in to comment.