Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_BDF2_ASA.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_IntegratorAdjointSensitivity.hpp"
17 
18 #include "Thyra_VectorStdOps.hpp"
19 #include "Thyra_MultiVectorStdOps.hpp"
20 
21 #include "../TestModels/SinCosModel.hpp"
22 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
23 
24 #include "Thyra_DefaultMultiVectorProductVector.hpp"
25 
26 #include <fstream>
27 #include <limits>
28 #include <sstream>
29 #include <vector>
30 
31 namespace Tempus_Test {
32 
33 using Teuchos::getParametersFromXmlFile;
35 using Teuchos::RCP;
36 using Teuchos::sublist;
37 
41 
42 // ************************************************************
43 // ************************************************************
44 TEUCHOS_UNIT_TEST(BDF2, SinCos_ASA)
45 {
46  std::vector<double> StepSize;
47  std::vector<double> ErrorNorm;
48  const int nTimeStepSizes = 7;
49  double dt = 0.2;
50  double order = 0.0;
53  for (int n = 0; n < nTimeStepSizes; n++) {
54  // Read params from .xml file
55  RCP<ParameterList> pList =
56  getParametersFromXmlFile("Tempus_BDF2_SinCos_SA.xml");
57 
58  // Setup the SinCosModel
59  RCP<ParameterList> scm_pl = sublist(pList, "SinCosModel", true);
60  RCP<SinCosModel<double> > model =
61  Teuchos::rcp(new SinCosModel<double>(scm_pl));
62 
63  dt /= 2;
64 
65  // Setup sensitivities
66  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
67  // ParameterList& sens_pl = pl->sublist("Sensitivities");
68  ParameterList& interp_pl = pl->sublist("Default Integrator")
69  .sublist("Solution History")
70  .sublist("Interpolator");
71  interp_pl.set("Interpolator Type", "Lagrange");
72  interp_pl.set("Order", 1);
73 
74  // Setup the Integrator and reset initial time step
75  pl->sublist("Default Integrator")
76  .sublist("Time Step Control")
77  .set("Initial Time Step", dt);
79  Tempus::createIntegratorAdjointSensitivity<double>(pl, model);
80  order = integrator->getStepper()->getOrder();
81 
82  // Initial Conditions
83  double t0 = pl->sublist("Default Integrator")
84  .sublist("Time Step Control")
85  .get<double>("Initial Time");
87  model->getExactSolution(t0).get_x();
88  const int num_param = model->get_p_space(0)->dim();
90  Thyra::createMembers(model->get_x_space(), num_param);
91  for (int i = 0; i < num_param; ++i)
92  Thyra::assign(DxDp0->col(i).ptr(),
93  *(model->getExactSensSolution(i, t0).get_x()));
94  integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
95  DxDp0, Teuchos::null, Teuchos::null);
96 
97  // Integrate to timeMax
98  bool integratorStatus = integrator->advanceTime();
99  TEST_ASSERT(integratorStatus)
100 
101  // Test if at 'Final Time'
102  double time = integrator->getTime();
103  double timeFinal = pl->sublist("Default Integrator")
104  .sublist("Time Step Control")
105  .get<double>("Final Time");
106  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
107 
108  // Time-integrated solution and the exact solution along with
109  // sensitivities (relying on response g(x) = x). Note we must transpose
110  // dg/dp since the integrator returns it in gradient form.
111  RCP<const Thyra::VectorBase<double> > x = integrator->getX();
112  RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
114  Thyra::createMembers(model->get_x_space(), num_param);
115  {
118  const int num_g = DgDp->domain()->dim();
119  for (int i = 0; i < num_g; ++i)
120  for (int j = 0; j < num_param; ++j) dxdp_view(i, j) = dgdp_view(j, i);
121  }
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()));
129 
130  // Plot sample solution and exact solution
131  if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
134 
135  std::ofstream ftmp("Tempus_BDF2_SinCos_AdjSens.dat");
136  RCP<const SolutionHistory<double> > solutionHistory =
137  integrator->getSolutionHistory();
138  for (int i = 0; i < solutionHistory->getNumStates(); i++) {
139  RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
140  const double time_i = solutionState->getTime();
141  RCP<const DPV> x_prod_plot =
142  Teuchos::rcp_dynamic_cast<const DPV>(solutionState->getX());
144  x_prod_plot->getVectorBlock(0);
145  RCP<const DMVPV> adjoint_prod_plot =
146  Teuchos::rcp_dynamic_cast<const DMVPV>(
147  x_prod_plot->getVectorBlock(1));
149  adjoint_prod_plot->getMultiVector();
150  RCP<const Thyra::VectorBase<double> > x_exact_plot =
151  model->getExactSolution(time_i).get_x();
152  ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
153  << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1)
154  << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
155  << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
156  << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
157  << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
158  << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
159  << get_ele(*(x_exact_plot), 1) << std::endl;
160  }
161  ftmp.close();
162  }
163 
164  // Calculate the error
165  RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
166  RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
167  Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
168  Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
169  StepSize.push_back(dt);
170  double L2norm = Thyra::norm_2(*xdiff);
171  L2norm *= L2norm;
172  Teuchos::Array<double> L2norm_DxDp(num_param);
173  Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
174  for (int i = 0; i < num_param; ++i)
175  L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
176  L2norm = std::sqrt(L2norm);
177  ErrorNorm.push_back(L2norm);
178 
179  // out << " n = " << n << " dt = " << dt << " error = " << L2norm
180  // << std::endl;
181  }
182 
183  // Check the order and intercept
184  double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
185  out << " Stepper = BDF2" << std::endl;
186  out << " =========================" << std::endl;
187  out << " Expected order: " << order << std::endl;
188  out << " Observed order: " << slope << std::endl;
189  out << " =========================" << std::endl;
190  TEST_FLOATING_EQUALITY(slope, 1.95006, 0.015);
191  TEST_FLOATING_EQUALITY(ErrorNorm[0], 0.0137394, 1.0e-4);
192 
193  if (comm->getRank() == 0) {
194  std::ofstream ftmp("Tempus_BDF2_SinCos_AdjSens-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;
199  }
200  ftmp.close();
201  }
202 }
203 
204 } // namespace Tempus_Test
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)
#define TEST_ASSERT(v1)
T * get() const
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
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.