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-2023 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 #include "demandDrivenData.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // Since dimensionSystems() can be reread we actually store a copy of
36 // the controlDict subDict (v.s. a reference to the subDict for e.g.
37 // dimensionedConstants)
41 
42 // Delete the above data at the end of the run
44 {
46  {
50  }
51 };
52 
54 
55 }
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
60 {
62  {
63  dictionary* cachedPtr = nullptr;
65  (
67  (
68  "DimensionSets",
69  cachedPtr
70  )
71  );
72  }
73  return *dimensionSystemsPtr_;
74 }
75 
76 
78 {
79  if (!unitSetPtr_)
80  {
81  const dictionary& dict = dimensionSystems();
82 
83  if (!dict.found("unitSet"))
84  {
86  << "Cannot find unitSet in dictionary " << dict.name()
87  << exit(FatalIOError);
88  }
89 
90  const word unitSetCoeffs(word(dict.lookup("unitSet")) + "Coeffs");
91 
92  if (!dict.found(unitSetCoeffs))
93  {
95  << "Cannot find " << unitSetCoeffs << " in dictionary "
96  << dict.name() << exit(FatalIOError);
97  }
98 
99  const dictionary& unitDict = dict.subDict(unitSetCoeffs);
100 
101  unitSetPtr_ = new HashTable<dimensionedScalar>(unitDict.size());
102 
103  forAllConstIter(dictionary, unitDict, iter)
104  {
105  if (iter().keyword() != "writeUnits")
106  {
107  dimensionedScalar dt(iter().keyword(), iter().stream());
108  const bool ok = unitSetPtr_->insert(iter().keyword(), dt);
109  if (!ok)
110  {
112  << "Duplicate unit " << iter().keyword()
113  << " in DimensionSets dictionary"
114  << exit(FatalIOError);
115  }
116  }
117  }
118 
119  const wordList writeUnitNames
120  (
121  unitDict.lookupOrDefault<wordList>
122  (
123  "writeUnits",
124  wordList(0)
125  )
126  );
127 
128  writeUnitSetPtr_ = new dimensionSets(*unitSetPtr_, writeUnitNames);
129 
130  if (writeUnitNames.size() != 0 && writeUnitNames.size() != 7)
131  {
133  << "Cannot find entry \"writeUnits\" in " << unitDict.name()
134  << " or it is not a wordList of size 7"
135  << exit(FatalIOError);
136  }
137  }
138 
139  return *unitSetPtr_;
140 }
141 
142 
144 {
145  if (!writeUnitSetPtr_)
146  {
147  (void)unitSet();
148  }
149  return *writeUnitSetPtr_;
150 }
151 
152 
153 const Foam::dimensionSet Foam::dimless(0, 0, 0, 0, 0, 0, 0);
154 
155 const Foam::dimensionSet Foam::dimMass(1, 0, 0, 0, 0, 0, 0);
156 const Foam::dimensionSet Foam::dimLength(0, 1, 0, 0, 0, 0, 0);
157 const Foam::dimensionSet Foam::dimTime(0, 0, 1, 0, 0, 0, 0);
158 const Foam::dimensionSet Foam::dimTemperature(0, 0, 0, 1, 0, 0, 0);
159 const Foam::dimensionSet Foam::dimMoles(0, 0, 0, 0, 1, 0, 0);
160 const Foam::dimensionSet Foam::dimCurrent(0, 0, 0, 0, 0, 1, 0);
161 const Foam::dimensionSet Foam::dimLuminousIntensity(0, 0, 0, 0, 0, 0, 1);
162 
166 
170 
175 
182 
185 
186 
187 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
188 
190 (
191  const HashTable<dimensionedScalar>& units,
192  const wordList& unitNames
193 )
194 :
195  units_(unitNames.size()),
196  conversion_(unitNames.size()),
197  conversionPivots_(unitNames.size()),
198  valid_(false)
199 {
200  forAll(unitNames, i)
201  {
202  units_.set
203  (
204  i,
206  (
207  units[unitNames[i]]
208  )
209  );
210  }
211 
212  if (unitNames.size() == 7)
213  {
214  valid_ = true;
215 
216  // Determine conversion from basic units to write units
217  for (label rowI = 0; rowI < conversion_.m(); rowI++)
218  {
219  scalar* row = conversion_[rowI];
220 
221  for (label columnI = 0; columnI < conversion_.n(); columnI++)
222  {
223  const dimensionedScalar& dSet = units_[columnI];
224  row[columnI] = dSet.dimensions()[rowI];
225  }
226  }
227 
228  conversionPivots_.setSize(conversion_.m());
229  LUDecompose(conversion_, conversionPivots_);
230  }
231 }
232 
233 
235 {
236  LUBacksubstitute(conversion_, conversionPivots_, exponents);
237 }
238 
239 
240 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
An STL-conforming hash table.
Definition: HashTable.H:127
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
label n() const
Return the number of columns.
Definition: MatrixI.H:64
label m() const
Return the number of rows.
Definition: MatrixI.H:57
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Dimension set for the base types.
Definition: dimensionSet.H:122
void coefficients(scalarField &) const
(if valid) obtain set of coefficients of unitNames
dimensionSets(const HashTable< dimensionedScalar > &, const wordList &unitNames)
Construct from all units and set of units to use for inversion.
const PtrList< dimensionedScalar > & units() const
Return the units.
const dimensionSet & dimensions() const
Return const reference to dimensions.
A class for handling words, derived from string.
Definition: word.H:62
Template functions to aid in the implementation of demand driven data.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
dictionary & switchSet(const char *subDictName, dictionary *&subDictPtr)
Internal function to lookup a sub-dictionary from controlDict.
Definition: debug.C:164
Namespace for OpenFOAM.
const dimensionSet dimViscosity
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const dimensionSet dimDynamicViscosity
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
dimensionSets * writeUnitSetPtr_(nullptr)
const dimensionSet dimEnergy
const dimensionSet dimCurrent
const dimensionSet dimPressure
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedScalar pow3(const dimensionedScalar &ds)
deleteDimensionSystemsPtr deleteDimensionSystemsPtr_
Definition: dimensionSets.C:53
const dimensionSet dimMassFlux
const dimensionSet dimPower
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution.
HashTable< dimensionedScalar > * unitSetPtr_(nullptr)
const dimensionSet dimGasConstant
const HashTable< dimensionedScalar > & unitSet()
Set of all dimensions.
Definition: dimensionSets.C:77
const dimensionSet dimless
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
const dimensionSet dimLength
const dimensionSet dimTemperature
const dimensionSet dimAcceleration
const dimensionSet dimForce
const dimensionSet dimCompressibility
const dimensionSet dimSpecificHeatCapacity
const dimensionSet dimMomentum
void deleteDemandDrivenData(DataPtr &dataPtr)
const dimensionSet dimTime
const dimensionSets & writeUnitSet()
Set of units.
const dimensionSet dimVol
const dimensionSet dimDensity
const dimensionSet dimVolume
const dimensionSet dimMoles
dictionary * dimensionSystemsPtr_(nullptr)
IOerror FatalIOError
const dimensionSet dimLuminousIntensity
const dimensionSet dimMass
const dimensionSet dimVelocity
const dimensionSet dimArea
dictionary & dimensionSystems()
Top level dictionary.
Definition: dimensionSets.C:59
const dimensionSet dimFlux
dictionary dict