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  if (limitType::CLAMP_NONE != withBounds_)
72  {
73  MinMax<scalar> currentRange = gMinMax(field);
74 
75  if (withBounds_ & limitType::CLAMP_MIN)
76  {
77  Log << ": min(" << currentRange.min() << ')';
78  }
79  if (withBounds_ & limitType::CLAMP_MAX)
80  {
81  Log << ": max(" << currentRange.max() << ')';
82  }
83  }
84 
85  if (limitType::CLAMP_MIN == withBounds_)
86  {
87  field.clamp_min(min_);
88  }
89  else if (limitType::CLAMP_MAX == withBounds_)
90  {
91  field.clamp_max(max_);
92  }
93  else if (limitType::CLAMP_RANGE == withBounds_)
94  {
95  field.clamp_range(min_, max_);
96  }
97 
98  return true;
99 }
100 
101 
102 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103 
105 (
106  const word& name,
107  const Time& runTime,
108  const dictionary& dict
109 )
110 :
112  fieldSet_(mesh_),
113  withBounds_(limitType::CLAMP_NONE),
114  min_(-VGREAT),
115  max_(VGREAT)
116 {
117  read(dict);
118 }
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124 {
125  withBounds_ = limitType::CLAMP_NONE;
126 
128  {
129  Info<< type() << " " << name() << ":" << nl;
130 
131  withBounds_ = limitTypeNames_.get("limit", dict);
132 
133  if (withBounds_ & limitType::CLAMP_MIN)
134  {
135  min_ = dict.get<scalar>("min");
136  Info<< " Imposing lower limit " << min_ << nl;
137  }
138 
139  if (withBounds_ & limitType::CLAMP_MAX)
140  {
141  max_ = dict.get<scalar>("max");
142  Info<< " Imposing upper limit " << max_ << nl;
143  }
144 
145  fieldSet_.read(dict);
146 
147  Info<< endl;
148 
149  return true;
150  }
151 
152  return false;
153 }
154 
155 
157 {
158  fieldSet_.updateSelection();
159 
160  Log << type() << ' ' << name() << ':' << nl;
161 
162  label count = 0, total = 0;
163 
164  for (const word& fieldName : fieldSet_.selectionNames())
165  {
166  ++total;
167  if
168  (
169  limitScalarField(fieldName)
170  || limitField<vector>(fieldName)
171  || limitField<sphericalTensor>(fieldName)
172  || limitField<symmTensor>(fieldName)
173  || limitField<tensor>(fieldName)
174  )
175  {
176  ++count;
177  }
178  }
179 
180  if (debug)
181  {
182  Log << " - limited " << count << '/' << total << " fields";
183  }
185  Log << endl;
186 
187  return true;
188 }
189 
190 
192 {
193  for (const word& fieldName : fieldSet_.selectionNames())
194  {
195  lookupObject<regIOobject>(fieldName).write();
196  }
197 
198  return true;
199 }
200 
201 
202 // ************************************************************************* //
virtual bool write()
Write the limitFields.
Definition: limitFields.C:184
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:98
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:116
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:149
Namespace for OpenFOAM.