49 std::vector<double> StepSize;
50 std::vector<double> ErrorNorm;
51 const int nTimeStepSizes = 7;
54 Teuchos::RCP<const Teuchos::Comm<int> > comm =
55 Teuchos::DefaultComm<int>::getComm();
56 for (
int n = 0; n < nTimeStepSizes; n++) {
58 RCP<ParameterList> pList =
59 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos_ASA.xml");
63 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
64 RCP<SinCosModel<double> > model =
66 RCP<SinCosModelAdjoint<double> > adjoint_model =
72 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
73 ParameterList& sens_pl = pl->sublist(
"Sensitivities");
74 sens_pl.set(
"Mass Matrix Is Identity",
false);
75 ParameterList& interp_pl = pl->sublist(
"Default Integrator")
76 .sublist(
"Solution History")
77 .sublist(
"Interpolator");
78 interp_pl.set(
"Interpolator Type",
"Lagrange");
79 interp_pl.set(
"Order", 0);
82 pl->sublist(
"Default Stepper").set(
"Use FSAL",
false);
86 pl->sublist(
"Default Stepper")
87 .set(
"Initial Condition Consistency Check",
false);
90 pl->sublist(
"Default Integrator")
91 .sublist(
"Time Step Control")
92 .set(
"Initial Time Step", dt);
93 RCP<Tempus::IntegratorAdjointSensitivity<double> > integrator =
96 order = integrator->getStepper()->getOrder();
99 double t0 = pl->sublist(
"Default Integrator")
100 .sublist(
"Time Step Control")
101 .get<
double>(
"Initial Time");
102 RCP<const Thyra::VectorBase<double> > x0 =
103 model->getExactSolution(t0).get_x();
104 const int num_param = model->get_p_space(0)->dim();
105 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
106 Thyra::createMembers(model->get_x_space(), num_param);
107 for (
int i = 0; i < num_param; ++i)
108 Thyra::assign(DxDp0->col(i).ptr(),
109 *(model->getExactSensSolution(i, t0).get_x()));
110 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
111 DxDp0, Teuchos::null, Teuchos::null);
114 bool integratorStatus = integrator->advanceTime();
115 TEST_ASSERT(integratorStatus)
118 double time = integrator->getTime();
119 double timeFinal = pl->sublist(
"Default Integrator")
120 .sublist(
"Time Step Control")
121 .get<
double>(
"Final Time");
122 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
127 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
128 RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
129 RCP<Thyra::MultiVectorBase<double> > DxDp =
130 Thyra::createMembers(model->get_x_space(), num_param);
132 Thyra::ConstDetachedMultiVectorView<double> dgdp_view(*DgDp);
133 Thyra::DetachedMultiVectorView<double> dxdp_view(*DxDp);
134 const int num_g = DgDp->domain()->dim();
135 for (
int i = 0; i < num_g; ++i)
136 for (
int j = 0; j < num_param; ++j) dxdp_view(i, j) = dgdp_view(j, i);
138 RCP<const Thyra::VectorBase<double> > x_exact =
139 model->getExactSolution(time).get_x();
140 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
141 Thyra::createMembers(model->get_x_space(), num_param);
142 for (
int i = 0; i < num_param; ++i)
143 Thyra::assign(DxDp_exact->col(i).ptr(),
144 *(model->getExactSensSolution(i, time).get_x()));
147 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
148 typedef Thyra::DefaultProductVector<double> DPV;
149 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
151 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_AdjSens.dat");
152 RCP<const SolutionHistory<double> > solutionHistory =
153 integrator->getSolutionHistory();
154 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
155 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
156 const double time_i = solutionState->getTime();
157 RCP<const DPV> x_prod_plot =
158 Teuchos::rcp_dynamic_cast<const DPV>(solutionState->getX());
159 RCP<const Thyra::VectorBase<double> > x_plot =
160 x_prod_plot->getVectorBlock(0);
161 RCP<const DMVPV> adjoint_prod_plot =
162 Teuchos::rcp_dynamic_cast<const DMVPV>(
163 x_prod_plot->getVectorBlock(1));
164 RCP<const Thyra::MultiVectorBase<double> > adjoint_plot =
165 adjoint_prod_plot->getMultiVector();
166 RCP<const Thyra::VectorBase<double> > x_exact_plot =
167 model->getExactSolution(time_i).get_x();
168 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
169 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1)
170 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
171 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
172 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
173 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
174 << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
175 << get_ele(*(x_exact_plot), 1) << std::endl;
181 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
182 RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
183 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
184 Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
185 StepSize.push_back(dt);
186 double L2norm = Thyra::norm_2(*xdiff);
188 Teuchos::Array<double> L2norm_DxDp(num_param);
189 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
190 for (
int i = 0; i < num_param; ++i)
191 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
192 L2norm = std::sqrt(L2norm);
193 ErrorNorm.push_back(L2norm);
201 out <<
" Stepper = BackwardEuler" << std::endl;
202 out <<
" =========================" << std::endl;
203 out <<
" Expected order: " << order << std::endl;
204 out <<
" Observed order: " << slope << std::endl;
205 out <<
" =========================" << std::endl;
206 TEST_FLOATING_EQUALITY(slope, order, 0.015);
207 TEST_FLOATING_EQUALITY(ErrorNorm[0], 0.142525, 1.0e-4);
209 if (comm->getRank() == 0) {
210 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_AdjSens-Error.dat");
211 double error0 = 0.8 * ErrorNorm[0];
212 for (
int n = 0; n < nTimeStepSizes; n++) {
213 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
214 << error0 * (StepSize[n] / StepSize[0]) << std::endl;