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 -------------------------------------------------------------------------------
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 
30 #include "scalarField.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace 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 
51 const Foam::Enum
52 <
53  Foam::implicitFunctions::composedFunctionImplicitFunction::modeType
54 >
55 Foam::implicitFunctions::composedFunctionImplicitFunction::modeTypeNames
56 ({
57  { modeType::ADD, "add" },
58  { modeType::SUBTRACT, "subtract" },
59  { modeType::MINDIST, "minDist" },
60  { modeType::INTERSECT, "intersect" },
61 });
62 
63 
64 Foam::label
65 Foam::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 
115 Foam::implicitFunctions::composedFunctionImplicitFunction::
116 composedFunctionImplicitFunction
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 
190 Foam::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 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
virtual scalar value(const vector &p) const
Macros for easy insertion into run-time selection tables.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:414
virtual vector grad(const vector &p) const
virtual scalar distanceToSurfaces(const vector &p) const
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
dimensionedScalar pos(const dimensionedScalar &ds)
label findMin(const ListType &input, label start=0)
Linear search for the index of the min element, similar to std::min_element but for lists and returns...
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Vector< scalar > vector
Definition: vector.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:139
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
defineTypeNameAndDebug(combustionModel, 0)
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
volScalarField & p
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133