KASKADE 7 development version
algorithm_base.hh
Go to the documentation of this file.
1#ifndef ALGORITHM_BASE_HH
2#define ALGORITHM_BASE_HH
3
4#include <vector>
5#include <string>
6#include <iostream>
7
8namespace LQAction
9{
10
12
13typedef enum toDo ToDo;
14}
15
16namespace Kaskade
17{
21
23
28template<typename T>
30{
31public:
32 void doAction(LQAction::ToDo td, std::string const& name_="noName")
33 {
34 switch(td)
35 {
36 case LQAction::LogValue : logValue(); break;
37 case LQAction::Reset : reset(name_); break;
38 case LQAction::Devalidate : devalidate(); break;
39 default : assert(!"strange error in doAction");
40 }
41 }
42
44 void reset(std::string const& name_)
45 {
46 logged=false;
47 valid=false;
48 name=name_;
49 logger.resize(0);
50 zero=0.0;
51 }
52
53
54 LoggedQuantity(std::string const& name_) { reset(name_); }
55 LoggedQuantity() { reset("noName");}
56
58 void logValue() {
59 if(valid && !logged) logger.push_back(currentValue);
60 if(!valid) logger.resize(logger.size()+1);
61 logged=true;
62 }
63
65 void logValue(int i) { if(valid) logger[i]=currentValue; logged=true; }
66
68 LoggedQuantity<T>& operator=(T const& rvalue) { currentValue=rvalue; logged=false; valid=true; return *this; }
69
71 int size() {int sz= logger.size(); if(valid && !logged) sz++; return sz;}
72
74 bool isValid() {return valid;}
75
77 void devalidate() {valid=false;}
78
80 bool isLogged() {return logged;}
81
83 T const& operator[](int i)
84 {
85 if(i < logger.size() && i >= 0) return logger[i];
86 if(i==logger.size() && valid && !logged) return currentValue;
87 namedAssertion(false,"Index failure!");
88 return currentValue;
89 }
90
92 operator T&() { namedAssertion(valid,"TypeCast: operator T&()"); return currentValue;}
93
96
97 T& value_nonconst() { valid = true; namedAssertion(valid,"TypeCast: value_nonconst()"); return currentValue;}
98
99 T const& value() const { namedAssertion(valid,"TypeCast: value()"); return currentValue;}
100
102 void print(std::ostream& s)
103 {
104 s.setf(std::ios::scientific,std::ios::floatfield);
105 s.precision(16);
106 for(int i=0; i!=size(); i++)
107 s << (*this)[i] << std::endl;
108 }
109
110private:
111 std::vector<T> logger;
112 T currentValue;
113 bool logged;
114 bool valid;
115 std::string name;
116 void namedAssertion(bool val,std::string caller) const
117 {
118 if(!val)
119 {
120 std::cout << name << " from " << caller << ":" << std::endl;
121 assert(0);
122 }
123 }
124 double zero;
125};
126
127//--------------------------------------------------
130{
131public:
132 IterationParameters(double desiredAccuracy_, int maxSteps_)
133 : desiredAccuracy(desiredAccuracy_),
134 maxSteps(maxSteps_)
135 {
136 }
137
138//Parameters with values that must be supplied by client
142
144
146 void logStep() {
148 }
149
151 virtual void reset() {
153 }
154
155protected:
156
158
160 virtual void doForAll(LQAction::ToDo td)
161 {
162 }
163
164
165 friend class NewtonsMethod;
166};
167
168
169//-------------------------------------------------------------------------------------------
170
172
175{
176public:
177
178 Algorithm() : report(0), measureTime(false) {}
179 virtual ~Algorithm(){}
180
181 void performTiming(bool doit) {measureTime=doit; }
182 void reportOnIteration(int level) {report=level; }
183
184protected:
185 virtual void initialize(){};
186 virtual void finalize(int){};
187 virtual void terminationMessage(int);
192
194
195private:
196 virtual int runAlgorithm()=0;
197
198 bool measureTime;
199};
200
201} // namespace Kaskade
202
203#endif
Abstract Vector for function space algorithms.
Base class for algorithms. Provides a unified interface and some simple wrapper routines,...
void reportOnIteration(int level)
virtual void finalize(int)
virtual void terminationMessage(int)
int algorithmWrapper()
Run algorithm, completely with initialization and finalization.
int oneStepWrapper()
Run one step of algorithm.
virtual void initialize()
void performTiming(bool doit)
Base class for algorithmic parameters.
void logStep()
Log all quantities in this class.
virtual void reset()
Reset all quantities in this class.
virtual void doForAll(LQAction::ToDo td)
To be overloaded by derived class.
IterationParameters(double desiredAccuracy_, int maxSteps_)
Class that represents a quantity that can be logged during the course of an algortihm.
bool isLogged()
returns true, iff current value is already logged
T const & value() const
void reset(std::string const &name_)
Delete any information contained in this class, on exit: state like freshly constructed.
T const & operator[](int i)
Return value from log buffer (used seldomly)
LoggedQuantity< T > & operator=(T const &rvalue)
Set current value.
int size()
Get size of log buffer.
void doAction(LQAction::ToDo td, std::string const &name_="noName")
void print(std::ostream &s)
print log-buffer into a stream, to be used for analysis of an algorithm
bool isValid()
Returns true, iff there is a valid current value.
void logValue()
Insert current value into log buffer.
void devalidate()
devalidate current value
LoggedQuantity(std::string const &name_)
void logValue(int i)
Insert current value into log buffer at a certain number i (used seldomly)
Base class for Newton's method. Defines the main algorithm, potentially using damping.
Definition: newton_base.hh:89
enum toDo ToDo