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 | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
27 #include "Time.H"
28 #include "polyMesh.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37  defineTypeNameAndDebug(timeActivatedFileUpdate, 0);
38 
40  (
41  functionObject,
42  timeActivatedFileUpdate,
43  dictionary
44  );
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
51 void Foam::functionObjects::timeActivatedFileUpdate::updateFile()
52 {
53  label i = lastIndex_;
54  while
55  (
56  i < timeVsFile_.size()-1
57  && timeVsFile_[i+1].first() < time_.value()
58  )
59  {
60  i++;
61  }
62 
63  if (i > lastIndex_)
64  {
65  Info<< nl << type() << ": copying file" << nl << timeVsFile_[i].second()
66  << nl << "to:" << nl << fileToUpdate_ << nl << endl;
67 
68  fileName destFile(fileToUpdate_ + Foam::name(pid()));
69  cp(timeVsFile_[i].second(), destFile);
70  mv(destFile, fileToUpdate_);
71  lastIndex_ = i;
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
78 Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
79 (
80  const word& name,
81  const Time& runTime,
82  const dictionary& dict
83 )
84 :
85  functionObject(name),
86  time_(runTime),
87  fileToUpdate_(dict.lookup("fileToUpdate")),
88  timeVsFile_(),
89  lastIndex_(-1)
90 {
91  read(dict);
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
96 
98 {}
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
104 (
105  const dictionary& dict
106 )
107 {
108  dict.lookup("fileToUpdate") >> fileToUpdate_;
109  dict.lookup("timeVsFile") >> timeVsFile_;
110 
111  lastIndex_ = -1;
112  fileToUpdate_.expand();
113 
114  Info<< type() << ": time vs file list:" << nl;
115  forAll(timeVsFile_, i)
116  {
117  timeVsFile_[i].second() = timeVsFile_[i].second().expand();
118  if (!isFile(timeVsFile_[i].second()))
119  {
121  << "File: " << timeVsFile_[i].second() << " not found"
122  << nl << exit(FatalError);
123  }
124 
125  Info<< " " << timeVsFile_[i].first() << tab
126  << timeVsFile_[i].second() << endl;
127  }
128  Info<< endl;
129 
130  updateFile();
131 
132  return true;
133 }
134 
135 
137 {
138  updateFile();
139 
140  return true;
141 }
142 
143 
145 {
146  return true;
147 }
148 
149 
150 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:913
virtual bool execute()
Execute file updates.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const char tab
Definition: Ostream.H:261
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:739
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:80
bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:551
static const char nl
Definition: Ostream.H:262
virtual bool read(const dictionary &)
Read the timeActivatedFileUpdate data.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:485
messageStream Info
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
addToRunTimeSelectionTable(functionObject, add, dictionary)
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576