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_BDF2_SinCos_SA.xml");
71 interp_pl.
set(
"Interpolator Type",
"Lagrange");
72 interp_pl.
set(
"Order", 1);
75 pl->
sublist(
"Default Integrator")
77 .
set(
"Initial Time Step", dt);
79 Tempus::createIntegratorAdjointSensitivity<double>(pl, model);
80 order = integrator->getStepper()->getOrder();
83 double t0 = pl->sublist(
"Default Integrator")
84 .sublist(
"Time Step Control")
85 .
get<
double>(
"Initial Time");
87 model->getExactSolution(t0).get_x();
88 const int num_param = model->get_p_space(0)->dim();
90 Thyra::createMembers(model->get_x_space(), num_param);
91 for (
int i = 0; i < num_param; ++i)
92 Thyra::assign(DxDp0->col(i).
ptr(),
93 *(model->getExactSensSolution(i, t0).get_x()));
94 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
95 DxDp0, Teuchos::null, Teuchos::null);
98 bool integratorStatus = integrator->advanceTime();
102 double time = integrator->getTime();
103 double timeFinal = pl->sublist(
"Default Integrator")
104 .sublist(
"Time Step Control")
105 .
get<
double>(
"Final Time");
114 Thyra::createMembers(model->get_x_space(), num_param);
118 const int num_g = DgDp->domain()->dim();
119 for (
int i = 0; i < num_g; ++i)
120 for (
int j = 0; j < num_param; ++j) dxdp_view(i, j) = dgdp_view(j, i);
123 model->getExactSolution(time).get_x();
125 Thyra::createMembers(model->get_x_space(), num_param);
126 for (
int i = 0; i < num_param; ++i)
127 Thyra::assign(DxDp_exact->col(i).
ptr(),
128 *(model->getExactSensSolution(i, time).get_x()));
131 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
135 std::ofstream ftmp(
"Tempus_BDF2_SinCos_AdjSens.dat");
137 integrator->getSolutionHistory();
138 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
140 const double time_i = solutionState->getTime();
142 Teuchos::rcp_dynamic_cast<
const DPV>(solutionState->getX());
144 x_prod_plot->getVectorBlock(0);
146 Teuchos::rcp_dynamic_cast<
const DMVPV>(
147 x_prod_plot->getVectorBlock(1));
149 adjoint_prod_plot->getMultiVector();
151 model->getExactSolution(time_i).get_x();
152 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
153 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1)
154 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
155 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
156 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
157 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
158 << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
159 << get_ele(*(x_exact_plot), 1) << std::endl;
167 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
168 Thyra::V_VmV(DxDpdiff.
ptr(), *DxDp_exact, *DxDp);
169 StepSize.push_back(dt);
170 double L2norm = Thyra::norm_2(*xdiff);
173 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
174 for (
int i = 0; i < num_param; ++i)
175 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
176 L2norm = std::sqrt(L2norm);
177 ErrorNorm.push_back(L2norm);
184 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
185 out <<
" Stepper = BDF2" << std::endl;
186 out <<
" =========================" << std::endl;
187 out <<
" Expected order: " << order << std::endl;
188 out <<
" Observed order: " << slope << std::endl;
189 out <<
" =========================" << std::endl;
193 if (comm->getRank() == 0) {
194 std::ofstream ftmp(
"Tempus_BDF2_SinCos_AdjSens-Error.dat");
195 double error0 = 0.8 * ErrorNorm[0];
196 for (
int n = 0; n < nTimeStepSizes; n++) {
197 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
198 << 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.