-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
advanced simulation paramaterization
- Loading branch information
1 parent
b8c6324
commit fa26a0f
Showing
5 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# TODO: finish | ||
# historgrams? | ||
# | ||
|
||
import pyphare.pharein as ph | ||
from pyphare.simulator.simulator import Simulator, startMPI | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import matplotlib as mpl | ||
|
||
mpl.use("Agg") | ||
|
||
from pyphare.cpp import cpp_lib | ||
|
||
cpp = cpp_lib() | ||
startMPI() | ||
|
||
time_step_nbr = 1000 | ||
time_step = 0.001 | ||
smallest_patch_size = 10 | ||
largest_patch_size = 20 | ||
rebalance_coarsest = False | ||
diag_outputs = "phare_outputs/harris/2d/load_balancing" | ||
|
||
|
||
def config(): | ||
sim = ph.Simulation( | ||
smallest_patch_size=smallest_patch_size, | ||
largest_patch_size=largest_patch_size, | ||
time_step_nbr=time_step_nbr, | ||
time_step=time_step, | ||
cells=(100, 100), | ||
dl=(0.2, 0.2), | ||
refinement_boxes={}, | ||
hyper_resistivity=0.001, | ||
resistivity=0.001, | ||
diag_options={ | ||
"format": "phareh5", | ||
"options": {"dir": diag_outputs, "mode": "overwrite"}, | ||
}, | ||
advanced={"integrator/rebalance_coarsest": rebalance_coarsest}, | ||
) | ||
|
||
def density(x, y): | ||
L = sim.simulation_domain()[1] | ||
return ( | ||
0.2 | ||
+ 1.0 / np.cosh((y - L * 0.3) / 0.5) ** 2 | ||
+ 1.0 / np.cosh((y - L * 0.7) / 0.5) ** 2 | ||
) | ||
|
||
def S(y, y0, l): | ||
return 0.5 * (1.0 + np.tanh((y - y0) / l)) | ||
|
||
def by(x, y): | ||
Lx = sim.simulation_domain()[0] | ||
Ly = sim.simulation_domain()[1] | ||
w1 = 0.2 | ||
w2 = 1.0 | ||
x0 = x - 0.5 * Lx | ||
y1 = y - 0.3 * Ly | ||
y2 = y - 0.7 * Ly | ||
w3 = np.exp(-(x0 * x0 + y1 * y1) / (w2 * w2)) | ||
w4 = np.exp(-(x0 * x0 + y2 * y2) / (w2 * w2)) | ||
w5 = 2.0 * w1 / w2 | ||
return (w5 * x0 * w3) + (-w5 * x0 * w4) | ||
|
||
def bx(x, y): | ||
Lx = sim.simulation_domain()[0] | ||
Ly = sim.simulation_domain()[1] | ||
w1 = 0.2 | ||
w2 = 1.0 | ||
x0 = x - 0.5 * Lx | ||
y1 = y - 0.3 * Ly | ||
y2 = y - 0.7 * Ly | ||
w3 = np.exp(-(x0 * x0 + y1 * y1) / (w2 * w2)) | ||
w4 = np.exp(-(x0 * x0 + y2 * y2) / (w2 * w2)) | ||
w5 = 2.0 * w1 / w2 | ||
v1 = -1 | ||
v2 = 1.0 | ||
return ( | ||
v1 | ||
+ (v2 - v1) * (S(y, Ly * 0.3, 0.5) - S(y, Ly * 0.7, 0.5)) | ||
+ (-w5 * y1 * w3) | ||
+ (+w5 * y2 * w4) | ||
) | ||
|
||
def bz(x, y): | ||
return 0.0 | ||
|
||
def b2(x, y): | ||
return bx(x, y) ** 2 + by(x, y) ** 2 + bz(x, y) ** 2 | ||
|
||
def T(x, y): | ||
K = 1 | ||
temp = 1.0 / density(x, y) * (K - b2(x, y) * 0.5) | ||
assert np.all(temp > 0) | ||
return temp | ||
|
||
def vx(x, y): | ||
return 0.0 | ||
|
||
def vy(x, y): | ||
return 0.0 | ||
|
||
def vz(x, y): | ||
return 0.0 | ||
|
||
def vthx(x, y): | ||
return np.sqrt(T(x, y)) | ||
|
||
def vthy(x, y): | ||
return np.sqrt(T(x, y)) | ||
|
||
def vthz(x, y): | ||
return np.sqrt(T(x, y)) | ||
|
||
vvv = { | ||
"vbulkx": vx, | ||
"vbulky": vy, | ||
"vbulkz": vz, | ||
"vthx": vthx, | ||
"vthy": vthy, | ||
"vthz": vthz, | ||
"nbr_part_per_cell": 100, | ||
} | ||
|
||
ph.MaxwellianFluidModel( | ||
bx=bx, | ||
by=by, | ||
bz=bz, | ||
protons={"charge": 1, "density": density, **vvv, "init": {"seed": 12334}}, | ||
) | ||
ph.ElectronModel(closure="isothermal", Te=0.0) | ||
ph.ParticleDiagnostics( | ||
quantity="domain", | ||
write_timestamps=[0, sim.final_time], | ||
population_name="protons", | ||
) | ||
return sim | ||
|
||
|
||
def get_time(path, time, datahier=None): | ||
time = "{:.10f}".format(time) | ||
from pyphare.pharesee.hierarchy import hierarchy_from | ||
|
||
return hierarchy_from( | ||
h5_filename=path + "/ions_pop_protons_domain.h5", time=time, hier=datahier | ||
) | ||
|
||
|
||
def post_advance(new_time): | ||
if cpp.mpi_rank() == 0: | ||
print(f"running tests at time {new_time}") | ||
from tests.simulator.test_advance import AdvanceTestBase | ||
|
||
test = AdvanceTestBase() | ||
test.base_test_overlaped_fields_are_equal( | ||
get_time(diag_outputs, new_time), new_time | ||
) | ||
print(f"tests passed") | ||
|
||
|
||
def check_time(sim, time=0): | ||
print("check_time", time) | ||
hier = get_time(diag_outputs, time) | ||
assert len(hier.levels()) == 1 # L0 ONLY! | ||
for ilvl, lvl in hier.levels().items(): | ||
for patch in lvl: | ||
for pd_key, pd in patch.patch_datas.items(): | ||
print("len(pd.dataset)", pd.dataset.size()) | ||
|
||
|
||
def test_balance(sim): | ||
check_time(sim) | ||
check_time(sim, time_step_nbr * time_step) | ||
|
||
|
||
def main(): | ||
sim = Simulator(config()).run() | ||
test_balance(sim) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |