composedFunctionImplicitFunction.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 OpenCFD Ltd.
9 Copyright (C) 2019-2020 DLR
10-------------------------------------------------------------------------------
11License
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
30#include "scalarField.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35namespace Foam
36{
37 namespace implicitFunctions
38 {
39 defineTypeNameAndDebug(composedFunctionImplicitFunction, 0);
41 (
42 implicitFunction,
43 composedFunctionImplicitFunction,
44 dict
45 );
46 }
47}
48
49// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50
51const Foam::Enum
52<
53 Foam::implicitFunctions::composedFunctionImplicitFunction::modeType
54>
55Foam::implicitFunctions::composedFunctionImplicitFunction::modeTypeNames
56({
57 { modeType::ADD, "add" },
58 { modeType::SUBTRACT, "subtract" },
59 { modeType::MINDIST, "minDist" },
60 { modeType::INTERSECT, "intersect" },
61});
62
63
64Foam::label
65Foam::implicitFunctions::composedFunctionImplicitFunction::selectFunction
66(
67 const scalarField& values
68) const
69{
70 switch (mode_)
71 {
72 case modeType::MINDIST:
73 {
74 scalarField absVal(mag(values));
75 return findMin(absVal);
76 }
77 case modeType::ADD:
78 {
79 return findMax(values);
80 }
81 case modeType::SUBTRACT:
82 {
83 // Note: start at the second entry
84 const label idx = findMin(values, 1);
85
86 if (values[idx] < values[0] && pos(values[0]))
87 {
88 return idx;
89 }
90 else
91 {
92 return 0;
93 }
94 }
95 case modeType::INTERSECT:
96 {
97 return findMin(values);
98 }
99 default:
100 {
102 << "This mode is not supported only " << nl
103 << "Supported modes are: " << nl
104 << modeTypeNames
105 << abort(FatalError);
106 }
107 }
108
109 return -1;
110}
111
112
113// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
114
115Foam::implicitFunctions::composedFunctionImplicitFunction::
116composedFunctionImplicitFunction
117(
118 const dictionary& dict
119)
120:
121 functions_(),
122 mode_(modeTypeNames.get("mode", dict)),
123 values_()
124{
125 const dictionary& funcDict = dict.subDict("composedFunction");
126
127 functions_.resize(funcDict.size());
128 values_.resize(funcDict.size(), Zero);
129
130 label funci = 0;
131
132 for (const entry& dEntry : funcDict)
133 {
134 const word& key = dEntry.keyword();
135
136 if (!dEntry.isDict())
137 {
138 FatalIOErrorInFunction(funcDict)
139 << "Entry " << key << " is not a dictionary" << endl
140 << exit(FatalIOError);
141 }
142
143 const dictionary& subdict = dEntry.dict();
144
145 functions_.set
146 (
147 funci,
148 implicitFunction::New(subdict.get<word>("type"), subdict)
149 );
150
151 ++funci;
152 }
153}
154
155
156// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157
159(
160 const vector& p
161) const
162{
163 forAll(values_,i)
164 {
165 values_[i] = functions_[i].value(p);
166 }
167
168 const label idx = selectFunction(values_);
169
170 return values_[idx];
171}
172
173
175(
176 const vector& p
177) const
178{
179 forAll(values_,i)
180 {
181 values_[i] = mag(functions_[i].value(p));
182 }
183
184 const label minIdx = findMin(values_);
185
186 return functions_[minIdx].grad(p);
187}
188
189
190Foam::scalar
192(
193 const vector& p
194) const
195{
196 forAll(values_,i)
197 {
198 values_[i] = mag(functions_[i].value(p));
199 }
200
201 const label minIdx = findMin(values_);
202
203 return functions_[minIdx].distanceToSurfaces(p);
204}
205
206
207// ************************************************************************* //
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:61
Computes the gradient of an input field.
Definition: grad.H:157
virtual scalar distanceToSurfaces(const vector &p) const
virtual scalar value(const vector &p) const
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
volScalarField & p
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
label findMin(const ListType &input, label start=0)
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
label findMax(const ListType &input, label start=0)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333