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: 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 
11 
13 
14 #include "../TestModels/DahlquistTestModel.hpp"
15 
16 namespace Tempus_Unit_Test {
17 
19 using Teuchos::RCP;
20 using Teuchos::rcp;
21 using Teuchos::rcp_const_cast;
22 using Teuchos::rcp_dynamic_cast;
23 using Teuchos::sublist;
24 
25 // ************************************************************
26 // ************************************************************
27 TEUCHOS_UNIT_TEST(EDIRK_TrapezoidalRule, Default_Construction)
28 {
31 
32  // Test stepper properties.
33  TEUCHOS_ASSERT(stepper->getOrder() == 2);
34 }
35 
36 // ************************************************************
37 // ************************************************************
38 TEUCHOS_UNIT_TEST(EDIRK_TrapezoidalRule, StepperFactory_Construction)
39 {
40  auto model = rcp(new Tempus_Test::SinCosModel<double>());
41  testFactoryConstruction("RK Trapezoidal Rule", model);
42 }
43 
44 // ************************************************************
45 // ************************************************************
46 TEUCHOS_UNIT_TEST(EDIRK_TrapezoidalRule, AppAction)
47 {
49  auto model = rcp(new Tempus_Test::SinCosModel<double>());
50  testRKAppAction(stepper, model, out, success);
51 }
52 
53 // ************************************************************
54 // ************************************************************
55 
56 class StepperRKModifierEDIRK_TrapezoidaTest
57  : virtual public Tempus::StepperRKModifierBase<double> {
58  public:
60  StepperRKModifierEDIRK_TrapezoidaTest(Teuchos::FancyOStream &Out,
61  bool &Success)
62  : out(Out), success(Success)
63  {
64  }
65 
66  // FSAL
68  virtual ~StepperRKModifierEDIRK_TrapezoidaTest() {}
69 
71  virtual void modify(
75  {
76  const double relTol = 1.0e-14;
77  auto stageNumber = stepper->getStageNumber();
78  Teuchos::SerialDenseVector<int, double> c = stepper->getTableau()->c();
79 
80  auto currentState = sh->getCurrentState();
81  auto workingState = sh->getWorkingState();
82  const double dt = workingState->getTimeStep();
83  double time = currentState->getTime();
84  if (stageNumber >= 0) time += c(stageNumber) * dt;
85 
86  auto x = workingState->getX();
87  auto xDot = workingState->getXDot();
88  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
89 
90  switch (actLoc) {
92  {
93  auto DME = Teuchos::rcp_dynamic_cast<
95  stepper->getModel());
96  TEST_FLOATING_EQUALITY(DME->getLambda(), -1.0, relTol);
97  }
98  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
99 
100  const double x_0 = get_ele(*(x), 0);
101  const double xDot_0 = get_ele(*(xDot), 0);
102  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol); // Should be x_0
103  TEST_FLOATING_EQUALITY(xDot_0, -1.0, relTol); // Should be xDot_0
104  TEST_ASSERT(std::abs(time) < relTol);
105  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
106  TEST_COMPARE(stageNumber, ==, -1);
107  break;
108  }
111  const double X_i = get_ele(*(x), 0);
112  const double f_i = get_ele(*(xDot), 0);
113 
114  if (stageNumber == 0) {
115  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
116  TEST_ASSERT(std::abs(f_i) < relTol);
117  TEST_ASSERT(std::abs(time) < relTol);
118  }
119  else if (stageNumber == 1) {
120  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
121  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
122  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
123  }
124  else {
125  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 2));
126  }
127 
128  break;
129  }
133  const double X_i = get_ele(*(x), 0);
134  const double f_i = get_ele(*(xDot), 0);
135 
136  if (stageNumber == 0) {
137  // X_i = 1, f_1 = 0
138  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
139  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
140  TEST_ASSERT(std::abs(time) < relTol);
141  }
142  else if (stageNumber == 1) {
143  // X_i = , f_i =
144  TEST_FLOATING_EQUALITY(X_i, 1.0 / 3.0, relTol);
145  TEST_FLOATING_EQUALITY(f_i, -1.0 / 3.0, relTol);
146  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
147  }
148  else {
149  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 2));
150  }
151 
152  break;
153  }
155  const double x_1 = get_ele(*(x), 0);
156  time = workingState->getTime();
157  TEST_FLOATING_EQUALITY(x_1, 1.0 / 3.0, relTol); // Should be x_1
158  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
159  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
160  TEST_COMPARE(stageNumber, ==, -1);
161 
162  if (stepper->getUseEmbedded() == true) {
163  TEST_FLOATING_EQUALITY(workingState->getTolRel(), 1.0, relTol);
164  TEST_ASSERT(std::abs(workingState->getTolAbs()) < relTol);
165  // e = 0 from doxygen above.
166  TEST_ASSERT(std::abs(workingState->getErrorRel()) < relTol);
167  }
168  }
169  }
170  }
171 
172  private:
174  bool &success;
175 };
176 
177 // ************************************************************
178 // ************************************************************
179 TEUCHOS_UNIT_TEST(DIRK, EDIRK_Trapezoida_Modifier)
180 {
184 
185  auto modifier = rcp(new StepperRKModifierEDIRK_TrapezoidaTest(out, success));
186 
187  stepper->setModel(model);
188  stepper->setAppAction(modifier);
189  stepper->setICConsistency("Consistent");
190  stepper->setUseFSAL(false);
191  stepper->initialize();
192 
193  // Create a SolutionHistory.
194  auto solutionHistory = Tempus::createSolutionHistoryME(model);
195 
196  // Take one time step.
197  stepper->setInitialConditions(solutionHistory);
198  solutionHistory->initWorkingState();
199  double dt = 1.0;
200  solutionHistory->getWorkingState()->setTimeStep(dt);
201  solutionHistory->getWorkingState()->setTime(dt);
202  stepper->takeStep(solutionHistory); // Primary testing occurs in
203  // modifierX during takeStep().
204  // Test stepper properties.
205  TEUCHOS_ASSERT(stepper->getOrder() == 2);
206 }
207 } // namespace Tempus_Unit_Test
void testDIRKAccessorsFullConstruction(const RCP< Tempus::StepperDIRK< double >> &stepper)
Unit test utility for ExplicitRK Stepper construction and accessors.
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.
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.
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).
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)