scalarRanges.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "scalarRanges.H"
27 #include "DynamicList.H"
28 #include "ListOps.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
33 :
34  List<scalarRange>(0)
35 {}
36 
37 
39 :
40  List<scalarRange>(0)
41 {
43 
44  while (is.good())
45  {
46  scalarRange sr(is);
47  if (sr.valid())
48  {
49  lst.append(sr);
50  }
51  }
52 
53  transfer(lst);
54 }
55 
56 
57 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
58 
59 bool Foam::scalarRanges::selected(const scalar value) const
60 {
61  forAll(*this, i)
62  {
63  if (operator[](i).selected(value))
64  {
65  return true;
66  }
67  }
68 
69  return false;
70 }
71 
72 
74 (
75  const List<scalar>& values
76 ) const
77 {
78  List<bool> lst(values.size(), false);
79 
80  // check ranges
81  forAll(values, i)
82  {
83  if (selected(values[i]))
84  {
85  lst[i] = true;
86  }
87  }
88 
89  // check specific values
90  forAll(*this, rangeI)
91  {
92  if (operator[](rangeI).isExact())
93  {
94  scalar target = operator[](rangeI).value();
95 
96  int nearestIndex = -1;
97  scalar nearestDiff = Foam::great;
98 
99  forAll(values, timeIndex)
100  {
101  scalar diff = fabs(values[timeIndex] - target);
102  if (diff < nearestDiff)
103  {
104  nearestDiff = diff;
105  nearestIndex = timeIndex;
106  }
107  }
108 
109  if (nearestIndex >= 0)
110  {
111  lst[nearestIndex] = true;
112  }
113  }
114  }
115 
116  return lst;
117 }
118 
119 
121 (
122  const List<scalar>& values
123 ) const
124 {
125  return subset(selected(values), values);
126 }
127 
128 
130 (
131  List<scalar>& values
132 ) const
133 {
134  inplaceSubset(selected(values), values);
135 }
136 
137 
138 // ************************************************************************* //
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
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:91
void transfer(List< scalarRange > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
A scalar range specifier.
Definition: scalarRange.H:63
bool valid() const
Is the range non-empty?
Definition: scalarRange.C:82
List< scalar > select(const List< scalar > &) const
Select a list of values that are within the ranges.
Definition: scalarRanges.C:121
bool selected(const scalar) const
Return true if the given value is within the ranges.
Definition: scalarRanges.C:59
scalarRanges()
Construct Null.
Definition: scalarRanges.C:32
void inplaceSelect(List< scalar > &) const
Select a list of values that are within the ranges.
Definition: scalarRanges.C:130
void inplaceSubset(const UList< T > &select, const T &value, ListType &)
Inplace extract elements of List when select is a certain value.
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:409
ListType subset(const UList< T > &select, const T &value, const ListType &)
Extract elements of List when select is a certain value.
label timeIndex
Definition: getTimeIndex.H:4