Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
38const char* const Foam::instant::typeName = "instant";
39
40
41// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
42
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
60Foam::Pair<Foam::label> Foam::instant::findRange
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 }
93 return range;
94}
95
96
97// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99Foam::instant::instant(scalar timeValue)
100:
101 Instant<word>(timeValue, Time::timeName(timeValue))
102{}
103
104
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// ************************************************************************* //
scalar range
A tuple of scalar value and key. The value often corresponds to a time value, thus the naming of the ...
Definition Instant.H:46
Instant(const Instant &)=default
scalar value() const noexcept
Definition Instant.H:139
const word & name() const noexcept
Definition Instant.H:149
An ordered pair of two objects of type <T> with first() and second() elements.
Definition Pair.H:66
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
instant()=default
Default construct, with value = 0 and empty name.
static label findStart(const UList< instant > &times, const scalar timeVal)
Find and return index of given start time (linear search).
Definition instant.C:37
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 const char *const typeName
The type name (eg, for pTraits).
Definition instant.H:64
A class for handling words, derived from Foam::string.
Definition word.H:66
auto & name
word timeName
Definition getTimeIndex.H:3