46 std::vector<std::string> RKMethods;
47 RKMethods.push_back(
"General DIRK");
48 RKMethods.push_back(
"RK Backward Euler");
49 RKMethods.push_back(
"DIRK 1 Stage Theta Method");
50 RKMethods.push_back(
"RK Implicit 1 Stage 1st order Radau IA");
51 RKMethods.push_back(
"SDIRK 2 Stage 2nd order");
52 RKMethods.push_back(
"RK Implicit 2 Stage 2nd order Lobatto IIIB");
53 RKMethods.push_back(
"SDIRK 2 Stage 3rd order");
54 RKMethods.push_back(
"EDIRK 2 Stage 3rd order");
55 RKMethods.push_back(
"EDIRK 2 Stage Theta Method");
56 RKMethods.push_back(
"SDIRK 3 Stage 4th order");
57 RKMethods.push_back(
"SDIRK 5 Stage 4th order");
58 RKMethods.push_back(
"SDIRK 5 Stage 5th order");
60 std::vector<double> RKMethodErrors;
61 RKMethodErrors.push_back(8.48235e-05);
62 RKMethodErrors.push_back(0.0383339);
63 RKMethodErrors.push_back(0.000221028);
64 RKMethodErrors.push_back(0.0428449);
65 RKMethodErrors.push_back(8.48235e-05);
66 RKMethodErrors.push_back(0.000297933);
67 RKMethodErrors.push_back(4.87848e-06);
68 RKMethodErrors.push_back(7.30827e-07);
69 RKMethodErrors.push_back(0.000272997);
70 RKMethodErrors.push_back(3.10132e-07);
71 RKMethodErrors.push_back(7.56838e-10);
72 RKMethodErrors.push_back(1.32374e-10);
74 Teuchos::RCP<const Teuchos::Comm<int> > comm =
75 Teuchos::DefaultComm<int>::getComm();
77 for (std::vector<std::string>::size_type m = 0; m != RKMethods.size(); m++) {
78 std::string RKMethod_ = RKMethods[m];
79 std::replace(RKMethod_.begin(), RKMethod_.end(),
' ',
'_');
80 std::replace(RKMethod_.begin(), RKMethod_.end(),
'/',
'.');
81 std::vector<double> StepSize;
82 std::vector<double> ErrorNorm;
83 const int nTimeStepSizes = 2;
86 for (
int n = 0; n < nTimeStepSizes; n++) {
88 RCP<ParameterList> pList =
89 getParametersFromXmlFile(
"Tempus_DIRK_SinCos.xml");
92 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
93 RCP<SinCosModel<double> > model =
97 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
98 pl->sublist(
"Default Stepper").set(
"Stepper Type", RKMethods[m]);
99 if (RKMethods[m] ==
"DIRK 1 Stage Theta Method" ||
100 RKMethods[m] ==
"EDIRK 2 Stage Theta Method") {
101 pl->sublist(
"Default Stepper").set<
double>(
"theta", 0.5);
103 else if (RKMethods[m] ==
"SDIRK 2 Stage 2nd order") {
104 pl->sublist(
"Default Stepper").set(
"gamma", 0.2928932188134524);
106 else if (RKMethods[m] ==
"SDIRK 2 Stage 3rd order") {
107 pl->sublist(
"Default Stepper")
108 .set<std::string>(
"Gamma Type",
"3rd Order A-stable");
114 ParameterList& sens_pl = pl->sublist(
"Sensitivities");
115 sens_pl.set(
"Mass Matrix Is Identity",
true);
116 ParameterList& interp_pl = pl->sublist(
"Default Integrator")
117 .sublist(
"Solution History")
118 .sublist(
"Interpolator");
119 interp_pl.set(
"Interpolator Type",
"Lagrange");
120 interp_pl.set(
"Order", 4);
123 pl->sublist(
"Default Integrator")
124 .sublist(
"Time Step Control")
125 .set(
"Initial Time Step", dt);
126 RCP<Tempus::IntegratorAdjointSensitivity<double> > integrator =
128 order = integrator->getStepper()->getOrder();
135 RCP<Thyra::VectorBase<double> > x0 =
136 model->getNominalValues().get_x()->clone_v();
137 const int num_param = model->get_p_space(0)->dim();
138 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
139 Thyra::createMembers(model->get_x_space(), num_param);
140 for (
int i = 0; i < num_param; ++i)
141 Thyra::assign(DxDp0->col(i).ptr(),
142 *(model->getExactSensSolution(i, 0.0).get_x()));
143 integrator->initializeSolutionHistory(0.0, x0, Teuchos::null,
144 Teuchos::null, DxDp0, Teuchos::null,
148 bool integratorStatus = integrator->advanceTime();
149 TEST_ASSERT(integratorStatus)
152 double time = integrator->getTime();
153 double timeFinal = pl->sublist(
"Default Integrator")
154 .sublist(
"Time Step Control")
155 .get<
double>(
"Final Time");
156 double tol = 100.0 * std::numeric_limits<double>::epsilon();
157 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
162 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
163 RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
164 RCP<Thyra::MultiVectorBase<double> > DxDp =
165 Thyra::createMembers(model->get_x_space(), num_param);
167 Thyra::ConstDetachedMultiVectorView<double> dgdp_view(*DgDp);
168 Thyra::DetachedMultiVectorView<double> dxdp_view(*DxDp);
169 const int num_g = DgDp->domain()->dim();
170 for (
int i = 0; i < num_g; ++i)
171 for (
int j = 0; j < num_param; ++j) dxdp_view(i, j) = dgdp_view(j, i);
173 RCP<const Thyra::VectorBase<double> > x_exact =
174 model->getExactSolution(time).get_x();
175 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
176 Thyra::createMembers(model->get_x_space(), num_param);
177 for (
int i = 0; i < num_param; ++i)
178 Thyra::assign(DxDp_exact->col(i).ptr(),
179 *(model->getExactSensSolution(i, time).get_x()));
182 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
183 typedef Thyra::DefaultProductVector<double> DPV;
184 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
186 std::ofstream ftmp(
"Tempus_" + RKMethod_ +
"_SinCos_AdjSens.dat");
187 RCP<const SolutionHistory<double> > solutionHistory =
188 integrator->getSolutionHistory();
189 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
190 RCP<const SolutionState<double> > solutionState =
191 (*solutionHistory)[i];
192 const double time_i = solutionState->getTime();
193 RCP<const DPV> x_prod_plot =
194 Teuchos::rcp_dynamic_cast<const DPV>(solutionState->getX());
195 RCP<const Thyra::VectorBase<double> > x_plot =
196 x_prod_plot->getVectorBlock(0);
197 RCP<const DMVPV> adjoint_prod_plot =
198 Teuchos::rcp_dynamic_cast<const DMVPV>(
199 x_prod_plot->getVectorBlock(1));
200 RCP<const Thyra::MultiVectorBase<double> > adjoint_plot =
201 adjoint_prod_plot->getMultiVector();
202 RCP<const Thyra::VectorBase<double> > x_exact_plot =
203 model->getExactSolution(time_i).get_x();
204 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
205 << get_ele(*(x_plot), 0) << std::setw(11)
206 << get_ele(*(x_plot), 1) << std::setw(11)
207 << get_ele(*(adjoint_plot->col(0)), 0) << std::setw(11)
208 << get_ele(*(adjoint_plot->col(0)), 1) << std::setw(11)
209 << get_ele(*(adjoint_plot->col(1)), 0) << std::setw(11)
210 << get_ele(*(adjoint_plot->col(1)), 1) << std::setw(11)
211 << get_ele(*(x_exact_plot), 0) << std::setw(11)
212 << get_ele(*(x_exact_plot), 1) << std::endl;
218 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
219 RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
220 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
221 Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
222 StepSize.push_back(dt);
223 double L2norm = Thyra::norm_2(*xdiff);
225 Teuchos::Array<double> L2norm_DxDp(num_param);
226 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
227 for (
int i = 0; i < num_param; ++i)
228 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
229 L2norm = std::sqrt(L2norm);
230 ErrorNorm.push_back(L2norm);
236 if (comm->getRank() == 0) {
237 std::ofstream ftmp(
"Tempus_" + RKMethod_ +
"_SinCos_AdjSens-Error.dat");
238 double error0 = 0.8 * ErrorNorm[0];
239 for (
int n = 0; n < (int)StepSize.size(); n++) {
240 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
241 << error0 * (pow(StepSize[n] / StepSize[0], order)) << std::endl;
256 out <<
" Stepper = " << RKMethods[m] << std::endl;
257 out <<
" =========================" << std::endl;
258 out <<
" Expected order: " << order << std::endl;
259 out <<
" Observed order: " << slope << std::endl;
260 out <<
" =========================" << std::endl;
261 TEST_FLOATING_EQUALITY(slope, order, 0.03);
262 TEST_FLOATING_EQUALITY(ErrorNorm[0], RKMethodErrors[m], 5.0e-4);
264 Teuchos::TimeMonitor::summarize();