DAPartDerivdFdBC.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAPartDerivdFdBC.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAPartDerivdFdBC, 0);
16 addToRunTimeSelectionTable(DAPartDeriv, DAPartDerivdFdBC, 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. This is not used
50  */
51 
52  // create dFdBC
53  //MatCreate(PETSC_COMM_WORLD, jacMat);
54  MatSetSizes(
55  jacMat,
56  PETSC_DECIDE,
57  PETSC_DECIDE,
58  1,
59  1);
60  MatSetFromOptions(jacMat);
61  MatMPIAIJSetPreallocation(jacMat, 1, NULL, 1, NULL);
62  MatSeqAIJSetPreallocation(jacMat, 1, NULL);
63  //MatSetOption(jacMat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
64  MatSetUp(jacMat);
65  MatZeroEntries(jacMat);
66  Info << "Partial derivative matrix created. " << mesh_.time().elapsedClockTime() << " s" << endl;
67 }
68 
70  const dictionary& options,
71  const Vec xvVec,
72  const Vec wVec,
73  Mat jacMat)
74 {
75  /*
76  Description:
77  Compute jacMat. Note for dFdBC, we have only one column so we can do brute
78  force finite-difference there is no need to do coloring
79 
80  Input:
81  options.objFuncSubDictPart: the objFunc subDict, obtained from DAOption
82 
83  options.objFuncName: the name of the objective
84 
85  options.objFuncPart: the part of the objective
86 
87  xvVec: the volume mesh coordinate vector
88 
89  wVec: the state variable vector
90 
91  Output:
92  jacMat: the partial derivative matrix dFdBC to compute
93  */
94 
95  word objFuncName, objFuncPart;
96  dictionary objFuncSubDictPart = options.subDict("objFuncSubDictPart");
97  options.readEntry<word>("objFuncName", objFuncName);
98  options.readEntry<word>("objFuncPart", objFuncPart);
99 
100  autoPtr<DAObjFunc> daObjFunc(
102  mesh_,
103  daOption_,
104  daModel_,
105  daIndex_,
106  daResidual_,
107  objFuncName,
108  objFuncPart,
109  objFuncSubDictPart)
110  .ptr());
111 
112  // zero all the matrices
113  MatZeroEntries(jacMat);
114 
115  dictionary mOptions;
116  mOptions.set("updateState", 1);
117  mOptions.set("updateMesh", 0);
118  scalar fRef = daObjFunc->masterFunction(mOptions, xvVec, wVec);
119 
120  scalar delta = daOption_.getSubDictOption<scalar>("adjPartDerivFDStep", "BC");
121  scalar rDelta = 1.0 / delta;
122 
123  // perturb BC
124  this->perturbBC(options, delta);
125 
126  // compute object
127  scalar fNew = daObjFunc->masterFunction(mOptions, xvVec, wVec);
128 
129  scalar partDeriv = (fNew - fRef) * rDelta;
130  PetscScalar partDerivValue = 0.0;
131  assignValueCheckAD(partDerivValue, partDeriv);
132 
133  MatSetValue(jacMat, 0, 0, partDerivValue, INSERT_VALUES);
134 
135  // reset perturbation
136  this->perturbBC(options, -1.0 * delta);
137  // call masterFunction again to reset the wVec to OpenFOAM field
138  fRef = daObjFunc->masterFunction(mOptions, xvVec, wVec);
139 
140  if (daOption_.getOption<label>("debug"))
141  {
142  Info << objFuncName << ": " << fRef << endl;
143  }
144 
145  MatAssemblyBegin(jacMat, MAT_FINAL_ASSEMBLY);
146  MatAssemblyEnd(jacMat, MAT_FINAL_ASSEMBLY);
147 }
148 
149 } // End namespace Foam
150 
151 // ************************************************************************* //
Foam::DAJacCon
Definition: DAJacCon.H:34
Foam::DAOption
Definition: DAOption.H:29
Foam::DAPartDeriv::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAPartDeriv.H:60
daOption
DAOption daOption(mesh, pyOptions_)
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::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
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
DAPartDerivdFdBC.H
Foam::DAResidual
Definition: DAResidual.H:35
Foam::DAPartDeriv::perturbBC
void perturbBC(const dictionary options, const scalar delta)
perturb the values in the boundary condition
Definition: DAPartDeriv.C:266
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::DAPartDerivdFdBC::calcPartDerivMat
virtual void calcPartDerivMat(const dictionary &options, const Vec xvVec, const Vec wVec, Mat jacMat)
compute the partial derivative matrix
Definition: DAPartDerivdFdBC.C:69
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
daModel
DAModel daModel(mesh, daOption)
Foam::DAPartDeriv
Definition: DAPartDeriv.H:36
Foam::DAPartDeriv::daResidual_
const DAResidual & daResidual_
DAResidual object.
Definition: DAPartDeriv.H:66
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAPartDerivdFdBC::initializePartDerivMat
virtual void initializePartDerivMat(const dictionary &options, Mat jacMat)
initialize partial derivative matrix
Definition: DAPartDerivdFdBC.C:40
Foam::DAPartDerivdFdBC::DAPartDerivdFdBC
DAPartDerivdFdBC(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAJacCon &daJacCon, const DAResidual &daResidual)
Definition: DAPartDerivdFdBC.C:19
Foam::DAPartDeriv::daModel_
const DAModel & daModel_
DAModel object.
Definition: DAPartDeriv.H:57