dune-fem  2.4.1-rc
femeoctable.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_FEMEOCTABLE_HH
2 #define DUNE_FEM_FEMEOCTABLE_HH
3 
4 #include <cassert>
5 #include <iostream>
6 #include <sstream>
7 #include <fstream>
8 #include <vector>
9 
10 #include <dune/common/exceptions.hh>
11 #include <dune/common/fvector.hh>
12 
13 #include <dune/fem/io/io.hh>
14 #include <dune/fem/io/parameter.hh>
15 
16 namespace Dune
17 {
18 
19  namespace Fem
20  {
21 
44  /*
46  class BaseEocCalculator
47  {
48  public:
49  static double calculate (double,double,double,double)=0;
50  }
51  */
52 
55  {
56  public:
57  static double calculate (double &eold, double &enew, double &hold, double &hnew )
58  {
59  double ret=0;
60  ret = log(eold/enew)/log(hold/hnew);
61  return ret;
62  }
63  };
64 
65 
66 
69  {
70  int nrOfTabs_;
71  std::vector< std::stringstream* > outputFile_;
72  std::vector< std::string > fileNames_;
73  std::vector< int > level_;
74  std::vector< std::vector< double > > prevError_;
75  std::vector< std::vector< double > > error_;
76  std::vector< std::vector< std::string > > description_;
77  std::vector< double > prevh_;
78  std::vector< bool > initial_;
79  std::vector< std::vector< int > > pos_;
80 
81  FemEocTable() :
82  nrOfTabs_(0),
83  outputFile_(),
84  fileNames_(),
85  level_(),
86  prevError_(),
87  error_(),
88  description_(),
89  prevh_(),
90  initial_(),
91  pos_()
92  {
93  }
94 
95  ~FemEocTable() {
96  std::ofstream filewriter;
97 
98  for(size_t k=0;k<outputFile_.size();++k)
99  {
100  std::stringstream filename;
101  filename << fileNames_[k] <<".dat";
102  filewriter.open( filename.str().c_str() );
103  filewriter << outputFile_[k]->str();
104  filewriter.close();
105  delete outputFile_[k];
106  outputFile_[k] = 0;
107  }
108  }
109 
110  int init(const std::string& path,
111  const std::string& name, const std::string& descript)
112  {
113  if (MPIManager::rank() != 0) return -1;
114  if( !createDirectory( path ) )
115  DUNE_THROW( IOError, "Failed to create path `" << path << "'." );
116  return init(path+"/"+name,descript);
117  }
118 
119  int init(const std::string& filename, const std::string& descript)
120  {
121  if (MPIManager::rank() != 0) return -1;
122 
124  outputFile_.push_back((std::stringstream *) 0);
125 
126  fileNames_.push_back("");
127  level_.push_back(0);
128  prevError_.push_back( std::vector< double >() );
129  error_.push_back( std::vector< double >() );
130  description_.push_back( std::vector< std::string >() );
131  prevh_.push_back(0);
132  initial_.push_back(1);
133  pos_.push_back( std::vector< int >() );
134 
135  const int tabId = outputFile_.size() -1;
136 
137  fileNames_[tabId] = filename;
138  outputFile_[tabId] = new std::stringstream;
139 
140  // write together with table the description
141  // of the scheme and problem in the simulation
142  *outputFile_[tabId] << descript << "\n";
143  nrOfTabs_ ++;
144 
145  return tabId;
146  }
147 
148  void checkTabId (const int tabId)
149  {
150  if (tabId > nrOfTabs_ || tabId <0)
151  {
152  std::cout<<"No table with id:"<<tabId<<" existing!"<<std::endl;
153  abort();
154  }
155  }
156 
157  template <class StrVectorType>
158  size_t addentry(const int tabId, const StrVectorType& descript,size_t size)
159  {
160  checkTabId(tabId);
161 
162  if (!initial_[tabId])
163  abort();
164  pos_[tabId].push_back(error_[tabId].size());
165  for (size_t i=0;i<size;++i) {
166  error_[tabId].push_back(0);
167  prevError_[tabId].push_back(0);
168  description_[tabId].push_back(descript[i]);
169  }
170  return pos_[tabId].size()-1;
171  }
172 
173  size_t addentry(const int tabId, const std::string& descript) {
174 
175  checkTabId(tabId);
176 
177  if (!initial_[tabId])
178  abort();
179  pos_[tabId].push_back(error_[tabId].size());
180  error_[tabId].push_back(0);
181  prevError_[tabId].push_back(0);
182  description_[tabId].push_back(descript);
183  return pos_[tabId].size()-1;
184  }
185 
186  template <class VectorType>
187  void seterrors(const int tabId, size_t id,const VectorType& err,size_t size)
188  {
189  checkTabId(tabId);
190 
191  assert(id<pos_[tabId].size());
192  int pos = pos_[tabId][ id ];
193  assert(pos+size <= error_[tabId].size());
194 
195  for (size_t i=0; i<size; ++i)
196  error_[tabId][pos+i] = err[i];
197  }
198 
199  template <int SIZE>
200  void seterrors(const int tabId, size_t id,const FieldVector<double,SIZE>& err)
201  {
202  seterrors(tabId,id,err,SIZE);
203  }
204 
205  void seterrors(const int tabId, size_t id,const double& err) {
206  checkTabId(tabId);
207 
208  int pos = pos_[tabId][id];
209  error_[tabId][pos] = err;
210  }
211 
213  template<class EocCalculator>
214  void writeerr( const int tabId,
215  std::vector<double> &vals,
216  std::vector<std::string> &descriptions,
217  std::string &delimiter,
218  std::string &terminatingChar,
219  std::string &header,
220  std::string &tableSpacer,
221  std::string &footer)
222 
223  {
224 
225  typedef EocCalculator EocCalculatorType;
226 
227  checkTabId(tabId);
228 
229  assert(vals.size() == descriptions.size());
230  if (MPIManager::rank() != 0) return;
231 
232  if (initial_[tabId]) {
233  *outputFile_[tabId] << header;
234  for(unsigned int k=0;k<descriptions.size();++k)
235  {
236  *outputFile_[tabId] << descriptions[k] << delimiter;
237  }
238  for (unsigned int i=0;i<error_[tabId].size();i++)
239  {
240  *outputFile_[tabId] << description_[tabId][i] << delimiter << "EOC" << delimiter;
241  }
242  *outputFile_[tabId] << terminatingChar << "\n" << tableSpacer <<"\n";
243  }
244 
245  *outputFile_[tabId] << level_[tabId] << delimiter;
246  for(unsigned int k =0; k<vals.size(); ++k)
247  *outputFile_[tabId] << vals[k] << delimiter;
248 
249  for (unsigned int i=0;i<error_[tabId].size();++i) {
250  *outputFile_[tabId] <<delimiter << error_[tabId][i] << delimiter;
251  if (initial_[tabId]) {
252  *outputFile_[tabId] << " --- ";
253  }
254  else {
255  *outputFile_[tabId] << EocCalculatorType :: calculate(prevError_[tabId][i], error_[tabId][i], prevh_[tabId], vals[0] );
256  }
257  prevError_[tabId][i]=error_[tabId][i];
258  error_[tabId][i] = -1; // uninitialized
259  }
260  *outputFile_[tabId] << terminatingChar<<"\n" << footer;
261 
263  // the file
264  outputFile_[tabId]->seekp(0,std::ios::end);
265  int length = outputFile_[tabId]->tellp();
266  length -= footer.length();
267  outputFile_[tabId]->seekp(length, std::ios::beg);
268 
269 
270  prevh_[tabId] = vals[0];
271  level_[tabId] ++;
272  initial_[tabId] = false;
273  }
274 
275  template<class EocCalculator>
276  void printerr(const int tabId,
277  std::vector<double> vals,
278  std::vector<std::string> descriptions,
279  std::ostream& out)
280  {
281  typedef EocCalculator EocCalculatorType;
282 
283  checkTabId(tabId);
284 
285  assert(descriptions.size() == vals.size());
286 
287  if (!Parameter::verbose()) return;
288 
289  out << "level: " << level_[tabId] << std::endl;
290  for(unsigned int k =0 ;k< vals.size();++k)
291  out << descriptions[k]<<": " << vals[k] << std::endl;
292 
293  for (unsigned int i=0;i<error_[tabId].size();++i)
294  {
295 
296  out << description_[tabId][i] << ": " << error_[tabId][i] << std::endl;
297  if (! initial_[tabId])
298  {
299  const double eoc = EocCalculatorType :: calculate( prevError_[tabId][i], error_[tabId][i], prevh_[tabId], vals[0]);
300 
301  out << "EOC (" <<description_[tabId][i] << "): " << eoc << std::endl;
302  }
303  out << std::endl;
304  }
305  }
306  public:
307  static FemEocTable& instance() {
308  static FemEocTable instance_;
309  return instance_;
310  }
311 
314  static int initialize(const std::string& path, const std::string& name, const std::string& descript) {
315  return instance().init(path,name,descript);
316  }
319  static int initialize(const std::string& name, const std::string& descript) {
320  return instance().init(name,descript);
321  }
322 
333  template <class StrVectorType>
334  static size_t addEntry(const int tabId, const StrVectorType& descript,size_t size) {
335  return instance().addentry(tabId,descript,size);
336  }
337 
338  template <class StrVectorType>
339  static size_t addEntry(const StrVectorType& descript,size_t size) {
340  return instance().addentry(0,descript,size);
341  }
342 
351  template <class StrVectorType>
352  static size_t addEntry(const int tabId,const StrVectorType& descript) {
353  return instance().addentry(tabId,descript,descript.size());
354  }
355 
356  template <class StrVectorType>
357  static size_t addEntry(const StrVectorType& descript) {
358  return instance().addentry(0,descript,descript.size());
359  }
366  static size_t addEntry(const int tabId, const std::string& descript) {
367  return instance().addentry(tabId, descript);
368  }
369 
370  static size_t addEntry(const std::string& descript) {
371  return instance().addentry(0, descript);
372  }
373 
380  static size_t addEntry(const int tabId, const char* descript) {
381  return addEntry(tabId,std::string(descript));
382  }
383 
384  static size_t addEntry(const char* descript) {
385  return addEntry(0,std::string(descript));
386  }
387 
388 
398  template <class VectorType>
399  static void setErrors(const int tabId, size_t id,const VectorType& err,int size)
400  {
401  instance().seterrors(tabId,id,err,size);
402  }
403 
404  template <class VectorType>
405  static void setErrors(size_t id,const VectorType& err,int size)
406  {
407  instance().seterrors(0,id,err,size);
408  }
409 
410 
419  template <class VectorType>
420  static void setErrors(const int tabId, size_t id,const VectorType& err) {
421  instance().seterrors(tabId,id,err,err.size());
422  }
423 
424  template <class VectorType>
425  static void setErrors(size_t id,const VectorType& err) {
426  instance().seterrors(0,id,err,err.size());
427  }
428 
429 
436  template <int SIZE>
437  static void setErrors(const int tabId, size_t id,const FieldVector<double,SIZE>& err) {
438  instance().seterrors(tabId,id,err);
439  }
440 
441  template <int SIZE>
442  static void setErrors(size_t id,const FieldVector<double,SIZE>& err) {
443  instance().seterrors(0,id,err);
444  }
451  static void setErrors(const int tabId,size_t id,const double& err) {
452  instance().seterrors(tabId,id,err);
453  }
454 
455  static void setErrors(size_t id,const double& err) {
456  instance().seterrors(0,id,err);
457  }
458 
474  static void write(const int tabId,
475  std::vector<double> &vals,
476  std::vector<std::string> &descriptions,
477  std::string delimiter = " ",
478  std::string terminatingChar = "",
479  std::string header ="",
480  std::string tableSpacer ="",
481  std::string footer ="" )
482  {
483  instance().writeerr<DefaultEocCalculator> (tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
484  }
485 
486  static void write(std::vector<double> &vals,
487  std::vector<std::string> &descriptions,
488  std::string delimiter = " ",
489  std::string terminatingChar ="",
490  std::string header ="",
491  std::string tableSpacer ="",
492  std::string footer =""
493  )
494  {
495  instance().writeerr<DefaultEocCalculator> (0, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
496  }
497 
498 
513  template<class EocCalculatorType>
514  static void write(const int tabId,
515  std::vector<double> &vals,
516  std::vector<std::string> &descriptions,
517  std::string delimiter = " ",
518  std::string terminatingChar ="",
519  std::string header ="",
520  std::string tableSpacer ="",
521  std::string footer =""
522  )
523  {
524  instance().template writeerr<EocCalculatorType>(tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
525  }
526 
527  template<class EocCalculatorType>
528  static void write(std::vector<double> &vals,
529  std::vector<std::string> &descriptions,
530  std::string delimiter = " ",
531  std::string terminatingChar ="",
532  std::string header ="",
533  std::string tableSpacer ="",
534  std::string footer =""
535  )
536  {
537  instance().template writeerr<EocCalculatorType>(0, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer );
538  }
539 
553  static void write(const int tabId,
554  std::vector<double> &vals,
555  std::vector<std::string> &descriptions,
556  std::ostream& out,
557  std::string delimiter = " ",
558  std::string terminatingChar ="",
559  std::string header = "",
560  std::string tableSpacer = "",
561  std::string footer = "")
562  {
563  // print last line to out
564  instance().printerr<DefaultEocCalculator>( tabId, vals, descriptions, out );
565 
566  // now write to file
567  instance().writeerr<DefaultEocCalculator>(tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
568  }
569 
570  static void write(std::vector<double> &vals,
571  std::vector<std::string> &descriptions,
572  std::ostream& out,
573  std::string delimiter = " ",
574  std::string terminatingChar ="",
575  std::string header = "",
576  std::string tableSpacer = "",
577  std::string footer = "")
578  {
579  // print last line to out
580  instance().printerr<DefaultEocCalculator>( 0, vals, descriptions, out );
581 
582  // now write to file
583  instance().writeerr<DefaultEocCalculator>(0, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
584  }
585 
586 
600  template <class EocCalculatorType>
601  static void write(const int tabId,
602  std::vector<double> &vals,
603  std::vector<std::string> &descriptions,
604  std::ostream& out,
605  std::string delimiter = " ",
606  std::string terminatingChar ="",
607  std::string header = "",
608  std::string tableSpacer = "",
609  std::string footer = "")
610  {
611  // print last line to out
612  instance().template printerr<EocCalculatorType>( tabId, vals, descriptions, out );
613 
614  // now write to file
615  instance().template writeerr<EocCalculatorType>(tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
616  }
617 
618  template <class EocCalculatorType>
619  static void write(std::vector<double> &vals,
620  std::vector<std::string> &descriptions,
621  std::ostream& out,
622  std::string delimiter = " ",
623  std::string terminatingChar ="",
624  std::string header = "",
625  std::string tableSpacer = "",
626  std::string footer = "")
627  {
628  // print last line to out
629  instance().template printerr<EocCalculatorType>(0, vals, descriptions, out );
630 
631  // now write to file
632  instance().template writeerr<EocCalculatorType>(0, vals, descriptions,delimiter, terminatingChar, header, tableSpacer, footer);
633  }
634 
635  }; // class FemEocTable
636 
637  } // namespace Fem
638 
639 } // namespace Dune
640 
641 #endif // #ifndef DUNE_FEM_FEMEOCTABLE_HH
static void setErrors(size_t id, const FieldVector< double, SIZE > &err)
Definition: femeoctable.hh:442
static int rank()
Definition: mpimanager.hh:116
static size_t addEntry(const std::string &descript)
Definition: femeoctable.hh:370
static void setErrors(const int tabId, size_t id, const double &err)
add a single error value for the given id (returned by addEntry)
Definition: femeoctable.hh:451
static void setErrors(const int tabId, 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: femeoctable.hh:437
static void write(const int tabId, std::vector< double > &vals, std::vector< std::string > &descriptions, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
commit a line to the eoc file
Definition: femeoctable.hh:474
static size_t addEntry(const char *descript)
Definition: femeoctable.hh:384
static size_t addEntry(const int tabId, const StrVectorType &descript, size_t size)
add a vector of new eoc values
Definition: femeoctable.hh:334
static size_t addEntry(const StrVectorType &descript)
Definition: femeoctable.hh:357
static void write(const int tabId, std::vector< double > &vals, std::vector< std::string > &descriptions, std::ostream &out, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
commit a line to the eoc file, using EocCalculatorType for non standart Eoc calculations.
Definition: femeoctable.hh:601
Write a self contained tex table for eoc runs with timing information.
Definition: femeoctable.hh:54
bool createDirectory(const std::string &inName)
create a directory
Definition: io.cc:19
static double log(const Double &v)
Definition: double.hh:865
The Fem Eoc Table writer.
Definition: femeoctable.hh:68
Definition: coordinate.hh:4
static int initialize(const std::string &name, const std::string &descript)
Definition: femeoctable.hh:319
static void write(std::vector< double > &vals, std::vector< std::string > &descriptions, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
Definition: femeoctable.hh:528
static size_t addEntry(const int tabId, const StrVectorType &descript)
add a vector of new eoc values
Definition: femeoctable.hh:352
static int initialize(const std::string &path, const std::string &name, const std::string &descript)
Definition: femeoctable.hh:314
static void write(const int tabId, std::vector< double > &vals, std::vector< std::string > &descriptions, std::ostream &out, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
commit a line to the eoc file
Definition: femeoctable.hh:553
static void setErrors(const int tabId, size_t id, const VectorType &err, int size)
add a vector of error values for the given id (returned by addEntry)
Definition: femeoctable.hh:399
static void setErrors(const int tabId, size_t id, const VectorType &err)
add a vector of error values for the given id (returned by addEntry)
Definition: femeoctable.hh:420
std::string path
Definition: readioparams.cc:155
static void write(const int tabId, std::vector< double > &vals, std::vector< std::string > &descriptions, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
commit a line to the eoc file, using EocCalculatorType to calculate the eoc.
Definition: femeoctable.hh:514
static void setErrors(size_t id, const VectorType &err, int size)
Definition: femeoctable.hh:405
static size_t addEntry(const StrVectorType &descript, size_t size)
Definition: femeoctable.hh:339
static bool verbose()
obtain the cached value for fem.verbose
Definition: io/parameter.hh:444
static void write(std::vector< double > &vals, std::vector< std::string > &descriptions, std::ostream &out, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
Definition: femeoctable.hh:570
static size_t addEntry(const int tabId, const std::string &descript)
add a single new eoc output
Definition: femeoctable.hh:366
static double calculate(double &eold, double &enew, double &hold, double &hnew)
Definition: femeoctable.hh:57
static void write(std::vector< double > &vals, std::vector< std::string > &descriptions, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
Definition: femeoctable.hh:486
static void write(std::vector< double > &vals, std::vector< std::string > &descriptions, std::ostream &out, std::string delimiter=" ", std::string terminatingChar="", std::string header="", std::string tableSpacer="", std::string footer="")
Definition: femeoctable.hh:619
static void setErrors(size_t id, const double &err)
Definition: femeoctable.hh:455
static size_t addEntry(const int tabId, const char *descript)
add a single new eoc output
Definition: femeoctable.hh:380
static void setErrors(size_t id, const VectorType &err)
Definition: femeoctable.hh:425
static FemEocTable & instance()
Definition: femeoctable.hh:307