DUNE-FEM (unstable)

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