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"
33
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
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
79 {
80 // Slaves do not copy if running 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
94(
95 const word& name,
96 const Time& runTime,
97 const dictionary& dict
98)
99:
101 fileToUpdate_("unknown-fileToUpdate"),
102 timeVsFile_(),
103 lastIndex_(-1),
104 modified_(false)
105{
106 read(dict);
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 Info<< type() << " " << name() << " output:" << nl
126 << " time vs file list:" << endl;
127
128 forAll(timeVsFile_, i)
129 {
130 timeVsFile_[i].second().expand();
131 const fileName& srcFile = timeVsFile_[i].second();
132
133 // Report case-relative path for information
134 Info<< " " << timeVsFile_[i].first() << tab
135 << time_.relativePath(srcFile, true) << endl;
136
137 if (Pstream::master() || time_.distributed())
138 {
139 if (!Foam::isFile(srcFile))
140 {
141 // Report full path on error
143 << "File not found: " << srcFile << endl
144 << exit(FatalError);
145 }
146 }
147 }
148
149 // Copy starting files
150 updateFile();
151
152 return true;
153}
154
155
157{
158 updateFile();
159
160 return true;
161}
162
163
165{
166 return true;
167}
168
169
171{
172 return modified_;
173}
174
175
176// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
virtual bool read()
Re-read model coefficients if they have changed.
bool distributed() const noexcept
Definition: TimePathsI.H:30
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: TimePathsI.H:87
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const Type & value() const
Return const reference to value.
A class for handling file names.
Definition: fileName.H:76
Abstract base-class for Time/database function objects.
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 read(const dictionary &)
Read the timeActivatedFileUpdate data.
Virtual base class for function objects with a reference to Time.
const Time & time_
Reference to the time database.
splitCell * master() const
Definition: splitCell.H:113
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
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: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
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: MSwindows.C:666
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: MSwindows.C:810
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:947
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
constexpr char tab
The tab '\t' character(0x09)
Definition: Ostream.H:52
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333