forked from mdolab/dafoam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
443 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/*---------------------------------------------------------------------------*\ | ||
DAFoam : Discrete Adjoint with OpenFOAM | ||
Version : v3 | ||
\*---------------------------------------------------------------------------*/ | ||
|
||
#include "DAObjFuncFieldMaxKS.H" | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
namespace Foam | ||
{ | ||
|
||
defineTypeNameAndDebug(DAObjFuncFieldMaxKS, 0); | ||
addToRunTimeSelectionTable(DAObjFunc, DAObjFuncFieldMaxKS, dictionary); | ||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // | ||
|
||
DAObjFuncFieldMaxKS::DAObjFuncFieldMaxKS( | ||
const fvMesh& mesh, | ||
const DAOption& daOption, | ||
const DAModel& daModel, | ||
const DAIndex& daIndex, | ||
const DAResidual& daResidual, | ||
const word objFuncName, | ||
const word objFuncPart, | ||
const dictionary& objFuncDict) | ||
: DAObjFunc( | ||
mesh, | ||
daOption, | ||
daModel, | ||
daIndex, | ||
daResidual, | ||
objFuncName, | ||
objFuncPart, | ||
objFuncDict) | ||
{ | ||
// Assign type, this is common for all objectives | ||
objFuncDict_.readEntry<word>("type", objFuncType_); | ||
|
||
objFuncDict_.readEntry<word>("varName", varName_); | ||
|
||
objFuncDict_.readEntry<word>("varType", varType_); | ||
|
||
objFuncDict_.readEntry<label>("component", component_); | ||
|
||
objFuncDict_.readEntry<scalar>("coeffKS", coeffKS_); | ||
} | ||
|
||
/// calculate the value of objective function | ||
void DAObjFuncFieldMaxKS::calcObjFunc( | ||
const labelList& objFuncFaceSources, | ||
const labelList& objFuncCellSources, | ||
scalarList& objFuncFaceValues, | ||
scalarList& objFuncCellValues, | ||
scalar& objFuncValue) | ||
{ | ||
/* | ||
Description: | ||
Calculate the patch approximated max using the KS function | ||
Input: | ||
objFuncFaceSources: List of face source (index) for this objective | ||
objFuncCellSources: List of cell source (index) for this objective | ||
Output: | ||
objFuncFaceValues: the discrete value of objective for each face source (index). | ||
This will be used for computing df/dw in the adjoint. | ||
objFuncCellValues: the discrete value of objective on each cell source (index). | ||
This will be used for computing df/dw in the adjoint. | ||
objFuncValue: the sum of objective, reduced across all processors and scaled by "scale" | ||
*/ | ||
|
||
// initialize objFunValue | ||
objFuncValue = 0.0; | ||
|
||
const objectRegistry& db = mesh_.thisDb(); | ||
|
||
if (varType_ == "scalar") | ||
{ | ||
const volScalarField& var = db.lookupObject<volScalarField>(varName_); | ||
|
||
forAll(var, cellI) | ||
{ | ||
objFuncValue += exp(coeffKS_ * var[cellI]); | ||
|
||
if (objFuncValue > 1e200) | ||
{ | ||
FatalErrorIn(" ") << "KS function summation term too large! " | ||
<< "Reduce coeffKS! " << abort(FatalError); | ||
} | ||
} | ||
} | ||
else if (varType_ == "vector") | ||
{ | ||
const volVectorField& var = db.lookupObject<volVectorField>(varName_); | ||
forAll(var, cellI) | ||
{ | ||
objFuncValue += exp(coeffKS_ * var[cellI][component_]); | ||
|
||
if (objFuncValue > 1e200) | ||
{ | ||
FatalErrorIn(" ") << "KS function summation term too large! " | ||
<< "Reduce coeffKS! " << abort(FatalError); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
FatalErrorIn("DAObjFuncFieldMaxKS::calcObjFunc") | ||
<< "varType not valid. Options are scalar or vector" | ||
<< abort(FatalError); | ||
} | ||
|
||
// need to reduce the sum of force across all processors | ||
reduce(objFuncValue, sumOp<scalar>()); | ||
|
||
objFuncValue = log(objFuncValue) / coeffKS_; | ||
|
||
// check if we need to calculate refDiff. | ||
this->calcRefVar(objFuncValue); | ||
|
||
return; | ||
} | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
} // End namespace Foam | ||
|
||
// ************************************************************************* // |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*---------------------------------------------------------------------------*\ | ||
DAFoam : Discrete Adjoint with OpenFOAM | ||
Version : v3 | ||
Description: | ||
Child class for approximated max variable (in tne | ||
entire field) using the KS function | ||
\*---------------------------------------------------------------------------*/ | ||
|
||
#ifndef DAObjFuncFieldMaxKS_H | ||
#define DAObjFuncFieldMaxKS_H | ||
|
||
#include "DAObjFunc.H" | ||
#include "addToRunTimeSelectionTable.H" | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
namespace Foam | ||
{ | ||
|
||
/*---------------------------------------------------------------------------*\ | ||
Class DAObjFuncFieldMaxKS Declaration | ||
\*---------------------------------------------------------------------------*/ | ||
|
||
class DAObjFuncFieldMaxKS | ||
: public DAObjFunc | ||
{ | ||
|
||
protected: | ||
|
||
/// name of the variable | ||
word varName_; | ||
|
||
/// type of the variable either vector or scalar | ||
word varType_; | ||
|
||
/// if vector which element? | ||
label component_; | ||
|
||
/// the KS coefficient | ||
scalar coeffKS_; | ||
|
||
public: | ||
TypeName("fieldMaxKS"); | ||
// Constructors | ||
|
||
//- Construct from components | ||
DAObjFuncFieldMaxKS( | ||
const fvMesh& mesh, | ||
const DAOption& daOption, | ||
const DAModel& daModel, | ||
const DAIndex& daIndex, | ||
const DAResidual& daResidual, | ||
const word objFuncName, | ||
const word objFuncPart, | ||
const dictionary& objFuncDict); | ||
|
||
//- Destructor | ||
virtual ~DAObjFuncFieldMaxKS() | ||
{ | ||
} | ||
|
||
/// calculate the value of objective function | ||
virtual void calcObjFunc( | ||
const labelList& objFuncFaceSources, | ||
const labelList& objFuncCellSources, | ||
scalarList& objFuncFaceValues, | ||
scalarList& objFuncCellValues, | ||
scalar& objFuncValue); | ||
}; | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
} // End namespace Foam | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
#endif | ||
|
||
// ************************************************************************* // |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/*---------------------------------------------------------------------------*\ | ||
DAFoam : Discrete Adjoint with OpenFOAM | ||
Version : v3 | ||
\*---------------------------------------------------------------------------*/ | ||
|
||
#include "DAObjFuncPatchMaxKS.H" | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
namespace Foam | ||
{ | ||
|
||
defineTypeNameAndDebug(DAObjFuncPatchMaxKS, 0); | ||
addToRunTimeSelectionTable(DAObjFunc, DAObjFuncPatchMaxKS, dictionary); | ||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // | ||
|
||
DAObjFuncPatchMaxKS::DAObjFuncPatchMaxKS( | ||
const fvMesh& mesh, | ||
const DAOption& daOption, | ||
const DAModel& daModel, | ||
const DAIndex& daIndex, | ||
const DAResidual& daResidual, | ||
const word objFuncName, | ||
const word objFuncPart, | ||
const dictionary& objFuncDict) | ||
: DAObjFunc( | ||
mesh, | ||
daOption, | ||
daModel, | ||
daIndex, | ||
daResidual, | ||
objFuncName, | ||
objFuncPart, | ||
objFuncDict) | ||
{ | ||
// Assign type, this is common for all objectives | ||
objFuncDict_.readEntry<word>("type", objFuncType_); | ||
|
||
objFuncDict_.readEntry<word>("varName", varName_); | ||
|
||
objFuncDict_.readEntry<word>("varType", varType_); | ||
|
||
objFuncDict_.readEntry<label>("component", component_); | ||
|
||
objFuncDict_.readEntry<scalar>("coeffKS", coeffKS_); | ||
} | ||
|
||
/// calculate the value of objective function | ||
void DAObjFuncPatchMaxKS::calcObjFunc( | ||
const labelList& objFuncFaceSources, | ||
const labelList& objFuncCellSources, | ||
scalarList& objFuncFaceValues, | ||
scalarList& objFuncCellValues, | ||
scalar& objFuncValue) | ||
{ | ||
/* | ||
Description: | ||
Calculate the patch approximated max using the KS function | ||
Input: | ||
objFuncFaceSources: List of face source (index) for this objective | ||
objFuncCellSources: List of cell source (index) for this objective | ||
Output: | ||
objFuncFaceValues: the discrete value of objective for each face source (index). | ||
This will be used for computing df/dw in the adjoint. | ||
objFuncCellValues: the discrete value of objective on each cell source (index). | ||
This will be used for computing df/dw in the adjoint. | ||
objFuncValue: the sum of objective, reduced across all processors and scaled by "scale" | ||
*/ | ||
|
||
// initialize objFunValue | ||
objFuncValue = 0.0; | ||
|
||
const objectRegistry& db = mesh_.thisDb(); | ||
|
||
if (varType_ == "scalar") | ||
{ | ||
const volScalarField& var = db.lookupObject<volScalarField>(varName_); | ||
|
||
forAll(objFuncFaceSources, idxI) | ||
{ | ||
const label& objFuncFaceI = objFuncFaceSources[idxI]; | ||
label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces; | ||
const label patchI = daIndex_.bFacePatchI[bFaceI]; | ||
const label faceI = daIndex_.bFaceFaceI[bFaceI]; | ||
|
||
objFuncValue += exp(coeffKS_ * var.boundaryField()[patchI][faceI]); | ||
|
||
if (objFuncValue > 1e200) | ||
{ | ||
FatalErrorIn(" ") << "KS function summation term too large! " | ||
<< "Reduce coeffKS! " << abort(FatalError); | ||
} | ||
} | ||
} | ||
else if (varType_ == "vector") | ||
{ | ||
const volVectorField& var = db.lookupObject<volVectorField>(varName_); | ||
|
||
// calculate area weighted heat flux | ||
forAll(objFuncFaceSources, idxI) | ||
{ | ||
const label& objFuncFaceI = objFuncFaceSources[idxI]; | ||
label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces; | ||
const label patchI = daIndex_.bFacePatchI[bFaceI]; | ||
const label faceI = daIndex_.bFaceFaceI[bFaceI]; | ||
|
||
objFuncValue += exp(coeffKS_ * var.boundaryField()[patchI][faceI][component_]); | ||
|
||
if (objFuncValue > 1e200) | ||
{ | ||
FatalErrorIn(" ") << "KS function summation term too large! " | ||
<< "Reduce coeffKS! " << abort(FatalError); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
FatalErrorIn("DAObjFuncPatchMaxKS::calcObjFunc") | ||
<< "varType not valid. Options are scalar or vector" | ||
<< abort(FatalError); | ||
} | ||
|
||
// need to reduce the sum of force across all processors | ||
reduce(objFuncValue, sumOp<scalar>()); | ||
|
||
objFuncValue = log(objFuncValue) / coeffKS_; | ||
|
||
// check if we need to calculate refDiff. | ||
this->calcRefVar(objFuncValue); | ||
|
||
return; | ||
} | ||
|
||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
||
} // End namespace Foam | ||
|
||
// ************************************************************************* // |
Oops, something went wrong.