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 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 "ListOps.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
34 {
35  if (scalarList::empty())
36  {
37  return -1;
38  }
39  else if (equal(p, first()))
40  {
41  return 0;
42  }
43  else if (equal(p, last()))
44  {
45  return nCells()-1;
46  }
47  else if (p < first() || p > last())
48  {
49  // The point is out-of-bounds
50  return -1;
51  }
52 
53  // Binary search, finds lower index and thus corresponds to the
54  // cell in which the point is found
55  return findLower(*this, p);
56 }
57 
58 
60 (
61  const scalar p,
62  const scalar tol
63 ) const
64 {
65  if (scalarList::empty())
66  {
67  return -1;
68  }
69  else if (Foam::mag(p - first()) <= tol)
70  {
71  return 0;
72  }
73  else if (Foam::mag(p - last()) <= tol)
74  {
75  return scalarList::size()-1;
76  }
77  else if (p < first() || p > last())
78  {
79  // The point is out-of-bounds
80  return -1;
81  }
82 
83  // Linear search
84  label i = 0;
85  scalar delta = GREAT;
86 
87  for (const scalar& val : *this)
88  {
89  const scalar diff = mag(p - val);
90 
91  if (diff <= tol)
92  {
93  return i;
94  }
95  else if (delta < diff)
96  {
97  // Moving further away
98  break;
99  }
100 
101  delta = diff;
102  ++i;
103  }
104 
105  // This point is within bounds, but not near a grid-point
106  return -2;
107 }
108 
109 
110 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::diff
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:378
Foam::PDRblock::location::findCell
label findCell(const scalar p) const
Find the cell index enclosing this location.
Definition: PDRblockLocation.C:33
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::findLower
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
delta
scalar delta
Definition: LISASMDCalcMethod2.H:8
PDRblock.H
Foam::PDRblock::location::findIndex
label findIndex(const scalar p, const scalar tol) const
Find the grid index, within the given tolerance.
Definition: PDRblockLocation.C:60
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
ListOps.H
Various functions to operate on Lists.
Foam::PDRblock::location::nCells
label nCells() const
The number of cells in this direction.
Definition: PDRblockI.H:49
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46