Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / db / scalarRange / scalarRanges.C
blob17d9b1ffd44e87b06fc4dda6e70c9a074472ee34
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "scalarRanges.H"
27 #include "DynamicList.H"
28 #include "ListOps.H"
30 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
32 Foam::scalarRanges::scalarRanges()
34     List<scalarRange>(0)
38 Foam::scalarRanges::scalarRanges(Istream& is)
40     List<scalarRange>(0)
42     DynamicList<scalarRange> lst;
44     while (is.good())
45     {
46         scalarRange sr(is);
47         if (sr.isDefined())
48         {
49             lst.append(sr);
50         }
51     }
53     transfer(lst);
57 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
59 bool Foam::scalarRanges::selected(const scalar& value) const
61     forAll(*this, i)
62     {
63         if (operator[](i).selected(value))
64         {
65             return true;
66         }
67     }
69     return false;
73 Foam::List<bool> Foam::scalarRanges::selected
75     const List<scalar>& values
76 ) const
78     List<bool> lst(values.size(), false);
80     // check ranges
81     forAll(values, i)
82     {
83         if (selected(values[i]))
84         {
85             lst[i] = true;
86         }
87     }
89     // check specific values
90     forAll(*this, rangeI)
91     {
92         if (operator[](rangeI).isExact())
93         {
94             scalar target = operator[](rangeI).value();
96             int nearestIndex = -1;
97             scalar nearestDiff = Foam::GREAT;
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             }
109             if (nearestIndex >= 0)
110             {
111                 lst[nearestIndex] = true;
112             }
113         }
114     }
116     return lst;
120 Foam::List<Foam::scalar> Foam::scalarRanges::select
122     const List<scalar>& values
123 ) const
125     return subset(selected(values), values);
129 void Foam::scalarRanges::inplaceSelect
131     List<scalar>& values
132 ) const
134     inplaceSubset(selected(values), values);
138 // ************************************************************************* //