Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_EDIRK_TrapezoidalRule.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 "Thyra_VectorStdOps.hpp"
15 
17 
18 #include "../TestModels/SinCosModel.hpp"
19 #include "../TestModels/DahlquistTestModel.hpp"
20 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
21 
23 #include <fstream>
24 #include <vector>
25 
26 namespace Tempus_Unit_Test {
27 
28 using Teuchos::RCP;
29 using Teuchos::rcp;
30 using Teuchos::rcp_const_cast;
31 using Teuchos::rcp_dynamic_cast;
33 using Teuchos::sublist;
34 using Teuchos::getParametersFromXmlFile;
35 
36 
37 // ************************************************************
38 // ************************************************************
39 TEUCHOS_UNIT_TEST(EDIRK_TrapezoidalRule, Default_Construction)
40 {
43 
44  // Test stepper properties.
45  TEUCHOS_ASSERT(stepper->getOrder() == 2);
46 }
47 
48 
49 // ************************************************************
50 // ************************************************************
51 TEUCHOS_UNIT_TEST(EDIRK_TrapezoidalRule, StepperFactory_Construction)
52 {
53  auto model = rcp(new Tempus_Test::SinCosModel<double>());
54  testFactoryConstruction("RK Trapezoidal Rule", model);
55 }
56 
57 
58 // ************************************************************
59 // ************************************************************
60 TEUCHOS_UNIT_TEST(EDIRK_TrapezoidalRule, AppAction)
61 {
63  auto model = rcp(new Tempus_Test::SinCosModel<double>());
64  testRKAppAction(stepper, model, out, success);
65 }
66 
67 
68 // ************************************************************
69 // ************************************************************
70 
71 class StepperRKModifierEDIRK_TrapezoidaTest
72  : virtual public Tempus::StepperRKModifierBase<double>
73 {
74 public:
75 
77  StepperRKModifierEDIRK_TrapezoidaTest(Teuchos::FancyOStream &Out, bool &Success)
78  : out(Out), success(Success)
79  {}
80 
81  // FSAL
83  virtual ~StepperRKModifierEDIRK_TrapezoidaTest(){}
84 
86  virtual void modify(
90  {
91  const double relTol = 1.0e-14;
92  auto stageNumber = stepper->getStageNumber();
93  Teuchos::SerialDenseVector<int,double> c = stepper->getTableau()->c();
94 
95  auto currentState = sh->getCurrentState();
96  auto workingState = sh->getWorkingState();
97  const double dt = workingState->getTimeStep();
98  double time = currentState->getTime();
99  if (stageNumber >= 0) time += c(stageNumber)*dt;
100 
101  auto x = workingState->getX();
102  auto xDot = workingState->getXDot();
103  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
104 
105  switch (actLoc) {
107  {
108  {
109  auto DME = Teuchos::rcp_dynamic_cast<
110  const Tempus_Test::DahlquistTestModel<double> > (stepper->getModel());
111  TEST_FLOATING_EQUALITY(DME->getLambda(), -1.0, relTol);
112  }
113  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
114 
115  const double x_0 = get_ele(*(x), 0);
116  const double xDot_0 = get_ele(*(xDot), 0);
117  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol); // Should be x_0
118  TEST_FLOATING_EQUALITY(xDot_0, -1.0, relTol); // Should be xDot_0
119  TEST_ASSERT(std::abs(time) < relTol);
120  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
121  TEST_COMPARE(stageNumber, ==, -1);
122  break;
123  }
126  {
127  const double X_i = get_ele(*(x), 0);
128  const double f_i = get_ele(*(xDot), 0);
129 
130  if (stageNumber == 0) {
131  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
132  TEST_ASSERT(std::abs(f_i) < relTol);
133  TEST_ASSERT(std::abs(time) < relTol);
134  } else if (stageNumber == 1) {
135  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
136  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
137  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
138  } else {
139  TEUCHOS_TEST_FOR_EXCEPT( !(-1 < stageNumber && stageNumber < 2));
140  }
141 
142  break;
143  }
147  {
148  const double X_i = get_ele(*(x), 0);
149  const double f_i = get_ele(*(xDot), 0);
150 
151  if (stageNumber == 0) {
152  // X_i = 1, f_1 = 0
153  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
154  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
155  TEST_ASSERT(std::abs(time) < relTol);
156  } else if (stageNumber == 1) {
157  // X_i = , f_i =
158  TEST_FLOATING_EQUALITY(X_i, 1.0/3.0, relTol);
159  TEST_FLOATING_EQUALITY(f_i, -1.0/3.0, relTol);
160  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
161  } else {
162  TEUCHOS_TEST_FOR_EXCEPT( !(-1 < stageNumber && stageNumber < 2));
163  }
164 
165  break;
166  }
168  {
169  const double x_1 = get_ele(*(x), 0);
170  time = workingState->getTime();
171  TEST_FLOATING_EQUALITY(x_1, 1.0/3.0, relTol); // Should be x_1
172  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
173  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
174  TEST_COMPARE(stageNumber, ==, -1);
175 
176  if (stepper->getUseEmbedded() == true) {
177  TEST_FLOATING_EQUALITY(workingState->getTolRel(), 1.0, relTol);
178  TEST_ASSERT(std::abs(workingState->getTolAbs()) < relTol);
179  // e = 0 from doxygen above.
180  TEST_ASSERT(std::abs(workingState->getErrorRel()) < relTol);
181  }
182 
183  }
184 
185  }
186 
187 
188  }
189 
190 private:
191 
192  Teuchos::FancyOStream & out;
193  bool & success;
194 };
195 
196 
197 // ************************************************************
198 // ************************************************************
199 TEUCHOS_UNIT_TEST(DIRK, EDIRK_Trapezoida_Modifier)
200 {
204 
205  auto modifier = rcp(new StepperRKModifierEDIRK_TrapezoidaTest(out, success));
206 
207  stepper->setModel(model);
208  stepper->setAppAction(modifier);
209  stepper->setICConsistency("Consistent");
210  stepper->setUseFSAL(false);
211  stepper->initialize();
212 
213  // Create a SolutionHistory.
214  auto solutionHistory = Tempus::createSolutionHistoryME(model);
215 
216  // Take one time step.
217  stepper->setInitialConditions(solutionHistory);
218  solutionHistory->initWorkingState();
219  double dt = 1.0;
220  solutionHistory->getWorkingState()->setTimeStep(dt);
221  solutionHistory->getWorkingState()->setTime(dt);
222  stepper->takeStep(solutionHistory); // Primary testing occurs in
223  // modifierX during takeStep().
224  // Test stepper properties.
225  TEUCHOS_ASSERT(stepper->getOrder() == 2);
226 }
227 } // namespace Tempus_Test
void testRKAppAction(const Teuchos::RCP< Tempus::StepperRKBase< double > > &stepper, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model, Teuchos::FancyOStream &out, bool &success)
Unit test utility for Stepper RK AppAction.
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
The classic Dahlquist Test Problem.
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
RK Trapezoidal Rule (A.K.A. RK Crank-Nicolson)
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Nonmember contructor from a Thyra ModelEvaluator.
Base class for Runge-Kutta methods, ExplicitRK, DIRK and IMEX.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ACTION_LOCATION
Indicates the location of application action (see algorithm).
void testDIRKAccessorsFullConstruction(const RCP< Tempus::StepperDIRK< double > > &stepper)
Unit test utility for ExplicitRK Stepper construction and accessors.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)