limitFields.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) 2019-2023 OpenCFD Ltd
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "limitFields.H"
29 #include "fieldTypes.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38  defineTypeNameAndDebug(limitFields, 0);
39  addToRunTimeSelectionTable(functionObject, limitFields, dictionary);
40 }
41 }
42 
43 const Foam::Enum
44 <
46 >
48 ({
49  { limitType::CLAMP_MIN, "min" },
50  { limitType::CLAMP_MAX, "max" },
51  { limitType::CLAMP_RANGE, "range" },
52  { limitType::CLAMP_RANGE, "both" },
53 });
54 
55 
56 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
57 
59 (
60  const word& fieldName
61 )
62 {
63  auto* fieldPtr = obr_.getObjectPtr<volScalarField>(fieldName);
64  if (!fieldPtr)
65  {
66  return false;
67  }
68 
69  auto& field = *fieldPtr;
70 
71  Log << " Limiting field " << fieldName << ":";
72 
73  if (limitType::CLAMP_NONE != withBounds_)
74  {
75  MinMax<scalar> currentRange = gMinMax(field);
76 
77  if (withBounds_ & limitType::CLAMP_MIN)
78  {
79  Log << " min(" << currentRange.min() << ')';
80  }
81  if (withBounds_ & limitType::CLAMP_MAX)
82  {
83  Log << " max(" << currentRange.max() << ')';
84  }
85  }
86 
87  if (limitType::CLAMP_MIN == withBounds_)
88  {
89  field.clamp_min(min_);
90  }
91  else if (limitType::CLAMP_MAX == withBounds_)
92  {
93  field.clamp_max(max_);
94  }
95  else if (limitType::CLAMP_RANGE == withBounds_)
96  {
97  field.clamp_range(min_, max_);
98  }
99 
100  Log << endl;
101 
102  return true;
103 }
104 
105 
106 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
107 
109 (
110  const word& name,
111  const Time& runTime,
112  const dictionary& dict
113 )
114 :
116  fieldSet_(mesh_),
117  withBounds_(limitType::CLAMP_NONE),
118  min_(-VGREAT),
119  max_(VGREAT)
120 {
121  read(dict);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
128 {
129  withBounds_ = limitType::CLAMP_NONE;
130 
132  {
133  Info<< type() << " " << name() << ":" << nl;
134 
135  withBounds_ = limitTypeNames_.get("limit", dict);
136 
137  if (withBounds_ & limitType::CLAMP_MIN)
138  {
139  min_ = dict.get<scalar>("min");
140  Info<< " Imposing lower limit " << min_ << nl;
141  }
142 
143  if (withBounds_ & limitType::CLAMP_MAX)
144  {
145  max_ = dict.get<scalar>("max");
146  Info<< " Imposing upper limit " << max_ << nl;
147  }
148 
149  fieldSet_.read(dict);
150 
151  Info<< endl;
152 
153  return true;
154  }
155 
156  return false;
157 }
158 
159 
161 {
162  fieldSet_.updateSelection();
163 
164  Log << type() << ' ' << name() << ':' << nl;
165 
166  label count = 0, total = 0;
167 
168  for (const word& fieldName : fieldSet_.selectionNames())
169  {
170  ++total;
171  if
172  (
173  limitScalarField(fieldName)
174  || limitField<vector>(fieldName)
175  || limitField<sphericalTensor>(fieldName)
176  || limitField<symmTensor>(fieldName)
177  || limitField<tensor>(fieldName)
178  )
179  {
180  ++count;
181  }
182  }
183 
184  if (debug)
185  {
186  Log << " - limited " << count << '/' << total << " fields";
187  }
189  Log << endl;
190 
191  return true;
192 }
193 
194 
196 {
197  for (const word& fieldName : fieldSet_.selectionNames())
198  {
199  lookupObject<regIOobject>(fieldName).write();
200  }
201 
202  return true;
203 }
204 
205 
206 // ************************************************************************* //
virtual bool write()
Write the limitFields.
Definition: limitFields.C:188
dictionary dict
scalar min_
Minimum limit.
Definition: limitFields.H:212
defineTypeNameAndDebug(ObukhovLength, 0)
rDeltaTY field()
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static const Enum< limitType > limitTypeNames_
Limit type names.
Definition: limitFields.H:197
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Type * getObjectPtr(const word &name, const bool recursive=false) const
Return non-const pointer to the object of the given Type, using a const-cast to have it behave like a...
scalar max_
Maximum limit.
Definition: limitFields.H:217
MinMax< Type > gMinMax(const FieldField< Field, Type > &f)
limitFields(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: limitFields.C:102
int debug
Static debugging option.
bool limitScalarField(const word &fieldName)
Limit a scalar field.
Definition: limitFields.C:52
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
limitType withBounds_
Limiting type.
Definition: limitFields.H:207
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
virtual bool read(const dictionary &dict)
Read the field min/max data.
Definition: limitFields.C:120
const objectRegistry & obr_
Reference to the region objectRegistry.
#define Log
Definition: PDRblock.C:28
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual bool read(const dictionary &dict)
Read optional controls.
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
virtual bool execute()
Execute, currently does nothing.
Definition: limitFields.C:153
Namespace for OpenFOAM.