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 :
95  timeFunctionObject(name, runTime),
96  fileToUpdate_("unknown-fileToUpdate"),
97  timeVsFile_(),
98  lastIndex_(-1),
99  modified_(false)
100 {
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:28
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::functionObjects::timeActivatedFileUpdate::execute
virtual bool execute()
Definition: timeActivatedFileUpdate.C:137
Foam::functionObjects::timeFunctionObject::time_
const Time & time_
Definition: timeFunctionObject.H:61
Foam::read
bool read(const char *buf, int32_t &val)
Definition: int32.H:125
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:52
Foam::functionObjects::timeActivatedFileUpdate::read
virtual bool read(const dictionary &)
Definition: timeActivatedFileUpdate.C:101
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Definition: UPstream.H:453
Foam::isFile
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Definition: POSIX.C:792
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::dimensioned::value
const Type & value() const
Definition: dimensionedType.C:427
polyMesh.H
Foam::TimeState::deltaTValue
scalar deltaTValue() const noexcept
Definition: TimeStateI.H:36
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::TimePaths::distributed
bool distributed() const noexcept
Definition: TimePathsI.H:23
Foam::functionObjects::timeActivatedFileUpdate::write
virtual bool write()
Definition: timeActivatedFileUpdate.C:145
Foam::Info
messageStream Info
Foam::pid
pid_t pid()
Definition: POSIX.C:244
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Definition: functionObject.C:156
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:119
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Definition: POSIX.C:1202
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Definition: atmBoundaryLayer.C:26
timeActivatedFileUpdate.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::cp
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Definition: POSIX.C:975
Foam::tab
constexpr char tab
Definition: Ostream.H:423
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObject::type
virtual const word & type() const =0
Foam::functionObjects::timeActivatedFileUpdate::filesModified
virtual bool filesModified() const
Definition: timeActivatedFileUpdate.C:151
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Definition: POSIX.C:717
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52