Skip to content

Commit

Permalink
Removed parts for DAFunction and re-organized codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Jan 3, 2025
1 parent a554d84 commit 9cf01e0
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 522 deletions.
2 changes: 1 addition & 1 deletion Allmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ fi

pip install .

ls dafoam/libs/*.so && echo " " && echo "*** Build Successful! ***" && echo " "
ls -R dafoam/libs && echo " " && echo "*** Build Successful! ***" && echo " "
28 changes: 12 additions & 16 deletions src/adjoint/DAFunction/DAFunction.C
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ DAFunction::DAFunction(
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict)
const word functionName)
: mesh_(mesh),
daOption_(daOption),
daModel_(daModel),
daIndex_(daIndex),
functionName_(functionName),
functionPart_(functionPart),
functionDict_(functionDict)
functionName_(functionName)
{
functionDict_ = daOption.getAllOptions().subDict("function").subDict(functionName);

// Assign type and scale, this is common for all objectives
functionDict_.readEntry<word>("type", functionType_);
functionDict_.readEntry<scalar>("scale", scale_);

// calcualte the face and cell indices that are associated with this objective
this->calcFunctionSources();
Expand All @@ -54,20 +55,19 @@ autoPtr<DAFunction> DAFunction::New(
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict)
const word functionName)
{
// standard setup for runtime selectable classes

dictionary functionDict = daOption.getAllOptions().subDict("function").subDict(functionName);

// look up the solver name
word modelType;
functionDict.readEntry<word>("type", modelType);

if (daOption.getAllOptions().lookupOrDefault<label>("debug", 0))
{
Info << "Selecting type: " << modelType << " for DAFunction. Name: " << functionName
<< " part: " << functionPart << endl;
Info << "Selecting type: " << modelType << " for DAFunction. Name: " << functionName << endl;
}

dictionaryConstructorTable::iterator cstrIter =
Expand All @@ -84,8 +84,6 @@ autoPtr<DAFunction> DAFunction::New(
" const DAModel&,"
" const DAIndex&,"
" const word,"
" const word,"
" const dictionary&"
")")
<< "Unknown DAFunction type "
<< modelType << nl << nl
Expand All @@ -100,9 +98,7 @@ autoPtr<DAFunction> DAFunction::New(
daOption,
daModel,
daIndex,
functionName,
functionPart,
functionDict));
functionName));
}

// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Expand Down
27 changes: 5 additions & 22 deletions src/adjoint/DAFunction/DAFunction.H
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ protected:
/// the name of the objective function
word functionName_;

/// the part of the objective function
word functionPart_;

/// the type of the objective function
word functionType_;

/// dictionary containing the information for the objective function
const dictionary& functionDict_;
dictionary functionDict_;

/// a sorted list of all face sources for the objective function
labelList faceSources_;
Expand Down Expand Up @@ -95,17 +92,13 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict),
const word functionName),
(
mesh,
daOption,
daModel,
daIndex,
functionName,
functionPart,
functionDict));
functionName));

// Constructors

Expand All @@ -115,9 +108,7 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict);
const word functionName);

// Selectors

Expand All @@ -127,9 +118,7 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict);
const word functionName);

//- Destructor
virtual ~DAFunction()
Expand Down Expand Up @@ -158,12 +147,6 @@ public:
return functionName_;
}

/// return the part of objective function
word getFunctionPart()
{
return functionPart_;
}

/// return the part of objective function
word getFunctionType()
{
Expand Down
16 changes: 4 additions & 12 deletions src/adjoint/DAFunction/DAFunctionForce.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,19 @@ DAFunctionForce::DAFunctionForce(
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict)
const word functionName)
: DAFunction(
mesh,
daOption,
daModel,
daIndex,
functionName,
functionPart,
functionDict),
functionName),
daTurb_(daModel.getDATurbulenceModel())
{

// for computing force, first read in some parameters from functionDict_
// these parameters are only for force objective

// Assign type, this is common for all objectives
functionDict_.readEntry<word>("type", functionType_);

// we support three direction modes
dirMode_ = functionDict_.getWord("directionMode");
if (dirMode_ == "fixedDirection")
Expand Down Expand Up @@ -69,19 +62,18 @@ DAFunctionForce::DAFunctionForce(
else
{
FatalErrorIn(" ") << "directionMode for "
<< functionName << " " << functionPart << " not valid!"
<< functionName << " not valid!"
<< "Options: fixedDirection, parallelToFlow, normalToFlow."
<< abort(FatalError);
}

if (fabs(mag(forceDir_) - 1.0) > 1.0e-8)
{
FatalErrorIn(" ") << "the magnitude of the direction parameter in "
<< functionName << " " << functionPart << " is not 1.0!"
<< functionName << " is not 1.0!"
<< abort(FatalError);
}

functionDict_.readEntry<scalar>("scale", scale_);
}

/// calculate the value of objective function
Expand Down
4 changes: 1 addition & 3 deletions src/adjoint/DAFunction/DAFunctionForce.H
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict);
const word functionName);

//- Destructor
virtual ~DAFunctionForce()
Expand Down
15 changes: 3 additions & 12 deletions src/adjoint/DAFunction/DAFunctionMoment.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,19 @@ DAFunctionMoment::DAFunctionMoment(
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict)
const word functionName)
: DAFunction(
mesh,
daOption,
daModel,
daIndex,
functionName,
functionPart,
functionDict),
functionName),
daTurb_(daModel.getDATurbulenceModel())
{

// for computing moment, first read in some parameters from functionDict_
// these parameters are only for moment objective

// Assign type, this is common for all objectives
functionDict_.readEntry<word>("type", functionType_);

scalarList dir;
functionDict_.readEntry<scalarList>("axis", dir);
momentDir_[0] = dir[0];
Expand All @@ -50,7 +43,7 @@ DAFunctionMoment::DAFunctionMoment(
if (fabs(mag(momentDir_) - 1.0) > 1.0e-4)
{
FatalErrorIn(" ") << "the magnitude of the axis parameter in "
<< functionName << " " << functionPart << " is not 1.0!"
<< functionName << " is not 1.0!"
<< abort(FatalError);
}

Expand All @@ -60,8 +53,6 @@ DAFunctionMoment::DAFunctionMoment(
momentCenter_[1] = center[1];
momentCenter_[2] = center[2];

functionDict_.readEntry<scalar>("scale", scale_);

}

/// calculate the value of objective function
Expand Down
4 changes: 1 addition & 3 deletions src/adjoint/DAFunction/DAFunctionMoment.H
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict);
const word functionName);

//- Destructor
virtual ~DAFunctionMoment()
Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAFunction/DAFunctionTotalPressure.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@ DAFunctionTotalPressure::DAFunctionTotalPressure(
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict)
const word functionName)
: DAFunction(
mesh,
daOption,
daModel,
daIndex,
functionName,
functionPart,
functionDict),
functionName),
daTurb_(const_cast<DATurbulenceModel&>(daModel.getDATurbulenceModel())),
rho_(daTurb_.rho())
{
// Assign type, this is common for all objectives
functionDict_.readEntry<word>("type", functionType_);

functionDict_.readEntry<scalar>("scale", scale_);
}

/// calculate the value of objective function
Expand Down
4 changes: 1 addition & 3 deletions src/adjoint/DAFunction/DAFunctionTotalPressure.H
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict);
const word functionName);

//- Destructor
virtual ~DAFunctionTotalPressure()
Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAFunction/DAFunctionWallHeatFlux.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ DAFunctionWallHeatFlux::DAFunctionWallHeatFlux(
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict)
const word functionName)
: DAFunction(
mesh,
daOption,
daModel,
daIndex,
functionName,
functionPart,
functionDict),
functionName),
wallHeatFlux_(
IOobject(
"wallHeatFlux",
Expand All @@ -43,10 +39,6 @@ DAFunctionWallHeatFlux::DAFunctionWallHeatFlux(
dimensionedScalar("wallHeatFlux", dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0),
"calculated")
{
// Assign type, this is common for all objectives
functionDict_.readEntry<word>("type", functionType_);

functionDict_.readEntry<scalar>("scale", scale_);

if (mesh_.thisDb().foundObject<DATurbulenceModel>("DATurbulenceModel"))
{
Expand Down
4 changes: 1 addition & 3 deletions src/adjoint/DAFunction/DAFunctionWallHeatFlux.H
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public:
const DAOption& daOption,
const DAModel& daModel,
const DAIndex& daIndex,
const word functionName,
const word functionPart,
const dictionary& functionDict);
const word functionName);

//- Destructor
virtual ~DAFunctionWallHeatFlux()
Expand Down
19 changes: 5 additions & 14 deletions src/adjoint/DAOutput/DAOutputFunction.C
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,14 @@ void DAOutputFunction::run(scalarList& output)
Compute the function value and assign them to the output array
*/

