Skip to content

Commit 3764667

Browse files
Some features in OpenFoamSimu class (#62)
* Modify openfoamsimu to account for the new name file format in sedfoam * Remove the automatic use of find_directory function * Modify path and file definition in openfoamsimu class * Add possibility to load local directory simulation in OpenFoamSimu * In openfoamsimu, add dataToLoad option to load only desired variables. --------- Authored-by: Remi.Chassagne <remi.chassagne@legi.grenoble-inp.fr>
1 parent 42aa07f commit 3764667

File tree

2 files changed

+111
-30
lines changed

2 files changed

+111
-30
lines changed

fluidfoam/openfoamsimu.py

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import os, sys
1313
import subprocess
14-
from fluidfoam import readmesh, readfield
14+
import numpy as np
15+
from fluidfoam import readmesh, readfield, OpenFoamFile
1516

1617
class Error(Exception):
1718
pass
@@ -34,21 +35,56 @@ class OpenFoamSimu(object):
3435
If simu=None, it will lists all existing simulation names in path
3536
and ask you to choose.\n
3637
timeStep: str, timeStep to load. If None, load the last time step\n
37-
structured: bool, true if the mesh is structured
38+
structured: bool, true if the mesh is structured\n
39+
dataToLoad: list of str, list containing the name of the varaibles
40+
to read and load. If None, read and load all saved variables.
3841
"""
3942

40-
def __init__(self, path, simu=None, timeStep=None, structured=False):
41-
42-
if simu == None:
43+
def __init__(self, path=None, simu=None, timeStep=None, structured=False,
44+
dataToLoad=None, precision=10, order='F'):
45+
46+
if path == None and simu == None:
47+
# If nothing if given, consider the current directory as the
48+
# simulation to load
49+
self.directory = os.getcwd()+'/'
50+
self.simu = os.getcwd().split('/')[-1]
51+
elif simu == None:
52+
# If only path is provided, consider all subfolders as possible
53+
# simulations to load
4354
self.directory = self._choose_simulation(path)
4455
self.simu = self.directory.split("/")[-2]
4556
else:
57+
# If path and simu are provided, consider the given directory
58+
# as the simulation to load
4659
self.simu = simu
47-
self.directory = self._find_directory(path, simu)
48-
49-
self.readopenfoam(timeStep=timeStep, structured=structured)
50-
51-
def readopenfoam(self, timeStep=None, structured=True):
60+
if path.endswith('/') is False:
61+
path += '/'
62+
self.directory = path + simu
63+
if self.directory.endswith('/') is False:
64+
self.directory += '/'
65+
66+
self.readmesh(structured=structured, precision=precision,
67+
order=order)
68+
self.readopenfoam(timeStep=timeStep, structured=structured,
69+
dataToLoad=dataToLoad, precision=precision,
70+
order=order)
71+
72+
def readmesh(self, structured=False, precision=10, order='F'):
73+
74+
X, Y, Z = readmesh(self.directory, structured=structured,
75+
precision=precision, order=order)
76+
self.x = X
77+
self.y = Y
78+
self.z = Z
79+
if structured:
80+
nx = np.unique(X).size
81+
ny = np.unique(Y).size
82+
nz = np.unique(Z).size
83+
self.ind = np.array(range(nx*ny*nz))
84+
self.shape = (nx, ny, nz)
85+
86+
def readopenfoam(self, timeStep=None, structured=False, dataToLoad=None,
87+
precision=10, order='F'):
5288
"""
5389
Reading SedFoam results
5490
Load the last time step saved of the simulation
@@ -57,6 +93,7 @@ def readopenfoam(self, timeStep=None, structured=True):
5793
timeStep : str or int, timeStep to load. If None, load the last time step\n
5894
structured : bool, true if the mesh is structured
5995
"""
96+
6097
if timeStep is None:
6198
dir_list = os.listdir(self.directory)
6299
time_list = []
@@ -76,29 +113,73 @@ def readopenfoam(self, timeStep=None, structured=True):
76113

77114
self.timeStep = timeStep
78115

79-
#Read Mesh
80-
X, Y, Z = readmesh(self.directory, structured=structured)
81-
self.x = X
82-
self.y = Y
83-
self.z = Z
84-
85116
#List all variables saved at the required time step removing potential
86117
#directory that cannot be loaded
87-
self.variables = []
88-
basepath = self.directory+self.timeStep+'/'
89-
for fname in os.listdir(basepath):
90-
path = os.path.join(basepath, fname)
91-
if os.path.isdir(path):
92-
# skip directories
93-
continue
94-
else:
95-
self.variables.append(fname)
96-
118+
if dataToLoad is None:
119+
self.variables = []
120+
basepath = self.directory+self.timeStep+'/'
121+
for fname in os.listdir(basepath):
122+
path = os.path.join(basepath, fname)
123+
if os.path.isdir(path):
124+
# skip directories
125+
continue
126+
else:
127+
self.variables.append(fname)
128+
else:
129+
self.variables = dataToLoad
97130

98131
for var in self.variables:
99132
#Load all variables and assign them as a variable of the object
100-
self.__setattr__(var, readfield(path=self.directory,
101-
time_name=self.timeStep, name=var, structured=structured))
133+
field = OpenFoamFile(path=self.directory, time_name=self.timeStep,
134+
name = var, structured=False, precision=precision,
135+
order=order)
136+
values = field.values
137+
138+
if field.type_data == "scalar":
139+
if structured and not field.uniform:
140+
try:
141+
values = np.reshape(values[self.ind], self.shape, order=order)
142+
except:
143+
print("Variable {} could not be loaded".format(var))
144+
self.variables.remove(var)
145+
continue
146+
elif field.type_data == "vector":
147+
shape = (3, values.size // 3)
148+
values = np.reshape(values, shape, order=order)
149+
if structured and not field.uniform:
150+
try:
151+
values[0:3, :] = values[0:3, self.ind]
152+
shape = (3,) + tuple(self.shape)
153+
values = np.reshape(values, shape, order=order)
154+
except:
155+
print("Variable {} could not be loaded".format(var))
156+
self.variables.remove(var)
157+
continue
158+
elif field.type_data == "symmtensor":
159+
shape = (6, values.size // 6)
160+
values = np.reshape(values, shape, order=order)
161+
if structured and not field.uniform:
162+
try:
163+
values[0:6, :] = values[0:6, self.ind]
164+
shape = (6,) + tuple(self.shape)
165+
values = np.reshape(values, shape, order=order)
166+
except:
167+
print("Variable {} could not be loaded".format(var))
168+
self.variables.remove(var)
169+
continue
170+
elif field.type_data == "tensor":
171+
shape = (9, values.size // 9)
172+
values = np.reshape(values, shape, order=order)
173+
if structured and not field.uniform:
174+
try:
175+
values[0:9, :] = values[0:9, self.ind]
176+
shape = (9,) + tuple(self.shape)
177+
values = np.reshape(values, shape, order=order)
178+
except:
179+
print("Variable {} could not be loaded".format(var))
180+
self.variables.remove(var)
181+
continue
182+
self.__setattr__(var.replace('.', '_'), values)
102183

103184
def keys(self):
104185
"""

fluidfoam/test_openfoamsimu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
class SimpleTestCase(unittest.TestCase):
1111
def test_read_simu(self):
12-
mySimu = OpenFoamSimu(path=case, simu=simu, timeStep=timeStep, structured=True)
12+
mySimu = OpenFoamSimu(path=case, simu=simu, timeStep=timeStep, structured=True, precision=12)
1313
keys = mySimu.keys()
14-
self.assertEqual(np.mean(mySimu.U), 0.03630165464270426)
14+
self.assertEqual(np.mean(mySimu.U), 0.036301654642704254)

0 commit comments

Comments
 (0)