Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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
30#include "Time.H"
31#include "polyMesh.H"
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36namespace Foam
37{
38namespace functionObjects
39{
41
43 (
47 );
48}
49}
50
51
52// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53
54void 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
100Foam::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)
113 read(dict);
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
174 updateFile();
175
176 return true;
177}
178
183}
184
185
187{
188 return modified_;
189}
190
191
192// ************************************************************************* //
#define Log
Definition PDRblock.C:28
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
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:122
scalar deltaTValue() const noexcept
Return time step value.
Definition TimeStateI.H:49
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const Type & value() const noexcept
Return const reference to value.
A class for handling file names.
Definition fileName.H:75
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
virtual const word & type() const =0
Runtime type information.
Performs a file copy/replacement once a specified time has been reached.
virtual bool filesModified() const
Did any file get changed during execution?
virtual bool execute()
Execute the function-object operations.
virtual bool write()
Write the function-object results (no-op).
virtual bool read(const dictionary &)
Read the function-object dictionary.
const Time & time_
Reference to the time database.
timeFunctionObject(const timeFunctionObject &)=delete
No copy construct.
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
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
engineTime & runTime
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
auto & name
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
messageStream Info
Information stream (stdout output on master, null elsewhere).
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
pid_t pid()
Return the PID of this process.
Definition POSIX.C:316
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:879
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition POSIX.C:1065
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition POSIX.C:1326
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
dictionary dict