13 #include "Teuchos_DefaultComm.hpp"
15 #include "Tempus_config.hpp"
16 #include "Tempus_IntegratorBasic.hpp"
17 #include "Tempus_IntegratorForwardSensitivity.hpp"
19 #include "Thyra_VectorStdOps.hpp"
20 #include "Thyra_MultiVectorStdOps.hpp"
22 #include "../TestModels/SinCosModel.hpp"
23 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
25 #include "Thyra_DefaultMultiVectorProductVector.hpp"
32 namespace Tempus_Test {
34 using Teuchos::getParametersFromXmlFile;
37 using Teuchos::sublist;
49 std::vector<double> StepSize;
50 std::vector<double> ErrorNorm;
51 const int nTimeStepSizes = 7;
56 for (
int n = 0; n < nTimeStepSizes; n++) {
58 RCP<ParameterList> pList =
59 getParametersFromXmlFile(
"Tempus_BDF2_SinCos_SA.xml");
62 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
63 scm_pl->set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
64 RCP<SinCosModel<double> > model =
70 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
71 ParameterList& sens_pl = pl->sublist(
"Sensitivities");
72 if (use_combined_method)
73 sens_pl.set(
"Sensitivity Method",
"Combined");
75 sens_pl.set(
"Sensitivity Method",
"Staggered");
78 sens_pl.set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
79 ParameterList& interp_pl = pl->sublist(
"Default Integrator")
80 .sublist(
"Solution History")
81 .sublist(
"Interpolator");
82 interp_pl.set(
"Interpolator Type",
"Lagrange");
83 interp_pl.set(
"Order", 1);
86 pl->sublist(
"Default Integrator")
87 .sublist(
"Time Step Control")
88 .set(
"Initial Time Step", dt);
89 RCP<Tempus::IntegratorForwardSensitivity<double> > integrator =
90 Tempus::createIntegratorForwardSensitivity<double>(pl, model);
91 order = integrator->getStepper()->getOrder();
94 double t0 = pl->sublist(
"Default Integrator")
95 .sublist(
"Time Step Control")
96 .get<
double>(
"Initial Time");
97 RCP<const Thyra::VectorBase<double> > x0 =
98 model->getExactSolution(t0).get_x();
99 const int num_param = model->get_p_space(0)->dim();
100 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
101 Thyra::createMembers(model->get_x_space(), num_param);
102 for (
int i = 0; i < num_param; ++i)
103 Thyra::assign(DxDp0->col(i).ptr(),
104 *(model->getExactSensSolution(i, t0).get_x()));
105 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
106 DxDp0, Teuchos::null, Teuchos::null);
109 bool integratorStatus = integrator->advanceTime();
113 double time = integrator->getTime();
114 double timeFinal = pl->sublist("Default Integrator")
115 .sublist("Time Step Control")
116 .get<
double>("Final Time");
117 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
120 RCP<const Thyra::VectorBase<
double> > x = integrator->getX();
121 RCP<const Thyra::MultiVectorBase<
double> > DxDp = integrator->getDxDp();
122 RCP<const Thyra::VectorBase<
double> > x_exact =
123 model->getExactSolution(time).get_x();
124 RCP<Thyra::MultiVectorBase<
double> > DxDp_exact =
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_BDF2_SinCos_Sens.dat");
135 RCP<const SolutionHistory<double> > solutionHistory =
136 integrator->getSolutionHistory();
137 RCP<Thyra::MultiVectorBase<double> > DxDp_exact_plot =
138 Thyra::createMembers(model->get_x_space(), num_param);
139 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
140 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
141 double time_i = solutionState->getTime();
142 RCP<const DMVPV> x_prod_plot =
143 Teuchos::rcp_dynamic_cast<
const DMVPV>(solutionState->getX());
144 RCP<const Thyra::VectorBase<double> > x_plot =
145 x_prod_plot->getMultiVector()->col(0);
146 RCP<const Thyra::MultiVectorBase<double> > DxDp_plot =
147 x_prod_plot->getMultiVector()->subView(
149 RCP<const Thyra::VectorBase<double> > x_exact_plot =
150 model->getExactSolution(time_i).get_x();
151 for (
int j = 0; j < num_param; ++j)
152 Thyra::assign(DxDp_exact_plot->col(j).ptr(),
153 *(model->getExactSensSolution(j, time_i).get_x()));
154 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
155 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1);
156 for (
int j = 0; j < num_param; ++j)
157 ftmp << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 0)
158 << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 1);
159 ftmp << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
160 << get_ele(*(x_exact_plot), 1);
161 for (
int j = 0; j < num_param; ++j)
162 ftmp << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 0)
163 << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 1);
170 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
171 RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
172 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
173 Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
174 StepSize.push_back(dt);
175 double L2norm = Thyra::norm_2(*xdiff);
178 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
179 for (
int i = 0; i < num_param; ++i)
180 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
181 L2norm = std::sqrt(L2norm);
182 ErrorNorm.push_back(L2norm);
184 out <<
" n = " << n <<
" dt = " << dt <<
" error = " << L2norm << std::endl;
188 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
189 out <<
" Stepper = BDF2" << std::endl;
190 out <<
" =========================" << std::endl;
191 out <<
" Expected order: " << order << std::endl;
192 out <<
" Observed order: " << slope << std::endl;
193 out <<
" =========================" << std::endl;
197 if (comm->getRank() == 0) {
198 std::ofstream ftmp(
"Tempus_BDF2_SinCos_Sens-Error.dat");
199 double error0 = 0.8 * ErrorNorm[0];
200 for (
int n = 0; n < nTimeStepSizes; n++) {
201 ftmp << StepSize[n] <<
" " << ErrorNorm[n] <<
" "
202 << error0 * (StepSize[n] / StepSize[0]) << std::endl;
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
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...
Solution state for integrators and steppers.