dimensionSets.C
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-2022 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 \*---------------------------------------------------------------------------*/
25 
26 #include "dimensionSet.H"
27 #include "dimensionedScalar.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 
34 // Since dimensionSystems() can be reread we actually store a copy of
35 // the controlDict subDict (v.s. a reference to the subDict for e.g.
36 // dimensionedConstants)
37 dictionary* dimensionSystemsPtr_(nullptr);
38 HashTable<dimensionedScalar>* unitSetPtr_(nullptr);
39 dimensionSets* writeUnitSetPtr_(nullptr);
40 
41 }
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
46 {
48  {
49  dictionary* cachedPtr = nullptr;
51  (
53  (
54  "DimensionSets",
55  cachedPtr
56  )
57  );
58  }
59  return *dimensionSystemsPtr_;
60 }
61 
62 
64 {
65  if (!unitSetPtr_)
66  {
67  const dictionary& dict = dimensionSystems();
68 
69  if (!dict.found("unitSet"))
70  {
72  << "Cannot find unitSet in dictionary " << dict.name()
73  << exit(FatalIOError);
74  }
75 
76  const word unitSetCoeffs(word(dict.lookup("unitSet")) + "Coeffs");
77 
78  if (!dict.found(unitSetCoeffs))
79  {
81  << "Cannot find " << unitSetCoeffs << " in dictionary "
82  << dict.name() << exit(FatalIOError);
83  }
84 
85  const dictionary& unitDict = dict.subDict(unitSetCoeffs);
86 
87  unitSetPtr_ = new HashTable<dimensionedScalar>(unitDict.size());
88 
89  forAllConstIter(dictionary, unitDict, iter)
90  {
91  if (iter().keyword() != "writeUnits")
92  {
93  dimensionedScalar dt(iter().keyword(), iter().stream());
94  const bool ok = unitSetPtr_->insert(iter().keyword(), dt);
95  if (!ok)
96  {
98  << "Duplicate unit " << iter().keyword()
99  << " in DimensionSets dictionary"
100  << exit(FatalIOError);
101  }
102  }
103  }
104 
105  const wordList writeUnitNames
106  (
107  unitDict.lookupOrDefault<wordList>
108  (
109  "writeUnits",
110  wordList(0)
111  )
112  );
113 
114  writeUnitSetPtr_ = new dimensionSets(*unitSetPtr_, writeUnitNames);
115 
116  if (writeUnitNames.size() != 0 && writeUnitNames.size() != 7)
117  {
119  << "Cannot find entry \"writeUnits\" in " << unitDict.name()
120  << " or it is not a wordList of size 7"
121  << exit(FatalIOError);
122  }
123  }
124 
125  return *unitSetPtr_;
126 }
127 
128 
130 {
131  if (!writeUnitSetPtr_)
132  {
133  (void)unitSet();
134  }
135  return *writeUnitSetPtr_;
136 }
137 
138 
139 const Foam::dimensionSet Foam::dimless(0, 0, 0, 0, 0, 0, 0);
140 
141 const Foam::dimensionSet Foam::dimMass(1, 0, 0, 0, 0, 0, 0);
142 const Foam::dimensionSet Foam::dimLength(0, 1, 0, 0, 0, 0, 0);
143 const Foam::dimensionSet Foam::dimTime(0, 0, 1, 0, 0, 0, 0);
144 const Foam::dimensionSet Foam::dimTemperature(0, 0, 0, 1, 0, 0, 0);
145 const Foam::dimensionSet Foam::dimMoles(0, 0, 0, 0, 1, 0, 0);
146 const Foam::dimensionSet Foam::dimCurrent(0, 0, 0, 0, 0, 1, 0);
147 const Foam::dimensionSet Foam::dimLuminousIntensity(0, 0, 0, 0, 0, 0, 1);
148 
152 
155 
160 
167 
170 
171 
172 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
173 
175 (
176  const HashTable<dimensionedScalar>& units,
177  const wordList& unitNames
178 )
179 :
180  units_(unitNames.size()),
181  conversion_(unitNames.size()),
182  conversionPivots_(unitNames.size()),
183  valid_(false)
184 {
185  forAll(unitNames, i)
186  {
187  units_.set
188  (
189  i,
191  (
192  units[unitNames[i]]
193  )
194  );
195  }
196 
197  if (unitNames.size() == 7)
198  {
199  valid_ = true;
200 
201  // Determine conversion from basic units to write units
202  for (label rowI = 0; rowI < conversion_.m(); rowI++)
203  {
204  scalar* row = conversion_[rowI];
205 
206  for (label columnI = 0; columnI < conversion_.n(); columnI++)
207  {
208  const dimensionedScalar& dSet = units_[columnI];
209  row[columnI] = dSet.dimensions()[rowI];
210  }
211  }
212 
213  conversionPivots_.setSize(conversion_.m());
214  LUDecompose(conversion_, conversionPivots_);
215  }
216 }
217 
218 
220 {
221  LUBacksubstitute(conversion_, conversionPivots_, exponents);
222 }
223 
224 
225 // ************************************************************************* //
const dimensionSet dimViscosity
const dimensionSet dimLuminousIntensity
dictionary dict
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
const dimensionSet dimCompressibility
const dimensionSet dimArea
const dimensionSet dimPower
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
dictionary & switchSet(const char *subDictName, dictionary *&subDictPtr)
Internal function to lookup a sub-dictionary from controlDict.
Definition: debug.C:164
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
const dimensionSet dimPressure
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
HashTable< dimensionedScalar > * unitSetPtr_(nullptr)
const dimensionSet dimless
dimensionSets(const HashTable< dimensionedScalar > &, const wordList &unitNames)
Construct from all units and set of units to use for inversion.
const dimensionSet dimLength
dimensionSets * writeUnitSetPtr_(nullptr)
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:1002
const HashTable< dimensionedScalar > & unitSet()
Set of all dimensions.
Definition: dimensionSets.C:63
const dimensionSet dimTime
Dimension set for the base types.
Definition: dimensionSet.H:121
const dimensionSet dimSpecificHeatCapacity
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
const dimensionSet dimCurrent
const dimensionSet dimAcceleration
A class for handling words, derived from string.
Definition: word.H:59
const dimensionSet dimVol
const dimensionSet dimFlux
const dimensionSet dimDensity
const dimensionSet dimDynamicViscosity
const dimensionSet dimForce
An STL-conforming hash table.
Definition: HashTable.H:61
dictionary & dimensionSystems()
Top level dictionary.
Definition: dimensionSets.C:45
const dimensionSet dimVelocity
const dimensionSet dimEnergy
const dimensionSet dimMass
dimensionedScalar pow3(const dimensionedScalar &ds)
const dimensionSet dimMassFlux
const dimensionSet dimMoles
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
const dimensionSet & dimensions() const
Return const reference to dimensions.
const dimensionSet dimVolume
const dimensionSets & writeUnitSet()
Set of units.
dictionary * dimensionSystemsPtr_(nullptr)
void coefficients(scalarField &) const
(if valid) obtain set of coefficients of unitNames
const dimensionSet dimGasConstant
const dimensionSet dimTemperature
Namespace for OpenFOAM.
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
IOerror FatalIOError