Skip to content

Commit fde2af2

Browse files
committed
E and B
1 parent 19aca86 commit fde2af2

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

pyphare/pyphare/pharesee/tovtk.py

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import h5py
66
import sys
77
import numpy as np
8+
import os
89

910

10-
def toFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz, gn=2):
11+
def BtoFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz, gn=2):
1112

1213
nbrPoints = npx * npy * npz
1314
b = np.zeros((nbrPoints, 3), dtype="f")
@@ -18,8 +19,7 @@ def toFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz, gn=2):
1819
bz = np.zeros((npx, npy, npz), dtype=np.float32)
1920

2021
# converts yee to pure primal
21-
# by averaging in the dual direction
22-
# first and last will be fucked
22+
# we average in the dual direction so we need one extract ghost data in that direction
2323
bx[:, :, 0] = (
2424
ph_bx[gn:-gn, gn - 1 : -gn + 1][:, 1:] + ph_bx[gn:-gn, gn - 1 : -gn + 1][:, :-1]
2525
) * 0.5
@@ -42,6 +42,40 @@ def toFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz, gn=2):
4242
return b
4343

4444

45+
def EtoFlatPrimal(ph_ex, ph_ey, ph_ez, npx, npy, npz, gn=2):
46+
47+
nbrPoints = npx * npy * npz
48+
e = np.zeros((nbrPoints, 3), dtype="f")
49+
50+
# pure primal arrays
51+
ex = np.zeros((npx, npy, npz), dtype=np.float32)
52+
ey = np.zeros((npx, npy, npz), dtype=np.float32)
53+
ez = np.zeros((npx, npy, npz), dtype=np.float32)
54+
55+
# converts yee to pure primal
56+
# we average in the dual direction so we need one extract ghost data in that direction
57+
ex[:, :, 0] = (
58+
ph_ex[gn - 1 : -gn + 1, gn:-gn][1:, :] + ph_ex[gn - 1 : -gn + 1, gn:-gn][:-1, :]
59+
) * 0.5
60+
61+
ey[:, :, 0] = (
62+
ph_ey[gn:-gn, gn - 1 : -gn + 1][:, 1:] + ph_ey[gn:-gn, gn - 1 : -gn + 1][:, :-1]
63+
) * 0.5
64+
65+
# ez already primal in 2D
66+
ez[:, :, 0] = ph_ez[gn:-gn, gn:-gn][:, :]
67+
68+
ex[:, :, 1] = ex[:, :, 0]
69+
ey[:, :, 1] = ey[:, :, 0]
70+
ez[:, :, 1] = ez[:, :, 0]
71+
72+
e[:, 0] = ex.flatten(order="F")
73+
e[:, 1] = ey.flatten(order="F")
74+
e[:, 2] = ez.flatten(order="F")
75+
76+
return e
77+
78+
4579
def boxFromPatch(patch):
4680
lower = patch.attrs["lower"]
4781
upper = patch.attrs["upper"]
@@ -60,7 +94,7 @@ def nbrNodes(box):
6094
def main():
6195

6296
path = sys.argv[1]
63-
phare_h5 = h5py.File(f"{path}/EM_B.h5", "r")
97+
phare_h5 = h5py.File(path, "r")
6498
times_str = list(phare_h5["t"].keys())
6599
times = np.asarray([float(time) for time in times_str])
66100
times.sort()
@@ -72,7 +106,28 @@ def main():
72106
if max_nbr_level < nbrLevels:
73107
max_nbr_level = nbrLevels
74108

75-
vtk = h5py.File(f"{path}/EM_B.vtkhdf", "w")
109+
numberOfTimes = times.size
110+
111+
phare_fn = os.path.basename(path).split(".")[0]
112+
data_directory = os.path.dirname(path)
113+
vtk_fn = f"{data_directory}/{phare_fn}.vtkhdf"
114+
115+
print("PHARE H5 to VTKHDF conversion")
116+
print("-----------------------------")
117+
print(f"Converting {phare_fn} to {vtk_fn} in {data_directory}")
118+
print(f"Max number of levels : {max_nbr_level}")
119+
print(f"Number of time steps : {numberOfTimes}")
120+
121+
if "_B" in phare_fn:
122+
print("Converting B fields")
123+
toFlatPrimal = BtoFlatPrimal
124+
elif "_E" in phare_fn:
125+
print("Converting E fields")
126+
toFlatPrimal = EtoFlatPrimal
127+
else:
128+
pass
129+
130+
vtk = h5py.File(vtk_fn, "w")
76131
root = vtk.create_group("VTKHDF", track_order=True)
77132
root.attrs["Version"] = (2, 2)
78133
type = b"OverlappingAMR"
@@ -86,14 +141,10 @@ def main():
86141
origin = [0, 0, 0]
87142
root.attrs.create("Origin", origin, dtype="f")
88143

89-
numberOfTimes = 3 # times.size
90-
91144
steps = root.create_group("Steps")
92145
steps.attrs.create("NSteps", data=numberOfTimes, dtype="i8")
93146
steps.create_dataset("Values", data=times[:numberOfTimes])
94147

95-
print(f"maxlevel:{max_nbr_level}")
96-
97148
for ilvl in range(max_nbr_level)[:]:
98149

99150
print(f"Processing level {ilvl}")
@@ -129,7 +180,6 @@ def main():
129180
time_str = f"{time:.10f}"
130181
phare_lvl_name = f"pl{ilvl}"
131182
dataOffsets += [current_size]
132-
print("amr_box_offset", amr_box_offset)
133183
AMRBoxOffsets += [amr_box_offset]
134184
# pass this time if this level does not exist
135185
if phare_lvl_name not in phare_h5["t"][time_str].keys():
@@ -150,9 +200,6 @@ def main():
150200
nbr_boxes += 1
151201
npx, npy, npz = nbrNodes(box)
152202
b = toFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz)
153-
# b = np.ones(12 * 14 * 2) # np.ones((12 * 14, 3), dtype="f")
154-
# print(f"b shape :{b.shape} npx:{npx} npy:{npy}")
155-
# print(f"box:{box}")
156203

157204
if first:
158205
# this is the first patch of the first time
@@ -166,8 +213,6 @@ def main():
166213
first = False
167214

168215
else:
169-
# print("else")
170-
# print(f"b shape :{b.shape[0]}")
171216
# dataset already created with shape (current_size,3)
172217
# we add b.shape[0] points (=npx*npy) to the first dim
173218
# hence need to resize the dataset.
@@ -176,7 +221,6 @@ def main():
176221
# pass
177222

178223
current_size += b.shape[0]
179-
# print(f"current_size:{current_size}")
180224

181225
# of of the patch loops at that time
182226

0 commit comments

Comments
 (0)