24 #ifndef INSTAT_REDUCED_PROBLEM_H_
25 #define INSTAT_REDUCED_PROBLEM_H_
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>
79 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
80 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
int dealdim>
96 template<
typename INTEGRATORDATACONT>
99 INTEGRATORDATACONT& idc,
100 int base_priority = 0);
116 template<
typename STATEINTEGRATORDATACONT,
typename CONTROLINTEGRATORCONT>
119 STATEINTEGRATORDATACONT & s_idc,
int base_priority = 0);
225 "InstatReducedProblem::ComputeRefinementIndicators");
237 GetU().PrintInfos(out);
252 void WriteToFile(
const VECTOR &v, std::string name, std::string outfile,
253 std::string dof_type, std::string filetype);
276 void WriteToFile(
const std::vector<double> &v, std::string outfile);
308 return control_integrator_;
360 template<
typename PDE>
376 template<
typename PDE>
386 INTEGRATOR integrator_;
387 CONTROLINTEGRATOR control_integrator_;
388 NONLINEARSOLVER nonlinear_state_solver_;
389 NONLINEARSOLVER nonlinear_adjoint_solver_;
390 CONTROLNONLINEARSOLVER nonlinear_gradient_solver_;
392 bool build_state_matrix_, build_adjoint_matrix_, build_control_matrix_;
393 bool state_reinit_, adjoint_reinit_, gradient_reinit_;
395 bool project_initial_data_;
398 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR,dopedim, dealdim>, VECTOR > ;
404 using namespace dealii;
407 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
408 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
410 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
411 PROBLEM, VECTOR, dopedim, dealdim>::declare_params(
414 NONLINEARSOLVER::declare_params(param_reader);
418 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
419 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
420 typename VECTOR,
int dopedim,
int dealdim>
421 template<
typename INTEGRATORDATACONT>
427 INTEGRATORDATACONT& idc,
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),
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)
443 state_reinit_ =
true;
444 adjoint_reinit_ =
true;
445 gradient_reinit_ =
true;
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>
458 CONTROLINTEGRATORCONT& c_idc,
459 STATEINTEGRATORDATACONT & s_idc,
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),
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)
475 state_reinit_ =
true;
476 adjoint_reinit_ =
true;
477 gradient_reinit_ =
true;
483 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
484 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
487 PROBLEM, VECTOR, dopedim, dealdim>::~InstatReducedProblem()
493 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
494 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
496 NONLINEARSOLVER&
InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR,
497 INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetNonlinearSolver(std::string type)
499 if ((type ==
"state") || (type ==
"tangent"))
501 return nonlinear_state_solver_;
503 else if ((type ==
"adjoint") || (type ==
"adjoint_hessian"))
505 return nonlinear_adjoint_solver_;
509 throw DOpEException(
"No Solver for Problem type:`" + type +
"' found",
510 "InstatReducedProblem::GetNonlinearSolver");
516 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
517 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
520 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetControlNonlinearSolver()
522 if ((this->GetProblem()->GetType() ==
"gradient") || (this->GetProblem()->GetType() ==
"hessian"))
524 return nonlinear_gradient_solver_;
528 throw DOpEException(
"No Solver for Problem type:`" + this->GetProblem()->GetType() +
"' found",
529 "InstatReducedProblem::GetControlNonlinearSolver");
535 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
536 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
538 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
539 PROBLEM, VECTOR, dopedim, dealdim>::ReInit()
546 state_reinit_ =
true;
547 adjoint_reinit_ =
true;
548 gradient_reinit_ =
true;
551 build_state_matrix_ =
true;
552 build_adjoint_matrix_ =
true;
559 build_control_matrix_ =
true;
564 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
565 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
567 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
570 this->InitializeFunctionalValues(this->GetProblem()->GetNFunctionals() + 1);
572 this->GetOutputHandler()->Write(
"Computing State Solution:", 4 + this->GetBasePriority());
574 this->SetProblemType(
"state");
575 auto& problem = this->GetProblem()->GetStateProblem();
577 if (state_reinit_ ==
true)
579 GetNonlinearSolver(
"state").ReInit(problem);
580 state_reinit_ =
false;
583 this->GetProblem()->AddAuxiliaryControl(&q,
"control");
584 this->ForwardTimeLoop(problem,this->GetU(),
"State",
true);
585 this->GetProblem()->DeleteAuxiliaryControl(
"control");
589 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
590 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
592 bool InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
593 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedConstraints(
602 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
603 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
605 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
613 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
614 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
616 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
617 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedAdjoint(
620 this->GetOutputHandler()->Write(
"Computing Adjoint Solution:", 4 + this->GetBasePriority());
622 this->SetProblemType(
"adjoint");
623 auto& problem = this->GetProblem()->GetAdjointProblem();
624 if (adjoint_reinit_ ==
true)
626 GetNonlinearSolver(
"adjoint").ReInit(problem);
627 adjoint_reinit_ =
false;
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");
639 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
640 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
642 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
643 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedGradient(
652 this->ComputeReducedAdjoint(q,gradient,gradient_transposed);
654 this->GetOutputHandler()->Write(
"Computing Reduced Gradient:",
655 4 + this->GetBasePriority());
656 if (this->GetProblem()->HasControlInDirichletData())
658 throw DOpEException(
"Control in Dirichlet Data for instationary problems not yet implemented!"
659 ,
"InstatReducedProblem::ComputeReducedGradient");
662 this->SetProblemType(
"gradient");
663 if (gradient_reinit_ ==
true)
665 GetControlNonlinearSolver().ReInit(*(this->GetProblem()));
666 state_reinit_ =
false;
673 const std::vector<double> times =
674 this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
676 n_dofs_per_interval =
677 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
678 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
681 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
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);
691 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
693 if (dopedim == dealdim)
695 this->GetControlIntegrator().AddDomainData(
"control",
698 else if (dopedim == 0)
700 this->GetControlIntegrator().AddParamData(
"control",
706 "InstatReducedProblem::ComputeReducedGradient");
709 this->GetControlIntegrator().AddDomainData(
"state",
710 &(GetU().GetSpacialVector()));
711 this->GetControlIntegrator().AddDomainData(
"adjoint",
712 &(GetZ().GetSpacialVector()));
713 gradient_transposed = 0.;
714 if (dopedim == dealdim)
716 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
718 this->GetControlIntegrator().ComputeNonlinearResidual(
720 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
722 else if (dopedim == 0)
724 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
726 this->GetControlIntegrator().ComputeNonlinearResidual(
729 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
734 gradient_transposed = gradient;
738 build_control_matrix_ = this->GetControlNonlinearSolver().NonlinearSolve(
740 build_control_matrix_);
741 if (dopedim == dealdim)
743 this->GetControlIntegrator().DeleteDomainData(
"control");
745 else if (dopedim == 0)
747 this->GetControlIntegrator().DeleteParamData(
"control");
753 "InstatReducedProblem::ComputeReducedGradient");
755 this->GetControlIntegrator().DeleteDomainData(
"state");
756 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
758 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
759 this->GetControlIntegrator());
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());
771 const std::vector<double> times =
772 this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
774 n_dofs_per_interval =
775 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
776 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
779 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
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);
793 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
795 if (dopedim == dealdim)
797 this->GetControlIntegrator().AddDomainData(
"control",
799 this->GetControlIntegrator().AddDomainData(
"fixed_rhs",&tmp.
GetSpacialVector());
801 else if (dopedim == 0)
803 this->GetControlIntegrator().AddParamData(
"control",
805 this->GetControlIntegrator().AddParamData(
"fixed_rhs",
811 "InstatReducedProblem::ComputeReducedGradient");
814 this->GetControlIntegrator().AddDomainData(
"state",
815 &(GetU().GetSpacialVector()));
816 this->GetControlIntegrator().AddDomainData(
"adjoint",
817 &(GetZ().GetSpacialVector()));
818 gradient_transposed = 0.;
819 if (dopedim == dealdim)
821 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
823 this->GetControlIntegrator().ComputeNonlinearResidual(
825 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
827 else if (dopedim == 0)
829 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
831 this->GetControlIntegrator().ComputeNonlinearResidual(
834 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
839 gradient_transposed = gradient;
843 build_control_matrix_ = this->GetControlNonlinearSolver().NonlinearSolve(
845 build_control_matrix_);
846 if (dopedim == dealdim)
848 this->GetControlIntegrator().DeleteDomainData(
"control");
849 this->GetControlIntegrator().DeleteDomainData(
"fixed_rhs");
851 else if (dopedim == 0)
853 this->GetControlIntegrator().DeleteParamData(
"control");
855 this->GetControlIntegrator().DeleteParamData(
"fixed_rhs");
861 "InstatReducedProblem::ComputeReducedGradient");
863 this->GetControlIntegrator().DeleteDomainData(
"state");
864 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
866 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
867 this->GetControlIntegrator());
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());
881 std::stringstream out;
882 out <<
"Unknown ControlType: "<<
DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType());
883 throw DOpEException(out.str(),
"InstatReducedProblem::ComputeReducedGradient");
889 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
890 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
892 double InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
893 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedCostFunctional(
896 this->ComputeReducedState(q);
898 if (this->GetFunctionalValues()[0].size() != 1)
900 if (this->GetFunctionalValues()[0].size() == 0)
902 "Apparently the CostFunctional was never evaluated! \n\tCheck if the return value of `NeedTimes' is set correctly.",
903 "InstatReducedProblem::ComputeReducedCostFunctional");
906 "The CostFunctional has been evaluated too many times! \n\tCheck if the return value of `NeedTimes' is set correctly.",
907 "InstatReducedProblem::ComputeReducedCostFunctional");
909 return this->GetFunctionalValues()[0][0];
914 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
915 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
917 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
918 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedFunctionals(
922 this->GetOutputHandler()->Write(
"Computing Functionals:" + this->GetBasePriority(), 4);
924 for (
unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
926 this->SetProblemType(
"aux_functional", i);
927 if (this->GetProblem()->GetFunctionalType().find(
"timelocal") != std::string::npos)
929 if (this->GetFunctionalValues()[i + 1].size() == 1)
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());
936 else if (this->GetFunctionalValues()[i + 1].size() > 1)
938 if (this->GetFunctionalValues()[i + 1].size()
939 == this->GetProblem()->GetSpaceTimeHandler()->GetMaxTimePoint() + 1)
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");
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());
961 throw DOpEException(
"Functional: " + this->GetProblem()->GetFunctionalType()
962 +
" was not evaluated ever!",
"InstatReducedProblem::ComputeFunctionals");
965 else if (this->GetProblem()->GetFunctionalType().find(
"timedistributed") != std::string::npos)
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());
974 throw DOpEException(
"Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
975 "InstatReducedProblem::ComputeFunctionals");
982 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
983 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
985 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
986 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedHessianVector(
992 this->GetOutputHandler()->Write(
"Computing ReducedHessianVector:",
993 4 + this->GetBasePriority());
996 hessian_direction = 0.;
1000 this->GetOutputHandler()->Write(
"\tSolving Tangent:",
1001 5 + this->GetBasePriority());
1002 this->SetProblemType(
"tangent");
1003 auto& problem = this->GetProblem()->GetTangentProblem();
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");
1010 this->ForwardTimeLoop(problem,this->GetDU(),
"Tangent",
false);
1015 this->GetOutputHandler()->Write(
"\tSolving Adjoint Hessian:",
1016 5 + this->GetBasePriority());
1017 this->SetProblemType(
"adjoint_hessian");
1018 auto& problem = this->GetProblem()->GetAdjointHessianProblem();
1020 this->GetProblem()->AddAuxiliaryState(&(this->GetDU()),
"tangent");
1022 this->BackwardTimeLoop(problem,this->GetDZ(),hessian_direction,hessian_direction_transposed,
"Hessian",
true);
1024 if (this->GetProblem()->HasControlInDirichletData())
1026 throw DOpEException(
"Control in Dirichlet Data for instationary problems not yet implemented!"
1027 ,
"InstatReducedProblem::ComputeReducedHessianVector");
1032 this->GetOutputHandler()->Write(
1033 "\tComputing Representation of the Hessian:",
1034 5 + this->GetBasePriority());
1035 this->SetProblemType(
"hessian");
1037 this->GetProblem()->AddAuxiliaryState(&(this->GetDZ()),
"adjoint_hessian");
1042 const std::vector<double> times =
1043 this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
1045 n_dofs_per_interval =
1046 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1047 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1050 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
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);
1062 this->GetProblem()->AddAuxiliaryToIntegrator(
1063 this->GetControlIntegrator());
1065 hessian_direction_transposed = 0.;
1066 if (dopedim == dealdim)
1068 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
1070 this->GetControlIntegrator().ComputeNonlinearResidual(
1073 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1075 else if (dopedim == 0)
1077 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
1079 this->GetControlIntegrator().ComputeNonlinearResidual(
1082 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
1086 hessian_direction *= -1.;
1087 hessian_direction_transposed = hessian_direction;
1090 build_control_matrix_ =
1091 this->GetControlNonlinearSolver().NonlinearSolve(
1092 *(this->GetProblem()),
1094 build_control_matrix_);
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());
1103 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1104 this->GetControlIntegrator());
1109 const std::vector<double> times =
1112 n_dofs_per_interval =
1113 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1114 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1117 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
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);
1132 this->GetProblem()->AddAuxiliaryToIntegrator(
1133 this->GetControlIntegrator());
1135 hessian_direction_transposed = 0.;
1136 if (dopedim == dealdim)
1138 this->GetControlIntegrator().AddDomainData(
"fixed_rhs",&tmp.
GetSpacialVector());
1139 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
1141 this->GetControlIntegrator().ComputeNonlinearResidual(
1144 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1145 this->GetControlIntegrator().DeleteDomainData(
"fixed_rhs");
1147 else if (dopedim == 0)
1149 this->GetControlIntegrator().AddParamData(
"fixed_rhs",
1151 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
1153 this->GetControlIntegrator().ComputeNonlinearResidual(
1156 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
1158 this->GetControlIntegrator().DeleteParamData(
"fixed_rhs");
1162 hessian_direction *= -1.;
1163 hessian_direction_transposed = hessian_direction;
1166 build_control_matrix_ =
1167 this->GetControlNonlinearSolver().NonlinearSolve(
1168 *(this->GetProblem()),
1170 build_control_matrix_);
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());
1179 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1180 this->GetControlIntegrator());
1188 std::stringstream out;
1189 out <<
"Unknown ControlType: "<<
DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType());
1190 throw DOpEException(out.str(),
"InstatReducedProblem::ComputeReducedHessianVector");
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");
1205 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
1206 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
1208 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1209 PROBLEM, VECTOR, dopedim, dealdim>::ComputeTimeFunctionals(
unsigned int step,
unsigned int num_steps)
1211 std::stringstream out;
1212 this->GetOutputHandler()->InitOut(out);
1213 out <<
"\t Precalculating functional values ";
1214 this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1216 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1218 this->GetIntegrator().AddDomainData(
"state", &(GetU().GetSpacialVector()));
1222 this->SetProblemType(
"cost_functional");
1223 if (this->GetProblem()->NeedTimeFunctional())
1225 if (this->GetProblem()->GetFunctionalType().find(
"domain") != std::string::npos)
1228 ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1230 if (this->GetProblem()->GetFunctionalType().find(
"point") != std::string::npos)
1233 ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1235 if (this->GetProblem()->GetFunctionalType().find(
"boundary") != std::string::npos)
1238 ret += this->GetIntegrator().ComputeBoundaryScalar(*(this->GetProblem()));
1240 if (this->GetProblem()->GetFunctionalType().find(
"face") != std::string::npos)
1243 ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1248 throw DOpEException(
"Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1249 "InstatReducedProblem::ComputeTimeFunctionals");
1252 if (this->GetProblem()->GetFunctionalType().find(
"timelocal") != std::string::npos)
1254 if (this->GetFunctionalValues()[0].size() != 1)
1256 this->GetFunctionalValues()[0].resize(1);
1257 this->GetFunctionalValues()[0][0] = 0.;
1259 this->GetFunctionalValues()[0][0] += ret;
1261 else if (this->GetProblem()->GetFunctionalType().find(
"timedistributed") != std::string::npos)
1263 if (this->GetFunctionalValues()[0].size() != 1)
1265 this->GetFunctionalValues()[0].resize(1);
1266 this->GetFunctionalValues()[0][0] = 0.;
1271 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1272 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1274 else if (step == num_steps)
1276 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1277 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
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));
1286 this->GetFunctionalValues()[0][0] += w * ret;
1290 throw DOpEException(
"Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1291 "InstatReducedProblem::ComputeTimeFunctionals");
1296 for (
unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
1300 this->SetProblemType(
"aux_functional", i);
1301 if (this->GetProblem()->NeedTimeFunctional())
1303 if (this->GetProblem()->GetFunctionalType().find(
"domain") != std::string::npos)
1306 ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1308 if (this->GetProblem()->GetFunctionalType().find(
"point") != std::string::npos)
1311 ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1313 if (this->GetProblem()->GetFunctionalType().find(
"boundary") != std::string::npos)
1316 ret += this->GetIntegrator().ComputeBoundaryScalar(*(this->GetProblem()));
1318 if (this->GetProblem()->GetFunctionalType().find(
"face") != std::string::npos)
1321 ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1327 "Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1328 "InstatReducedProblem::ComputeTimeFunctionals");
1331 if (this->GetProblem()->GetFunctionalType().find(
"timelocal") != std::string::npos)
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);
1339 else if (this->GetProblem()->GetFunctionalType().find(
"timedistributed") != std::string::npos)
1341 if (this->GetFunctionalValues()[i + 1].size() != 1)
1343 this->GetFunctionalValues()[i + 1].resize(1);
1344 this->GetFunctionalValues()[i + 1][0] = 0.;
1349 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1350 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1352 else if (step == num_steps)
1354 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1355 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
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));
1364 this->GetFunctionalValues()[i + 1][0] += w * ret;
1369 "Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1370 "InstatReducedProblem::ComputeTimeFunctionals");
1375 this->GetIntegrator().DeleteDomainData(
"state");
1376 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1381 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
1382 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
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)
1387 if (dof_type ==
"state")
1389 auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1390 data_out.attach_dof_handler(this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFHandler());
1392 data_out.add_data_vector(v, name);
1393 data_out.build_patches();
1395 std::ofstream output(outfile.c_str());
1397 if (filetype ==
".vtk")
1399 data_out.write_vtk(output);
1401 else if (filetype ==
".gpl")
1403 data_out.write_gnuplot(output);
1407 throw DOpEException(
"Don't know how to write filetype `" + filetype +
"'!",
1408 "InstatReducedProblem::WriteToFile");
1412 else if (dof_type ==
"control")
1414 #if dope_dimension >0
1415 auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1416 data_out.attach_dof_handler (this->GetProblem()->GetSpaceTimeHandler()->GetControlDoFHandler());
1418 data_out.add_data_vector (v,name);
1419 data_out.build_patches ();
1421 std::ofstream output(outfile.c_str());
1423 if(filetype ==
".vtk")
1425 data_out.write_vtk (output);
1427 else if(filetype ==
".gpl")
1429 data_out.write_gnuplot (output);
1433 throw DOpEException(
"Don't know how to write filetype `" + filetype +
"'!",
"InstatReducedProblem::WriteToFile");
1437 std::ofstream output(outfile.c_str());
1440 for (
unsigned int i = 0; i < off.size(); i++)
1442 output << off(i) << std::endl;
1448 throw DOpEException(
"No such DoFHandler `" + dof_type +
"'!",
1449 "InstatReducedProblem::WriteToFile");
1455 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
1456 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
1458 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1461 std::string dof_type)
1471 unsigned int maxt = this->GetProblem()->GetSpaceTimeHandler()->GetMaxTimePoint();
1473 for(
unsigned int i = 0; i <= maxt; i++)
1475 this->GetOutputHandler()->SetIterationNumber(i,
"Time");
1482 throw DOpEException(
"Unknown ControlType: "+
DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType()),
"InstatReducedProblem::WriteToFile");
1488 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1489 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1490 typename VECTOR,
int dopedim,
int dealdim>
1493 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(
1494 const std::vector<double> &v, std::string outfile)
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());
1503 out <<
"#Time\tvalue" << std::endl;
1504 for (
unsigned int i = 0; i < v.size(); i++)
1506 out << t[i] <<
"\t" << v[i] << std::endl;
1513 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1514 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1515 typename VECTOR,
int dopedim,
int dealdim>
1516 template<
typename PDE>
1518 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::
1523 unsigned int max_timestep =
1524 problem.GetSpaceTimeHandler()->GetMaxTimePoint();
1525 const std::vector<double> times =
1526 problem.GetSpaceTimeHandler()->GetTimes();
1528 n_dofs_per_interval =
1529 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1530 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1533 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
1535 problem.SetTime(times[local_to_global[0]], local_to_global[0], it,
true);
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++)
1547 n_dofs += dofs_per_block[i];
1554 this->GetOutputHandler()->SetIterationNumber(0,
"Time");
1556 this->GetOutputHandler()->Write(
"Computing Initial Values:",
1557 4 + this->GetBasePriority());
1559 auto& initial_problem = problem.GetInitialProblem();
1560 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1563 build_state_matrix_ = this->GetNonlinearSolver(
"state").NonlinearSolve_Initial(
1564 initial_problem, u_alt,
true,
true);
1565 build_state_matrix_ =
true;
1567 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1571 this->GetOutputHandler()->Write(u_alt, outname + this->GetPostIndex(),
1572 problem.GetDoFType());
1577 ComputeTimeFunctionals(0,
1579 this->SetProblemType(
"state");
1584 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval(); it
1585 != problem.GetSpaceTimeHandler()->GetTimeDoFHandler().after_last_interval(); ++it)
1587 it.get_time_dof_indices(local_to_global);
1588 problem.SetTime(times[local_to_global[0]], local_to_global[0], it);
1594 for (
unsigned int i = 1; i < n_dofs_per_interval; i++)
1596 this->GetOutputHandler()->SetIterationNumber(local_to_global[i],
1598 double time = times[local_to_global[i]];
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());
1611 this->GetProblem()->AddAuxiliaryToIntegrator(
1612 this->GetIntegrator());
1614 this->GetNonlinearSolver(
"state").NonlinearLastTimeEvals(problem,
1617 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1618 this->GetIntegrator());
1620 problem.SetTime(time, local_to_global[i], it);
1621 this->GetProblem()->AddAuxiliaryToIntegrator(
1622 this->GetIntegrator());
1623 this->GetProblem()->AddPreviousAuxiliaryToIntegrator(
1624 this->GetIntegrator());
1627 = this->GetNonlinearSolver(
"state").NonlinearSolve(problem,
1629 build_state_matrix_);
1631 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1632 this->GetIntegrator());
1633 this->GetProblem()->DeletePreviousAuxiliaryFromIntegrator(
1634 this->GetIntegrator());
1639 outname + this->GetPostIndex(), problem.GetDoFType());
1642 ComputeTimeFunctionals(local_to_global[i], max_timestep);
1643 this->SetProblemType(
"state");
1651 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1652 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1653 typename VECTOR,
int dopedim,
int dealdim>
1654 template<
typename PDE>
1656 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::
1661 unsigned int max_timestep =
1662 problem.GetSpaceTimeHandler()->GetMaxTimePoint();
1663 const std::vector<double> times =
1664 problem.GetSpaceTimeHandler()->GetTimes();
1666 n_dofs_per_interval =
1667 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1668 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1671 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().last_interval();
1674 problem.SetTime(times[local_to_global[local_to_global.size()-1]],local_to_global[local_to_global.size()-1], it);
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++)
1686 n_dofs += dofs_per_block[i];
1692 this->GetOutputHandler()->SetIterationNumber(max_timestep,
"Time");
1694 this->GetOutputHandler()->Write(
"Computing Initial Values:",
1695 4 + this->GetBasePriority());
1697 auto& initial_problem = problem.GetInitialProblem();
1698 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1699 this->GetProblem()->AddPreviousAuxiliaryToIntegrator(this->GetIntegrator());
1702 build_state_matrix_ = this->GetNonlinearSolver(
"adjoint").NonlinearSolve_Initial(
1703 initial_problem, u_alt,
true,
true);
1704 build_state_matrix_ =
true;
1706 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1707 this->GetProblem()->DeletePreviousAuxiliaryFromIntegrator(this->GetIntegrator());
1711 this->GetOutputHandler()->Write(u_alt, outname + this->GetPostIndex(),
1712 problem.GetDoFType());
1717 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().last_interval(); it
1718 != problem.GetSpaceTimeHandler()->GetTimeDoFHandler().before_first_interval(); --it)
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);
1727 for (
unsigned int i = 1; i < n_dofs_per_interval; i++)
1729 unsigned int j = n_dofs_per_interval-1-i;
1730 this->GetOutputHandler()->SetIterationNumber(local_to_global[j],
1732 double time = times[local_to_global[j]];
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());
1745 this->GetProblem()->AddAuxiliaryToIntegrator(
1746 this->GetIntegrator());
1748 this->GetNonlinearSolver(
"adjoint").NonlinearLastTimeEvals(problem,
1751 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1752 this->GetIntegrator());
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());
1763 build_adjoint_matrix_
1764 = this->GetNonlinearSolver(
"adjoint").NonlinearSolve(problem,
1766 build_adjoint_matrix_);
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());
1779 outname + this->GetPostIndex(), problem.GetDoFType());
1784 if(outname ==
"Adjoint")
1786 this->SetProblemType(
"gradient");
1793 std::stringstream out;
1794 this->GetOutputHandler()->InitOut(out);
1795 out <<
"\t Precalculating gradient values ";
1796 this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1798 if(local_to_global[j] != 0)
1803 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1805 this->GetControlIntegrator().AddDomainData(
"adjoint",&(sol.
GetSpacialVector()));
1808 if (dopedim == dealdim)
1813 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",&tmp_2);
1814 this->GetControlIntegrator().ComputeNonlinearResidual(*(this->GetProblem()), tmp,
true);
1815 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1817 else if (dopedim == 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");
1827 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
1829 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1834 std::stringstream out;
1835 this->GetOutputHandler()->InitOut(out);
1836 out <<
"\t Precalculating gradient values ";
1837 this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1840 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1843 this->GetControlIntegrator().AddDomainData(
"adjoint",&(sol.
GetSpacialVector()));
1846 if (dopedim == dealdim)
1848 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
1850 this->GetControlIntegrator().ComputeNonlinearResidual(
1852 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1854 else if (dopedim == 0)
1856 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
1858 this->GetControlIntegrator().ComputeNonlinearResidual(
1861 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
1869 build_control_matrix_ = this->GetControlNonlinearSolver().NonlinearSolve(
1871 build_control_matrix_);
1873 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
1875 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1878 "Gradient" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
1880 "Gradient_Transposed" + this->GetPostIndex(),
1881 this->GetProblem()->GetDoFType());
1885 throw DOpEException(
"Unknown ControlType: "+
DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType())+
". In case Adjoint.",
"InstatReducedProblem::BackwardTimeLoop");
1887 this->SetProblemType(
"adjoint");
1889 else if (outname ==
"Hessian")
1891 this->SetProblemType(
"hessian");
1898 std::stringstream out;
1899 this->GetOutputHandler()->InitOut(out);
1900 out <<
"\t Precalculating hessian values ";
1901 this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1903 if(local_to_global[j] != 0)
1908 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1910 this->GetControlIntegrator().AddDomainData(
"adjoint_hessian",&(sol.
GetSpacialVector()));
1913 if (dopedim == dealdim)
1918 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",&tmp_2);
1919 this->GetControlIntegrator().ComputeNonlinearResidual(*(this->GetProblem()), tmp,
true);
1920 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1922 else if (dopedim == 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");
1932 this->GetControlIntegrator().DeleteDomainData(
"adjoint_hessian");
1934 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1939 std::stringstream out;
1940 this->GetOutputHandler()->InitOut(out);
1941 out <<
"\t Precalculating hessian values ";
1942 this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1944 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
1947 this->GetControlIntegrator().AddDomainData(
"adjoint_hessian",&(sol.
GetSpacialVector()));
1950 if (dopedim == dealdim)
1952 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
1954 this->GetControlIntegrator().ComputeNonlinearResidual(
1957 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1959 else if (dopedim == 0)
1961 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
1963 this->GetControlIntegrator().ComputeNonlinearResidual(
1966 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
1976 build_control_matrix_ =
1977 this->GetControlNonlinearSolver().NonlinearSolve(
1978 *(this->GetProblem()),
1980 build_control_matrix_);
1983 "HessianDirection" + this->GetPostIndex(),
1984 this->GetProblem()->GetDoFType());
1986 "HessianDirection_Transposed" + this->GetPostIndex(),
1987 this->GetProblem()->GetDoFType());
1989 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetControlIntegrator());
1990 this->GetControlIntegrator().DeleteDomainData(
"adjoint_hessian");
1994 throw DOpEException(
"Unknown ControlType: "+
DOpEtypesToString(this->GetProblem()->GetSpaceTimeHandler()->GetControlType())+
". In case Hessian.",
"InstatReducedProblem::BackwardTimeLoop");
1996 this->SetProblemType(
"adjoint_hessian");
2000 throw DOpEException(
"Unknown type "+outname,
"InstatReducedProblem::BackwardTimeLoop");
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 ¶m_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: 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 ¶m_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