Skip to content

Commit

Permalink
Added a writeField API.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Jun 18, 2024
1 parent 33c0a16 commit bce10c3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pyofm/pyOFM.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,18 @@ def readField(self, fieldName, fieldType, timeName, field):
"""

self.ofMesh.readField(fieldName, fieldType, timeName, field)

def writeField(self, fieldName, fieldType, field):
"""
Write OpenFoam field based on the internal field values from an array
Inputs:
fieldName: name of the field to read
fieldType: can be either volScalarField or volVectorField
field: an np array to save values of the field
Output:
An OpenFOAM field variable written to the disk (usually in the 0 folder)
"""

self.ofMesh.writeField(fieldName, fieldType, field)
59 changes: 59 additions & 0 deletions src/OFMesh.H
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,65 @@ public:
<< " not supported! Options are volScalariField or volVectorField" << abort(FatalError);
}
}

void writeField(
const word fieldName,
const word fieldType,
const double* field)
{
if (fieldType == "volScalarField")
{
volScalarField state(
IOobject(
fieldName,
"0",
meshPtr_(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false),
meshPtr_(),
dimensionedScalar(fieldName, dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0),
"zeroGradient");

forAll(state, cellI)
{
state[cellI] = field[cellI];
}
state.correctBoundaryConditions();
state.write();
}
else if (fieldType == "volVectorField")
{
volVectorField state(
IOobject(
fieldName,
"0",
meshPtr_(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false),
meshPtr_(),
dimensionedVector(fieldName, dimensionSet(0, 0, 0, 0, 0, 0, 0), vector::zero),
"zeroGradient");

label counterI = 0;
forAll(state, cellI)
{
for (label i = 0; i < 3; i++)
{
state[cellI][i] = field[counterI];
counterI++;
}
}
state.correctBoundaryConditions();
state.write();
}
else
{
FatalErrorIn("readField") << "fieldType " << fieldType
<< " not supported! Options are volScalariField or volVectorField" << abort(FatalError);
}
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
13 changes: 13 additions & 0 deletions src/pyOFMesh.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cdef extern from "OFMesh.H" namespace "Foam":
int getLocalBoundaryFaceOwner(int,int)
int getLocalFaceNeighbour(int)
void readField(char* , char *, char *, double *)
void writeField(char* , char *, double *)

# create python wrappers that call cpp functions
cdef class pyOFMesh:
Expand Down Expand Up @@ -146,3 +147,15 @@ cdef class pyOFMesh:

cdef double *field_data = <double*>field.data
self._thisptr.readField(fieldName.encode(), fieldType.encode(), timeName.encode(), field_data)

def writeField(self, fieldName, fieldType, np.ndarray[double, ndim=1, mode="c"] field):
if fieldType == "volScalarField":
assert len(field) == self.getNLocalCells(), "invalid array size!"
elif fieldType == "volVectorField":
assert len(field) == self.getNLocalCells() * 3, "invalid array size!"
else:
print("fieldType invalid!")
exit(1)

cdef double *field_data = <double*>field.data
self._thisptr.writeField(fieldName.encode(), fieldType.encode(), field_data)

0 comments on commit bce10c3

Please sign in to comment.