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"
33 namespace Tempus_Test {
37 using Teuchos::sublist;
38 using Teuchos::getParametersFromXmlFile;
48 std::vector<double> StepSize;
49 std::vector<double> ErrorNorm;
50 const int nTimeStepSizes = 7;
56 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
59 for (
int n=0; n<nTimeStepSizes; n++) {
63 getParametersFromXmlFile(
"Tempus_BDF2_SinCos_SA.xml");
78 interp_pl.
set(
"Interpolator Type",
"Lagrange");
79 interp_pl.
set(
"Order", 1);
82 pl->
sublist(
"Default Integrator")
83 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
85 Tempus::integratorAdjointSensitivity<double>(pl, model);
86 order = integrator->getStepper()->getOrder();
89 double t0 = pl->sublist(
"Default Integrator")
90 .sublist(
"Time Step Control").
get<
double>(
"Initial Time");
92 model->getExactSolution(t0).get_x();
93 const int num_param = model->get_p_space(0)->dim();
95 Thyra::createMembers(model->get_x_space(), num_param);
96 for (
int i=0; i<num_param; ++i)
97 Thyra::assign(DxDp0->col(i).
ptr(),
98 *(model->getExactSensSolution(i, t0).get_x()));
99 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
100 DxDp0, Teuchos::null, Teuchos::null);
103 bool integratorStatus = integrator->advanceTime();
107 double time = integrator->getTime();
108 double timeFinal =pl->sublist(
"Default Integrator")
109 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
118 Thyra::createMembers(model->get_x_space(), num_param);
122 const int num_g = DgDp->domain()->dim();
123 for (
int i=0; i<num_g; ++i)
124 for (
int j=0; j<num_param; ++j)
125 dxdp_view(i,j) = dgdp_view(j,i);
128 model->getExactSolution(time).get_x();
130 Thyra::createMembers(model->get_x_space(), num_param);
131 for (
int i=0; i<num_param; ++i)
132 Thyra::assign(DxDp_exact->col(i).
ptr(),
133 *(model->getExactSensSolution(i, time).get_x()));
136 if (comm->getRank() == 0 && n == nTimeStepSizes-1) {
140 std::ofstream ftmp(
"Tempus_BDF2_SinCos_AdjSens.dat");
142 integrator->getSolutionHistory();
143 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
145 const double time_i = solutionState->getTime();
147 Teuchos::rcp_dynamic_cast<
const DPV>(solutionState->getX());
149 x_prod_plot->getVectorBlock(0);
151 Teuchos::rcp_dynamic_cast<
const DMVPV>(x_prod_plot->getVectorBlock(1));
153 adjoint_prod_plot->getMultiVector();
155 model->getExactSolution(time_i).get_x();
156 ftmp << std::fixed << std::setprecision(7)
158 << std::setw(11) << get_ele(*(x_plot), 0)
159 << std::setw(11) << get_ele(*(x_plot), 1)
160 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
161 << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
162 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
163 << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
164 << std::setw(11) << get_ele(*(x_exact_plot), 0)
165 << std::setw(11) << get_ele(*(x_exact_plot), 1)
174 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
175 Thyra::V_VmV(DxDpdiff.
ptr(), *DxDp_exact, *DxDp);
176 StepSize.push_back(dt);
177 double L2norm = Thyra::norm_2(*xdiff);
180 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
181 for (
int i=0; i<num_param; ++i)
182 L2norm += L2norm_DxDp[i]*L2norm_DxDp[i];
183 L2norm = std::sqrt(L2norm);
184 ErrorNorm.push_back(L2norm);
191 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
192 *my_out <<
" Stepper = BDF2" << std::endl;
193 *my_out <<
" =========================" << std::endl;
194 *my_out <<
" Expected order: " << order << std::endl;
195 *my_out <<
" Observed order: " << slope << std::endl;
196 *my_out <<
" =========================" << std::endl;
200 if (comm->getRank() == 0) {
201 std::ofstream ftmp(
"Tempus_BDF2_SinCos_AdjSens-Error.dat");
202 double error0 = 0.8*ErrorNorm[0];
203 for (
int n=0; n<nTimeStepSizes; n++) {
204 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
205 << error0*(StepSize[n]/StepSize[0]) << std::endl;
basic_FancyOStream & setProcRankAndSize(const int procRank, const int numProcs)
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...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...