|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import pyphare.pharein as ph #lgtm [py/import-and-import-from] |
| 4 | +from pyphare.pharein import Simulation |
| 5 | +from pyphare.pharein import MaxwellianFluidModel |
| 6 | +from pyphare.pharein import ElectromagDiagnostics, FluidDiagnostics |
| 7 | +from pyphare.pharein import ElectronModel |
| 8 | +from pyphare.simulator.simulator import Simulator |
| 9 | +from pyphare.pharein import global_vars as gv |
| 10 | + |
| 11 | + |
| 12 | +import matplotlib.pyplot as plt |
| 13 | +import matplotlib as mpl |
| 14 | +import numpy as np |
| 15 | +mpl.use('Agg') |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +def config(): |
| 21 | + """ Configure the simulation |
| 22 | +
|
| 23 | + This function defines the Simulation object, |
| 24 | + user initialization model and diagnostics. |
| 25 | + """ |
| 26 | + Simulation( |
| 27 | + smallest_patch_size=20, |
| 28 | + largest_patch_size=20, |
| 29 | + time_step_nbr=6000, # number of time steps (not specified if time_step and final_time provided) |
| 30 | + final_time=30, # simulation final time (not specified if time_step and time_step_nbr provided) |
| 31 | + boundary_types="periodic", # boundary condition, string or tuple, length == len(cell) == len(dl) |
| 32 | + cells=2500, # integer or tuple length == dimension |
| 33 | + dl=0.2, # mesh size of the root level, float or tuple |
| 34 | + #max_nbr_levels=1, # (default=1) max nbr of levels in the AMR hierarchy |
| 35 | + nesting_buffer=0, |
| 36 | + refinement_boxes = {"L0":{"B0":[(125,), (750,)]}}, |
| 37 | + diag_options={"format": "phareh5", "options": {"dir": "shock_20dx_dx02_refined","mode":"overwrite"}} |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | + def density(x): |
| 42 | + from pyphare.pharein.global_vars import sim |
| 43 | + L = sim.simulation_domain()[0] |
| 44 | + v1=1 |
| 45 | + v2=1. |
| 46 | + return v1 + (v2-v1)*(S(x,L*0.2,1) -S(x, L*0.8, 1)) |
| 47 | + |
| 48 | + |
| 49 | + def S(x,x0,l): |
| 50 | + return 0.5*(1+np.tanh((x-x0)/l)) |
| 51 | + |
| 52 | + |
| 53 | + def bx(x): |
| 54 | + return 0. |
| 55 | + |
| 56 | + |
| 57 | + def by(x): |
| 58 | + from pyphare.pharein.global_vars import sim |
| 59 | + L = sim.simulation_domain()[0] |
| 60 | + v1=0.125 |
| 61 | + v2=4.0 |
| 62 | + return v1 + (v2-v1)*(S(x , L * 0.2, 1) -S(x, L * 0.8, 1)) |
| 63 | + |
| 64 | + def bz(x): |
| 65 | + return 0. |
| 66 | + |
| 67 | + def T(x): |
| 68 | + return 0.1 |
| 69 | + |
| 70 | + |
| 71 | + def vx(x): |
| 72 | + from pyphare.pharein.global_vars import sim |
| 73 | + L = sim.simulation_domain()[0] |
| 74 | + v1 = 0. |
| 75 | + v2 = 0. |
| 76 | + return v1 + (v2-v1) * (S(x, L*0.25, 1) -S(x, L * 0.75, 1)) |
| 77 | + |
| 78 | + |
| 79 | + def vy(x): |
| 80 | + return 0. |
| 81 | + |
| 82 | + |
| 83 | + def vz(x): |
| 84 | + return 0. |
| 85 | + |
| 86 | + |
| 87 | + def vthx(x): |
| 88 | + return T(x) |
| 89 | + |
| 90 | + |
| 91 | + def vthy(x): |
| 92 | + return T(x) |
| 93 | + |
| 94 | + |
| 95 | + def vthz(x): |
| 96 | + return T(x) |
| 97 | + |
| 98 | + |
| 99 | + vvv = { |
| 100 | + "vbulkx": vx, "vbulky": vy, "vbulkz": vz, |
| 101 | + "vthx": vthx, "vthy": vthy, "vthz": vthz |
| 102 | + } |
| 103 | + |
| 104 | + MaxwellianFluidModel( |
| 105 | + bx=bx, by=by, bz=bz, |
| 106 | + protons={"charge": 1, "density": density, **vvv} |
| 107 | + ) |
| 108 | + |
| 109 | + ElectronModel(closure="isothermal", Te=0.12) |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + sim = ph.global_vars.sim |
| 114 | + |
| 115 | + timestamps = np.arange(0, sim.final_time +sim.time_step, sim.time_step) |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + for quantity in ["E", "B"]: |
| 120 | + ElectromagDiagnostics( |
| 121 | + quantity=quantity, |
| 122 | + write_timestamps=timestamps, |
| 123 | + compute_timestamps=timestamps, |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | + for quantity in ["density", "bulkVelocity"]: |
| 128 | + FluidDiagnostics( |
| 129 | + quantity=quantity, |
| 130 | + write_timestamps=timestamps, |
| 131 | + compute_timestamps=timestamps, |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +def plot(bhier): |
| 138 | + times = np.sort(np.asarray(list(bhier.time_hier.keys()))) |
| 139 | + |
| 140 | + components =("B_y", "B_z") |
| 141 | + ylims = ((0.0, 2.),(0.,1.0)) |
| 142 | + |
| 143 | + for component,ylim in zip(components,ylims): |
| 144 | + for it,t in enumerate(times): |
| 145 | + fig,ax = plt.subplots(figsize=(10,6)) |
| 146 | + for il,level in bhier.levels(t).items(): |
| 147 | + patches = level.patches |
| 148 | + if il == 0: |
| 149 | + marker="+" |
| 150 | + alpha=1 |
| 151 | + ls='-' |
| 152 | + else: |
| 153 | + marker='o' |
| 154 | + alpha=0.4 |
| 155 | + ls='none' |
| 156 | + |
| 157 | + for ip, patch in enumerate(patches): |
| 158 | + val = patch.patch_datas["EM_"+component].dataset[:] |
| 159 | + x_val = patch.patch_datas["EM_"+component].x |
| 160 | + label="${}$ level {} patch {}".format(component,il,ip) |
| 161 | + ax.plot(x_val, val, label=label, |
| 162 | + marker=marker, alpha=alpha, ls=ls) |
| 163 | + ax.set_ylim(ylim) |
| 164 | + |
| 165 | + ax.legend(ncol=4) |
| 166 | + ax.set_title("t = {:05.2f}".format(t)) |
| 167 | + fig.savefig("{}_{:04d}.png".format(component,it)) |
| 168 | + plt.close(fig) |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +def main(): |
| 173 | + config() |
| 174 | + simulator = Simulator(gv.sim) |
| 175 | + simulator.initialize() |
| 176 | + simulator.run() |
| 177 | + |
| 178 | + |
| 179 | + #if cpp.mpi_rank() == 0: |
| 180 | + # b = hierarchy_from(h5_filename="phare_outputs/EM_B.h5") |
| 181 | + # plot(b) |
| 182 | + |
| 183 | +if __name__=="__main__": |
| 184 | + main() |
0 commit comments