DOpE
instatreducedproblem.h
Go to the documentation of this file.
1 
24 #ifndef INSTAT_REDUCED_PROBLEM_H_
25 #define INSTAT_REDUCED_PROBLEM_H_
26 
28 #include "integrator.h"
29 #include "parameterreader.h"
30 #include "statevector.h"
31 #include "solutionextractor.h"
32 #include "pdeinterface.h"
33 #include "functionalinterface.h"
34 #include "dirichletdatainterface.h"
35 #include "dopeexception.h"
38 #include "newtonsolvermixeddims.h"
39 //#include "integratormixeddims.h"
40 #include "cglinearsolver.h"
41 #include "gmreslinearsolver.h"
42 #include "directlinearsolver.h"
43 #include "voidlinearsolver.h"
44 #include "constraintinterface.h"
45 #include "helper.h"
46 #include "dwrdatacontainer.h"
47 
48 #include <deal.II/base/data_out_base.h>
49 #include <deal.II/numerics/data_out.h>
50 #include <deal.II/numerics/matrix_tools.h>
51 #include <deal.II/numerics/vector_tools.h>
52 #include <deal.II/base/function.h>
53 #include <deal.II/lac/sparse_matrix.h>
54 #include <deal.II/lac/compressed_simple_sparsity_pattern.h>
55 #include <deal.II/lac/sparse_direct.h>
56 #include <deal.II/lac/block_sparsity_pattern.h>
57 #include <deal.II/lac/block_sparse_matrix.h>
58 #include <deal.II/lac/vector.h>
59 
60 #include <fstream>
61 
62 namespace DOpE
63 {
79 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
80  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim, int dealdim>
81 class InstatReducedProblem: public ReducedProblemInterface<PROBLEM, VECTOR>
82 {
83  public:
96  template<typename INTEGRATORDATACONT>
97  InstatReducedProblem(PROBLEM *OP, DOpEtypes::VectorStorageType state_behavior,
98  ParameterReader &param_reader,
99  INTEGRATORDATACONT& idc,
100  int base_priority = 0);
101 
102 
116  template<typename STATEINTEGRATORDATACONT, typename CONTROLINTEGRATORCONT>
117  InstatReducedProblem(PROBLEM *OP, DOpEtypes::VectorStorageType state_behavior,
118  ParameterReader &param_reader, CONTROLINTEGRATORCONT& c_idc,
119  STATEINTEGRATORDATACONT & s_idc, int base_priority = 0);
120 
121  virtual ~InstatReducedProblem();
122 
123  /******************************************************/
124 
130  static void declare_params(ParameterReader &param_reader);
131 
132  /******************************************************/
133 
139  void ReInit();
140 
141  /******************************************************/
142 
149 
150  /******************************************************/
151 
158 
159 
160  /******************************************************/
161 
168  ControlVector<VECTOR>& gradient_transposed);
169 
170  /******************************************************/
171 
178 
179  /******************************************************/
180 
187 
188  /******************************************************/
189 
196  ControlVector<VECTOR>& hessian_direction,
197  ControlVector<VECTOR>& hessian_direction_transposed);
198 
199  /******************************************************/
200 
220  template<class DWRC>
221  void
223  {
224  throw DOpEException("ExcNotImplemented",
225  "InstatReducedProblem::ComputeRefinementIndicators");
226  }
227 
228  /******************************************************/
229 
235  void StateSizeInfo(std::stringstream& out)
236  {
237  GetU().PrintInfos(out);
238  }
239 
240  /******************************************************/
241 
252  void WriteToFile(const VECTOR &v, std::string name, std::string outfile,
253  std::string dof_type, std::string filetype);
254 
255  /******************************************************/
256 
265  void WriteToFile(const ControlVector<VECTOR> &v, std::string name, std::string dof_type);
266 
267  /******************************************************/
268 
276  void WriteToFile(const std::vector<double> &v, std::string outfile);
277 
278  protected:
279  const StateVector<VECTOR> & GetU() const
280  {
281  return u_;
282  }
284  {
285  return u_;
286  }
288  {
289  return z_;
290  }
292  {
293  return du_;
294  }
296  {
297  return dz_;
298  }
299 
300  NONLINEARSOLVER& GetNonlinearSolver(std::string type);
301  CONTROLNONLINEARSOLVER& GetControlNonlinearSolver();
302  INTEGRATOR& GetIntegrator()
303  {
304  return integrator_;
305  }
306  CONTROLINTEGRATOR& GetControlIntegrator()
307  {
308  return control_integrator_;
309  }
310 
311  /******************************************************/
312 
321  void ComputeTimeFunctionals(unsigned int step, unsigned int num_steps);
331 
332  /******************************************************/
333 
346 
347  /******************************************************/
348 
360  template<typename PDE>
361  void ForwardTimeLoop(PDE& problem, StateVector<VECTOR>& sol, std::string outname, bool eval_funcs);
362 
363  /******************************************************/
364 
376  template<typename PDE>
377  void BackwardTimeLoop(PDE& problem, StateVector<VECTOR>& sol, ControlVector<VECTOR>& temp_q, ControlVector<VECTOR>& temp_q_trans, std::string outname, bool eval_grads);
378 
379  private:
380 
385 
386  INTEGRATOR integrator_;
387  CONTROLINTEGRATOR control_integrator_;
388  NONLINEARSOLVER nonlinear_state_solver_;
389  NONLINEARSOLVER nonlinear_adjoint_solver_;
390  CONTROLNONLINEARSOLVER nonlinear_gradient_solver_;
391 
392  bool build_state_matrix_, build_adjoint_matrix_, build_control_matrix_;
393  bool state_reinit_, adjoint_reinit_, gradient_reinit_;
394 
395  bool project_initial_data_;
396 
397  friend class SolutionExtractor<InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
398  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR,dopedim, dealdim>, VECTOR > ;
399 };
400 
401 /*************************************************************************/
402 /*****************************IMPLEMENTATION******************************/
403 /*************************************************************************/
404 using namespace dealii;
405 
406 /******************************************************/
407 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
408  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
409  int dealdim>
410 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
411  PROBLEM, VECTOR, dopedim, dealdim>::declare_params(
412  ParameterReader &param_reader)
413 {
414  NONLINEARSOLVER::declare_params(param_reader);
415 }
416 /******************************************************/
417 
418  template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER,
419  typename CONTROLINTEGRATOR, typename INTEGRATOR, typename PROBLEM,
420  typename VECTOR, int dopedim, int dealdim>
421  template<typename INTEGRATORDATACONT>
422  InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
423  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::InstatReducedProblem(
424  PROBLEM *OP,
425  DOpEtypes::VectorStorageType state_behavior,
426  ParameterReader &param_reader,
427  INTEGRATORDATACONT& idc,
428  int base_priority) :
429  ReducedProblemInterface<PROBLEM, VECTOR> (OP,
430  base_priority),
431  u_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
432  z_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
433  du_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
434  dz_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
435  integrator_(idc),
436  control_integrator_(idc),
437  nonlinear_state_solver_(integrator_, param_reader),
438  nonlinear_adjoint_solver_(integrator_, param_reader),
439  nonlinear_gradient_solver_(control_integrator_, param_reader)
440  {
441  //Solvers should be ReInited
442  {
443  state_reinit_ = true;
444  adjoint_reinit_ = true;
445  gradient_reinit_ = true;
446  }
447  }
448 
449 /******************************************************/
450  template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER,
451  typename CONTROLINTEGRATOR, typename INTEGRATOR, typename PROBLEM,
452  typename VECTOR, int dopedim, int dealdim>
453  template<typename STATEINTEGRATORDATACONT, typename CONTROLINTEGRATORCONT>
454  InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
455  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::InstatReducedProblem(
456  PROBLEM *OP, DOpEtypes::VectorStorageType state_behavior,
457  ParameterReader &param_reader,
458  CONTROLINTEGRATORCONT& c_idc,
459  STATEINTEGRATORDATACONT & s_idc,
460  int base_priority) :
461  ReducedProblemInterface<PROBLEM, VECTOR> (OP,
462  base_priority),
463  u_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
464  z_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
465  du_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
466  dz_(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
467  integrator_(s_idc),
468  control_integrator_(c_idc),
469  nonlinear_state_solver_(integrator_, param_reader),
470  nonlinear_adjoint_solver_(integrator_, param_reader),
471  nonlinear_gradient_solver_(control_integrator_, param_reader)
472  {
473  //Solvers should be ReInited
474  {
475  state_reinit_ = true;
476  adjoint_reinit_ = true;
477  gradient_reinit_ = true;
478  }
479  }
480 
481 /******************************************************/
482 
483 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
484  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
485  int dealdim>
486 InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
487  PROBLEM, VECTOR, dopedim, dealdim>::~InstatReducedProblem()
488 {
489 }
490 
491 /******************************************************/
492 
493 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
494  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
495  int dealdim>
496 NONLINEARSOLVER& InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR,
497  INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetNonlinearSolver(std::string type)
498 {
499  if ((type == "state") || (type == "tangent"))
500  {
501  return nonlinear_state_solver_;
502  }
503  else if ((type == "adjoint") || (type == "adjoint_hessian"))
504  {
505  return nonlinear_adjoint_solver_;
506  }
507  else
508  {
509  throw DOpEException("No Solver for Problem type:`" + type + "' found",
510  "InstatReducedProblem::GetNonlinearSolver");
511 
512  }
513 }
514 /******************************************************/
515 
516 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
517  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
518  int dealdim>
519 CONTROLNONLINEARSOLVER& InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
520  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetControlNonlinearSolver()
521 {
522  if ((this->GetProblem()->GetType() == "gradient") || (this->GetProblem()->GetType() == "hessian"))
523  {
524  return nonlinear_gradient_solver_;
525  }
526  else
527  {
528  throw DOpEException("No Solver for Problem type:`" + this->GetProblem()->GetType() + "' found",
529  "InstatReducedProblem::GetControlNonlinearSolver");
530 
531  }
532 }
533 /******************************************************/
534 
535 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
536  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
537  int dealdim>
538 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
539  PROBLEM, VECTOR, dopedim, dealdim>::ReInit()
540 {
542 
543  //Some Solvers must be reinited when called
544  // Better have subproblems, so that solver can be reinited here
545  {
546  state_reinit_ = true;
547  adjoint_reinit_ = true;
548  gradient_reinit_ = true;
549  }
550 
551  build_state_matrix_ = true;
552  build_adjoint_matrix_ = true;
553 
554  GetU().ReInit();
555  GetZ().ReInit();
556  GetDU().ReInit();
557  GetDZ().ReInit();
558 
559  build_control_matrix_ = true;
560 }
561 
562 /******************************************************/
563 
564 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
565  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
566  int dealdim>
567 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
568  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedState(const ControlVector<VECTOR>& q)
569 {
570  this->InitializeFunctionalValues(this->GetProblem()->GetNFunctionals() + 1);
571 
572  this->GetOutputHandler()->Write("Computing State Solution:", 4 + this->GetBasePriority());
573 
574  this->SetProblemType("state");
575  auto& problem = this->GetProblem()->GetStateProblem();
576 
577  if (state_reinit_ == true)
578  {
579  GetNonlinearSolver("state").ReInit(problem);
580  state_reinit_ = false;
581  }
582 
583  this->GetProblem()->AddAuxiliaryControl(&q,"control");
584  this->ForwardTimeLoop(problem,this->GetU(),"State",true);
585  this->GetProblem()->DeleteAuxiliaryControl("control");
586 }
587 /******************************************************/
588 
589 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
590  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
591  int dealdim>
592 bool InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
593  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedConstraints(
594  const ControlVector<VECTOR>& /*q*/,
596 {
597  abort();
598 }
599 
600 /******************************************************/
601 
602 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
603  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
604  int dealdim>
605 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
606  PROBLEM, VECTOR, dopedim, dealdim>::GetControlBoxConstraints(ControlVector<VECTOR>& /*lb*/, ControlVector<VECTOR>& /*ub*/)
607 {
608  abort();
609 }
610 
611 /******************************************************/
612 
613 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
614  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
615  int dealdim>
616 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
617  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedAdjoint(
618  const ControlVector<VECTOR>& q, ControlVector<VECTOR>& temp_q, ControlVector<VECTOR>& temp_q_trans)
619 {
620  this->GetOutputHandler()->Write("Computing Adjoint Solution:", 4 + this->GetBasePriority());
621 
622  this->SetProblemType("adjoint");
623  auto& problem = this->GetProblem()->GetAdjointProblem();
624  if (adjoint_reinit_ == true)
625  {
626  GetNonlinearSolver("adjoint").ReInit(problem);
627  adjoint_reinit_ = false;
628  }
629 
630  this->GetProblem()->AddAuxiliaryState(&(this->GetU()),"state");
631  this->GetProblem()->AddAuxiliaryControl(&q,"control");
632  this->BackwardTimeLoop(problem,this->GetZ(),temp_q,temp_q_trans,"Adjoint",true);
633  this->GetProblem()->DeleteAuxiliaryControl("control");
634  this->GetProblem()->DeleteAuxiliaryState("state");
635 }
636 
637 /******************************************************/
638 
639 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
640  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
641  int dealdim>
642 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
643  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedGradient(
644  const ControlVector<VECTOR>& q,
645  ControlVector<VECTOR>& gradient,
646  ControlVector<VECTOR>& gradient_transposed)
647 {
648  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() != DOpEtypes::ControlType::initial)
649  {
650  gradient = 0.;
651  }
652  this->ComputeReducedAdjoint(q,gradient,gradient_transposed);
653 
654  this->GetOutputHandler()->Write("Computing Reduced Gradient:",
655  4 + this->GetBasePriority());
656  if (this->GetProblem()->HasControlInDirichletData())
657  {
658  throw DOpEException("Control in Dirichlet Data for instationary problems not yet implemented!"
659  ,"InstatReducedProblem::ComputeReducedGradient");
660  }
661 
662  this->SetProblemType("gradient");
663  if (gradient_reinit_ == true)
664  {
665  GetControlNonlinearSolver().ReInit(*(this->GetProblem()));
666  state_reinit_ = false;
667  }
668 
669  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::initial)
670  {
671 
672  //Set time to initial
673  const std::vector<double> times =
674  this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
675  const unsigned int
676  n_dofs_per_interval =
677  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
678  std::vector<unsigned int> local_to_global(n_dofs_per_interval);
679  {
680  TimeIterator it =
681  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
682  it.get_time_dof_indices(local_to_global);
683  this->GetProblem()->SetTime(times[local_to_global[0]],local_to_global[0], it,true);
684  GetU().SetTimeDoFNumber(local_to_global[0], it);
685  GetZ().SetTimeDoFNumber(local_to_global[0], it);
686  q.SetTimeDoFNumber(local_to_global[0]);
687  gradient.SetTimeDoFNumber(local_to_global[0]);
688  gradient_transposed.SetTimeDoFNumber(local_to_global[0]);
689  }
690 
691  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
692 
693  if (dopedim == dealdim)
694  {
695  this->GetControlIntegrator().AddDomainData("control",
696  &(q.GetSpacialVector()));
697  }
698  else if (dopedim == 0)
699  {
700  this->GetControlIntegrator().AddParamData("control",
701  &(q.GetSpacialVectorCopy()));
702  }
703  else
704  {
705  throw DOpEException("dopedim not implemented",
706  "InstatReducedProblem::ComputeReducedGradient");
707  }
708 
709  this->GetControlIntegrator().AddDomainData("state",
710  &(GetU().GetSpacialVector()));
711  this->GetControlIntegrator().AddDomainData("adjoint",
712  &(GetZ().GetSpacialVector()));
713  gradient_transposed = 0.;
714  if (dopedim == dealdim)
715  {
716  this->GetControlIntegrator().AddDomainData("last_newton_solution",
717  &(gradient_transposed.GetSpacialVector()));
718  this->GetControlIntegrator().ComputeNonlinearResidual(
719  *(this->GetProblem()), gradient.GetSpacialVector(), true);
720  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
721  }
722  else if (dopedim == 0)
723  {
724  this->GetControlIntegrator().AddParamData("last_newton_solution",
725  &(gradient_transposed.GetSpacialVectorCopy()));
726  this->GetControlIntegrator().ComputeNonlinearResidual(
727  *(this->GetProblem()), gradient.GetSpacialVector(), true);
728 
729  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
730  gradient_transposed.UnLockCopy();
731  }
732 
733  gradient *= -1.;
734  gradient_transposed = gradient;
735 
736  //Compute l^2 representation of the Gradient
737 
738  build_control_matrix_ = this->GetControlNonlinearSolver().NonlinearSolve(
739  *(this->GetProblem()), gradient_transposed.GetSpacialVector(), true,
740  build_control_matrix_);
741  if (dopedim == dealdim)
742  {
743  this->GetControlIntegrator().DeleteDomainData("control");
744  }
745  else if (dopedim == 0)
746  {
747  this->GetControlIntegrator().DeleteParamData("control");
748  q.UnLockCopy();
749  }
750  else
751  {
752  throw DOpEException("dopedim not implemented",
753  "InstatReducedProblem::ComputeReducedGradient");
754  }
755  this->GetControlIntegrator().DeleteDomainData("state");
756  this->GetControlIntegrator().DeleteDomainData("adjoint");
757 
758  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
759  this->GetControlIntegrator());
760 
761  this->GetOutputHandler()->Write(gradient,
762  "Gradient" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
763  this->GetOutputHandler()->Write(gradient_transposed,
764  "Gradient_Transposed" + this->GetPostIndex(),
765  this->GetProblem()->GetDoFType());
766  }//End initial
767  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::stationary)
768  {
769 
770  //Set time to initial
771  const std::vector<double> times =
772  this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
773  const unsigned int
774  n_dofs_per_interval =
775  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
776  std::vector<unsigned int> local_to_global(n_dofs_per_interval);
777  {
778  TimeIterator it =
779  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
780  it.get_time_dof_indices(local_to_global);
781  this->GetProblem()->SetTime(times[local_to_global[0]],local_to_global[0], it,true);
782  GetU().SetTimeDoFNumber(local_to_global[0], it);
783  GetZ().SetTimeDoFNumber(local_to_global[0], it);
784  q.SetTimeDoFNumber(local_to_global[0]);
785  gradient.SetTimeDoFNumber(local_to_global[0]);
786  gradient_transposed.SetTimeDoFNumber(local_to_global[0]);
787  }
788  //Dupliziere ggf. vorberechnete Werte.
789  ControlVector<VECTOR> tmp = gradient;
790  tmp.GetSpacialVector() = gradient.GetSpacialVector();
791 
792 
793  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
794 
795  if (dopedim == dealdim)
796  {
797  this->GetControlIntegrator().AddDomainData("control",
798  &(q.GetSpacialVector()));
799  this->GetControlIntegrator().AddDomainData("fixed_rhs",&tmp.GetSpacialVector());
800  }
801  else if (dopedim == 0)
802  {
803  this->GetControlIntegrator().AddParamData("control",
804  &(q.GetSpacialVectorCopy()));
805  this->GetControlIntegrator().AddParamData("fixed_rhs",
806  &(tmp.GetSpacialVectorCopy()));
807  }
808  else
809  {
810  throw DOpEException("dopedim not implemented",
811  "InstatReducedProblem::ComputeReducedGradient");
812  }
813 
814  this->GetControlIntegrator().AddDomainData("state",
815  &(GetU().GetSpacialVector()));
816  this->GetControlIntegrator().AddDomainData("adjoint",
817  &(GetZ().GetSpacialVector()));
818  gradient_transposed = 0.;
819  if (dopedim == dealdim)
820  {
821  this->GetControlIntegrator().AddDomainData("last_newton_solution",
822  &(gradient_transposed.GetSpacialVector()));
823  this->GetControlIntegrator().ComputeNonlinearResidual(
824  *(this->GetProblem()), gradient.GetSpacialVector(), true);
825  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
826  }
827  else if (dopedim == 0)
828  {
829  this->GetControlIntegrator().AddParamData("last_newton_solution",
830  &(gradient_transposed.GetSpacialVectorCopy()));
831  this->GetControlIntegrator().ComputeNonlinearResidual(
832  *(this->GetProblem()), gradient.GetSpacialVector(), true);
833 
834  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
835  gradient_transposed.UnLockCopy();
836  }
837 
838  gradient *= -1.;
839  gradient_transposed = gradient;
840 
841  //Compute l^2 representation of the Gradient
842 
843  build_control_matrix_ = this->GetControlNonlinearSolver().NonlinearSolve(
844  *(this->GetProblem()), gradient_transposed.GetSpacialVector(), true,
845  build_control_matrix_);
846  if (dopedim == dealdim)
847  {
848  this->GetControlIntegrator().DeleteDomainData("control");
849  this->GetControlIntegrator().DeleteDomainData("fixed_rhs");
850  }
851  else if (dopedim == 0)
852  {
853  this->GetControlIntegrator().DeleteParamData("control");
854  q.UnLockCopy();
855  this->GetControlIntegrator().DeleteParamData("fixed_rhs");
856  tmp.UnLockCopy();
857  }
858  else
859  {
860  throw DOpEException("dopedim not implemented",
861  "InstatReducedProblem::ComputeReducedGradient");
862  }
863  this->GetControlIntegrator().DeleteDomainData("state");
864  this->GetControlIntegrator().DeleteDomainData("adjoint");
865 
866  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
867  this->GetControlIntegrator());
868 
869  this->GetOutputHandler()->Write(gradient,
870  "Gradient" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
871  this->GetOutputHandler()->Write(gradient_transposed,
872  "Gradient_Transposed" + this->GetPostIndex(),
873  this->GetProblem()->GetDoFType());
874  }//End stationary
875  else if (this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::nonstationary)
876  {
877  //Nothing to do, all in the adjoint calculation
878  }
879  else
880  {
881  std::stringstream out;
882  out << "Unknown ControlType: "<<DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType());
883  throw DOpEException(out.str(), "InstatReducedProblem::ComputeReducedGradient");
884  }
885 }
886 
887 /******************************************************/
888 
889 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
890  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
891  int dealdim>
892 double InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
893  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedCostFunctional(
894  const ControlVector<VECTOR>& q)
895 {
896  this->ComputeReducedState(q);
897 
898  if (this->GetFunctionalValues()[0].size() != 1)
899  {
900  if (this->GetFunctionalValues()[0].size() == 0)
901  throw DOpEException(
902  "Apparently the CostFunctional was never evaluated! \n\tCheck if the return value of `NeedTimes' is set correctly.",
903  "InstatReducedProblem::ComputeReducedCostFunctional");
904  else
905  throw DOpEException(
906  "The CostFunctional has been evaluated too many times! \n\tCheck if the return value of `NeedTimes' is set correctly.",
907  "InstatReducedProblem::ComputeReducedCostFunctional");
908  }
909  return this->GetFunctionalValues()[0][0];
910 }
911 
912 /******************************************************/
913 
914 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
915  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
916  int dealdim>
917 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
918  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedFunctionals(
919  const ControlVector<VECTOR>& /*q*/)
920 {
921  //We dont need q as the values are precomputed during Solve State...
922  this->GetOutputHandler()->Write("Computing Functionals:" + this->GetBasePriority(), 4);
923 
924  for (unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
925  {
926  this->SetProblemType("aux_functional", i);
927  if (this->GetProblem()->GetFunctionalType().find("timelocal") != std::string::npos)
928  {
929  if (this->GetFunctionalValues()[i + 1].size() == 1)
930  {
931  std::stringstream out;
932  this->GetOutputHandler()->InitOut(out);
933  out << this->GetProblem()->GetFunctionalName() << ": " << this->GetFunctionalValues()[i + 1][0];
934  this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
935  }
936  else if (this->GetFunctionalValues()[i + 1].size() > 1)
937  {
938  if (this->GetFunctionalValues()[i + 1].size()
939  == this->GetProblem()->GetSpaceTimeHandler()->GetMaxTimePoint() + 1)
940  {
941  std::stringstream out;
942  this->GetOutputHandler()->InitOut(out);
943  out << this->GetProblem()->GetFunctionalName() << " too large. Writing to file instead: ";
944  this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
945  this->GetOutputHandler()->Write(this->GetFunctionalValues()[i + 1],
946  this->GetProblem()->GetFunctionalName()
947  + this->GetPostIndex(), "time");
948  }
949  else
950  {
951  std::stringstream out;
952  this->GetOutputHandler()->InitOut(out);
953  out << this->GetProblem()->GetFunctionalName() << ": ";
954  for (unsigned int k = 0; k < this->GetFunctionalValues()[i + 1].size(); k++)
955  out << this->GetFunctionalValues()[i + 1][k] << " ";
956  this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
957  }
958  }
959  else
960  {
961  throw DOpEException("Functional: " + this->GetProblem()->GetFunctionalType()
962  + " was not evaluated ever!", "InstatReducedProblem::ComputeFunctionals");
963  }
964  }
965  else if (this->GetProblem()->GetFunctionalType().find("timedistributed") != std::string::npos)
966  {
967  std::stringstream out;
968  this->GetOutputHandler()->InitOut(out);
969  out << this->GetProblem()->GetFunctionalName() << ": " << this->GetFunctionalValues()[i + 1][0];
970  this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
971  }
972  else
973  {
974  throw DOpEException("Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
975  "InstatReducedProblem::ComputeFunctionals");
976  }
977  }
978 }
979 
980 /******************************************************/
981 
982 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
983  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
984  int dealdim>
985 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
986  PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedHessianVector(
987  const ControlVector<VECTOR>& q,
988  const ControlVector<VECTOR>& direction,
989  ControlVector<VECTOR>& hessian_direction,
990  ControlVector<VECTOR>& hessian_direction_transposed)
991 {
992  this->GetOutputHandler()->Write("Computing ReducedHessianVector:",
993  4 + this->GetBasePriority());
994  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() != DOpEtypes::ControlType::initial)
995  {
996  hessian_direction = 0.;
997  }
998  //Solving the Tangent Problem
999  {
1000  this->GetOutputHandler()->Write("\tSolving Tangent:",
1001  5 + this->GetBasePriority());
1002  this->SetProblemType("tangent");
1003  auto& problem = this->GetProblem()->GetTangentProblem();
1004 
1005  this->GetProblem()->AddAuxiliaryControl(&q,"control");
1006  this->GetProblem()->AddAuxiliaryControl(&direction,"dq");
1007  this->GetProblem()->AddAuxiliaryState(&(this->GetU()),"state");
1008  this->GetProblem()->AddAuxiliaryState(&(this->GetZ()),"adjoint");
1009 
1010  this->ForwardTimeLoop(problem,this->GetDU(),"Tangent",false);
1011 
1012  }
1013  //Solving the Adjoint-Hessian Problem
1014  {
1015  this->GetOutputHandler()->Write("\tSolving Adjoint Hessian:",
1016  5 + this->GetBasePriority());
1017  this->SetProblemType("adjoint_hessian");
1018  auto& problem = this->GetProblem()->GetAdjointHessianProblem();
1019 
1020  this->GetProblem()->AddAuxiliaryState(&(this->GetDU()),"tangent");
1021 
1022  this->BackwardTimeLoop(problem,this->GetDZ(),hessian_direction,hessian_direction_transposed,"Hessian",true);
1023  }
1024  if (this->GetProblem()->HasControlInDirichletData())
1025  {
1026  throw DOpEException("Control in Dirichlet Data for instationary problems not yet implemented!"
1027  ,"InstatReducedProblem::ComputeReducedHessianVector");
1028  }
1029 
1030  //Computing Hessian times Vector Representation
1031  {
1032  this->GetOutputHandler()->Write(
1033  "\tComputing Representation of the Hessian:",
1034  5 + this->GetBasePriority());
1035  this->SetProblemType("hessian");
1036 
1037  this->GetProblem()->AddAuxiliaryState(&(this->GetDZ()),"adjoint_hessian");
1038 
1039  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::initial)
1040  {
1041  //Set time to initial
1042  const std::vector<double> times =
1043  this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
1044  const unsigned int
1045  n_dofs_per_interval =
1046  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1047  std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1048  {
1049  TimeIterator it =
1050  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
1051  it.get_time_dof_indices(local_to_global);
1052  this->GetProblem()->SetTime(times[local_to_global[0]],local_to_global[0], it,true);
1053  GetU().SetTimeDoFNumber(local_to_global[0], it);
1054  GetZ().SetTimeDoFNumber(local_to_global[0], it);
1055  GetDU().SetTimeDoFNumber(local_to_global[0], it);
1056  GetDZ().SetTimeDoFNumber(local_to_global[0], it);
1057  q.SetTimeDoFNumber(local_to_global[0]);
1058  hessian_direction.SetTimeDoFNumber(local_to_global[0]);
1059  hessian_direction_transposed.SetTimeDoFNumber(local_to_global[0]);
1060  }
1061 
1062  this->GetProblem()->AddAuxiliaryToIntegrator(
1063  this->GetControlIntegrator());
1064 
1065  hessian_direction_transposed = 0.;
1066  if (dopedim == dealdim)
1067  {
1068  this->GetControlIntegrator().AddDomainData("last_newton_solution",
1069  &(hessian_direction_transposed.GetSpacialVector()));
1070  this->GetControlIntegrator().ComputeNonlinearResidual(
1071  *(this->GetProblem()), hessian_direction.GetSpacialVector(),
1072  true);
1073  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
1074  }
1075  else if (dopedim == 0)
1076  {
1077  this->GetControlIntegrator().AddParamData("last_newton_solution",
1078  &(hessian_direction_transposed.GetSpacialVectorCopy()));
1079  this->GetControlIntegrator().ComputeNonlinearResidual(
1080  *(this->GetProblem()), hessian_direction.GetSpacialVector(),
1081  true);
1082  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
1083  hessian_direction_transposed.UnLockCopy();
1084  }
1085 
1086  hessian_direction *= -1.;
1087  hessian_direction_transposed = hessian_direction;
1088  //Compute l^2 representation of the HessianVector
1089  //hessian Matrix is the same as control matrix
1090  build_control_matrix_ =
1091  this->GetControlNonlinearSolver().NonlinearSolve(
1092  *(this->GetProblem()),
1093  hessian_direction_transposed.GetSpacialVector(), true,
1094  build_control_matrix_);
1095 
1096  this->GetOutputHandler()->Write(hessian_direction,
1097  "HessianDirection" + this->GetPostIndex(),
1098  this->GetProblem()->GetDoFType());
1099  this->GetOutputHandler()->Write(hessian_direction_transposed,
1100  "HessianDirection_Transposed" + this->GetPostIndex(),
1101  this->GetProblem()->GetDoFType());
1102 
1103  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1104  this->GetControlIntegrator());
1105  }//Endof the case of control in the initial values
1106  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::stationary)
1107  {
1108  //Set time to initial
1109  const std::vector<double> times =
1110  this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
1111  const unsigned int
1112  n_dofs_per_interval =
1113  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1114  std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1115  {
1116  TimeIterator it =
1117  this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
1118  it.get_time_dof_indices(local_to_global);
1119  this->GetProblem()->SetTime(times[local_to_global[0]],local_to_global[0], it,true);
1120  GetU().SetTimeDoFNumber(local_to_global[0], it);
1121  GetZ().SetTimeDoFNumber(local_to_global[0], it);
1122  GetDU().SetTimeDoFNumber(local_to_global[0], it);
1123  GetDZ().SetTimeDoFNumber(local_to_global[0], it);
1124  q.SetTimeDoFNumber(local_to_global[0]);
1125  hessian_direction.SetTimeDoFNumber(local_to_global[0]);
1126  hessian_direction_transposed.SetTimeDoFNumber(local_to_global[0]);
1127  }
1128  //Dupliziere ggf. vorberechnete Werte.
1129  ControlVector<VECTOR> tmp = hessian_direction;
1130  tmp.GetSpacialVector() = hessian_direction.GetSpacialVector();
1131 
1132  this->GetProblem()->AddAuxiliaryToIntegrator(
1133  this->GetControlIntegrator());
1134 
1135  hessian_direction_transposed = 0.;
1136  if (dopedim == dealdim)
1137  {
1138  this->GetControlIntegrator().AddDomainData("fixed_rhs",&tmp.GetSpacialVector());
1139  this->GetControlIntegrator().AddDomainData("last_newton_solution",
1140  &(hessian_direction_transposed.GetSpacialVector()));
1141  this->GetControlIntegrator().ComputeNonlinearResidual(
1142  *(this->GetProblem()), hessian_direction.GetSpacialVector(),
1143  true);
1144  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
1145  this->GetControlIntegrator().DeleteDomainData("fixed_rhs");
1146  }
1147  else if (dopedim == 0)
1148  {
1149  this->GetControlIntegrator().AddParamData("fixed_rhs",
1150  &(tmp.GetSpacialVectorCopy()));
1151  this->GetControlIntegrator().AddParamData("last_newton_solution",
1152  &(hessian_direction_transposed.GetSpacialVectorCopy()));
1153  this->GetControlIntegrator().ComputeNonlinearResidual(
1154  *(this->GetProblem()), hessian_direction.GetSpacialVector(),
1155  true);
1156  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
1157  hessian_direction_transposed.UnLockCopy();
1158  this->GetControlIntegrator().DeleteParamData("fixed_rhs");
1159  tmp.UnLockCopy();
1160  }
1161 
1162  hessian_direction *= -1.;
1163  hessian_direction_transposed = hessian_direction;
1164  //Compute l^2 representation of the HessianVector
1165  //hessian Matrix is the same as control matrix
1166  build_control_matrix_ =
1167  this->GetControlNonlinearSolver().NonlinearSolve(
1168  *(this->GetProblem()),
1169  hessian_direction_transposed.GetSpacialVector(), true,
1170  build_control_matrix_);
1171 
1172  this->GetOutputHandler()->Write(hessian_direction,
1173  "HessianDirection" + this->GetPostIndex(),
1174  this->GetProblem()->GetDoFType());
1175  this->GetOutputHandler()->Write(hessian_direction_transposed,
1176  "HessianDirection_Transposed" + this->GetPostIndex(),
1177  this->GetProblem()->GetDoFType());
1178 
1179  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1180  this->GetControlIntegrator());
1181  }//Endof stationary
1182  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::nonstationary)
1183  {
1184  //Nothing to do
1185  }
1186  else
1187  {
1188  std::stringstream out;
1189  out << "Unknown ControlType: "<<DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType());
1190  throw DOpEException(out.str(), "InstatReducedProblem::ComputeReducedHessianVector");
1191  }
1192  }//End of HessianVector Repr.
1193 
1194  //Cleaning
1195  this->GetProblem()->DeleteAuxiliaryControl("control");
1196  this->GetProblem()->DeleteAuxiliaryControl("dq");
1197  this->GetProblem()->DeleteAuxiliaryState("state");
1198  this->GetProblem()->DeleteAuxiliaryState("adjoint");
1199  this->GetProblem()->DeleteAuxiliaryState("tangent");
1200  this->GetProblem()->DeleteAuxiliaryState("adjoint_hessian");
1201 }
1202 
1203 /******************************************************/
1204 
1205 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
1206  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
1207  int dealdim>
1208 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1209  PROBLEM, VECTOR, dopedim, dealdim>::ComputeTimeFunctionals(unsigned int step, unsigned int num_steps)
1210 {
1211  std::stringstream out;
1212  this->GetOutputHandler()->InitOut(out);
1213  out << "\t Precalculating functional values ";
1214  this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1215 
1216  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1217 
1218  this->GetIntegrator().AddDomainData("state", &(GetU().GetSpacialVector()));
1219  double ret = 0;
1220  bool found = false;
1221  {//CostFunctional
1222  this->SetProblemType("cost_functional");
1223  if (this->GetProblem()->NeedTimeFunctional())
1224  {
1225  if (this->GetProblem()->GetFunctionalType().find("domain") != std::string::npos)
1226  {
1227  found = true;
1228  ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1229  }
1230  if (this->GetProblem()->GetFunctionalType().find("point") != std::string::npos)
1231  {
1232  found = true;
1233  ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1234  }
1235  if (this->GetProblem()->GetFunctionalType().find("boundary") != std::string::npos)
1236  {
1237  found = true;
1238  ret += this->GetIntegrator().ComputeBoundaryScalar(*(this->GetProblem()));
1239  }
1240  if (this->GetProblem()->GetFunctionalType().find("face") != std::string::npos)
1241  {
1242  found = true;
1243  ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1244  }
1245 
1246  if (!found)
1247  {
1248  throw DOpEException("Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1249  "InstatReducedProblem::ComputeTimeFunctionals");
1250  }
1251  //Wert speichern
1252  if (this->GetProblem()->GetFunctionalType().find("timelocal") != std::string::npos)
1253  {
1254  if (this->GetFunctionalValues()[0].size() != 1)
1255  {
1256  this->GetFunctionalValues()[0].resize(1);
1257  this->GetFunctionalValues()[0][0] = 0.;
1258  }
1259  this->GetFunctionalValues()[0][0] += ret;
1260  }
1261  else if (this->GetProblem()->GetFunctionalType().find("timedistributed") != std::string::npos)
1262  {//TODO was passiert hier? Vermutlich sollte hier spaeter Zeitintegration durchgefuehrt werden?
1263  if (this->GetFunctionalValues()[0].size() != 1)
1264  {
1265  this->GetFunctionalValues()[0].resize(1);
1266  this->GetFunctionalValues()[0][0] = 0.;
1267  }
1268  double w = 0.;
1269  if ((step == 0))
1270  {
1271  w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1272  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1273  }
1274  else if (step == num_steps)
1275  {
1276  w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1277  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1278  }
1279  else
1280  {
1281  w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1282  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1283  w += 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1284  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1285  }
1286  this->GetFunctionalValues()[0][0] += w * ret;
1287  }
1288  else
1289  {
1290  throw DOpEException("Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1291  "InstatReducedProblem::ComputeTimeFunctionals");
1292  }
1293  }
1294  }
1295  {//Aux Functionals
1296  for (unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
1297  {
1298  ret = 0;
1299  found = false;
1300  this->SetProblemType("aux_functional", i);
1301  if (this->GetProblem()->NeedTimeFunctional())
1302  {
1303  if (this->GetProblem()->GetFunctionalType().find("domain") != std::string::npos)
1304  {
1305  found = true;
1306  ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1307  }
1308  if (this->GetProblem()->GetFunctionalType().find("point") != std::string::npos)
1309  {
1310  found = true;
1311  ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1312  }
1313  if (this->GetProblem()->GetFunctionalType().find("boundary") != std::string::npos)
1314  {
1315  found = true;
1316  ret += this->GetIntegrator().ComputeBoundaryScalar(*(this->GetProblem()));
1317  }
1318  if (this->GetProblem()->GetFunctionalType().find("face") != std::string::npos)
1319  {
1320  found = true;
1321  ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1322  }
1323 
1324  if (!found)
1325  {
1326  throw DOpEException(
1327  "Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1328  "InstatReducedProblem::ComputeTimeFunctionals");
1329  }
1330  //Wert speichern
1331  if (this->GetProblem()->GetFunctionalType().find("timelocal") != std::string::npos)
1332  {
1333  std::stringstream out;
1334  this->GetOutputHandler()->InitOut(out);
1335  out << "\t" << this->GetProblem()->GetFunctionalName() << ": " << ret;
1336  this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1337  this->GetFunctionalValues()[i + 1].push_back(ret);
1338  }
1339  else if (this->GetProblem()->GetFunctionalType().find("timedistributed") != std::string::npos)
1340  {
1341  if (this->GetFunctionalValues()[i + 1].size() != 1)
1342  {
1343  this->GetFunctionalValues()[i + 1].resize(1);
1344  this->GetFunctionalValues()[i + 1][0] = 0.;
1345  }
1346  double w = 0.;
1347  if ((step == 0))
1348  {
1349  w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1350  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1351  }
1352  else if (step == num_steps)
1353  {
1354  w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1355  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1356  }
1357  else
1358  {
1359  w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1360  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1361  w += 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1362  - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1363  }
1364  this->GetFunctionalValues()[i + 1][0] += w * ret;
1365  }
1366  else
1367  {
1368  throw DOpEException(
1369  "Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1370  "InstatReducedProblem::ComputeTimeFunctionals");
1371  }
1372  }
1373  }
1374  }
1375  this->GetIntegrator().DeleteDomainData("state");
1376  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1377 
1378 }
1379 
1380 /******************************************************/
1381 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
1382 typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
1383 int dealdim>
1384 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1385 PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(const VECTOR &v, std::string name, std::string outfile, std::string dof_type, std::string filetype)
1386 {
1387  if (dof_type == "state")
1388  {
1389  auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1390  data_out.attach_dof_handler(this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFHandler());
1391 
1392  data_out.add_data_vector(v, name);
1393  data_out.build_patches();
1394 
1395  std::ofstream output(outfile.c_str());
1396 
1397  if (filetype == ".vtk")
1398  {
1399  data_out.write_vtk(output);
1400  }
1401  else if (filetype == ".gpl")
1402  {
1403  data_out.write_gnuplot(output);
1404  }
1405  else
1406  {
1407  throw DOpEException("Don't know how to write filetype `" + filetype + "'!",
1408  "InstatReducedProblem::WriteToFile");
1409  }
1410  data_out.clear();
1411  }
1412  else if (dof_type == "control")
1413  {
1414 #if dope_dimension >0
1415  auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1416  data_out.attach_dof_handler (this->GetProblem()->GetSpaceTimeHandler()->GetControlDoFHandler());
1417 
1418  data_out.add_data_vector (v,name);
1419  data_out.build_patches ();
1420 
1421  std::ofstream output(outfile.c_str());
1422 
1423  if(filetype == ".vtk")
1424  {
1425  data_out.write_vtk (output);
1426  }
1427  else if(filetype == ".gpl")
1428  {
1429  data_out.write_gnuplot (output);
1430  }
1431  else
1432  {
1433  throw DOpEException("Don't know how to write filetype `" + filetype + "'!","InstatReducedProblem::WriteToFile");
1434  }
1435  data_out.clear();
1436 #else
1437  std::ofstream output(outfile.c_str());
1438  Vector<double> off;
1439  off = v;
1440  for (unsigned int i = 0; i < off.size(); i++)
1441  {
1442  output << off(i) << std::endl;
1443  }
1444 #endif
1445  }
1446  else
1447  {
1448  throw DOpEException("No such DoFHandler `" + dof_type + "'!",
1449  "InstatReducedProblem::WriteToFile");
1450  }
1451 }
1452 
1453 /******************************************************/
1454 
1455 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER, typename CONTROLINTEGRATOR,
1456  typename INTEGRATOR, typename PROBLEM, typename VECTOR, int dopedim,
1457  int dealdim>
1458 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1459  PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(const ControlVector<VECTOR> &v,
1460  std::string name,
1461  std::string dof_type)
1462 {
1463  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::initial
1464  || this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::stationary)
1465  {
1466  v.SetTimeDoFNumber(0);
1467  this->GetOutputHandler()->Write(v.GetSpacialVector(), name, dof_type);
1468  }
1469  else if ( this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::nonstationary)
1470  {
1471  unsigned int maxt = this->GetProblem()->GetSpaceTimeHandler()->GetMaxTimePoint();
1472 
1473  for(unsigned int i = 0; i <= maxt; i++)
1474  {
1475  this->GetOutputHandler()->SetIterationNumber(i, "Time");
1476  v.SetTimeDoFNumber(i);
1477  this->GetOutputHandler()->Write(v.GetSpacialVector(), name, dof_type);
1478  }
1479  }
1480  else
1481  {
1482  throw DOpEException("Unknown ControlType: "+DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType()), "InstatReducedProblem::WriteToFile");
1483  }
1484 }
1485 
1486 /******************************************************/
1487 
1488  template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER,
1489  typename CONTROLINTEGRATOR, typename INTEGRATOR, typename PROBLEM,
1490  typename VECTOR, int dopedim, int dealdim>
1491  void
1492  InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
1493  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(
1494  const std::vector<double> &v, std::string outfile)
1495  {
1496  //TODO This should get timedofhandler later on.
1497  const std::vector<double>& t =
1498  this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
1499  std::ofstream out(outfile.c_str());
1500  assert( t.size() == v.size());
1501  assert(out.is_open());
1502 
1503  out << "#Time\tvalue" << std::endl;
1504  for (unsigned int i = 0; i < v.size(); i++)
1505  {
1506  out << t[i] << "\t" << v[i] << std::endl;
1507  }
1508  out.close();
1509  }
1510 
1511 /******************************************************/
1512 
1513 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER,
1514  typename CONTROLINTEGRATOR, typename INTEGRATOR, typename PROBLEM,
1515  typename VECTOR, int dopedim, int dealdim>
1516  template<typename PDE>
1517  void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
1518  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::
1519  ForwardTimeLoop(PDE& problem, StateVector<VECTOR>& sol, std::string outname, bool eval_funcs)
1520  {
1521  VECTOR u_alt;
1522 
1523  unsigned int max_timestep =
1524  problem.GetSpaceTimeHandler()->GetMaxTimePoint();
1525  const std::vector<double> times =
1526  problem.GetSpaceTimeHandler()->GetTimes();
1527  const unsigned int
1528  n_dofs_per_interval =
1529  problem.GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1530  std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1531  {
1532  TimeIterator it =
1533  problem.GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
1534  it.get_time_dof_indices(local_to_global);
1535  problem.SetTime(times[local_to_global[0]], local_to_global[0], it,true);
1536  sol.SetTimeDoFNumber(local_to_global[0], it);
1537  }
1538  //u_alt auf initial_values setzen
1539  {
1540  //dazu erstmal gesamt-dof berechnen
1541  const std::vector<unsigned int>& dofs_per_block =
1542  this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFsPerBlock();
1543  unsigned int n_dofs = 0;
1544  unsigned int n_blocks = dofs_per_block.size();
1545  for (unsigned int i = 0; i < n_blocks; i++)
1546  {
1547  n_dofs += dofs_per_block[i];
1548  }
1549  //und dann auf den Helper zuerueckgreifen (wegen Templateisierung)
1550  DOpEHelper::ReSizeVector(n_dofs, dofs_per_block, u_alt);
1551  }
1552 
1553  //Projection der Anfangsdaten
1554  this->GetOutputHandler()->SetIterationNumber(0, "Time");
1555  {
1556  this->GetOutputHandler()->Write("Computing Initial Values:",
1557  4 + this->GetBasePriority());
1558 
1559  auto& initial_problem = problem.GetInitialProblem();
1560  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1561 
1562  //TODO: Possibly another solver for the initial value than for the pde...
1563  build_state_matrix_ = this->GetNonlinearSolver("state").NonlinearSolve_Initial(
1564  initial_problem, u_alt, true, true);
1565  build_state_matrix_ = true;
1566 
1567  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1568 
1569  }
1570  sol.GetSpacialVector() = u_alt;
1571  this->GetOutputHandler()->Write(u_alt, outname + this->GetPostIndex(),
1572  problem.GetDoFType());
1573 
1574 
1575  if(eval_funcs)
1576  {//Funktional Auswertung in t_0
1577  ComputeTimeFunctionals(0,
1578  max_timestep);
1579  this->SetProblemType("state");
1580  }
1581 
1582 
1583  for (TimeIterator it =
1584  problem.GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval(); it
1585  != problem.GetSpaceTimeHandler()->GetTimeDoFHandler().after_last_interval(); ++it)
1586  {
1587  it.get_time_dof_indices(local_to_global);
1588  problem.SetTime(times[local_to_global[0]], local_to_global[0], it);
1589  sol.SetTimeDoFNumber(local_to_global[0], it);
1590  //TODO Eventuell waere ein Test mit nicht-gleichmaessigen Zeitschritten sinnvoll!
1591 
1592  //we start here at i=1 because we assume that the most
1593  //left DoF in the actual interval is already computed!
1594  for (unsigned int i = 1; i < n_dofs_per_interval; i++)
1595  {
1596  this->GetOutputHandler()->SetIterationNumber(local_to_global[i],
1597  "Time");
1598  double time = times[local_to_global[i]];
1599 
1600  std::stringstream out;
1601  this->GetOutputHandler()->InitOut(out);
1602  out << "\t Timestep: " << local_to_global[i] << " ("
1603  << times[local_to_global[i - 1]] << " -> " << time
1604  << ") using " << problem.GetName();
1605  problem.GetOutputHandler()->Write(out,
1606  4 + this->GetBasePriority());
1607 
1608  sol.SetTimeDoFNumber(local_to_global[i], it);
1609  sol.GetSpacialVector() = 0;
1610 
1611  this->GetProblem()->AddAuxiliaryToIntegrator(
1612  this->GetIntegrator());
1613 
1614  this->GetNonlinearSolver("state").NonlinearLastTimeEvals(problem,
1615  u_alt, sol.GetSpacialVector());
1616 
1617  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1618  this->GetIntegrator());
1619 
1620  problem.SetTime(time, local_to_global[i], it);
1621  this->GetProblem()->AddAuxiliaryToIntegrator(
1622  this->GetIntegrator());
1623  this->GetProblem()->AddPreviousAuxiliaryToIntegrator(
1624  this->GetIntegrator());
1625 
1626  build_state_matrix_
1627  = this->GetNonlinearSolver("state").NonlinearSolve(problem,
1628  u_alt, sol.GetSpacialVector(), true,
1629  build_state_matrix_);
1630 
1631  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1632  this->GetIntegrator());
1633  this->GetProblem()->DeletePreviousAuxiliaryFromIntegrator(
1634  this->GetIntegrator());
1635 
1636  //TODO do a transfer to the next grid for changing spatial meshes!
1637  u_alt = sol.GetSpacialVector();
1638  this->GetOutputHandler()->Write(sol.GetSpacialVector(),
1639  outname + this->GetPostIndex(), problem.GetDoFType());
1640  if(eval_funcs)
1641  {//Funktional Auswertung in t_n//if abfrage, welcher typ
1642  ComputeTimeFunctionals(local_to_global[i], max_timestep);
1643  this->SetProblemType("state");
1644  }
1645  }
1646  }
1647  }
1648 
1649 /******************************************************/
1650 
1651 template<typename CONTROLNONLINEARSOLVER, typename NONLINEARSOLVER,
1652  typename CONTROLINTEGRATOR, typename INTEGRATOR, typename PROBLEM,
1653  typename VECTOR, int dopedim, int dealdim>
1654  template<typename PDE>
1655  void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
1656  CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::
1657  BackwardTimeLoop(PDE& problem, StateVector<VECTOR>& sol, ControlVector<VECTOR>& temp_q, ControlVector<VECTOR>& temp_q_trans, std::string outname, bool eval_grads)
1658  {
1659  VECTOR u_alt;
1660 
1661  unsigned int max_timestep =
1662  problem.GetSpaceTimeHandler()->GetMaxTimePoint();
1663  const std::vector<double> times =
1664  problem.GetSpaceTimeHandler()->GetTimes();
1665  const unsigned int
1666  n_dofs_per_interval =
1667  problem.GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1668  std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1669  {
1670  TimeIterator it =
1671  problem.GetSpaceTimeHandler()->GetTimeDoFHandler().last_interval();
1672  it.get_time_dof_indices(local_to_global);
1673  //The initial values for the dual problem
1674  problem.SetTime(times[local_to_global[local_to_global.size()-1]],local_to_global[local_to_global.size()-1], it);
1675  sol.SetTimeDoFNumber(local_to_global[local_to_global.size()-1], it);
1676  }
1677  //u_alt auf initial_values setzen
1678  {
1679  //dazu erstmal gesamt-dof berechnen
1680  const std::vector<unsigned int>& dofs_per_block =
1681  this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFsPerBlock();
1682  unsigned int n_dofs = 0;
1683  unsigned int n_blocks = dofs_per_block.size();
1684  for (unsigned int i = 0; i < n_blocks; i++)
1685  {
1686  n_dofs += dofs_per_block[i];
1687  }
1688  //und dann auf den Helper zuerueckgreifen (wegen Templateisierung)
1689  DOpEHelper::ReSizeVector(n_dofs, dofs_per_block, u_alt);
1690  }
1691  //Projection der Anfangsdaten
1692  this->GetOutputHandler()->SetIterationNumber(max_timestep, "Time");
1693  {
1694  this->GetOutputHandler()->Write("Computing Initial Values:",
1695  4 + this->GetBasePriority());
1696 
1697  auto& initial_problem = problem.GetInitialProblem();
1698  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1699  this->GetProblem()->AddPreviousAuxiliaryToIntegrator(this->GetIntegrator());
1700 
1701  //TODO: Possibly another solver for the initial value than for the pde...
1702  build_state_matrix_ = this->GetNonlinearSolver("adjoint").NonlinearSolve_Initial(
1703  initial_problem, u_alt, true, true);
1704  build_state_matrix_ = true;
1705 
1706  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1707  this->GetProblem()->DeletePreviousAuxiliaryFromIntegrator(this->GetIntegrator());
1708 
1709  }
1710  sol.GetSpacialVector() = u_alt;
1711  this->GetOutputHandler()->Write(u_alt, outname + this->GetPostIndex(),
1712  problem.GetDoFType());
1713 
1714  //TODO: Maybe we should calculate the local gradient computations here
1715 
1716  for (TimeIterator it =
1717  problem.GetSpaceTimeHandler()->GetTimeDoFHandler().last_interval(); it
1718  != problem.GetSpaceTimeHandler()->GetTimeDoFHandler().before_first_interval(); --it)
1719  {
1720  it.get_time_dof_indices(local_to_global);
1721  problem.SetTime(times[local_to_global[local_to_global.size()-1]],local_to_global[local_to_global.size()-1], it);
1722  sol.SetTimeDoFNumber(local_to_global[local_to_global.size()-1], it);
1723  //TODO Eventuell waere ein Test mit nicht-gleichmaessigen Zeitschritten sinnvoll!
1724 
1725  //we start here at i= 1 and transform i -> n_dofs_per_interval-1-i because we assume that the most
1726  //right DoF in the actual interval is already computed!
1727  for (unsigned int i = 1; i < n_dofs_per_interval; i++)
1728  {
1729  unsigned int j = n_dofs_per_interval-1-i;
1730  this->GetOutputHandler()->SetIterationNumber(local_to_global[j],
1731  "Time");
1732  double time = times[local_to_global[j]];
1733 
1734  std::stringstream out;
1735  this->GetOutputHandler()->InitOut(out);
1736  out << "\t Timestep: " << local_to_global[j+1] << " ("
1737  << times[local_to_global[j + 1]] << " -> " << time
1738  << ") using " << problem.GetName();
1739  problem.GetOutputHandler()->Write(out,
1740  4 + this->GetBasePriority());
1741 
1742  sol.SetTimeDoFNumber(local_to_global[j], it);
1743  sol.GetSpacialVector() = 0;
1744 
1745  this->GetProblem()->AddAuxiliaryToIntegrator(
1746  this->GetIntegrator());
1747 
1748  this->GetNonlinearSolver("adjoint").NonlinearLastTimeEvals(problem,
1749  u_alt, sol.GetSpacialVector());
1750 
1751  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1752  this->GetIntegrator());
1753 
1754  problem.SetTime(time,local_to_global[j], it);
1755  this->GetProblem()->AddAuxiliaryToIntegrator(
1756  this->GetIntegrator());
1757  this->GetProblem()->AddNextAuxiliaryToIntegrator(
1758  this->GetIntegrator());
1759  if(local_to_global[j] != 0)
1760  this->GetProblem()->AddPreviousAuxiliaryToIntegrator(
1761  this->GetIntegrator());
1762 
1763  build_adjoint_matrix_
1764  = this->GetNonlinearSolver("adjoint").NonlinearSolve(problem,
1765  u_alt, sol.GetSpacialVector(), true,
1766  build_adjoint_matrix_);
1767 
1768  this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1769  this->GetIntegrator());
1770  this->GetProblem()->DeleteNextAuxiliaryFromIntegrator(
1771  this->GetIntegrator());
1772  if(local_to_global[j] != 0)
1773  this->GetProblem()->DeletePreviousAuxiliaryFromIntegrator(
1774  this->GetIntegrator());
1775 
1776  //TODO do a transfer to the next grid for changing spatial meshes!
1777  u_alt = sol.GetSpacialVector();
1778  this->GetOutputHandler()->Write(sol.GetSpacialVector(),
1779  outname + this->GetPostIndex(), problem.GetDoFType());
1780 
1781  //Maybe build local gradient here
1782  if(eval_grads)
1783  {
1784  if(outname == "Adjoint")
1785  {
1786  this->SetProblemType("gradient");
1787  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::initial)
1788  {
1789  //Nothing to do
1790  }
1791  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::stationary)
1792  {
1793  std::stringstream out;
1794  this->GetOutputHandler()->InitOut(out);
1795  out << "\t Precalculating gradient values ";
1796  this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1797 
1798  if(local_to_global[j] != 0)
1799  {
1800  //Update Residual
1801  //Only if not the initial time: Calculations at initial time are
1802  //performed in the ComputeReducedGradient function.
1803  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1804  temp_q.SetTimeDoFNumber(local_to_global[j]);
1805  this->GetControlIntegrator().AddDomainData("adjoint",&(sol.GetSpacialVector()));
1806 
1807  VECTOR tmp = temp_q.GetSpacialVector();
1808  if (dopedim == dealdim)
1809  {
1810  VECTOR tmp_2 = temp_q.GetSpacialVector();
1811  tmp = 0.;
1812  tmp_2 = 0.;
1813  this->GetControlIntegrator().AddDomainData("last_newton_solution",&tmp_2);
1814  this->GetControlIntegrator().ComputeNonlinearResidual(*(this->GetProblem()), tmp, true);
1815  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
1816  }
1817  else if (dopedim == 0)
1818  {
1819  dealii::Vector<double> tmp_2 = temp_q.GetSpacialVectorCopy();
1820  tmp = 0.;
1821  tmp_2 = 0.;
1822  this->GetControlIntegrator().AddParamData("last_newton_solution",&tmp_2);
1823  this->GetControlIntegrator().ComputeNonlinearResidual(*(this->GetProblem()), tmp, true);
1824  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
1825  temp_q.UnLockCopy();
1826  }
1827  this->GetControlIntegrator().DeleteDomainData("adjoint");
1828  temp_q.GetSpacialVector() -= tmp;
1829  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1830  }
1831  }//End of type stationary
1832  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::nonstationary)
1833  {
1834  std::stringstream out;
1835  this->GetOutputHandler()->InitOut(out);
1836  out << "\t Precalculating gradient values ";
1837  this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1838 
1839  //Distributed Problem, calculate local Gradient contributions at each time point
1840  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1841  temp_q.SetTimeDoFNumber(local_to_global[j]);
1842  temp_q_trans.SetTimeDoFNumber(local_to_global[j]);
1843  this->GetControlIntegrator().AddDomainData("adjoint",&(sol.GetSpacialVector()));
1844  temp_q_trans.GetSpacialVector() = 0.;
1845 
1846  if (dopedim == dealdim)
1847  {
1848  this->GetControlIntegrator().AddDomainData("last_newton_solution",
1849  &(temp_q_trans.GetSpacialVector()));
1850  this->GetControlIntegrator().ComputeNonlinearResidual(
1851  *(this->GetProblem()), temp_q.GetSpacialVector(), true);
1852  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
1853  }
1854  else if (dopedim == 0)
1855  {
1856  this->GetControlIntegrator().AddParamData("last_newton_solution",
1857  &(temp_q_trans.GetSpacialVectorCopy()));
1858  this->GetControlIntegrator().ComputeNonlinearResidual(
1859  *(this->GetProblem()), temp_q.GetSpacialVector(), true);
1860 
1861  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
1862  temp_q_trans.UnLockCopy();
1863  }
1864  temp_q.GetSpacialVector() *= -1.;
1865  //Prescale with inverse of time step size to anticipate the time-scalar product.
1866  temp_q_trans.GetSpacialVector().equ(1./problem.GetSpaceTimeHandler()->GetStepSize(),temp_q.GetSpacialVector());
1867  //Compute l^2 representation of the Gradient
1868 
1869  build_control_matrix_ = this->GetControlNonlinearSolver().NonlinearSolve(
1870  *(this->GetProblem()), temp_q_trans.GetSpacialVector(), true,
1871  build_control_matrix_);
1872 
1873  this->GetControlIntegrator().DeleteDomainData("adjoint");
1874 
1875  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1876 
1877  this->GetOutputHandler()->Write(temp_q.GetSpacialVector(),
1878  "Gradient" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
1879  this->GetOutputHandler()->Write(temp_q_trans.GetSpacialVector(),
1880  "Gradient_Transposed" + this->GetPostIndex(),
1881  this->GetProblem()->GetDoFType());
1882  }//End of type nonstationary
1883  else
1884  {
1885  throw DOpEException("Unknown ControlType: "+DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType())+". In case Adjoint.", "InstatReducedProblem::BackwardTimeLoop");
1886  }
1887  this->SetProblemType("adjoint");
1888  }//Endof Adjoint case
1889  else if (outname == "Hessian")
1890  {
1891  this->SetProblemType("hessian");
1892  if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::initial)
1893  {
1894  //Nothing to do
1895  }
1896  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::stationary)
1897  {
1898  std::stringstream out;
1899  this->GetOutputHandler()->InitOut(out);
1900  out << "\t Precalculating hessian values ";
1901  this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1902 
1903  if(local_to_global[j] != 0)
1904  {
1905  //Update Residual
1906  //Only if not the initial time: Calculations at initial time are
1907  //performed in the ComputeReducedHessian function.
1908  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1909  temp_q.SetTimeDoFNumber(local_to_global[j]);
1910  this->GetControlIntegrator().AddDomainData("adjoint_hessian",&(sol.GetSpacialVector()));
1911 
1912  VECTOR tmp = temp_q.GetSpacialVector();
1913  if (dopedim == dealdim)
1914  {
1915  VECTOR tmp_2 = temp_q.GetSpacialVector();
1916  tmp = 0.;
1917  tmp_2 = 0.;
1918  this->GetControlIntegrator().AddDomainData("last_newton_solution",&tmp_2);
1919  this->GetControlIntegrator().ComputeNonlinearResidual(*(this->GetProblem()), tmp, true);
1920  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
1921  }
1922  else if (dopedim == 0)
1923  {
1924  dealii::Vector<double> tmp_2 = temp_q.GetSpacialVectorCopy();
1925  tmp = 0.;
1926  tmp_2 = 0.;
1927  this->GetControlIntegrator().AddParamData("last_newton_solution",&tmp_2);
1928  this->GetControlIntegrator().ComputeNonlinearResidual(*(this->GetProblem()), tmp, true);
1929  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
1930  temp_q.UnLockCopy();
1931  }
1932  this->GetControlIntegrator().DeleteDomainData("adjoint_hessian");
1933  temp_q.GetSpacialVector() -= tmp;
1934  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1935  }
1936  }//End stationary
1937  else if(this->GetProblem()->GetSpaceTimeHandler()->GetControlType() == DOpEtypes::ControlType::nonstationary)
1938  {
1939  std::stringstream out;
1940  this->GetOutputHandler()->InitOut(out);
1941  out << "\t Precalculating hessian values ";
1942  this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1943 
1944  this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1945  temp_q.SetTimeDoFNumber(local_to_global[j]);
1946  temp_q_trans.SetTimeDoFNumber(local_to_global[j]);
1947  this->GetControlIntegrator().AddDomainData("adjoint_hessian",&(sol.GetSpacialVector()));
1948  temp_q_trans.GetSpacialVector() = 0.;
1949 
1950  if (dopedim == dealdim)
1951  {
1952  this->GetControlIntegrator().AddDomainData("last_newton_solution",
1953  &(temp_q_trans.GetSpacialVector()));
1954  this->GetControlIntegrator().ComputeNonlinearResidual(
1955  *(this->GetProblem()), temp_q.GetSpacialVector(),
1956  true);
1957  this->GetControlIntegrator().DeleteDomainData("last_newton_solution");
1958  }
1959  else if (dopedim == 0)
1960  {
1961  this->GetControlIntegrator().AddParamData("last_newton_solution",
1962  &(temp_q_trans.GetSpacialVectorCopy()));
1963  this->GetControlIntegrator().ComputeNonlinearResidual(
1964  *(this->GetProblem()), temp_q.GetSpacialVector(),
1965  true);
1966  this->GetControlIntegrator().DeleteParamData("last_newton_solution");
1967  temp_q_trans.UnLockCopy();
1968  }
1969 
1970  temp_q.GetSpacialVector() *= -1.;
1971  //Prescale with inverse of time step size to anticipate the time-scalar product.
1972  temp_q_trans.GetSpacialVector().equ(1./problem.GetSpaceTimeHandler()->GetStepSize(),temp_q.GetSpacialVector());
1973 
1974  //Compute l^2 representation of the HessianVector
1975  //hessian Matrix is the same as control matrix
1976  build_control_matrix_ =
1977  this->GetControlNonlinearSolver().NonlinearSolve(
1978  *(this->GetProblem()),
1979  temp_q_trans.GetSpacialVector(), true,
1980  build_control_matrix_);
1981 
1982  this->GetOutputHandler()->Write(temp_q.GetSpacialVector(),
1983  "HessianDirection" + this->GetPostIndex(),
1984  this->GetProblem()->GetDoFType());
1985  this->GetOutputHandler()->Write(temp_q_trans.GetSpacialVector(),
1986  "HessianDirection_Transposed" + this->GetPostIndex(),
1987  this->GetProblem()->GetDoFType());
1988 
1989  this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1990  this->GetControlIntegrator().DeleteDomainData("adjoint_hessian");
1991  }//End nonstationary
1992  else
1993  {
1994  throw DOpEException("Unknown ControlType: "+DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType())+". In case Hessian.", "InstatReducedProblem::BackwardTimeLoop");
1995  }
1996  this->SetProblemType("adjoint_hessian");
1997  }//Endof Hessian case
1998  else
1999  {
2000  throw DOpEException("Unknown type "+outname,"InstatReducedProblem::BackwardTimeLoop");
2001  }
2002  }
2003  }//End interval loop
2004  }//End time loop
2005  }
2007 }
2008 #endif
CONTROLNONLINEARSOLVER & GetControlNonlinearSolver()
Definition: instatreducedproblem.h:520
void UnLockCopy() const
Definition: controlvector.h:199
Definition: constraintvector.h:48
StateVector< VECTOR > & GetDU()
Definition: instatreducedproblem.h:291
const dealii::Vector< double > & GetSpacialVectorCopy() const
Definition: controlvector.cc:276
void GetControlBoxConstraints(ControlVector< VECTOR > &lb, ControlVector< VECTOR > &ub)
Definition: instatreducedproblem.h:606
Definition: dopetypes.h:106
VECTOR & GetSpacialVector()
Definition: statevector.cc:383
void ReSizeVector(unsigned int ndofs, const std::vector< unsigned int > &dofs_per_block, dealii::BlockVector< double > &vector)
Definition: helper.cc:30
InstatReducedProblem(PROBLEM *OP, DOpEtypes::VectorStorageType state_behavior, ParameterReader &param_reader, INTEGRATORDATACONT &idc, int base_priority=0)
Definition: instatreducedproblem.h:423
Definition: parameterreader.h:36
StateVector< VECTOR > & GetU()
Definition: instatreducedproblem.h:283
Definition: timeiterator.h:62
Definition: dopetypes.h:105
Definition: controlvector.h:49
void ComputeReducedHessianVector(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &direction, ControlVector< VECTOR > &hessian_direction, ControlVector< VECTOR > &hessian_direction_transposed)
Definition: instatreducedproblem.h:986
void BackwardTimeLoop(PDE &problem, StateVector< VECTOR > &sol, ControlVector< VECTOR > &temp_q, ControlVector< VECTOR > &temp_q_trans, std::string outname, bool eval_grads)
Definition: instatreducedproblem.h:1657
StateVector< VECTOR > & GetZ()
Definition: instatreducedproblem.h:287
void SetTimeDoFNumber(unsigned int time_point) const
Definition: controlvector.cc:161
INTEGRATOR & GetIntegrator()
Definition: instatreducedproblem.h:302
Definition: solutionextractor.h:40
Definition: dopetypes.h:107
Definition: instatreducedproblem.h:81
void WriteToFile(const VECTOR &v, std::string name, std::string outfile, std::string dof_type, std::string filetype)
Definition: instatreducedproblem.h:1385
virtual ~InstatReducedProblem()
Definition: instatreducedproblem.h:487
void ComputeReducedFunctionals(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:918
void StateSizeInfo(std::stringstream &out)
Definition: instatreducedproblem.h:235
void ReInit()
Definition: instatreducedproblem.h:539
const SpaceTimeHandlerBase< VECTOR > * GetSpaceTimeHandler() const
Definition: controlvector.h:215
void ForwardTimeLoop(PDE &problem, StateVector< VECTOR > &sol, std::string outname, bool eval_funcs)
Definition: instatreducedproblem.h:1519
void get_time_dof_indices(std::vector< unsigned int > &local_dof_indices) const
Definition: timeiterator.h:207
const StateVector< VECTOR > & GetU() const
Definition: instatreducedproblem.h:279
Definition: statevector.h:50
void ComputeReducedGradient(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)
Definition: instatreducedproblem.h:643
VECTOR & GetSpacialVector()
******************************************************/
Definition: controlvector.cc:204
void ComputeReducedAdjoint(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &temp_q, ControlVector< VECTOR > &temp_q_trans)
Definition: instatreducedproblem.h:617
void ComputeTimeFunctionals(unsigned int step, unsigned int num_steps)
Definition: instatreducedproblem.h:1209
static void declare_params(ParameterReader &param_reader)
Definition: instatreducedproblem.h:411
VectorStorageType
Definition: dopetypes.h:120
NONLINEARSOLVER & GetNonlinearSolver(std::string type)
Definition: instatreducedproblem.h:497
Definition: reducedprobleminterface.h:335
double ComputeReducedCostFunctional(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:893
bool ComputeReducedConstraints(const ControlVector< VECTOR > &q, ConstraintVector< VECTOR > &g)
Definition: instatreducedproblem.h:593
void SetTimeDoFNumber(unsigned int dof_number, const TimeIterator &interval) const
Definition: statevector.cc:263
StateVector< VECTOR > & GetDZ()
Definition: instatreducedproblem.h:295
void ComputeRefinementIndicators(DWRC &)
Definition: instatreducedproblem.h:222
Definition: dopeexception.h:35
CONTROLINTEGRATOR & GetControlIntegrator()
Definition: instatreducedproblem.h:306
std::string DOpEtypesToString(const C &)
Definition: dopetypes.h:134
virtual void ReInit()
Definition: reducedprobleminterface.h:357
void ComputeReducedState(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:568