24 #ifndef REDUCED_ALGORITHM_H_
25 #define REDUCED_ALGORITHM_H_
33 #include <deal.II/lac/vector.h>
53 template<
typename PROBLEM,
typename VECTOR>
175 return ExceptionHandler_;
180 return OutputHandler_;
219 return base_priority_;
235 using namespace dealii;
239 template<
typename PROBLEM,
typename VECTOR>
249 template<
typename PROBLEM,
typename VECTOR>
267 OutputHandler_ = Output;
273 rem_exception_ =
true;
277 ExceptionHandler_ = Except;
278 rem_exception_ =
false;
280 OP_->RegisterOutputHandler(OutputHandler_);
281 OP_->RegisterExceptionHandler(ExceptionHandler_);
282 Solver_->RegisterOutputHandler(OutputHandler_);
283 Solver_->RegisterExceptionHandler(ExceptionHandler_);
285 base_priority_ = base_priority;
290 template<
typename PROBLEM,
typename VECTOR>
293 if (ExceptionHandler_ && rem_exception_)
295 delete ExceptionHandler_;
297 if (OutputHandler_ && rem_output_)
299 delete OutputHandler_;
304 template<
typename PROBLEM,
typename VECTOR>
313 std::stringstream out;
315 out <<
"**************************************************\n";
316 out <<
"* Starting Forward Solver *\n";
317 out <<
"* Solving : " << this->GetProblem()->GetName() <<
"\t*\n";
321 this->GetReducedProblem()->StateSizeInfo(out);
322 out <<
"**************************************************";
323 this->GetOutputHandler()->Write(out, 1 + this->GetBasePriority(), 1, 1);
327 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
331 this->GetExceptionHandler()->HandleCriticalException(e);
334 out <<
"CostFunctional: " << cost;
335 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
339 this->GetReducedProblem()->ComputeReducedFunctionals(q);
343 this->GetExceptionHandler()->HandleCriticalException(e);
349 template<
typename PROBLEM,
typename VECTOR>
362 std::stringstream out;
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());
374 for (
unsigned int i = 0; i < niter; i++)
376 FirstDifferenceQuotient(cost_diff, eps, q, dq);
382 template<
typename PROBLEM,
typename VECTOR>
391 std::stringstream out;
395 double cost_right = 0.;
397 cost_right = this->GetReducedProblem()->ComputeReducedCostFunctional(
400 point.
add(-2. * eps, dq);
402 double cost_left = 0.;
404 cost_left = this->GetReducedProblem()->ComputeReducedCostFunctional(
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());
415 template<
typename PROBLEM,
typename VECTOR>
428 std::stringstream out;
431 hessian_transposed(q);
433 this->GetReducedProblem()->ComputeReducedCostFunctional(point);
434 this->GetReducedProblem()->ComputeReducedGradient(point, gradient,
435 gradient_transposed);
437 this->GetReducedProblem()->ComputeReducedHessianVector(point, dq, hessian,
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());
445 for (
unsigned int i = 0; i < niter; i++)
447 SecondDifferenceQuotient(cost_diff, eps, q, dq);
453 template<
typename PROBLEM,
typename VECTOR>
461 std::stringstream out;
463 double cost_mid = 0.;
465 cost_mid = this->GetReducedProblem()->ComputeReducedCostFunctional(point);
469 double cost_right = 0.;
471 cost_right = this->GetReducedProblem()->ComputeReducedCostFunctional(
474 point.
add(-2. * eps, dq);
476 double cost_left = 0.;
478 cost_left = this->GetReducedProblem()->ComputeReducedCostFunctional(
481 double diffquot = (cost_left - 2. * cost_mid + cost_right) / (eps * eps);
483 out << eps <<
"\t" << exact <<
"\t" << diffquot <<
"\t"
484 << (exact - diffquot) / exact << std::endl;
485 this->GetOutputHandler()->Write(out, 3 + this->GetBasePriority());
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 ¶m_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 ¶m_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 ¶m_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