Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_ObserverTest.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 
9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 
13 #include "Thyra_VectorStdOps.hpp"
14 
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_IntegratorObserverLogging.hpp"
17 #include "Tempus_IntegratorObserverComposite.hpp"
18 
19 #include "../TestModels/SinCosModel.hpp"
20 #include "../TestModels/VanDerPolModel.hpp"
21 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
22 
23 #include <vector>
24 
25 namespace Tempus_Test {
26 
27 using Teuchos::RCP;
28 using Teuchos::rcp;
29 using Teuchos::ParameterList;
30 using Teuchos::sublist;
31 using Teuchos::getParametersFromXmlFile;
32 
36 
37 
38 // ************************************************************
39 // ************************************************************
40 TEUCHOS_UNIT_TEST(Observer, IntegratorObserverLogging)
41 {
42  // Read params from .xml file
43  RCP<ParameterList> pList =
44  getParametersFromXmlFile("Tempus_Observer_SinCos.xml");
45 
46  // Setup the SinCosModel
47  RCP<ParameterList> scm_pl = sublist(pList, "SinCosModel", true);
48  RCP<SinCosModel<double> > model =
49  Teuchos::rcp(new SinCosModel<double> (scm_pl));
50 
51  // Setup the Integrator and reset initial time step
52  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
53  RCP<Tempus::IntegratorBasic<double> > integrator =
54  Tempus::integratorBasic<double>(pl, model);
55 
56  RCP<Tempus::IntegratorObserverLogging<double> > loggingObs =
58  integrator->setObserver(loggingObs);
59 
60  // Integrate to timeMax
61  bool integratorStatus = integrator->advanceTime();
62  TEST_ASSERT(integratorStatus)
63 
64  // Test if at 'Final Time'
65  double time = integrator->getTime();
66  double timeFinal = pl->sublist("Demo Integrator")
67  .sublist("Time Step Control").get<double>("Final Time");
68  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
69 
70  // Construct the reference counter and order for comparison.
71  std::map<std::string,int> refCounters;
72  std::list<std::string> refOrder;
73 
74  refCounters[loggingObs->nameObserveStartIntegrator_ ] = 1;
75  refCounters[loggingObs->nameObserveStartTimeStep_ ] = 10;
76  refCounters[loggingObs->nameObserveNextTimeStep_ ] = 10;
77  refCounters[loggingObs->nameObserveBeforeTakeStep_ ] = 10;
78  refCounters[loggingObs->nameObserveAfterTakeStep_ ] = 10;
79  refCounters[loggingObs->nameObserveAfterCheckTimeStep_] = 10;
80  refCounters[loggingObs->nameObserveEndTimeStep_ ] = 10;
81  refCounters[loggingObs->nameObserveEndIntegrator_ ] = 1;
82 
83  refOrder.push_back(loggingObs->nameObserveStartIntegrator_ );
84  for (int i=0 ; i<10; ++i) {
85  refOrder.push_back(loggingObs->nameObserveStartTimeStep_ );
86  refOrder.push_back(loggingObs->nameObserveNextTimeStep_ );
87  refOrder.push_back(loggingObs->nameObserveBeforeTakeStep_ );
88  refOrder.push_back(loggingObs->nameObserveAfterTakeStep_ );
89  refOrder.push_back(loggingObs->nameObserveAfterCheckTimeStep_);
90  refOrder.push_back(loggingObs->nameObserveEndTimeStep_ );
91  }
92  refOrder.push_back(loggingObs->nameObserveEndIntegrator_ );
93 
94  const std::map<std::string,int>& counters = *(loggingObs->getCounters());
95  const std::list<std::string>& order = *(loggingObs->getOrder());
96 
97  // Compare against reference.
98  TEST_EQUALITY(
99  counters.find(loggingObs->nameObserveStartIntegrator_ )->second,
100  refCounters.find(loggingObs->nameObserveStartIntegrator_ )->second);
101  TEST_EQUALITY(
102  counters.find(loggingObs->nameObserveStartTimeStep_ )->second,
103  refCounters.find(loggingObs->nameObserveStartTimeStep_ )->second);
104  TEST_EQUALITY(
105  counters.find(loggingObs->nameObserveNextTimeStep_ )->second,
106  refCounters.find(loggingObs->nameObserveNextTimeStep_ )->second);
107  TEST_EQUALITY(
108  counters.find(loggingObs->nameObserveBeforeTakeStep_ )->second,
109  refCounters.find(loggingObs->nameObserveBeforeTakeStep_ )->second);
110  TEST_EQUALITY(
111  counters.find(loggingObs->nameObserveAfterTakeStep_ )->second,
112  refCounters.find(loggingObs->nameObserveAfterTakeStep_ )->second);
113  TEST_EQUALITY(
114  counters.find(loggingObs->nameObserveAfterCheckTimeStep_)->second,
115  refCounters.find(loggingObs->nameObserveAfterCheckTimeStep_)->second);
116  TEST_EQUALITY(
117  counters.find(loggingObs->nameObserveEndTimeStep_ )->second,
118  refCounters.find(loggingObs->nameObserveEndTimeStep_ )->second);
119  TEST_EQUALITY(
120  counters.find(loggingObs->nameObserveEndIntegrator_ )->second,
121  refCounters.find(loggingObs->nameObserveEndIntegrator_ )->second);
122 
123  TEUCHOS_ASSERT(order.size() == refOrder.size());
124  std::list<std::string>::const_iterator orderIter = order.begin();
125  std::list<std::string>::const_iterator refOrderIter = refOrder.begin();
126  for ( ; orderIter != order.end(); ++orderIter,++refOrderIter) {
127  //std::cout << *orderIter << std::endl;
128  TEST_EQUALITY(*orderIter, *refOrderIter);
129  }
130 
131  // Test the reset.
132  loggingObs->resetLogCounters();
133  TEST_EQUALITY(
134  counters.find(loggingObs->nameObserveStartIntegrator_ )->second, 0);
135  TEST_EQUALITY(
136  counters.find(loggingObs->nameObserveStartTimeStep_ )->second, 0);
137  TEST_EQUALITY(
138  counters.find(loggingObs->nameObserveNextTimeStep_ )->second, 0);
139  TEST_EQUALITY(
140  counters.find(loggingObs->nameObserveBeforeTakeStep_ )->second, 0);
141  TEST_EQUALITY(
142  counters.find(loggingObs->nameObserveAfterTakeStep_ )->second, 0);
143  TEST_EQUALITY(
144  counters.find(loggingObs->nameObserveAfterCheckTimeStep_)->second, 0);
145  TEST_EQUALITY(
146  counters.find(loggingObs->nameObserveEndTimeStep_ )->second, 0);
147  TEST_EQUALITY(
148  counters.find(loggingObs->nameObserveEndIntegrator_ )->second, 0);
149  TEST_EQUALITY(order.size(), 0);
150 
151  Teuchos::TimeMonitor::summarize();
152 }
153 
154 TEUCHOS_UNIT_TEST( Observer, IntegratorObserverComposite) {
155 
156  // Read params from .xml file
157  RCP<ParameterList> pList =
158  getParametersFromXmlFile("Tempus_Observer_SinCos.xml");
159 
160  // Setup the SinCosModel
161  RCP<ParameterList> scm_pl = sublist(pList, "SinCosModel", true);
162  RCP<SinCosModel<double> > model =
163  Teuchos::rcp(new SinCosModel<double> (scm_pl));
164 
165  // Setup the Integrator and reset initial time step
166  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
167  RCP<Tempus::IntegratorBasic<double> > integrator =
168  Tempus::integratorBasic<double>(pl, model);
169 
170  RCP<Tempus::IntegratorObserverLogging<double> > loggingObs =
172 
173  // creating another logging observer
174  RCP<Tempus::IntegratorObserverLogging<double> > loggingObs2 =
176 
177  RCP<Tempus::IntegratorObserverComposite<double> > compObs =
179 
180  compObs->addObserver(loggingObs);
181  compObs->addObserver(loggingObs2);
182 
183  compObs->observeStartIntegrator(*integrator);
184  compObs->observeStartTimeStep(*integrator);
185  compObs->observeBeforeTakeStep(*integrator);
186  compObs->observeAfterTakeStep(*integrator);
187  compObs->observeAfterCheckTimeStep(*integrator);
188  compObs->observeEndTimeStep(*integrator);
189 
190 
191  for (int i=0 ; i<10; ++i) {
192  compObs->observeStartTimeStep(*integrator);
193  compObs->observeBeforeTakeStep(*integrator);
194  compObs->observeAfterTakeStep(*integrator);
195  compObs->observeAfterCheckTimeStep(*integrator);
196  compObs->observeEndTimeStep(*integrator);
197  }
198  compObs->observeEndIntegrator(*integrator);
199 
200  const std::map<std::string,int>& counters = *(loggingObs->getCounters());
201 
202  TEST_EQUALITY(
203  counters.find(loggingObs->nameObserveStartIntegrator_ )->second, 1);
204  TEST_EQUALITY(
205  counters.find(loggingObs->nameObserveStartTimeStep_ )->second,11);
206  TEST_EQUALITY(
207  counters.find(loggingObs->nameObserveBeforeTakeStep_ )->second,11);
208  TEST_EQUALITY(
209  counters.find(loggingObs->nameObserveAfterTakeStep_ )->second,11);
210  TEST_EQUALITY(
211  counters.find(loggingObs->nameObserveAfterCheckTimeStep_ )->second,11);
212  TEST_EQUALITY(
213  counters.find(loggingObs->nameObserveEndTimeStep_ )->second,11);
214  TEST_EQUALITY(
215  counters.find(loggingObs->nameObserveEndIntegrator_ )->second, 1);
216  TEST_EQUALITY(
217  counters.find(loggingObs2->nameObserveStartIntegrator_ )->second, 1);
218  TEST_EQUALITY(
219  counters.find(loggingObs2->nameObserveStartTimeStep_ )->second,11);
220  TEST_EQUALITY(
221  counters.find(loggingObs2->nameObserveBeforeTakeStep_ )->second,11);
222  TEST_EQUALITY(
223  counters.find(loggingObs2->nameObserveAfterTakeStep_ )->second,11);
224  TEST_EQUALITY(
225  counters.find(loggingObs2->nameObserveAfterCheckTimeStep_)->second,11);
226  TEST_EQUALITY(
227  counters.find(loggingObs2->nameObserveEndTimeStep_ )->second,11);
228  TEST_EQUALITY(
229  counters.find(loggingObs2->nameObserveEndIntegrator_ )->second, 1);
230 }
231 
232 
233 } // namespace Tempus_Test
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
This observer logs calls to observer functions. This observer simply logs and counts the calls to eac...
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...