Loading...
Searching...
No Matches
AMICache.H
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) 2025 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
26Class
27 Foam::AMICache
28
29Description
30 Provides caching of weights and addressing to AMIInterpolation
31
32SourceFiles
33 AMICache.C
34
35SeeAlso
36 Foam::AMIInterpolation.H
37
38\*---------------------------------------------------------------------------*/
39
40#ifndef Foam_AMICache_H
41#define Foam_AMICache_H
42
43#include "cylindricalCS.H"
44#include "mapDistribute.H"
45#include "className.H"
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
54/*---------------------------------------------------------------------------*\
55 Class AMICache Declaration
56\*---------------------------------------------------------------------------*/
57
58class AMICache
59{
60public:
61
62 // Public Data Types
64 //- Tolerance used when caching the AMI to identify e.g. if the
65 //- current rotation angle has already been captured
66 static scalar cacheThetaTolerance_;
67
68
69private:
70
71 // Private Data
72
73 //- Cache size
74 label size_;
75
76 //- Axis of rotation for rotational cyclics
77 vector rotationAxis_;
78
79 //- Point on axis of rotation for rotational cyclics
80 point rotationCentre_;
81
82 //- Maximum number of bins for which to search for cached bins
83 label nThetaStencilMax_;
84
85 //- Flag that values should always be cached if the bin is empty
86 // even if a valid stencil an be created
87 bool forceCache_;
88
89 //- Flag to indicate that the cache is complete
90 bool complete_;
91
92 //- Flag to indicate interpolation direction
93 mutable bool toSource_;
94
95 //- Cache index 0 in lists
96 label index0_;
97
98 //- Cache index 1 in lists
99 label index1_;
100
101 //- Interpolation weight between cache indices 0 and 1
102 scalar interpWeight_;
103
104 //- Local co-ordinate system
106
107 //- List of cached angle snapshots
108 // Note: intialised to GREAT
109 List<scalar> theta_;
110
111 //- List of source addresses
112 List<labelListList> cachedSrcAddress_;
113
114 //- List of source weights
115 List<scalarListList> cachedSrcWeights_;
116
117 //- List of source weights sums
118 List<scalarField> cachedSrcWeightsSum_;
119
120 //- List of source parallel maps
121 List<autoPtr<mapDistribute>> cachedSrcMapPtr_;
122
123 //- List of target addresses
124 List<labelListList> cachedTgtAddress_;
125
126 //- List of target weights
127 List<scalarListList> cachedTgtWeights_;
128
129 //- List of target weights sums
130 List<scalarField> cachedTgtWeightsSum_;
131
132 //- List of target parallel maps
133 List<autoPtr<mapDistribute>> cachedTgtMapPtr_;
134
135
136 // Private Member Functions
137
138 //- Get rotation angle for point using local co-ordinate system
139 scalar getRotationAngle(const point& globalPoint) const;
140
141
142public:
143
144 // RunTime Type Information
145
146 TypeName("AMICache");
147
148 // Constructors
149
150 //- Null constructor
151 AMICache(const bool toSource = true);
152
153 //- Construct from dictionary
154 AMICache(const dictionary& dict, const bool toSource = true);
155
156 //- Construct as copy
157 AMICache(const AMICache& cache);
158
159 //- Construct from agglomeration of AMIInterpolation. Agglomeration
160 //- passed in as new coarse size and addressing from fine from coarse
162 (
163 const AMICache& cache,
164 const AMIInterpolation& fineAMI,
165 const labelList& sourceRestrictAddressing,
166 const labelList& targetRestrictAddressing
167 );
168
169 //- Construct from stream
170 AMICache(Istream& is);
171
172
173 //- Destructor
174 virtual ~AMICache() = default;
175
176
177 // Member Functions
178
179 //- Return true if cache is active
180 constexpr bool active() const noexcept { return size_ > 0; }
181
182 //- Set the cache size; also deactivates the cache if size <= 0
183 void setSize(const label size) noexcept
184 {
185 size_ = size;
186 }
188 //- Return cache size
189 constexpr label size() const noexcept { return size_; }
190
191 //- Return true if cache is complete
192 constexpr bool complete() const noexcept { return complete_; }
193
194 //- Return cache lower bound index
195 constexpr label index0() const noexcept { return index0_; }
196
197 //- Return cache upper bound index
198 constexpr label index1() const noexcept { return index1_; }
199
200 //- Return cache interpolation weight
201 constexpr label weight() const noexcept { return interpWeight_; }
202
203 //- Return list of cached rotation angles
204 const List<scalar>& theta() const noexcept { return theta_; }
205
206 //- Return List of source addresses
207 const List<labelListList>& cachedSrcAddress() const noexcept
208 {
209 return cachedSrcAddress_;
210 }
211
212 //- Return List of source weights
213 const List<scalarListList>& cachedSrcWeights() const noexcept
214 {
215 return cachedSrcWeights_;
216 }
217
218 //- Return List of source weights sums
219 const List<scalarField>& cachedSrcWeightsSum() const noexcept
220 {
221 return cachedSrcWeightsSum_;
222 }
223
224 //- Return List of source parallel maps
225 const List<autoPtr<mapDistribute>>& cachedSrcMapPtr() const noexcept
226 {
227 return cachedSrcMapPtr_;
228 }
229
230 //- Return List of target addresses
232 {
233 return cachedTgtAddress_;
234 }
236 //- Return List of target weights
238 {
239 return cachedTgtWeights_;
241
242 //- Return List of target weights sums
244 {
245 return cachedTgtWeightsSum_;
246 }
247
248 //- Return List of target parallel maps
250 {
251 return cachedTgtMapPtr_;
252 }
254 //- Apply cached evaluation based on user supplied evaluation function
255 template<class Type, class EvalFunction>
256 bool apply(List<Type>& result, const EvalFunction& eval) const;
257
258 //- Flag that lower bound is applicable
259 constexpr bool applyLower() const noexcept
260 {
261 return index0_ != -1 && index1_ == -1;
262 }
264 //- Flag that upper bound is applicable
265 constexpr bool applyUpper() const noexcept
266 {
267 return index0_ == -1 && index1_ != -1;
269
270 //- Flag that interpolation is applicable
271 constexpr bool applyInterpolate() const noexcept
272 {
273 return index0_ != -1 && index1_ != -1;
274 }
275
276 //- Set the interpolation direction
277 constexpr bool setDirection(bool toSource) const noexcept
279 toSource_ = toSource;
280 return toSource_;
281 }
282
283 //- Restore AMI weights and addressing from the cache
284 bool restoreCache(const point& globalPoint);
285
286 //- Add AMI weights and addressing to the cache
287 void addToCache
288 (
289 const AMIInterpolation& ami,
290 const point& globalPoint
291 );
292
293 //- Check cache index is within bounds
294 void checkBounds(const label index) const
295 {
296 if ((index < 0) || (index > size_-1))
297 {
299 << "Supplied out of bounds: " << index << "/"
300 << size_ << abort(FatalError);
301 }
303
304 //- Return true of the cache index is set for bini
305 bool validIndex(const label bini) const
306 {
307 // Note: theta_ is initialised to GREAT
308 return theta_[bini] < constant::mathematical::twoPi;
309 }
311 //- Return bin index for angle theta
312 label thetaIndex(const scalar theta) const noexcept
313 {
314 return
315 floor
316 (
319 *size_
320 );
321 }
322
323 // Helper functions to retrieve cached values at index0 and index1
324 // Note: uses interpolation direction toSource_
325 #undef defineMethods01
326 #define defineMethods01(Src, Tgt, idx) \
327 const labelListList& c##Src##Address##idx() const \
328 { \
329 checkBounds(index##idx##_); \
330 return toSource_ ? \
331 cached##Src##Address_[index##idx##_] \
332 : cached##Tgt##Address_[index##idx##_]; \
333 } \
334 const scalarListList& c##Src##Weights##idx() const \
335 { \
336 checkBounds(index##idx##_); \
337 return toSource_ ? \
338 cached##Src##Weights_[index##idx##_] \
339 : cached##Tgt##Weights_[index##idx##_]; \
340 } \
341 const scalarField& c##Src##WeightsSum##idx() const \
342 { \
343 checkBounds(index##idx##_); \
344 return toSource_ ? \
345 cached##Src##WeightsSum_[index##idx##_] \
346 : cached##Tgt##WeightsSum_[index##idx##_]; \
347 } \
348 const autoPtr<mapDistribute>& c##Src##MapPtr##idx() const \
349 { \
350 checkBounds(index##idx##_); \
351 return toSource_ ? \
352 cached##Src##MapPtr_[index##idx##_] \
353 : cached##Tgt##MapPtr_[index##idx##_]; \
354 }
355
356 defineMethods01(Src, Tgt, 0)
357 defineMethods01(Src, Tgt, 1)
358 defineMethods01(Tgt, Src, 0)
359 defineMethods01(Tgt, Src, 1)
360
361
362 // Helper functions to retrieve cached values at supplied index
363 // Note: uses interpolation direction toSource_
364 #undef defineMethodsIndex
365 #define defineMethodsIndex(Src, Tgt) \
366 const labelListList& c##Src##Address(const label index) const \
367 { \
368 checkBounds(index); \
369 return toSource_ ? \
370 cached##Src##Address_[index] \
371 : cached##Tgt##Address_[index]; \
372 } \
373 const scalarListList& c##Src##Weights(const label index) const \
374 { \
375 checkBounds(index); \
376 return toSource_ ? \
377 cached##Src##Weights_[index] \
378 : cached##Tgt##Weights_[index]; \
379 } \
380 const scalarField& c##Src##WeightsSum(const label index) const \
381 { \
382 checkBounds(index); \
383 return toSource_ ? \
384 cached##Src##WeightsSum_[index] \
385 : cached##Tgt##WeightsSum_[index]; \
386 } \
387 const autoPtr<mapDistribute>& c##Src##MapPtr(const label index) \
388 const \
389 { \
390 checkBounds(index); \
391 return toSource_ ? \
392 cached##Src##MapPtr_[index] \
393 : cached##Tgt##MapPtr_[index]; \
394 }
396 defineMethodsIndex(Src, Tgt)
397 defineMethodsIndex(Tgt, Src)
398
399
400 // I-O
401
402 //- Write AMI as a dictionary
403 void write(Ostream& os) const;
404
405 //- Write AMI raw
406 bool writeData(Ostream& os) const;
407};
409
410// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411
412} // End namespace Foam
413
414// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
415
416#endif
418// ************************************************************************* //
#define defineMethods01(Src, Tgt, idx)
Definition AMICache.H:431
#define defineMethodsIndex(Src, Tgt)
Definition AMICache.H:470
constexpr bool applyUpper() const noexcept
Flag that upper bound is applicable.
Definition AMICache.H:356
const List< autoPtr< mapDistribute > > & cachedSrcMapPtr() const noexcept
Return List of source parallel maps.
Definition AMICache.H:302
const List< autoPtr< mapDistribute > > & cachedTgtMapPtr() const noexcept
Return List of target parallel maps.
Definition AMICache.H:334
constexpr bool setDirection(bool toSource) const noexcept
Set the interpolation direction.
Definition AMICache.H:372
void checkBounds(const label index) const
Check cache index is within bounds.
Definition AMICache.H:395
constexpr bool complete() const noexcept
Return true if cache is complete.
Definition AMICache.H:253
constexpr bool active() const noexcept
Return true if cache is active.
Definition AMICache.H:235
const List< labelListList > & cachedSrcAddress() const noexcept
Return List of source addresses.
Definition AMICache.H:278
constexpr label index1() const noexcept
Return cache upper bound index.
Definition AMICache.H:263
bool writeData(Ostream &os) const
Write AMI raw.
Definition AMICache.C:617
const List< scalarField > & cachedSrcWeightsSum() const noexcept
Return List of source weights sums.
Definition AMICache.H:294
static scalar cacheThetaTolerance_
Tolerance used when caching the AMI to identify e.g. if the current rotation angle has already been c...
Definition AMICache.H:63
const List< scalarListList > & cachedSrcWeights() const noexcept
Return List of source weights.
Definition AMICache.H:286
virtual ~AMICache()=default
Destructor.
constexpr bool applyInterpolate() const noexcept
Flag that interpolation is applicable.
Definition AMICache.H:364
void write(Ostream &os) const
Write AMI as a dictionary.
Definition AMICache.C:604
constexpr bool applyLower() const noexcept
Flag that lower bound is applicable.
Definition AMICache.H:348
constexpr label size() const noexcept
Return cache size.
Definition AMICache.H:248
label thetaIndex(const scalar theta) const noexcept
Return bin index for angle theta.
Definition AMICache.H:417
bool validIndex(const label bini) const
Return true of the cache index is set for bini.
Definition AMICache.H:408
const List< scalarField > & cachedTgtWeightsSum() const noexcept
Return List of target weights sums.
Definition AMICache.H:326
void addToCache(const AMIInterpolation &ami, const point &globalPoint)
Add AMI weights and addressing to the cache.
Definition AMICache.C:337
bool apply(List< Type > &result, const EvalFunction &eval) const
Apply cached evaluation based on user supplied evaluation function.
const List< scalar > & theta() const noexcept
Return list of cached rotation angles.
Definition AMICache.H:273
void setSize(const label size) noexcept
Set the cache size; also deactivates the cache if size <= 0.
Definition AMICache.H:240
bool restoreCache(const point &globalPoint)
Restore AMI weights and addressing from the cache.
Definition AMICache.C:418
const List< scalarListList > & cachedTgtWeights() const noexcept
Return List of target weights.
Definition AMICache.H:318
constexpr label weight() const noexcept
Return cache interpolation weight.
Definition AMICache.H:268
constexpr label index0() const noexcept
Return cache lower bound index.
Definition AMICache.H:258
AMICache(const bool toSource=true)
Null constructor.
Definition AMICache.C:102
const List< labelListList > & cachedTgtAddress() const noexcept
Return List of target addresses.
Definition AMICache.H:310
TypeName("AMICache")
Interpolation class dealing with transfer of data between two primitive patches with an arbitrary mes...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Macro definitions for declaring ClassName(), NamespaceName(), etc.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
constexpr scalar twoPi(2 *M_PI)
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
errorManip< error > abort(error &err)
Definition errorManip.H:139
vector point
Point is a vector.
Definition point.H:37
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
Vector< scalar > vector
Definition vector.H:57
runTime write()
dictionary dict
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition typeInfo.H:68