Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_BackwardEuler_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 <vector>
27 #include <fstream>
28 #include <sstream>
29 #include <limits>
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(BackwardEuler, 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_BackwardEuler_SinCos_ASA.xml");
57 
58  // Setup the SinCosModel
59  // Here we test using an explicit adjoint model for adjoint sensitivities
60  RCP<ParameterList> scm_pl = sublist(pList, "SinCosModel", true);
61  RCP<SinCosModel<double> > model =
62  Teuchos::rcp(new SinCosModel<double>(scm_pl));
63  RCP<SinCosModelAdjoint<double> > adjoint_model =
65 
66  dt /= 2;
67 
68  // Setup sensitivities
69  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
70  ParameterList& sens_pl = pl->sublist("Sensitivities");
71  sens_pl.set("Mass Matrix Is Identity", false); // Just for testing
72  ParameterList& interp_pl = pl->sublist("Default Integrator")
73  .sublist("Solution History")
74  .sublist("Interpolator");
75  interp_pl.set("Interpolator Type", "Lagrange");
76  interp_pl.set("Order", 0);
77 
78  // Set FSAL to false, because it is not currently setup for ASA.
79  pl->sublist("Default Stepper").set("Use FSAL", false);
80 
81  // Set IC consistency check to false, because it is not currently
82  // setup for ASA.
83  pl->sublist("Default Stepper")
84  .set("Initial Condition Consistency Check", false);
85 
86  // Setup the Integrator and reset initial time step
87  pl->sublist("Default Integrator")
88  .sublist("Time Step Control")
89  .set("Initial Time Step", dt);
91  Tempus::createIntegratorAdjointSensitivity<double>(pl, model,
92  adjoint_model);
93  order = integrator->getStepper()->getOrder();
94 
95  // Initial Conditions
96  double t0 = pl->sublist("Default Integrator")
97  .sublist("Time Step Control")
98  .get<double>("Initial Time");
100  model->getExactSolution(t0).get_x();
101  const int num_param = model->get_p_space(0)->dim();
103  Thyra::createMembers(model->get_x_space(), num_param);
104  for (int i = 0; i < num_param; ++i)
105  Thyra::assign(DxDp0->col(i).ptr(),
106  *(model->getExactSensSolution(i, t0).get_x()));
107  integrator->initializeSolutionHistory(t0, x0, Teuchos::null, Teuchos::null,
108  DxDp0, Teuchos::null, Teuchos::null);
109 
110  // Integrate to timeMax
111  bool integratorStatus = integrator->advanceTime();
112  TEST_ASSERT(integratorStatus)
113 
114  // Test if at 'Final Time'
115  double time = integrator->getTime();
116  double timeFinal = pl->sublist("Default Integrator")
117  .sublist("Time Step Control")
118  .get<double>("Final Time");
119  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
120 
121  // Time-integrated solution and the exact solution along with
122  // sensitivities (relying on response g(x) = x). Note we must transpose
123  // dg/dp since the integrator returns it in gradient form.
124  RCP<const Thyra::VectorBase<double> > x = integrator->getX();
125  RCP<const Thyra::MultiVectorBase<double> > DgDp = integrator->getDgDp();
127  Thyra::createMembers(model->get_x_space(), num_param);
128  {
131  const int num_g = DgDp->domain()->dim();
132  for (int i = 0; i < num_g; ++i)
133  for (int j = 0; j < num_param; ++j) dxdp_view(i, j) = dgdp_view(j, i);
134  }
136  model->getExactSolution(time).get_x();
138  Thyra::createMembers(model->get_x_space(), num_param);
139  for (int i = 0; i < num_param; ++i)
140  Thyra::assign(DxDp_exact->col(i).ptr(),
141  *(model->getExactSensSolution(i, time).get_x()));
142 
143  // Plot sample solution, exact solution, and adjoint solution
144  if (comm->getRank() == 0 && n == nTimeStepSizes - 1) {
147 
148  std::ofstream ftmp("Tempus_BackwardEuler_SinCos_AdjSens.dat");
149  RCP<const SolutionHistory<double> > solutionHistory =
150  integrator->getSolutionHistory();
151  for (int i = 0; i < solutionHistory->getNumStates(); i++) {
152  RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
153  const double time_i = solutionState->getTime();
154  RCP<const DPV> x_prod_plot =
155  Teuchos::rcp_dynamic_cast<const DPV>(solutionState->getX());
157  x_prod_plot->getVectorBlock(0);
158  RCP<const DMVPV> adjoint_prod_plot =
159  Teuchos::rcp_dynamic_cast<const DMVPV>(
160  x_prod_plot->getVectorBlock(1));
162  adjoint_prod_plot->getMultiVector();
163  RCP<const Thyra::VectorBase<double> > x_exact_plot =
164  model->getExactSolution(time_i).get_x();
165  ftmp << std::fixed << std::setprecision(7) << time_i << std::setw(11)
166  << get_ele(*(x_plot), 0) << std::setw(11) << get_ele(*(x_plot), 1)
167  << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 0)
168  << std::setw(11) << get_ele(*(adjoint_plot->col(0)), 1)
169  << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 0)
170  << std::setw(11) << get_ele(*(adjoint_plot->col(1)), 1)
171  << std::setw(11) << get_ele(*(x_exact_plot), 0) << std::setw(11)
172  << get_ele(*(x_exact_plot), 1) << std::endl;
173  }
174  ftmp.close();
175  }
176 
177  // Calculate the error
178  RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
179  RCP<Thyra::MultiVectorBase<double> > DxDpdiff = DxDp->clone_mv();
180  Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
181  Thyra::V_VmV(DxDpdiff.ptr(), *DxDp_exact, *DxDp);
182  StepSize.push_back(dt);
183  double L2norm = Thyra::norm_2(*xdiff);
184  L2norm *= L2norm;
185  Teuchos::Array<double> L2norm_DxDp(num_param);
186  Thyra::norms_2(*DxDpdiff, L2norm_DxDp());
187  for (int i = 0; i < num_param; ++i)
188  L2norm += L2norm_DxDp[i] * L2norm_DxDp[i];
189  L2norm = std::sqrt(L2norm);
190  ErrorNorm.push_back(L2norm);
191 
192  // out << " n = " << n << " dt = " << dt << " error = " << L2norm
193  // << std::endl;
194  }
195 
196  // Check the order and intercept
197  double slope = computeLinearRegressionLogLog<double>(StepSize, ErrorNorm);
198  out << " Stepper = BackwardEuler" << std::endl;
199  out << " =========================" << std::endl;
200  out << " Expected order: " << order << std::endl;
201  out << " Observed order: " << slope << std::endl;
202  out << " =========================" << std::endl;
203  TEST_FLOATING_EQUALITY(slope, order, 0.015);
204  TEST_FLOATING_EQUALITY(ErrorNorm[0], 0.142525, 1.0e-4);
205 
206  if (comm->getRank() == 0) {
207  std::ofstream ftmp("Tempus_BackwardEuler_SinCos_AdjSens-Error.dat");
208  double error0 = 0.8 * ErrorNorm[0];
209  for (int n = 0; n < nTimeStepSizes; n++) {
210  ftmp << StepSize[n] << " " << ErrorNorm[n] << " "
211  << error0 * (StepSize[n] / StepSize[0]) << std::endl;
212  }
213  ftmp.close();
214  }
215 }
216 
217 } // 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.