instant.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) 2011 OpenFOAM Foundation
9  Copyright (C) 2018-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "instant.H"
30 #include "Time.H"
31 #include "Pair.H"
32 #include "UList.H"
33 #include <cstdlib> // std::atof
34 #include <utility> // std::move
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 const char* const Foam::instant::typeName = "instant";
39 
40 
41 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
42 
43 Foam::label Foam::instant::findStart
44 (
45  const UList<instant>& times,
46  const scalar timeVal
47 )
48 {
49  for (label i = 0; i < times.size(); ++i)
50  {
51  if (timeVal <= times[i].value())
52  {
53  return i;
54  }
55  }
56  return 0;
57 }
58 
59 
61 (
62  const UList<instant>& times,
63  const scalar timeVal,
64  const label start
65 )
66 {
67  Pair<label> range(start, -1); // lower/upper
68 
69  for (label i = start+1; i < times.size(); ++i)
70  {
71  if (timeVal < times[i].value())
72  {
73  break;
74  }
75  else
76  {
77  range.first() = i;
78  }
79  }
80 
81  if (range.first() < 0 || range.first() >= times.size())
82  {
83  // Invalid
84  return Pair<label>(-1, -1);
85  }
86 
87  if (range.first() < times.size()-1)
88  {
89  // Upper part of range within bounds
90  range.second() = range.first()+1;
91  }
92 
93  return range;
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 Foam::instant::instant(scalar timeValue)
100 :
101  Instant<word>(timeValue, Time::timeName(timeValue))
102 {}
103 
104 
106 :
107  Instant<word>(0, timeName)
108 {
109  value() = std::atof(name().c_str());
110 }
111 
112 
114 :
115  Instant<word>(0, std::move(timeName))
116 {
117  value() = std::atof(name().c_str());
118 }
119 
120 
121 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
static Pair< label > findRange(const UList< instant > &times, const scalar timeVal, const label start=-1)
Find lower/upper indices for given time value in list of instances (linear search) continuing after t...
Definition: instant.C:54
static label findStart(const UList< instant > &times, const scalar timeVal)
Find and return index of given start time (linear search)
Definition: instant.C:37
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
scalar range
scalar value() const noexcept
The value (const access)
Definition: Instant.H:134
word timeName
Definition: getTimeIndex.H:3
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
const word & name() const noexcept
The name/key (const access)
Definition: Instant.H:144
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
instant()=default
Default construct, with value = 0 and empty name.
static const char *const typeName
The type name (eg, for pTraits)
Definition: instant.H:64
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
A tuple of scalar value and key. The value often corresponds to a time value, thus the naming of the ...
Definition: Instant.H:45