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  Original code Copyright (C) 2011-2018 Bernhard Gschaider
9  Copyright (C) 2019 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 
35 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
36 
37 const Foam::fvPatch&
39 {
40  const polyMesh& pmesh = pp.boundaryMesh().mesh().mesh();
41 
42  const fvMesh* meshptr = isA<fvMesh>(pmesh);
43 
44  if (!meshptr)
45  {
47  << "Point patch not attached to a base fvMesh, "
48  << "cannot use patch expressions" << nl << endl
49  << exit(FatalError);
50  }
51 
52  return meshptr->boundary()[pp.index()];
53 }
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
59 :
60  patchExprFieldBase(false)
61 {}
62 
63 
65 (
66  bool allowGradient
67 )
68 :
69  debug_(false),
70  allowGradient_(allowGradient),
71  evalOnConstruct_(false),
72  valueExpr_(),
73  gradExpr_(),
74  fracExpr_()
75 {}
76 
77 
79 (
80  const dictionary& dict,
81  bool allowGradient,
82  bool isPointVal
83 )
84 :
85  debug_(dict.lookupOrDefault("debug", false)),
86  allowGradient_(allowGradient),
87  evalOnConstruct_(dict.lookupOrDefault<bool>("evalOnConstruct", false)),
88  valueExpr_(),
89  gradExpr_(),
90  fracExpr_()
91 {
92  if (debug_)
93  {
94  Info<< "Expression BC with " << dict << nl;
95  }
96 
97  string expr;
98 
99  if (dict.readIfPresent("valueExpr", expr))
100  {
101  valueExpr_ = expressions::exprString(expr, dict);
102  }
103  else
104  {
105  // No value expression - same as Zero
106  if (debug_)
107  {
108  Info<< "No valueExpr" << nl;
109  }
110  }
111 
112  if (allowGradient)
113  {
114  if (dict.readIfPresent("gradientExpr", expr))
115  {
116  gradExpr_ = expressions::exprString(expr, dict);
117  }
118  else
119  {
120  // No gradient expression - same as Zero
121 
122  if (debug_)
123  {
124  Info<< "No gradientExpr" << nl;
125  }
126  }
127 
128  if (dict.readIfPresent("fractionExpr", expr))
129  {
130  if (!expr.empty() && expr != "0")
131  {
132  if (isPointVal)
133  {
134  expr = "toPoint(" + expr + ")";
135  }
136 
137  fracExpr_ = expressions::exprString(expr, dict);
138  }
139  }
140  else
141  {
142  // No fraction expression - same as 1 (one)
143  // Mixed BC may elect to simply ignore gradient expression
144  }
145  }
146 }
147 
148 
150 (
151  const patchExprFieldBase& rhs
152 )
153 :
154  debug_(rhs.debug_),
155  allowGradient_(rhs.allowGradient_),
156  evalOnConstruct_(rhs.evalOnConstruct_),
157  valueExpr_(rhs.valueExpr_),
158  gradExpr_(rhs.gradExpr_),
159  fracExpr_(rhs.fracExpr_)
160 {}
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
166 {
167  os.writeEntryIfDifferent<bool>("evalOnConstruct", false, evalOnConstruct_);
168 
169  // Do not emit debug_ value
170 
171  if (!valueExpr_.empty())
172  {
173  os.writeEntry("valueExpr", valueExpr_);
174  }
175  if (!gradExpr_.empty())
176  {
177  os.writeEntry("gradientExpr", gradExpr_);
178  }
179  if (!fracExpr_.empty())
180  {
181  os.writeEntry("fractionExpr", fracExpr_);
182  }
183 }
184 
185 
186 // ************************************************************************* //
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:231
Foam::expressions::patchExprFieldBase::debug_
bool debug_
Definition: patchExprFieldBase.H:94
Foam::facePointPatch
A pointPatch based on a polyPatch.
Definition: facePointPatch.H:57
Foam::expressions::patchExprFieldBase::allowGradient_
bool allowGradient_
Definition: patchExprFieldBase.H:95
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::expressions::patchExprFieldBase::gradExpr_
expressions::exprString gradExpr_
Definition: patchExprFieldBase.H:102
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::facePointPatch::index
virtual label index() const
Return the index of this patch in the pointBoundaryMesh.
Definition: facePointPatch.H:166
patchExprFieldBase.H
Foam::expressions::patchExprFieldBase
Base class for managing patches with expressions. The expected input supports values,...
Definition: patchExprFieldBase.H:88
Foam::expressions::patchExprFieldBase::patchExprFieldBase
patchExprFieldBase()
Null constructor.
Definition: patchExprFieldBase.C:58
Foam::MeshObject::mesh
const Mesh & mesh() const
Definition: MeshObject.H:122
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::fvPatch
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:63
Foam::pointBoundaryMesh::mesh
const pointMesh & mesh() const
Return the mesh reference.
Definition: pointBoundaryMesh.H:96
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
fvMesh.H
Foam::expressions::patchExprFieldBase::fracExpr_
expressions::exprString fracExpr_
Definition: patchExprFieldBase.H:103
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::fvMesh::boundary
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:589
Foam::expressions::patchExprFieldBase::getFvPatch
static const fvPatch & getFvPatch(const facePointPatch &fp)
Find (guess) fvPatch from a pointPatch.
Definition: patchExprFieldBase.C:38
Foam::expressions::patchExprFieldBase::write
void write(Ostream &os) const
Write.
Definition: patchExprFieldBase.C:165
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
facePointPatch.H
Foam::expressions::exprString
Definition: exprString.H:60
Foam::pointPatch::boundaryMesh
const pointBoundaryMesh & boundaryMesh() const
Return boundaryMesh reference.
Definition: pointPatch.H:135
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:219
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:101
Foam::expressions::patchExprFieldBase::evalOnConstruct_
bool evalOnConstruct_
Slightly dodgy concept here.
Definition: patchExprFieldBase.H:98
pointMesh.H