Function1UnitConversions.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) 2024 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 "Function1.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
31 (
32  std::initializer_list<unitConversion> l
33 )
34 :
35  x(dimless),
36  value(dimless)
37 {
38  auto i = l.begin();
39  x.reset(*i);
40  value.reset(*(++i));
41 }
42 
43 
45 :
46  x(dimless),
47  value(dimless)
48 {
49  is >> *this;
50 }
51 
52 
55 {
56  return autoPtr<unitConversions>(new unitConversions(*this));
57 }
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
63 (
64  const word& keyword,
65  const dictionary& dict
66 )
67 {
68  const entry* entryPtr = dict.lookupEntryPtr(keyword, false, true);
69 
70  if (entryPtr)
71  {
72  ITstream& is = entryPtr->stream();
73 
74  is.readBegin("unitConversions");
75  x.readIfPresent(keyword, dict, is);
76  value.readIfPresent(keyword, dict, is);
77  is.readEnd("unitConversions");
78 
79  return true;
80  }
81  else
82  {
84  {
86  << "Optional entry '" << keyword << "' is not present,"
87  << " the default value '" << token::BEGIN_LIST << x.info()
88  << token::SPACE << value.info() << token::END_LIST
89  << "' will be used." << endl;
90  }
91 
92  return false;
93  }
94 }
95 
96 
97 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
98 
100 (
101  const word& typeName,
103  const dictionary& dict
104 )
105 {
106  if (!units.x.standard() || !units.value.standard())
107  {
109  << "Unit conversions are not supported by "
110  << typeName << " function1 types" << abort(FatalError);
111  }
112 }
113 
114 
116 {
117  os << units;
118 }
119 
120 
121 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
122 
123 Foam::Istream& Foam::operator>>
124 (
125  Istream& is,
127 )
128 {
129  is.readBegin("unitConversions");
130  is >> units.x >> units.value;
131  is.readEnd("unitConversions");
132 
133  is.check("Istream& operator>>(Istream&, unitConversions&)");
134 
135  return is;
136 }
137 
138 
139 Foam::Ostream& Foam::operator<<
140 (
141  Ostream& os,
143 )
144 {
145  os << token::BEGIN_LIST
146  << units.x << token::SPACE << units.value
147  << token::END_LIST;
148 
149  os.check("Ostream& operator<<(Ostream&, const unitConversions&)");
150 
151  return os;
152 }
153 
154 
155 // ************************************************************************* //
Input token stream.
Definition: ITstream.H:53
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
static bool writeOptionalEntries
If true write optional keywords and values.
Definition: dictionary.H:262
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
void reset(const unitConversion &)
Reset the unit conversion.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define IOInfoInFunction(ios)
Report an IO information message using Foam::Info.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const dimensionSet dimless
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
error FatalError
void assertNoConvertUnits(const word &typeName, const Function1s::unitConversions &units, const dictionary &dict)
Generate an error in an context where unit conversions are not supported.
dictionary dict
bool readIfPresent(const word &keyword, const dictionary &)
Update if found in the dictionary.
autoPtr< unitConversions > clone() const
Construct a clone.
unitConversion value
Unit conversion for result values.
Definition: Function1.H:71
unitConversions(std::initializer_list< unitConversion >)
Construct from initialiser list.
unitConversion x
Unit conversion for x-axis values.
Definition: Function1.H:68