48 std::vector<double> StepSize;
49 std::vector<double> ErrorNorm;
50 const int nTimeStepSizes = 7;
53 Teuchos::RCP<const Teuchos::Comm<int> > comm =
54 Teuchos::DefaultComm<int>::getComm();
55 for (
int n = 0; n < nTimeStepSizes; n++) {
57 RCP<ParameterList> pList =
58 getParametersFromXmlFile(
"Tempus_BDF2_SinCos_SA.xml");
61 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
62 RCP<SinCosModel<double> > model =
68 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
70 ParameterList& interp_pl = pl->sublist(
"Default Integrator")
71 .sublist(
"Solution History")
72 .sublist(
"Interpolator");
73 interp_pl.set(
"Interpolator Type",
"Lagrange");
74 interp_pl.set(
"Order", 1);
77 pl->sublist(
"Default Integrator")
78 .sublist(
"Time Step Control")
79 .set(
"Initial Time Step", dt);
80 RCP<Tempus::IntegratorAdjointSensitivity<double> > integrator =
82 order = integrator->getStepper()->getOrder();
85 double t0 = pl->sublist(
"Default Integrator")
86 .sublist(
"Time Step Control")
87 .get<
double>(
"Initial Time");
88 RCP<const Thyra::VectorBase<double> > x0 =
89 model->getExactSolution(t0).get_x();
90 const int num_param = model->get_p_space(0)->dim();
91 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
92 Thyra::createMembers(model->get_x_space(), num_param);
93 for (
int i = 0; i < num_param; ++i)
94 Thyra::assign(DxDp0->col(i).ptr(),
95 *(model->getExactSensSolution(i, t0).get_x()));
96 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
97 DxDp0, Teuchos::null, Teuchos::null);
100 bool integratorStatus = integrator->advanceTime();
101 TEST_ASSERT(integratorStatus)
104 double time = integrator->getTime();
105 double timeFinal = pl->sublist(
"Default Integrator")
106 .sublist(
"Time Step Control")
107 .get<
double>(
"Final Time");
108 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
113 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
114 RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
115 RCP<Thyra::MultiVectorBase<double> > DxDp =
116 Thyra::createMembers(model->get_x_space(), num_param);
118 Thyra::ConstDetachedMultiVectorView<double> dgdp_view(*DgDp);
119 Thyra::DetachedMultiVectorView<double> dxdp_view(*DxDp);
120 const int num_g = DgDp->domain()->dim();
121 for (
int i = 0; i < num_g; ++i)
122 for (
int j = 0; j < num_param; ++j) dxdp_view(i, j) = dgdp_view(j, i);
124 RCP<const Thyra::VectorBase<double> > x_exact =
125 model->getExactSolution(time).get_x();
126 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
127 Thyra::createMembers(model->get_x_space(), num_param);
128 for (
int i = 0; i < num_param; ++i)
129 Thyra::assign(DxDp_exact->col(i).ptr(),
130 *(model->getExactSensSolution(i, time).get_x()));
133 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
134 typedef Thyra::DefaultProductVector<double> DPV;
135 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
137 std::ofstream ftmp(
"Tempus_BDF2_SinCos_AdjSens.dat");
138 RCP<const SolutionHistory<double> > solutionHistory =
139 integrator->getSolutionHistory();
140 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
141 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
142 const double time_i = solutionState->getTime();
143 RCP<const DPV> x_prod_plot =
144 Teuchos::rcp_dynamic_cast<const DPV>(solutionState->getX());
145 RCP<const Thyra::VectorBase<double> > x_plot =
146 x_prod_plot->getVectorBlock(0);
147 RCP<const DMVPV> adjoint_prod_plot =
148 Teuchos::rcp_dynamic_cast<const DMVPV>(
149 x_prod_plot->getVectorBlock(1));
150 RCP<const Thyra::MultiVectorBase<double> > adjoint_plot =
151 adjoint_prod_plot->getMultiVector();
152 RCP<const Thyra::VectorBase<double> > x_exact_plot =
153 model->getExactSolution(time_i).get_x();
154 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
155 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1)
156 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
157 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
158 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
159 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
160 << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
161 << get_ele(*(x_exact_plot), 1) << std::endl;
167 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
168 RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
169 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
170 Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
171 StepSize.push_back(dt);
172 double L2norm = Thyra::norm_2(*xdiff);
174 Teuchos::Array<double> L2norm_DxDp(num_param);
175 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
176 for (
int i = 0; i < num_param; ++i)
177 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
178 L2norm = std::sqrt(L2norm);
179 ErrorNorm.push_back(L2norm);
187 out <<
" Stepper = BDF2" << std::endl;
188 out <<
" =========================" << std::endl;
189 out <<
" Expected order: " << order << std::endl;
190 out <<
" Observed order: " << slope << std::endl;
191 out <<
" =========================" << std::endl;
192 TEST_FLOATING_EQUALITY(slope, 1.95006, 0.015);
193 TEST_FLOATING_EQUALITY(ErrorNorm[0], 0.0137394, 1.0e-4);
195 if (comm->getRank() == 0) {
196 std::ofstream ftmp(
"Tempus_BDF2_SinCos_AdjSens-Error.dat");
197 double error0 = 0.8 * ErrorNorm[0];
198 for (
int n = 0; n < nTimeStepSizes; n++) {
199 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
200 << error0 * (StepSize[n] / StepSize[0]) << std::endl;