DAPartDerivdFdW.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAPartDerivdFdW.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAPartDerivdFdW, 0);
16 addToRunTimeSelectionTable(DAPartDeriv, DAPartDerivdFdW, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const word modelType,
21  const fvMesh& mesh,
22  const DAOption& daOption,
23  const DAModel& daModel,
24  const DAIndex& daIndex,
25  const DAJacCon& daJacCon,
26  const DAResidual& daResidual)
27  : DAPartDeriv(modelType,
28  mesh,
29  daOption,
30  daModel,
31  daIndex,
32  daJacCon,
33  daResidual)
34 {
35 }
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
39 
41  const dictionary& options,
42  Mat jacMat)
43 {
44  /*
45  Description:
46  Initialize jacMat
47 
48  Input:
49  options.objFuncFaceSources: The list of objFunc face indices
50 
51  options.objFuncCellSources: The list of objFunc cell indices
52  */
53 
54  labelList objFuncFaceSources;
55  labelList objFuncCellSources;
56  options.readEntry<labelList>("objFuncFaceSources", objFuncFaceSources);
57  options.readEntry<labelList>("objFuncCellSources", objFuncCellSources);
58 
59  // nLocalObjFuncGeoElements: the number of objFunc discrete elements for local procs
60  nLocalObjFuncGeoElements_ = objFuncFaceSources.size() + objFuncCellSources.size();
61 
62  // create dFdW
63  //MatCreate(PETSC_COMM_WORLD, jacMat);
64  MatSetSizes(
65  jacMat,
68  PETSC_DETERMINE,
69  PETSC_DETERMINE);
70  MatSetFromOptions(jacMat);
71  MatMPIAIJSetPreallocation(jacMat, 200, NULL, 200, NULL);
72  MatSeqAIJSetPreallocation(jacMat, 200, NULL);
73  //MatSetOption(jacMat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
74  MatSetUp(jacMat);
75  MatZeroEntries(jacMat);
76  Info << "Partial derivative matrix created. " << mesh_.time().elapsedClockTime() << " s" << endl;
77 }
78 
80  const dictionary& options,
81  const Vec xvVec,
82  const Vec wVec,
83  Mat jacMat)
84 {
85 
86  /*
87  Description:
88  Compute jacMat. We use coloring acclerated finite-difference for dFdW
89 
90  Input:
91 
92  options.objFuncSubDictPart: the objFunc subDict, obtained from DAOption
93 
94  options.objFuncName: the name of the objective
95 
96  options.objFuncPart: the part of the objective
97 
98  xvVec: the volume mesh coordinate vector
99 
100  wVec: the state variable vector
101 
102  Output:
103  jacMat: the partial derivative matrix dFdW to compute
104  */
105 
106  label transposed = 0;
107 
108  // initialize coloredColumn vector
109  Vec coloredColumn;
110  VecCreate(PETSC_COMM_WORLD, &coloredColumn);
111  VecSetSizes(coloredColumn, nLocalObjFuncGeoElements_, PETSC_DECIDE);
112  VecSetFromOptions(coloredColumn);
113  VecZeroEntries(coloredColumn);
114 
115  word objFuncName, objFuncPart;
116  dictionary objFuncSubDictPart = options.subDict("objFuncSubDictPart");
117  options.readEntry<word>("objFuncName", objFuncName);
118  options.readEntry<word>("objFuncPart", objFuncPart);
119 
120  autoPtr<DAObjFunc> daObjFunc(
122  mesh_,
123  daOption_,
124  daModel_,
125  daIndex_,
126  daResidual_,
127  objFuncName,
128  objFuncPart,
129  objFuncSubDictPart)
130  .ptr());
131 
132  // zero all the matrices
133  MatZeroEntries(jacMat);
134 
135  Vec wVecNew;
136  VecDuplicate(wVec, &wVecNew);
137  VecCopy(wVec, wVecNew);
138 
139  // initialize f vectors
140  Vec fVecRef, fVec;
141  VecDuplicate(coloredColumn, &fVec);
142  VecDuplicate(coloredColumn, &fVecRef);
143  VecZeroEntries(fVec);
144  VecZeroEntries(fVecRef);
145 
146  // set up state normalization vector
147  Vec normStatePerturbVec;
148  this->setNormStatePerturbVec(&normStatePerturbVec);
149 
150  dictionary mOptions;
151  mOptions.set("updateState", 1);
152  mOptions.set("updateMesh", 0);
153  daObjFunc->masterFunction(mOptions, xvVec, wVec);
154  const scalarList& objFuncFaceValues = daObjFunc->getObjFuncFaceValues();
155  const scalarList& objFuncCellValues = daObjFunc->getObjFuncCellValues();
156  daJacCon_.setObjFuncVec(objFuncFaceValues, objFuncCellValues, fVecRef);
157 
158  // NOTE: for some objectives, we need special treatment to compute
159  // reference coefficient based on unperturbed states, e.g., totalPressureRatio
160  // and totalTemperatureRatio. Once the refCoeffs is computed, we don't recompute
161  // them when perturbing states, so here we need to set daObjFunc->calcRefCoeffs = 0
162  // After the perturbation is done, we need to reset it to 1
163  daObjFunc->calcRefCoeffs = 0;
164 
165  // NOTE: for some objectives, we need to scale dFdW so we first fetch their
166  // scaling before perturbing W and after computing the reference objFunc
167  scalar scalingKS = 1.0;
168  PetscScalar scalingKSValue = 0.0;
169  if (objFuncSubDictPart.getWord("type") == "vonMisesStressKS")
170  {
171  scalar coeffKS = objFuncSubDictPart.getScalar("coeffKS");
172  // expSumKS should be computed by calling the above masterFunction
173  // based on unperturbed W
174  scalingKS = 1.0 / daObjFunc->expSumKS / coeffKS;
175  }
176  assignValueCheckAD(scalingKSValue, scalingKS);
177 
178  scalar delta = daOption_.getSubDictOption<scalar>("adjPartDerivFDStep", "State");
179  scalar rDelta = 1.0 / delta;
180  PetscScalar rDeltaValue = 0.0;
181  assignValueCheckAD(rDeltaValue, rDelta);
182 
183  label nColors = daJacCon_.getNJacConColors();
184 
185  label printInterval = daOption_.getOption<label>("printInterval");
186  for (label color = 0; color < nColors; color++)
187  {
188  label eTime = mesh_.time().elapsedClockTime();
189  // print progress
190  if (color % printInterval == 0 or color == nColors - 1)
191  {
192  Info << modelType_ << ": " << color << " of " << nColors
193  << ", ExecutionTime: " << eTime << " s" << endl;
194  }
195 
196  // perturb states
197  this->perturbStates(
199  normStatePerturbVec,
200  color,
201  delta,
202  wVecNew);
203 
204  // compute object
205  daObjFunc->masterFunction(mOptions, xvVec, wVecNew);
206  daJacCon_.setObjFuncVec(objFuncFaceValues, objFuncCellValues, fVec);
207 
208  // reset state perburbation
209  VecCopy(wVec, wVecNew);
210 
211  // compute residual partial using finite-difference
212  VecAXPY(fVec, -1.0, fVecRef);
213  VecScale(fVec, rDeltaValue);
214  // NOTE: need to further scale fVec by scalingKS for KS objectives
215  // If no KS objectives are used, scalingKS=1
216  VecScale(fVec, scalingKSValue);
217 
218  // compute the colored coloumn and assign resVec to jacMat
219  daJacCon_.calcColoredColumns(color, coloredColumn);
220  this->setPartDerivMat(fVec, coloredColumn, transposed, jacMat);
221  }
222 
223  // reset calcRefCoeffs to 1
224  daObjFunc->calcRefCoeffs = 1;
225 
226  // call the master function again to reset wVec to OpenFOAM fields
227  scalar fRef = daObjFunc->masterFunction(mOptions, xvVec, wVec);
228 
229  if (daOption_.getOption<label>("debug"))
230  {
231  Info << objFuncName << ": " << fRef << endl;
232  }
233 
234  MatAssemblyBegin(jacMat, MAT_FINAL_ASSEMBLY);
235  MatAssemblyEnd(jacMat, MAT_FINAL_ASSEMBLY);
236 }
237 
238 } // End namespace Foam
239 
240 // ************************************************************************* //
Foam::DAJacCon
Definition: DAJacCon.H:34
Foam::DAJacCon::calcColoredColumns
void calcColoredColumns(const label colorI, Vec coloredColumn) const
calculate the colored column vector
Definition: DAJacCon.C:1950
Foam::DAOption
Definition: DAOption.H:29
Foam::DAJacCon::setObjFuncVec
virtual void setObjFuncVec(scalarList objFuncFaceValues, scalarList objFuncCellValues, Vec objFuncVec) const
assign values for the objective function vector based on the face and cell value lists
Definition: DAJacCon.C:1932
Foam::DAPartDeriv::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAPartDeriv.H:60
daOption
DAOption daOption(mesh, pyOptions_)
Foam::DAPartDeriv::setPartDerivMat
void setPartDerivMat(const Vec resVec, const Vec coloredColumn, const label transposed, Mat jacMat, const scalar jacLowerBound=1e-30) const
set values for the partial derivative matrix
Definition: DAPartDeriv.C:165
Foam::DAPartDeriv::daJacCon_
const DAJacCon & daJacCon_
DAJacCon object.
Definition: DAPartDeriv.H:63
Foam::DAJacCon::getJacConColor
Vec getJacConColor() const
return DAJacCon::jacConColors_
Definition: DAJacCon.H:272
Foam::DAOption::getOption
classType getOption(const word key) const
get an option from subDict and key
Definition: DAOption.H:92
Foam::DAPartDeriv::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAPartDeriv.H:54
Foam::DAJacCon::getNJacConColors
label getNJacConColors() const
get the number of JacCon colors
Definition: DAJacCon.H:252
Foam::DAPartDeriv::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAPartDeriv.H:51
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAOption::getSubDictOption
classType getSubDictOption(const word subDict, const word subDictKey) const
get an dictionary option from subDict and key
Definition: DAOption.H:165
Foam::DAIndex
Definition: DAIndex.H:32
assignValueCheckAD
#define assignValueCheckAD(valA, valB)
Definition: DAMacroFunctions.H:103
Foam::DAModel
Definition: DAModel.H:59
Foam
Definition: multiFreqScalarFvPatchField.C:144
DAPartDerivdFdW.H
Foam::DAObjFunc::New
static autoPtr< DAObjFunc > New(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAResidual &daResidual, const word objFuncName, const word objFuncPart, const dictionary &objFuncDict)
Definition: DAObjFunc.C:62
Foam::DAResidual
Definition: DAResidual.H:35
Foam::DAPartDerivdFdW::nLocalObjFuncGeoElements_
label nLocalObjFuncGeoElements_
the number of objFunc discrete elements for local procs
Definition: DAPartDerivdFdW.H:33
Foam::DAPartDerivdFdW::calcPartDerivMat
virtual void calcPartDerivMat(const dictionary &options, const Vec xvVec, const Vec wVec, Mat jacMat)
compute the partial derivative matrix
Definition: DAPartDerivdFdW.C:79
Foam::DAPartDerivdFdW::initializePartDerivMat
virtual void initializePartDerivMat(const dictionary &options, Mat jacMat)
initialize partial derivative matrix
Definition: DAPartDerivdFdW.C:40
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
Foam::DAPartDeriv::modelType_
const word modelType_
the name of the jacCon matrix
Definition: DAPartDeriv.H:48
daModel
DAModel daModel(mesh, daOption)
Foam::DAPartDeriv
Definition: DAPartDeriv.H:36
Foam::DAPartDeriv::daResidual_
const DAResidual & daResidual_
DAResidual object.
Definition: DAPartDeriv.H:66
Foam::DAPartDeriv::perturbStates
void perturbStates(const Vec jacConColors, const Vec normStatePerturbVec, const label colorI, const scalar delta, Vec wVec)
perturb state variables given a color index
Definition: DAPartDeriv.C:98
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAPartDeriv::setNormStatePerturbVec
void setNormStatePerturbVec(Vec *normStatePerturbVec)
setup the state normalization vector
Definition: DAPartDeriv.C:511
Foam::DAPartDerivdFdW::DAPartDerivdFdW
DAPartDerivdFdW(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAJacCon &daJacCon, const DAResidual &daResidual)
Definition: DAPartDerivdFdW.C:19
Foam::DAIndex::nLocalAdjointStates
label nLocalAdjointStates
number of local adjoint states (including all cells and faces)
Definition: DAIndex.H:145
Foam::DAPartDeriv::daModel_
const DAModel & daModel_
DAModel object.
Definition: DAPartDeriv.H:57