DAJacCondFdW.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAJacCondFdW.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAJacCondFdW, 0);
16 addToRunTimeSelectionTable(DAJacCon, DAJacCondFdW, 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  : DAJacCon(modelType, mesh, daOption, daModel, daIndex)
26 {
27  // NOTE: we need to initialize petsc vectors and setup state boundary con
28  this->initializePetscVecs();
30 }
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
36 {
37  /*
38  Description:
39  Clear all members to avoid memory leak because we will initalize
40  multiple objects of DAJacCon. Here we need to delete all members
41  in the parent and child classes
42  */
44  this->clearDAJacConMembers();
45 }
46 
48 {
49  /*
50  Description:
51  Initialize jacConColors_
52  */
53 
54  // dFdW Colors the jacConColoredColumn will be initialized in
55  // DAJacCondFdW::initializeJacCon
56  VecCreate(PETSC_COMM_WORLD, &jacConColors_);
57  VecSetSizes(jacConColors_, daIndex_.nLocalAdjointStates, PETSC_DECIDE);
58  VecSetFromOptions(jacConColors_);
59 
60  return;
61 }
62 
63 void DAJacCondFdW::initializeJacCon(const dictionary& options)
64 {
65  /*
66  Description:
67  Initialize the connectivity matrix
68 
69  Input:
70  options.objFuncFaceSources: a labelList that contains all the
71  face indices for the objective
72 
73  options.objFuncCellSources: a labelList that contains all the
74  cell indices for the objective
75 
76  Output:
77  jacCon_: connectivity matrix for dFdW, here dFdWCon has a
78  size of nLocalObjFuncGeoElements * nGlobalAdjointStates
79  The reason that dFdWCon has nLocalObjFuncGeoElements rows is
80  because we need to divide the objective function into
81  nLocalObjFuncGeoElements discrete value such that we can
82  use coloring to compute dFdW
83  */
84 
85  labelList objFuncFaceSources;
86  labelList objFuncCellSources;
87  options.readEntry<labelList>("objFuncFaceSources", objFuncFaceSources);
88  options.readEntry<labelList>("objFuncCellSources", objFuncCellSources);
89 
90  objFuncFaceSize_ = objFuncFaceSources.size();
91  objFuncCellSize_ = objFuncCellSources.size();
92 
93  // nLocalObjFuncGeoElements: the number of objFunc discrete elements for local procs
94  label nLocalObjFuncGeoElements = objFuncFaceSize_ + objFuncCellSize_;
95 
96  globalObjFuncGeoNumbering_ = DAUtility::genGlobalIndex(nLocalObjFuncGeoElements);
97 
98  MatCreate(PETSC_COMM_WORLD, &jacCon_);
99  MatSetSizes(
100  jacCon_,
101  nLocalObjFuncGeoElements,
103  PETSC_DETERMINE,
104  PETSC_DETERMINE);
105  MatSetFromOptions(jacCon_);
106  MatMPIAIJSetPreallocation(jacCon_, 200, NULL, 200, NULL);
107  MatSeqAIJSetPreallocation(jacCon_, 200, NULL);
108  //MatSetOption(jacCon_, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
109  MatSetUp(jacCon_);
110  MatZeroEntries(jacCon_);
111 
112  Info << "dFdWCon Created!" << endl;
113 }
114 
115 void DAJacCondFdW::setupJacCon(const dictionary& options)
116 {
117  /*
118  Description:
119  Setup the jacCon_ matrix
120 
121  Input:
122  options.objFuncConInfo: the connectivity information for
123  this objective function, usually obtained from Foam::DAObjFunc
124 
125  options.objFuncFaceSources: a labelList that contains all the
126  face indices for the objective, usually obtained from Foam::DAObjFunc
127 
128  options.objFuncCellSources: a labelList that contains all the
129  cell indices for the objective, usually obtained from Foam::DAObjFunc
130  */
131 
132  Info << "Setting up dFdWCon.." << endl;
133 
134  MatZeroEntries(jacCon_);
135 
136  Mat connectedStatesP;
137 
138  List<List<word>> objFuncConInfo;
139  labelList objFuncFaceSources;
140  labelList objFuncCellSources;
141  options.readEntry<List<List<word>>>("objFuncConInfo", objFuncConInfo);
142  options.readEntry<labelList>("objFuncFaceSources", objFuncFaceSources);
143  options.readEntry<labelList>("objFuncCellSources", objFuncCellSources);
144 
145  // maximal connectivity level information
146  label maxConLevel = objFuncConInfo.size() - 1;
147 
148  forAll(objFuncFaceSources, idxI)
149  {
150 
151  const label& objFuncFaceI = objFuncFaceSources[idxI];
152  label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
153  const label patchI = daIndex_.bFacePatchI[bFaceI];
154  const label faceI = daIndex_.bFaceFaceI[bFaceI];
155 
156  // create a shorter handle for the boundary patch
157  const fvPatch& patch = mesh_.boundary()[patchI];
158  // get the cells associated with this boundary patch
159  const UList<label>& pFaceCells = patch.faceCells();
160 
161  // Now get the cell that borders this face
162  label idxN = pFaceCells[faceI];
163 
164  //zero the connections
165  this->createConnectionMat(&connectedStatesP);
166 
167  forAll(objFuncConInfo, idxJ) // idxJ: con level
168  {
169  // set connectedStatesLocal: the locally connected state variables for this level
170  wordList connectedStatesLocal(0);
171  forAll(objFuncConInfo[idxJ], idxK)
172  {
173  word conName = objFuncConInfo[idxJ][idxK];
174  // Exclude surfaceScalarState when appending connectedStatesLocal
175  // whether to add it depends on addFace parameter
176  if (daIndex_.adjStateType[conName] != "surfaceScalarState")
177  {
178  connectedStatesLocal.append(conName);
179  }
180  }
181 
182  // set connectedStatesInterProc: the globally connected state variables for this level
183  List<List<word>> connectedStatesInterProc;
184 
185  if (idxJ == 0)
186  {
187  // pass a zero list, no need to add interProc connecitivity for level 0
188  connectedStatesInterProc.setSize(0);
189  }
190  else if (idxJ != maxConLevel)
191  {
192  connectedStatesInterProc.setSize(maxConLevel - idxJ + 1);
193  for (label k = 0; k < maxConLevel - idxJ + 1; k++)
194  {
195  label conSize = objFuncConInfo[k + idxJ].size();
196  for (label l = 0; l < conSize; l++)
197  {
198  word conName = objFuncConInfo[k + idxJ][l];
199  // Exclude surfaceScalarState when appending connectedStatesLocal
200  // whether to add it depends on addFace parameter
201  if (daIndex_.adjStateType[conName] != "surfaceScalarState")
202  {
203  connectedStatesInterProc[k].append(conName);
204  }
205  }
206  }
207  }
208  else
209  {
210  connectedStatesInterProc.setSize(1);
211  label conSize = objFuncConInfo[maxConLevel].size();
212  for (label l = 0; l < conSize; l++)
213  {
214  word conName = objFuncConInfo[maxConLevel][l];
215  // Exclude surfaceScalarState when appending connectedStatesLocal
216  // whether to add it depends on addFace parameter
217  if (daIndex_.adjStateType[conName] != "surfaceScalarState")
218  {
219  connectedStatesInterProc[0].append(conName);
220  }
221  }
222  }
223 
224  // check if we need to add face
225  label addFace = 0;
226  forAll(stateInfo_["surfaceScalarStates"], idxK)
227  {
228  word conName = stateInfo_["surfaceScalarStates"][idxK];
229  if (DAUtility::isInList<word>(conName, objFuncConInfo[idxJ]))
230  {
231  addFace = 1;
232  }
233  }
234 
235  this->addStateConnections(
236  connectedStatesP,
237  idxN,
238  idxJ,
239  connectedStatesLocal,
240  connectedStatesInterProc,
241  addFace);
242  }
243 
244  label glbRowI = this->getGlobalObjFuncGeoIndex("face", idxI);
245 
246  this->setupJacobianConnections(jacCon_, connectedStatesP, glbRowI);
247  }
248 
249  forAll(objFuncCellSources, idxI)
250  {
251  label cellI = objFuncCellSources[idxI];
252 
253  //zero the connections
254  this->createConnectionMat(&connectedStatesP);
255 
256  forAll(objFuncConInfo, idxJ) // idxJ: con level
257  {
258  // set connectedStatesLocal: the locally connected state variables for this level
259  wordList connectedStatesLocal = objFuncConInfo[idxJ];
260 
261  // set connectedStatesInterProc: the globally connected state variables for this level
262  List<List<word>> connectedStatesInterProc;
263 
264  if (idxJ == 0)
265  {
266  // pass a zero list, no need to add interProc connecitivity for level 0
267  connectedStatesInterProc.setSize(0);
268  }
269  else if (idxJ != maxConLevel)
270  {
271  connectedStatesInterProc.setSize(maxConLevel - idxJ + 1);
272  for (label k = 0; k < maxConLevel - idxJ + 1; k++)
273  {
274  connectedStatesInterProc[k] = objFuncConInfo[k + idxJ];
275  }
276  }
277  else
278  {
279  connectedStatesInterProc.setSize(1);
280  connectedStatesInterProc[0] = objFuncConInfo[maxConLevel];
281  }
282 
283  // check if we need to add face
284  label addFace = 0;
285  forAll(stateInfo_["surfaceScalarStates"], idxK)
286  {
287  word conName = stateInfo_["surfaceScalarStates"][idxK];
288  if (DAUtility::isInList<word>(conName, objFuncConInfo[idxJ]))
289  {
290  addFace = 1;
291  }
292  }
293 
294  this->addStateConnections(
295  connectedStatesP,
296  cellI,
297  idxJ,
298  connectedStatesLocal,
299  connectedStatesInterProc,
300  addFace);
301  }
302 
303  label glbRowI = this->getGlobalObjFuncGeoIndex("cell", idxI);
304 
305  this->setupJacobianConnections(jacCon_, connectedStatesP, glbRowI);
306  }
307 
308  MatAssemblyBegin(jacCon_, MAT_FINAL_ASSEMBLY);
309  MatAssemblyEnd(jacCon_, MAT_FINAL_ASSEMBLY);
310 
311  // output the matrix to a file
312  wordList writeJacobians;
313  daOption_.getAllOptions().readEntry<wordList>("writeJacobians", writeJacobians);
314  if (writeJacobians.found("dFdWCon"))
315  {
317  }
318 }
319 
321  const word idxType,
322  const label idxI) const
323 {
324  /*
325  Description:
326  Get the local index of a geometry element of an objective
327 
328  Input:
329  idxType: face means it is a faceSource and cell means it is a cellSource
330 
331  idxI: the index of the face or cell source
332 
333  Output:
334  The local index
335 
336  Example:
337  To enable coloring, we divide the objective into discrete face/cells.
338  So the number of local rows in DAJacCon::jacCon_ is nLocalObjFuncGeoElements
339 
340  If the faceSource and cellSource on proc0 read
341  objFuncFaceSources_ = {11, 12}; <---- these are the indices of the faces for the objective
342  objFuncCellSources_ = {23, 24, 25};
343 
344  and the faceSource and cellSource on proc1 read
345  objFuncFaceSources_ = {21, 22};
346  objFuncCellSources_ = {33, 34, 35};
347 
348  Then, globalObjFuncGeoNumbering_ reads:
349 
350  globalObjFuncGeoNumbering: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }
351  Local face/cell Indices: 11 12 23 24 25 | 21 22 33 34 35
352  ------- proc 0 -----|------- proc 1 -----
353  */
354 
355  label localIdx = -9999;
356  if (idxType == "face")
357  {
358  localIdx = idxI;
359  }
360  else if (idxType == "cell")
361  {
362  localIdx = objFuncFaceSize_ + idxI;
363  }
364  else
365  {
366  FatalErrorIn("") << "idxType: " << idxType << "not supported!"
367  << abort(FatalError);
368  }
369  return localIdx;
370 }
371 
373  const word idxType,
374  const label idxI) const
375 {
376  /*
377  Description:
378  Get the global index of a geometry element of an objective
379 
380  Input:
381  idxType: face means it is a faceSource and cell means it is a cellSource
382 
383  idxI: the index of the face or cell source
384 
385  Output:
386  The global index
387 
388  Example:
389  To enable coloring, we divide the objective into discrete face/cells.
390  So the number of local rows in DAJacCon::jacCon_ is nLocalObjFuncGeoElements
391 
392  If the faceSource and cellSource on proc0 read
393  objFuncFaceSources_ = {11, 12}; <---- these are the indices of the faces for the objective
394  objFuncCellSources_ = {23, 24, 25};
395 
396  and the faceSource and cellSource on proc1 read
397  objFuncFaceSources_ = {21, 22};
398  objFuncCellSources_ = {33, 34, 35};
399 
400  Then, globalObjFuncGeoNumbering_ reads:
401 
402  globalObjFuncGeoNumbering: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }
403  Local face/cell Indices: 11 12 23 24 25 | 21 22 33 34 35
404  ------- proc 0 -----|------- proc 1 -----
405  */
406  label localIdx = this->getLocalObjFuncGeoIndex(idxType, idxI);
407 
408  return globalObjFuncGeoNumbering_.toGlobal(localIdx);
409 }
410 
412  scalarList objFuncFaceValues,
413  scalarList objFuncCellValues,
414  Vec objFuncVec) const
415 {
416  /*
417  Description:
418  Assign the values from objFuncFaceValues and objFuncCellValues
419  to the objFuncVec
420 
421  Input:
422  objFuncFaceValues, objFuncCellValues: these two lists are computed
423  by calling DAObjFunc::calcObjFunc or DAObjFunc::getObjFuncValue
424 
425  Output:
426  objFuncVec: a vector that contains all the discret values from all processors
427  It will be primarily used in Foam::DAPartDerivdFdW
428  */
429  PetscScalar* objFuncVecArray;
430  VecGetArray(objFuncVec, &objFuncVecArray);
431 
432  forAll(objFuncFaceValues, idxI)
433  {
434  label localIdx = getLocalObjFuncGeoIndex("face", idxI);
435  assignValueCheckAD(objFuncVecArray[localIdx], objFuncFaceValues[idxI]);
436  }
437 
438  forAll(objFuncCellValues, idxI)
439  {
440  label localIdx = getLocalObjFuncGeoIndex("cell", idxI);
441  assignValueCheckAD(objFuncVecArray[localIdx], objFuncCellValues[idxI]);
442  }
443 
444  VecRestoreArray(objFuncVec, &objFuncVecArray);
445 }
446 
447 } // End namespace Foam
448 
449 // ************************************************************************* //
Foam::DAJacCon
Definition: DAJacCon.H:34
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DAJacCon::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAJacCon.H:49
Foam::DAJacCon::jacCon_
Mat jacCon_
Jacobian connectivity mat.
Definition: DAJacCon.H:79
Foam::DAJacCondFdW::initializeJacCon
virtual void initializeJacCon(const dictionary &options)
Definition: DAJacCondFdW.C:63
Foam::DAIndex::bFacePatchI
labelList bFacePatchI
Definition: DAIndex.H:110
DAJacCondFdW.H
Foam::DAJacCon::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAJacCon.H:52
Foam::DAJacCon::clearDAJacConMembers
void clearDAJacConMembers()
clear members in DAJacCon
Definition: DAJacCon.H:164
Foam::DAOption
Definition: DAOption.H:29
daOption
DAOption daOption(mesh, pyOptions_)
Foam::DAIndex::adjStateType
HashTable< word > adjStateType
hash table of adjoint state types, e.g., volVectorState for a given state name
Definition: DAIndex.H:77
Foam::DAJacCon::initializeStateBoundaryCon
void initializeStateBoundaryCon()
initialize state boundary connection
Definition: DAJacCon.C:88
Foam::DAJacCon::setupJacobianConnections
void setupJacobianConnections(Mat conMat, Mat connections, const label idxI)
assign values in connections to a specific row idxI in conMat
Definition: DAJacCon.C:126
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::DAIndex
Definition: DAIndex.H:32
assignValueCheckAD
#define assignValueCheckAD(valA, valB)
Definition: DAMacroFunctions.H:103
Foam::DAJacCondFdW::globalObjFuncGeoNumbering_
globalIndex globalObjFuncGeoNumbering_
the global numbering for the discrete source of objective function
Definition: DAJacCondFdW.H:35
Foam::DAModel
Definition: DAModel.H:59
k
dimensionedScalar & k
Definition: createRefsHeatTransfer.H:6
Foam
Definition: multiFreqScalarFvPatchField.C:144
Foam::DAJacCondFdW::objFuncCellSize_
label objFuncCellSize_
the size of objFunc cells
Definition: DAJacCondFdW.H:51
Foam::DAJacCon::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAJacCon.H:58
Foam::DAIndex::nLocalInternalFaces
label nLocalInternalFaces
local internal face size
Definition: DAIndex.H:98
Foam::DAJacCondFdW::DAJacCondFdW
DAJacCondFdW(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAJacCondFdW.C:19
Foam::DAJacCondFdW::getLocalObjFuncGeoIndex
label getLocalObjFuncGeoIndex(const word idxType, const label idxI) const
get the local index of geometry element for objective
Definition: DAJacCondFdW.C:320
Foam::DAUtility::writeMatrixBinary
static void writeMatrixBinary(const Mat matIn, const word prefix)
write petsc matrix in binary format
Definition: DAUtility.C:355
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
Foam::DAJacCondFdW::setupJacCon
virtual void setupJacCon(const dictionary &options)
assign 1 to all non-zero elements for the Jacobian connecitivyt matrix
Definition: DAJacCondFdW.C:115
daModel
DAModel daModel(mesh, daOption)
Foam::DAJacCon::createConnectionMat
void createConnectionMat(Mat *connectedStates)
allocate connectedState matrix
Definition: DAJacCon.C:161
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAJacCon::jacConColors_
Vec jacConColors_
jacCon matrix colors
Definition: DAJacCon.H:82
Foam::DAUtility::genGlobalIndex
static globalIndex genGlobalIndex(const label localIndexSize)
generate global index numbering for local-global index transferring
Definition: DAUtility.C:599
Foam::DAJacCondFdW::clear
virtual void clear()
clear members in parent and child objects
Definition: DAJacCondFdW.C:35
Foam::DAJacCondFdW::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: DAJacCondFdW.C:411
Foam::DAJacCondFdW::getGlobalObjFuncGeoIndex
label getGlobalObjFuncGeoIndex(const word idxType, const label idxI) const
get the global index of geometry element for objective
Definition: DAJacCondFdW.C:372
Foam::DAJacCondFdW::objFuncFaceSize_
label objFuncFaceSize_
the size of objFunc faces
Definition: DAJacCondFdW.H:48
Foam::DAJacCon::addStateConnections
void addStateConnections(Mat connections, const label cellI, const label connectedLevelLocal, const wordList connectedStatesLocal, const List< List< word >> connectedStateInterProc, const label addFace)
a high-level function to add connected state column indices to the connectivity matrix
Definition: DAJacCon.C:188
Foam::DAIndex::bFaceFaceI
labelList bFaceFaceI
Definition: DAIndex.H:116
Foam::DAJacCon::stateInfo_
HashTable< wordList > stateInfo_
the regState_ list from DAStateInfo object
Definition: DAJacCon.H:67
Foam::DAIndex::nLocalAdjointStates
label nLocalAdjointStates
number of local adjoint states (including all cells and faces)
Definition: DAIndex.H:145
Foam::DAJacCondFdW::initializePetscVecs
void initializePetscVecs()
initialize petsc vectors
Definition: DAJacCondFdW.C:47