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