DAOption.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6  Description:
7  DAOption contains an OpenFOAM dictionary that has all the options
8  defined in the python layer. It also has functions to get and set
9  values from DAOption::allOptions_
10 
11 \*---------------------------------------------------------------------------*/
12 
13 #ifndef DAOption_H
14 #define DAOption_H
15 
16 #include "fvOptions.H"
17 #include "Python.h"
18 #include "DAUtility.H"
19 
20 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
21 
22 namespace Foam
23 {
24 
25 /*---------------------------------------------------------------------------*\
26  Class DAOption Declaration
27 \*---------------------------------------------------------------------------*/
28 
29 class DAOption
30  : public regIOobject
31 {
32 
33 private:
35  DAOption(const DAOption&);
36 
38  void operator=(const DAOption&);
39 
41  const fvMesh& mesh_;
42 
44  dictionary allOptions_;
45 
46 public:
48  DAOption(
49  const fvMesh& mesh,
50  PyObject* pyOptions);
51 
53  virtual ~DAOption();
54 
56  const dictionary& getAllOptions() const
57  {
58  return allOptions_;
59  }
60 
62  template<class classType>
63  classType getOption(const word key) const;
64 
66  template<class classType>
67  void setOption(
68  const word key,
69  const classType value);
70 
72  template<class classType>
73  classType getSubDictOption(
74  const word subDict,
75  const word subDictKey) const;
76 
78  template<class classType>
79  void setSubDictOption(
80  const word subDict,
81  const word subDictKey,
82  const classType value);
83 
85  bool writeData(Ostream& os) const;
86 
88  void updateDAOption(PyObject* pyOptions);
89 };
90 
91 template<class classType>
92 classType DAOption::getOption(const word key) const
93 {
94  /*
95  Description:
96  Get an option from allOptions_
97  NOTE: the return value can not be a dictionary. If one need to get a sub
98  dictionary, use DAOption::getAllOptions().subDict("subDictName") instead
99 
100  Input:
101  key: the key in the DAOption::allOptions_ dictionary
102 
103  Output:
104  classType: the value for the DAOption::allOptions_[key]
105 
106  Example:
107  If DAOption::allOptions_ reads
108  {
109  flowCondition Incompressible;
110  nkTol 1.e-6;
111  objFuncs (CD CL);
112  }
113  Then, we can get the values by:
114  {
115  word val1=daOption.getOption<word>("flowCondition");
116  scalar val2=daOption.getOption<scalar>("nkTol");
117  List<word> val3=daOption.getOption< List<word> >("objFuncs");
118  }
119  */
120 
121  classType val;
122  allOptions_.readEntry<classType>(key, val);
123  return val;
124 }
125 
126 template<class classType>
128  const word key,
129  const classType value)
130 {
131  /*
132  Description:
133  Set an option to DAOption::allOptions_.
134  NOTE: the value can not be a dictionary.
135  NOTE: changing allOptions_ will not change self.options in pyDAFoam
136 
137  Input:
138  key: the key in the DAOption::allOptions_ dictionary
139  value: the value to set
140 
141  Output:
142  DAOption::allOptions_: the key value in allOptions will be replaced
143 
144  Example:
145  If DAOption::allOptions_ reads
146  {
147  flowCondition Incompressible;
148  nkTol 1.e-6;
149  objFuncs (CD CL);
150  }
151  Then, we can set the values by:
152  {
153  daOption.setOption<word>("flowCondition","Compressible");
154  daOption.setOption<scalar>("nkTol",1.0e-5);
155  daOption.getOption< List<word> >("objFuncs",{CMX CMY});
156  }
157  */
158 
159  allOptions_.set(key, value);
160 
161  return;
162 }
163 
164 template<class classType>
166  const word subDict,
167  const word subDictKey) const
168 {
169  /*
170  Description:
171  Get value from a key from a sub dictionary in allOptions_
172 
173  Input:
174  subDict: the name of the subDict
175  subDictKey: the key in the subDict
176 
177  Output:
178  classType: the value for the allOptions_[subDict][subDictKey]
179 
180  Example:
181  If allOptions_ reads
182  {
183  flowBCs
184  {
185  useWallFunction true;
186  value 1.0;
187  };
188  flowEndTime 100.0;
189  }
190  Then, we can get the sub dictionary flowBCs by:
191  scalar val=daOption.getSubDictOption<scalar>("flowBCs","value");
192  */
193 
194  dictionary subDictF = allOptions_.subDict(subDict);
195  classType val;
196  subDictF.readEntry<classType>(subDictKey, val);
197  return val;
198 }
199 
200 template<class classType>
202  const word subDict,
203  const word subDictKey,
204  const classType value)
205 {
206  /*
207  Description:
208  Set a sub dictionary option to allOptions_
209  NOTE: changing allOptions_ will not change self.options in pyDAFoam
210 
211  Input:
212  subDict: the name of the subDict
213  subDictKey: the key in the subDict
214  value: the value to set
215 
216  Output:
217  allOptions_: the key value in allOptions will be replaced
218 
219  Example:
220  If allOptions_ reads
221  {
222  flowBCs
223  {
224  useWallFunction true;
225  value 1.0;
226  };
227  flowEndTime 100.0;
228  }
229  Then, we can set the sub dictionary flowBCs-value by:
230  daOption.setSubDictOption<scalar>("flowBCs","value",2.0);
231  */
232 
233  dictionary& subDictF = allOptions_.subDict(subDict);
234  subDictF.set(subDictKey, value);
235  return;
236 }
237 
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 
240 } // End namespace Foam
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 
244 #endif
245 
246 // ************************************************************************* //
Foam::DAOption::setSubDictOption
void setSubDictOption(const word subDict, const word subDictKey, const classType value)
set an dictionary option to subDict and key
Definition: DAOption.H:201
Foam::DAOption::~DAOption
virtual ~DAOption()
Destructor.
Definition: DAOption.C:46
Foam::DAOption
Definition: DAOption.H:29
DAUtility.H
Foam::DAOption::getOption
classType getOption(const word key) const
get an option from subDict and key
Definition: DAOption.H:92
Foam::DAOption::updateDAOption
void updateDAOption(PyObject *pyOptions)
update the allOptions_ dict in DAOption based on the pyOptions from pyDAFoam
Definition: DAOption.C:50
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
Definition: multiFreqScalarFvPatchField.C:144
Foam::DAOption::writeData
bool writeData(Ostream &os) const
this is a virtual function for regIOobject
Definition: DAOption.C:66
Foam::DAOption::setOption
void setOption(const word key, const classType value)
set an option to subDict and key
Definition: DAOption.H:127