Skip to content

Commit 24f511b

Browse files
committed
Added an API to read OF fields.
1 parent 2269975 commit 24f511b

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

pyofm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = '1.2.0'
1+
__version__ = '1.2.1'
22

33
from .pyOFM import PYOFM

pyofm/pyOFM.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,17 @@ def writeCompression(self):
306306
if "on" in cols[1]:
307307
writecompress = True
308308
return writecompress
309+
310+
def readField(self, fieldName, fieldType, timeName, field):
311+
"""
312+
Read OpenFoam field and return the internal field as an array
313+
314+
Inputs:
315+
fieldName: name of the field to read
316+
fieldType: can be either volScalarField or volVectorField
317+
timeName: the time folder name to read, e.g., "0" or "1000"
318+
Output:
319+
field: an np array to save the field
320+
"""
321+
322+
self.ofMesh.readField(fieldName, fieldType, timeName, field)

src/OFMesh.H

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ private:
3535
/// all the arguments
3636
char* argsAll_;
3737

38-
autoPtr<argList> argsPtr_;
39-
4038
autoPtr<fvMesh> meshPtr_;
4139

4240
autoPtr<Time> runTimePtr_;
4341

42+
autoPtr<argList> argsPtr_;
43+
4444
label nLocalPoints_;
4545

4646
label nLocalCells_;
@@ -171,6 +171,58 @@ public:
171171
const UList<label>& pFaceCells = meshPtr_().boundaryMesh()[patchI].faceCells();
172172
return pFaceCells[faceI];
173173
}
174+
175+
void readField(
176+
const word fieldName,
177+
const word fieldType,
178+
const word timeName,
179+
double* field)
180+
{
181+
if (fieldType == "volScalarField")
182+
{
183+
volScalarField state(
184+
IOobject(
185+
fieldName,
186+
timeName,
187+
meshPtr_(),
188+
IOobject::MUST_READ,
189+
IOobject::NO_WRITE,
190+
false),
191+
meshPtr_());
192+
193+
forAll(state, cellI)
194+
{
195+
field[cellI] = state[cellI];
196+
}
197+
}
198+
else if (fieldType == "volVectorField")
199+
{
200+
volVectorField state(
201+
IOobject(
202+
fieldName,
203+
timeName,
204+
meshPtr_(),
205+
IOobject::MUST_READ,
206+
IOobject::NO_WRITE,
207+
false),
208+
meshPtr_());
209+
210+
label counterI = 0;
211+
forAll(state, cellI)
212+
{
213+
for (label i = 0; i < 3; i++)
214+
{
215+
field[counterI] = state[cellI][i];
216+
counterI++;
217+
}
218+
}
219+
}
220+
else
221+
{
222+
FatalErrorIn("readField") << "fieldType " << fieldType
223+
<< " not supported! Options are volScalariField or volVectorField" << abort(FatalError);
224+
}
225+
}
174226
};
175227

176228
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

src/pyOFMesh.pyx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
'''
1616

17-
import numpy as np
17+
cimport numpy as np
1818

1919
from libcpp.string cimport string
2020

@@ -41,6 +41,7 @@ cdef extern from "OFMesh.H" namespace "Foam":
4141
int getLocalFaceOwner(int)
4242
int getLocalBoundaryFaceOwner(int,int)
4343
int getLocalFaceNeighbour(int)
44+
void readField(char* , char *, char *, double *)
4445

4546
# create python wrappers that call cpp functions
4647
cdef class pyOFMesh:
@@ -132,4 +133,16 @@ cdef class pyOFMesh:
132133
return self._thisptr.getLocalBoundaryFaceOwner(patchI,faceI)
133134

134135
def getLocalFaceNeighbour(self, faceI):
135-
return self._thisptr.getLocalFaceNeighbour(faceI)
136+
return self._thisptr.getLocalFaceNeighbour(faceI)
137+
138+
def readField(self, fieldName, fieldType, timeName, np.ndarray[double, ndim=1, mode="c"] field):
139+
if fieldType == "volScalarField":
140+
assert len(field) == self.getNLocalCells(), "invalid array size!"
141+
elif fieldType == "volVectorField":
142+
assert len(field) == self.getNLocalCells() * 3, "invalid array size!"
143+
else:
144+
print("fieldType invalid!")
145+
exit(1)
146+
147+
cdef double *field_data = <double*>field.data
148+
self._thisptr.readField(fieldName.encode(), fieldType.encode(), timeName.encode(), field_data)

src/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from distutils.extension import Extension
1616
from Cython.Build import cythonize
1717
import os
18+
import numpy
1819

1920
libName = "pyOFMesh"
2021

@@ -47,6 +48,7 @@
4748
os.getenv("FOAM_SRC") + "/codipack/include",
4849
os.getenv("FOAM_SRC") + "/medipack/include",
4950
os.getenv("FOAM_SRC") + "/medipack/src",
51+
numpy.get_include(),
5052
],
5153
# These are from Make/options:EXE_LIBS
5254
libraries=["meshTools", "finiteVolume"],
@@ -56,6 +58,7 @@
5658
extra_compile_args=[
5759
# "-DFULLDEBUG -g -O0", # this is for debugging
5860
"-std=c++11",
61+
"-Wno-deprecated-copy",
5962
"-m64",
6063
"-DOPENFOAM_PLUS=1812",
6164
"-Dlinux64",

0 commit comments

Comments
 (0)