timeActivatedFileUpdate.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-2026 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 
27 #include "Time.H"
28 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
38 
40  (
44  );
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
51 void Foam::functionObjects::timeActivatedFileUpdate::updateFile()
52 {
53  label i = lastIndex_;
54  while
55  (
56  i < fileVsTime_.size()-1
57  && fileVsTime_[i+1].first() <= time_.userTimeValue()
58  )
59  {
60  i++;
61  }
62 
63  if (i > lastIndex_)
64  {
65  Info<< nl << indent
66  << type() << ": copying file" << nl << fileVsTime_[i].second()
67  << nl << "to:" << nl << fileToUpdate_ << endl;
68 
69  fileName destFile(fileToUpdate_ + Foam::name(pid()));
70  cp(fileVsTime_[i].second(), destFile);
71  mv(destFile, fileToUpdate_);
72  lastIndex_ = i;
73  }
74 }
75 
76 
77 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
78 
79 Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
80 (
81  const word& name,
82  const Time& runTime,
83  const dictionary& dict
84 )
85 :
86  functionObject(name, runTime),
87  fileToUpdate_(dict.lookup("fileToUpdate")),
88  fileVsTime_(),
89  lastIndex_(-1)
90 {
91  read(dict);
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
96 
98 {}
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
104 (
105  const dictionary& dict
106 )
107 {
108  dict.lookup("fileToUpdate") >> fileToUpdate_;
109  dict.lookupBackwardsCompatible({"fileVsTime", "timeVsFile"}) >> fileVsTime_;
110 
111  lastIndex_ = -1;
112  fileToUpdate_.expand();
113 
114  Info<< indent << "file vs time list:" << nl;
115  Info<< incrIndent;
116  forAll(fileVsTime_, i)
117  {
118  fileVsTime_[i].second() = fileVsTime_[i].second().expand();
119  if (!isFile(fileVsTime_[i].second()))
120  {
122  << "File: " << fileVsTime_[i].second() << " not found"
123  << nl << exit(FatalError);
124  }
125 
126  Info<< indent << fileVsTime_[i].first() << tab
127  << fileVsTime_[i].second() << endl;
128  }
129  Info<< decrIndent;
130 
131  updateFile();
132 
133  return true;
134 }
135 
136 
138 {
139  updateFile();
140 
141  return true;
142 }
143 
144 
146 {
147  return true;
148 }
149 
150 
151 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Macros for easy insertion into run-time selection tables.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
scalar userTimeValue() const
Return current user time value.
Definition: Time.C:838
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base-class for Time/database functionObjects.
virtual const word & type() const =0
Runtime type information.
const Time & time_
Reference to time.
Performs a file copy/replacement once a specified time has been reached.
virtual bool read(const dictionary &)
Read the timeActivatedFileUpdate data.
A class for handling words, derived from string.
Definition: word.H:63
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
addToRunTimeSelectionTable(functionObject, fvModel, dictionary)
const unitSet & lookup(const word &unitName)
Lookup and return the named unit from the table.
Definition: units.C:346
Namespace for OpenFOAM.
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
Definition: POSIX.C:555
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:272
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:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
static const char tab
Definition: Ostream.H:296
messageStream Info
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:265
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:753
error FatalError
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:948
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
static const char nl
Definition: Ostream.H:297
dictionary dict