Skip to content
Draft
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
36 changes: 31 additions & 5 deletions src/adjoint/DAFunction/DAFunctionVariableVolSum.C
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ DAFunctionVariableVolSum::DAFunctionVariableVolSum(
multiplyVol_ = functionDict_.lookupOrDefault<label>("multiplyVol", 1);

divByTotalVol_ = functionDict_.lookupOrDefault<label>("divByTotalVol", 0);

invertField_ = functionDict_.lookupOrDefault<label>("invertField", 0);

invertVal_ = functionDict_.lookupOrDefault<label>("invertVal", 1); // Binary inversion from 0 to 1, vice versa
}

/// calculate the value of objective function
Expand All @@ -57,7 +61,7 @@ scalar DAFunctionVariableVolSum::calcFunction()

const objectRegistry& db = mesh_.thisDb();

scalar totalVol = 1.0;
scalar totalVol = 0.0;

if (divByTotalVol_)
{
Expand All @@ -67,6 +71,10 @@ scalar DAFunctionVariableVolSum::calcFunction()
}
reduce(totalVol, sumOp<scalar>());
}
else
{
totalVol = 1.0;
}

if (varType_ == "scalar")
{
Expand All @@ -75,40 +83,52 @@ scalar DAFunctionVariableVolSum::calcFunction()
forAll(cellSources_, idxI)
{
const label& cellI = cellSources_[idxI];
scalar value = var[cellI];
scalar volume = 1.0;
if (multiplyVol_)
{
volume = mesh_.V()[cellI];
}
if (invertField_)
{

value = invertVal_ - value;
}
if (isSquare_)
{
functionValue += scale_ * volume * var[cellI] * var[cellI];
functionValue += scale_ * volume * value * value;
}
else
{
functionValue += scale_ * volume * var[cellI];
functionValue += scale_ * volume * value;
}
}
}
else if (varType_ == "vector")
{

const volVectorField& var = db.lookupObject<volVectorField>(varName_);
// calculate mass
forAll(cellSources_, idxI)
{
const label& cellI = cellSources_[idxI];
scalar value = var[cellI][index_];
scalar volume = 1.0;
if (multiplyVol_)
{
volume = mesh_.V()[cellI];
}
if (invertField_)
{
value = invertVal_ - value;
}
if (isSquare_)
{
functionValue += scale_ * volume * var[cellI][index_] * var[cellI][index_];
functionValue += scale_ * volume * value * value;
}
else
{
functionValue += scale_ * volume * var[cellI][index_];
functionValue += scale_ * volume * value;
}
}
}
Expand All @@ -122,8 +142,14 @@ scalar DAFunctionVariableVolSum::calcFunction()
// need to reduce the sum of force across all processors
reduce(functionValue, sumOp<scalar>());

Info << "Raw functionValue = " << functionValue << endl;
Info << "Total Volume = " << totalVol << endl;
Info << "Ratio = " << functionValue/totalVol << endl;
Info << "volume = " << totalVol << endl;

functionValue /= totalVol;


// check if we need to calculate refDiff.
this->calcRefVar(functionValue);

Expand Down
6 changes: 6 additions & 0 deletions src/adjoint/DAFunction/DAFunctionVariableVolSum.H
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ protected:
/// whether to multiply the variable by the volume
label multiplyVol_;

/// whether to invert the binary scalar field
label invertField_;

/// scalar value to invert the field by
scalar invertVal_;

public:
TypeName("variableVolSum");
// Constructors
Expand Down
114 changes: 114 additions & 0 deletions src/adjoint/DAFunction/DAFunctionViscousDissipation.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "DAFunctionViscousDissipation.H"

namespace Foam
{

defineTypeNameAndDebug(DAFunctionViscousDissipation, 0);
addToRunTimeSelectionTable(
DAFunction,
DAFunctionViscousDissipation,
dictionary);

DAFunctionViscousDissipation::DAFunctionViscousDissipation(
const fvMesh& mesh,
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName)
: DAFunction(
mesh,
daOption,
daModel,
daIndex,
functionName),
daTurb_(daModel.getDATurbulenceModel())
{
}

scalar DAFunctionViscousDissipation::calcFunction()
{
/*
Description:
Calculate the total power dissipation (Reference: https://doi.org/10.1002/nme.1468)

J = \int [ 2*muEff*D:D + alphaPorosity*(U·U) ] dV

where

D = symm(grad(U))

The first term represents viscous dissipation while the second
represents Brinkman (Darcy) dissipation.

Output:
functionValue
*/

scalar functionValue = 0.0;

const objectRegistry& db = mesh_.thisDb();

//-------------------------------------------------------------
// Flow variables
//-------------------------------------------------------------

const volVectorField& U =
db.lookupObject<volVectorField>("U");

const volScalarField& alpha =
db.lookupObject<volScalarField>("alpha");

//-------------------------------------------------------------
// Velocity gradient
//-------------------------------------------------------------

tmp<volTensorField> tGradU = fvc::grad(U);

//-------------------------------------------------------------
// Symmetric strain-rate tensor
//-------------------------------------------------------------

volSymmTensorField D(
symm(tGradU()));

//-------------------------------------------------------------
// Effective viscosity
//-------------------------------------------------------------

tmp<volScalarField> tNuEff = daTurb_.nuEff();

//-------------------------------------------------------------
// Integrate objective
//-------------------------------------------------------------

forAll(cellSources_, idxI)
{
const label cellI = cellSources_[idxI];

scalar viscousTerm =
2.0
* tNuEff()[cellI]
* (D[cellI] && D[cellI]);

scalar brinkmanTerm =
alpha[cellI]
* magSqr(U[cellI]);

functionValue +=
scale_
* (viscousTerm + brinkmanTerm)
* mesh_.V()[cellI];
}

//-------------------------------------------------------------
// Parallel reduction
//-------------------------------------------------------------

reduce(functionValue, sumOp<scalar>());

this->calcRefVar(functionValue);

return functionValue;
}

}
53 changes: 53 additions & 0 deletions src/adjoint/DAFunction/DAFunctionViscousDissipation.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*---------------------------------------------------------------------------*\

DAFoam : Discrete Adjoint with OpenFOAM

Description:
Power (viscous) dissipation objective function
Usage:
"function": {
"viscousDissipation": {
"type": "DAViscousDissipation",
"scale": 1.0,
"source": "allCells"
}

\*---------------------------------------------------------------------------*/

#ifndef DAFunctionViscousDissipation_H
#define DAFunctionViscousDissipation_H

#include "DAFunction.H"
#include "addToRunTimeSelectionTable.H"

namespace Foam
{

class DAFunctionViscousDissipation
: public DAFunction
{

protected:
/// Turbulence model
const DATurbulenceModel& daTurb_;

public:
TypeName("DAViscousDissipation");

DAFunctionViscousDissipation(
const fvMesh& mesh,
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName);

virtual ~DAFunctionViscousDissipation()
{
}

virtual scalar calcFunction();
};

}

#endif
1 change: 1 addition & 0 deletions src/adjoint/Make/files
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ DAFunction/DAFunctionMeshQualityKS.C
DAFunction/DAFunctionVonMisesStressKS.C
DAFunction/DAFunctionResidualNorm.C
DAFunction/DAFunctionFieldMax.C
DAFunction/DAFunctionViscousDissipation.C

DAFvSource/DAFvSource.C
DAFvSource/DAFvSourceActuatorDisk.C
Expand Down
Loading