Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more options for variance and enabled ListList for DAOption #535

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
373 changes: 287 additions & 86 deletions src/adjoint/DAObjFunc/DAObjFuncVariance.C

Large diffs are not rendered by default.

31 changes: 21 additions & 10 deletions src/adjoint/DAObjFunc/DAObjFuncVariance.H
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,42 @@ class DAObjFuncVariance
{

protected:

/// name of the variable
word varName_;

/// data mode options are probePoint surface or field
word mode_;

/// type of the variable either vector or scalar
word varType_;

/// component of the vector variable
List<label> components_;

/// cell index of the probe points
labelList refCellIndex_;
labelList probeCellIndex_;

/// coordinates of the probe points
List<List<scalar>> probePointCoords_;

/// names of the surfaces for ref data
List<word> surfaceNames_;

/// component for vector at the above probe points cellI
labelList refCellComp_;

/// probe value
List<List<scalar>> refValue_;

/// number of reference points
label nRefPoints_;

/// the global reference points in parallel
label nRefPointsGlobal_;
/// total number of reference points
label nRefPoints_ = 0;

/// upper bound to be considered as non-data
scalar varUpperBound_;

/// whether we find the reference data
label isRefData_;

/// DATurbulenceModel object
const DATurbulenceModel& daTurb_;

public:
TypeName("variance");
// Constructors
Expand Down
12 changes: 12 additions & 0 deletions src/adjoint/DASolver/DASolver.H
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,18 @@ public:
/// write state variables that are NO_WRITE to disk
void writeAdjStates();

/// return the elapsed clock time for testing speed
scalar getElapsedClockTime()
{
return runTimePtr_->elapsedClockTime();
}

/// return the elapsed CPU time for testing speed
scalar getElapsedCpuTime()
{
return runTimePtr_->elapsedCpuTime();
}

#ifdef CODI_AD_REVERSE

/// global tape for reverse-mode AD
Expand Down
58 changes: 57 additions & 1 deletion src/adjoint/DAUtility/DAUtility.C
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ void DAUtility::pyDict2OFDict(
valSetLabel.setSize(label(listSize));
List<word> valSetWord;
valSetWord.setSize(label(listSize));
// we also support scalarListList or labelListList
List<List<scalar>> valSetScalarList;
valSetScalarList.setSize(label(listSize));
List<List<label>> valSetLabelList;
valSetLabelList.setSize(label(listSize));

label isListListScalar = 0;
label isListListLabel = 0;

// need to check what type of list this is
// by checking its first element
Expand Down Expand Up @@ -179,11 +187,48 @@ void DAUtility::pyDict2OFDict(
label valSet = PyObject_IsTrue(valueListJ);
valSetLabel[j] = valSet;
}
else if (tmpTypeWord == "list")
{
// it is a ListList, we need to add values to
// valSetScalarList or valSetLabelList
// NOTE: we support only labelListList or scalarListList
// size of the list
Py_ssize_t listSize1 = PyList_Size(valueListJ);
PyObject* tmp1 = PyList_GetItem(valueListJ, 0);
const char* tmpType1 = Py_TYPE(tmp1)->tp_name;
word tmpTypeWord1 = word(tmpType1);

valSetLabelList[j].setSize(listSize1);
valSetScalarList[j].setSize(listSize1);

// assign value to the OpenFOAM listList
for (label k = 0; k < listSize1; k++)
{
PyObject* valueListK = PyList_GetItem(valueListJ, k);
if (tmpTypeWord1 == "int")
{
long valSet = PyLong_AsLong(valueListK);
valSetLabelList[j][k] = label(valSet);
isListListLabel = 1;
}
else if (tmpTypeWord1 == "float")
{
scalar valSet = PyFloat_AS_DOUBLE(valueListK);
valSetScalarList[j][k] = valSet;
isListListScalar = 1;
}
else
{
FatalErrorIn("pyDict2OFDict") << "listList only supports double and int"
<< abort(FatalError);
}
}
}
else
{
FatalErrorIn("pyDict2OFDict") << "Type: <" << tmpTypeWord << "> for " << keyUTF8
<< " list is not supported! Options are:"
<< " str, int, bool, and float!"
<< " str, int, bool, float, and list!"
<< abort(FatalError);
}
}
Expand All @@ -205,6 +250,17 @@ void DAUtility::pyDict2OFDict(
{
ofDict.add(keyUTF8, valSetLabel, overwrite);
}

// if there are listLists, add them separately
if (isListListLabel)
{
ofDict.add(keyUTF8, valSetLabelList, overwrite);
}

if (isListListScalar)
{
ofDict.add(keyUTF8, valSetScalarList, overwrite);
}
}
else if (word(valueType) == "dict")
{
Expand Down
16 changes: 16 additions & 0 deletions src/pyDASolvers/DASolvers.H
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,22 @@ public:
return returnVal;
}

/// return the elapsed clock time for testing speed
double getElapsedClockTime()
{
double returnVal = 0.0;
assignValueCheckAD(returnVal, DASolverPtr_->getElapsedClockTime());
return returnVal;
}

/// return the elapsed CPU time for testing speed
double getElapsedCpuTime()
{
double returnVal = 0.0;
assignValueCheckAD(returnVal, DASolverPtr_->getElapsedCpuTime());
return returnVal;
}

void calcCouplingFaceCoords(
const double* volCoords,
double* surfCoords)
Expand Down
8 changes: 8 additions & 0 deletions src/pyDASolvers/pyDASolvers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ cdef extern from "DASolvers.H" namespace "Foam":
double getObjFuncValue(char *)
double getObjFuncValueUnsteady(char *)
double getObjFuncUnsteadyScaling(char *)
double getElapsedClockTime()
double getElapsedCpuTime()
void calcCouplingFaceCoords(double *, double *)
void calcCouplingFaceCoordsAD(double *, double *, double *)
void getForces(PetscVec, PetscVec, PetscVec)
Expand Down Expand Up @@ -378,6 +380,12 @@ cdef class pyDASolvers:

def getObjFuncUnsteadyScaling(self, objFuncName):
return self._thisptr.getObjFuncUnsteadyScaling(objFuncName)

def getElapsedClockTime(self):
return self._thisptr.getElapsedClockTime()

def getElapsedCpuTime(self):
return self._thisptr.getElapsedCpuTime()

def calcCouplingFaceCoords(self,
np.ndarray[double, ndim=1, mode="c"] volCoords,
Expand Down
10 changes: 8 additions & 2 deletions src/utilities/postProcessing/getFIData/Make/options
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ EXE_INC = \
-Wno-conversion-null \
-Wno-deprecated-copy \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude
-I$(LIB_SRC)/meshTools/lnInclude \
-I$(LIB_SRC)/TurbulenceModels/turbulenceModels/lnInclude \
-I$(LIB_SRC)/TurbulenceModels/incompressible/lnInclude \
-I$(LIB_SRC)/TurbulenceModels/compressible/lnInclude

EXE_LIBS = \
-lfiniteVolume \
-lmeshTools
-lmeshTools \
-lturbulenceModels \
-lincompressibleTurbulenceModels \
-lcompressibleTurbulenceModels

Loading
Loading