Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_BackwardEuler_PseudoTransient_SA.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
14#include "Tempus_config.hpp"
15#include "Tempus_IntegratorPseudoTransientForwardSensitivity.hpp"
16#include "Tempus_IntegratorPseudoTransientAdjointSensitivity.hpp"
17
18#include "Thyra_VectorStdOps.hpp"
19#include "Thyra_MultiVectorStdOps.hpp"
20
21#include "../TestModels/SteadyQuadraticModel.hpp"
22
23#include "Thyra_DefaultMultiVectorProductVector.hpp"
24#include "Thyra_DefaultProductVector.hpp"
25
26#include <vector>
27#include <fstream>
28
29namespace Tempus_Test {
30
31using Teuchos::getParametersFromXmlFile;
32using Teuchos::ParameterList;
33using Teuchos::RCP;
34using Teuchos::sublist;
35
36// NOX row sum scaling for the stepper of the SteadyQuadratic parameters
37void setRowSumScaling(ParameterList &pl)
38{
39 pl.sublist("Default Stepper")
40 .sublist("Default Solver")
41 .sublist("NOX")
42 .sublist("Thyra Group Options")
43 .set("Function Scaling", "Row Sum");
44}
45
46// ************************************************************
47// ************************************************************
48void test_pseudotransient_fsa(const bool use_dfdp_as_tangent,
49 Teuchos::FancyOStream &out, bool &success,
50 const bool use_row_sum_scaling = false)
51{
52 // Read params from .xml file
53 RCP<ParameterList> pList =
54 getParametersFromXmlFile("Tempus_BackwardEuler_SteadyQuadratic.xml");
55
56 // Setup the SteadyQuadraticModel
57 RCP<ParameterList> scm_pl = sublist(pList, "SteadyQuadraticModel", true);
58 scm_pl->set("Use DfDp as Tangent", use_dfdp_as_tangent);
59 RCP<SteadyQuadraticModel<double> > model =
60 Teuchos::rcp(new SteadyQuadraticModel<double>(scm_pl));
61
62 // Setup sensitivities
63 RCP<ParameterList> pl = sublist(pList, "Tempus", true);
64 ParameterList &sens_pl = pl->sublist("Sensitivities");
65 sens_pl.set("Use DfDp as Tangent", use_dfdp_as_tangent);
66 // With row sum scaling, the sensitivity integrator has its own solver
67 sens_pl.set("Reuse State Linear Solver", !use_row_sum_scaling);
68 sens_pl.set("Force W Update", true); // Have to do this because for this
69 // model the solver seems to be overwriting the matrix
70 if (use_row_sum_scaling) setRowSumScaling(*pl);
71
72 // Setup the Integrator
73 RCP<Tempus::IntegratorPseudoTransientForwardSensitivity<double> > integrator =
75 model);
76
77 // Integrate to timeMax
78 bool integratorStatus = integrator->advanceTime();
79 TEST_ASSERT(integratorStatus);
80
81 // Test if at 'Final Time'
82 double time = integrator->getTime();
83 double timeFinal = pl->sublist("Default Integrator")
84 .sublist("Time Step Control")
85 .get<double>("Final Time");
86 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
87
88 // Time-integrated solution and the exact solution
89 RCP<const Thyra::VectorBase<double> > x_vec = integrator->getX();
90 RCP<const Thyra::MultiVectorBase<double> > DxDp_vec = integrator->getDxDp();
91 const double x = Thyra::get_ele(*x_vec, 0);
92 const double dxdb = Thyra::get_ele(*(DxDp_vec->col(0)), 0);
93 const double x_exact = model->getSteadyStateSolution();
94 const double dxdb_exact = model->getSteadyStateSolutionSensitivity();
95
96 TEST_FLOATING_EQUALITY(x, x_exact, 1.0e-6);
97 TEST_FLOATING_EQUALITY(dxdb, dxdb_exact, 1.0e-6);
98}
99
100TEUCHOS_UNIT_TEST(BackwardEuler, SteadyQuadratic_PseudoTransient_FSA)
101{
102 test_pseudotransient_fsa(false, out, success);
103}
104
105TEUCHOS_UNIT_TEST(BackwardEuler, SteadyQuadratic_PseudoTransient_FSA_Tangent)
106{
107 test_pseudotransient_fsa(true, out, success);
108}
109
110// NOX row sum scaling: the state and sensitivity integrators are created from
111// the same parameters, in which the NOX solver of the state stepper stores its
112// row sum scaling objects (in the state residual space). The sensitivity
113// integrator must not use them.
114TEUCHOS_UNIT_TEST(BackwardEuler, SteadyQuadratic_PseudoTransient_FSA_RowSumScaling)
115{
116 test_pseudotransient_fsa(false, out, success, true);
117}
118
119// ************************************************************
120// ************************************************************
121void test_pseudotransient_asa(Teuchos::FancyOStream &out, bool &success,
122 const bool use_row_sum_scaling = false)
123{
124 // Read params from .xml file
125 RCP<ParameterList> pList =
126 getParametersFromXmlFile("Tempus_BackwardEuler_SteadyQuadratic.xml");
127
128 // Setup the SteadyQuadraticModel
129 RCP<ParameterList> scm_pl = sublist(pList, "SteadyQuadraticModel", true);
130 RCP<SteadyQuadraticModel<double> > model =
131 Teuchos::rcp(new SteadyQuadraticModel<double>(scm_pl));
132
133 // Setup sensitivities
134 RCP<ParameterList> pl = sublist(pList, "Tempus", true);
135 // ParameterList& sens_pl = pl->sublist("Sensitivities");
136 if (use_row_sum_scaling) setRowSumScaling(*pl);
137
138 // Setup the Integrator. The implicit adjoint operator does not provide the
139 // row sums needed by row sum scaling, so use an explicit adjoint model: the
140 // model is scalar, hence it is its own adjoint.
141 RCP<Tempus::IntegratorPseudoTransientAdjointSensitivity<double> > integrator =
142 use_row_sum_scaling
144 pl, model, model)
146 model);
147
148 // Integrate to timeMax
149 bool integratorStatus = integrator->advanceTime();
150 TEST_ASSERT(integratorStatus);
151
152 // Test if at 'Final Time'
153 double time = integrator->getTime();
154 double timeFinal = pl->sublist("Default Integrator")
155 .sublist("Time Step Control")
156 .get<double>("Final Time");
157 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
158
159 // Time-integrated solution and the exact solution using the fact that g = x
160 RCP<const Thyra::VectorBase<double> > x_vec = integrator->getX();
161 RCP<const Thyra::MultiVectorBase<double> > DxDp_vec = integrator->getDgDp();
162 const double x = Thyra::get_ele(*x_vec, 0);
163 const double dxdb = Thyra::get_ele(*(DxDp_vec->col(0)), 0);
164 const double x_exact = model->getSteadyStateSolution();
165 const double dxdb_exact = model->getSteadyStateSolutionSensitivity();
166
167 TEST_FLOATING_EQUALITY(x, x_exact, 1.0e-6);
168 TEST_FLOATING_EQUALITY(dxdb, dxdb_exact, 1.0e-6);
169}
170
171TEUCHOS_UNIT_TEST(BackwardEuler, SteadyQuadratic_PseudoTransient_ASA)
172{
173 test_pseudotransient_asa(out, success);
174}
175
176// NOX row sum scaling: the state and adjoint integrators are created from the
177// same parameters, in which the NOX solver of the state stepper stores its row
178// sum scaling objects (in the state residual space). The adjoint integrator
179// must not use them.
180TEUCHOS_UNIT_TEST(BackwardEuler, SteadyQuadratic_PseudoTransient_ASA_RowSumScaling)
181{
182 test_pseudotransient_asa(out, success, true);
183}
184
185} // namespace Tempus_Test
Simple quadratic equation with a stable steady-state.
void test_pseudotransient_fsa(const bool use_dfdp_as_tangent, Teuchos::FancyOStream &out, bool &success, const bool use_row_sum_scaling=false)
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
void setRowSumScaling(ParameterList &pl)
void test_pseudotransient_asa(Teuchos::FancyOStream &out, bool &success, const bool use_row_sum_scaling=false)
Teuchos::RCP< Tempus::IntegratorPseudoTransientAdjointSensitivity< Scalar > > integratorPseudoTransientAdjointSensitivity()
Nonmember constructor.
Teuchos::RCP< Tempus::IntegratorPseudoTransientForwardSensitivity< Scalar > > createIntegratorPseudoTransientForwardSensitivity()
Nonmember constructor.