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 "Thyra_DefaultMultiVectorProductVector.hpp"
31 namespace Tempus_Test {
33 using Teuchos::getParametersFromXmlFile;
36 using Teuchos::sublist;
46 std::vector<double> StepSize;
47 std::vector<double> ErrorNorm;
48 const int nTimeStepSizes = 7;
53 for (
int n = 0; n < nTimeStepSizes; n++) {
56 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos_ASA.xml");
71 sens_pl.
set(
"Mass Matrix Is Identity",
false);
75 interp_pl.
set(
"Interpolator Type",
"Lagrange");
76 interp_pl.
set(
"Order", 0);
79 pl->
sublist(
"Default Stepper").
set(
"Use FSAL",
false);
84 .
set(
"Initial Condition Consistency Check",
false);
87 pl->
sublist(
"Default Integrator")
89 .
set(
"Initial Time Step", dt);
91 Tempus::createIntegratorAdjointSensitivity<double>(pl, model,
93 order = integrator->getStepper()->getOrder();
96 double t0 = pl->sublist(
"Default Integrator")
97 .sublist(
"Time Step Control")
98 .
get<
double>(
"Initial Time");
100 model->getExactSolution(t0).get_x();
101 const int num_param = model->get_p_space(0)->dim();
103 Thyra::createMembers(model->get_x_space(), num_param);
104 for (
int i = 0; i < num_param; ++i)
105 Thyra::assign(DxDp0->col(i).
ptr(),
106 *(model->getExactSensSolution(i, t0).get_x()));
107 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
108 DxDp0, Teuchos::null, Teuchos::null);
111 bool integratorStatus = integrator->advanceTime();
115 double time = integrator->getTime();
116 double timeFinal = pl->sublist(
"Default Integrator")
117 .sublist(
"Time Step Control")
118 .
get<
double>(
"Final Time");
127 Thyra::createMembers(model->get_x_space(), num_param);
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) dxdp_view(i, j) = dgdp_view(j, i);
136 model->getExactSolution(time).get_x();
138 Thyra::createMembers(model->get_x_space(), num_param);
139 for (
int i = 0; i < num_param; ++i)
140 Thyra::assign(DxDp_exact->col(i).
ptr(),
141 *(model->getExactSensSolution(i, time).get_x()));
144 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
148 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_AdjSens.dat");
150 integrator->getSolutionHistory();
151 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
153 const double time_i = solutionState->getTime();
155 Teuchos::rcp_dynamic_cast<
const DPV>(solutionState->getX());
157 x_prod_plot->getVectorBlock(0);
159 Teuchos::rcp_dynamic_cast<
const DMVPV>(
160 x_prod_plot->getVectorBlock(1));
162 adjoint_prod_plot->getMultiVector();
164 model->getExactSolution(time_i).get_x();
165 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
166 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1)
167 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
168 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
169 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
170 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
171 << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
172 << get_ele(*(x_exact_plot), 1) << std::endl;
180 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
181 Thyra::V_VmV(DxDpdiff.
ptr(), *DxDp_exact, *DxDp);
182 StepSize.push_back(dt);
183 double L2norm = Thyra::norm_2(*xdiff);
186 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
187 for (
int i = 0; i < num_param; ++i)
188 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
189 L2norm = std::sqrt(L2norm);
190 ErrorNorm.push_back(L2norm);
197 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
198 out <<
" Stepper = BackwardEuler" << std::endl;
199 out <<
" =========================" << std::endl;
200 out <<
" Expected order: " << order << std::endl;
201 out <<
" Observed order: " << slope << std::endl;
202 out <<
" =========================" << std::endl;
206 if (comm->getRank() == 0) {
207 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_AdjSens-Error.dat");
208 double error0 = 0.8 * ErrorNorm[0];
209 for (
int n = 0; n < nTimeStepSizes; n++) {
210 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
211 << error0 * (StepSize[n] / StepSize[0]) << std::endl;
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Solution state for integrators and steppers.