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-2019 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  Log << nl << type() << ": copying file" << nl << timeVsFile_[i].second()
71  << nl << "to:" << nl << fileToUpdate_ << nl << endl;
72 
74  {
75  // Slaves do not copy if running non-distributed
76  fileName destFile(fileToUpdate_ + Foam::name(pid()));
77  cp(timeVsFile_[i].second(), destFile);
78  mv(destFile, fileToUpdate_);
79  }
80  lastIndex_ = i;
81  modified_ = true;
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
87 
88 Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
89 (
90  const word& name,
91  const Time& runTime,
92  const dictionary& dict
93 )
94 :
96  fileToUpdate_("unknown-fileToUpdate"),
97  timeVsFile_(),
98  lastIndex_(-1),
99  modified_(false)
100 {
101  read(dict);
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
108 (
109  const dictionary& dict
110 )
111 {
113 
114  dict.readEntry("fileToUpdate", fileToUpdate_);
115  dict.readEntry("timeVsFile", timeVsFile_);
116 
117  lastIndex_ = -1;
118  fileToUpdate_.expand();
119 
120  Info<< type() << " " << name() << " output:" << nl
121  << " time vs file list:" << endl;
122 
123  forAll(timeVsFile_, i)
124  {
125  timeVsFile_[i].second() = timeVsFile_[i].second().expand();
126  if (!isFile(timeVsFile_[i].second()))
127  {
129  << "File: " << timeVsFile_[i].second() << " not found"
130  << nl << exit(FatalError);
131  }
132 
133  Info<< " " << timeVsFile_[i].first() << tab
134  << timeVsFile_[i].second() << endl;
135  }
136 
137  // Copy starting files
138  updateFile();
139 
140  return true;
141 }
142 
143 
145 {
146  updateFile();
147 
148  return true;
149 }
150 
151 
153 {
154  return true;
155 }
156 
157 
159 {
160  return modified_;
161 }
162 
163 
164 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::timeActivatedFileUpdate::execute
virtual bool execute()
Execute file updates.
Definition: timeActivatedFileUpdate.C:144
Foam::functionObjects::timeFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: timeFunctionObject.H:65
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:56
Foam::functionObjects::timeActivatedFileUpdate::read
virtual bool read(const dictionary &)
Read the timeActivatedFileUpdate data.
Definition: timeActivatedFileUpdate.C:108
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::isFile
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:658
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:434
polyMesh.H
Foam::TimeState::deltaTValue
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::TimePaths::distributed
bool distributed() const noexcept
Definition: TimePathsI.H:30
Foam::functionObjects::timeActivatedFileUpdate::write
virtual bool write()
Do nothing.
Definition: timeActivatedFileUpdate.C:152
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::pid
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:939
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
timeActivatedFileUpdate.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::cp
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: MSwindows.C:802
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::functionObjects::timeActivatedFileUpdate::filesModified
virtual bool filesModified() const
Did any file get changed during execution?
Definition: timeActivatedFileUpdate.C:158
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59