dimensionSets.C
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) 2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "dimensionSet.H"
30#include "dimensionedScalar.H"
31#include "simpleRegIOobject.H"
32#include "demandDrivenData.H"
33
34// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35
36namespace Foam
37{
38
39/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
40
41// Since dimensionSystems() can be reread we actually store a copy of
42// the controlDict subDict (v.s. a reference to the subDict for e.g.
43// dimensionedConstants)
47
49
50// Helper class to
51// register re-reader
52// deallocate demand-driven data
53class addDimensionSetsToDebug
54:
56{
57public:
58
59 addDimensionSetsToDebug(const addDimensionSetsToDebug&) = delete;
60 void operator=(const addDimensionSetsToDebug&) = delete;
61
62 explicit addDimensionSetsToDebug(const char* name)
63 :
64 ::Foam::simpleRegIOobject(Foam::debug::addDimensionSetObject, name)
65 {}
66
67 virtual ~addDimensionSetsToDebug()
68 {
69 deleteDemandDrivenData(dimensionSystemsPtr_);
70 deleteDemandDrivenData(unitSetPtr_);
71 deleteDemandDrivenData(writeUnitSetPtr_);
72 }
73
74 virtual void readData(Foam::Istream& is)
75 {
76 deleteDemandDrivenData(dimensionSystemsPtr_);
77 deleteDemandDrivenData(unitSetPtr_);
78 deleteDemandDrivenData(writeUnitSetPtr_);
79 dimensionSystemsPtr_ = new dictionary(is);
80 }
81
82 virtual void writeData(Foam::Ostream& os) const
83 {
85 }
86};
87
88addDimensionSetsToDebug addDimensionSetsToDebug_("DimensionSets");
89
91
92
94{
96 {
97 dictionary* cachedPtr(nullptr);
99 (
101 (
102 "DimensionSets",
103 cachedPtr
104 )
105 );
106 }
107 return *dimensionSystemsPtr_;
108}
109
110
112{
113 if (!unitSetPtr_)
114 {
116
117 if (!dict.found("unitSet"))
118 {
120 << "Cannot find unitSet in dictionary " << dict.name()
121 << exit(FatalIOError);
122 }
123
124 const word unitSetCoeffs(dict.get<word>("unitSet") + "Coeffs");
125
126 const dictionary* unitDictPtr = dict.findDict(unitSetCoeffs);
127
128 if (!unitDictPtr)
129 {
131 << "Cannot find " << unitSetCoeffs << " in dictionary "
132 << dict.name() << nl
133 << exit(FatalIOError);
134 }
135
136 const dictionary& unitDict = *unitDictPtr;
137
138 unitSetPtr_ = new HashTable<dimensionedScalar>(2*unitDict.size());
139
140 wordList writeUnitNames;
141
142 for (const entry& dEntry : unitDict)
143 {
144 if ("writeUnits" == dEntry.keyword())
145 {
146 dEntry.readEntry(writeUnitNames);
147 }
148 else
149 {
151 dt.read(dEntry.stream(), unitDict);
152
153 bool ok = unitSetPtr_->insert(dEntry.keyword(), dt);
154 if (!ok)
155 {
157 << "Duplicate unit " << dEntry.keyword()
158 << " in DimensionSets dictionary"
159 << exit(FatalIOError);
160 }
161 }
162 }
163
164 if (writeUnitNames.size() != 0 && writeUnitNames.size() != 7)
165 {
167 << "Cannot find entry \"writeUnits\" in " << unitDict.name()
168 << " or it is not a wordList of size 7"
169 << exit(FatalIOError);
170 }
171
172 writeUnitSetPtr_ = new dimensionSets(*unitSetPtr_, writeUnitNames);
173 }
174
175 return *unitSetPtr_;
176}
177
178
180{
181 if (!writeUnitSetPtr_)
182 {
183 (void)unitSet();
184 }
185 return *writeUnitSetPtr_;
186}
187
188
190
191const dimensionSet dimMass(1, 0, 0, 0, 0, 0, 0);
192const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0);
193const dimensionSet dimTime(0, 0, 1, 0, 0, 0, 0);
194const dimensionSet dimTemperature(0, 0, 0, 1, 0, 0, 0);
195const dimensionSet dimMoles(0, 0, 0, 0, 1, 0, 0);
196const dimensionSet dimCurrent(0, 0, 0, 0, 0, 1, 0);
197const dimensionSet dimLuminousIntensity(0, 0, 0, 0, 0, 0, 1);
198
202
205
210
217
218
219// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220
221} // End namespace Foam
222
223
224// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
225
227(
228 const HashTable<dimensionedScalar>& units,
229 const wordList& unitNames
230)
231:
232 units_(unitNames.size()),
233 conversion_(unitNames.size()),
234 conversionPivots_(unitNames.size()),
235 valid_(false)
236{
237 forAll(unitNames, i)
238 {
239 units_.set
240 (
241 i,
243 (
244 units[unitNames[i]]
245 )
246 );
247 }
248
249 if (unitNames.size() == 7)
250 {
251 valid_ = true;
252
253 // Determine conversion from basic units to write units
254 for (label rowI = 0; rowI < conversion_.m(); rowI++)
255 {
256 scalar* row = conversion_[rowI];
257
258 for (label columnI = 0; columnI < conversion_.n(); columnI++)
259 {
260 const dimensionedScalar& dSet = units_[columnI];
261 row[columnI] = dSet.dimensions()[rowI];
262 }
263 }
264 conversionPivots_.setSize(conversion_.m());
265 LUDecompose(conversion_, conversionPivots_);
266 }
267}
268
269
271{
272 LUBacksubstitute(conversion_, conversionPivots_, exponents);
273}
274
275
276// ************************************************************************* //
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void setSize(const label n)
Alias for resize()
Definition: List.H:218
label n() const noexcept
The number of columns.
Definition: MatrixI.H:103
label m() const noexcept
The number of rows.
Definition: MatrixI.H:96
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary pointer if present.
Definition: dictionaryI.H:127
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionaryI.H:87
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:109
Construction of unit sets.
Definition: dimensionSets.H:83
const PtrList< dimensionedScalar > & units() const noexcept
Return the units.
void coefficients(scalarField &exponents) const
(if valid) obtain set of coefficients of unitNames
const dimensionSet & dimensions() const
Return const reference to dimensions.
bool read(const dictionary &dict)
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
Abstract base class for registered object with I/O. Used in debug symbol registration.
A class for handling words, derived from Foam::string.
Definition: word.H:68
Template functions to aid in the implementation of demand driven data.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
dictionary & switchSet(const char *subDictName, dictionary *&subDictPtr)
Internal function to lookup a sub-dictionary from controlDict.
Definition: debug.C:180
Namespace for OpenFOAM.
const dimensionSet dimPressure
const dimensionSet dimViscosity
const dimensionSet dimPower
const dimensionSet dimless
Dimensionless.
const dimensionSet dimCompressibility
dimensionedSymmTensor sqr(const dimensionedVector &dv)
const dimensionSet dimEnergy
const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:52
dimensionedScalar pow3(const dimensionedScalar &ds)
const dimensionSet dimTime(0, 0, 1, 0, 0, 0, 0)
Definition: dimensionSets.H:53
const dimensionSet dimCurrent(0, 0, 0, 0, 0, 1, 0)
Definition: dimensionSets.H:56
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
const dimensionSet dimMoles(0, 0, 0, 0, 1, 0, 0)
Definition: dimensionSets.H:55
const dimensionSet dimArea(sqr(dimLength))
Definition: dimensionSets.H:59
const dimensionSet dimVelocity
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
const dimensionSet dimGasConstant
dictionary & dimensionSystems()
Top level dictionary.
Definition: dimensionSets.C:93
dictionary * dimensionSystemsPtr_(nullptr)
const dimensionSet dimSpecificHeatCapacity(dimGasConstant)
Definition: dimensionSets.H:76
const dimensionSet dimTemperature(0, 0, 0, 1, 0, 0, 0)
Definition: dimensionSets.H:54
const HashTable< dimensionedScalar > & unitSet()
Set of all dimensions.
const dimensionSet dimForce
IOerror FatalIOError
const dimensionSets & writeUnitSet()
Set of units.
const dimensionSet dimDynamicViscosity
const dimensionSet dimAcceleration
const dimensionSet dimVolume(pow3(dimLength))
Definition: dimensionSets.H:60
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
HashTable< dimensionedScalar > * unitSetPtr_(nullptr)
const dimensionSet dimDensity
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
dimensionSets * writeUnitSetPtr_(nullptr)
void deleteDemandDrivenData(DataPtr &dataPtr)
const dimensionSet dimMass(1, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:51
const dimensionSet dimVol(dimVolume)
Older spelling for dimVolume.
Definition: dimensionSets.H:64
const dimensionSet dimLuminousIntensity(0, 0, 0, 0, 0, 0, 1)
Definition: dimensionSets.H:57
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
const bool writeData(pdfDictionary.get< bool >("writeData"))