Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_Test_BDF2_CDR.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#include "Teuchos_DefaultComm.hpp"
14
15#include "Tempus_config.hpp"
16#include "Tempus_IntegratorBasic.hpp"
17#include "Tempus_StepperBDF2.hpp"
18
19#ifdef TEMPUS_ENABLE_TPETRA_STACK
20#include "../TestModels/CDR_Model_Tpetra.hpp"
21#include "Tpetra_Core.hpp"
22#endif
23#include "../TestModels/VanDerPolModel.hpp"
24#include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
25
26#include "Stratimikos_DefaultLinearSolverBuilder.hpp"
27
28#include <fstream>
29#include <limits>
30#include <sstream>
31#include <vector>
32
33namespace Tempus_Test {
34
35using Teuchos::getParametersFromXmlFile;
36using Teuchos::ParameterList;
37using Teuchos::RCP;
38using Teuchos::rcp;
39using Teuchos::rcp_const_cast;
40using Teuchos::sublist;
41
45
46// ************************************************************
47// ************************************************************
48template <typename SC, typename Model, typename Comm>
49void CDR_Test(const Comm& comm, const int commSize, Teuchos::FancyOStream& out,
50 bool& success)
51{
52 RCP<Tempus::IntegratorBasic<double>> integrator;
53 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
54 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
55 std::vector<double> StepSize;
56
57 // Read params from .xml file
58 auto pList = getParametersFromXmlFile("Tempus_BDF2_CDR.xml");
59 // Set initial time step = 2*dt specified in input file (for convergence
60 // study)
61 //
62 auto pl = sublist(pList, "Tempus", true);
63 auto dt = pl->sublist("Demo Integrator")
64 .sublist("Time Step Control")
65 .get<double>("Initial Time Step");
66 dt *= 2.0;
67 auto model_pl = sublist(pList, "CDR Model", true);
68
69 const auto nTimeStepSizes =
70 model_pl->get<int>("Number of Time Step Sizes", 5);
71
72 for (int n = 0; n < nTimeStepSizes; n++) {
73 // Create CDR Model
74 const int num_elements = model_pl->get<int>("num elements");
75 const auto left_end = model_pl->get<SC>("left end");
76 const auto right_end = model_pl->get<SC>("right end");
77 const auto a_convection = model_pl->get<SC>("a (convection)");
78 const auto k_source = model_pl->get<SC>("k (source)");
79
80 auto model = rcp(new Model(comm, num_elements, left_end, right_end,
81 a_convection, k_source));
82
83 // Set the factory
84 ::Stratimikos::DefaultLinearSolverBuilder builder;
85
86 auto p = rcp(new ParameterList);
87 p->set("Linear Solver Type", "Belos");
88 p->set("Preconditioner Type", "None");
89 builder.setParameterList(p);
90
91 auto lowsFactory = builder.createLinearSolveStrategy("");
92
93 model->set_W_factory(lowsFactory);
94
95 // Set the step size
96 dt /= 2;
97
98 // Setup the Integrator and reset initial time step
99 pl->sublist("Demo Integrator")
100 .sublist("Time Step Control")
101 .set("Initial Time Step", dt);
102 integrator = Tempus::createIntegratorBasic<double>(pl, model);
103
104 // Integrate to timeMax
105 bool integratorStatus = integrator->advanceTime();
106 TEST_ASSERT(integratorStatus)
107
108 // Test if at 'Final Time'
109 double time = integrator->getTime();
110 double timeFinal = pl->sublist("Demo Integrator")
111 .sublist("Time Step Control")
112 .get<double>("Final Time");
113 double tol = 100.0 * std::numeric_limits<double>::epsilon();
114 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
115
116 // Store off the final solution and step size
117 StepSize.push_back(dt);
118 auto solution = Thyra::createMember(model->get_x_space());
119 Thyra::copy(*(integrator->getX()), solution.ptr());
120 solutions.push_back(solution);
121 auto solutionDot = Thyra::createMember(model->get_x_space());
122 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
123 solutionsDot.push_back(solutionDot);
124
125 // Output finest temporal solution for plotting
126 // This only works for ONE MPI process
127 if ((n == nTimeStepSizes - 1) && (commSize == 1)) {
128 std::ofstream ftmp("Tempus_BDF2_CDR.dat");
129 ftmp << "TITLE=\"BDF2 Solution to CDR\"\n"
130 << "VARIABLES=\"z\",\"T\"\n";
131 const auto dx =
132 std::fabs(left_end - right_end) / static_cast<double>(num_elements);
133 auto solutionHistory = integrator->getSolutionHistory();
134 int nStates = solutionHistory->getNumStates();
135 for (int i = 0; i < nStates; i++) {
136 auto solutionState = (*solutionHistory)[i];
137 auto x = solutionState->getX();
138 auto ttime = solutionState->getTime();
139 ftmp << "ZONE T=\"Time=" << ttime << "\", I=" << num_elements + 1
140 << ", F=BLOCK\n";
141 for (int j = 0; j < num_elements + 1; j++) {
142 const auto x_coord = left_end + static_cast<double>(j) * dx;
143 ftmp << x_coord << " ";
144 }
145 ftmp << std::endl;
146 for (int j = 0; j < num_elements + 1; j++)
147 ftmp << get_ele(*x, j) << " ";
148 ftmp << std::endl;
149 }
150 ftmp.close();
151 }
152 }
153
154 // Check the order and intercept
155 if (nTimeStepSizes > 2) {
156 double xSlope = 0.0;
157 double xDotSlope = 0.0;
158 std::vector<double> xErrorNorm;
159 std::vector<double> xDotErrorNorm;
160 auto stepper = integrator->getStepper();
161 auto order = stepper->getOrder();
162 writeOrderError("Tempus_BDF2_CDR-Error.dat", stepper, StepSize, solutions,
163 xErrorNorm, xSlope, solutionsDot, xDotErrorNorm, xDotSlope,
164 out);
165 TEST_FLOATING_EQUALITY(xSlope, order, 0.35);
166 TEST_COMPARE(xSlope, >, 0.95);
167 TEST_FLOATING_EQUALITY(xDotSlope, order, 0.35);
168 TEST_COMPARE(xDotSlope, >, 0.95);
169
170 TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.0145747, 1.0e-4);
171 TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.0563621, 1.0e-4);
172 }
173
174 // Write fine mesh solution at final time
175 // This only works for ONE MPI process
176 if (commSize == 1) {
177 auto pListCDR = getParametersFromXmlFile("Tempus_BDF2_CDR.xml");
178 auto model_pl_CDR = sublist(pListCDR, "CDR Model", true);
179 const auto num_elements = model_pl_CDR->get<int>("num elements");
180 const auto left_end = model_pl_CDR->get<double>("left end");
181 const auto right_end = model_pl_CDR->get<double>("right end");
182
183 const auto& x = *(solutions[solutions.size() - 1]);
184
185 std::ofstream ftmp("Tempus_BDF2_CDR-Solution.dat");
186 for (int n = 0; n < num_elements + 1; n++) {
187 const auto dx =
188 std::fabs(left_end - right_end) / static_cast<double>(num_elements);
189 const auto x_coord = left_end + static_cast<double>(n) * dx;
190 ftmp << x_coord << " " << Thyra::get_ele(x, n) << std::endl;
191 }
192 ftmp.close();
193 }
194
195 Teuchos::TimeMonitor::summarize();
196}
197
198#ifdef TEMPUS_ENABLE_TPETRA_STACK
199// ************************************************************
200// ************************************************************
201TEUCHOS_UNIT_TEST(BDF2, CDR_Tpetra)
202{
203 // Get default Tpetra template types
204 using SC = Tpetra::Vector<>::scalar_type;
205 using LO = Tpetra::Vector<>::local_ordinal_type;
206 using GO = Tpetra::Vector<>::global_ordinal_type;
207 using Node = Tpetra::Vector<>::node_type;
208
209 auto comm = Tpetra::getDefaultComm();
210
211 CDR_Test<SC, Tempus_Test::CDR_Model_Tpetra<SC, LO, GO, Node>>(
212 comm, comm->getSize(), out, success);
213}
214#endif
215
216} // namespace Tempus_Test
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Solution state for integrators and steppers.
void writeOrderError(const std::string filename, Teuchos::RCP< Tempus::Stepper< Scalar > > stepper, std::vector< Scalar > &StepSize, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar > > > &solutions, std::vector< Scalar > &xErrorNorm, Scalar &xSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar > > > &solutionsDot, std::vector< Scalar > &xDotErrorNorm, Scalar &xDotSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar > > > &solutionsDotDot, std::vector< Scalar > &xDotDotErrorNorm, Scalar &xDotDotSlope, Teuchos::FancyOStream &out)
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
void CDR_Test(const Comm &comm, const int commSize, Teuchos::FancyOStream &out, bool &success)