DUNE-FEM (unstable)

femeoctable.hh
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 
11 #include <dune/common/fvector.hh>
12 
13 #include <dune/fem/io/io.hh>
14 #include <dune/fem/io/parameter.hh>
15 #include <dune/fem/storage/singleton.hh>
16 
17 namespace Dune
18 {
19 
20  namespace Fem
21  {
22 
45  /*
47  class BaseEocCalculator
48  {
49  public:
50  static double calculate (double,double,double,double)=0;
51  }
52  */
53 
56  {
57  public:
58  static double calculate (double &eold, double &enew, double &hold, double &hnew )
59  {
60  double ret=0;
61  ret = log(eold/enew)/log(hold/hnew);
62  return ret;
63  }
64  };
65 
66 
67 
70  {
71  int nrOfTabs_;
72  std::vector< std::stringstream* > outputFile_;
73  std::vector< std::string > fileNames_;
74  std::vector< int > level_;
75  std::vector< std::vector< double > > prevError_;
76  std::vector< std::vector< double > > error_;
77  std::vector< std::vector< std::string > > description_;
78  std::vector< double > prevh_;
79  std::vector< bool > initial_;
80  std::vector< std::vector< int > > pos_;
81 
82  public:
83  FemEocTable() :
84  nrOfTabs_(0),
85  outputFile_(),
86  fileNames_(),
87  level_(),
88  prevError_(),
89  error_(),
90  description_(),
91  prevh_(),
92  initial_(),
93  pos_()
94  {
95  }
96 
97  ~FemEocTable() {
98  std::ofstream filewriter;
99 
100  for(size_t k=0;k<outputFile_.size();++k)
101  {
102  std::stringstream filename;
103  filename << fileNames_[k] <<".dat";
104  filewriter.open( filename.str().c_str() );
105  filewriter << outputFile_[k]->str();
106  filewriter.close();
107  delete outputFile_[k];
108  outputFile_[k] = 0;
109  }
110  }
111 
112  private:
113  int init(const std::string& path,
114  const std::string& name, const std::string& descript)
115  {
116  if (MPIManager::rank() != 0) return -1;
117  if( !createDirectory( path ) )
118  DUNE_THROW( IOError, "Failed to create path `" << path << "'." );
119  return init(path+"/"+name,descript);
120  }
121 
122  int init(const std::string& filename, const std::string& descript)
123  {
124  if (MPIManager::rank() != 0) return -1;
125 
127  outputFile_.push_back((std::stringstream *) 0);
128 
129  fileNames_.push_back("");
130  level_.push_back(0);
131  prevError_.push_back( std::vector< double >() );
132  error_.push_back( std::vector< double >() );
133  description_.push_back( std::vector< std::string >() );
134  prevh_.push_back(0);
135  initial_.push_back(1);
136  pos_.push_back( std::vector< int >() );
137 
138  const int tabId = outputFile_.size() -1;
139 
140  fileNames_[tabId] = filename;
141  outputFile_[tabId] = new std::stringstream;
142 
143  // write together with table the description
144  // of the scheme and problem in the simulation
145  *outputFile_[tabId] << descript << "\n";
146  nrOfTabs_ ++;
147 
148  return tabId;
149  }
150 
151  void checkTabId (const int tabId)
152  {
153  if (tabId > nrOfTabs_ || tabId <0)
154  {
155  std::cout<<"No table with id:"<<tabId<<" existing!"<<std::endl;
156  abort();
157  }
158  }
159 
160  template <class StrVectorType>
161  size_t addentry(const int tabId, const StrVectorType& descript,size_t size)
162  {
163  checkTabId(tabId);
164 
165  if (!initial_[tabId])
166  abort();
167  pos_[tabId].push_back(error_[tabId].size());
168  for (size_t i=0;i<size;++i) {
169  error_[tabId].push_back(0);
170  prevError_[tabId].push_back(0);
171  description_[tabId].push_back(descript[i]);
172  }
173  return pos_[tabId].size()-1;
174  }
175 
176  size_t addentry(const int tabId, const std::string& descript) {
177 
178  checkTabId(tabId);
179 
180  if (!initial_[tabId])
181  abort();
182  pos_[tabId].push_back(error_[tabId].size());
183  error_[tabId].push_back(0);
184  prevError_[tabId].push_back(0);
185  description_[tabId].push_back(descript);
186  return pos_[tabId].size()-1;
187  }
188 
189  template <class VectorType>
190  void seterrors(const int tabId, size_t id,const VectorType& err,size_t size)
191  {
192  checkTabId(tabId);
193 
194  assert(id<pos_[tabId].size());
195  int pos = pos_[tabId][ id ];
196  assert(pos+size <= error_[tabId].size());
197 
198  for (size_t i=0; i<size; ++i)
199  error_[tabId][pos+i] = err[i];
200  }
201 
202  template <int SIZE>
203  void seterrors(const int tabId, size_t id,const FieldVector<double,SIZE>& err)
204  {
205  seterrors(tabId,id,err,SIZE);
206  }
207 
208  void seterrors(const int tabId, size_t id,const double& err) {
209  checkTabId(tabId);
210 
211  int pos = pos_[tabId][id];
212  error_[tabId][pos] = err;
213  }
214 
216  template<class EocCalculator>
217  void writeerr( const int tabId,
218  std::vector<double> &vals,
219  std::vector<std::string> &descriptions,
220  std::string &delimiter,
221  std::string &terminatingChar,
222  std::string &header,
223  std::string &tableSpacer,
224  std::string &footer)
225 
226  {
227 
228  typedef EocCalculator EocCalculatorType;
229 
230  checkTabId(tabId);
231 
232  assert(vals.size() == descriptions.size());
233  if (MPIManager::rank() != 0) return;
234 
235  if (initial_[tabId]) {
236  *outputFile_[tabId] << header;
237  for(unsigned int k=0;k<descriptions.size();++k)
238  {
239  *outputFile_[tabId] << descriptions[k] << delimiter;
240  }
241  for (unsigned int i=0;i<error_[tabId].size();i++)
242  {
243  *outputFile_[tabId] << description_[tabId][i] << delimiter << "EOC" << delimiter;
244  }
245  *outputFile_[tabId] << terminatingChar << "\n" << tableSpacer <<"\n";
246  }
247 
248  *outputFile_[tabId] << level_[tabId] << delimiter;
249  for(unsigned int k =0; k<vals.size(); ++k)
250  *outputFile_[tabId] << vals[k] << delimiter;
251 
252  for (unsigned int i=0;i<error_[tabId].size();++i) {
253  *outputFile_[tabId] <<delimiter << error_[tabId][i] << delimiter;
254  if (initial_[tabId]) {
255  *outputFile_[tabId] << " --- ";
256  }
257  else {
258  *outputFile_[tabId] << EocCalculatorType :: calculate(prevError_[tabId][i], error_[tabId][i], prevh_[tabId], vals[0] );
259  }
260  prevError_[tabId][i]=error_[tabId][i];
261  error_[tabId][i] = -1; // uninitialized
262  }
263  *outputFile_[tabId] << terminatingChar<<"\n" << footer;
264 
266  // the file
267  outputFile_[tabId]->seekp(0,std::ios::end);
268  int length = outputFile_[tabId]->tellp();
269  length -= footer.length();
270  outputFile_[tabId]->seekp(length, std::ios::beg);
271 
272 
273  prevh_[tabId] = vals[0];
274  level_[tabId] ++;
275  initial_[tabId] = false;
276  }
277 
278  template<class EocCalculator>
279  void printerr(const int tabId,
280  std::vector<double> vals,
281  std::vector<std::string> descriptions,
282  std::ostream& out)
283  {
284  typedef EocCalculator EocCalculatorType;
285 
286  checkTabId(tabId);
287 
288  assert(descriptions.size() == vals.size());
289 
290  if (!Parameter::verbose()) return;
291 
292  out << "level: " << level_[tabId] << std::endl;
293  for(unsigned int k =0 ;k< vals.size();++k)
294  out << descriptions[k]<<": " << vals[k] << std::endl;
295 
296  for (unsigned int i=0;i<error_[tabId].size();++i)
297  {
298 
299  out << description_[tabId][i] << ": " << error_[tabId][i] << std::endl;
300  if (! initial_[tabId])
301  {
302  const double eoc = EocCalculatorType :: calculate( prevError_[tabId][i], error_[tabId][i], prevh_[tabId], vals[0]);
303 
304  out << "EOC (" <<description_[tabId][i] << "): " << eoc << std::endl;
305  }
306  out << std::endl;
307  }
308  }
309  public:
310  friend class Dune::Fem::Singleton< FemEocTable >;
311 
312  static FemEocTable& instance()
313  {
315  }
316 
319  static int initialize(const std::string& path, const std::string& name, const std::string& descript) {
320  return instance().init(path,name,descript);
321  }
324  static int initialize(const std::string& name, const std::string& descript) {
325  return instance().init(name,descript);
326  }
327 
338  template <class StrVectorType>
339  static size_t addEntry(const int tabId, const StrVectorType& descript,size_t size) {
340  return instance().addentry(tabId,descript,size);
341  }
342 
343  template <class StrVectorType>
344  static size_t addEntry(const StrVectorType& descript,size_t size) {
345  return instance().addentry(0,descript,size);
346  }
347 
356  template <class StrVectorType>
357  static size_t addEntry(const int tabId,const StrVectorType& descript) {
358  return instance().addentry(tabId,descript,descript.size());
359  }
360 
361  template <class StrVectorType>
362  static size_t addEntry(const StrVectorType& descript) {
363  return instance().addentry(0,descript,descript.size());
364  }
371  static size_t addEntry(const int tabId, const std::string& descript) {
372  return instance().addentry(tabId, descript);
373  }
374 
375  static size_t addEntry(const std::string& descript) {
376  return instance().addentry(0, descript);
377  }
378 
385  static size_t addEntry(const int tabId, const char* descript) {
386  return addEntry(tabId,std::string(descript));
387  }
388 
389  static size_t addEntry(const char* descript) {
390  return addEntry(0,std::string(descript));
391  }
392 
393 
403  template <class VectorType>
404  static void setErrors(const int tabId, size_t id,const VectorType& err,int size)
405  {
406  instance().seterrors(tabId,id,err,size);
407  }
408 
409  template <class VectorType>
410  static void setErrors(size_t id,const VectorType& err,int size)
411  {
412  instance().seterrors(0,id,err,size);
413  }
414 
415 
424  template <class VectorType>
425  static void setErrors(const int tabId, size_t id,const VectorType& err) {
426  instance().seterrors(tabId,id,err,err.size());
427  }
428 
429  template <class VectorType>
430  static void setErrors(size_t id,const VectorType& err) {
431  instance().seterrors(0,id,err,err.size());
432  }
433 
434 
441  template <int SIZE>
442  static void setErrors(const int tabId, size_t id,const FieldVector<double,SIZE>& err) {
443  instance().seterrors(tabId,id,err);
444  }
445 
446  template <int SIZE>
447  static void setErrors(size_t id,const FieldVector<double,SIZE>& err) {
448  instance().seterrors(0,id,err);
449  }
456  static void setErrors(const int tabId,size_t id,const double& err) {
457  instance().seterrors(tabId,id,err);
458  }
459 
460  static void setErrors(size_t id,const double& err) {
461  instance().seterrors(0,id,err);
462  }
463 
479  static void write(const int tabId,
480  std::vector<double> &vals,
481  std::vector<std::string> &descriptions,
482  std::string delimiter = " ",
483  std::string terminatingChar = "",
484  std::string header ="",
485  std::string tableSpacer ="",
486  std::string footer ="" )
487  {
488  instance().writeerr<DefaultEocCalculator> (tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
489  }
490 
491  static void write(std::vector<double> &vals,
492  std::vector<std::string> &descriptions,
493  std::string delimiter = " ",
494  std::string terminatingChar ="",
495  std::string header ="",
496  std::string tableSpacer ="",
497  std::string footer =""
498  )
499  {
500  instance().writeerr<DefaultEocCalculator> (0, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
501  }
502 
503 
518  template<class EocCalculatorType>
519  static void write(const int tabId,
520  std::vector<double> &vals,
521  std::vector<std::string> &descriptions,
522  std::string delimiter = " ",
523  std::string terminatingChar ="",
524  std::string header ="",
525  std::string tableSpacer ="",
526  std::string footer =""
527  )
528  {
529  instance().template writeerr<EocCalculatorType>(tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
530  }
531 
532  template<class EocCalculatorType>
533  static void write(std::vector<double> &vals,
534  std::vector<std::string> &descriptions,
535  std::string delimiter = " ",
536  std::string terminatingChar ="",
537  std::string header ="",
538  std::string tableSpacer ="",
539  std::string footer =""
540  )
541  {
542  instance().template writeerr<EocCalculatorType>(0, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer );
543  }
544 
558  static void write(const int tabId,
559  std::vector<double> &vals,
560  std::vector<std::string> &descriptions,
561  std::ostream& out,
562  std::string delimiter = " ",
563  std::string terminatingChar ="",
564  std::string header = "",
565  std::string tableSpacer = "",
566  std::string footer = "")
567  {
568  // print last line to out
569  instance().printerr<DefaultEocCalculator>( tabId, vals, descriptions, out );
570 
571  // now write to file
572  instance().writeerr<DefaultEocCalculator>(tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
573  }
574 
575  static void write(std::vector<double> &vals,
576  std::vector<std::string> &descriptions,
577  std::ostream& out,
578  std::string delimiter = " ",
579  std::string terminatingChar ="",
580  std::string header = "",
581  std::string tableSpacer = "",
582  std::string footer = "")
583  {
584  // print last line to out
585  instance().printerr<DefaultEocCalculator>( 0, vals, descriptions, out );
586 
587  // now write to file
588  instance().writeerr<DefaultEocCalculator>(0, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
589  }
590 
591 
605  template <class EocCalculatorType>
606  static void write(const int tabId,
607  std::vector<double> &vals,
608  std::vector<std::string> &descriptions,
609  std::ostream& out,
610  std::string delimiter = " ",
611  std::string terminatingChar ="",
612  std::string header = "",
613  std::string tableSpacer = "",
614  std::string footer = "")
615  {
616  // print last line to out
617  instance().template printerr<EocCalculatorType>( tabId, vals, descriptions, out );
618 
619  // now write to file
620  instance().template writeerr<EocCalculatorType>(tabId, vals, descriptions, delimiter, terminatingChar, header, tableSpacer, footer);
621  }
622 
623  template <class EocCalculatorType>
624  static void write(std::vector<double> &vals,
625  std::vector<std::string> &descriptions,
626  std::ostream& out,
627  std::string delimiter = " ",
628  std::string terminatingChar ="",
629  std::string header = "",
630  std::string tableSpacer = "",
631  std::string footer = "")
632  {
633  // print last line to out
634  instance().template printerr<EocCalculatorType>(0, vals, descriptions, out );
635 
636  // now write to file
637  instance().template writeerr<EocCalculatorType>(0, vals, descriptions,delimiter, terminatingChar, header, tableSpacer, footer);
638  }
639 
640  }; // class FemEocTable
641 
642  } // namespace Fem
643 
644 } // namespace Dune
645 
646 #endif // #ifndef DUNE_FEM_FEMEOCTABLE_HH
Write a self contained tex table for eoc runs with timing information.
Definition: femeoctable.hh:56
The Fem Eoc Table writer.
Definition: femeoctable.hh:70
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:558
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:404
static int initialize(const std::string &path, const std::string &name, const std::string &descript)
Definition: femeoctable.hh:319
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:456
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:606
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:519
static size_t addEntry(const int tabId, const StrVectorType &descript)
add a vector of new eoc values
Definition: femeoctable.hh:357
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:479
static int initialize(const std::string &name, const std::string &descript)
Definition: femeoctable.hh:324
static size_t addEntry(const int tabId, const char *descript)
add a single new eoc output
Definition: femeoctable.hh:385
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:425
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:442
static size_t addEntry(const int tabId, const std::string &descript)
add a single new eoc output
Definition: femeoctable.hh:371
static size_t addEntry(const int tabId, const StrVectorType &descript, size_t size)
add a vector of new eoc values
Definition: femeoctable.hh:339
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:456
return singleton instance of given Object type.
Definition: singleton.hh:93
static DUNE_EXPORT Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:123
vector space out of a tensor product of fields.
Definition: fvector.hh:95
Default exception class for I/O errors.
Definition: exceptions.hh:231
A few common exception classes.
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)