Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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"
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
36namespace functionObjects
37{
40}
41}
42
43const Foam::Enum
44<
46>
48({
49 { limitType::CLAMP_MIN, "min" },
50 { limitType::CLAMP_MAX, "max" },
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
74 {
75 MinMax<scalar> currentRange = gMinMax(field);
76
78 {
79 Log << " min(" << currentRange.min() << ')';
80 }
82 {
83 Log << " max(" << currentRange.max() << ')';
84 }
85 }
86
88 {
89 field.clamp_min(min_);
90 }
92 {
93 field.clamp_max(max_);
94 }
96 {
97 field.clamp_range(min_, max_);
98 }
99
100 Log << endl;
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)
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// ************************************************************************* //
#define Log
Definition PDRblock.C:28
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition MinMax.H:106
const T & max() const noexcept
The max value.
Definition MinMax.H:217
const T & min() const noexcept
The min value.
Definition MinMax.H:207
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Abstract base-class for Time/database function objects.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
const fvMesh & mesh_
Reference to the fvMesh.
fvMeshFunctionObject(const fvMeshFunctionObject &)=delete
No copy construct.
Limits input fields to user-specified min and max bounds.
@ CLAMP_MAX
Clamp maximum value.
@ CLAMP_MIN
Clamp minimum value.
bool limitScalarField(const word &fieldName)
Limit a scalar field.
Definition limitFields.C:52
volFieldSelection fieldSet_
Fields to limit.
static const Enum< limitType > limitTypeNames_
Limit type names.
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
limitFields(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
virtual bool execute()
Execute the function-object operations.
bool limitField(const word &fieldName)
Limit a field.
virtual bool write()
Write the function-object results.
limitType withBounds_
Limiting type.
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
const objectRegistry & obr_
Reference to the region objectRegistry.
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...
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
rDeltaTY field()
engineTime & runTime
auto & name
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
Namespace for handling debugging switches.
Definition debug.C:45
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
GeometricField< scalar, fvPatchField, volMesh > volScalarField
messageStream Info
Information stream (stdout output on master, null elsewhere).
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
MinMax< Type > gMinMax(const FieldField< Field, Type > &f)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict