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