DUNE-FEM (unstable)

explicit.hh
1#ifndef DUNE_FEM_SOLVER_RUNGEKUTTA_EXPLICIT_HH
2#define DUNE_FEM_SOLVER_RUNGEKUTTA_EXPLICIT_HH
3
4//- system includes
5#include <iostream>
6#include <cmath>
7#include <vector>
8#include <cassert>
9
10//- Dune includes
11#include <dune/fem/io/parameter.hh>
12#include <dune/fem/operator/common/spaceoperatorif.hh>
13#include <dune/fem/solver/odesolverinterface.hh>
14#include <dune/fem/solver/timeprovider.hh>
15#include <dune/fem/solver/rungekutta/butchertable.hh>
16
17namespace DuneODE
18{
19
20 using namespace Dune;
21 using namespace Fem;
22 using namespace std;
23
62 template<class DestinationImp>
64 : public OdeSolverInterface<DestinationImp>
65 {
66 public:
67 typedef DestinationImp DestinationType;
69 typedef typename DestinationType :: DiscreteFunctionSpaceType SpaceType;
70
71 typedef typename OdeSolverInterface<DestinationImp> :: MonitorType MonitorType ;
72
73 using OdeSolverInterface<DestinationImp> :: solve ;
74 protected:
75 SimpleButcherTable< double > defaultButcherTables( const int order ) const
76 {
77 switch( order )
78 {
79 case 1: return explicitEulerButcherTable();
80 case 2: return tvd2ButcherTable();
81 case 3: return tvd3ButcherTable();
82 case 4: return rk4ButcherTable();
83 case 5:
84 case 6: return expl6ButcherTable();
85
86 default:
87 std::cerr<< "Warning: ExplicitRungeKutta of order "<< order << " not implemented, using order 6!" << std::endl;
88 return expl6ButcherTable();
89 }
90 }
91
92 public:
101 const SimpleButcherTable< double >& butcherTable,
102 bool verbose )
103 : A_( butcherTable.A() ),
104 b_( butcherTable.b() ),
105 c_( butcherTable.c() ),
106 Upd(),
107 op_(op),
108 tp_(tp),
109 ord_( butcherTable.order() ),
110 stages_( butcherTable.stages() ),
111 initialized_(false)
112 {
113 assert(ord_>0);
114
115 // create update memory
116 for (int i=0; i<stages_; ++i)
117 {
118 Upd.emplace_back( new DestinationType("URK",op_.space()) );
119 }
120 Upd.emplace_back(new DestinationType("Ustep",op_.space()) );
121
122 }
123
132 const int pord,
133 bool verbose )
134 : ExplicitRungeKuttaSolver( op, tp, defaultButcherTables( pord ), verbose )
135 {
136 }
137
138 ExplicitRungeKuttaSolver(OperatorType& op,
140 const int pord,
141 const Dune::Fem::ParameterReader &parameter = Dune::Fem::Parameter::container() )
142 : ExplicitRungeKuttaSolver( op, tp, pord, true )
143 {}
144
146 void initialize(const DestinationType& U0)
147 {
148 if( ! initialized_ )
149 {
150 // Compute Steps
151 op_(U0, *(Upd[0]));
152 initialized_ = true;
153
154 // provide operators time step estimate
156 }
157 }
158
160 void solve(DestinationType& U0, MonitorType& monitor )
161 {
162 // no information here
163 monitor.reset();
164
165 // initialize
166 if( ! initialized_ )
167 {
168 DUNE_THROW(InvalidStateException,"ExplicitRungeKuttaSolver wasn't initialized before first call!");
169 }
170
171 // get cfl * timeStepEstimate
172 const double dt = tp_.deltaT();
173 // get time
174 const double t = tp_.time();
175
176 // set new time
177 op_.setTime( t );
178
179 // Compute Steps
180 op_(U0, *(Upd[0]));
181
182 // provide operators time step estimate
184
185 for (int i=1; i<stages_; ++i)
186 {
187 (Upd[ord_])->assign(U0);
188 for (int j=0; j<i ; ++j)
189 {
190 (Upd[ord_])->axpy((A_[i][j]*dt), *(Upd[j]));
191 }
192
193 // set new time
194 op_.setTime( t + c_[i]*dt );
195
196 // apply operator
197 op_( *(Upd[ord_]), *(Upd[i]) );
198
199 // provide operators time step estimate
201 }
202
203 // Perform Update
204 for (int j=0; j<stages_; ++j)
205 {
206 U0.axpy((b_[j]*dt), *(Upd[j]));
207 }
208 }
209
210 void description(std::ostream& out) const
211 {
212 out << "ExplRungeKutta, steps: " << ord_
213 //<< ", cfl: " << this->tp_.factor()
214 << "\\\\" <<std::endl;
215 }
216
217 protected:
218 // Butcher table A,b,c
222
223 // stages of Runge-Kutta solver
224 std::vector< std::unique_ptr< DestinationType > > Upd;
225
226 // operator to solve for
227 OperatorType& op_;
228 // time provider
229 TimeProviderBase& tp_;
230
231 // order of RK solver
232 const int ord_;
233 // number of stages
234 const int stages_;
235 // init flag
236 bool initialized_;
237 };
238
241} // namespace DuneODE
242
243#endif // #ifndef DUNE_FEM_SOLVER_RUNGEKUTTA_EXPLICIT_HH
Exlicit RungeKutta ODE solver.
Definition: explicit.hh:65
ExplicitRungeKuttaSolver(OperatorType &op, TimeProviderBase &tp, const int pord, bool verbose)
constructor
Definition: explicit.hh:130
void description(std::ostream &out) const
print description of ODE solver to out stream
Definition: explicit.hh:210
ExplicitRungeKuttaSolver(OperatorType &op, TimeProviderBase &tp, const SimpleButcherTable< double > &butcherTable, bool verbose)
constructor
Definition: explicit.hh:99
void solve(DestinationType &U0, MonitorType &monitor)
solve the system
Definition: explicit.hh:160
void initialize(const DestinationType &U0)
apply operator once to get dt estimate
Definition: explicit.hh:146
Interface class for ODE Solver.
Definition: odesolverinterface.hh:21
Monitor MonitorType
monitor type
Definition: odesolverinterface.hh:59
virtual void setTime(const double time)
set time for operators
Definition: spaceoperatorif.hh:76
virtual const DiscreteFunctionSpaceType & space() const =0
return reference to space (needed by ode solvers)
virtual double timeStepEstimate() const
estimate maximum time step
Definition: spaceoperatorif.hh:86
general base for time providers
Definition: timeprovider.hh:36
double time() const
obtain the current time
Definition: timeprovider.hh:94
void provideTimeStepEstimate(const double dtEstimate)
set time step estimate to minimum of given value and internal time step estiamte
Definition: timeprovider.hh:142
double deltaT() const
obtain the size of the current time step
Definition: timeprovider.hh:113
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)