DAUtility.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6  Description:
7  DAUtility contains basic functions such as matrix file IO, and
8  it is independent of fvMesh. All the functions in DAUtility are static,
9  so they can be directly called, e.g., DAUtility::boundVar(...)
10 
11 \*---------------------------------------------------------------------------*/
12 
13 #ifndef DAUtility_H
14 #define DAUtility_H
15 
16 #include <petscksp.h>
17 #include "Python.h"
18 #include "fvOptions.H"
19 #include "globalIndex.H"
20 #include "IOMRFZoneListDF.H"
21 
22 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
23 
24 namespace Foam
25 {
26 
27 typedef void (*pyComputeInterface)(const double*, int, double*, int, void*);
28 typedef void (*pyJacVecProdInterface)(const double*, double*, int, const double*, const double*, int, void*);
29 
30 /*---------------------------------------------------------------------------*\
31  Class DAUtility Declaration
32 \*---------------------------------------------------------------------------*/
33 class DAUtility
34 {
35 
36 private:
38  DAUtility(const DAUtility&);
39 
41  void operator=(const DAUtility&);
42 
43 public:
45  DAUtility();
46 
48  virtual ~DAUtility();
49 
51  static void pyDict2OFDict(
52  PyObject* pyDict,
53  dictionary& ofDict);
54 
56  template<class classType>
57  static label isInList(
58  const classType value,
59  const List<classType>& list);
60 
62  template<class classType>
63  static label listReplaceVal(
64  List<classType>& listIn,
65  const classType valOrig,
66  const classType valNew);
67 
69  template<class classType>
70  static label listDeleteVal(
71  List<classType>& listIn,
72  const classType valDel);
73 
75  static void writeMatrixBinary(
76  const Mat matIn,
77  const word prefix);
78 
80  static void writeMatrixASCII(
81  const Mat matIn,
82  const word prefix);
83 
85  static void readMatrixBinary(
86  Mat matIn,
87  const word prefix);
88 
90  static void writeVectorASCII(
91  const Vec vecIn,
92  const word prefix);
93 
95  static void readVectorBinary(
96  Vec vecIn,
97  const word prefix);
98 
100  static void writeVectorBinary(
101  const Vec vecIn,
102  const word prefix);
103 
105  static void boundVar(
106  const dictionary& allOptions,
107  volScalarField& var,
108  const label printToScreen);
109 
111  static void boundVar(
112  const dictionary& allOptions,
113  volVectorField& var,
114  const label printToScreen);
115 
117  static label isValueCloseToRef(
118  const scalar val,
119  const scalar refVal,
120  const scalar tol = 1.0e-6);
121 
123  static globalIndex genGlobalIndex(const label localIndexSize);
124 
127 
129  static void* pyCalcBeta;
131 
132  static void* pyCalcBetaJacVecProd;
134 };
135 
136 template<class classType>
138  const classType value,
139  const List<classType>& list)
140 {
141  /*
142  Description:
143  Check whether a value is in the list
144 
145  Input:
146  value: a value to check with type: classType
147  list: list to check the value
148 
149  Example:
150  If the list reads
151  list1={"test","run","compare"};
152  label val=isInList<word>("run",list1);
153  Here the return value: val==1 (value found in the list)
154  */
155 
156  forAll(list, idxI)
157  {
158  if (list[idxI] == value)
159  {
160  return 1;
161  }
162  }
163  return 0;
164 }
165 
166 template<class classType>
168  List<classType>& listIn,
169  const classType valOrig,
170  const classType valNew)
171 {
172  /*
173  Description:
174  Replace a value in hte list, the list sequence is preserved
175 
176  Input:
177  listIn: the list to replace
178  valOrig: original value in the list
179  valNew: new value to replace
180 
181  Example:
182  List<word> listIn={"apple","orange"};
183  listReplaceVal<word>(listIn,"apple","banana");
184  Now listIn will be {"banana","orange"}
185  NOTE: if list has multiple valOrig, they will be all replaced
186  */
187 
188  label foundVal = 0;
189  forAll(listIn, idxI)
190  {
191  const classType& val = listIn[idxI];
192  if (val == valOrig)
193  {
194  listIn[idxI] = valNew;
195  foundVal = 1;
196  }
197  }
198 
199  return foundVal;
200 }
201 
202 template<class classType>
204  List<classType>& listIn,
205  const classType valDel)
206 {
207  /*
208  Description:
209  Delete a value in the list, the sequence of other
210  elements will be preserved
211 
212  Input:
213  listIn: the list to delete value
214  valDel: value to delete
215 
216  Output:
217  foundVal: 1 means the requested value is found and deleted
218 
219  Example:
220  List<word> listIn={"apple","orange"};
221  listDeleteVal<word>(listIn,"apple");
222  Now listIn will be {orange"}
223  NOTE: if list has multiple val, they will be all deleted
224  */
225 
226  // we will create a new list and delete the value
227  List<classType> listNew;
228 
229  label foundVal = 0;
230  forAll(listIn, idxI)
231  {
232  const classType& val = listIn[idxI];
233  if (val == valDel)
234  {
235  foundVal = 1;
236  // do not append
237  }
238  else
239  {
240  listNew.append(val);
241  }
242  }
243 
244  listIn.clear();
245  listIn = listNew;
246 
247  return foundVal;
248 }
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 } // End namespace Foam
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 #endif
257 
258 // ************************************************************************* //
Foam::DAUtility::readMatrixBinary
static void readMatrixBinary(Mat matIn, const word prefix)
read petsc matrix in binary format
Definition: DAUtility.C:322
Foam::DAUtility::listReplaceVal
static label listReplaceVal(List< classType > &listIn, const classType valOrig, const classType valNew)
replace a value in the list
Definition: DAUtility.H:167
allOptions
const dictionary & allOptions
Definition: createRefsRhoSimpleC.H:15
Foam::DAUtility::pyCalcBetaJacVecProdInterface
static pyJacVecProdInterface pyCalcBetaJacVecProdInterface
Definition: DAUtility.H:133
Foam::DAUtility::readVectorBinary
static void readVectorBinary(Vec vecIn, const word prefix)
read petsc vector in binary format
Definition: DAUtility.C:226
Foam::DAUtility::pyCalcBeta
static void * pyCalcBeta
define a function pointer template for Python call back
Definition: DAUtility.H:129
Foam::DAUtility::DAUtility
DAUtility()
Constructors.
Definition: DAUtility.C:16
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DAUtility::pyCalcBetaInterface
static pyComputeInterface pyCalcBetaInterface
Definition: DAUtility.H:130
Foam::DAUtility::~DAUtility
virtual ~DAUtility()
Destructor.
Definition: DAUtility.C:20
IOMRFZoneListDF.H
Foam::DAUtility
Definition: DAUtility.H:33
Foam::DAUtility::pyCalcBetaJacVecProd
static void * pyCalcBetaJacVecProd
Definition: DAUtility.H:132
Foam::DAUtility::angleOfAttackRadForwardAD
static scalar angleOfAttackRadForwardAD
angle of attack in radian used in forward mode AD
Definition: DAUtility.H:126
Foam::DAUtility::isValueCloseToRef
static label isValueCloseToRef(const scalar val, const scalar refVal, const scalar tol=1.0e-6)
check whether a value is close to a reference value by a tolerance
Definition: DAUtility.C:651
Foam::DAUtility::pyDict2OFDict
static void pyDict2OFDict(PyObject *pyDict, dictionary &ofDict)
convert a python dictionary object to OpenFoam dictionary
Definition: DAUtility.C:24
Foam::DAUtility::boundVar
static void boundVar(const dictionary &allOptions, volScalarField &var, const label printToScreen)
bound a volScalar variable based on parametes defined in DAOption::allOptions_
Definition: DAUtility.C:419
Foam::DAUtility::writeVectorBinary
static void writeVectorBinary(const Vec vecIn, const word prefix)
write petsc vector in binary format
Definition: DAUtility.C:259
Foam
Definition: multiFreqScalarFvPatchField.C:144
Foam::pyJacVecProdInterface
void(* pyJacVecProdInterface)(const double *, double *, int, const double *, const double *, int, void *)
Definition: DAUtility.H:28
Foam::DAUtility::listDeleteVal
static label listDeleteVal(List< classType > &listIn, const classType valDel)
delete a value in the list
Definition: DAUtility.H:203
Foam::DAUtility::writeMatrixBinary
static void writeMatrixBinary(const Mat matIn, const word prefix)
write petsc matrix in binary format
Definition: DAUtility.C:355
Foam::DAUtility::writeVectorASCII
static void writeVectorASCII(const Vec vecIn, const word prefix)
write petsc vector in ascii format
Definition: DAUtility.C:291
Foam::DAUtility::writeMatrixASCII
static void writeMatrixASCII(const Mat matIn, const word prefix)
write petsc matrix in ascii format
Definition: DAUtility.C:387
Foam::pyComputeInterface
void(* pyComputeInterface)(const double *, int, double *, int, void *)
Definition: DAUtility.H:27
Foam::DAUtility::isInList
static label isInList(const classType value, const List< classType > &list)
check whether a value is in the list
Definition: DAUtility.H:137
Foam::DAUtility::genGlobalIndex
static globalIndex genGlobalIndex(const label localIndexSize)
generate global index numbering for local-global index transferring
Definition: DAUtility.C:599