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-2023 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 << type() << ": copying file" << nl << fileVsTime_[i].second()
66  << nl << "to:" << nl << fileToUpdate_ << nl << endl;
67 
68  fileName destFile(fileToUpdate_ + Foam::name(pid()));
69  cp(fileVsTime_[i].second(), destFile);
70  mv(destFile, fileToUpdate_);
71  lastIndex_ = i;
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
78 Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
79 (
80  const word& name,
81  const Time& runTime,
82  const dictionary& dict
83 )
84 :
85  functionObject(name, runTime),
86  fileToUpdate_(dict.lookup("fileToUpdate")),
87  fileVsTime_(),
88  lastIndex_(-1)
89 {
90  read(dict);
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
95 
97 {}
98 
99 
100 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101 
103 (
104  const dictionary& dict
105 )
106 {
107  dict.lookup("fileToUpdate") >> fileToUpdate_;
108  dict.lookupBackwardsCompatible({"fileVsTime", "timeVsFile"}) >> fileVsTime_;
109 
110  lastIndex_ = -1;
111  fileToUpdate_.expand();
112 
113  Info<< type() << ": file vs time list:" << nl;
114  forAll(fileVsTime_, i)
115  {
116  fileVsTime_[i].second() = fileVsTime_[i].second().expand();
117  if (!isFile(fileVsTime_[i].second()))
118  {
120  << "File: " << fileVsTime_[i].second() << " not found"
121  << nl << exit(FatalError);
122  }
123 
124  Info<< " " << fileVsTime_[i].first() << tab
125  << fileVsTime_[i].second() << endl;
126  }
127  Info<< endl;
128 
129  updateFile();
130 
131  return true;
132 }
133 
134 
136 {
137  updateFile();
138 
139  return true;
140 }
141 
142 
144 {
145  return true;
146 }
147 
148 
149 // ************************************************************************* //
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:434
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:830
A list of keyword definitions, which are a keyword followed by any number of values (e....
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:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
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
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
static const char tab
Definition: Ostream.H:265
messageStream Info
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
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
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict