47 const bool use_dfdp_as_tangent, Teuchos::FancyOStream &out,
50 std::vector<double> StepSize;
51 std::vector<double> ErrorNorm;
52 const int nTimeStepSizes = 7;
55 Teuchos::RCP<const Teuchos::Comm<int> > comm =
56 Teuchos::DefaultComm<int>::getComm();
57 for (
int n = 0; n < nTimeStepSizes; n++) {
59 RCP<ParameterList> pList =
60 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
63 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
64 scm_pl->set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
65 RCP<SinCosModel<double> > model =
71 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
72 ParameterList &sens_pl = pl->sublist(
"Sensitivities");
73 if (use_combined_method)
74 sens_pl.set(
"Sensitivity Method",
"Combined");
76 sens_pl.set(
"Sensitivity Method",
"Staggered");
77 sens_pl.set(
"Reuse State Linear Solver",
true);
79 sens_pl.set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
82 pl->sublist(
"Default Integrator")
83 .sublist(
"Time Step Control")
84 .set(
"Initial Time Step", dt);
85 RCP<Tempus::IntegratorForwardSensitivity<double> > integrator =
86 Tempus::createIntegratorForwardSensitivity<double>(pl, model);
87 order = integrator->getStepper()->getOrder();
90 double t0 = pl->sublist(
"Default Integrator")
91 .sublist(
"Time Step Control")
92 .get<
double>(
"Initial Time");
93 RCP<const Thyra::VectorBase<double> > x0 =
94 model->getExactSolution(t0).get_x();
95 const int num_param = model->get_p_space(0)->dim();
96 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
97 Thyra::createMembers(model->get_x_space(), num_param);
98 for (
int i = 0; i < num_param; ++i)
99 Thyra::assign(DxDp0->col(i).ptr(),
100 *(model->getExactSensSolution(i, t0).get_x()));
101 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
102 DxDp0, Teuchos::null, Teuchos::null);
105 bool integratorStatus = integrator->advanceTime();
106 TEST_ASSERT(integratorStatus)
109 double time = integrator->getTime();
110 double timeFinal = pl->sublist(
"Default Integrator")
111 .sublist(
"Time Step Control")
112 .get<
double>(
"Final Time");
113 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
116 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
117 RCP<const Thyra::MultiVectorBase<double> > DxDp = integrator->getDxDp();
118 RCP<const Thyra::VectorBase<double> > x_exact =
119 model->getExactSolution(time).get_x();
120 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
121 Thyra::createMembers(model->get_x_space(), num_param);
122 for (
int i = 0; i < num_param; ++i)
123 Thyra::assign(DxDp_exact->col(i).ptr(),
124 *(model->getExactSensSolution(i, time).get_x()));
127 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
128 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
130 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_Sens.dat");
131 RCP<const SolutionHistory<double> > solutionHistory =
132 integrator->getSolutionHistory();
133 RCP<Thyra::MultiVectorBase<double> > DxDp_exact_plot =
134 Thyra::createMembers(model->get_x_space(), num_param);
135 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
136 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
137 double time_i = solutionState->getTime();
138 RCP<const DMVPV> x_prod_plot =
139 Teuchos::rcp_dynamic_cast<const DMVPV>(solutionState->getX());
140 RCP<const Thyra::VectorBase<double> > x_plot =
141 x_prod_plot->getMultiVector()->col(0);
142 RCP<const Thyra::MultiVectorBase<double> > DxDp_plot =
143 x_prod_plot->getMultiVector()->subView(
144 Teuchos::Range1D(1, num_param));
145 RCP<const Thyra::VectorBase<double> > x_exact_plot =
146 model->getExactSolution(time_i).get_x();
147 for (
int j = 0; j < num_param; ++j)
148 Thyra::assign(DxDp_exact_plot->col(j).ptr(),
149 *(model->getExactSensSolution(j, time_i).get_x()));
150 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
151 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1);
152 for (
int j = 0; j < num_param; ++j)
153 ftmp << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 0)
154 << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 1);
155 ftmp << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
156 << get_ele(*(x_exact_plot), 1);
157 for (
int j = 0; j < num_param; ++j)
158 ftmp << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 0)
159 << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 1);
166 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
167 RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
168 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
169 Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
170 StepSize.push_back(dt);
171 double L2norm = Thyra::norm_2(*xdiff);
173 Teuchos::Array<double> L2norm_DxDp(num_param);
174 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
175 for (
int i = 0; i < num_param; ++i)
176 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
177 L2norm = std::sqrt(L2norm);
178 ErrorNorm.push_back(L2norm);
180 out <<
" n = " << n <<
" dt = " << dt <<
" error = " << L2norm << std::endl;
184 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
185 out <<
" Stepper = BackwardEuler" << std::endl;
186 out <<
" =========================" << std::endl;
187 out <<
" Expected order: " << order << std::endl;
188 out <<
" Observed order: " << slope << std::endl;
189 out <<
" =========================" << std::endl;
190 TEST_FLOATING_EQUALITY(slope, order, 0.015);
191 TEST_FLOATING_EQUALITY(ErrorNorm[0], 0.163653, 1.0e-4);
193 if (comm->getRank() == 0) {
194 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_Sens-Error.dat");
195 double error0 = 0.8 * ErrorNorm[0];
196 for (
int n = 0; n < nTimeStepSizes; n++) {
197 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
198 << error0 * (StepSize[n] / StepSize[0]) << std::endl;