24 #ifndef _STAT_REDUCED_PROBLEM_H_
25 #define _STAT_REDUCED_PROBLEM_H_
33 #include <lac/vector.h>
35 #include <lac/block_sparsity_pattern.h>
36 #include <lac/block_sparse_matrix.h>
52 #include <base/data_out_base.h>
53 #include <numerics/data_out.h>
54 #include <numerics/matrix_tools.h>
55 #include <numerics/vector_tools.h>
56 #include <base/function.h>
57 #include <lac/sparse_matrix.h>
58 #include <lac/compressed_simple_sparsity_pattern.h>
59 #include <lac/block_sparsity_pattern.h>
60 #include <lac/sparse_direct.h>
80 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
81 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
82 typename VECTOR,
int dopedim,
int dealdim>
98 template<
typename INTEGRATORDATACONT>
101 int base_priority = 0);
116 template<
typename STATEINTEGRATORDATACONT,
117 typename CONTROLINTEGRATORCONT>
120 STATEINTEGRATORDATACONT & s_idc,
int base_priority = 0);
219 template<
class DWRC,
class PDE>
222 DWRC& dwrc, PDE& pde);
260 GetU().PrintInfos(out);
276 WriteToFile(
const VECTOR &v, std::string name, std::string outfile,
277 std::string dof_type, std::string filetype);
293 std::string outfile, std::string dof_type, std::string filetype);
389 CONTROLNONLINEARSOLVER&
399 return _control_integrator;
409 INTEGRATOR _integrator;
410 CONTROLINTEGRATOR _control_integrator;
411 NONLINEARSOLVER _nonlinear_state_solver;
412 NONLINEARSOLVER _nonlinear_adjoint_solver;
413 CONTROLNONLINEARSOLVER _nonlinear_gradient_solver;
415 bool _build_state_matrix, _build_adjoint_matrix, _build_control_matrix;
416 bool _state_reinit, _adjoint_reinit, _gradient_reinit;
419 StatReducedProblem<CONTROLNONLINEARSOLVER, NONLINEARSOLVER,
420 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>,
427 using namespace dealii;
430 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
431 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
432 typename VECTOR,
int dopedim,
int dealdim>
435 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::declare_params(
438 NONLINEARSOLVER::declare_params(param_reader);
442 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
443 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
444 typename VECTOR,
int dopedim,
int dealdim>
445 template<
typename INTEGRATORDATACONT>
448 PROBLEM *OP, std::string state_behavior,
452 base_priority), _u(OP->GetSpaceTimeHandler(), state_behavior,
453 param_reader), _z(OP->GetSpaceTimeHandler(), state_behavior,
454 param_reader), _du(OP->GetSpaceTimeHandler(), state_behavior,
455 param_reader), _dz(OP->GetSpaceTimeHandler(), state_behavior,
456 param_reader), _z_for_ee(OP->GetSpaceTimeHandler(),
457 state_behavior, param_reader), _integrator(idc), _control_integrator(
458 idc), _nonlinear_state_solver(_integrator, param_reader), _nonlinear_adjoint_solver(
459 _integrator, param_reader), _nonlinear_gradient_solver(
460 _control_integrator, param_reader)
465 _state_reinit =
true;
466 _adjoint_reinit =
true;
467 _gradient_reinit =
true;
473 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
474 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
475 typename VECTOR,
int dopedim,
int dealdim>
476 template<
typename STATEINTEGRATORDATACONT,
typename CONTROLINTEGRATORCONT>
479 PROBLEM *OP, std::string state_behavior,
481 STATEINTEGRATORDATACONT & s_idc,
int base_priority)
483 base_priority), _u(OP->GetSpaceTimeHandler(), state_behavior,
484 param_reader), _z(OP->GetSpaceTimeHandler(), state_behavior,
485 param_reader), _du(OP->GetSpaceTimeHandler(), state_behavior,
486 param_reader), _dz(OP->GetSpaceTimeHandler(), state_behavior,
487 param_reader), _z_for_ee(OP->GetSpaceTimeHandler(),
488 state_behavior, param_reader), _integrator(s_idc), _control_integrator(
489 c_idc), _nonlinear_state_solver(_integrator, param_reader), _nonlinear_adjoint_solver(
490 _integrator, param_reader), _nonlinear_gradient_solver(
491 _control_integrator, param_reader)
496 _state_reinit =
true;
497 _adjoint_reinit =
true;
498 _gradient_reinit =
true;
504 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
505 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
506 typename VECTOR,
int dopedim,
int dealdim>
508 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::~StatReducedProblem()
514 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
515 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
516 typename VECTOR,
int dopedim,
int dealdim>
519 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetNonlinearSolver(
522 if ((type ==
"state") || (type ==
"tangent"))
524 return _nonlinear_state_solver;
526 else if ((type ==
"adjoint") || (type ==
"adjoint_hessian")
527 || (type ==
"adjoint_for_ee"))
529 return _nonlinear_adjoint_solver;
533 throw DOpEException(
"No Solver for Problem type:`" + type +
"' found",
534 "StatReducedProblem::GetNonlinearSolver");
540 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
541 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
542 typename VECTOR,
int dopedim,
int dealdim>
543 CONTROLNONLINEARSOLVER&
545 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetControlNonlinearSolver()
547 if ((this->GetProblem()->GetType() ==
"gradient")
548 || (this->GetProblem()->GetType() ==
"hessian"))
550 return _nonlinear_gradient_solver;
555 "No Solver for Problem type:`" + this->GetProblem()->GetType()
556 +
"' found",
"StatReducedProblem::GetControlNonlinearSolver");
562 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
563 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
564 typename VECTOR,
int dopedim,
int dealdim>
567 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ReInit()
574 _state_reinit =
true;
575 _adjoint_reinit =
true;
576 _gradient_reinit =
true;
579 _build_state_matrix =
true;
580 _build_adjoint_matrix =
true;
586 GetZForEE().ReInit();
588 _build_control_matrix =
true;
593 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
594 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
595 typename VECTOR,
int dopedim,
int dealdim>
598 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedState(
601 this->InitializeFunctionalValues(
602 this->GetProblem()->GetNFunctionals() + 1);
604 this->GetOutputHandler()->Write(
"Computing State Solution:",
605 4 + this->GetBasePriority());
607 this->SetProblemType(
"state");
608 auto& problem = this->GetProblem()->GetStateProblem();
609 if (_state_reinit ==
true)
611 GetNonlinearSolver(
"state").ReInit(problem);
612 _state_reinit =
false;
615 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
616 if (dopedim == dealdim)
620 else if (dopedim == 0)
622 this->GetIntegrator().AddParamData(
"control",
628 "StatReducedProblem::ComputeReducedState");
631 _build_state_matrix = this->GetNonlinearSolver(
"state").NonlinearSolve(
632 problem, (GetU().GetSpacialVector()),
true, _build_state_matrix);
634 if (dopedim == dealdim)
636 this->GetIntegrator().DeleteDomainData(
"control");
638 else if (dopedim == 0)
640 this->GetIntegrator().DeleteParamData(
"control");
646 "StatReducedProblem::ComputeReducedState");
648 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
650 this->GetOutputHandler()->Write((GetU().GetSpacialVector()),
651 "State" + this->GetPostIndex(), problem.GetDoFType());
656 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
657 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
658 typename VECTOR,
int dopedim,
int dealdim>
661 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedConstraints(
664 this->GetOutputHandler()->Write(
"Evaluating Constraints:",
665 4 + this->GetBasePriority());
667 this->SetProblemType(
"constraints");
673 if (dopedim == dealdim)
675 this->GetControlIntegrator().AddDomainData(
"control",
678 else if (dopedim == 0)
680 this->GetControlIntegrator().AddParamData(
"control",
686 "StatReducedProblem::ComputeReducedConstraints");
688 this->GetControlIntegrator().ComputeLocalControlConstraints(
690 if (dopedim == dealdim)
692 this->GetControlIntegrator().DeleteDomainData(
"control");
694 else if (dopedim == 0)
696 this->GetControlIntegrator().DeleteParamData(
"control");
702 "StatReducedProblem::ComputeReducedConstraints");
708 unsigned int nglobal = gc.size();
712 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
714 for (
unsigned int i = 0; i < nglobal; i++)
717 this->SetProblemType(
"global_constraints", i);
718 this->GetIntegrator().AddDomainData(
"state",
719 &(GetU().GetSpacialVector()));
720 if (dopedim == dealdim)
722 this->GetIntegrator().AddDomainData(
"control",
725 else if (dopedim == 0)
727 this->GetIntegrator().AddParamData(
"control",
733 "StatReducedProblem::ComputeReducedConstraints");
739 if (this->GetProblem()->GetConstraintType().find(
"domain")
740 != std::string::npos)
743 ret += this->GetIntegrator().ComputeDomainScalar(
744 *(this->GetProblem()));
746 if (this->GetProblem()->GetConstraintType().find(
"point")
747 != std::string::npos)
750 ret += this->GetIntegrator().ComputePointScalar(
751 *(this->GetProblem()));
753 if (this->GetProblem()->GetConstraintType().find(
"boundary")
754 != std::string::npos)
757 ret += this->GetIntegrator().ComputeBoundaryScalar(
758 *(this->GetProblem()));
760 if (this->GetProblem()->GetConstraintType().find(
"face")
761 != std::string::npos)
764 ret += this->GetIntegrator().ComputeFaceScalar(
765 *(this->GetProblem()));
771 "Unknown Constraint Type: "
772 + this->GetProblem()->GetConstraintType(),
773 "StatReducedProblem::ComputeReducedConstraints");
778 if (dopedim == dealdim)
780 this->GetIntegrator().DeleteDomainData(
"control");
782 else if (dopedim == 0)
784 this->GetIntegrator().DeleteParamData(
"control");
790 "StatReducedProblem::ComputeReducedConstraints");
792 this->GetIntegrator().DeleteDomainData(
"state");
795 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
796 this->GetIntegrator());
801 if (g.
HasType(
"local_global_control") || g.
HasType(
"local_global_state"))
804 "There are global in space, local in time constraints given. In the stationary case they should be moved to global in space and time!",
805 "StatReducedProblem::ComputeReducedConstraints");
809 this->GetProblem()->PostProcessConstraints(g);
817 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
818 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
819 typename VECTOR,
int dopedim,
int dealdim>
822 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::GetControlBoxConstraints(
831 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
832 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
833 typename VECTOR,
int dopedim,
int dealdim>
836 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedAdjoint(
839 this->GetOutputHandler()->Write(
"Computing Reduced Adjoint:",
840 4 + this->GetBasePriority());
842 this->SetProblemType(
"adjoint");
843 if (_adjoint_reinit ==
true)
845 GetNonlinearSolver(
"adjoint").ReInit(*(this->GetProblem()));
846 _adjoint_reinit =
false;
849 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
851 this->GetIntegrator().AddDomainData(
"state",
852 &(GetU().GetSpacialVector()));
854 if (dopedim == dealdim)
858 else if (dopedim == 0)
860 this->GetIntegrator().AddParamData(
"control",
866 "StatReducedProblem::ComputeReducedAdjoint");
869 _build_adjoint_matrix =
870 this->GetNonlinearSolver(
"adjoint").NonlinearSolve(
871 *(this->GetProblem()), (GetZ().GetSpacialVector()),
true,
872 _build_adjoint_matrix);
874 if (dopedim == dealdim)
876 this->GetIntegrator().DeleteDomainData(
"control");
878 else if (dopedim == 0)
880 this->GetIntegrator().DeleteParamData(
"control");
886 "StatReducedProblem::ComputeReducedAdjoint");
888 this->GetIntegrator().DeleteDomainData(
"state");
890 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
892 this->GetOutputHandler()->Write((GetZ().GetSpacialVector()),
893 "Adjoint" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
898 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
899 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
900 typename VECTOR,
int dopedim,
int dealdim>
903 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeDualForErrorEstimation(
907 this->GetOutputHandler()->Write(
"Computing Dual for Error Estimation:",
908 4 + this->GetBasePriority());
912 this->SetProblemType(
"adjoint_for_ee");
917 "StatPDEProblem::ComputeDualForErrorEstimation");
921 auto& problem = *(this->GetProblem());
922 if (_adjoint_reinit ==
true)
924 GetNonlinearSolver(
"adjoint_for_ee").ReInit(problem);
925 _adjoint_reinit =
false;
928 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
930 this->GetIntegrator().AddDomainData(
"state",
931 &(GetU().GetSpacialVector()));
933 if (dopedim == dealdim)
937 else if (dopedim == 0)
939 this->GetIntegrator().AddParamData(
"control",
945 "StatReducedProblem::ComputeReducedAdjoint");
948 _build_adjoint_matrix =
949 this->GetNonlinearSolver(
"adjoint_for_ee").NonlinearSolve(problem,
950 (GetZForEE().GetSpacialVector()),
true, _build_adjoint_matrix);
952 if (dopedim == dealdim)
954 this->GetIntegrator().DeleteDomainData(
"control");
956 else if (dopedim == 0)
958 this->GetIntegrator().DeleteParamData(
"control");
964 "StatReducedProblem::ComputeReducedAdjoint");
967 this->GetIntegrator().DeleteDomainData(
"state");
969 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
971 this->GetOutputHandler()->Write((GetZForEE().GetSpacialVector()),
972 "Adjoint_for_ee" + this->GetPostIndex(), problem.GetDoFType());
978 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
979 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
980 typename VECTOR,
int dopedim,
int dealdim>
983 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedGradient(
987 this->ComputeReducedAdjoint(q);
989 this->GetOutputHandler()->Write(
"Computing Reduced Gradient:",
990 4 + this->GetBasePriority());
994 if (this->GetProblem()->HasControlInDirichletData())
996 tmp.reinit(GetU().GetSpacialVector());
997 this->SetProblemType(
"adjoint");
998 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1000 if (dopedim == dealdim)
1002 this->GetIntegrator().AddDomainData(
"control",
1005 else if (dopedim == 0)
1007 this->GetIntegrator().AddParamData(
"control",
1013 "StatReducedProblem::ComputeReducedGradient");
1016 this->GetIntegrator().AddDomainData(
"state",
1017 &(GetU().GetSpacialVector()));
1018 this->GetIntegrator().AddDomainData(
"last_newton_solution",
1019 &(GetZ().GetSpacialVector()));
1021 this->GetIntegrator().ComputeNonlinearResidual(*(this->GetProblem()),
1025 if (dopedim == dealdim)
1027 this->GetIntegrator().DeleteDomainData(
"control");
1029 else if (dopedim == 0)
1031 this->GetIntegrator().DeleteParamData(
"control");
1037 "StatReducedProblem::ComputeReducedGradient");
1040 this->GetIntegrator().DeleteDomainData(
"state");
1041 this->GetIntegrator().DeleteDomainData(
"last_newton_solution");
1042 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1043 this->GetIntegrator());
1046 this->SetProblemType(
"gradient");
1047 if (_gradient_reinit ==
true)
1049 GetControlNonlinearSolver().ReInit(*(this->GetProblem()));
1050 _gradient_reinit =
false;
1053 this->GetProblem()->AddAuxiliaryToIntegrator(
1054 this->GetControlIntegrator());
1056 if (dopedim == dealdim)
1058 this->GetControlIntegrator().AddDomainData(
"control",
1061 else if (dopedim == 0)
1063 this->GetControlIntegrator().AddParamData(
"control",
1069 "StatReducedProblem::ComputeReducedGradient");
1072 this->GetControlIntegrator().AddDomainData(
"state",
1073 &(GetU().GetSpacialVector()));
1074 this->GetControlIntegrator().AddDomainData(
"adjoint",
1075 &(GetZ().GetSpacialVector()));
1076 if (this->GetProblem()->HasControlInDirichletData())
1077 this->GetControlIntegrator().AddDomainData(
"adjoint_residual", &tmp);
1079 gradient_transposed = 0.;
1080 if (dopedim == dealdim)
1082 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
1084 this->GetControlIntegrator().ComputeNonlinearResidual(
1086 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1088 else if (dopedim == 0)
1090 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
1092 this->GetControlIntegrator().ComputeNonlinearResidual(
1095 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
1101 gradient_transposed = gradient;
1105 _build_control_matrix = this->GetControlNonlinearSolver().NonlinearSolve(
1107 _build_control_matrix);
1108 if (dopedim == dealdim)
1110 this->GetControlIntegrator().DeleteDomainData(
"control");
1112 else if (dopedim == 0)
1114 this->GetControlIntegrator().DeleteParamData(
"control");
1120 "StatReducedProblem::ComputeReducedGradient");
1122 this->GetControlIntegrator().DeleteDomainData(
"state");
1123 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
1124 if (this->GetProblem()->HasControlInDirichletData())
1125 this->GetControlIntegrator().DeleteDomainData(
"adjoint_residual");
1127 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1128 this->GetControlIntegrator());
1130 this->GetOutputHandler()->Write(gradient,
1131 "Gradient" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
1132 this->GetOutputHandler()->Write(gradient_transposed,
1133 "Gradient_Transposed" + this->GetPostIndex(),
1134 this->GetProblem()->GetDoFType());
1140 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1141 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1142 typename VECTOR,
int dopedim,
int dealdim>
1145 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedCostFunctional(
1148 this->ComputeReducedState(q);
1153 this->GetOutputHandler()->Write(
"Computing Cost Functional:",
1154 4 + this->GetBasePriority());
1156 this->SetProblemType(
"cost_functional");
1158 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1160 if (dopedim == dealdim)
1164 else if (dopedim == 0)
1166 this->GetIntegrator().AddParamData(
"control",
1172 "StatReducedProblem::ComputeReducedCostFunctional");
1174 this->GetIntegrator().AddDomainData(
"state",
1175 &(GetU().GetSpacialVector()));
1177 if (this->GetProblem()->GetFunctionalType().find(
"domain")
1178 != std::string::npos)
1181 ret += this->GetIntegrator().ComputeDomainScalar(*(this->GetProblem()));
1183 if (this->GetProblem()->GetFunctionalType().find(
"point")
1184 != std::string::npos)
1187 ret += this->GetIntegrator().ComputePointScalar(*(this->GetProblem()));
1189 if (this->GetProblem()->GetFunctionalType().find(
"boundary")
1190 != std::string::npos)
1193 ret += this->GetIntegrator().ComputeBoundaryScalar(
1194 *(this->GetProblem()));
1196 if (this->GetProblem()->GetFunctionalType().find(
"face")
1197 != std::string::npos)
1200 ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1206 "Unknown Functional Type: "
1207 + this->GetProblem()->GetFunctionalType(),
1208 "StatReducedProblem::ComputeReducedCostFunctional");
1211 if (dopedim == dealdim)
1213 this->GetIntegrator().DeleteDomainData(
"control");
1215 else if (dopedim == 0)
1217 this->GetIntegrator().DeleteParamData(
"control");
1223 "StatReducedProblem::ComputeReducedCostFunctional");
1225 this->GetIntegrator().DeleteDomainData(
"state");
1226 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1228 this->GetFunctionalValues()[0].push_back(ret);
1234 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1235 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1236 typename VECTOR,
int dopedim,
int dealdim>
1239 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedFunctionals(
1242 this->GetOutputHandler()->Write(
"Computing Functionals:",
1243 4 + this->GetBasePriority());
1245 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1247 if (dopedim == dealdim)
1251 else if (dopedim == 0)
1253 this->GetIntegrator().AddParamData(
"control",
1259 "StatReducedProblem::ComputeReducedFunctionals");
1261 this->GetIntegrator().AddDomainData(
"state",
1262 &(GetU().GetSpacialVector()));
1264 for (
unsigned int i = 0; i < this->GetProblem()->GetNFunctionals(); i++)
1269 this->SetProblemType(
"aux_functional", i);
1270 if (this->GetProblem()->GetFunctionalType().find(
"domain")
1271 != std::string::npos)
1274 ret += this->GetIntegrator().ComputeDomainScalar(
1275 *(this->GetProblem()));
1277 if (this->GetProblem()->GetFunctionalType().find(
"point")
1278 != std::string::npos)
1281 ret += this->GetIntegrator().ComputePointScalar(
1282 *(this->GetProblem()));
1284 if (this->GetProblem()->GetFunctionalType().find(
"boundary")
1285 != std::string::npos)
1288 ret += this->GetIntegrator().ComputeBoundaryScalar(
1289 *(this->GetProblem()));
1291 if (this->GetProblem()->GetFunctionalType().find(
"face")
1292 != std::string::npos)
1295 ret += this->GetIntegrator().ComputeFaceScalar(*(this->GetProblem()));
1301 "Unknown Functional Type: "
1302 + this->GetProblem()->GetFunctionalType(),
1303 "StatReducedProblem::ComputeReducedFunctionals");
1305 this->GetFunctionalValues()[i + 1].push_back(ret);
1306 std::stringstream out;
1307 this->GetOutputHandler()->InitOut(out);
1308 out << this->GetProblem()->GetFunctionalName() <<
": " << ret;
1309 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
1312 if (dopedim == dealdim)
1314 this->GetIntegrator().DeleteDomainData(
"control");
1316 else if (dopedim == 0)
1318 this->GetIntegrator().DeleteParamData(
"control");
1324 "StatReducedProblem::ComputeReducedFunctionals");
1326 this->GetIntegrator().DeleteDomainData(
"state");
1327 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1333 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1334 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1335 typename VECTOR,
int dopedim,
int dealdim>
1336 template<
class DWRC,
class PDE>
1339 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeRefinementIndicators(
1346 pde.ResidualModifier = boost::bind<void>(boost::mem_fn(&DWRC::ResidualModifier),boost::ref(dwrc),_1);
1347 pde.VectorResidualModifier = boost::bind<void>(boost::mem_fn(&DWRC::VectorResidualModifier),boost::ref(dwrc),_1);
1351 const unsigned int n_elements =
1352 this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFHandler().get_tria().n_active_cells();
1356 if(this->GetProblem()->EEFunctionalIsCost() || !dwrc.NeedDual())
1358 this->GetOutputHandler()->Write(
"Computing Error Indicators:",
1359 4 + this->GetBasePriority());
1360 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1363 this->GetIntegrator().AddDomainData(
"state",
1364 &(GetU().GetSpacialVector()));
1365 this->GetIntegrator().AddDomainData(
"adjoint_for_ee",
1366 &(GetZ().GetSpacialVector()));
1368 if (dopedim == dealdim)
1370 this->GetIntegrator().AddDomainData(
"control",
1373 else if (dopedim == 0)
1375 this->GetIntegrator().AddParamData(
"control",
1381 "StatReducedProblem::ComputeRefinementIndicators");
1384 this->SetProblemType(
"error_evaluation");
1387 dwrc.PrepareWeights(GetU(), GetZ());
1388 dwrc.PrepareWeights(q);
1391 this->GetIntegrator().ComputeRefinementIndicators(*this->GetProblem(),
1395 dwrc.ClearWeightData();
1398 if (dopedim == dealdim)
1400 this->GetIntegrator().DeleteDomainData(
"control");
1402 else if (dopedim == 0)
1404 this->GetIntegrator().DeleteParamData(
"control");
1410 "StatReducedProblem::ComputeRefinementIndicators");
1412 this->GetIntegrator().DeleteDomainData(
"state");
1413 this->GetIntegrator().DeleteDomainData(
"adjoint_for_ee");
1414 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1415 this->GetIntegrator());
1419 throw DOpEException(
"Estimating the error in other functionals than cost is not implemented",
1420 "StatReducedProblem::ComputeRefinementIndicators");
1424 std::stringstream out;
1425 this->GetOutputHandler()->InitOut(out);
1426 out <<
"Error estimate using "<<dwrc.GetName();
1428 out<<
" For the computation of "<<this->GetProblem()->GetFunctionalName();
1429 out<<
": "<< dwrc.GetError();
1430 this->GetOutputHandler()->Write(out, 2 + this->GetBasePriority());
1435 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1436 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1437 typename VECTOR,
int dopedim,
int dealdim>
1440 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedHessianVector(
1445 this->GetOutputHandler()->Write(
"Computing ReducedHessianVector:",
1446 4 + this->GetBasePriority());
1447 this->GetOutputHandler()->Write(
"\tSolving Tangent:",
1448 5 + this->GetBasePriority());
1450 this->SetProblemType(
"tangent");
1452 this->GetProblem()->AddAuxiliaryToIntegrator(this->GetIntegrator());
1454 this->GetIntegrator().AddDomainData(
"state",
1455 &(GetU().GetSpacialVector()));
1456 this->GetControlIntegrator().AddDomainData(
"state",
1457 &(GetU().GetSpacialVector()));
1459 if (dopedim == dealdim)
1461 this->GetIntegrator().AddDomainData(
"dq",
1465 else if (dopedim == 0)
1467 this->GetIntegrator().AddParamData(
"dq",
1469 this->GetIntegrator().AddParamData(
"control",
1475 "StatReducedProblem::ComputeReducedHessianVector");
1479 _build_state_matrix = this->GetNonlinearSolver(
"tangent").NonlinearSolve(
1480 *(this->GetProblem()), (GetDU().GetSpacialVector()),
true,
1481 _build_state_matrix);
1483 this->GetOutputHandler()->Write((GetDU().GetSpacialVector()),
1484 "Tangent" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
1486 this->GetOutputHandler()->Write(
"\tSolving Adjoint Hessian:",
1487 5 + this->GetBasePriority());
1488 this->SetProblemType(
"adjoint_hessian");
1489 this->GetIntegrator().AddDomainData(
"adjoint",
1490 &(GetZ().GetSpacialVector()));
1491 this->GetIntegrator().AddDomainData(
"tangent",
1492 &(GetDU().GetSpacialVector()));
1493 this->GetControlIntegrator().AddDomainData(
"adjoint",
1494 &(GetZ().GetSpacialVector()));
1495 this->GetControlIntegrator().AddDomainData(
"tangent",
1496 &(GetDU().GetSpacialVector()));
1499 _build_adjoint_matrix =
1500 this->GetNonlinearSolver(
"adjoint_hessian").NonlinearSolve(
1501 *(this->GetProblem()), (GetDZ().GetSpacialVector()),
true,
1502 _build_adjoint_matrix);
1504 this->GetOutputHandler()->Write((GetDZ().GetSpacialVector()),
1505 "Hessian" + this->GetPostIndex(), this->GetProblem()->GetDoFType());
1507 this->GetIntegrator().AddDomainData(
"adjoint_hessian",
1508 &(GetDZ().GetSpacialVector()));
1509 this->GetControlIntegrator().AddDomainData(
"adjoint_hessian",
1510 &(GetDZ().GetSpacialVector()));
1512 this->GetOutputHandler()->Write(
1513 "\tComputing Representation of the Hessian:",
1514 5 + this->GetBasePriority());
1518 if (this->GetProblem()->HasControlInDirichletData())
1520 tmp.reinit(GetU().GetSpacialVector());
1521 tmp_second.reinit(GetU().GetSpacialVector());
1522 this->SetProblemType(
"adjoint");
1523 this->GetIntegrator().AddDomainData(
"last_newton_solution",
1524 &(GetZ().GetSpacialVector()));
1526 this->GetIntegrator().ComputeNonlinearResidual(*(this->GetProblem()),
1530 this->GetIntegrator().DeleteDomainData(
"last_newton_solution");
1531 this->SetProblemType(
"adjoint_hessian");
1532 this->GetIntegrator().AddDomainData(
"last_newton_solution",
1533 &(GetDZ().GetSpacialVector()));
1535 this->GetIntegrator().ComputeNonlinearResidual(*(this->GetProblem()),
1539 this->GetIntegrator().DeleteDomainData(
"last_newton_solution");
1542 this->GetProblem()->DeleteAuxiliaryFromIntegrator(this->GetIntegrator());
1544 this->SetProblemType(
"hessian");
1545 this->GetProblem()->AddAuxiliaryToIntegrator(
1546 this->GetControlIntegrator());
1547 if (dopedim == dealdim)
1549 this->GetIntegrator().DeleteDomainData(
"dq");
1550 this->GetIntegrator().DeleteDomainData(
"control");
1551 this->GetControlIntegrator().AddDomainData(
"dq",
1553 this->GetControlIntegrator().AddDomainData(
"control",
1556 else if (dopedim == 0)
1558 this->GetIntegrator().DeleteParamData(
"dq");
1559 this->GetIntegrator().DeleteParamData(
"control");
1562 this->GetControlIntegrator().AddParamData(
"dq",
1564 this->GetControlIntegrator().AddParamData(
"control",
1570 "StatReducedProblem::ComputeReducedHessianVector");
1572 if (this->GetProblem()->HasControlInDirichletData())
1574 this->GetControlIntegrator().AddDomainData(
"adjoint_residual", &tmp);
1575 this->GetControlIntegrator().AddDomainData(
"hessian_residual",
1580 hessian_direction_transposed = 0.;
1581 if (dopedim == dealdim)
1583 this->GetControlIntegrator().AddDomainData(
"last_newton_solution",
1585 this->GetControlIntegrator().ComputeNonlinearResidual(
1588 this->GetControlIntegrator().DeleteDomainData(
"last_newton_solution");
1590 else if (dopedim == 0)
1592 this->GetControlIntegrator().AddParamData(
"last_newton_solution",
1594 this->GetControlIntegrator().ComputeNonlinearResidual(
1597 this->GetControlIntegrator().DeleteParamData(
"last_newton_solution");
1600 hessian_direction *= -1.;
1601 hessian_direction_transposed = hessian_direction;
1604 _build_control_matrix =
1605 this->GetControlNonlinearSolver().NonlinearSolve(
1606 *(this->GetProblem()),
1608 _build_control_matrix);
1610 this->GetOutputHandler()->Write(hessian_direction,
1611 "HessianDirection" + this->GetPostIndex(),
1612 this->GetProblem()->GetDoFType());
1613 this->GetOutputHandler()->Write(hessian_direction_transposed,
1614 "HessianDirection_Transposed" + this->GetPostIndex(),
1615 this->GetProblem()->GetDoFType());
1618 if (dopedim == dealdim)
1620 this->GetControlIntegrator().DeleteDomainData(
"dq");
1621 this->GetControlIntegrator().DeleteDomainData(
"control");
1623 else if (dopedim == 0)
1625 this->GetControlIntegrator().DeleteParamData(
"dq");
1626 this->GetControlIntegrator().DeleteParamData(
"control");
1633 "StatReducedProblem::ComputeReducedHessianVector");
1635 this->GetIntegrator().DeleteDomainData(
"state");
1636 this->GetIntegrator().DeleteDomainData(
"adjoint");
1637 this->GetIntegrator().DeleteDomainData(
"tangent");
1638 this->GetIntegrator().DeleteDomainData(
"adjoint_hessian");
1639 this->GetControlIntegrator().DeleteDomainData(
"state");
1640 this->GetControlIntegrator().DeleteDomainData(
"adjoint");
1641 this->GetControlIntegrator().DeleteDomainData(
"tangent");
1642 this->GetControlIntegrator().DeleteDomainData(
"adjoint_hessian");
1643 if (this->GetProblem()->HasControlInDirichletData())
1645 this->GetControlIntegrator().DeleteDomainData(
"adjoint_residual");
1646 this->GetControlIntegrator().DeleteDomainData(
"hessian_residual");
1648 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1649 this->GetControlIntegrator());
1655 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1656 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1657 typename VECTOR,
int dopedim,
int dealdim>
1660 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::ComputeReducedGradientOfGlobalConstraints(
1667 std::stringstream out;
1668 out <<
"Computing Reduced Gradient of global constraint " << num <<
" :";
1669 this->GetOutputHandler()->Write(out, 4 + this->GetBasePriority());
1671 this->SetProblemType(
"global_constraint_gradient", num);
1673 if (dopedim == dealdim)
1675 this->GetControlIntegrator().AddDomainData(
"control",
1678 else if (dopedim == 0)
1680 this->GetControlIntegrator().AddParamData(
"control",
1686 "StatReducedProblem::ComputeReducedGradient");
1688 this->GetProblem()->AddAuxiliaryToIntegrator(
1689 this->GetControlIntegrator());
1690 this->GetControlIntegrator().AddDomainData(
"constraints_local",
1692 this->GetControlIntegrator().AddParamData(
"constraints_global",
1696 this->GetControlIntegrator().ComputeNonlinearRhs(*(this->GetProblem()),
1698 gradient_transposed = gradient;
1700 this->GetControlIntegrator().DeleteDomainData(
"constraints_local");
1701 this->GetControlIntegrator().DeleteParamData(
"constraints_global");
1702 this->GetProblem()->DeleteAuxiliaryFromIntegrator(
1703 this->GetControlIntegrator());
1704 if (dopedim == dealdim)
1706 this->GetControlIntegrator().DeleteDomainData(
"control");
1708 else if (dopedim == 0)
1710 this->GetControlIntegrator().DeleteParamData(
"control");
1716 "StatReducedProblem::ComputeReducedGradient");
1722 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1723 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1724 typename VECTOR,
int dopedim,
int dealdim>
1727 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(
1728 const VECTOR &v, std::string name, std::string outfile,
1729 std::string dof_type, std::string filetype)
1731 if (dof_type ==
"state")
1734 this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1735 data_out.attach_dof_handler(
1736 this->GetProblem()->GetSpaceTimeHandler()->GetStateDoFHandler());
1738 data_out.add_data_vector(v, name);
1739 data_out.build_patches();
1741 std::ofstream output(outfile.c_str());
1743 if (filetype ==
".vtk")
1745 data_out.write_vtk(output);
1747 else if (filetype ==
".gpl")
1749 data_out.write_gnuplot(output);
1754 "Don't know how to write filetype `" + filetype +
"'!",
1755 "StatReducedProblem::WriteToFile");
1759 else if (dof_type ==
"control")
1761 #if dope_dimension >0
1762 auto& data_out = this->GetProblem()->GetSpaceTimeHandler()->GetDataOut();
1763 data_out.attach_dof_handler (this->GetProblem()->GetSpaceTimeHandler()->GetControlDoFHandler());
1765 data_out.add_data_vector (v,name);
1766 data_out.build_patches ();
1768 std::ofstream output(outfile.c_str());
1770 if(filetype ==
".vtk")
1772 data_out.write_vtk (output);
1774 else if (filetype ==
".gpl")
1776 data_out.write_gnuplot(output);
1780 throw DOpEException(
"Don't know how to write filetype `" + filetype +
"'!",
1781 "StatReducedProblem::WriteToFile");
1785 if (filetype ==
".txt")
1787 std::ofstream output(outfile.c_str());
1790 for (
unsigned int i = 0; i < off.size(); i++)
1792 output << off(i) << std::endl;
1798 "Don't know how to write filetype `" + filetype +
"'!",
1799 "StatReducedProblem::WriteToFile");
1805 throw DOpEException(
"No such DoFHandler `" + dof_type +
"'!",
1806 "StatReducedProblem::WriteToFile");
1812 template<
typename CONTROLNONLINEARSOLVER,
typename NONLINEARSOLVER,
1813 typename CONTROLINTEGRATOR,
typename INTEGRATOR,
typename PROBLEM,
1814 typename VECTOR,
int dopedim,
int dealdim>
1817 CONTROLINTEGRATOR, INTEGRATOR, PROBLEM, VECTOR, dopedim, dealdim>::WriteToFile(
1819 std::string dof_type, std::string filetype)
void ComputeRefinementIndicators(const ControlVector< VECTOR > &q, DWRC &dwrc, PDE &pde)
Definition: statreducedproblem.h:1339
CONTROLINTEGRATOR & GetControlIntegrator()
Definition: statreducedproblem.h:397
void UnLockCopy() const
Definition: controlvector.h:196
Definition: constraintvector.h:47
const dealii::Vector< double > & GetSpacialVectorCopy() const
Definition: controlvector.cc:132
NONLINEARSOLVER & GetNonlinearSolver(std::string type)
Definition: statreducedproblem.h:519
CONTROLNONLINEARSOLVER & GetControlNonlinearSolver()
Definition: statreducedproblem.h:545
void ComputeReducedFunctionals(const ControlVector< VECTOR > &q)
Definition: statreducedproblem.h:1239
void ReInit()
Definition: statreducedproblem.h:567
INTEGRATOR & GetIntegrator()
Definition: statreducedproblem.h:392
StatReducedProblem(PROBLEM *OP, std::string state_behavior, ParameterReader ¶m_reader, INTEGRATORDATACONT &idc, int base_priority=0)
Definition: statreducedproblem.h:447
void ComputeDualForErrorEstimation(const ControlVector< VECTOR > &q, DOpEtypes::WeightComputation weight_comp)
Definition: statreducedproblem.h:903
StateVector< VECTOR > & GetU()
Definition: statreducedproblem.h:354
Definition: parameterreader.h:36
void WriteToFile(const VECTOR &v, std::string name, std::string outfile, std::string dof_type, std::string filetype)
Definition: statreducedproblem.h:1727
void ComputeReducedAdjoint(const ControlVector< VECTOR > &q)
Definition: statreducedproblem.h:836
const dealii::Vector< double > & GetGlobalConstraints() const
Definition: constraintvector.cc:174
Definition: statreducedproblem.h:83
StateVector< VECTOR > & GetZ()
Definition: statreducedproblem.h:359
StateVector< VECTOR > & GetZForEE()
Definition: statreducedproblem.h:382
Definition: controlvector.h:48
Definition: dopetypes.h:80
static void declare_params(ParameterReader ¶m_reader)
Definition: statreducedproblem.h:435
void GetControlBoxConstraints(ControlVector< VECTOR > &lb, ControlVector< VECTOR > &ub)
Definition: statreducedproblem.h:822
double ComputeReducedCostFunctional(const ControlVector< VECTOR > &q)
Definition: statreducedproblem.h:1145
const StateVector< VECTOR > & GetZForEE() const
Definition: statreducedproblem.h:377
StateVector< VECTOR > & GetDZ()
Definition: statreducedproblem.h:369
void ComputeReducedGradient(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)
Definition: statreducedproblem.h:983
Definition: statevector.h:49
VECTOR & GetSpacialVector()
Definition: controlvector.cc:118
bool HasType(std::string name) const
Definition: constraintvector.cc:119
WeightComputation
Definition: dopetypes.h:78
void ComputeReducedState(const ControlVector< VECTOR > &q)
Definition: statreducedproblem.h:598
Definition: reducedprobleminterface.h:338
void ComputeReducedHessianVector(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &direction, ControlVector< VECTOR > &hessian_direction, ControlVector< VECTOR > &hessian_direction_transposed)
Definition: statreducedproblem.h:1440
bool ComputeReducedConstraints(const ControlVector< VECTOR > &q, ConstraintVector< VECTOR > &g)
Definition: statreducedproblem.h:661
void StateSizeInfo(std::stringstream &out)
Definition: statreducedproblem.h:258
VECTOR & GetSpacialVector(std::string name)
Definition: constraintvector.cc:130
Definition: dopeexception.h:35
void WriteToFile(const std::vector< double > &, std::string)
Definition: statreducedproblem.h:303
const StateVector< VECTOR > & GetU() const
Definition: statreducedproblem.h:349
virtual bool IsFeasible() const
Definition: constraintvector.cc:574
StateVector< VECTOR > & GetDU()
Definition: statreducedproblem.h:364
virtual void ReInit()
Definition: reducedprobleminterface.h:360
~StatReducedProblem()
Definition: statreducedproblem.h:508
void ComputeReducedGradientOfGlobalConstraints(unsigned int num, const ControlVector< VECTOR > &q, const ConstraintVector< VECTOR > &g, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)
Definition: statreducedproblem.h:1660