Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_BackwardEuler_FSA.hpp
Go to the documentation of this file.
1//@HEADER
2// *****************************************************************************
3// Tempus: Time Integration and Sensitivity Analysis Package
4//
5// Copyright 2017 NTESS and the Tempus contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8//@HEADER
9
10#include "Teuchos_UnitTestHarness.hpp"
11#include "Teuchos_XMLParameterListHelpers.hpp"
12#include "Teuchos_TimeMonitor.hpp"
13#include "Teuchos_DefaultComm.hpp"
14
15#include "Tempus_config.hpp"
16#include "Tempus_IntegratorBasic.hpp"
17#include "Tempus_IntegratorForwardSensitivity.hpp"
18#include "Tempus_IntegratorPseudoTransientForwardSensitivity.hpp"
19
20#include "Thyra_VectorStdOps.hpp"
21#include "Thyra_MultiVectorStdOps.hpp"
22
23#include "../TestModels/SinCosModel.hpp"
25
26#include "Thyra_DefaultMultiVectorProductVector.hpp"
27
28#include <vector>
29#include <fstream>
30#include <sstream>
31#include <limits>
32
33namespace Tempus_Test {
34
35using Teuchos::getParametersFromXmlFile;
36using Teuchos::ParameterList;
37using Teuchos::RCP;
38using Teuchos::sublist;
39
40using Tempus::IntegratorBasic;
41using Tempus::SolutionHistory;
42using Tempus::SolutionState;
43
44// ************************************************************
45// ************************************************************
46void test_sincos_fsa(const bool use_combined_method,
47 const bool use_dfdp_as_tangent, Teuchos::FancyOStream &out,
48 bool &success, const bool use_row_sum_scaling = false)
49{
50 std::vector<double> StepSize;
51 std::vector<double> ErrorNorm;
52 const int nTimeStepSizes = 7;
53 double dt = 0.2;
54 double order = 0.0;
55 Teuchos::RCP<const Teuchos::Comm<int> > comm =
56 Teuchos::DefaultComm<int>::getComm();
57 for (int n = 0; n < nTimeStepSizes; n++) {
58 // Read params from .xml file
59 RCP<ParameterList> pList =
60 getParametersFromXmlFile("Tempus_BackwardEuler_SinCos.xml");
61
62 // Setup the SinCosModel
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 =
66 Teuchos::rcp(new SinCosModel<double>(scm_pl));
67
68 dt /= 2;
69
70 // Setup sensitivities
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");
75 else {
76 sens_pl.set("Sensitivity Method", "Staggered");
77 // With row sum scaling, the sensitivity stepper has its own solver
78 sens_pl.set("Reuse State Linear Solver", !use_row_sum_scaling);
79 }
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")
84 .sublist("NOX")
85 .sublist("Thyra Group Options")
86 .set("Function Scaling", "Row Sum");
87
88 // Setup the Integrator and reset initial time step
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();
95
96 // Initial Conditions
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);
110
111 // Integrate to timeMax
112 bool integratorStatus = integrator->advanceTime();
113 TEST_ASSERT(integratorStatus)
114
115 // Test if at 'Final Time'
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);
121
122 // Time-integrated solution and the exact solution
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()));
132
133 // Plot sample solution and exact solution
134 if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
135 typedef Thyra::DefaultMultiVectorProductVector<double> DMVPV;
136
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);
167 ftmp << std::endl;
168 }
169 ftmp.close();
170 }
171
172 // Calculate the error
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);
179 L2norm *= L2norm;
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);
186
187 out << " n = " << n << " dt = " << dt << " error = " << L2norm << std::endl;
188 }
189
190 // Check the order and intercept
191 double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
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);
199
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;
206 }
207 ftmp.close();
208 }
209}
210
211} // namespace Tempus_Test
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation.
void test_sincos_fsa(const bool use_combined_method, const bool use_dfdp_as_tangent, Teuchos::FancyOStream &out, bool &success, const bool use_row_sum_scaling=false)
Scalar computeLinearRegressionLogLog(std::vector< Scalar > &x, std::vector< Scalar > &y)
Teuchos::RCP< IntegratorForwardSensitivity< Scalar > > createIntegratorForwardSensitivity()
Nonmember constructor.