Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2018 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "OFile.h"
23 : #include "Exception.h"
24 : #include "core/Action.h"
25 : #include "core/PlumedMain.h"
26 : #include "core/Value.h"
27 : #include "Communicator.h"
28 : #include "Tools.h"
29 : #include <cstdarg>
30 : #include <cstring>
31 :
32 : #include <iostream>
33 : #include <string>
34 : #include <cstdlib>
35 : #include <cerrno>
36 :
37 : #ifdef __PLUMED_HAS_ZLIB
38 : #include <zlib.h>
39 : #endif
40 :
41 : namespace PLMD {
42 :
43 841852 : size_t OFile::llwrite(const char*ptr,size_t s) {
44 : size_t r;
45 841852 : if(linked) return linked->llwrite(ptr,s);
46 841847 : if(! (comm && comm->Get_rank()>0)) {
47 661229 : if(!fp) plumed_merror("writing on uninitilized File");
48 661229 : if(gzfp) {
49 : #ifdef __PLUMED_HAS_ZLIB
50 1254 : r=gzwrite(gzFile(gzfp),ptr,s);
51 : #else
52 : plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked");
53 : #endif
54 : } else {
55 659975 : r=fwrite(ptr,1,s,fp);
56 : }
57 : }
58 : // This barrier is apparently useless since it comes
59 : // just before a Bcast.
60 : //
61 : // Anyway, it looks like it is solving an issue that appeared on
62 : // TRAVIS (at least on my laptop) so I add it here.
63 : // GB
64 841847 : if(comm) comm->Barrier();
65 :
66 :
67 841847 : if(comm) comm->Bcast(r,0);
68 841847 : return r;
69 : }
70 :
71 2307 : OFile::OFile():
72 : linked(NULL),
73 : fieldChanged(false),
74 : backstring("bck"),
75 : enforceRestart_(false),
76 2307 : enforceBackup_(false)
77 : {
78 2307 : fmtField();
79 2307 : buflen=1;
80 2307 : actual_buffer_length=0;
81 2307 : buffer=new char[buflen];
82 : // these are set to zero to avoid valgrind errors
83 2307 : for(unsigned i=0; i<buflen; ++i) buffer[i]=0;
84 2307 : buffer_string=new char [1000];
85 : // these are set to zero to avoid valgrind errors
86 2307 : for(unsigned i=0; i<1000; ++i) buffer_string[i]=0;
87 2307 : }
88 :
89 5776 : OFile::~OFile() {
90 2307 : delete [] buffer_string;
91 2307 : delete [] buffer;
92 3469 : }
93 :
94 1 : OFile& OFile::link(OFile&l) {
95 1 : fp=NULL;
96 1 : gzfp=NULL;
97 1 : linked=&l;
98 1 : return *this;
99 : }
100 :
101 1159 : OFile& OFile::setLinePrefix(const std::string&l) {
102 1159 : linePrefix=l;
103 1159 : return *this;
104 : }
105 :
106 4764167 : int OFile::printf(const char*fmt,...) {
107 : va_list arg;
108 4764167 : va_start(arg, fmt);
109 4764167 : int r=std::vsnprintf(&buffer[actual_buffer_length],buflen-actual_buffer_length,fmt,arg);
110 4764167 : va_end(arg);
111 4764167 : if(r>=buflen-actual_buffer_length) {
112 3668 : int newlen=buflen;
113 3668 : while(newlen<=r+actual_buffer_length) newlen*=2;
114 3668 : char* newbuf=new char [newlen];
115 3668 : memmove(newbuf,buffer,buflen);
116 3668 : for(int k=buflen; k<newlen; k++) newbuf[k]=0;
117 3668 : delete [] buffer;
118 3668 : buffer=newbuf;
119 3668 : buflen=newlen;
120 : va_list arg;
121 3668 : va_start(arg, fmt);
122 3668 : r=std::vsnprintf(&buffer[actual_buffer_length],buflen-actual_buffer_length,fmt,arg);
123 3668 : va_end(arg);
124 : }
125 4764167 : plumed_massert(r>-1 && r<buflen-actual_buffer_length,"error using fmt string " + std::string(fmt));
126 :
127 : // Line is buffered until newline, then written with a PLUMED: prefix
128 4764167 : char*p1=buffer;
129 : char*p2;
130 : // newline is only searched in the just added portion:
131 4764167 : char*psearch=p1+actual_buffer_length;
132 4764167 : actual_buffer_length+=r;
133 10289356 : while((p2=strchr(psearch,'\n'))) {
134 761022 : if(linePrefix.length()>0) llwrite(linePrefix.c_str(),linePrefix.length());
135 761022 : llwrite(p1,p2-p1+1);
136 761022 : actual_buffer_length-=(p2-p1)+1;
137 761022 : p1=p2+1;
138 761022 : psearch=p1;
139 : };
140 4764167 : if(buffer!=p1) memmove(buffer,p1,actual_buffer_length);
141 4764167 : return r;
142 : }
143 :
144 1290 : OFile& OFile::addConstantField(const std::string&name) {
145 1290 : Field f;
146 1290 : f.name=name;
147 1290 : const_fields.push_back(f);
148 1290 : return *this;
149 : }
150 :
151 :
152 90 : OFile& OFile::clearFields() {
153 90 : fields.clear();
154 90 : const_fields.clear();
155 90 : previous_fields.clear();
156 90 : return *this;
157 : }
158 :
159 3233711 : OFile& OFile::fmtField(const std::string&fmt) {
160 3233711 : this->fieldFmt=fmt;
161 3233711 : return *this;
162 : }
163 :
164 2307 : OFile& OFile::fmtField() {
165 2307 : this->fieldFmt="%23.16lg";
166 2307 : return *this;
167 : }
168 :
169 3252071 : OFile& OFile::printField(const std::string&name,double v) {
170 3252071 : sprintf(buffer_string,fieldFmt.c_str(),v);
171 3252071 : printField(name,buffer_string);
172 3252071 : return *this;
173 : }
174 :
175 607500 : OFile& OFile::printField(const std::string&name,int v) {
176 607500 : sprintf(buffer_string," %d",v);
177 607500 : printField(name,buffer_string);
178 607500 : return *this;
179 : }
180 :
181 4046413 : OFile& OFile::printField(const std::string&name,const std::string & v) {
182 : unsigned i;
183 4046413 : for(i=0; i<const_fields.size(); i++) if(const_fields[i].name==name) break;
184 4046413 : if(i>=const_fields.size()) {
185 3796739 : Field field;
186 3796739 : field.name=name;
187 3796739 : field.value=v;
188 3796739 : fields.push_back(field);
189 : } else {
190 249674 : if(const_fields[i].value!=v) fieldChanged=true;
191 249674 : const_fields[i].value=v;
192 : }
193 4046413 : return *this;
194 : }
195 :
196 2666 : OFile& OFile::setupPrintValue( Value *val ) {
197 2666 : if( val->isPeriodic() ) {
198 100 : addConstantField("min_" + val->getName() );
199 100 : addConstantField("max_" + val->getName() );
200 : }
201 2666 : return *this;
202 : }
203 :
204 85623 : OFile& OFile::printField( Value* val, const double& v ) {
205 85623 : printField( val->getName(), v );
206 85623 : if( val->isPeriodic() ) {
207 9060 : std::string min, max; val->getDomain( min, max );
208 4530 : printField( "min_" + val->getName(), min );
209 9060 : printField("max_" + val->getName(), max );
210 : }
211 85623 : return *this;
212 : }
213 :
214 601313 : OFile& OFile::printField() {
215 601313 : bool reprint=false;
216 601313 : if(fieldChanged || fields.size()!=previous_fields.size()) {
217 754 : reprint=true;
218 4389319 : } else for(unsigned i=0; i<fields.size(); i++) {
219 7577524 : if( previous_fields[i].name!=fields[i].name ||
220 3788760 : (fields[i].constant && fields[i].value!=previous_fields[i].value) ) {
221 2 : reprint=true;
222 2 : break;
223 : }
224 : }
225 601313 : if(reprint) {
226 756 : printf("#! FIELDS");
227 756 : for(unsigned i=0; i<fields.size(); i++) printf(" %s",fields[i].name.c_str());
228 756 : printf("\n");
229 2038 : for(unsigned i=0; i<const_fields.size(); i++) {
230 1282 : printf("#! SET %s %s",const_fields[i].name.c_str(),const_fields[i].value.c_str());
231 1282 : printf("\n");
232 : }
233 : }
234 601313 : for(unsigned i=0; i<fields.size(); i++) printf("%s",fields[i].value.c_str());
235 601313 : printf("\n");
236 601313 : previous_fields=fields;
237 601313 : fields.clear();
238 601313 : fieldChanged=false;
239 601313 : return *this;
240 : }
241 :
242 49 : void OFile::setBackupString( const std::string& str ) {
243 49 : backstring=str;
244 49 : }
245 :
246 4 : void OFile::backupAllFiles( const std::string& str ) {
247 8 : if(str=="/dev/null") return;
248 4 : plumed_assert( backstring!="bck" && !checkRestart());
249 4 : size_t found=str.find_last_of("/\\");
250 4 : std::string filename = appendSuffix(str,getSuffix());
251 8 : std::string directory=filename.substr(0,found+1);
252 8 : std::string file=filename.substr(found+1);
253 4 : if( FileExist(filename) ) backupFile("bck", filename);
254 4 : for(int i=0;; i++) {
255 4 : std::string num; Tools::convert(i,num);
256 4 : std::string filestr = directory + backstring + "." + num + "." + file;
257 4 : if( !FileExist(filestr) ) break;
258 0 : backupFile( "bck", filestr);
259 4 : }
260 : }
261 :
262 683 : void OFile::backupFile( const std::string& bstring, const std::string& fname ) {
263 1366 : if(fname=="/dev/null") return;
264 673 : int maxbackup=100;
265 673 : if(std::getenv("PLUMED_MAXBACKUP")) Tools::convert(std::getenv("PLUMED_MAXBACKUP"),maxbackup);
266 673 : if(maxbackup>0 && (!comm || comm->Get_rank()==0)) {
267 554 : FILE* ff=std::fopen(const_cast<char*>(fname.c_str()),"r");
268 554 : if(ff) {
269 33 : std::fclose(ff);
270 33 : std::string backup;
271 33 : size_t found=fname.find_last_of("/\\");
272 66 : std::string directory=fname.substr(0,found+1);
273 66 : std::string file=fname.substr(found+1);
274 33 : for(int i=0;; i++) {
275 33 : std::string num;
276 33 : Tools::convert(i,num);
277 33 : if(i>maxbackup) plumed_merror("cannot backup file "+file+" maximum number of backup is "+num+"\n");
278 33 : backup=directory+bstring +"."+num+"."+file;
279 33 : FILE* fff=std::fopen(backup.c_str(),"r");
280 33 : if(!fff) break;
281 0 : else std::fclose(fff);
282 0 : }
283 33 : int check=rename(fname.c_str(),backup.c_str());
284 66 : plumed_massert(check==0,"renaming "+fname+" into "+backup+" failed for reason: "+strerror(errno));
285 : }
286 : }
287 : }
288 :
289 779 : OFile& OFile::open(const std::string&path) {
290 779 : plumed_assert(!cloned);
291 779 : eof=false;
292 779 : err=false;
293 779 : fp=NULL;
294 779 : gzfp=NULL;
295 779 : this->path=path;
296 779 : this->path=appendSuffix(path,getSuffix());
297 779 : if(checkRestart()) {
298 96 : fp=std::fopen(const_cast<char*>(this->path.c_str()),"a");
299 96 : mode="a";
300 96 : if(Tools::extension(this->path)=="gz") {
301 : #ifdef __PLUMED_HAS_ZLIB
302 24 : gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"a9");
303 : #else
304 : plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked");
305 : #endif
306 : }
307 : } else {
308 683 : backupFile( backstring, this->path );
309 683 : if(comm)comm->Barrier();
310 683 : fp=std::fopen(const_cast<char*>(this->path.c_str()),"w");
311 683 : mode="w";
312 683 : if(Tools::extension(this->path)=="gz") {
313 : #ifdef __PLUMED_HAS_ZLIB
314 1 : gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"w9");
315 : #else
316 : plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked");
317 : #endif
318 : }
319 : }
320 779 : if(plumed) plumed->insertFile(*this);
321 779 : return *this;
322 : }
323 :
324 50 : OFile& OFile::rewind() {
325 : // we use here "hard" rewind, which means close/reopen
326 : // the reason is that normal rewind does not work when in append mode
327 : // moreover, we can take a backup of the file
328 50 : plumed_assert(fp);
329 50 : clearFields();
330 50 : if(gzfp) {
331 : #ifdef __PLUMED_HAS_ZLIB
332 15 : gzclose((gzFile)gzfp);
333 : #endif
334 35 : } else fclose(fp);
335 50 : if(!comm || comm->Get_rank()==0) {
336 50 : std::string fname=this->path;
337 50 : size_t found=fname.find_last_of("/\\");
338 100 : std::string directory=fname.substr(0,found+1);
339 100 : std::string file=fname.substr(found+1);
340 100 : std::string backup=directory+backstring +".last."+file;
341 50 : int check=rename(fname.c_str(),backup.c_str());
342 100 : plumed_massert(check==0,"renaming "+fname+" into "+backup+" failed for reason: "+strerror(errno));
343 : }
344 50 : if(gzfp) {
345 : #ifdef __PLUMED_HAS_ZLIB
346 15 : gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"w9");
347 : #endif
348 35 : } else fp=std::fopen(const_cast<char*>(path.c_str()),"w");
349 50 : return *this;
350 : }
351 :
352 5387 : FileBase& OFile::flush() {
353 5387 : if(heavyFlush) {
354 3147 : if(gzfp) {
355 : #ifdef __PLUMED_HAS_ZLIB
356 9 : gzclose(gzFile(gzfp));
357 9 : gzfp=(void*)gzopen(const_cast<char*>(path.c_str()),"a");
358 : #endif
359 : } else {
360 3138 : fclose(fp);
361 3138 : fp=std::fopen(const_cast<char*>(path.c_str()),"a");
362 : }
363 : } else {
364 2240 : FileBase::flush();
365 : // if(gzfp) gzflush(gzFile(gzfp),Z_FINISH);
366 : // for some reason flushing with Z_FINISH has problems on linux
367 : // I thus use this (incomplete) flush
368 : #ifdef __PLUMED_HAS_ZLIB
369 2240 : if(gzfp) gzflush(gzFile(gzfp),Z_FULL_FLUSH);
370 : #endif
371 : }
372 5387 : return *this;
373 : }
374 :
375 783 : bool OFile::checkRestart()const {
376 783 : if(enforceRestart_) return true;
377 782 : else if(enforceBackup_) return false;
378 782 : else if(action) return action->getRestart();
379 10 : else if(plumed) return plumed->getRestart();
380 10 : else return false;
381 : }
382 :
383 1 : OFile& OFile::enforceRestart() {
384 1 : enforceRestart_=true;
385 1 : enforceBackup_=false;
386 1 : return *this;
387 : }
388 :
389 0 : OFile& OFile::enforceBackup() {
390 0 : enforceBackup_=true;
391 0 : enforceRestart_=false;
392 0 : return *this;
393 : }
394 :
395 :
396 2523 : }
|