DAPartDerivdRdACTP.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAPartDerivdRdACTP.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAPartDerivdRdACTP, 0);
16 addToRunTimeSelectionTable(DAPartDeriv, DAPartDerivdRdACTP, 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 
49  // now initialize the memory for the jacobian itself
50  label localSize = daIndex_.nLocalAdjointStates;
51 
52  // create dRdACTPT
53  //MatCreate(PETSC_COMM_WORLD, jacMat);
54  MatSetSizes(
55  jacMat,
56  localSize,
57  PETSC_DECIDE,
58  PETSC_DETERMINE,
59  nActDVs_);
60  MatSetFromOptions(jacMat);
61  MatMPIAIJSetPreallocation(jacMat, nActDVs_, NULL, nActDVs_, NULL);
62  MatSeqAIJSetPreallocation(jacMat, nActDVs_, 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. We use brute-force finite-difference
78 
79  Input:
80 
81  options.isPC: whether to compute the jacMat for preconditioner
82 
83  xvVec: the volume mesh coordinate vector
84 
85  wVec: the state variable vector
86 
87  Output:
88  jacMat: the partial derivative matrix dRdACTP to compute
89  */
90 
91  word actuatorName = options.getWord("actuatorName");
92 
93  DAResidual& daResidual = const_cast<DAResidual&>(daResidual_);
94 
95  // zero all the matrices
96  MatZeroEntries(jacMat);
97 
98  // initialize residual vectors
99  Vec resVecRef, resVec;
100  VecDuplicate(wVec, &resVec);
101  VecDuplicate(wVec, &resVecRef);
102  VecZeroEntries(resVec);
103  VecZeroEntries(resVecRef);
104 
105  dictionary mOptions;
106  mOptions.set("updateState", 1);
107  mOptions.set("updateMesh", 0);
108  mOptions.set("setResVec", 1);
109  mOptions.set("isPC", options.getLabel("isPC"));
110  daResidual.masterFunction(mOptions, xvVec, wVec, resVecRef);
111 
112  scalar delta = daOption_.getSubDictOption<scalar>("adjPartDerivFDStep", "ACTP");
113  scalar rDelta = 1.0 / delta;
114  PetscScalar rDeltaValue = 0.0;
115  assignValueCheckAD(rDeltaValue, rDelta);
116 
117  Vec xvVecNew;
118  VecDuplicate(xvVec, &xvVecNew);
119  VecZeroEntries(xvVecNew);
120 
121  dictionary& pointModelSubDict = const_cast<dictionary&>(
122  daOption_.getAllOptions().subDict("fvSource").subDict(actuatorName));
123 
124  scalarList center, amplitude;
125  pointModelSubDict.readEntry<scalarList>("center", center);
126  pointModelSubDict.readEntry<scalarList>("amplitude", amplitude);
127  scalar periodicity = pointModelSubDict.getScalar("periodicity");
128  scalar phase = pointModelSubDict.getScalar("phase");
129  scalar scale = pointModelSubDict.getScalar("scale");
130 
131  for (label i = 0; i < nActDVs_; i++)
132  {
133 
134  // perturb ACTP
135  if (i == 0)
136  {
137  // perturb x
138  center[0] += delta;
139  pointModelSubDict.set("center", center);
140  }
141  else if (i == 1)
142  {
143  // perturb y
144  center[1] += delta;
145  pointModelSubDict.set("center", center);
146  }
147  else if (i == 2)
148  {
149  // perturb z
150  center[2] += delta;
151  pointModelSubDict.set("center", center);
152  }
153  else if (i == 3)
154  {
155  // perturb amplitude x
156  amplitude[0] += delta;
157  pointModelSubDict.set("amplitude", amplitude);
158  }
159  else if (i == 4)
160  {
161  // perturb amplitude y
162  amplitude[1] += delta;
163  pointModelSubDict.set("amplitude", amplitude);
164  }
165  else if (i == 5)
166  {
167  // perturb amplitude z
168  amplitude[2] += delta;
169  pointModelSubDict.set("amplitude", amplitude);
170  }
171  else if (i == 6)
172  {
173  // perturb periodicity
174  periodicity += delta;
175  pointModelSubDict.set("periodicity", periodicity);
176  }
177  else if (i == 7)
178  {
179  // perturb phase
180  phase += delta;
181  pointModelSubDict.set("phase", phase);
182  }
183  else if (i == 8)
184  {
185  // perturb scale
186  scale += delta;
187  pointModelSubDict.set("scale", scale);
188  }
189 
190  // Info<<daOption_.getAllOptions().subDict("fvSource")<<endl;
191 
192  // compute residual
193  daResidual.masterFunction(mOptions, xvVecNew, wVec, resVec);
194 
195  // reset perturbation
196  if (i == 0)
197  {
198  // reset x
199  center[0] -= delta;
200  pointModelSubDict.set("center", center);
201  }
202  else if (i == 1)
203  {
204  // reset y
205  center[1] -= delta;
206  pointModelSubDict.set("center", center);
207  }
208  else if (i == 2)
209  {
210  // reset z
211  center[2] -= delta;
212  pointModelSubDict.set("center", center);
213  }
214  else if (i == 3)
215  {
216  // reset amplitude x
217  amplitude[0] -= delta;
218  pointModelSubDict.set("amplitude", amplitude);
219  }
220  else if (i == 4)
221  {
222  // reset amplitude y
223  amplitude[1] -= delta;
224  pointModelSubDict.set("amplitude", amplitude);
225  }
226  else if (i == 5)
227  {
228  // reset amplitude z
229  amplitude[2] -= delta;
230  pointModelSubDict.set("amplitude", amplitude);
231  }
232  else if (i == 6)
233  {
234  // reset periodicity
235  periodicity -= delta;
236  pointModelSubDict.set("periodicity", periodicity);
237  }
238  else if (i == 7)
239  {
240  // reset phase
241  phase -= delta;
242  pointModelSubDict.set("phase", phase);
243  }
244  else if (i == 8)
245  {
246  // reset scale
247  scale -= delta;
248  pointModelSubDict.set("scale", scale);
249  }
250 
251  // Info<<daOption_.getAllOptions().subDict("fvSource")<<endl;
252 
253  // compute residual partial using finite-difference
254  VecAXPY(resVec, -1.0, resVecRef);
255  VecScale(resVec, rDeltaValue);
256 
257  // assign resVec to jacMat
258  PetscInt Istart, Iend;
259  VecGetOwnershipRange(resVec, &Istart, &Iend);
260 
261  const PetscScalar* resVecArray;
262  VecGetArrayRead(resVec, &resVecArray);
263  for (label j = Istart; j < Iend; j++)
264  {
265  label relIdx = j - Istart;
266  PetscScalar val = resVecArray[relIdx];
267  MatSetValue(jacMat, j, i, val, INSERT_VALUES);
268  }
269  VecRestoreArrayRead(resVec, &resVecArray);
270  }
271 
272  label eTime = mesh_.time().elapsedClockTime();
273  Info << modelType_ << " ExecutionTime: " << eTime << " s" << endl;
274 
275  // call the master function again to reset the xvVec and wVec to OpenFOAM fields and points
276  daResidual.masterFunction(mOptions, xvVec, wVec, resVecRef);
277 
278  MatAssemblyBegin(jacMat, MAT_FINAL_ASSEMBLY);
279  MatAssemblyEnd(jacMat, MAT_FINAL_ASSEMBLY);
280 }
281 
282 } // End namespace Foam
283 
284 // ************************************************************************* //
Foam::DAJacCon
Definition: DAJacCon.H:34
Foam::DAPartDerivdRdACTP::nActDVs_
label nActDVs_
number of design variables for actuator points
Definition: DAPartDerivdRdACTP.H:33
Foam::DAOption
Definition: DAOption.H:29
Foam::DAPartDeriv::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAPartDeriv.H:60
daOption
DAOption daOption(mesh, pyOptions_)
Foam::DAPartDeriv::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAPartDeriv.H:54
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
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::DAPartDerivdRdACTP::initializePartDerivMat
virtual void initializePartDerivMat(const dictionary &options, Mat jacMat)
initialize partial derivative matrix
Definition: DAPartDerivdRdACTP.C:40
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::DAResidual
Definition: DAResidual.H:35
Foam::DAPartDerivdRdACTP::DAPartDerivdRdACTP
DAPartDerivdRdACTP(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAJacCon &daJacCon, const DAResidual &daResidual)
Definition: DAPartDerivdRdACTP.C:19
Foam::DAResidual::masterFunction
void masterFunction(const dictionary &options, const Vec xvVec, const Vec wVec, Vec resVec)
the master function that compute the residual vector given the state and point vectors
Definition: DAResidual.C:81
Foam::DAPartDerivdRdACTP::calcPartDerivMat
virtual void calcPartDerivMat(const dictionary &options, const Vec xvVec, const Vec wVec, Mat jacMat)
compute the partial derivative matrix
Definition: DAPartDerivdRdACTP.C:69
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
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAIndex::nLocalAdjointStates
label nLocalAdjointStates
number of local adjoint states (including all cells and faces)
Definition: DAIndex.H:145
DAPartDerivdRdACTP.H