12 #include "Teuchos_DefaultComm.hpp"
14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_IntegratorForwardSensitivity.hpp"
17 #include "Tempus_IntegratorPseudoTransientForwardSensitivity.hpp"
19 #include "Thyra_VectorStdOps.hpp"
20 #include "Thyra_MultiVectorStdOps.hpp"
22 #include "../TestModels/SinCosModel.hpp"
23 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
25 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
26 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
27 #include "Thyra_DefaultMultiVectorProductVector.hpp"
28 #include "Thyra_DefaultProductVector.hpp"
35 namespace Tempus_Test {
39 using Teuchos::sublist;
40 using Teuchos::getParametersFromXmlFile;
49 const bool use_dfdp_as_tangent,
52 std::vector<double> StepSize;
53 std::vector<double> ErrorNorm;
54 const int nTimeStepSizes = 7;
60 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
63 for (
int n=0; n<nTimeStepSizes; n++) {
67 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
71 scm_pl->
set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
80 if (use_combined_method)
81 sens_pl.
set(
"Sensitivity Method",
"Combined");
83 sens_pl.
set(
"Sensitivity Method",
"Staggered");
84 sens_pl.
set(
"Reuse State Linear Solver",
true);
86 sens_pl.
set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
89 pl->
sublist(
"Default Integrator")
90 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
92 Tempus::integratorForwardSensitivity<double>(pl, model);
93 order = integrator->getStepper()->getOrder();
96 double t0 = pl->sublist(
"Default Integrator")
97 .sublist(
"Time Step Control").
get<
double>(
"Initial Time");
99 model->getExactSolution(t0).get_x();
100 const int num_param = model->get_p_space(0)->dim();
102 Thyra::createMembers(model->get_x_space(), num_param);
103 for (
int i=0; i<num_param; ++i)
104 Thyra::assign(DxDp0->col(i).
ptr(),
105 *(model->getExactSensSolution(i, t0).get_x()));
106 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
107 DxDp0, Teuchos::null, Teuchos::null);
110 bool integratorStatus = integrator->advanceTime();
114 double time = integrator->getTime();
115 double timeFinal =pl->sublist(
"Default Integrator")
116 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
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) {
134 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_Sens.dat");
136 integrator->getSolutionHistory();
138 Thyra::createMembers(model->get_x_space(), num_param);
139 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
141 double time_i = solutionState->getTime();
143 Teuchos::rcp_dynamic_cast<
const DMVPV>(solutionState->getX());
145 x_prod_plot->getMultiVector()->col(0);
149 model->getExactSolution(time_i).get_x();
150 for (
int j=0; j<num_param; ++j)
151 Thyra::assign(DxDp_exact_plot->col(j).
ptr(),
152 *(model->getExactSensSolution(j, time_i).get_x()));
153 ftmp << std::fixed << std::setprecision(7)
155 << std::setw(11) << get_ele(*(x_plot), 0)
156 << std::setw(11) << get_ele(*(x_plot), 1);
157 for (
int j=0; j<num_param; ++j)
158 ftmp << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 0)
159 << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 1);
160 ftmp << std::setw(11) << get_ele(*(x_exact_plot), 0)
161 << std::setw(11) << get_ele(*(x_exact_plot), 1);
162 for (
int j=0; j<num_param; ++j)
163 ftmp << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 0)
164 << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 1);
173 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
174 Thyra::V_VmV(DxDpdiff.
ptr(), *DxDp_exact, *DxDp);
175 StepSize.push_back(dt);
176 double L2norm = Thyra::norm_2(*xdiff);
179 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
180 for (
int i=0; i<num_param; ++i)
181 L2norm += L2norm_DxDp[i]*L2norm_DxDp[i];
182 L2norm = std::sqrt(L2norm);
183 ErrorNorm.push_back(L2norm);
185 *my_out <<
" n = " << n <<
" dt = " << dt <<
" error = " << L2norm
191 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
192 *my_out <<
" Stepper = BackwardEuler" << 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_BackwardEuler_SinCos_Sens-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_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...
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...