Skip to content

Commit

Permalink
Changed designVar to solverInput and re-organized all partial calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Dec 31, 2024
1 parent 60657fc commit 1af5130
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 629 deletions.
735 changes: 133 additions & 602 deletions dafoam/mphys/mphys_dafoam.py

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,27 @@ def __init__(self):
## },
self.function = {}

## Design variable information. Different type of design variables require different keys
## For alpha, we need to prescribe a list of far field patch names from which the angle of
## Solver input information. Different type of design variables require different keys
## For patchVelocity, we need to set a list of far field patch names from which the angle of
## attack is computed, this is usually a far field patch. Also, we need to prescribe
## flow and normal axies, and alpha = atan( U_normal / U_flow ) at patches
## Example
## designVar = {
## "shapey" : {"designVarType": "FFD"},
## "twist": {"designVarType": "FFD"},
## "alpha" = {
## "designVarType": "AOA",
## solverInput = {
## "aero_vol_coords" : {"type": "volCoord"},
## "patchV" = {
## "type": "patchVelocity",
## "patches": ["farField"],
## "flowAxis": "x",
## "normalAxis": "y"
## },
## "ux0" = {
## "designVarType": "BC",
## "type": "patchVariable",
## "patches": ["inlet"],
## "variable": "U",
## "comp": 0
## },
## }
self.designVar = {}
self.solverInput = {}

