patchExprFieldBase.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-2018 Bernhard Gschaider
9  Copyright (C) 2019-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 "patchExprFieldBase.H"
30 #include "facePointPatch.H"
31 #include "fvMesh.H"
32 #include "fvPatch.H"
33 #include "pointMesh.H"
34 #include "stringOps.H"
35 
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 
38 void Foam::expressions::patchExprFieldBase::readExpressions
39 (
40  const dictionary& dict,
41  enum expectedTypes expectedType,
42  bool wantPointData
43 )
44 {
45  if (debug_)
46  {
47  Info<< "Expression BC with " << dict << nl;
48  }
49 
50  valueExpr_.clear();
51  gradExpr_.clear();
52  fracExpr_.clear();
53 
54  string exprValue, exprGrad, exprFrac;
55  bool evalValue = false, evalGrad = false, evalFrac = false;
56 
57  if (expectedTypes::VALUE_TYPE == expectedType)
58  {
59  // Mandatory
60  evalValue = dict.readEntry("valueExpr", exprValue);
61  }
62  else if (expectedTypes::GRADIENT_TYPE == expectedType)
63  {
64  // Mandatory
65  evalGrad = dict.readEntry("gradientExpr", exprGrad);
66  }
67  else
68  {
69  // MIXED_TYPE
70  evalValue = dict.readIfPresent("valueExpr", exprValue);
71  evalGrad = dict.readIfPresent("gradientExpr", exprGrad);
72 
73  if (!evalValue && !evalGrad)
74  {
76  << "Entries 'valueExpr' and 'gradientExpr' "
77  "(mixed-conditon) not found in dictionary "
78  << dict.name() << nl
79  << exit(FatalIOError);
80  }
81 
82  if (debug_)
83  {
84  if (!evalValue)
85  {
86  Info<< "Mixed with no valueExpr" << nl;
87  }
88  if (!evalGrad)
89  {
90  Info<< "Mixed with no gradientExpr" << nl;
91  }
92  }
93  }
94 
95 
96  // When both value/gradient specified (ie, mixed) expect a fraction
97  // - if missing, defer treatment to inherited BC
98 
99  if (evalValue && evalGrad && dict.readIfPresent("fractionExpr", exprFrac))
100  {
101  stringOps::inplaceTrim(exprFrac);
102 
103  if (exprFrac == "0" || exprFrac == "1")
104  {
105  // Special cases, handled with more efficiency
106  fracExpr_ = exprFrac;
107  }
108  else if (!exprFrac.empty())
109  {
110  evalFrac = true;
111  if (wantPointData)
112  {
113  exprFrac = "toPoint(" + exprFrac + ")";
114  }
115  }
116  }
117 
118 
119  // Expansions
120 
121  if (evalValue)
122  {
123  valueExpr_ = expressions::exprString(exprValue, dict);
124  }
125  if (evalGrad)
126  {
127  gradExpr_ = expressions::exprString(exprGrad, dict);
128  }
129  if (evalFrac)
130  {
131  fracExpr_ = expressions::exprString(exprFrac, dict);
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
139 :
140  debug_(false),
141  evalOnConstruct_(false),
142  valueExpr_(),
143  gradExpr_(),
144  fracExpr_()
145 {}
146 
147 
149 (
150  const dictionary& dict,
151  enum expectedTypes expectedType,
152  bool wantPointData
153 )
154 :
155  debug_(dict.getOrDefault("debug", false)),
156  evalOnConstruct_(dict.getOrDefault("evalOnConstruct", false)),
157  valueExpr_(),
158  gradExpr_(),
159  fracExpr_()
160 {
161  readExpressions(dict, expectedType, wantPointData);
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
168 {
169  os.writeEntryIfDifferent<bool>("evalOnConstruct", false, evalOnConstruct_);
170 
171  // Do not emit debug_ value
172 
173  if (!valueExpr_.empty())
174  {
175  os.writeEntry("valueExpr", valueExpr_);
176  }
177  if (!gradExpr_.empty())
178  {
179  os.writeEntry("gradientExpr", gradExpr_);
180  }
181  if (!fracExpr_.empty())
182  {
183  os.writeEntry("fractionExpr", fracExpr_);
184  }
185 }
186 
187 
188 // ************************************************************************* //
Foam::Ostream::writeEntryIfDifferent
Ostream & writeEntryIfDifferent(const word &key, const T &value1, const T &value2)
Write a keyword/value entry only when the two values differ.
Definition: Ostream.H:244
Foam::expressions::patchExprFieldBase::debug_
bool debug_
Add debugging.
Definition: patchExprFieldBase.H:101
Foam::expressions::patchExprFieldBase::expectedTypes
expectedTypes
Enumeration of expected expressions.
Definition: patchExprFieldBase.H:90
Foam::stringOps::inplaceTrim
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1069
Foam::FatalIOError
IOerror FatalIOError
Foam::expressions::patchExprFieldBase::gradExpr_
expressions::exprString gradExpr_
Definition: patchExprFieldBase.H:108
patchExprFieldBase.H
Foam::expressions::patchExprFieldBase::patchExprFieldBase
patchExprFieldBase()
Default construct.
Definition: patchExprFieldBase.C:138
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
fvMesh.H
Foam::expressions::patchExprFieldBase::fracExpr_
expressions::exprString fracExpr_
Definition: patchExprFieldBase.H:109
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::expressions::patchExprFieldBase::write
void write(Ostream &os) const
Write.
Definition: patchExprFieldBase.C:167
Foam::nl
constexpr char nl
Definition: Ostream.H:385
facePointPatch.H
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
fvPatch.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::expressions::patchExprFieldBase::valueExpr_
expressions::exprString valueExpr_
Definition: patchExprFieldBase.H:107
stringOps.H
pointMesh.H