Debian Codegraph @

packages/nox/test/epetra/Thyra/ModelEvaluatorRosenbrock.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "NOX_Common.H"
11 #include "ModelEvaluatorRosenbrock.hpp"
12 #include "Teuchos_ScalarTraits.hpp"
13 #include "Teuchos_TimeMonitor.hpp"
14 #include "Epetra_CrsMatrix.h"
15
16 // Thyra support
17 #include "Thyra_DefaultSpmdVectorSpace.hpp"
18 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
19 #include "Thyra_DetachedMultiVectorView.hpp"
20 #include "Thyra_DetachedVectorView.hpp"
21 #include "Thyra_MultiVectorStdOps.hpp"
22 #include "Thyra_VectorStdOps.hpp"
23
24 // Thyra Epetra support
25 #include "Thyra_EpetraThyraWrappers.hpp"
26 #include "Thyra_get_Epetra_Operator.hpp"
27 #include "Thyra_EpetraLinearOp.hpp"
28
29 RosenbrockModelEvaluator::
30 RosenbrockModelEvaluator(const Teuchos::RCP<const Epetra_Comm>& comm):
31 d_(0.0),
32 epetra_comm_(comm)
33 {
34 using Teuchos::rcp;
35
36 const int nx = 2;
37
38 map_x_ = rcp(new Epetra_Map(nx,0,*epetra_comm_));
39 x_space_ = Thyra::create_VectorSpace(map_x_);
40
41 f_epetra_map_ = map_x_;
42 f_space_ = x_space_;
43
44 x0_ = rcp(new Epetra_Vector(*map_x_));
45 (*x0_)[0] = -1.2;
46 (*x0_)[1] = 1.0;
47
48 x_analytic_ = rcp(new Epetra_Vector(*map_x_));
49 (*x_analytic_)[0] = 1.0;
50 (*x_analytic_)[1] = 1.0;
51
52 // Initialize the graph for W CrsMatrix object
53 W_graph_ = rcp(new Epetra_CrsGraph(::Copy,*map_x_,nx));
54 {
55 int indices[nx] = { 0, 1 };
56 for( int i = 0; i < nx; ++i )
57 W_graph_->InsertGlobalIndices(i,nx,indices);
58 }
59 W_graph_->FillComplete();
60
61 }
62
63 // Overridden from Thyra::ModelEvaulator
64
65 Teuchos::RCP<const Thyra::VectorSpaceBase<double> >
66 RosenbrockModelEvaluator::get_x_space() const
67 {
68 return x_space_;
69 }
70
71 Teuchos::RCP<const Thyra::VectorSpaceBase<double> >
72 RosenbrockModelEvaluator::get_f_space() const
73 {
74 return f_space_;
75 }
76
77 Thyra::ModelEvaluatorBase::InArgs<double>
78 RosenbrockModelEvaluator::getNominalValues() const
79 {
80 nominal_values_ = this->createInArgs();
81 nominal_values_.set_x(Thyra::create_Vector(x0_,x_space_));
82 nominal_values_.set_x_dot(Teuchos::null);
83 nominal_values_.set_alpha(0.0);
84 nominal_values_.set_beta(1.0);
85 return nominal_values_;
86 }
87
88 Teuchos::RCP<Thyra::LinearOpBase<double> >
89 RosenbrockModelEvaluator::create_W_op() const
90 {
91 Teuchos::RCP<Epetra_CrsMatrix> W_epetra =
92 Teuchos::rcp(new Epetra_CrsMatrix(::Copy,*W_graph_));
93
94 return Thyra::nonconstEpetraLinearOp(W_epetra);
95 }
96
97 Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<double> >
98 RosenbrockModelEvaluator::get_W_factory() const
99 {
100 return W_factory_;
101 }
102
103 // Misc
104 Teuchos::RCP<const Epetra_Vector>
105 RosenbrockModelEvaluator::get_analytic_solution() const
106 {
107 return x_analytic_;
108 }
109 void
110 RosenbrockModelEvaluator::
111 set_W_factory(const Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<double> >& W_factory)
112 {
113 W_factory_ = W_factory;
114 }
115
116 Thyra::ModelEvaluatorBase::InArgs<double>
117 RosenbrockModelEvaluator::createInArgs() const
118 {
119 Thyra::ModelEvaluatorBase::InArgsSetup<double> inArgs;
120 inArgs.setModelEvalDescription(this->description());
121 inArgs.setSupports(IN_ARG_x,true);
122 inArgs.setSupports(IN_ARG_x_dot,true);
123 inArgs.setSupports(IN_ARG_t,true);
124 inArgs.setSupports(IN_ARG_alpha,true);
125 inArgs.setSupports(IN_ARG_beta,true);
126 return inArgs;
127 }
128
129 Thyra::ModelEvaluatorBase::OutArgs<double>
130 RosenbrockModelEvaluator::createOutArgsImpl() const
131 {
132 Thyra::ModelEvaluatorBase::OutArgsSetup<double> outArgs;
133 outArgs.setModelEvalDescription(this->description());
134 outArgs.setSupports(OUT_ARG_f,true);
135 outArgs.setSupports(OUT_ARG_W_op,true);
136 outArgs.set_W_properties(
137 DerivativeProperties(
138 DERIV_LINEARITY_NONCONST
139 ,DERIV_RANK_FULL
140 ,false // supportsAdjoint
141 )
142 );
143 return outArgs;
144 }
145
146 void
147 RosenbrockModelEvaluator::
148 evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs<double>& inArgs,
149 const Thyra::ModelEvaluatorBase::OutArgs<double>& outArgs) const
150 {
151 using Teuchos::dyn_cast;
152 using Teuchos::rcp_dynamic_cast;
153 //
154 // Get the input arguments
155 //
156
157 TEUCHOS_ASSERT(nonnull(inArgs.get_x()));
158 const Thyra::ConstDetachedVectorView<double> x(inArgs.get_x());
159
160 Teuchos::RCP<const Thyra::VectorBase<double> > x_dot_rcp = inArgs.get_x_dot();
161 double alpha = inArgs.get_alpha();
162 double beta = inArgs.get_beta();
163 //double time = inArgs.get_t();
164
165 //
166 // Get the output arguments
167 //
168 const Teuchos::RCP< Thyra::VectorBase<double> > f_out = outArgs.get_f();
169 const Teuchos::RCP< Thyra::LinearOpBase< double > > W_out = outArgs.get_W_op();
170
171 //
172 // Compute the functions
173 //
174 if(nonnull(f_out)) {
175 if (nonnull(x_dot_rcp)) {
176 NOX_FUNC_TIME_MONITOR("RosenbrockModelEvaluator::eval f_out");
177 Thyra::DetachedVectorView<double> f(f_out);
178 const Thyra::ConstDetachedVectorView<double> x_dot(x_dot_rcp);
179 f[0] = x_dot[0] + 10.0 * ( x[1] - x[0] * x[0] );
180 f[1] = x_dot[1] + 1.0 - x[0];
181 std::cout << "Residual Evaluation: Transient" << std::endl;
182 }
183 else {
184 NOX_FUNC_TIME_MONITOR("RosenbrockModelEvaluator::eval f_out");
185 const Thyra::DetachedVectorView<double> f(f_out);
186 f[0] = 10.0 * ( x[1] - x[0] * x[0] );
187 f[1] = 1.0 - x[0];
188 std::cout << "Residual Evaluation: Steady-State" << std::endl;
189 }
190 }
191 if(nonnull(W_out)) {
192 Teuchos::RCP<Epetra_Operator> W_epetra= Thyra::get_Epetra_Operator(*W_out);
193 Teuchos::RCP<Epetra_CrsMatrix> W_epetracrs = rcp_dynamic_cast<Epetra_CrsMatrix>(W_epetra);
194 TEUCHOS_ASSERT(nonnull(W_epetracrs));
195 Epetra_CrsMatrix& DfDx = *W_epetracrs;
196
197 DfDx.PutScalar(0.0);
198
199 int indexes[2];
200 indexes[0] = 0;
201 indexes[1] = 1;
202
203 double values[2];
204
205 if (nonnull(x_dot_rcp)) {
206 // Augment Jacobian for Pseudo-transient, use identity for V
207 // Row [0]
208 values[0] = alpha * 1.0 + beta * ( -20.0 * x[0] );
209 values[1] = beta * 10.0;
210 DfDx.SumIntoGlobalValues( 0, 2, values, indexes );
211 // Row [1]
212 values[0] = beta * (-1.0);
213 values[1] = alpha * 1.0 + beta * 0.0;
214 DfDx.SumIntoGlobalValues( 1, 2, values, indexes );
215 std::cout << "Jacobian Evaluation: Transient, alpha=" << alpha << ", beta=" << beta << std::endl;
216 }
217 else {
218 // Row [0]
219 values[0] = -20.0 * x[0];
220 values[1] = 10.0;
221 DfDx.SumIntoGlobalValues( 0, 2, values, indexes );
222 // Row [1]
223 values[0] = -1.0;
224 values[1] = 0.0;
225 DfDx.SumIntoGlobalValues( 1, 2, values, indexes );
226 std::cout << "Jacobian Evaluation: Steady-State, alpha=" << alpha << ", beta=" << beta << std::endl;
227 }
228
229 }
230 }