timeActivatedFileUpdate.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-2016 OpenFOAM Foundation
9  Copyright (C) 2015-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 
30 #include "Time.H"
31 #include "polyMesh.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(timeActivatedFileUpdate, 0);
41 
43  (
44  functionObject,
45  timeActivatedFileUpdate,
46  dictionary
47  );
48 }
49 }
50 
51 
52 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53 
54 void Foam::functionObjects::timeActivatedFileUpdate::updateFile()
55 {
56  modified_ = false;
57 
58  label i = lastIndex_;
59  while
60  (
61  i < timeVsFile_.size()-1
62  && timeVsFile_[i+1].first() < time_.value()+0.5*time_.deltaTValue()
63  )
64  {
65  i++;
66  }
67 
68  if (i > lastIndex_)
69  {
70  const fileName& srcFile = timeVsFile_[i].second();
71 
72  // Report case-relative path for information
73  Log << nl << type() << ": copying file" << nl
74  << "from: " << time_.relativePath(srcFile, true) << nl
75  << "to : " << time_.relativePath(fileToUpdate_, true) << nl
76  << endl;
77 
78  if
79  (
81  || (
82  fileHandler().distributed()
83  && UPstream::master(fileHandler().comm())
84  )
85  )
86  {
87  // Copy on master only for non-distributed
88  fileName tmpFile(fileToUpdate_ + Foam::name(pid()));
89  Foam::cp(srcFile, tmpFile);
90  Foam::mv(tmpFile, fileToUpdate_);
91  }
92  lastIndex_ = i;
93  modified_ = true;
94  }
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
100 Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
101 (
102  const word& name,
103  const Time& runTime,
104  const dictionary& dict
105 )
106 :
107  timeFunctionObject(name, runTime),
108  fileToUpdate_(),
109  timeVsFile_(),
110  lastIndex_(-1),
111  modified_(false)
112 {
114 }
115 
116 
117 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
118 
120 (
121  const dictionary& dict
122 )
123 {
125 
126  dict.readEntry("fileToUpdate", fileToUpdate_);
127  dict.readEntry("timeVsFile", timeVsFile_);
128 
129  lastIndex_ = -1;
130  fileToUpdate_.expand();
131 
132  if (fileToUpdate_.empty() || timeVsFile_.empty())
133  {
135  << "Bad entries for fileToUpdate and/or timeVsFile" << endl
136  << exit(FatalIOError);
137  }
138 
139  Info<< type() << " " << name() << " output:" << nl
140  << " time vs file list:" << nl;
141 
142  for (auto& tuple : timeVsFile_)
143  {
144  fileName& srcFile = tuple.second();
145  srcFile.expand();
146 
147  // Report case-relative path for information
148  Info<< " " << tuple.first() << tab
149  << time_.relativePath(srcFile, true) << nl;
150 
151  // No need for distributed test since dictionaries are read on the
152  // master only or otherwise they need to be copied everywhere
153  if (UPstream::master()) // || time_.distributed())
154  {
155  if (!Foam::isFile(srcFile))
156  {
157  // Report full path on error
159  << "File not found: " << srcFile << endl
160  << exit(FatalIOError);
161  }
162  }
163  }
164 
165  // Copy starting files
166  updateFile();
167 
168  return true;
169 }
170 
171 
173 {
174  updateFile();
175 
176  return true;
177 }
178 
181 {
182  return true;
183 }
184 
185 
187 {
188  return modified_;
189 }
190 
191 
192 // ************************************************************************* //
const Type & value() const noexcept
Return const reference to value.
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:1324
A class for handling file names.
Definition: fileName.H:72
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:49
virtual bool execute()
Execute file updates.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual bool filesModified() const
Did any file get changed during execution?
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: POSIX.C:1063
fileName relativePath(const fileName &input, const bool caseTag=false) const
Return the input relative to the globalPath by stripping off a leading value of the globalPath...
Definition: TimePathsI.H:103
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
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
virtual const word & type() const =0
Runtime type information.
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:314
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool read(const dictionary &)
Read the timeActivatedFileUpdate data.
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:877
string & expand(const bool allowEmpty=false)
Inplace expand initial tags, tildes, and all occurrences of environment variables as per stringOps::e...
Definition: string.C:166
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
#define Log
Definition: PDRblock.C:28
messageStream Info
Information stream (stdout output on master, null elsewhere)
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
const Time & time_
Reference to the time database.