Loading...
Searching...
No Matches
PDRblockLocation.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 "PDRblock.H"
29#include "gradingDescriptors.H"
30
31// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32
33namespace Foam
34{
35
36// Prepend a value by shifting contents
37template<class T>
38static void prependList(List<T>& list, const T& val)
39{
40 const label oldLen = list.size();
41 list.resize(oldLen + 1);
42
43 for (label i = oldLen; i > 0; --i)
44 {
45 list[i] = std::move(list[i-1]);
46 }
47
48 list[0] = val;
49}
50
51} // End namespace Foam
52
53
54// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
55
57(
58 const scalarList& x,
59 const scalarList& y,
60 const scalarList& z
61)
62{
63 return boundBox
64 (
65 point(x.first(), y.first(), z.first()),
66 point(x.last(), y.last(), z.last())
67 );
68}
69
70
71Foam::Vector<Foam::gradingDescriptors>
73{
75 (
76 ctrl.x().grading(),
77 ctrl.y().grading(),
78 ctrl.z().grading()
79 );
80}
81
84{
85 return labelVector
86 (
87 ctrl.x().nCells(),
88 ctrl.y().nCells(),
89 ctrl.z().nCells()
90 );
91}
92
93
94// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95
97{
98 label nTotal = 0;
99 for (const label nDiv : divisions_)
100 {
101 nTotal += nDiv;
102 }
103
104 return nTotal;
105}
106
107
109{
110 // Begin/end nodes for each segment
111 const scalarList& knots = *this;
112
113 gradingDescriptors gds(divisions_.size());
114
115 forAll(gds, i)
116 {
117 //- Construct from components
118 gds[i] = gradingDescriptor
119 (
120 knots[i+1] - knots[i], // blockFraction from delta
121 divisions_[i], // nDivFraction from nDivs
122 expansion_[i]
123 );
124 }
125
126 gds.normalise();
128 return gds;
129}
130
131
133{
134 // Begin/end nodes for each segment
135 scalarList& knots = *this;
136
137 knots.resize(len, Zero);
138
139 len = Foam::max(0, len-1);
140
141 divisions_.resize(len, Zero);
142 expansion_.resize(len, Zero);
143}
144
145
147(
148 const scalar p,
149 const label nDiv,
150 scalar expRatio
151)
152{
153 // Begin/end nodes for each segment
154 scalarList& knots = *this;
155
156 // Is monotonic?
157 if (knots.size() && (p <= knots.last()))
158 {
160 << "Cannot append point " << p
161 << " which is <= last value " << knots.last() << endl;
162 return;
163 }
164
165 if (nDiv < 1)
166 {
168 << "Negative or zero divisions " << nDiv << endl;
169 return;
170 }
171
172 // Correct expansion ratios - negative is the same as inverse.
173 if (expRatio < 0)
174 {
175 expRatio = 1.0/(-expRatio);
176 }
177 else if (equal(expRatio, 0))
178 {
179 expRatio = 1;
180 }
181
182 // Now append (push_back)
183 knots.append(p);
184 divisions_.append(nDiv);
185 expansion_.append(expRatio);
186}
187
188
190(
191 const scalar p,
192 const label nDiv,
193 scalar expRatio
194)
195{
196 // Begin/end nodes for each segment
197 scalarList& knots = static_cast<scalarList&>(*this);
198
199 // Is monotonic?
200 if (knots.size() && (p >= knots.first()))
201 {
203 << "Cannot prepend point " << p
204 << " which is >= first value " << knots.first() << endl;
205 return;
206 }
207
208 if (nDiv < 1)
209 {
211 << "Negative or zero divisions " << nDiv << endl;
212 return;
213 }
214
215 // Correct expansion ratios - negative is the same as inverse.
216 if (expRatio < 0)
217 {
218 expRatio = 1.0/(-expRatio);
219 }
220 else if (equal(expRatio, 0))
221 {
222 expRatio = 1;
223 }
224
225 // Now prepend (push_front)
226 prependList(knots, p);
228 prependList(expansion_, expRatio);
229}
230
231
233(
234 Ostream& os,
235 const direction cmpt
236) const
237{
238 if (cmpt < vector::nComponents)
239 {
240 os.beginBlock(vector::componentNames[cmpt]);
241 }
242
243
244 const scalarList& knots = *this;
245
246 os << indent << "points " << flatOutput(knots);
247 os.endEntry();
248
249 os << indent << "nCells " << flatOutput(divisions_);
250 os.endEntry();
251
252 os << indent << "ratios " << flatOutput(expansion_);
253 os.endEntry();
254
255 if (cmpt < vector::nComponents)
256 {
257 os.endBlock();
259 os << nl;
260}
261
262
264(
265 const scalar low,
266 const scalar upp,
267 const label nCells
268)
269{
270 scalarList& grid = *this;
271
272 grid.resize_nocopy(nCells+1);
273
274 grid.front() = low;
275 grid.back() = upp;
276
277 const scalar span = (upp - low);
278
279 for (label pointi = 1; pointi < nCells; ++pointi)
280 {
281 grid[pointi] = low + (pointi*span)/nCells;
282 }
283}
284
285
287{
289
290 for (label edgei = 0; edgei < this->nCells(); ++edgei)
291 {
292 limits.add(width(edgei));
293 }
295 return limits;
296}
297
298
299Foam::label Foam::PDRblock::location::findCell(const scalar p) const
300{
301 if (scalarList::empty() || p < first() || p > last())
302 {
303 return -1;
304 }
305 else if (equal(p, first()))
306 {
307 return 0;
308 }
309 else if (equal(p, last()))
310 {
311 return nCells()-1;
312 }
313 else if (p < first() || p > last())
314 {
315 // The point is out-of-bounds
316 return -1;
317 }
318
319 // Binary search, finds lower index and thus corresponds to the
320 // cell in which the point is found
321 return findLower(*this, p);
322}
323
324
326(
327 const scalar p,
328 const scalar tol
329) const
330{
331 if (scalarList::empty())
332 {
333 return -1;
334 }
335 else if (Foam::mag(p - first()) <= tol)
336 {
337 return 0;
338 }
339 else if (Foam::mag(p - last()) <= tol)
340 {
341 return scalarList::size()-1;
342 }
343 else if (p < first() || p > last())
344 {
345 // The point is out-of-bounds
346 return -1;
347 }
348
349 // Linear search
350 label i = 0;
351 scalar delta = GREAT;
352
353 for (const scalar& val : *this)
354 {
355 const scalar diff = mag(p - val);
356
357 if (diff <= tol)
358 {
359 return i;
360 }
361 else if (delta < diff)
362 {
363 // Moving further away
364 break;
365 }
366
367 delta = diff;
368 ++i;
369 }
370
371 // This point is within bounds, but not near a grid-point
372 return -2;
373}
374
375
376// ************************************************************************* //
scalar y
scalar delta
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
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition ListI.H:171
void append(const T &val)
Append an element at the end of the list.
Definition List.H:497
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
label findIndex(const scalar p, const scalar tol) const
Find the grid index, within the given tolerance.
label findCell(const scalar p) const
Find the cell index enclosing this location.
scalarMinMax edgeLimits() const
Return min/max edge lengths.
void reset(const scalar low, const scalar upp, const label nCells)
Reset with min/max range and number of divisions.
label nCells() const noexcept
The number of cells in this direction.
Definition PDRblockI.H:30
scalar width(const label i) const
Cell size at element position.
Definition PDRblockI.H:84
vector span(const label i, const label j, const label k) const
Cell dimensions at i,j,k position.
Definition PDRblockI.H:177
const Vector< location > & grid() const noexcept
The grid point locations in the i,j,k (x,y,z) directions.
Definition PDRblock.H:718
const boundBox & bounds() const noexcept
The mesh bounding box.
Definition PDRblock.H:741
Vector< gradingDescriptors > grading() const
Equivalent edge grading descriptors in (x,y,z) directions.
Definition PDRblock.C:730
T & first()
Access first element of the list, position [0].
Definition UList.H:957
bool empty() const noexcept
Definition UList.H:701
T & back()
Access last element of the list, position [size()-1].
Definition UListI.H:253
T & front()
Access first element of the list, position [0].
Definition UListI.H:239
label size() const noexcept
Definition UList.H:706
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
T & last()
Access last element of the list, position [size()-1].
Definition UList.H:971
static const char *const componentNames[]
static constexpr direction nComponents
Number of components in this vector space.
Templated 3D Vector derived from VectorSpace adding construction from 3 components,...
Definition Vector.H:61
A bounding box defined in terms of min/max extrema points.
Definition boundBox.H:71
Handles the specification for grading within a section of a block.
List of gradingDescriptor for the sections of a block with additional IO functionality.
void normalise()
Normalize blockFractions and nDivFractions for the list of gradingDescriptors, and call correct().
const labelVector & sizes() const noexcept
The (i,j,k) addressing dimensions.
volScalarField & p
auto limits
Definition setRDeltaT.H:186
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
Vector< label > labelVector
Vector of labels.
Definition labelVector.H:47
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition label.H:180
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition MinMax.H:97
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Binary search to find the index of the last element in a sorted list that is less than value.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition triad.C:373
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
uint8_t direction
Definition direction.H:49
vector point
Point is a vector.
Definition point.H:37
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
static void prependList(List< T > &list, const T &val)
List< scalar > scalarList
List of scalar.
Definition scalarList.H:32
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
gradingDescriptors grading() const
Return edge grading descriptors for the locations.
scalarList expansion_
The expansion ratio per segment.
Definition PDRblock.H:308
labelList divisions_
The number of division per segment.
Definition PDRblock.H:303
void prepend(const scalar p, label nDiv, scalar expRatio=1)
Add point/divisions/expand to front of list (push_front).
void append(const scalar p, label nDiv, scalar expRatio=1)
Add point/divisions/expand to end of list (push_back).
void resize(label len)
Resize lists.
label nCells() const
Total number of cells in this direction.
void writeDict(Ostream &os, const direction cmpt) const
Write as dictionary contents for specified vector direction.