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