Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_SDIRK_21Pair.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(SDIRK_21Pair, Default_Construction)
40 {
41  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
43 
44  // Test stepper properties.
45  TEUCHOS_ASSERT(stepper->getOrder() == 2);
46 }
47 
48 
49 // ************************************************************
50 // ************************************************************
51 TEUCHOS_UNIT_TEST(SDIRK_21Pair, StepperFactory_Construction)
52 {
53  auto model = rcp(new Tempus_Test::SinCosModel<double>());
54  testFactoryConstruction("SDIRK 2(1) Pair", model);
55 }
56 
57 
58 // ************************************************************
59 // ************************************************************
60 TEUCHOS_UNIT_TEST(SDIRK_21Pair, AppAction)
61 {
62  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
63  auto model = rcp(new Tempus_Test::SinCosModel<double>());
64  testRKAppAction(stepper, model, out, success);
65 }
66 
67 
68 // ************************************************************
69 // ************************************************************
70 class StepperRKModifierSDIRK21Test
71  : virtual public Tempus::StepperRKModifierBase<double>
72 {
73 public:
74 
76  StepperRKModifierSDIRK21Test(Teuchos::FancyOStream &Out, bool &Success)
77  : out(Out), success(Success)
78  {}
79 
81  virtual ~StepperRKModifierSDIRK21Test(){}
82 
84  virtual void modify(
88  {
89  const double relTol = 1.0e-14;
90  auto stageNumber = stepper->getStageNumber();
91  Teuchos::SerialDenseVector<int,double> c = stepper->getTableau()->c();
92 
93  auto currentState = sh->getCurrentState();
94  auto workingState = sh->getWorkingState();
95  const double dt = workingState->getTimeStep();
96  double time = currentState->getTime();
97  if (stageNumber >= 0) time += c(stageNumber)*dt;
98 
99  auto x = workingState->getX();
100  auto xDot = workingState->getXDot();
101  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
102 
103  switch (actLoc) {
105  {
106  {
107  auto DME = Teuchos::rcp_dynamic_cast<
108  const Tempus_Test::DahlquistTestModel<double> > (stepper->getModel());
109  TEST_FLOATING_EQUALITY(DME->getLambda(), -1.0, relTol);
110  }
111  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
112 
113  const double x_0 = get_ele(*(x), 0);
114  const double xDot_0 = get_ele(*(xDot), 0);
115  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol); // Should be x_0
116  TEST_FLOATING_EQUALITY(xDot_0, -1.0, relTol); // Should be xDot_0
117  TEST_ASSERT(std::abs(time) < relTol);
118  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
119  TEST_COMPARE(stageNumber, ==, -1);
120  break;
121  }
124  {
125  const double X_i = get_ele(*(x), 0);
126  const double f_i = get_ele(*(xDot), 0);
127 
128  if (stageNumber == 0) {
129  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
130  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
131  TEST_ASSERT(std::abs(f_i) < relTol);
132  } else if (stageNumber == 1) {
133  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol);
134  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
135  TEST_ASSERT(std::abs(time) < relTol);
136  } else {
137  TEUCHOS_TEST_FOR_EXCEPT( !(-1 < stageNumber && stageNumber < 2));
138  }
139 
140  break;
141  }
145  {
146  const double X_i = get_ele(*(x), 0); // 0.5
147  const double f_i = get_ele(*(xDot), 0); // -0.5
148 
149  if (stageNumber == 0) {
150  // X_i = 1, f_1 = 0
151  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol);
152  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
153  TEST_FLOATING_EQUALITY(f_i, -0.5, relTol);
154  } else if (stageNumber == 1) {
155  // X_i = , f_i =
156  TEST_FLOATING_EQUALITY(X_i, 0.75, relTol);
157  TEST_FLOATING_EQUALITY(f_i, -0.75, relTol);
158  TEST_ASSERT(std::abs(time) < relTol);
159  } else {
160  TEUCHOS_TEST_FOR_EXCEPT( !(-1 < stageNumber && stageNumber < 2));
161  }
162 
163  break;
164  }
166  {
167  const double x_1 = get_ele(*(x), 0);
168  time = workingState->getTime();
169  TEST_FLOATING_EQUALITY(x_1, 3.0/8.0, relTol); // Should be x_1
170  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
171  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
172  TEST_COMPARE(stageNumber, ==, -1);
173 
174  if (stepper->getUseEmbedded() == true) {
175  TEST_FLOATING_EQUALITY(workingState->getTolRel(), 1.0, relTol);
176  TEST_ASSERT(std::abs(workingState->getTolAbs()) < relTol);
177  // e = 0 from doxygen above.
178  TEST_ASSERT(std::abs(workingState->getErrorRel()) < relTol);
179  }
180 
181  }
182 
183  }
184 
185 
186  }
187 
188 private:
189 
190  Teuchos::FancyOStream & out;
191  bool & success;
192 };
193 
194 
195 // ************************************************************
196 // ************************************************************
197 TEUCHOS_UNIT_TEST(SDIRK_21Pair, Modifier)
198 {
199  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
202 
203  auto modifier = rcp(new StepperRKModifierSDIRK21Test(out, success));
204 
205  stepper->setModel(model);
206  stepper->setAppAction(modifier);
207  stepper->setICConsistency("Consistent");
208  stepper->setUseFSAL(false);
209  stepper->initialize();
210 
211  // Create a SolutionHistory.
212  auto solutionHistory = Tempus::createSolutionHistoryME(model);
213 
214  // Take one time step.
215  stepper->setInitialConditions(solutionHistory);
216  solutionHistory->initWorkingState();
217  double dt = 1.0;
218  solutionHistory->getWorkingState()->setTimeStep(dt);
219  solutionHistory->getWorkingState()->setTime(dt);
220  stepper->takeStep(solutionHistory); // Primary testing occurs in
221  // modifierX during takeStep().
222  // Test stepper properties.
223  TEUCHOS_ASSERT(stepper->getOrder() == 2);
224 }
225 
226 } // 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...
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)