dimensionedConstants.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Global
28  dimensionedConstants
29 
30 Description
31  Dictionary reading and supplying the dimensioned constants used within
32  OpenFOAM, particularly for thermodynamics.
33 
34  The values are read from the OpenFOAM etc/controlDict and should be
35  changed to run with a different set of units from the default SI units.
36 
37 SourceFiles
38  dimensionedConstants.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef dimensionedConstants_H
43 #define dimensionedConstants_H
44 
45 #include "dictionary.H"
46 #include "dimensionedScalar.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 dictionary& dimensionedConstants();
56 
57 
58 dimensionedScalar dimensionedConstant(const word& group, const word& varName);
59 
60 
61 template<class T>
63 (
64  const word& group,
65  const word& varName,
66  const T& defaultValue
67 )
68 {
70 
71  const word unitSet(dict.get<word>("unitSet"));
72 
73  dictionary& unitDict(dict.subDict(unitSet + "Coeffs"));
74 
75  if (unitDict.found(group))
76  {
77  dictionary& groupDict = unitDict.subDict(group);
78 
79  // Leaner version of dictionary::getOrAdd()
80  // without writeOptionalEntries
81 
82  if (groupDict.found(varName))
83  {
84  return groupDict.get<T>(varName);
85  }
86  else
87  {
88  groupDict.add(varName, defaultValue);
89  return defaultValue;
90  }
91  }
92  else
93  {
94  unitDict.add(group, dictionary::null);
95  unitDict.subDict(group).add(varName, defaultValue);
96 
97  return defaultValue;
98  }
99 }
100 
101 
102 //- Defined dimensioned constant , lookup as \a Name
103 #define defineDimensionedConstant(Group,Switch,Tag,Name) \
104  const Foam::dimensionedScalar Switch; \
105  class add##Tag##ToDimensionedConstant \
106  : \
107  public Foam::simpleRegIOobject \
108  { \
109  public: \
110  add##Tag##ToDimensionedConstant(const char* name) \
111  : \
112  Foam::simpleRegIOobject \
113  (Foam::debug::addDimensionedConstantObject,name) \
114  { \
115  Foam::dimensionedScalar ds \
116  ( \
117  Foam::dimensionedConstant \
118  ( \
119  Group, \
120  Name \
121  ) \
122  ); \
123  Foam::dimensionedScalar& s = const_cast<Foam::dimensionedScalar&> \
124  ( \
125  Switch \
126  ); \
127  s.dimensions().reset(ds.dimensions()); \
128  s = ds; \
129  } \
130  virtual ~add##Tag##ToDimensionedConstant() = default; \
131  virtual void readData(Foam::Istream& is) \
132  { \
133  const_cast<Foam::dimensionedScalar&>(Switch) = \
134  Foam::dimensionedConstant \
135  ( \
136  Group, \
137  Name \
138  ); \
139  } \
140  virtual void writeData(Foam::Ostream& os) const \
141  { \
142  os << Switch; \
143  } \
144  }; \
145  add##Tag##ToDimensionedConstant add##Tag##ToDimensionedConstant_(Name)
146 
147 
148 //- Defined dimensioned constant with default , lookup as \a Name
149 #define defineDimensionedConstantWithDefault\
150 (Group,Switch,DefaultExpr,Tag,Name) \
151  const Foam::dimensionedScalar Switch; \
152  class add##Tag##ToDimensionedConstantWithDefault \
153  : \
154  public Foam::simpleRegIOobject \
155  { \
156  public: \
157  add##Tag##ToDimensionedConstantWithDefault(const char* name) \
158  : \
159  Foam::simpleRegIOobject \
160  (Foam::debug::addDimensionedConstantObject,name) \
161  { \
162  Foam::dimensionedScalar ds \
163  ( \
164  Foam::dimensionedConstant \
165  ( \
166  Group, \
167  Name, \
168  Foam::dimensionedScalar(Name,DefaultExpr) \
169  ) \
170  ); \
171  Foam::dimensionedScalar& s = const_cast<Foam::dimensionedScalar&> \
172  ( \
173  Switch \
174  ); \
175  s.dimensions().reset(ds.dimensions()); \
176  s = ds; \
177  } \
178  virtual ~add##Tag##ToDimensionedConstantWithDefault() = default; \
179  virtual void readData(Foam::Istream& is) \
180  { \
181  const_cast<Foam::dimensionedScalar&>(Switch) = \
182  Foam::dimensionedConstant \
183  ( \
184  Group, \
185  Name, \
186  Foam::dimensionedScalar(Name,DefaultExpr) \
187  ); \
188  } \
189  virtual void writeData(Foam::Ostream& os) const \
190  { \
191  os << Switch; \
192  } \
193  }; \
194  add##Tag##ToDimensionedConstantWithDefault \
195  add##Tag##ToDimensionedConstantWithDefault_(Name)
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 #endif
205 
206 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::constant::atomic::group
constexpr const char *const group
Group name for atomic constants.
Definition: atomicConstants.H:52
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionary.C:364
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::unitSet
const HashTable< dimensionedScalar > & unitSet()
Set of all dimensions.
Definition: dimensionSets.C:111
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:385
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:528
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::dimensionedConstants
dictionary & dimensionedConstants()
Definition: dimensionedConstants.C:41
Foam::dimensionedScalar
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Definition: dimensionedScalarFwd.H:43
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::dimensionedConstant
dimensionedScalar dimensionedConstant(const word &group, const word &varName)
Definition: dimensionedConstants.C:52
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
dimensionedScalar.H
dictionary.H
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:708