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 
79  {
80  // Copy on master only for non-distributed
81  fileName tmpFile(fileToUpdate_ + Foam::name(pid()));
82  Foam::cp(srcFile, tmpFile);
83  Foam::mv(tmpFile, fileToUpdate_);
84  }
85  lastIndex_ = i;
86  modified_ = true;
87  }
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
93 Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
94 (
95  const word& name,
96  const Time& runTime,
97  const dictionary& dict
98 )
99 :
100  timeFunctionObject(name, runTime),
101  fileToUpdate_(),
102  timeVsFile_(),
103  lastIndex_(-1),
104  modified_(false)
105 {
107 }
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
113 (
114  const dictionary& dict
115 )
116 {
118 
119  dict.readEntry("fileToUpdate", fileToUpdate_);
120  dict.readEntry("timeVsFile", timeVsFile_);
121 
122  lastIndex_ = -1;
123  fileToUpdate_.expand();
124 
125  if (fileToUpdate_.empty() || timeVsFile_.empty())
126  {
128  << "Bad entries for fileToUpdate and/or timeVsFile" << endl
129  << exit(FatalIOError);
130  }
131 
132  Info<< type() << " " << name() << " output:" << nl
133  << " time vs file list:" << nl;
134 
135  for (auto& tuple : timeVsFile_)
136  {
137  fileName& srcFile = tuple.second();
138  srcFile.expand();
139 
140  // Report case-relative path for information
141  Info<< " " << tuple.first() << tab
142  << time_.relativePath(srcFile, true) << nl;
143 
144  if (Pstream::master() || time_.distributed())
145  {
146  if (!Foam::isFile(srcFile))
147  {
148  // Report full path on error
150  << "File not found: " << srcFile << endl
151  << exit(FatalIOError);
152  }
153  }
154  }
155 
156  // Copy starting files
157  updateFile();
158 
159  return true;
160 }
161 
162 
164 {
165  updateFile();
166 
167  return true;
168 }
169 
172 {
173  return true;
174 }
175 
176 
178 {
179  return modified_;
180 }
181 
182 
183 // ************************************************************************* //
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:1277
A class for handling file names.
Definition: fileName.H:71
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:36
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:120
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: POSIX.C:1016
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:80
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:48
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:752
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
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:267
bool distributed() const noexcept
True if case running with parallel distributed directories (ie. not NFS mounted)
Definition: TimePathsI.H:23
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:830
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:607
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static bool master(const label communicator=worldComm)
Am I the master rank.
Definition: UPstream.H:672
#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.