47 const bool use_dfdp_as_tangent, Teuchos::FancyOStream &out,
48 bool &success,
const bool use_row_sum_scaling =
false)
50 std::vector<double> StepSize;
51 std::vector<double> ErrorNorm;
52 const int nTimeStepSizes = 7;
55 Teuchos::RCP<const Teuchos::Comm<int> > comm =
56 Teuchos::DefaultComm<int>::getComm();
57 for (
int n = 0; n < nTimeStepSizes; n++) {
59 RCP<ParameterList> pList =
60 getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
63 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
64 scm_pl->set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
65 RCP<SinCosModel<double> > model =
71 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
72 ParameterList &sens_pl = pl->sublist(
"Sensitivities");
73 if (use_combined_method)
74 sens_pl.set(
"Sensitivity Method",
"Combined");
76 sens_pl.set(
"Sensitivity Method",
"Staggered");
78 sens_pl.set(
"Reuse State Linear Solver", !use_row_sum_scaling);
80 sens_pl.set(
"Use DfDp as Tangent", use_dfdp_as_tangent);
81 if (use_row_sum_scaling)
82 pl->sublist(
"Default Stepper")
83 .sublist(
"Default Solver")
85 .sublist(
"Thyra Group Options")
86 .set(
"Function Scaling",
"Row Sum");
89 pl->sublist(
"Default Integrator")
90 .sublist(
"Time Step Control")
91 .set(
"Initial Time Step", dt);
92 RCP<Tempus::IntegratorForwardSensitivity<double> > integrator =
94 order = integrator->getStepper()->getOrder();
97 double t0 = pl->sublist(
"Default Integrator")
98 .sublist(
"Time Step Control")
99 .get<
double>(
"Initial Time");
100 RCP<const Thyra::VectorBase<double> > x0 =
101 model->getExactSolution(t0).get_x();
102 const int num_param = model->get_p_space(0)->dim();
103 RCP<Thyra::MultiVectorBase<double> > DxDp0 =
104 Thyra::createMembers(model->get_x_space(), num_param);
105 for (
int i = 0; i < num_param; ++i)
106 Thyra::assign(DxDp0->col(i).ptr(),
107 *(model->getExactSensSolution(i, t0).get_x()));
108 integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
109 DxDp0, Teuchos::null, Teuchos::null);
112 bool integratorStatus = integrator->advanceTime();
113 TEST_ASSERT(integratorStatus)
116 double time = integrator->getTime();
117 double timeFinal = pl->sublist(
"Default Integrator")
118 .sublist(
"Time Step Control")
119 .get<
double>(
"Final Time");
120 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
123 RCP<const Thyra::VectorBase<double> > x = integrator->getX();
124 RCP<const Thyra::MultiVectorBase<double> > DxDp = integrator->getDxDp();
125 RCP<const Thyra::VectorBase<double> > x_exact =
126 model->getExactSolution(time).get_x();
127 RCP<Thyra::MultiVectorBase<double> > DxDp_exact =
128 Thyra::createMembers(model->get_x_space(), num_param);
129 for (
int i = 0; i < num_param; ++i)
130 Thyra::assign(DxDp_exact->col(i).ptr(),
131 *(model->getExactSensSolution(i, time).get_x()));
134 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
135 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
137 std::ofstream ftmp(
"Tempus_BackwardEuler_SinCos_Sens.dat");
138 RCP<const SolutionHistory<double> > solutionHistory =
139 integrator->getSolutionHistory();
140 RCP<Thyra::MultiVectorBase<double> > DxDp_exact_plot =
141 Thyra::createMembers(model->get_x_space(), num_param);
142 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
143 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
144 double time_i = solutionState->getTime();
145 RCP<const DMVPV> x_prod_plot =
146 Teuchos::rcp_dynamic_cast<const DMVPV>(solutionState->getX());
147 RCP<const Thyra::VectorBase<double> > x_plot =
148 x_prod_plot->getMultiVector()->col(0);
149 RCP<const Thyra::MultiVectorBase<double> > DxDp_plot =
150 x_prod_plot->getMultiVector()->subView(
151 Teuchos::Range1D(1, num_param));
152 RCP<const Thyra::VectorBase<double> > x_exact_plot =
153 model->getExactSolution(time_i).get_x();
154 for (
int j = 0; j < num_param; ++j)
155 Thyra::assign(DxDp_exact_plot->col(j).ptr(),
156 *(model->getExactSensSolution(j, time_i).get_x()));
157 ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
158 << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1);
159 for (
int j = 0; j < num_param; ++j)
160 ftmp << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 0)
161 << std::setw(11) << get_ele(*(DxDp_plot->col(j)), 1);
162 ftmp << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
163 << get_ele(*(x_exact_plot), 1);
164 for (
int j = 0; j < num_param; ++j)
165 ftmp << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 0)
166 << std::setw(11) << get_ele(*(DxDp_exact_plot->col(j)), 1);
173 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
174 RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
175 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
176 Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
177 StepSize.push_back(dt);
178 double L2norm = Thyra::norm_2(*xdiff);
180 Teuchos::Array<double> L2norm_DxDp(num_param);
181 Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
182 for (
int i = 0; i < num_param; ++i)
183 L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
184 L2norm = std::sqrt(L2norm);
185 ErrorNorm.push_back(L2norm);
187 out <<
" n = " << n <<
" dt = " << dt <<
" error = " << L2norm << std::endl;
192 out <<
" Stepper = BackwardEuler" << std::endl;
193 out <<
" =========================" << std::endl;
194 out <<
" Expected order: " << order << std::endl;
195 out <<
" Observed order: " << slope << std::endl;
196 out <<
" =========================" << std::endl;
197 TEST_FLOATING_EQUALITY(slope, order, 0.015);
198 TEST_FLOATING_EQUALITY(ErrorNorm[0], 0.163653, 1.0e-4);
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;