## List of patch names for the design surface. These patch names need to be of wall type
## and shows up in the constant/polyMesh/boundary file
Expand Down Expand Up @@ -1062,7 +1061,7 @@ def _initializeAdjTotalDeriv(self):
An empty dict that contains total derivative of objective function with respect design variables
"""

designVarDict = self.getOption("designVar")
designVarDict = self.getOption("solverInput")
functionDict = self.getOption("function")

adjTotalDeriv = {}
Expand Down Expand Up @@ -2353,7 +2352,7 @@ def solveAdjointUnsteady(self):
self.solverAD.createMLRKSPMatrixFree(PCMat, ksp)

functionDict = self.getOption("function")
designVarDict = self.getOption("designVar")
designVarDict = self.getOption("solverInput")

# init the dFdW vec
wSize = self.solver.getNLocalAdjointStates()
Expand Down Expand Up @@ -2569,7 +2568,7 @@ def solveAdjoint(self):
# ************ Now compute the total derivatives **********************
Info("Computing total derivatives....")

designVarDict = self.getOption("designVar")
designVarDict = self.getOption("solverInput")
for designVarName in designVarDict:
Info("Computing total derivatives for %s" % designVarName)
###################### BC: boundary condition as design variable ###################
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DAFunction/DAFunctionForce.C
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ DAFunctionForce::DAFunctionForce(
// initial value for forceDir_. it will be dynamically adjusted later
forceDir_ = {1.0, 0.0, 0.0};
word patchVelocityInputName = functionDict_.getWord("patchVelocityInputName");
dictionary patchVSubDict = daOption_.getAllOptions().subDict("designVar").subDict(patchVelocityInputName);
dictionary patchVSubDict = daOption_.getAllOptions().subDict("solverInput").subDict(patchVelocityInputName);
HashTable<label> axisIndices;
axisIndices.set("x", 0);
axisIndices.set("y", 1);
Expand Down
2 changes: 2 additions & 0 deletions src/adjoint/DAInput/DAInput.H
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public:
virtual void run(const scalarList& input) = 0;

virtual label size() = 0;

virtual label distributed() = 0;
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
8 changes: 4 additions & 4 deletions src/adjoint/DAInput/DAInputPatchVelocity.C
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ void DAInputPatchVelocity::run(const scalarList& input)
globalVar.patchVelocity[1] = input[1];

wordList patchNames;
dictionary aoaSubDict = daOption_.getAllOptions().subDict("designVar").subDict(inputName_);
aoaSubDict.readEntry<wordList>("patches", patchNames);
dictionary patchVSubDict = daOption_.getAllOptions().subDict("solverInput").subDict(inputName_);
patchVSubDict.readEntry<wordList>("patches", patchNames);

#ifndef CODI_ADR
Info << "DAInputPatchVelocity. " << endl;
Info << "Setting UMag = " << input[0] << " AoA = " << input[1] << " degs at " << patchNames << endl;
#endif

// the streamwise axis of aoa, aoa = tan( U_normal/U_flow )
word flowAxis = aoaSubDict.getWord("flowAxis");
word normalAxis = aoaSubDict.getWord("normalAxis");
word flowAxis = patchVSubDict.getWord("flowAxis");
word normalAxis = patchVSubDict.getWord("normalAxis");
scalar UMag = input[0];

HashTable<label> axisIndices;
Expand Down
5 changes: 5 additions & 0 deletions src/adjoint/DAInput/DAInputPatchVelocity.H
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public:
// [UMag, AoA]
return 2;
}

virtual label distributed()
{
return 0;
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
5 changes: 5 additions & 0 deletions src/adjoint/DAInput/DAInputStateVar.H
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public:
{
return daIndex_.nLocalAdjointStates;
}

virtual label distributed()
{
return 1;
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
5 changes: 5 additions & 0 deletions src/adjoint/DAInput/DAInputVolCoord.H
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public:
{
return daIndex_.nLocalXv;
}

virtual label distributed()
{
return 1;
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
2 changes: 2 additions & 0 deletions src/adjoint/DAOutput/DAOutput.H
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public:
virtual void run(scalarList& output) = 0;

virtual label size() = 0;

virtual label distributed() = 0;
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
5 changes: 5 additions & 0 deletions src/adjoint/DAOutput/DAOutputFunction.H
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public:
{
return 1;
}

virtual label distributed()
{
return 0;
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
5 changes: 5 additions & 0 deletions src/adjoint/DAOutput/DAOutputResidual.H
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public:
{
return daIndex_.nLocalAdjointStates;
}

virtual label distributed()
{
return 1;
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
6 changes: 3 additions & 3 deletions src/adjoint/DASolver/DAPimpleFoam/DAPimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ void DAPimpleFoam::initSolver()
daFvSourcePtr_->calcFvSource(fvSource);
}

// reduceIO does not write mesh, but if there is a FFD variable, set writeMesh to 1
dictionary dvSubDict = daOptionPtr_->getAllOptions().subDict("designVar");
// reduceIO does not write mesh, but if there is a shape variable, set writeMesh to 1
dictionary dvSubDict = daOptionPtr_->getAllOptions().subDict("solverInput");
forAll(dvSubDict.toc(), idxI)
{
word dvName = dvSubDict.toc()[idxI];
if (dvSubDict.subDict(dvName).getWord("designVarType") == "FFD")
if (dvSubDict.subDict(dvName).getWord("type") == "volCoord")
{
reduceIOWriteMesh_ = 1;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/adjoint/DASolver/DARhoPimpleFoam/DARhoPimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ void DARhoPimpleFoam::initSolver()
}

// reduceIO does not write mesh, but if there is a FFD variable, set writeMesh to 1
dictionary dvSubDict = daOptionPtr_->getAllOptions().subDict("designVar");
dictionary dvSubDict = daOptionPtr_->getAllOptions().subDict("solverInput");
forAll(dvSubDict.toc(), idxI)
{
word dvName = dvSubDict.toc()[idxI];
if (dvSubDict.subDict(dvName).getWord("designVarType") == "FFD")
if (dvSubDict.subDict(dvName).getWord("type") == "volCoord")
{
reduceIOWriteMesh_ = 1;
break;
Expand Down
34 changes: 34 additions & 0 deletions src/adjoint/DASolver/DASolver.C
Original file line number Diff line number Diff line change
Expand Up @@ -4872,6 +4872,40 @@ label DASolver::getOutputSize(
return daOutput->size();
}

label DASolver::getInputDistributed(
const word inputName,
const word inputType)
{
autoPtr<DAInput> daInput(
DAInput::New(
inputName,
inputType,
meshPtr_(),
daOptionPtr_(),
daModelPtr_(),
daIndexPtr_()));

return daInput->distributed();
}

label DASolver::getOutputDistributed(
const word outputName,
const word outputType)
{
autoPtr<DAOutput> daOutput(
DAOutput::New(
outputName,
outputType,
meshPtr_(),
daOptionPtr_(),
daModelPtr_(),
daIndexPtr_(),
daResidualPtr_(),
daFunctionPtrList_));

return daOutput->distributed();
}

void DASolver::calcJacTVecProduct(
const word inputName,
const word inputType,
Expand Down
10 changes: 10 additions & 0 deletions src/adjoint/DASolver/DASolver.H
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ public:
label getOutputSize(
const word outputName,
const word outputType);

/// get whether the input is distributed among processors
label getInputDistributed(
const word inputName,
const word inputType);

/// get whether the output is distributed among processors
label getOutputDistributed(
const word outputName,
const word outputType);

/// calculate the Jacobian-matrix-transposed and vector product for product = [dOutput/dInput]^T * seed
void calcJacTVecProduct(
Expand Down
14 changes: 14 additions & 0 deletions src/pyDASolvers/DASolvers.H
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public:
return DASolverPtr_->getOutputSize(outputName, outputType);
}

label getInputDistributed(
const word inputName,
const word inputType)
{
return DASolverPtr_->getInputDistributed(inputName, inputType);
}

label getOutputDistributed(
const word outputName,
const word outputType)
{
return DASolverPtr_->getOutputDistributed(outputName, outputType);
}

/// calculate the Jacobian-matrix-transposed and vector product for product = [dOutput/dInput]^T * seed
void calcJacTVecProduct(
const word inputName,
Expand Down
8 changes: 8 additions & 0 deletions src/pyDASolvers/pyDASolvers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ cdef extern from "DASolvers.H" namespace "Foam":
void calcJacTVecProduct(char *, char *, int, int, double *, char *, char *, int, int, double *, double *)
int getInputSize(char *, char *)
int getOutputSize(char *, char *)
int getInputDistributed(char *, char *)
int getOutputDistributed(char *, char *)
void setSolverInput(char *, char *, int, double *, double *)
void setRunStatus(char *)
void calcdRdWT(int, PetscMat)
Expand Down Expand Up @@ -230,6 +232,12 @@ cdef class pyDASolvers:
def getOutputSize(self, outputName, outputType):
return self._thisptr.getOutputSize(outputName.encode(), outputType.encode())

def getInputDistributed(self, inputName, inputType):
return self._thisptr.getInputDistributed(inputName.encode(), inputType.encode())

def getOutputDistributed(self, outputName, outputType):
return self._thisptr.getOutputDistributed(outputName.encode(), outputType.encode())

def calcJacTVecProduct(self,
inputName,
inputType,
Expand Down
4 changes: 2 additions & 2 deletions tests/runRegTests_DARhoSimpleFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
"dynAdjustTol": False
},
"normalizeStates": {"U": U0, "p": p0, "phi": 1.0, "T": T0, "nuTilda": 1e-3},
"designVar": {
"aero_vol_coords": {"designVarType": "volCoord"},
"solverInput": {
"aero_vol_coords": {"type": "volCoord"},
},
}

Expand Down
6 changes: 3 additions & 3 deletions tests/runRegTests_DASimpleFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
},
"adjEqnOption": {"gmresRelTol": 1.0e-12, "pcFillLevel": 1, "jacMatReOrdering": "rcm", "dynAdjustTol": False},
"normalizeStates": {"U": U0, "p": U0 * U0 / 2.0, "phi": 1.0, "nuTilda": 1e-3},
"designVar": {
"aero_vol_coords": {"designVarType": "volCoord"},
"patchV": {"designVarType": "patchVelocity", "patches": ["inout"], "flowAxis": "x", "normalAxis": "y"},
"solverInput": {
"aero_vol_coords": {"type": "volCoord"},
"patchV": {"type": "patchVelocity", "patches": ["inout"], "flowAxis": "x", "normalAxis": "y"},
},
}

Expand Down

0 comments on commit 1af5130

Please sign in to comment.