DOpE
reducedalgorithm.h
Go to the documentation of this file.
1 
24 #ifndef REDUCED_ALGORITHM_H_
25 #define REDUCED_ALGORITHM_H_
26 
27 #include "optproblemcontainer.h"
29 #include "dopeexceptionhandler.h"
30 #include "outputhandler.h"
31 #include "controlvector.h"
32 
33 #include <deal.II/lac/vector.h>
34 
35 #include <iostream>
36 #include <assert.h>
37 #include <iomanip>
38 
39 namespace DOpE
40 {
41 
53  template<typename PROBLEM, typename VECTOR>
55  {
56  public:
70  ReducedAlgorithm(PROBLEM* OP,
72  ParameterReader &param_reader,
73  DOpEExceptionHandler<VECTOR>* Except = NULL,
74  DOpEOutputHandler<VECTOR>* Output = NULL, int base_priority = 0);
75  virtual ~ReducedAlgorithm();
76 
82  static void
83  declare_params(ParameterReader &param_reader);
84 
89  virtual void
91  {
92  this->GetReducedProblem()->ReInit();
93  if (rem_output_)
94  {
95  this->GetOutputHandler()->ReInit();
96  }
97  }
98 
107  virtual int
108  Solve(ControlVector<VECTOR>& q, double global_tol = -1.)=0;
109 
116  virtual void
118 
132  virtual void
133  CheckGrads(double c, ControlVector<VECTOR>& q,
134  ControlVector<VECTOR>& dq, unsigned int niter = 1, double eps = 1.);
143  virtual void
144  FirstDifferenceQuotient(double exact, double eps,
145  const ControlVector<VECTOR>& q, const ControlVector<VECTOR>& dq);
157  virtual void
158  CheckHessian(double c, ControlVector<VECTOR>& q,
159  ControlVector<VECTOR>& dq, unsigned int niter = 1, double eps = 1.);
168  virtual void
169  SecondDifferenceQuotient(double exact, double eps,
170  const ControlVector<VECTOR>& q, const ControlVector<VECTOR>& dq);
171 
174  {
175  return ExceptionHandler_;
176  }
179  {
180  return OutputHandler_;
181  }
182  protected:
186  PROBLEM*
188  {
189  return OP_;
190  }
194  const PROBLEM*
195  GetProblem() const
196  {
197  return OP_;
198  }
204  {
205  return Solver_;
206  }
212  {
213  return Solver_;
214  }
215 
216  int
218  {
219  return base_priority_;
220  }
221 
222  private:
223  PROBLEM* OP_;
225  DOpEExceptionHandler<VECTOR>* ExceptionHandler_;
226  DOpEOutputHandler<VECTOR>* OutputHandler_;
227  bool rem_exception_;
228  bool rem_output_;
229  int base_priority_;
230  };
231 
232  /***************************************************************************************/
233  /****************************************IMPLEMENTATION*********************************/
234  /***************************************************************************************/
235  using namespace dealii;
236 
237  /******************************************************/
238 
239  template<typename PROBLEM, typename VECTOR>
240  void
242  ParameterReader &param_reader)
243  {
245  }
246 
247  /******************************************************/
248 
249  template<typename PROBLEM, typename VECTOR>
252  ParameterReader &param_reader, DOpEExceptionHandler<VECTOR>* Except,
253  DOpEOutputHandler<VECTOR>* Output, int base_priority)
254  {
255  assert(OP);
256  assert(S);
257 
258  OP_ = OP;
259  Solver_ = S;
260  if (Output == NULL)
261  {
262  OutputHandler_ = new DOpEOutputHandler<VECTOR>(S, param_reader);
263  rem_output_ = true;
264  }
265  else
266  {
267  OutputHandler_ = Output;
268  rem_output_ = false;
269  }
270  if (Except == NULL)
271  {
272  ExceptionHandler_ = new DOpEExceptionHandler<VECTOR>(OutputHandler_);
273  rem_exception_ = true;
274  }
275  else
276  {
277  ExceptionHandler_ = Except;
278  rem_exception_ = false;
279  }
280  OP_->RegisterOutputHandler(OutputHandler_);
281  OP_->RegisterExceptionHandler(ExceptionHandler_);
282  Solver_->RegisterOutputHandler(OutputHandler_);
283  Solver_->RegisterExceptionHandler(ExceptionHandler_);
284 
285  base_priority_ = base_priority;
286  }
287 
288  /******************************************************/
289 
290  template<typename PROBLEM, typename VECTOR>
292  {
293  if (ExceptionHandler_ && rem_exception_)
294  {
295  delete ExceptionHandler_;
296  }
297  if (OutputHandler_ && rem_output_)
298  {
299  delete OutputHandler_;
300  }
301  }
302  /******************************************************/
303 
304  template<typename PROBLEM, typename VECTOR>
305  void
308  {
309  q.ReInit();
310 
311  //Solve j'(q) = 0
312  double cost = 0.;
313  std::stringstream out;
314 
315  out << "**************************************************\n";
316  out << "* Starting Forward Solver *\n";
317  out << "* Solving : " << this->GetProblem()->GetName() << "\t*\n";
318  out << "* CDoFs : ";
319  q.PrintInfos(out);
320  out << "* SDoFs : ";
321  this->GetReducedProblem()->StateSizeInfo(out);
322  out << "**************************************************";
323  this->GetOutputHandler()->Write(out, 1 + this->GetBasePriority(), 1, 1);
324 
325  try
326  {
327  cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
328  }
329  catch (DOpEException& e)
330  {
331  this->GetExceptionHandler()->HandleCriticalException(e);
332  }
333 
334  out << "CostFunctional: " << cost;
335  this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
336 
337  try
338  {
339  this->GetReducedProblem()->ComputeReducedFunctionals(q);
340  }
341  catch (DOpEException& e)
342  {
343  this->GetExceptionHandler()->HandleCriticalException(e);
344  }
345 
346  }
347  /******************************************************/
348 
349  template<typename PROBLEM, typename VECTOR>
350  void
352  ControlVector<VECTOR>& q, ControlVector<VECTOR>& dq, unsigned int niter,
353  double eps)
354  {
355  q.ReInit();
356  dq.ReInit();
357 
358  dq = c;
359 
360  ControlVector<VECTOR> point(q);
361  point = q;
362  std::stringstream out;
363 
364  ControlVector<VECTOR> gradient(q), gradient_transposed(q);
365 
366  this->GetReducedProblem()->ComputeReducedCostFunctional(point);
367  this->GetReducedProblem()->ComputeReducedGradient(point, gradient,
368  gradient_transposed);
369  double cost_diff = gradient * dq;
370  out << "Checking Gradients...." << std::endl;
371  out << " Epsilon \t Exact \t Diff.Quot. \t Rel. Error ";
372  this->GetOutputHandler()->Write(out, 3 + this->GetBasePriority());
373 
374  for (unsigned int i = 0; i < niter; i++)
375  {
376  FirstDifferenceQuotient(cost_diff, eps, q, dq);
377  eps /= 10.;
378  }
379  }
380  /******************************************************/
381 
382  template<typename PROBLEM, typename VECTOR>
383  void
385  double exact, double eps, const ControlVector<VECTOR>& q,
386  const ControlVector<VECTOR>& dq)
387  {
388  ControlVector<VECTOR> point(q);
389  point = q;
390 
391  std::stringstream out;
392 
393  point.add(eps, dq);
394 
395  double cost_right = 0.;
396  //Differenzenquotient
397  cost_right = this->GetReducedProblem()->ComputeReducedCostFunctional(
398  point);
399 
400  point.add(-2. * eps, dq);
401 
402  double cost_left = 0.;
403  //Differenzenquotient
404  cost_left = this->GetReducedProblem()->ComputeReducedCostFunctional(
405  point);
406 
407  double diffquot = (cost_right - cost_left) / (2. * eps);
408  out << eps << "\t" << exact << "\t" << diffquot << "\t"
409  << (exact - diffquot) / exact << std::endl;
410  this->GetOutputHandler()->Write(out, 3 + this->GetBasePriority());
411  }
412 
413  /******************************************************/
414 
415  template<typename PROBLEM, typename VECTOR>
416  void
418  ControlVector<VECTOR>& q, ControlVector<VECTOR>& dq, unsigned int niter,
419  double eps)
420  {
421  q.ReInit();
422  dq.ReInit();
423 
424  dq = c;
425 
426  ControlVector<VECTOR> point(q);
427  point = q;
428  std::stringstream out;
429 
430  ControlVector<VECTOR> gradient(q), gradient_transposed(q), hessian(q),
431  hessian_transposed(q);
432 
433  this->GetReducedProblem()->ComputeReducedCostFunctional(point);
434  this->GetReducedProblem()->ComputeReducedGradient(point, gradient,
435  gradient_transposed);
436 
437  this->GetReducedProblem()->ComputeReducedHessianVector(point, dq, hessian,
438  hessian_transposed);
439 
440  double cost_diff = hessian * dq;
441  out << "Checking Hessian...." << std::endl;
442  out << " Epsilon \t Exact \t Diff.Quot. \t Rel. Error ";
443  this->GetOutputHandler()->Write(out, 3 + this->GetBasePriority());
444 
445  for (unsigned int i = 0; i < niter; i++)
446  {
447  SecondDifferenceQuotient(cost_diff, eps, q, dq);
448  eps /= 10.;
449  }
450  }
451  /******************************************************/
452 
453  template<typename PROBLEM, typename VECTOR>
454  void
456  double exact, double eps, const ControlVector<VECTOR>& q,
457  const ControlVector<VECTOR>& dq)
458  {
459  ControlVector<VECTOR> point(q);
460  point = q;
461  std::stringstream out;
462 
463  double cost_mid = 0.;
464  //Differenzenquotient
465  cost_mid = this->GetReducedProblem()->ComputeReducedCostFunctional(point);
466 
467  point.add(eps, dq);
468 
469  double cost_right = 0.;
470  //Differenzenquotient
471  cost_right = this->GetReducedProblem()->ComputeReducedCostFunctional(
472  point);
473 
474  point.add(-2. * eps, dq);
475 
476  double cost_left = 0.;
477  //Differenzenquotient
478  cost_left = this->GetReducedProblem()->ComputeReducedCostFunctional(
479  point);
480 
481  double diffquot = (cost_left - 2. * cost_mid + cost_right) / (eps * eps);
482 
483  out << eps << "\t" << exact << "\t" << diffquot << "\t"
484  << (exact - diffquot) / exact << std::endl;
485  this->GetOutputHandler()->Write(out, 3 + this->GetBasePriority());
486  }
487 
488 /******************************************************/
489 
490 }
491 #endif
void PrintInfos(std::stringstream &out)
Definition: controlvector.cc:896
virtual int Solve(ControlVector< VECTOR > &q, double global_tol=-1.)=0
DOpEExceptionHandler< VECTOR > * GetExceptionHandler()
Definition: reducedalgorithm.h:173
int GetBasePriority() const
Definition: reducedalgorithm.h:217
static void declare_params(ParameterReader &param_reader)
Definition: outputhandler.cc:40
virtual void CheckGrads(double c, ControlVector< VECTOR > &q, ControlVector< VECTOR > &dq, unsigned int niter=1, double eps=1.)
Definition: reducedalgorithm.h:351
Definition: parameterreader.h:36
Definition: optproblemcontainer.h:70
Definition: controlvector.h:49
PROBLEM * GetProblem()
Definition: reducedalgorithm.h:187
virtual ~ReducedAlgorithm()
Definition: reducedalgorithm.h:291
Definition: reducedalgorithm.h:54
void add(double s, const ControlVector &dq)
Definition: controlvector.cc:629
void ReInit()
Definition: controlvector.cc:96
virtual void SolveForward(ControlVector< VECTOR > &q)
Definition: reducedalgorithm.h:306
virtual void FirstDifferenceQuotient(double exact, double eps, const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &dq)
Definition: reducedalgorithm.h:384
virtual void SecondDifferenceQuotient(double exact, double eps, const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &dq)
Definition: reducedalgorithm.h:455
static void declare_params(ParameterReader &param_reader)
Definition: reducedalgorithm.h:241
DOpEOutputHandler< VECTOR > * GetOutputHandler()
Definition: reducedalgorithm.h:178
Definition: reducedprobleminterface.h:335
ReducedProblemInterface< PROBLEM, VECTOR > * GetReducedProblem()
Definition: reducedalgorithm.h:211
const PROBLEM * GetProblem() const
Definition: reducedalgorithm.h:195
ReducedAlgorithm(PROBLEM *OP, ReducedProblemInterface< PROBLEM, VECTOR > *S, ParameterReader &param_reader, DOpEExceptionHandler< VECTOR > *Except=NULL, DOpEOutputHandler< VECTOR > *Output=NULL, int base_priority=0)
Definition: reducedalgorithm.h:250
const ReducedProblemInterface< PROBLEM, VECTOR > * GetReducedProblem() const
Definition: reducedalgorithm.h:203
Definition: dopeexception.h:35
virtual void CheckHessian(double c, ControlVector< VECTOR > &q, ControlVector< VECTOR > &dq, unsigned int niter=1, double eps=1.)
Definition: reducedalgorithm.h:417
Definition: optproblemcontainer.h:72
virtual void ReInit()
Definition: reducedalgorithm.h:90