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-2021 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, keyType::LITERAL);
61  }
62  else if (expectedTypes::GRADIENT_TYPE == expectedType)
63  {
64  // Mandatory
65  evalGrad = dict.readEntry("gradientExpr", exprGrad, keyType::LITERAL);
66  }
67  else
68  {
69  // MIXED_TYPE
70  evalValue =
71  dict.readIfPresent("valueExpr", exprValue, keyType::LITERAL);
72 
73  evalGrad =
74  dict.readIfPresent("gradientExpr", exprGrad, keyType::LITERAL);
75 
76  if (!evalValue && !evalGrad)
77  {
79  << "Entries 'valueExpr' and 'gradientExpr' "
80  "(mixed-conditon) not found in dictionary "
81  << dict.name() << nl
82  << exit(FatalIOError);
83  }
84 
85  if (debug_)
86  {
87  if (!evalValue)
88  {
89  Info<< "Mixed with no valueExpr" << nl;
90  }
91  if (!evalGrad)
92  {
93  Info<< "Mixed with no gradientExpr" << nl;
94  }
95  }
96  }
97 
98 
99  // When both value/gradient specified (ie, mixed) expect a fraction
100  // - if missing, defer treatment to inherited BC
101 
102  if (evalValue && evalGrad && dict.readIfPresent("fractionExpr", exprFrac))
103  {
104  stringOps::inplaceTrim(exprFrac);
105 
106  if (exprFrac == "0" || exprFrac == "1")
107  {
108  // Special cases, handled with more efficiency
109  fracExpr_ = exprFrac;
110  }
111  else if (!exprFrac.empty())
112  {
113  evalFrac = true;
114  if (wantPointData)
115  {
116  exprFrac = "toPoint(" + exprFrac + ")";
117  }
118  }
119  }
120 
121 
122  // Expansions
123 
124  if (evalValue)
125  {
126  valueExpr_ = expressions::exprString(exprValue, dict);
127  }
128  if (evalGrad)
129  {
130  gradExpr_ = expressions::exprString(exprGrad, dict);
131  }
132  if (evalFrac)
133  {
134  fracExpr_ = expressions::exprString(exprFrac, dict);
135  }
136 }
137 
138 
139 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
140 
142 :
143  debug_(false),
144  evalOnConstruct_(false),
145  valueExpr_(),
146  gradExpr_(),
147  fracExpr_()
148 {}
149 
150 
152 (
153  const dictionary& dict,
154  enum expectedTypes expectedType,
155  bool wantPointData
156 )
157 :
158  debug_(dict.getOrDefault("debug", false)),
159  evalOnConstruct_(dict.getOrDefault("evalOnConstruct", false)),
160  valueExpr_(),
161  gradExpr_(),
162  fracExpr_()
163 {
164  readExpressions(dict, expectedType, wantPointData);
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
171 {
172  os.writeEntryIfDifferent<bool>("evalOnConstruct", false, evalOnConstruct_);
173 
174  // Do not emit debug_ value
175 
176  // Write expression, but not empty ones
177  valueExpr_.writeEntry("valueExpr", os, false);
178  gradExpr_.writeEntry("gradientExpr", os, false);
179  fracExpr_.writeEntry("fractionExpr", os, false);
180 }
181 
182 
183 // ************************************************************************* //
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:1067
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:141
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
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:123
os
OBJstream os(runTime.globalPath()/outputName)
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:170
Foam::nl
constexpr char nl
Definition: Ostream.H:404
facePointPatch.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
fvPatch.H
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
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