dune-fem  2.4.1-rc
femeoc.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_FEMEOC_HH
2 #define DUNE_FEM_FEMEOC_HH
3 
4 #include <cassert>
5 #include <iostream>
6 #include <sstream>
7 #include <fstream>
8 #include <vector>
9 #include <tuple>
10 
11 #include <dune/common/fvector.hh>
12 #include <dune/fem/io/io.hh>
14 #include <dune/fem/io/parameter.hh>
15 
16 namespace Dune
17 {
18 
19  namespace Fem
20  {
21 
48  class FemEoc
49  {
50  typedef std::pair< std::string, double > DoublePairType;
51  typedef std::pair< std::string, int > IntPairType;
52 
53  // level, h, size, time, counter, errors,
54  // [avgTimeStep, minTimeStep, maxTimeStep],
55  // [newton_iterations, ils_iterations, max_newton_iterations, max_ils_iterations]
56  typedef std::tuple< int, double, double, double, int, std::vector< double >,
57  std::vector< double >, std::vector< int > >
58  DataTuple;
59 
61 
62  class ErrorColumnWriter;
63  class EOCColumnWriter;
64 
65  TableWriter *tableWriter_;
66  std::string filename_;
67  int level_;
68  std::vector< double > error_;
69  std::vector< const EOCColumnWriter * > eocColumns_;
70  std::vector< std::string > description_;
71  std::vector< int > pos_;
72 
73  FemEoc ()
74  : tableWriter_( 0 ),
75  level_( 0 )
76  {}
77 
78  ~FemEoc()
79  {
80  clearFile();
81  }
82 
83  void clearFile()
84  {
85  if( tableWriter_ )
86  delete tableWriter_;
87  tableWriter_ = 0;
88  }
89 
90  void init ( const std::string& path, const std::string &name, const std::string &descript )
91  {
92  if( MPIManager::rank() == 0 )
93  {
94  if( createDirectory( path ) )
95  init( path + "/" + name, descript );
96  else
97  std::cerr << "Error: Unable to create path '" << path << "'" << std::endl;
98  }
99  }
100 
101  void init ( const std::string &filename, const std::string &descript )
102  {
103  if( MPIManager::rank() != 0 )
104  return;
105 
106  std::string name = filename;
107 
108  // add time stamp to file name, if requested (prevents results from being overwritten)
109  if( Parameter::getValue< bool >( "fem.io.eocFileTimeStamp", false ) )
110  {
111  time_t seconds = time(0);
112  struct tm *ptm = localtime( &seconds );
113  char timeString[20];
114  strftime( timeString, 20, "_%d%m%Y_%H%M%S", ptm );
115  name += std::string( timeString );
116  }
117 
118  if( !tableWriter_ )
119  {
120  filename_ = name + "_body.tex";
121  std::ofstream main( (name + "_main.tex").c_str() );
122 
123  if (!main) {
124  std::cerr << "Could not open file : "
125  << (name+"_main.tex").c_str()
126  << " ... ABORTING" << std::endl;
127  abort();
128  }
129 
130  main << "\\documentclass[12pt,english]{article}\n"
131  << "\\usepackage[T1]{fontenc}\n"
132  << "\\usepackage[latin1]{inputenc}\n"
133  << "\\usepackage{setspace}\n"
134  << "\\onehalfspacing\n"
135  << "\\makeatletter\n"
136  << "\\providecommand{\\boldsymbol}[1]{\\mbox{\\boldmath $#1$}}\n"
137  << "\\providecommand{\\tabularnewline}{\\\\}\n"
138  << "\\usepackage{babel}\n"
139  << "\\makeatother\n"
140  << "\\begin{document}\n"
141  << "\\begin{center}\\large\n"
142  << "\n\\end{center}\n\n"
143  << descript
144  << "\\input{"
145  << filename_
146  << "}\n\n"
147  << "\\end{document}\n" << std::endl;
148  main.close();
149  }
150  else
151  {
152  std::cerr << "Could not open file : "
153  << " already opened!"
154  << " ... ABORTING" << std::endl;
155  abort();
156  }
157  }
158 
159  template <class StrVectorType>
160  size_t addentry(const StrVectorType& descript,size_t size)
161  {
162  if( tableWriter_ )
163  {
164  std::cerr << "Trying to add a new entry to FemEoc although "
165  << "entries have already been writen to disk!"
166  << " ... ABORTING" << std::endl;
167  abort();
168  }
169  pos_.push_back(error_.size());
170  for (size_t i=0;i<size;++i) {
171  error_.push_back(0);
172  description_.push_back(descript[i]);
173  }
174  return pos_.size()-1;
175  }
176 
177  size_t addentry ( const std::string &descript )
178  {
179  if( tableWriter_ )
180  {
181  std::cerr << "Trying to add a new entry to FemEoc although "
182  << "entries have already been writen to disk!"
183  << " ... ABORTING" << std::endl;
184  abort();
185  }
186  pos_.push_back(error_.size());
187  error_.push_back(0);
188  description_.push_back(descript);
189  return pos_.size()-1;
190  }
191 
192  template <class VectorType>
193  void seterrors(size_t id,const VectorType& err,size_t size)
194  {
195  assert(id<pos_.size());
196  int pos = pos_[ id ];
197  assert(pos+size <= error_.size());
198 
199  for (size_t i=0; i<size; ++i)
200  error_[pos+i] = err[i];
201  }
202 
203  template <int SIZE>
204  void seterrors(size_t id,const FieldVector<double,SIZE>& err)
205  {
206  seterrors(id,err,SIZE);
207  }
208 
209  void seterrors(size_t id,const double& err) {
210  int pos = pos_[id];
211  error_[pos] = err;
212  }
213 
214  void writeerr ( double h, double size, double time, int counter );
215  void writeerr(double h,double size,double time,int counter,
216  const std::vector< DoublePairType>& doubleValues,
217  const std::vector< IntPairType>& intValues);
218 
219  void writeerr(double h,double size,double time,int counter,
220  double avgTimeStep,double minTimeStep,double maxTimeStep ,
221  const int newton_iterations, const int ils_iterations,
222  const int max_newton_iterations, const int max_ils_iterations);
223 
224  // do the same calculations as in write, but don't overwrite status
225  void printerr(const double h,
226  const double size,
227  const double time,
228  const int counter,
229  std::ostream& out);
230  void printerr(const double h,
231  const double size,
232  const double time,
233  const int counter,
234  const std::vector< DoublePairType>& doubleValues,
235  const std::vector< IntPairType>& intValues,
236  std::ostream& out);
237 
238  public:
239  static FemEoc& instance() {
240  static FemEoc instance_;
241  return instance_;
242  }
244  static void clear() {
245  instance().clearFile();
246  }
248  static void initialize(const std::string& path, const std::string& name, const std::string& descript) {
249  instance().init(path,name,descript);
250  }
252  static void initialize(const std::string& name, const std::string& descript) {
253  instance().init(name,descript);
254  }
262  template <class StrVectorType>
263  static size_t addEntry(const StrVectorType& descript,size_t size) {
264  return instance().addentry(descript,size);
265  }
272  template <class StrVectorType>
273  static size_t addEntry(const StrVectorType& descript) {
274  return instance().addentry(descript,descript.size());
275  }
280  static size_t addEntry(const std::string& descript) {
281  return instance().addentry(descript);
282  }
287  static size_t addEntry(const char* descript) {
288  return addEntry(std::string(descript));
289  }
295  template <class VectorType>
296  static void setErrors(size_t id,const VectorType& err,int size)
297  {
298  instance().seterrors(id,err,size);
299  }
305  template <class VectorType>
306  static void setErrors(size_t id,const VectorType& err) {
307  instance().seterrors(id,err,err.size());
308  }
312  template <int SIZE>
313  static void setErrors(size_t id,const FieldVector<double,SIZE>& err) {
314  instance().seterrors(id,err);
315  }
319  static void setErrors(size_t id,const double& err) {
320  instance().seterrors(id,err);
321  }
329  static void write(double h,double size,double time,int counter)
330  {
331  instance().writeerr(h,size,time,counter);
332  }
333 
342  static void write(const double h,
343  const double size,
344  const double time,
345  const int counter,
346  std::ostream& out)
347  {
348  // print last line to out
349  instance().printerr( h, size, time, counter, out );
350 
351  // now write to file
352  instance().writeerr(h,size,time,counter);
353  }
354 
369  static void write(const double h,
370  const double size,
371  const double time,
372  const int counter,
373  const double avgTimeStep,
374  const double minTimeStep,
375  const double maxTimeStep,
376  const int newton_iterations,
377  const int ils_iterations,
378  const int max_newton_iterations,
379  const int max_ils_iterations)
380  {
381  std::vector< DoublePairType > doubleValues;
382  doubleValues.push_back( DoublePairType( "avg dt", avgTimeStep ) );
383  doubleValues.push_back( DoublePairType( "min dt", minTimeStep ) );
384  doubleValues.push_back( DoublePairType( "max dt", maxTimeStep ) );
385 
386  std::vector< IntPairType > intValues;
387  intValues.push_back( IntPairType( "Newton", newton_iterations ) );
388  intValues.push_back( IntPairType( "ILS", ils_iterations ) );
389  intValues.push_back( IntPairType( "max{Newton/linS}", max_newton_iterations ) );
390  intValues.push_back( IntPairType( "max{ILS/linS}", max_ils_iterations ) );
391 
392  // now write to file
393  instance().writeerr(h,size,time,counter, doubleValues, intValues );
394  }
395 
405  static void write(const double h,
406  const double size,
407  const double time,
408  const int counter,
409  const std::vector< DoublePairType >& doubleValues,
410  const std::vector< IntPairType >& intValues )
411  {
412  // now write to file
413  instance().writeerr(h,size,time,counter, doubleValues, intValues );
414  }
415 
430  static void write(const double h,
431  const double size,
432  const double time,
433  const int counter,
434  const double avgTimeStep,
435  const double minTimeStep,
436  const double maxTimeStep,
437  const int newton_iterations,
438  const int ils_iterations,
439  const int max_newton_iterations,
440  const int max_ils_iterations,
441  std::ostream& out)
442  {
443  std::vector< DoublePairType > doubleValues;
444  doubleValues.push_back( DoublePairType( "avg dt", avgTimeStep ) );
445  doubleValues.push_back( DoublePairType( "min dt", minTimeStep ) );
446  doubleValues.push_back( DoublePairType( "max dt", maxTimeStep ) );
447 
448  std::vector< IntPairType > intValues;
449  intValues.push_back( IntPairType( "Newton", newton_iterations ) );
450  intValues.push_back( IntPairType( "ILS", ils_iterations ) );
451  intValues.push_back( IntPairType( "max{Newton/linS}", max_newton_iterations ) );
452  intValues.push_back( IntPairType( "max{ILS/linS}", max_ils_iterations ) );
453 
454  // print last line to out
455  instance().printerr( h, size, time, counter, doubleValues, intValues, out );
456 
457  // now write to file
458  instance().writeerr(h,size,time,counter, doubleValues, intValues );
459  }
460 
475  static void write(const double h,
476  const double size,
477  const double time,
478  const int counter,
479  const std::vector< DoublePairType >& doubleValues,
480  const std::vector< IntPairType >& intValues,
481  std::ostream& out)
482  {
483  // print last line to out
484  instance().printerr( h, size, time, counter, doubleValues, intValues, out );
485 
486  // now write to file
487  instance().writeerr(h,size,time,counter, doubleValues, intValues );
488  }
489 
490  }; // end class FemEoc
491 
492 
494  : public Fem::AbstractColumnWriter< FemEoc::DataTuple >
495  {
497 
498  public:
499  ErrorColumnWriter ( const std::string &header, const int index )
500  : header_( header ),
501  index_( index )
502  {}
503 
504  std::string entry ( const FemEoc::DataTuple &data ) const
505  {
506  return toString( error( data ) );
507  }
508 
509  std::string header () const { return header_; }
510 
511  protected:
512  double error ( const FemEoc::DataTuple &data ) const
513  {
514  return std::get< 5 >( data )[ index_ ];
515  }
516 
517  std::string toString ( const double &error ) const
518  {
519  std::ostringstream s;
520  s << "$" << error << "$";
521  // s << " " << error << " ";
522  return s.str();
523  }
524 
525  private:
526  std::string header_;
527  int index_;
528  };
529 
530 
533  {
535 
536  public:
537  explicit EOCColumnWriter ( const int index )
538  : BaseType( "EOC", index ),
539  hOld_( std::numeric_limits< double >::infinity() )
540  {}
541 
542  std::string entry ( const DataTuple &data ) const
543  {
544  const double h = std::get< 1 >( data );
545  const double e = BaseType::error( data );
546 
547  std::string entry = "---";
548  if( hOld_ < std::numeric_limits< double >::infinity() )
549  entry = BaseType::toString( eoc( h, e ) );
550  hOld_ = h;
551  eOld_ = e;
552  return entry;
553  }
554 
555  double eoc ( const double h, const double e ) const
556  {
557  return std::log( e / eOld_ ) / std::log( h / hOld_ );
558  }
559 
560  private:
561  mutable double hOld_;
562  mutable double eOld_;
563  };
564 
565 
566 
567  inline void FemEoc
568  ::writeerr ( double h, double size, double time, int counter )
569  {
570  std::vector< DoublePairType > doubleValues;
571  std::vector< IntPairType > intValues;
572  writeerr( h, size, time, counter, doubleValues, intValues);
573  }
574 
575 
576  inline void FemEoc
577  ::writeerr(double h,double size,double time,int counter,
578  const std::vector< DoublePairType >& doubleValues,
579  const std::vector< IntPairType >& intValues )
580  {
581  if( MPIManager::rank() != 0 )
582  return;
583 
584  if( !tableWriter_ )
585  {
587  columns.push_back( new Fem::NumberColumnWriter< DataTuple, Fem::TupleDataSource< 0 > >( "level" ) );
588  columns.push_back( new Fem::NumberColumnWriter< DataTuple, Fem::TupleDataSource< 1 > >( "h" ) );
589  columns.push_back( new Fem::NumberColumnWriter< DataTuple, Fem::TupleDataSource< 2 > >( "size" ) );
590  columns.push_back( new Fem::NumberColumnWriter< DataTuple, Fem::TupleDataSource< 3 > >( "CPU-time" ) );
591  columns.push_back( new Fem::NumberColumnWriter< DataTuple, Fem::TupleDataSource< 4 > >( "counter" ) );
592  columns.push_back( (const TableWriter::ColumnWriterType *)0 );
593 
594  typedef Fem::ArrayDataSource< Fem::TupleDataSource< 6 > > DoubleValueSource;
595  for( unsigned int i = 0; i < doubleValues.size(); ++i )
596  {
597  columns.push_back( new Fem::NumberColumnWriter< DataTuple, DoubleValueSource >( doubleValues[ i ].first, DoubleValueSource( i ) ) );
598  }
599 
600  typedef Fem::ArrayDataSource< Fem::TupleDataSource< 7 > > IntValueSource;
601  for( unsigned int i = 0; i < intValues.size(); ++i )
602  {
603  columns.push_back( new Fem::NumberColumnWriter< DataTuple, IntValueSource >( intValues[ i ].first, IntValueSource( i ) ) );
604  }
605 
606  eocColumns_.resize( error_.size(), (const EOCColumnWriter *)0 );
607  for( unsigned int i = 0; i < error_.size(); ++i )
608  {
609  columns.push_back( (const TableWriter::ColumnWriterType *)0 );
610  columns.push_back( new ErrorColumnWriter( description_[ i ], i ) );
611  eocColumns_[ i ] = new EOCColumnWriter( i );
612  columns.push_back( eocColumns_[ i ] );
613  }
614 
615  tableWriter_ = new TableWriter( filename_, columns );
616  }
617 
618  std::vector< double > doubleVals( doubleValues.size() );
619  for( unsigned int i=0; i<doubleValues.size(); ++i )
620  doubleVals[ i ] = doubleValues[ i ].second;
621 
622  std::vector< int > intVals( intValues.size() );
623  for( unsigned int i=0; i<intValues.size(); ++i )
624  intVals[ i ] = intValues[ i ].second;
625 
626  DataTuple data( level_, h, size, time, counter, error_, doubleVals, intVals );
627  tableWriter_->writeRow( data );
628  ++level_;
629  }
630 
631 
632  inline void FemEoc
633  ::printerr(const double h,
634  const double size,
635  const double time,
636  const int counter,
637  std::ostream& out)
638  {
639  std::vector< DoublePairType > doubleValues;
640  std::vector< IntPairType > intValues;
641  printerr( h, size, time, counter, doubleValues, intValues, out );
642  }
643 
644  inline void FemEoc
645  ::printerr(const double h,
646  const double size,
647  const double time,
648  const int counter,
649  const std::vector< DoublePairType >& doubleValues,
650  const std::vector< IntPairType >& intValues,
651  std::ostream& out)
652  {
653  if (!Parameter::verbose()) return;
654 
655  out << "level: " << level_ << std::endl;
656  out << "h " << h << std::endl;
657  out << "size: " << size << std::endl;
658  out << "time: " << time << " sec. " << std::endl;
659  out << "counter: " << counter << std::endl;
660  for( unsigned int i=0; i<doubleValues.size(); ++i )
661  {
662  out << doubleValues[ i ].first << ": " << doubleValues[ i ].second << std::endl;
663  }
664  for( unsigned int i=0; i<intValues.size(); ++i )
665  {
666  out << intValues[ i ].first << ": " << intValues[ i ].second << std::endl;
667  }
668 
669  for (unsigned int i=0;i<error_.size();++i)
670  {
671  out << description_[i] << ": " << error_[i] << std::endl;
672  if( tableWriter_ )
673  {
674  const double eoc = eocColumns_[ i ]->eoc( h, error_[ i ] );
675  out << "EOC (" <<description_[i] << "): " << eoc << std::endl;
676  }
677  out << std::endl;
678  }
679  }
680 
681  } // end namespace Fem
682 
683 } // end namespace Dune
684 
685 #endif // #ifndef DUNE_FEM_FEMEOC_HH
EOCColumnWriter(const int index)
Definition: femeoc.hh:537
int main(int argc, char **argv)
Definition: dataconvert.cc:29
static void write(const double h, const double size, const double time, const int counter, const std::vector< DoublePairType > &doubleValues, const std::vector< IntPairType > &intValues)
commit a line to the eoc file
Definition: femeoc.hh:405
ErrorColumnWriter(const std::string &header, const int index)
Definition: femeoc.hh:499
static int rank()
Definition: mpimanager.hh:116
static void clear()
close file and allow FemEoc to used for a second run
Definition: femeoc.hh:244
gets the N th element of a provided tuple assuming its a number
Definition: latextablewriter.hh:166
std::string entry(const FemEoc::DataTuple &data) const
Definition: femeoc.hh:504
Write a self contained tex table for eoc runs with timing information.
Definition: femeoc.hh:48
bool createDirectory(const std::string &inName)
create a directory
Definition: io.cc:19
Definition: latextablewriter.hh:61
static void write(const double h, const double size, const double time, const int counter, const double avgTimeStep, const double minTimeStep, const double maxTimeStep, const int newton_iterations, const int ils_iterations, const int max_newton_iterations, const int max_ils_iterations, std::ostream &out)
commit a line to the eoc file
Definition: femeoc.hh:430
static double log(const Double &v)
Definition: double.hh:865
Class representing column writer in general.
Definition: latextablewriter.hh:39
static void write(const double h, const double size, const double time, const int counter, const std::vector< DoublePairType > &doubleValues, const std::vector< IntPairType > &intValues, std::ostream &out)
commit a line to the eoc file
Definition: femeoc.hh:475
static void setErrors(size_t id, const FieldVector< double, SIZE > &err)
add a vector in a FieldVector of error values for the given id (returned by addEntry) ...
Definition: femeoc.hh:313
writes latex tables based on user-defined row structure
Definition: latextablewriter.hh:244
std::string toString(const double &error) const
Definition: femeoc.hh:517
Definition: coordinate.hh:4
std::vector< const ColumnWriterType * > ColumnWriterVectorType
Abstract column vector type.
Definition: latextablewriter.hh:249
static size_t addEntry(const StrVectorType &descript, size_t size)
add a vector of new eoc values
Definition: femeoc.hh:263
double eoc(const double h, const double e) const
Definition: femeoc.hh:555
static size_t addEntry(const char *descript)
add a single new eoc output
Definition: femeoc.hh:287
Definition: latextablewriter.hh:80
std::string header() const
Definition: femeoc.hh:509
static void write(double h, double size, double time, int counter)
commit a line to the eoc file
Definition: femeoc.hh:329
static void initialize(const std::string &path, const std::string &name, const std::string &descript)
open file path/name and write a description string into tex file
Definition: femeoc.hh:248
STL namespace.
std::string path
Definition: readioparams.cc:155
static size_t addEntry(const std::string &descript)
add a single new eoc output
Definition: femeoc.hh:280
static void setErrors(size_t id, const VectorType &err)
add a vector of error values for the given id (returned by addEntry)
Definition: femeoc.hh:306
static bool verbose()
obtain the cached value for fem.verbose
Definition: io/parameter.hh:444
Definition: femeoc.hh:531
static void write(const double h, const double size, const double time, const int counter, const double avgTimeStep, const double minTimeStep, const double maxTimeStep, const int newton_iterations, const int ils_iterations, const int max_newton_iterations, const int max_ils_iterations)
commit a line to the eoc file
Definition: femeoc.hh:369
static void setErrors(size_t id, const VectorType &err, int size)
add a vector of error values for the given id (returned by addEntry)
Definition: femeoc.hh:296
std::string entry(const DataTuple &data) const
Definition: femeoc.hh:542
static FemEoc & instance()
Definition: femeoc.hh:239
double error(const FemEoc::DataTuple &data) const
Definition: femeoc.hh:512
static void initialize(const std::string &name, const std::string &descript)
open file name and write description string into tex file
Definition: femeoc.hh:252
static void write(const double h, const double size, const double time, const int counter, std::ostream &out)
commit a line to the eoc file
Definition: femeoc.hh:342
static void setErrors(size_t id, const double &err)
add a single error value for the given id (returned by addEntry)
Definition: femeoc.hh:319
Definition: femeoc.hh:493
static size_t addEntry(const StrVectorType &descript)
add a vector of new eoc values
Definition: femeoc.hh:273