Skip to content

Commit

Permalink
Added patch and field Max objs. (mdolab#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe authored Sep 17, 2024
1 parent a57881f commit c59e8ce
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/adjoint/DAObjFunc/DAObjFuncFieldMaxKS.C
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

// ************************************************************************* //
82 changes: 82 additions & 0 deletions src/adjoint/DAObjFunc/DAObjFuncFieldMaxKS.H
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

// ************************************************************************* //
145 changes: 145 additions & 0 deletions src/adjoint/DAObjFunc/DAObjFuncPatchMaxKS.C
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

// ************************************************************************* //
Loading

0 comments on commit c59e8ce

Please sign in to comment.