9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_IntegratorAdjointSensitivity.hpp"
18 #include "Thyra_VectorStdOps.hpp"
19 #include "Thyra_MultiVectorStdOps.hpp"
21 #include "../TestModels/SinCosModel.hpp"
22 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
24 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
25 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
26 #include "Thyra_DefaultMultiVectorProductVector.hpp"
27 #include "Thyra_DefaultProductVector.hpp"
34 namespace Tempus_Test {
37 using Teuchos::ParameterList;
38 using Teuchos::sublist;
39 using Teuchos::getParametersFromXmlFile;
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 Teuchos::RCP<Teuchos::FancyOStream> my_out =
57 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
58 my_out->setProcRankAndSize(comm->getRank(), comm->getSize());
59 my_out->setOutputToRootOnly(0);
60 for (
int n=0; n<nTimeStepSizes; n++) {
63 RCP<ParameterList> pList =
64 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
67 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
68 RCP<SinCosModel<double> > model =
74 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
75 ParameterList& sens_pl = pl->sublist(
"Sensitivities");
76 sens_pl.set(
"Mass Matrix Is Identity",
false);
77 ParameterList& interp_pl =
78 pl->sublist(
"Default Integrator").sublist(
"Solution History").sublist(
"Interpolator");
79 interp_pl.set(
"Interpolator Type",
"Lagrange");
80 interp_pl.set(
"Order", 0);
83 pl->sublist(
"Default Stepper").set(
"Use FSAL",
false);
87 pl->sublist(
"Default Stepper")
88 .set(
"Initial Condition Consistency Check",
false);
91 pl->sublist(
"Default Integrator")
92 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
93 RCP<Tempus::IntegratorAdjointSensitivity<double> > integrator =
94 Tempus::integratorAdjointSensitivity<double>(pl, model);
95 order = integrator->getStepper()->getOrder();
98 double t0 = pl->sublist(
"Default Integrator")
99 .sublist(
"Time Step Control").get<
double>(
"Initial Time");
100 RCP<const Thyra::VectorBase<double> > x0 =
101 model->getExactSolution(t0).get_x();
102 const int num_param = model->get_p_space(0)->dim();
103 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
104 Thyra::createMembers(model->get_x_space(), num_param);
105 for (
int i=0; i<num_param; ++i)
106 Thyra::assign(DxDp0->col(i).ptr(),
107 *(model->getExactSensSolution(i, t0).get_x()));
108 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
109 DxDp0, Teuchos::null, Teuchos::null);
112 bool integratorStatus = integrator->advanceTime();
113 TEST_ASSERT(integratorStatus)
116 double time = integrator->getTime();
117 double timeFinal =pl->sublist(
"Default Integrator")
118 .sublist(
"Time Step Control").get<
double>(
"Final Time");
119 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
124 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
125 RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
126 RCP<Thyra::MultiVectorBase<double> > DxDp =
127 Thyra::createMembers(model->get_x_space(), num_param);
129 Thyra::ConstDetachedMultiVectorView<double> dgdp_view(*DgDp);
130 Thyra::DetachedMultiVectorView<double> dxdp_view(*DxDp);
131 const int num_g = DgDp->domain()->dim();
132 for (
int i=0; i<num_g; ++i)
133 for (
int j=0; j<num_param; ++j)
134 dxdp_view(i,j) = dgdp_view(j,i);
136 RCP<const Thyra::VectorBase<double> > x_exact =
137 model->getExactSolution(time).get_x();
138 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
139 Thyra::createMembers(model->get_x_space(), num_param);
140 for (
int i=0; i<num_param; ++i)
141 Thyra::assign(DxDp_exact->col(i).ptr(),
142 *(model->getExactSensSolution(i, time).get_x()));
145 if (comm->getRank() == 0 && n == nTimeStepSizes-1) {
146 typedef Thyra::DefaultProductVector<double> DPV;
147 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
149 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_AdjSens.dat");
151 integrator->getSolutionHistory();
152 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
153 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
154 const double time_i = solutionState->getTime();
155 RCP<const DPV> x_prod_plot =
156 Teuchos::rcp_dynamic_cast<
const DPV>(solutionState->getX());
157 RCP<const Thyra::VectorBase<double> > x_plot =
158 x_prod_plot->getVectorBlock(0);
159 RCP<const DMVPV > adjoint_prod_plot =
160 Teuchos::rcp_dynamic_cast<
const DMVPV>(x_prod_plot->getVectorBlock(1));
161 RCP<const Thyra::MultiVectorBase<double> > adjoint_plot =
162 adjoint_prod_plot->getMultiVector();
163 RCP<const Thyra::VectorBase<double> > x_exact_plot =
164 model->getExactSolution(time_i).get_x();
165 ftmp << std::fixed << std::setprecision(7)
167 << std::setw(11) << get_ele(*(x_plot), 0)
168 << std::setw(11) << get_ele(*(x_plot), 1)
169 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
170 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
171 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
172 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
173 << std::setw(11) << get_ele(*(x_exact_plot), 0)
174 << std::setw(11) << get_ele(*(x_exact_plot), 1)
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 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
202 *my_out <<
" Stepper = BackwardEuler" << std::endl;
203 *my_out <<
" =========================" << std::endl;
204 *my_out <<
" Expected order: " << order << std::endl;
205 *my_out <<
" Observed order: " << slope << std::endl;
206 *my_out <<
" =========================" << std::endl;
207 TEST_FLOATING_EQUALITY( slope, order, 0.015 );
208 TEST_FLOATING_EQUALITY( ErrorNorm[0], 0.151746, 1.0e-4 );
210 if (comm->getRank() == 0) {
211 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_AdjSens-Error.dat");
212 double error0 = 0.8*ErrorNorm[0];
213 for (
int n=0; n<nTimeStepSizes; n++) {
214 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
215 << error0*(StepSize[n]/StepSize[0]) << std::endl;
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...