13 #include "Teuchos_DefaultComm.hpp"
15 #include "Tempus_config.hpp"
16 #include "Tempus_IntegratorBasic.hpp"
17 #include "Tempus_IntegratorForwardSensitivity.hpp"
18 #include "Tempus_IntegratorPseudoTransientForwardSensitivity.hpp"
20 #include "Thyra_VectorStdOps.hpp"
21 #include "Thyra_MultiVectorStdOps.hpp"
23 #include "../TestModels/SinCosModel.hpp"
24 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
26 #include "Thyra_DefaultMultiVectorProductVector.hpp"
33 namespace Tempus_Test {
35 using Teuchos::getParametersFromXmlFile;
38 using Teuchos::sublist;
50 std::vector<double> StepSize;
51 std::vector<double> ErrorNorm;
52 const int nTimeStepSizes = 7;
57 for (
int n = 0; n < nTimeStepSizes; n++) {
60 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
64 scm_pl->
set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
73 if (use_combined_method)
74 sens_pl.
set(
"Sensitivity Method",
"Combined");
76 sens_pl.
set(
"Sensitivity Method",
"Staggered");
77 sens_pl.
set(
"Reuse State Linear Solver",
true);
79 sens_pl.
set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
82 pl->
sublist(
"Default Integrator")
84 .
set(
"Initial Time Step", dt);
86 Tempus::createIntegratorForwardSensitivity<double>(pl, model);
87 order = integrator->getStepper()->getOrder();
90 double t0 = pl->sublist(
"Default Integrator")
91 .sublist(
"Time Step Control")
92 .
get<
double>(
"Initial Time");
94 model->getExactSolution(t0).get_x();
95 const int num_param = model->get_p_space(0)->dim();
97 Thyra::createMembers(model->get_x_space(), num_param);
98 for (
int i = 0; i < num_param; ++i)
99 Thyra::assign(DxDp0->col(i).
ptr(),
100 *(model->getExactSensSolution(i, t0).get_x()));
101 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
102 DxDp0, Teuchos::null, Teuchos::null);
105 bool integratorStatus = integrator->advanceTime();
109 double time = integrator->getTime();
110 double timeFinal = pl->sublist(
"Default Integrator")
111 .sublist(
"Time Step Control")
112 .
get<
double>(
"Final Time");
119 model->getExactSolution(time).get_x();
121 Thyra::createMembers(model->get_x_space(), num_param);
122 for (
int i = 0; i < num_param; ++i)
123 Thyra::assign(DxDp_exact->col(i).
ptr(),
124 *(model->getExactSensSolution(i, time).get_x()));
127 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
130 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_Sens.dat");
132 integrator->getSolutionHistory();
134 Thyra::createMembers(model->get_x_space(), num_param);
135 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
137 double time_i = solutionState->getTime();
139 Teuchos::rcp_dynamic_cast<
const DMVPV>(solutionState->getX());
141 x_prod_plot->getMultiVector()->col(0);
143 x_prod_plot->getMultiVector()->subView(
146 model->getExactSolution(time_i).get_x();
147 for (
int j = 0; j < num_param; ++j)
148 Thyra::assign(DxDp_exact_plot->col(j).
ptr(),
149 *(model->getExactSensSolution(j, time_i).get_x()));
150 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
151 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1);
152 for (
int j = 0; j < num_param; ++j)
153 ftmp << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 0)
154 << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 1);
155 ftmp << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
156 << get_ele(*(x_exact_plot), 1);
157 for (
int j = 0; j < num_param; ++j)
158 ftmp << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 0)
159 << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 1);
168 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
169 Thyra::V_VmV(DxDpdiff.
ptr(), *DxDp_exact, *DxDp);
170 StepSize.push_back(dt);
171 double L2norm = Thyra::norm_2(*xdiff);
174 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
175 for (
int i = 0; i < num_param; ++i)
176 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
177 L2norm = std::sqrt(L2norm);
178 ErrorNorm.push_back(L2norm);
180 out <<
" n = " << n <<
" dt = " << dt <<
" error = " << L2norm << std::endl;
184 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
185 out <<
" Stepper = BackwardEuler" << 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_BackwardEuler_SinCos_Sens-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;
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void test_sincos_fsa(const bool use_combined_method, const bool use_dfdp_as_tangent, Teuchos::FancyOStream &out, bool &success)
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.