9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
14 #include "Thyra_VectorStdOps.hpp"
15 #include "Thyra_MultiVectorStdOps.hpp"
17 #include "Tempus_IntegratorBasic.hpp"
18 #include "Tempus_IntegratorAdjointSensitivity.hpp"
20 #include "Thyra_DefaultMultiVectorProductVector.hpp"
21 #include "Thyra_DefaultProductVector.hpp"
23 #include "../TestModels/SinCosModel.hpp"
24 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
29 namespace Tempus_Test {
32 using Teuchos::ParameterList;
33 using Teuchos::sublist;
34 using Teuchos::getParametersFromXmlFile;
44 std::vector<std::string> RKMethods;
45 RKMethods.push_back(
"RK Forward Euler");
46 RKMethods.push_back(
"RK Explicit 4 Stage");
47 RKMethods.push_back(
"RK Explicit 3/8 Rule");
48 RKMethods.push_back(
"RK Explicit 4 Stage 3rd order by Runge");
49 RKMethods.push_back(
"RK Explicit 5 Stage 3rd order by Kinnmark and Gray");
50 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order");
51 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order TVD");
52 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order by Heun");
53 RKMethods.push_back(
"RK Explicit 2 Stage 2nd order by Runge");
54 RKMethods.push_back(
"RK Explicit Trapezoidal");
55 RKMethods.push_back(
"General ERK");
56 std::vector<double> RKMethodErrors;
57 RKMethodErrors.push_back(0.154904);
58 RKMethodErrors.push_back(4.55982e-06);
59 RKMethodErrors.push_back(4.79132e-06);
60 RKMethodErrors.push_back(0.000113603);
61 RKMethodErrors.push_back(4.98796e-05);
62 RKMethodErrors.push_back(0.00014564);
63 RKMethodErrors.push_back(0.000121968);
64 RKMethodErrors.push_back(0.000109495);
65 RKMethodErrors.push_back(0.00559871);
66 RKMethodErrors.push_back(0.00710492);
67 RKMethodErrors.push_back(4.55982e-06);
69 Teuchos::RCP<const Teuchos::Comm<int> > comm =
70 Teuchos::DefaultComm<int>::getComm();
71 Teuchos::RCP<Teuchos::FancyOStream> my_out =
72 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
73 my_out->setProcRankAndSize(comm->getRank(), comm->getSize());
74 my_out->setOutputToRootOnly(0);
76 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 = 7;
86 for (
int n=0; n<nTimeStepSizes; n++) {
89 RCP<ParameterList> pList =
90 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
93 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
94 RCP<SinCosModel<double> > model =
98 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
99 if (RKMethods[m] ==
"General ERK") {
100 pl->sublist(
"Demo Integrator").set(
"Stepper Name",
"Demo Stepper 2");
101 pl->sublist(
"Demo Stepper 2").set(
"Initial Condition Consistency",
103 pl->sublist(
"Demo Stepper 2").set(
"Initial Condition Consistency Check",
106 pl->sublist(
"Demo Stepper").set(
"Stepper Type", RKMethods[m]);
107 pl->sublist(
"Demo Stepper").set(
"Initial Condition Consistency",
109 pl->sublist(
"Demo Stepper").set(
"Initial Condition Consistency Check",
117 ParameterList& sens_pl = pl->sublist(
"Sensitivities");
118 sens_pl.set(
"Mass Matrix Is Identity",
true);
119 ParameterList& interp_pl =
120 pl->sublist(
"Demo Integrator").sublist(
"Solution History").sublist(
"Interpolator");
121 interp_pl.set(
"Interpolator Type",
"Lagrange");
122 interp_pl.set(
"Order", 3);
125 pl->sublist(
"Demo Integrator")
126 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
127 RCP<Tempus::IntegratorAdjointSensitivity<double> > integrator =
128 Tempus::integratorAdjointSensitivity<double>(pl, model);
129 order = integrator->getStepper()->getOrder();
132 double t0 = pl->sublist(
"Demo Integrator")
133 .sublist(
"Time Step Control").get<
double>(
"Initial Time");
136 RCP<Thyra::VectorBase<double> > x0 =
137 model->getNominalValues().get_x()->clone_v();
138 const int num_param = model->get_p_space(0)->dim();
139 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
140 Thyra::createMembers(model->get_x_space(), num_param);
141 for (
int i=0; i<num_param; ++i)
142 Thyra::assign(DxDp0->col(i).ptr(),
143 *(model->getExactSensSolution(i, t0).get_x()));
144 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
145 DxDp0, Teuchos::null, Teuchos::null);
148 bool integratorStatus = integrator->advanceTime();
149 TEST_ASSERT(integratorStatus)
152 double time = integrator->getTime();
153 double timeFinal = pl->sublist(
"Demo Integrator")
154 .sublist(
"Time Step Control").get<
double>(
"Final Time");
155 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
160 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
161 RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
162 RCP<Thyra::MultiVectorBase<double> > DxDp =
163 Thyra::createMembers(model->get_x_space(), num_param);
165 Thyra::ConstDetachedMultiVectorView<double> dgdp_view(*DgDp);
166 Thyra::DetachedMultiVectorView<double> dxdp_view(*DxDp);
167 const int num_g = DgDp->domain()->dim();
168 for (
int i=0; i<num_g; ++i)
169 for (
int j=0; j<num_param; ++j)
170 dxdp_view(i,j) = dgdp_view(j,i);
172 RCP<const Thyra::VectorBase<double> > x_exact =
173 model->getExactSolution(time).get_x();
174 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
175 Thyra::createMembers(model->get_x_space(), num_param);
176 for (
int i=0; i<num_param; ++i)
177 Thyra::assign(DxDp_exact->col(i).ptr(),
178 *(model->getExactSensSolution(i, time).get_x()));
181 if (comm->getRank() == 0 && n == nTimeStepSizes-1) {
182 typedef Thyra::DefaultProductVector<double> DPV;
183 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
185 std::ofstream ftmp(
"Tempus_"+RKMethod_+
"_SinCos_AdjSens.dat");
187 integrator->getSolutionHistory();
188 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
189 RCP<const SolutionState<double> > solutionState =
190 (*solutionHistory)[i];
191 const double time_i = solutionState->getTime();
192 RCP<const DPV> x_prod_plot =
193 Teuchos::rcp_dynamic_cast<
const DPV>(solutionState->getX());
194 RCP<const Thyra::VectorBase<double> > x_plot =
195 x_prod_plot->getVectorBlock(0);
196 RCP<const DMVPV > adjoint_prod_plot =
197 Teuchos::rcp_dynamic_cast<
const DMVPV>(x_prod_plot->getVectorBlock(1));
198 RCP<const Thyra::MultiVectorBase<double> > adjoint_plot =
199 adjoint_prod_plot->getMultiVector();
200 RCP<const Thyra::VectorBase<double> > x_exact_plot =
201 model->getExactSolution(time_i).get_x();
202 ftmp << std::fixed << std::setprecision(7)
204 << std::setw(11) << get_ele(*(x_plot), 0)
205 << std::setw(11) << get_ele(*(x_plot), 1)
206 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
207 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
208 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
209 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
210 << std::setw(11) << get_ele(*(x_exact_plot), 0)
211 << std::setw(11) << get_ele(*(x_exact_plot), 1)
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);
232 *my_out <<
" n = " << n <<
" dt = " << dt <<
" error = " << L2norm
237 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
238 *my_out <<
" Stepper = " << RKMethods[m] << std::endl;
239 *my_out <<
" =========================" << std::endl;
240 *my_out <<
" Expected order: " << order << std::endl;
241 *my_out <<
" Observed order: " << slope << std::endl;
242 *my_out <<
" =========================" << std::endl;
243 TEST_FLOATING_EQUALITY( slope, order, 0.015 );
244 TEST_FLOATING_EQUALITY( ErrorNorm[0], RKMethodErrors[m], 1.0e-4 );
246 if (comm->getRank() == 0) {
247 std::ofstream ftmp(
"Tempus_"+RKMethod_+
"_SinCos_AdjSens-Error.dat");
248 double error0 = 0.8*ErrorNorm[0];
249 for (
int n=0; n<nTimeStepSizes; n++) {
250 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
251 << error0*(pow(StepSize[n]/StepSize[0],order)) << std::endl;
257 Teuchos::TimeMonitor::summarize();
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...