Skip to content

Commit 0ddf406

Browse files
In OpenFoamSimu, allow reading cell center if exist instead of reconstructing mesh (#73)
* Modify openfoamsimu to account for the new name file format in sedfoam * 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. * Add extract_profile function * Allow reading cell centers, if present in output directory, instead of reconstructing mesh
1 parent b0b1d8d commit 0ddf406

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

fluidfoam/openfoamsimu.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,54 @@ def __init__(self, path=None, simu=None, timeStep=None, structured=False,
6363
if self.directory.endswith('/') is False:
6464
self.directory += '/'
6565

66-
self.readmesh(structured=structured, precision=precision,
67-
order=order)
66+
self.readmesh(timeStep=timeStep, structured=structured,
67+
precision=precision, order=order)
68+
6869
self.readopenfoam(timeStep=timeStep, structured=structured,
6970
dataToLoad=dataToLoad, precision=precision,
7071
order=order)
7172

72-
def readmesh(self, structured=False, precision=10, order='F'):
73+
def readmesh(self, timeStep=None, structured=False, precision=10, order='F'):
74+
75+
if timeStep is None:
76+
dir_list = os.listdir(self.directory)
77+
time_list = []
7378

74-
X, Y, Z = readmesh(self.directory, structured=structured,
75-
precision=precision, order=order)
79+
for directory in dir_list:
80+
try:
81+
float(directory)
82+
time_list.append(directory)
83+
except:
84+
pass
85+
time_list.sort(key=float)
86+
timeStep = time_list[-1]
87+
88+
elif type(timeStep) is int:
89+
#timeStep should be in a str format
90+
timeStep = str(timeStep)
91+
92+
self.timeStep = timeStep
93+
94+
# Check if cell center position is written in the output directory
95+
try:
96+
field = OpenFoamFile(path=self.directory, time_name=self.timeStep,
97+
name='C', structured=False, precision=precision,
98+
order=order)
99+
values = field.values
100+
shape = (3, values.size // 3)
101+
values = np.reshape(values, shape, order=order)
102+
if structured and not field.uniform:
103+
try:
104+
values[0:3, :] = values[0:3, self.ind]
105+
shape = (3,) + tuple(self.shape)
106+
values = np.reshape(values, shape, order=order)
107+
except:
108+
print("Variable {} could not be loaded".format(var))
109+
self.variables.remove(var)
110+
X, Y, Z = values[0], values[1], values[2]
111+
except FileNotFoundError:
112+
X, Y, Z = readmesh(self.directory, structured=structured,
113+
precision=precision, order=order)
76114
self.x = X
77115
self.y = Y
78116
self.z = Z
@@ -125,6 +163,12 @@ def readopenfoam(self, timeStep=None, structured=False, dataToLoad=None,
125163
continue
126164
else:
127165
self.variables.append(fname)
166+
167+
#Remove C, Cx, Cy and Cz if present
168+
var_to_remove = ['C', 'Cx', 'Cy', 'Cz']
169+
for var in var_to_remove:
170+
if var in self.variables:
171+
self.variables.remove(var)
128172
else:
129173
self.variables = dataToLoad
130174

0 commit comments

Comments
 (0)