Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_Test_NewmarkExplicitAForm_BallParabolic.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 
13 #include "Tempus_config.hpp"
14 #include "Tempus_IntegratorBasic.hpp"
15 
16 #include "Tempus_StepperFactory.hpp"
17 #include "Tempus_StepperNewmarkImplicitAForm.hpp"
18 #include "Tempus_StepperNewmarkImplicitDForm.hpp"
19 
20 #include "../TestModels/HarmonicOscillatorModel.hpp"
21 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
22 
23 #include <fstream>
24 #include <limits>
25 #include <sstream>
26 #include <vector>
27 
28 namespace Tempus_Test {
29 
30 using Teuchos::getParametersFromXmlFile;
32 using Teuchos::RCP;
33 using Teuchos::rcp_const_cast;
34 using Teuchos::rcp_dynamic_cast;
35 using Teuchos::sublist;
36 
40 
41 // ************************************************************
42 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, BallParabolic)
43 {
44  // Tolerance to check if test passed
45  double tolerance = 1.0e-14;
46  std::vector<std::string> options;
47  options.push_back("useFSAL=true");
48  options.push_back("useFSAL=false");
49  options.push_back("ICConsistency and Check");
50 
51  for (const auto& option : options) {
52  // Read params from .xml file
53  RCP<ParameterList> pList = getParametersFromXmlFile(
54  "Tempus_Test_NewmarkExplicitAForm_BallParabolic.xml");
55 
56  // Setup the HarmonicOscillatorModel
57  RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
60 
61  // Setup the Integrator and reset initial time step
62  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
63  RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
64  stepperPL->remove("Zero Initial Guess");
65  if (option == "useFSAL=true")
66  stepperPL->set("Use FSAL", true);
67  else if (option == "useFSAL=false")
68  stepperPL->set("Use FSAL", false);
69  else if (option == "ICConsistency and Check") {
70  stepperPL->set("Initial Condition Consistency", "Consistent");
71  stepperPL->set("Initial Condition Consistency Check", true);
72  }
73 
75  Tempus::createIntegratorBasic<double>(pl, model);
76 
77  // Integrate to timeMax
78  bool integratorStatus = integrator->advanceTime();
79  TEST_ASSERT(integratorStatus)
80 
81  // Test if at 'Final Time'
82  double time = integrator->getTime();
83  double timeFinal = pl->sublist("Default Integrator")
84  .sublist("Time Step Control")
85  .get<double>("Final Time");
86  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
87 
88  // Time-integrated solution and the exact solution
89  RCP<Thyra::VectorBase<double> > x = integrator->getX();
91  model->getExactSolution(time).get_x();
92 
93  // Plot sample solution and exact solution
94  std::ofstream ftmp("Tempus_Test_NewmarkExplicitAForm_BallParabolic.dat");
95  ftmp.precision(16);
96  RCP<const SolutionHistory<double> > solutionHistory =
97  integrator->getSolutionHistory();
98  bool passed = true;
99  double err = 0.0;
101  for (int i = 0; i < solutionHistory->getNumStates(); i++) {
102  RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
103  double time_i = solutionState->getTime();
104  RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
105  x_exact_plot = model->getExactSolution(time_i).get_x();
106  ftmp << time_i << " " << get_ele(*(x_plot), 0) << " "
107  << get_ele(*(x_exact_plot), 0) << std::endl;
108  if (abs(get_ele(*(x_plot), 0) - get_ele(*(x_exact_plot), 0)) > err)
109  err = abs(get_ele(*(x_plot), 0) - get_ele(*(x_exact_plot), 0));
110  }
111  ftmp.close();
112  out << "Max error = " << err << "\n \n";
113  if (err > tolerance) passed = false;
114 
116  !passed, std::logic_error,
117  "\n Test failed! Max error = " << err << " > tolerance = " << tolerance
118  << "\n!");
119 
120  // Check the order and intercept
121  RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
122  out << " Stepper = " << stepper->description() << "\n with "
123  << option << std::endl;
124  out << " =========================" << std::endl;
125  out << " Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
126  out << " Computed solution: " << get_ele(*(x), 0) << std::endl;
127  out << " =========================" << std::endl;
128  TEST_ASSERT(std::abs(get_ele(*(x), 0)) < 1.0e-14);
129  }
130 }
131 
132 } // namespace Tempus_Test
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
T * get() const
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
bool remove(std::string const &name, bool throwIfNotExists=true)
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Solution state for integrators and steppers.