DAPartDerivdFdACTD.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAPartDerivdFdACTD.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAPartDerivdFdACTD, 0);
16 addToRunTimeSelectionTable(DAPartDeriv, DAPartDerivdFdACTD, 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 dFdACTD
53  //MatCreate(PETSC_COMM_WORLD, jacMat);
54  MatSetSizes(
55  jacMat,
56  PETSC_DECIDE,
57  PETSC_DECIDE,
58  nActDVs_,
59  1
60  );
61  MatSetFromOptions(jacMat);
62  MatMPIAIJSetPreallocation(jacMat, 1, NULL, 1, NULL);
63  MatSeqAIJSetPreallocation(jacMat, 1, NULL);
64  //MatSetOption(jacMat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
65  MatSetUp(jacMat);
66  MatZeroEntries(jacMat);
67  Info << "Partial derivative matrix created. " << mesh_.time().elapsedClockTime() << " s" << endl;
68 }
69 
71  const dictionary& options,
72  const Vec xvVec,
73  const Vec wVec,
74  Mat jacMat)
75 {
76  /*
77  Description:
78  Compute jacMat. Note for dFdACTD, we have only one column so we can do brute
79  force finite-difference there is no need to do coloring
80 
81  Input:
82  options.objFuncSubDictPart: the objFunc subDict, obtained from DAOption
83 
84  options.objFuncName: the name of the objective
85 
86  options.objFuncPart: the part of the objective
87 
88  xvVec: the volume mesh coordinate vector
89 
90  wVec: the state variable vector
91 
92  Output:
93  jacMat: the partial derivative matrix dFdACTD to compute
94  */
95 
96  word objFuncName, objFuncPart;
97  dictionary objFuncSubDictPart = options.subDict("objFuncSubDictPart");
98  options.readEntry<word>("objFuncName", objFuncName);
99  options.readEntry<word>("objFuncPart", objFuncPart);
100 
101  autoPtr<DAObjFunc> daObjFunc(
103  mesh_,
104  daOption_,
105  daModel_,
106  daIndex_,
107  daResidual_,
108  objFuncName,
109  objFuncPart,
110  objFuncSubDictPart)
111  .ptr());
112 
113  // zero all the matrices
114  MatZeroEntries(jacMat);
115 
116  word actuatorName = options.getWord("actuatorName");
117 
118  dictionary mOptions;
119  mOptions.set("updateState", 1);
120  mOptions.set("updateMesh", 0);
121  scalar fRef = daObjFunc->masterFunction(mOptions, xvVec, wVec);
122 
123  scalar delta = daOption_.getSubDictOption<scalar>("adjPartDerivFDStep", "ACTD");
124  scalar rDelta = 1.0 / delta;
125 
126  dictionary& diskModelSubDict = const_cast<dictionary&>(
127  daOption_.getAllOptions().subDict("fvSource").subDict(actuatorName));
128 
129  scalarList center;
130  diskModelSubDict.readEntry<scalarList>("center", center);
131  scalar innerRadius = diskModelSubDict.getScalar("innerRadius");
132  scalar outerRadius = diskModelSubDict.getScalar("outerRadius");
133  scalar POD = diskModelSubDict.getScalar("POD");
134  scalar scale = diskModelSubDict.getScalar("scale");
135  scalar expM = diskModelSubDict.getScalar("expM");
136  scalar expN = diskModelSubDict.getScalar("expN");
137  scalar targetThrust = diskModelSubDict.getScalar("targetThrust");
138 
139  DAFvSource& fvSource = const_cast<DAFvSource&>(
140  mesh_.thisDb().lookupObject<DAFvSource>("DAFvSource"));
141 
142  for (label i = 0; i < nActDVs_; i++)
143  {
144  // perturb ACTD
145  if (i == 0)
146  {
147  // perturb x
148  center[0] += delta;
149  diskModelSubDict.set("center", center);
150  }
151  else if (i == 1)
152  {
153  // perturb y
154  center[1] += delta;
155  diskModelSubDict.set("center", center);
156  }
157  else if (i == 2)
158  {
159  // perturb z
160  center[2] += delta;
161  diskModelSubDict.set("center", center);
162  }
163  else if (i == 3)
164  {
165  // perturb innerRadius
166  innerRadius += delta;
167  diskModelSubDict.set("innerRadius", innerRadius);
168  }
169  else if (i == 4)
170  {
171  // perturb outerRadius
172  outerRadius += delta;
173  diskModelSubDict.set("outerRadius", outerRadius);
174  }
175  else if (i == 5)
176  {
177  // perturb scale
178  scale += delta;
179  diskModelSubDict.set("scale", scale);
180  }
181  else if (i == 6)
182  {
183  // perturb POD
184  POD += delta;
185  diskModelSubDict.set("POD", POD);
186  }
187  else if (i == 7)
188  {
189  // perturb expM
190  expM += delta;
191  diskModelSubDict.set("expM", expM);
192  }
193  else if (i == 8)
194  {
195  // perturb expN
196  expN += delta;
197  diskModelSubDict.set("expN", expN);
198  }
199  else if (i == 9)
200  {
201  targetThrust += delta;
202  diskModelSubDict.set("targetThrust", targetThrust);
203  }
204 
205  // we need to synchronize the DAOption to actuatorDVs
206  fvSource.syncDAOptionToActuatorDVs();
207  fvSource.updateFvSource();
208 
209  // compute object
210  scalar fNew = daObjFunc->masterFunction(mOptions, xvVec, wVec);
211 
212  // reset perturbation
213  if (i == 0)
214  {
215  // reset x
216  center[0] -= delta;
217  diskModelSubDict.set("center", center);
218  }
219  else if (i == 1)
220  {
221  // reset y
222  center[1] -= delta;
223  diskModelSubDict.set("center", center);
224  }
225  else if (i == 2)
226  {
227  // reset z
228  center[2] -= delta;
229  diskModelSubDict.set("center", center);
230  }
231  else if (i == 3)
232  {
233  // reset innerRadius
234  innerRadius -= delta;
235  diskModelSubDict.set("innerRadius", innerRadius);
236  }
237  else if (i == 4)
238  {
239  // reset outerRadius
240  outerRadius -= delta;
241  diskModelSubDict.set("outerRadius", outerRadius);
242  }
243  else if (i == 5)
244  {
245  // reset scale
246  scale -= delta;
247  diskModelSubDict.set("scale", scale);
248  }
249  else if (i == 6)
250  {
251  // reset POD
252  POD -= delta;
253  diskModelSubDict.set("POD", POD);
254  }
255  else if (i == 7)
256  {
257  // reset expM
258  expM -= delta;
259  diskModelSubDict.set("expM", expM);
260  }
261  else if (i == 8)
262  {
263  // reset expN
264  expN -= delta;
265  diskModelSubDict.set("expN", expN);
266  }
267  else if (i == 9)
268  {
269  targetThrust -= delta;
270  diskModelSubDict.set("targetThrust", targetThrust);
271  }
272 
273  // we need to synchronize the DAOption to actuatorDVs
274  fvSource.syncDAOptionToActuatorDVs();
275  fvSource.updateFvSource();
276 
277  scalar partDeriv = (fNew - fRef) * rDelta;
278  PetscScalar partDerivValue = 0.0;
279  assignValueCheckAD(partDerivValue, partDeriv);
280 
281  MatSetValue(jacMat, i, 0, partDerivValue, INSERT_VALUES);
282 
283  }
284 
285  // call masterFunction again to reset the wVec to OpenFOAM field
286  fRef = daObjFunc->masterFunction(mOptions, xvVec, wVec);
287 
288  if (daOption_.getOption<label>("debug"))
289  {
290  Info << objFuncName << ": " << fRef << endl;
291  }
292 
293  MatAssemblyBegin(jacMat, MAT_FINAL_ASSEMBLY);
294  MatAssemblyEnd(jacMat, MAT_FINAL_ASSEMBLY);
295 }
296 
297 } // End namespace Foam
298 
299 // ************************************************************************* //
Foam::DAFvSource
Definition: DAFvSource.H:34
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::DAPartDerivdFdACTD::DAPartDerivdFdACTD
DAPartDerivdFdACTD(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAJacCon &daJacCon, const DAResidual &daResidual)
Definition: DAPartDerivdFdACTD.C:19
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
fvSource
volScalarField & fvSource
Definition: createRefsHeatTransfer.H:7
Foam::DAPartDeriv::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAPartDeriv.H:51
Foam::DAOption::getAllOptions
const dictionary & getAllOptions() const
return a reference of allOptions_ dictionary
Definition: DAOption.H:56
Foam::DAPartDerivdFdACTD::calcPartDerivMat
virtual void calcPartDerivMat(const dictionary &options, const Vec xvVec, const Vec wVec, Mat jacMat)
compute the partial derivative matrix
Definition: DAPartDerivdFdACTD.C:70
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::DAPartDerivdFdACTD::initializePartDerivMat
virtual void initializePartDerivMat(const dictionary &options, Mat jacMat)
initialize partial derivative matrix
Definition: DAPartDerivdFdACTD.C:40
Foam::DAIndex
Definition: DAIndex.H:32
assignValueCheckAD
#define assignValueCheckAD(valA, valB)
Definition: DAMacroFunctions.H:103
DAPartDerivdFdACTD.H
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
Foam::DAResidual
Definition: DAResidual.H:35
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
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::DAPartDerivdFdACTD::nActDVs_
label nActDVs_
number of design variables for actuator disk
Definition: DAPartDerivdFdACTD.H:33
Foam::DAPartDeriv::daModel_
const DAModel & daModel_
DAModel object.
Definition: DAPartDeriv.H:57