Skip to content

Commit

Permalink
add new functionality (probes location read) in readpostpro (#42)
Browse files Browse the repository at this point in the history
* add read probes_loc

* pep8

* update example
  • Loading branch information
CyrilleBonamy committed Dec 6, 2022
1 parent 266f019 commit 546ccae
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 41 deletions.
4 changes: 2 additions & 2 deletions examples/plot_3_postprocprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
sol = '../output_samples/ascii/'

# import readprobes function from fluidfoam package
timeU, u = readprobes(sol, time_name = 'mergeTime', name = 'U')
timeP, p = readprobes(sol, time_name = 'mergeTime', name = 'p')
probes_locU, timeU, u = readprobes(sol, time_name = 'mergeTime', name = 'U')
probes_locP, timeP, p = readprobes(sol, time_name = 'mergeTime', name = 'p')

###############################################################################
# Now plots the pressure and y velocity for the first probe
Expand Down
74 changes: 43 additions & 31 deletions fluidfoam/readof.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@


def _make_path(path, time_name=None, name=None):
if time_name != None and name == None: # pragma: no cover
if time_name is not None and name is None: # pragma: no cover
path = os.path.join(path, time_name)
elif name != None and time_name == None: # pragma: no cover
elif name is not None and time_name is None: # pragma: no cover
path = os.path.join(path, name)
elif name != None and time_name != None:
elif name is not None and time_name is not None:
path = os.path.join(path, time_name, name)
if not os.path.exists(path) and os.path.exists(path + ".gz"):
path += ".gz"
Expand Down Expand Up @@ -117,8 +117,10 @@ def __init__(

self.boundary = self._parse_session(b"boundaryField")

if name == None:
self._parse_data(boundary=boundary, precision=precision, datatype=datatype)
if name is None:
self._parse_data(boundary=boundary,
precision=precision,
datatype=datatype)
elif name.endswith("boundary"):
self.boundaryface = self._parse_boundaryfile()
elif name.endswith("faces"):
Expand All @@ -128,9 +130,13 @@ def __init__(
elif name.endswith("owner") or name.endswith("neighbour"):
self._parse_owner()
else:
self._parse_data(boundary=boundary, precision=precision, datatype=datatype)
self._parse_data(boundary=boundary,
precision=precision,
datatype=datatype)
if structured:
self._determine_order(boundary=boundary, order=order, precision=precision)
self._determine_order(boundary=boundary,
order=order,
precision=precision)

def _parse_boundaryfile(self):

Expand All @@ -143,7 +149,7 @@ def _parse_boundaryfile(self):
in_section = True
level += 1
continue
if in_section == True:
if in_section is True:
if line == b"{":
level += 1
continue
Expand Down Expand Up @@ -172,7 +178,7 @@ def _parse_session(self, title):
if line == title:
in_section = True
continue
if in_section == True:
if in_section is True:
if line == b"{":
level += 1
continue
Expand All @@ -195,7 +201,7 @@ def _parse_session(self, title):

def _parse_data(self, boundary, datatype, precision=15):

if boundary != None:
if boundary is not None:
boun = str.encode(boundary)
if b"value" in self.content.split(boun)[1].split(b"}")[0]:
data = self.content.split(boun)[1].split(b"value")[1]
Expand Down Expand Up @@ -248,7 +254,8 @@ def _parse_data(self, boundary, datatype, precision=15):
else:
data = words[1].split(b";")[0]
if self.verbose:
print(R+"Warning : uniform field of type " + self.type_data + "!\n")
print(R+"Warning : uniform field of type "
+ self.type_data + "!\n")
print("Only constant field in output\n"+W)
elif shortline.count(b";") >= 1:
nb_pts = int(shortline.split(b"(")[0])
Expand All @@ -258,7 +265,8 @@ def _parse_data(self, boundary, datatype, precision=15):
elif self.codestream:
nb_pts = 0
if self.verbose:
print(R+"Warning : codeStream field! I can not read the source code!\n"+W)
print(R+"Warning : codeStream field! "
+ "I can not read the source code!\n"+W)
else:
nb_pts = int(lines[1])
data = b"\n(".join(data.split(b"\n(")[1:])
Expand Down Expand Up @@ -343,7 +351,8 @@ def _nearest_data(self, boundary, precision):
else:
data = words[1].split(b";")[0]
if self.verbose:
print(R+"Warning : uniform field of type " + self.type_data + "!\n")
print(R+"Warning : uniform field of type "
+ self.type_data + "!\n")
print("Only constant field in output\n"+W)
elif shortline.count(b";") >= 1:
nb_pts = int(shortline.split(b"(")[0])
Expand All @@ -353,7 +362,8 @@ def _nearest_data(self, boundary, precision):
elif self.codestream:
nb_pts = 0
if self.verbose:
print(R+"Warning : codeStream field! I can not read the source code!\n"+W)
print(R+"Warning : codeStream field! "
+ "I can not read the source code!\n"+W)
else:
nb_pts = int(lines[1])
data = b"\n(".join(data.split(b"\n(")[1:])
Expand Down Expand Up @@ -459,9 +469,8 @@ def _parse_face(self):
else:
self.faces[i - 1] = {}
self.faces[i - 1]["npts"] = int(line.split(b"(")[0])
self.faces[i - 1]["id_pts"] = [
int(s) for s in ((line.split(b"(")[1].split(b")")[0]).split())
]
self.faces[i - 1]["id_pts"] = [int(s) for s in
(line.split(b"(")[1].split(b")")[0]).split()]

def _parse_points(self, precision):

Expand Down Expand Up @@ -933,7 +942,8 @@ def readmesh(
"""

# hack because in dynamic Mesh cases, no polyMesh directory for initial time
# hack
# because in dynamic Mesh cases, no polyMesh directory for initial time
if time_name == "0":
time_name = None
if not os.path.exists(os.path.join(path, "constant/polyMesh")):
Expand All @@ -943,15 +953,15 @@ def readmesh(
" Please verify the directory of your case.",
)

if boundary != None:
if boundary is not None:
facefile = OpenFoamFile(
path + "/constant/polyMesh/", name="faces", verbose=verbose
)
pointfile = OpenFoamFile(
path + "/constant/polyMesh/",
name="faces",
verbose=verbose)
if time_name != None:
if time_name is not None:
pointfile = OpenFoamFile(
path=path,
time_name=time_name,
Expand Down Expand Up @@ -990,19 +1000,22 @@ def readmesh(
path + "/constant/polyMesh/", name="owner", verbose=verbose
)
nmesh = owner.nb_cell
if time_name == None and os.path.exists(os.path.join(path, "constant/C")):
if time_name is None and os.path.exists(os.path.join(path,
"constant/C")):
xs, ys, zs = readvector(
path, "constant", "C", precision=precision, verbose=verbose
)
elif time_name != None and os.path.exists(_make_path(path, time_name, "C")):
elif time_name is not None and os.path.exists(_make_path(path,
time_name,
"C")):
xs, ys, zs = readvector(
path, time_name, "C", precision=precision, verbose=verbose
)
else:
facefile = OpenFoamFile(
path + "/constant/polyMesh/", name="faces", verbose=verbose
)
if time_name != None:
if time_name is not None:
pointfile = OpenFoamFile(
path=path,
time_name=time_name,
Expand Down Expand Up @@ -1065,18 +1078,17 @@ def readmesh(

if __name__ == "__main__":

dirs = ["0.ascii", "0.asciigz", "0.bin", "0.bingz"]
dirs = ["ascii", "asciigz", "bin", "bingz"]

for d in dirs:
rep = os.path.join(os.path.dirname(__file__), "../output_samples")
rep = os.path.join(os.path.dirname(__file__), "../output_samples/", d)

values = readscalar(rep, d, "alpha")
values = readscalar(rep, "0", "alpha")

values = readsymmtensor(rep, d, "sigma")
values = readsymmtensor(rep, "0", "sigma")

values = readtensor(rep, d, "Taus")
values = readtensor(rep, "0", "Taus")

values = readvector(rep, d, "U")
values = readvector(rep, "0", "U")

path = os.path.join(rep, d)
xs, ys, zs = readmesh(path)
xs, ys, zs = readmesh(rep)
27 changes: 23 additions & 4 deletions fluidfoam/readpostpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def readforce(path, namepatch="forces", time_name="0", name="forces"):
for dummy, line in enumerate(data[:-1]):
if "#".encode() in line:
j += 1
elif "#".encode() not in line and header == True:
elif "#".encode() not in line and header is True:
header = False
line = line.replace(b"(", b"")
line = line.replace(b")", b"")
Expand Down Expand Up @@ -118,6 +118,7 @@ def readprobes(path, probes_name="probes", time_name="0", name="U"):
time_vect = None
tab = None
jj = 0
probes_loc = None

path_probes_name = glob(f'{path}/**/'+probes_name, recursive=True)[0]
if time_name is "latestTime":
Expand All @@ -134,7 +135,8 @@ def readprobes(path, probes_name="probes", time_name="0", name="U"):
time_list.sort(key=float)
time_list = np.array(time_list)
for timename in time_list:
time_vect, tab = readprobes(path, probes_name, timename, name)
probes_loc, time_vect, tab = readprobes(
path, probes_name, timename, name)
if "tab_merge" in locals():
for jj in range(np.size(time_vect[:])):
if time_vect[jj] > timevect_merge[-1]:
Expand All @@ -149,14 +151,26 @@ def readprobes(path, probes_name="probes", time_name="0", name="U"):
else:
timevect_merge = time_vect
tab_merge = tab
return timevect_merge, tab_merge
return probes_loc, timevect_merge, tab_merge

with open(os.path.join(path_probes_name, time_name, name), "rb") as f:
content = f.readlines()
j = 0
for dummy, line in enumerate(content):
if "#".encode() in line:
j += 1
elif "#".encode() not in line:
break
n_probes = j-2
probes_loc = np.zeros([n_probes, 3], dtype=float)
j = 0
header = True
for dummy, line in enumerate(content):
if "#".encode() in line:
if j<n_probes:
for k in range(3):
probes_loc[j, k] = (line.split(
b"(")[1].split(b")")[0].split()[k])
j += 1
elif "#".encode() not in line and header:
header = False
Expand Down Expand Up @@ -204,5 +218,10 @@ def readprobes(path, probes_name="probes", time_name="0", name="U"):
time_vect = np.array([])
if tab is None:
tab = np.array([])
return probes_loc, time_vect, tab


if __name__ == "__main__":

return time_vect, tab
rep = "../output_samples/ascii"
probes_loc, time_vect, dummy = readprobes(rep, time_name="latestTime")
8 changes: 4 additions & 4 deletions fluidfoam/test_readpostpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def test_read_forces(self):
print("create force object")

def test_read_probes(self):
time_vect, dummy = fluidfoam.readprobes(case)
probes_loc, time_vect, dummy = fluidfoam.readprobes(case)
self.assertEqual(10, len(time_vect))
time_vect, dummy = fluidfoam.readprobes(case, time_name="latestTime")
probes_loc, time_vect, dummy = fluidfoam.readprobes(case, time_name="latestTime")
self.assertEqual(6, len(time_vect))
time_vect, dummy = fluidfoam.readprobes(case, time_name="mergeTime")
probes_loc, time_vect, dummy = fluidfoam.readprobes(case, time_name="mergeTime")
self.assertEqual(13, len(time_vect))
time_vect, dummy = fluidfoam.readprobes(case, time_name="0", name="p")
probes_loc, time_vect, dummy = fluidfoam.readprobes(case, time_name="0", name="p")

0 comments on commit 546ccae

Please sign in to comment.