24 #ifndef _INSTAT_REDUCED_PROBLEM_H_
25 #define _INSTAT_REDUCED_PROBLEM_H_
48 #include <base/data_out_base.h>
49 #include <numerics/data_out.h>
50 #include <numerics/matrix_tools.h>
51 #include <numerics/vector_tools.h>
52 #include <base/function.h>
53 #include <lac/sparse_matrix.h>
54 #include <lac/compressed_simple_sparsity_pattern.h>
55 #include <lac/sparse_direct.h>
56 #include <lac/block_sparsity_pattern.h>
57 #include <lac/block_sparse_matrix.h>
58 #include <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);
268 std::string dof_type, std::string filetype);
279 void WriteToFile(
const std::vector<double> &v, std::string outfile);
311 return _control_integrator;
361 template<
typename PDE>
374 template<
typename PDE>
384 INTEGRATOR _integrator;
385 CONTROLINTEGRATOR _control_integrator;
386 NONLINEARSOLVER _nonlinear_state_solver;
387 NONLINEARSOLVER _nonlinear_adjoint_solver;
388 CONTROLNONLINEARSOLVER _nonlinear_gradient_solver;
390 bool _build_state_matrix, _build_adjoint_matrix, _build_control_matrix;
391 bool _state_reinit, _adjoint_reinit, _gradient_reinit;
393 bool _project_initial_data;
396 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR,dopedim, dealdim>, VECTOR > ;
402 using namespace dealii;
405 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
406 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
408 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
409 PROBLEM, VECTOR, dopedim, dealdim>::declare_params(
412 NONLINEARSOLVER::declare_params(param_reader);
416 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
417 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
418 typename VECTOR,
int dopedim,
int dealdim>
419 template<
typename INTEGRATORDATACONT>
423 std::string state_behavior,
425 INTEGRATORDATACONT& idc,
429 _u(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
430 _z(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
431 _du(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
432 _dz(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
434 _control_integrator(idc),
435 _nonlinear_state_solver(_integrator, param_reader),
436 _nonlinear_adjoint_solver(_integrator, param_reader),
437 _nonlinear_gradient_solver(_control_integrator, param_reader)
441 _state_reinit =
true;
442 _adjoint_reinit =
true;
443 _gradient_reinit =
true;
447 throw DOpEException(
"ControlType: stationary has no meeaning here." ,
"InstatReducedProblem::InstatReducedProblem");
452 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
453 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
454 typename VECTOR,
int dopedim,
int dealdim>
455 template<
typename STATEINTEGRATORDATACONT,
typename CONTROLINTEGRATORCONT>
458 PROBLEM *OP, std::string state_behavior,
460 CONTROLINTEGRATORCONT& c_idc,
461 STATEINTEGRATORDATACONT & s_idc,
465 _u(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
466 _z(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
467 _du(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
468 _dz(OP->GetSpaceTimeHandler(), state_behavior, param_reader),
470 _control_integrator(c_idc),
471 _nonlinear_state_solver(_integrator, param_reader),
472 _nonlinear_adjoint_solver(_integrator, param_reader),
473 _nonlinear_gradient_solver(_control_integrator, param_reader)
477 _state_reinit =
true;
478 _adjoint_reinit =
true;
479 _gradient_reinit =
true;
483 throw DOpEException(
"ControlType: stationary has no meeaning here." ,
"InstatReducedProblem::InstatReducedProblem");
489 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
490 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
493 PROBLEM, VECTOR, dopedim, dealdim>::~InstatReducedProblem()
499 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
500 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
502 NONLINEARSOLVER&
InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR,
503 INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetNonlinearSolver(std::string type)
505 if ((type ==
"state") || (type ==
"tangent"))
507 return _nonlinear_state_solver;
509 else if ((type ==
"adjoint") || (type ==
"adjoint_hessian"))
511 return _nonlinear_adjoint_solver;
515 throw DOpEException(
"No Solver for Problem type:`" + type +
"' found",
516 "InstatReducedProblem::GetNonlinearSolver");
522 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
523 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
526 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetControlNonlinearSolver()
528 if ((this->GetProblem()->GetType() ==
"gradient") || (this->GetProblem()->GetType() ==
"hessian"))
530 return _nonlinear_gradient_solver;
534 throw DOpEException(
"No Solver for Problem type:`" + this->GetProblem()->GetType() +
"' found",
535 "InstatReducedProblem::GetControlNonlinearSolver");
541 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
542 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
544 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
545 PROBLEM, VECTOR, dopedim, dealdim>::ReInit()
552 _state_reinit =
true;
553 _adjoint_reinit =
true;
554 _gradient_reinit =
true;
557 _build_state_matrix =
true;
558 _build_adjoint_matrix =
true;
565 _build_control_matrix =
true;
570 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
571 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
573 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
576 this->InitializeFunctionalValues(this->GetProblem()->GetNFunctionals() + 1);
578 this->GetOutputHandler()->Write(
"Computing State Solution:", 4 + this->GetBasePriority());
580 this->SetProblemType(
"state");
581 auto& problem = this->GetProblem()->GetStateProblem();
583 if (_state_reinit ==
true)
585 GetNonlinearSolver(
"state").ReInit(problem);
586 _state_reinit =
false;
589 this->GetProblem()->AddAuxiliaryControl(&q,
"control");
590 this->ForwardTimeLoop(problem,this->GetU(),
"State",
true);
591 this->GetProblem()->DeleteAuxiliaryControl(
"control");
595 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
596 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
598 bool InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
599 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedConstraints(
608 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
609 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
611 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
619 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
620 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
622 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
623 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedAdjoint(
626 this->GetOutputHandler()->Write(
"Computing Adjoint Solution:", 4 + this->GetBasePriority());
628 this->SetProblemType(
"adjoint");
629 auto& problem = this->GetProblem()->GetAdjointProblem();
630 if (_adjoint_reinit ==
true)
632 GetNonlinearSolver(
"adjoint").ReInit(problem);
633 _adjoint_reinit =
false;
636 this->GetProblem()->AddAuxiliaryState(&(this->GetU()),
"state");
637 this->GetProblem()->AddAuxiliaryControl(&q,
"control");
638 this->BackwardTimeLoop(problem,this->GetZ(),
"Adjoint");
639 this->GetProblem()->DeleteAuxiliaryControl(
"control");
640 this->GetProblem()->DeleteAuxiliaryState(
"state");
645 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
646 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
648 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
649 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedGradient(
654 this->ComputeReducedAdjoint(q);
656 this->GetOutputHandler()->Write(
"Computing Reduced Gradient:",
657 4 + this->GetBasePriority());
658 if (this->GetProblem()->HasControlInDirichletData())
660 throw DOpEException(
"Control in Dirichlet Data for instationary problems not yet implemented!"
661 ,
"InstatReducedProblem::ComputeReducedGradient");
664 this->SetProblemType(
"gradient");
665 if (_gradient_reinit ==
true)
667 GetControlNonlinearSolver().ReInit(*(this->GetProblem()));
668 _state_reinit =
false;
674 const std::vector<double> times =
675 this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
677 n_dofs_per_interval =
678 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
679 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
682 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
684 this->GetProblem()->SetTime(times[local_to_global[0]], it);
685 GetU().SetTimeDoFNumber(local_to_global[0], it);
686 GetZ().SetTimeDoFNumber(local_to_global[0], it);
689 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetControlIntegrator());
691 if (dopedim == dealdim)
693 this->GetControlIntegrator().AddDomainData(
"control",
696 else if (dopedim == 0)
698 this->GetControlIntegrator().AddParamData(
"control",
704 "InstatReducedProblem::ComputeReducedGradient");
707 this->GetControlIntegrator().AddDomainData(
"state",
708 &(GetU().GetSpacialVector()));
709 this->GetControlIntegrator().AddDomainData(
"adjoint",
710 &(GetZ().GetSpacialVector()));
711 gradient_transposed = 0.;
712 if (dopedim == dealdim)
714 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
716 this->GetControlIntegrator().ComputeNonlinearResidual(
718 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
720 else if (dopedim == 0)
722 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
724 this->GetControlIntegrator().ComputeNonlinearResidual(
727 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
732 gradient_transposed = gradient;
736 _build_control_matrix = this->GetControlNonlinearSolver().NonlinearSolve(
738 _build_control_matrix);
739 if (dopedim == dealdim)
741 this->GetControlIntegrator().DeleteDomainData(
"control");
743 else if (dopedim == 0)
745 this->GetControlIntegrator().DeleteParamData(
"control");
751 "InstatReducedProblem::ComputeReducedGradient");
753 this->GetControlIntegrator().DeleteDomainData(
"state");
754 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
756 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
757 this->GetControlIntegrator());
759 this->GetOutputHandler()->Write(gradient,
760 "Gradient" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
761 this->GetOutputHandler()->Write(gradient_transposed,
762 "Gradient_Transposed" + this->GetPostIndex(),
763 this->GetProblem()->GetDoFType());
767 std::stringstream out;
768 out <<
"Unknown ControlType: "<<this->GetProblem()->GetSpaceTimeHandler()->GetControlType();
769 throw DOpEException(out.str(),
"InstatReducedProblem::ComputeReducedGradient");
775 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
776 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
778 double InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
779 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedCostFunctional(
782 this->ComputeReducedState(q);
784 if (this->GetFunctionalValues()[0].size() != 1)
786 if (this->GetFunctionalValues()[0].size() == 0)
788 "Apparently the CostFunctional was never evaluated! \n\tCheck if the return value of `NeedTimes' is set correctly.",
789 "InstatReducedProblem::ComputeReducedCostFunctional");
792 "The CostFunctional has been evaluated too many times! \n\tCheck if the return value of `NeedTimes' is set correctly.",
793 "InstatReducedProblem::ComputeReducedCostFunctional");
795 return this->GetFunctionalValues()[0][0];
800 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
801 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
803 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
804 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedFunctionals(
808 this->GetOutputHandler()->Write(
"Computing Functionals:" + this->GetBasePriority(), 4);
810 for (
unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
812 this->SetProblemType(
"aux_functional", i);
813 if (this->GetProblem()->GetFunctionalType().find(
"timelocal"))
815 if (this->GetFunctionalValues()[i + 1].size() == 1)
817 std::stringstream out;
818 this->GetOutputHandler()->InitOut(out);
819 out << this->GetProblem()->GetFunctionalName() <<
": " << this->GetFunctionalValues()[i + 1][0];
820 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
822 else if (this->GetFunctionalValues()[i + 1].size() > 1)
824 if (this->GetFunctionalValues()[i + 1].size()
825 == this->GetProblem()->GetSpaceTimeHandler()->GetMaxTimePoint() + 1)
827 std::stringstream out;
828 this->GetOutputHandler()->InitOut(out);
829 out << this->GetProblem()->GetFunctionalName() <<
" too large. Writing to file instead: ";
830 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
831 this->GetOutputHandler()->Write(this->GetFunctionalValues()[i + 1],
832 this->GetProblem()->GetFunctionalName()
833 + this->GetPostIndex(),
"time");
837 std::stringstream out;
838 this->GetOutputHandler()->InitOut(out);
839 out << this->GetProblem()->GetFunctionalName() <<
": ";
840 for (
unsigned int k = 0; k < this->GetFunctionalValues()[i + 1].size(); k++)
841 out << this->GetFunctionalValues()[i + 1][k] <<
" ";
842 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
847 throw DOpEException(
"Functional: " + this->GetProblem()->GetFunctionalType()
848 +
" was not evaluated ever!",
"InstatReducedProblem::ComputeFunctionals");
851 else if (this->GetProblem()->GetFunctionalType().find(
"timedistributed"))
853 std::stringstream out;
854 this->GetOutputHandler()->InitOut(out);
855 out << this->GetProblem()->GetFunctionalName() <<
": " << this->GetFunctionalValues()[i + 1][0];
856 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
860 throw DOpEException(
"Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
861 "InstatReducedProblem::ComputeFunctionals");
868 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
869 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
871 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
872 PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedHessianVector(
878 this->GetOutputHandler()->Write(
"Computing ReducedHessianVector:",
879 4 + this->GetBasePriority());
883 this->GetOutputHandler()->Write(
"\tSolving Tangent:",
884 5 + this->GetBasePriority());
885 this->SetProblemType(
"tangent");
886 auto& problem = this->GetProblem()->GetTangentProblem();
888 this->GetProblem()->AddAuxiliaryControl(&q,
"control");
889 this->GetProblem()->AddAuxiliaryControl(&direction,
"dq");
890 this->GetProblem()->AddAuxiliaryState(&(this->GetU()),
"state");
891 this->GetProblem()->AddAuxiliaryState(&(this->GetZ()),
"adjoint");
893 this->ForwardTimeLoop(problem,this->GetDU(),
"Tangent",
false);
898 this->GetOutputHandler()->Write(
"\tSolving Adjoint Hessian:",
899 5 + this->GetBasePriority());
900 this->SetProblemType(
"adjoint_hessian");
901 auto& problem = this->GetProblem()->GetAdjointHessianProblem();
903 this->GetProblem()->AddAuxiliaryState(&(this->GetDU()),
"tangent");
905 this->BackwardTimeLoop(problem,this->GetDZ(),
"Hessian");
907 if (this->GetProblem()->HasControlInDirichletData())
909 throw DOpEException(
"Control in Dirichlet Data for instationary problems not yet implemented!"
910 ,
"InstatReducedProblem::ComputeReducedHessianVector");
915 this->GetOutputHandler()->Write(
916 "\tComputing Representation of the Hessian:",
917 5 + this->GetBasePriority());
918 this->SetProblemType(
"hessian");
920 this->GetProblem()->AddAuxiliaryState(&(this->GetDZ()),
"adjoint_hessian");
925 const std::vector<double> times =
926 this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
928 n_dofs_per_interval =
929 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
930 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
933 this->GetProblem()->GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
935 this->GetProblem()->SetTime(times[local_to_global[0]], it);
936 GetU().SetTimeDoFNumber(local_to_global[0], it);
937 GetZ().SetTimeDoFNumber(local_to_global[0], it);
938 GetDU().SetTimeDoFNumber(local_to_global[0], it);
939 GetDZ().SetTimeDoFNumber(local_to_global[0], it);
942 this->GetProblem()->AddAuxiliaryToIntegrator(
943 this->GetControlIntegrator());
945 hessian_direction_transposed = 0.;
946 if (dopedim == dealdim)
948 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
950 this->GetControlIntegrator().ComputeNonlinearResidual(
953 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
955 else if (dopedim == 0)
957 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
959 this->GetControlIntegrator().ComputeNonlinearResidual(
962 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
966 hessian_direction *= -1.;
967 hessian_direction_transposed = hessian_direction;
970 _build_control_matrix =
971 this->GetControlNonlinearSolver().NonlinearSolve(
972 *(this->GetProblem()),
974 _build_control_matrix);
976 this->GetOutputHandler()->Write(hessian_direction,
977 "HessianDirection" + this->GetPostIndex(),
978 this->GetProblem()->GetDoFType());
979 this->GetOutputHandler()->Write(hessian_direction_transposed,
980 "HessianDirection_Transposed" + this->GetPostIndex(),
981 this->GetProblem()->GetDoFType());
983 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
984 this->GetControlIntegrator());
988 std::stringstream out;
989 out <<
"Unknown ControlType: "<<this->GetProblem()->
GetSpaceTimeHandler()->GetControlType();
990 throw DOpEException(out.str(),
"InstatReducedProblem::ComputeReducedHessianVector");
995 this->GetProblem()->DeleteAuxiliaryControl(
"control");
996 this->GetProblem()->DeleteAuxiliaryControl(
"dq");
997 this->GetProblem()->DeleteAuxiliaryState(
"state");
998 this->GetProblem()->DeleteAuxiliaryState(
"adjoint");
999 this->GetProblem()->DeleteAuxiliaryState(
"tangent");
1000 this->GetProblem()->DeleteAuxiliaryState(
"adjoint_hessian");
1005 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
1006 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
1008 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1009 PROBLEM, VECTOR, dopedim, dealdim>::ComputeTimeFunctionals(
unsigned int step,
unsigned int num_steps)
1012 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1014 this->GetIntegrator().AddDomainData(
"state", &(GetU().GetSpacialVector()));
1018 this->SetProblemType(
"cost_functional");
1019 if (this->GetProblem()->NeedTimeFunctional())
1021 if (this->GetProblem()->GetFunctionalType().find(
"domain") != std::string::npos)
1024 ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1026 if (this->GetProblem()->GetFunctionalType().find(
"point") != std::string::npos)
1029 ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1031 if (this->GetProblem()->GetFunctionalType().find(
"boundary") != std::string::npos)
1034 ret += this->GetIntegrator().ComputeBoundaryScalar(*(this->GetProblem()));
1036 if (this->GetProblem()->GetFunctionalType().find(
"face") != std::string::npos)
1039 ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1044 throw DOpEException(
"Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1045 "InstatReducedProblem::ComputeTimeFunctionals");
1048 if (this->GetProblem()->GetFunctionalType().find(
"timelocal"))
1050 if (this->GetFunctionalValues()[0].size() != 1)
1052 this->GetFunctionalValues()[0].resize(1);
1053 this->GetFunctionalValues()[0][0] = 0.;
1055 this->GetFunctionalValues()[0][0] += ret;
1057 else if (this->GetProblem()->GetFunctionalType().find(
"timedistributed"))
1059 if (this->GetFunctionalValues()[0].size() != 1)
1061 this->GetFunctionalValues()[0].resize(1);
1066 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1067 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1069 else if (step + 1 == num_steps)
1071 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1072 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1076 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1077 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1078 w += 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1079 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1081 this->GetFunctionalValues()[0][0] += w * ret;
1085 throw DOpEException(
"Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1086 "InstatReducedProblem::ComputeTimeFunctionals");
1091 for (
unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
1095 this->SetProblemType(
"aux_functional", i);
1096 if (this->GetProblem()->NeedTimeFunctional())
1098 if (this->GetProblem()->GetFunctionalType().find(
"domain") != std::string::npos)
1101 ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1103 if (this->GetProblem()->GetFunctionalType().find(
"point") != std::string::npos)
1106 ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1108 if (this->GetProblem()->GetFunctionalType().find(
"boundary") != std::string::npos)
1111 ret += this->GetIntegrator().ComputeBoundaryScalar(*(this->GetProblem()));
1113 if (this->GetProblem()->GetFunctionalType().find(
"face") != std::string::npos)
1116 ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1122 "Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1123 "InstatReducedProblem::ComputeTimeFunctionals");
1126 if (this->GetProblem()->GetFunctionalType().find(
"timelocal"))
1128 std::stringstream out;
1129 this->GetOutputHandler()->InitOut(out);
1130 out <<
"\t" << this->GetProblem()->GetFunctionalName() <<
": " << ret;
1131 this->GetOutputHandler()->Write(out, 5 + this->GetBasePriority());
1132 this->GetFunctionalValues()[i + 1].push_back(ret);
1134 else if (this->GetProblem()->GetFunctionalType().find(
"timedistributed"))
1136 if (this->GetFunctionalValues()[i + 1].size() != 1)
1138 this->GetFunctionalValues()[i + 1].resize(1);
1143 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1144 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1146 else if (step + 1 == num_steps)
1148 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1149 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1153 w = 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step + 1)
1154 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step));
1155 w += 0.5 * (this->GetProblem()->GetSpaceTimeHandler()->GetTime(step)
1156 - this->GetProblem()->GetSpaceTimeHandler()->GetTime(step - 1));
1158 this->GetFunctionalValues()[i + 1][0] += w * ret;
1163 "Unknown Functional Type: " + this->GetProblem()->GetFunctionalType(),
1164 "InstatReducedProblem::ComputeTimeFunctionals");
1169 this->GetIntegrator().DeleteDomainData(
"state");
1170 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1175 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
1176 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
1178 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1179 PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(
const VECTOR &v, std::string name, std::string outfile, std::string dof_type, std::string filetype)
1181 if (dof_type ==
"state")
1183 auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1184 data_out.attach_dof_handler(this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFHandler());
1186 data_out.add_data_vector(v, name);
1187 data_out.build_patches();
1189 std::ofstream output(outfile.c_str());
1191 if (filetype ==
".vtk")
1193 data_out.write_vtk(output);
1195 else if (filetype ==
".gpl")
1197 data_out.write_gnuplot(output);
1201 throw DOpEException(
"Don't know how to write filetype `" + filetype +
"'!",
1202 "InstatReducedProblem::WriteToFile");
1206 else if (dof_type ==
"control")
1208 #if dope_dimension >0
1209 auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1210 data_out.attach_dof_handler (this->GetProblem()->GetSpaceTimeHandler()->GetControlDoFHandler());
1212 data_out.add_data_vector (v,name);
1213 data_out.build_patches ();
1215 std::ofstream output(outfile.c_str());
1217 if(filetype ==
".vtk")
1219 data_out.write_vtk (output);
1221 else if(filetype ==
".gpl")
1223 data_out.write_gnuplot (output);
1227 throw DOpEException(
"Don't know how to write filetype `" + filetype +
"'!",
"InstatReducedProblem::WriteToFile");
1231 std::ofstream output(outfile.c_str());
1234 for (
unsigned int i = 0; i < off.size(); i++)
1236 output << off(i) << std::endl;
1242 throw DOpEException(
"No such DoFHandler `" + dof_type +
"'!",
1243 "InstatReducedProblem::WriteToFile");
1249 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
typename CONTROLINTEGRATOR,
1250 typename INTEGRATOR,
typename PROBLEM,
typename VECTOR,
int dopedim,
1252 void InstatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER, CONTROLINTEGRATOR, INTEGRATOR,
1255 std::string outfile,
1256 std::string dof_type,
1257 std::string filetype)
1264 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1265 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1266 typename VECTOR,
int dopedim,
int dealdim>
1269 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(
1270 const std::vector<double> &v, std::string outfile)
1273 const std::vector<double>& t =
1274 this->GetProblem()->GetSpaceTimeHandler()->GetTimes();
1275 std::ofstream out(outfile.c_str());
1276 assert( t.size() == v.size());
1277 assert(out.is_open());
1279 out <<
"#Time\tvalue" << std::endl;
1280 for (
unsigned int i = 0; i < v.size(); i++)
1282 out << t[i] <<
"\t" << v[i] << std::endl;
1289 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1290 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1291 typename VECTOR,
int dopedim,
int dealdim>
1292 template<
typename PDE>
1294 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::
1299 unsigned int max_timestep =
1300 problem.GetSpaceTimeHandler()->GetMaxTimePoint();
1301 const std::vector<double> times =
1302 problem.GetSpaceTimeHandler()->GetTimes();
1304 n_dofs_per_interval =
1305 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1306 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1309 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval();
1311 problem.SetTime(times[local_to_global[0]], it);
1317 const std::vector<unsigned int>& dofs_per_block =
1318 this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFsPerBlock();
1319 unsigned int n_dofs = 0;
1320 unsigned int n_blocks = dofs_per_block.size();
1321 for (
unsigned int i = 0; i < n_blocks; i++)
1323 n_dofs += dofs_per_block[i];
1330 this->GetOutputHandler()->SetIterationNumber(0,
"Time");
1332 this->GetOutputHandler()->Write(
"Computing Initial Values:",
1333 4 + this->GetBasePriority());
1335 auto& initial_problem = problem.GetInitialProblem();
1336 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1339 _build_state_matrix = this->GetNonlinearSolver(
"state").NonlinearSolve_Initial(
1340 initial_problem, u_alt,
true,
true);
1341 _build_state_matrix =
true;
1343 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1347 this->GetOutputHandler()->Write(u_alt, outname + this->GetPostIndex(),
1348 problem.GetDoFType());
1353 ComputeTimeFunctionals(0,
1355 this->SetProblemType(
"state");
1360 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().first_interval(); it
1361 != problem.GetSpaceTimeHandler()->GetTimeDoFHandler().after_last_interval(); ++it)
1363 it.get_time_dof_indices(local_to_global);
1364 problem.SetTime(times[local_to_global[0]], it);
1370 for (
unsigned int i = 1; i < n_dofs_per_interval; i++)
1372 this->GetOutputHandler()->SetIterationNumber(local_to_global[i],
1374 double time = times[local_to_global[i]];
1376 std::stringstream out;
1377 this->GetOutputHandler()->InitOut(out);
1378 out <<
"\t\t Timestep: " << local_to_global[i] <<
" ("
1379 << times[local_to_global[i - 1]] <<
" -> " << time
1380 <<
") using " << problem.GetName();
1381 problem.GetOutputHandler()->Write(out,
1382 4 + this->GetBasePriority());
1387 this->GetProblem()->AddAuxiliaryToIntegrator(
1388 this->GetIntegrator());
1390 this->GetNonlinearSolver(
"state").NonlinearLastTimeEvals(problem,
1393 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1394 this->GetIntegrator());
1396 problem.SetTime(time, it);
1397 this->GetProblem()->AddAuxiliaryToIntegrator(
1398 this->GetIntegrator());
1401 = this->GetNonlinearSolver(
"state").NonlinearSolve(problem,
1403 _build_state_matrix);
1405 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1406 this->GetIntegrator());
1411 outname + this->GetPostIndex(), problem.GetDoFType());
1414 ComputeTimeFunctionals(local_to_global[i], max_timestep);
1415 this->SetProblemType(
"state");
1423 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1424 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1425 typename VECTOR,
int dopedim,
int dealdim>
1426 template<
typename PDE>
1428 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::
1433 unsigned int max_timestep =
1434 problem.GetSpaceTimeHandler()->GetMaxTimePoint();
1435 const std::vector<double> times =
1436 problem.GetSpaceTimeHandler()->GetTimes();
1438 n_dofs_per_interval =
1439 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().GetLocalNbrOfDoFs();
1440 std::vector<unsigned int> local_to_global(n_dofs_per_interval);
1443 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().last_interval();
1445 problem.SetTime(times[local_to_global[local_to_global.size()-1]], it);
1451 const std::vector<unsigned int>& dofs_per_block =
1452 this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFsPerBlock();
1453 unsigned int n_dofs = 0;
1454 unsigned int n_blocks = dofs_per_block.size();
1455 for (
unsigned int i = 0; i < n_blocks; i++)
1457 n_dofs += dofs_per_block[i];
1463 this->GetOutputHandler()->SetIterationNumber(max_timestep,
"Time");
1465 this->GetOutputHandler()->Write(
"Computing Initial Values:",
1466 4 + this->GetBasePriority());
1468 auto& initial_problem = problem.GetInitialProblem();
1469 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1472 _build_state_matrix = this->GetNonlinearSolver(
"adjoint").NonlinearSolve_Initial(
1473 initial_problem, u_alt,
true,
true);
1474 _build_state_matrix =
true;
1476 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1480 this->GetOutputHandler()->Write(u_alt, outname + this->GetPostIndex(),
1481 problem.GetDoFType());
1486 problem.GetSpaceTimeHandler()->GetTimeDoFHandler().last_interval(); it
1487 != problem.GetSpaceTimeHandler()->GetTimeDoFHandler().before_first_interval(); --it)
1489 it.get_time_dof_indices(local_to_global);
1490 problem.SetTime(times[local_to_global[local_to_global.size()-1]], it);
1496 for (
unsigned int i = 1; i < n_dofs_per_interval; i++)
1498 unsigned int j = n_dofs_per_interval-1-i;
1499 this->GetOutputHandler()->SetIterationNumber(local_to_global[j],
1501 double time = times[local_to_global[j]];
1503 std::stringstream out;
1504 this->GetOutputHandler()->InitOut(out);
1505 out <<
"\t\t Timestep: " << local_to_global[j+1] <<
" ("
1506 << times[local_to_global[j + 1]] <<
" -> " << time
1507 <<
") using " << problem.GetName();
1508 problem.GetOutputHandler()->Write(out,
1509 4 + this->GetBasePriority());
1514 this->GetProblem()->AddAuxiliaryToIntegrator(
1515 this->GetIntegrator());
1517 this->GetNonlinearSolver(
"adjoint").NonlinearLastTimeEvals(problem,
1520 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1521 this->GetIntegrator());
1523 problem.SetTime(time, it);
1524 this->GetProblem()->AddAuxiliaryToIntegrator(
1525 this->GetIntegrator());
1527 _build_adjoint_matrix
1528 = this->GetNonlinearSolver(
"adjoint").NonlinearSolve(problem,
1530 _build_adjoint_matrix);
1532 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1533 this->GetIntegrator());
1538 outname + this->GetPostIndex(), problem.GetDoFType());
CONTROLNONLINEARSOLVER & GetControlNonlinearSolver()
Definition: instatreducedproblem.h:526
void UnLockCopy() const
Definition: controlvector.h:196
Definition: constraintvector.h:47
StateVector< VECTOR > & GetDU()
Definition: instatreducedproblem.h:294
const dealii::Vector< double > & GetSpacialVectorCopy() const
Definition: controlvector.cc:132
void GetControlBoxConstraints(ControlVector< VECTOR > &lb, ControlVector< VECTOR > &ub)
Definition: instatreducedproblem.h:612
Definition: dopetypes.h:106
void BackwardTimeLoop(PDE &problem, StateVector< VECTOR > &sol, std::string outname)
Definition: instatreducedproblem.h:1429
VECTOR & GetSpacialVector()
Definition: statevector.cc:329
void ReSizeVector(unsigned int ndofs, const std::vector< unsigned int > &dofs_per_block, dealii::BlockVector< double > &vector)
Definition: helper.cc:30
Definition: parameterreader.h:36
StateVector< VECTOR > & GetU()
Definition: instatreducedproblem.h:286
Definition: timeiterator.h:63
Definition: dopetypes.h:105
Definition: controlvector.h:48
void ComputeReducedHessianVector(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &direction, ControlVector< VECTOR > &hessian_direction, ControlVector< VECTOR > &hessian_direction_transposed)
Definition: instatreducedproblem.h:872
StateVector< VECTOR > & GetZ()
Definition: instatreducedproblem.h:290
INTEGRATOR & GetIntegrator()
Definition: instatreducedproblem.h:305
PROBLEM * GetProblem()
Definition: reducedprobleminterface.h:493
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:1179
virtual ~InstatReducedProblem()
Definition: instatreducedproblem.h:493
void ComputeReducedFunctionals(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:804
void StateSizeInfo(std::stringstream &out)
Definition: instatreducedproblem.h:235
void ReInit()
Definition: instatreducedproblem.h:545
const SpaceTimeHandlerBase< VECTOR > * GetSpaceTimeHandler() const
Definition: controlvector.h:211
void ForwardTimeLoop(PDE &problem, StateVector< VECTOR > &sol, std::string outname, bool eval_funcs)
Definition: instatreducedproblem.h:1295
void get_time_dof_indices(std::vector< unsigned int > &local_dof_indices) const
Definition: timeiterator.h:208
const StateVector< VECTOR > & GetU() const
Definition: instatreducedproblem.h:282
Definition: statevector.h:49
void ComputeReducedAdjoint(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:623
void ComputeReducedGradient(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)
Definition: instatreducedproblem.h:649
VECTOR & GetSpacialVector()
Definition: controlvector.cc:118
void ComputeTimeFunctionals(unsigned int step, unsigned int num_steps)
Definition: instatreducedproblem.h:1009
static void declare_params(ParameterReader ¶m_reader)
Definition: instatreducedproblem.h:409
NONLINEARSOLVER & GetNonlinearSolver(std::string type)
Definition: instatreducedproblem.h:503
InstatReducedProblem(PROBLEM *OP, std::string state_behavior, ParameterReader ¶m_reader, INTEGRATORDATACONT &idc, int base_priority=0)
Definition: instatreducedproblem.h:421
Definition: reducedprobleminterface.h:338
double ComputeReducedCostFunctional(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:779
bool ComputeReducedConstraints(const ControlVector< VECTOR > &q, ConstraintVector< VECTOR > &g)
Definition: instatreducedproblem.h:599
void SetTimeDoFNumber(unsigned int dof_number, const TimeIterator &interval) const
Definition: statevector.cc:231
StateVector< VECTOR > & GetDZ()
Definition: instatreducedproblem.h:298
void ComputeRefinementIndicators(DWRC &)
Definition: instatreducedproblem.h:222
Definition: dopeexception.h:35
CONTROLINTEGRATOR & GetControlIntegrator()
Definition: instatreducedproblem.h:309
virtual void ReInit()
Definition: reducedprobleminterface.h:360
void ComputeReducedState(const ControlVector< VECTOR > &q)
Definition: instatreducedproblem.h:574