dictionary functionSubDict =
daOption_.getAllOptions().subDict("function").subDict(outputName_);
word functionName = outputName_;

// loop over all parts for this functionName
scalar fVal = 0.0;
forAll(functionSubDict.toc(), idxJ)
{
// get the subDict for this part
word functionPart = functionSubDict.toc()[idxJ];
label idxI = this->getFunctionListIndex(functionName, daFunctionList_);
DAFunction& daFunction = daFunctionList_[idxI];

// get function from daFunctionList_
label objIndx = this->getFunctionListIndex(outputName_, functionPart, daFunctionList_);
DAFunction& daFunction = daFunctionList_[objIndx];
// compute the objective function
scalar fVal = daFunction.getFunctionValue();

// compute the objective function
fVal += daFunction.getFunctionValue();
}
output[0] = fVal;
}

Expand Down
5 changes: 2 additions & 3 deletions src/adjoint/DAOutput/DAOutputFunction.H
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,18 @@ public:
/// return the index of a give functionName in daFunctionList
label getFunctionListIndex(
const word functionName,
const word functionPart,
UPtrList<DAFunction>& daFunctionList)
{
forAll(daFunctionList, idxI)
{
DAFunction& daFunction = daFunctionList[idxI];
word functionName1 = daFunction.getFunctionName();
word functionPart1 = daFunction.getFunctionPart();
if (functionPart1 == functionPart && functionName1 == functionName)
if (functionName1 == functionName)
{
return idxI;
}
}
return -9999;
}

virtual void run(scalarList& output);
Expand Down
Loading

0 comments on commit 9cf01e0

Please sign in to comment.