Skip to content

Commit bce10c3

Browse files
committed
Added a writeField API.
1 parent 33c0a16 commit bce10c3

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

pyofm/pyOFM.py

+15
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,18 @@ def readField(self, fieldName, fieldType, timeName, field):
320320
"""
321321

322322
self.ofMesh.readField(fieldName, fieldType, timeName, field)
323+
324+
def writeField(self, fieldName, fieldType, field):
325+
"""
326+
Write OpenFoam field based on the internal field values from an array
327+
328+
Inputs:
329+
fieldName: name of the field to read
330+
fieldType: can be either volScalarField or volVectorField
331+
field: an np array to save values of the field
332+
Output:
333+
An OpenFOAM field variable written to the disk (usually in the 0 folder)
334+
335+
"""
336+
337+
self.ofMesh.writeField(fieldName, fieldType, field)

src/OFMesh.H

+59
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,65 @@ public:
223223
<< " not supported! Options are volScalariField or volVectorField" << abort(FatalError);
224224
}
225225
}
226+
227+
void writeField(
228+
const word fieldName,
229+
const word fieldType,
230+
const double* field)
231+
{
232+
if (fieldType == "volScalarField")
233+
{
234+
volScalarField state(
235+
IOobject(
236+
fieldName,
237+
"0",
238+
meshPtr_(),
239+
IOobject::NO_READ,
240+
IOobject::NO_WRITE,
241+
false),
242+
meshPtr_(),
243+
dimensionedScalar(fieldName, dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0),
244+
"zeroGradient");
245+
246+
forAll(state, cellI)
247+
{
248+
state[cellI] = field[cellI];
249+
}
250+
state.correctBoundaryConditions();
251+
state.write();
252+
}
253+
else if (fieldType == "volVectorField")
254+
{
255+
volVectorField state(
256+
IOobject(
257+
fieldName,
258+
"0",
259+
meshPtr_(),
260+
IOobject::NO_READ,
261+
IOobject::NO_WRITE,
262+
false),
263+
meshPtr_(),
264+
dimensionedVector(fieldName, dimensionSet(0, 0, 0, 0, 0, 0, 0), vector::zero),
265+
"zeroGradient");
266+
267+
label counterI = 0;
268+
forAll(state, cellI)
269+
{
270+
for (label i = 0; i < 3; i++)
271+
{
272+
state[cellI][i] = field[counterI];
273+
counterI++;
274+
}
275+
}
276+
state.correctBoundaryConditions();
277+
state.write();
278+
}
279+
else
280+
{
281+
FatalErrorIn("readField") << "fieldType " << fieldType
282+
<< " not supported! Options are volScalariField or volVectorField" << abort(FatalError);
283+
}
284+
}
226285
};
227286

228287
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

src/pyOFMesh.pyx

+13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ cdef extern from "OFMesh.H" namespace "Foam":
4242
int getLocalBoundaryFaceOwner(int,int)
4343
int getLocalFaceNeighbour(int)
4444
void readField(char* , char *, char *, double *)
45+
void writeField(char* , char *, double *)
4546

4647
# create python wrappers that call cpp functions
4748
cdef class pyOFMesh:
@@ -146,3 +147,15 @@ cdef class pyOFMesh:
146147

147148
cdef double *field_data = <double*>field.data
148149
self._thisptr.readField(fieldName.encode(), fieldType.encode(), timeName.encode(), field_data)
150+
151+
def writeField(self, fieldName, fieldType, np.ndarray[double, ndim=1, mode="c"] field):
152+
if fieldType == "volScalarField":
153+
assert len(field) == self.getNLocalCells(), "invalid array size!"
154+
elif fieldType == "volVectorField":
155+
assert len(field) == self.getNLocalCells() * 3, "invalid array size!"
156+
else:
157+
print("fieldType invalid!")
158+
exit(1)
159+
160+
cdef double *field_data = <double*>field.data
161+
self._thisptr.writeField(fieldName.encode(), fieldType.encode(), field_data)

0 commit comments

Comments
 (0)