| 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 |
#ifndef NOX_THYRA_MODEL_EVALUATOR_HEQ_DEF_HPP |
| 10 |
#define NOX_THYRA_MODEL_EVALUATOR_HEQ_DEF_HPP |
| 11 |
|
| 12 |
// Thyra support |
| 13 |
#include "Thyra_DefaultSpmdVectorSpace.hpp" |
| 14 |
#include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp" |
| 15 |
#include "Thyra_DetachedMultiVectorView.hpp" |
| 16 |
#include "Thyra_DetachedVectorView.hpp" |
| 17 |
#include "Thyra_MultiVectorStdOps.hpp" |
| 18 |
#include "Thyra_VectorStdOps.hpp" |
| 19 |
#include "Thyra_PreconditionerBase.hpp" |
| 20 |
|
| 21 |
// Epetra support |
| 22 |
#include "Thyra_EpetraThyraWrappers.hpp" |
| 23 |
#include "Thyra_get_Epetra_Operator.hpp" |
| 24 |
#include "Epetra_Comm.h" |
| 25 |
#include "Epetra_Map.h" |
| 26 |
#include "Epetra_Vector.h" |
| 27 |
#include "Epetra_CrsGraph.h" |
| 28 |
#include "Epetra_CrsMatrix.h" |
| 29 |
|
| 30 |
// Nonmember constuctors |
| 31 |
|
| 32 |
template<class Scalar> |
| 33 |
Teuchos::RCP<ModelEvaluatorHeq<Scalar> > |
| 34 |
modelEvaluatorHeq(const Teuchos::RCP<const Epetra_Comm>& comm, |
| 35 |
const int num_global_elements, |
| 36 |
const Scalar paramC) |
| 37 |
{ |
| 38 |
return Teuchos::rcp(new ModelEvaluatorHeq<Scalar>(comm,num_global_elements,paramC)); |
| 39 |
} |
| 40 |
|
| 41 |
// Constructor |
| 42 |
|
| 43 |
template<class Scalar> |
| 44 |
ModelEvaluatorHeq<Scalar>:: |
| 45 |
ModelEvaluatorHeq(const Teuchos::RCP<const Epetra_Comm>& comm, |
| 46 |
const int num_global_elements, |
| 47 |
const Scalar paramC) : |
| 48 |
comm_(comm), |
| 49 |
num_global_elements_(num_global_elements), |
| 50 |
paramC_(paramC), |
| 51 |
showGetInvalidArg_(false) |
| 52 |
{ |
| 53 |
using Teuchos::RCP; |
| 54 |
using Teuchos::rcp; |
| 55 |
using ::Thyra::VectorBase; |
| 56 |
typedef ::Thyra::ModelEvaluatorBase MEB; |
| 57 |
typedef Teuchos::ScalarTraits<Scalar> ST; |
| 58 |
|
| 59 |
TEUCHOS_ASSERT(nonnull(comm_)); |
| 60 |
|
| 61 |
// owned space |
| 62 |
x_map_ = rcp(new Epetra_Map(num_global_elements_,0,*comm_)); |
| 63 |
x_space_ = ::Thyra::create_VectorSpace(x_map_); |
| 64 |
|
| 65 |
// residual space |
| 66 |
f_map_ = x_map_; |
| 67 |
f_space_ = x_space_; |
| 68 |
|
| 69 |
x0_ = ::Thyra::createMember(x_space_); |
| 70 |
V_S(x0_.ptr(), ST::zero()); |
| 71 |
|
| 72 |
// Create the nodal coordinates |
| 73 |
{ |
| 74 |
node_coordinates_.resize(num_global_elements_); |
| 75 |
Scalar dx = 1.0/static_cast<Scalar>(num_global_elements_); |
| 76 |
for (int i=0; i < num_global_elements_; i++) |
| 77 |
node_coordinates_[i] = 0.5*dx + dx*static_cast<Scalar>(i); |
| 78 |
} |
| 79 |
|
| 80 |
// Compute the kernel of the integral operator, A_heq_ |
| 81 |
A_heq_ = rcp(new Epetra_CrsMatrix(::Copy,*x_map_,num_global_elements_)); |
| 82 |
int num_local_elements = x_map_->NumMyElements(); |
| 83 |
for (int i=0; i<num_local_elements; i++) { // Loop over rows on process |
| 84 |
int row = x_map_->GID(i); |
| 85 |
for(int j=0; j<num_global_elements_; j++) { // Loop over entries in row |
| 86 |
Scalar value = node_coordinates_[row]/(node_coordinates_[row]+node_coordinates_[j]); |
| 87 |
A_heq_->InsertGlobalValues(row,1,&value,&j); |
| 88 |
} |
| 89 |
} |
| 90 |
A_heq_->FillComplete(); |
| 91 |
Scalar cc = 0.5*paramC_/static_cast<Scalar>(num_global_elements_); |
| 92 |
A_heq_->Scale(cc); |
| 93 |
|
| 94 |
ones_ = Teuchos::rcp(new Epetra_Vector(*x_map_)); |
| 95 |
ones_->PutScalar(1.0); |
| 96 |
|
| 97 |
MEB::InArgsSetup<Scalar> inArgs; |
| 98 |
inArgs.setModelEvalDescription(this->description()); |
| 99 |
inArgs.setSupports(MEB::IN_ARG_x); |
| 100 |
prototypeInArgs_ = inArgs; |
| 101 |
|
| 102 |
MEB::OutArgsSetup<Scalar> outArgs; |
| 103 |
outArgs.setModelEvalDescription(this->description()); |
| 104 |
outArgs.setSupports(MEB::OUT_ARG_f); |
| 105 |
outArgs.setSupports(MEB::OUT_ARG_W_op); |
| 106 |
outArgs.setSupports(MEB::OUT_ARG_W_prec); |
| 107 |
prototypeOutArgs_ = outArgs; |
| 108 |
|
| 109 |
nominalValues_ = inArgs; |
| 110 |
nominalValues_.set_x(x0_); |
| 111 |
} |
| 112 |
|
| 113 |
// Initializers/Accessors |
| 114 |
|
| 115 |
template<class Scalar> |
| 116 |
void ModelEvaluatorHeq<Scalar>::set_x0(const Teuchos::ArrayView<const Scalar> &x0_in) |
| 117 |
{ |
| 118 |
#ifdef TEUCHOS_DEBUG |
| 119 |
TEUCHOS_ASSERT_EQUALITY(x_space_->dim(), x0_in.size()); |
| 120 |
#endif |
| 121 |
Thyra::DetachedVectorView<Scalar> x0(x0_); |
| 122 |
x0.sv().values()().assign(x0_in); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
template<class Scalar> |
| 127 |
void ModelEvaluatorHeq<Scalar>::setShowGetInvalidArgs(bool showGetInvalidArg) |
| 128 |
{ |
| 129 |
showGetInvalidArg_ = showGetInvalidArg; |
| 130 |
} |
| 131 |
|
| 132 |
template<class Scalar> |
| 133 |
void ModelEvaluatorHeq<Scalar>:: |
| 134 |
set_W_factory(const Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<Scalar> >& W_factory) |
| 135 |
{ |
| 136 |
W_factory_ = W_factory; |
| 137 |
} |
| 138 |
|
| 139 |
// Public functions overridden from ModelEvaulator |
| 140 |
|
| 141 |
|
| 142 |
template<class Scalar> |
| 143 |
Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > |
| 144 |
ModelEvaluatorHeq<Scalar>::get_x_space() const |
| 145 |
{ |
| 146 |
return x_space_; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
template<class Scalar> |
| 151 |
Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > |
| 152 |
ModelEvaluatorHeq<Scalar>::get_f_space() const |
| 153 |
{ |
| 154 |
return f_space_; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
template<class Scalar> |
| 159 |
Thyra::ModelEvaluatorBase::InArgs<Scalar> |
| 160 |
ModelEvaluatorHeq<Scalar>::getNominalValues() const |
| 161 |
{ |
| 162 |
return nominalValues_; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
template<class Scalar> |
| 167 |
Teuchos::RCP<Thyra::LinearOpBase<Scalar> > |
| 168 |
ModelEvaluatorHeq<Scalar>::create_W_op() const |
| 169 |
{ |
| 170 |
Teuchos::RCP<Epetra_CrsMatrix> W_epetra = |
| 171 |
Teuchos::rcp(new Epetra_CrsMatrix(::Copy,*x_map_,num_global_elements_)); |
| 172 |
|
| 173 |
return Thyra::nonconstEpetraLinearOp(W_epetra); |
| 174 |
} |
| 175 |
|
| 176 |
template<class Scalar> |
| 177 |
Teuchos::RCP< ::Thyra::PreconditionerBase<Scalar> > |
| 178 |
ModelEvaluatorHeq<Scalar>::create_W_prec() const |
| 179 |
{ |
| 180 |
Teuchos::RCP<Epetra_CrsMatrix> W_epetra = |
| 181 |
Teuchos::rcp(new Epetra_CrsMatrix(::Copy,*x_map_,1)); |
| 182 |
|
| 183 |
const Teuchos::RCP<Thyra::LinearOpBase< Scalar > > W_op = |
| 184 |
Thyra::nonconstEpetraLinearOp(W_epetra); |
| 185 |
|
| 186 |
Teuchos::RCP<Thyra::DefaultPreconditioner<Scalar> > prec = |
| 187 |
Teuchos::rcp(new Thyra::DefaultPreconditioner<Scalar>); |
| 188 |
|
| 189 |
prec->initializeRight(W_op); |
| 190 |
|
| 191 |
return prec; |
| 192 |
} |
| 193 |
|
| 194 |
template<class Scalar> |
| 195 |
Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 196 |
ModelEvaluatorHeq<Scalar>::get_W_factory() const |
| 197 |
{ |
| 198 |
return W_factory_; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
template<class Scalar> |
| 203 |
Thyra::ModelEvaluatorBase::InArgs<Scalar> |
| 204 |
ModelEvaluatorHeq<Scalar>::createInArgs() const |
| 205 |
{ |
| 206 |
return prototypeInArgs_; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
// Private functions overridden from ModelEvaulatorDefaultBase |
| 211 |
|
| 212 |
|
| 213 |
template<class Scalar> |
| 214 |
Thyra::ModelEvaluatorBase::OutArgs<Scalar> |
| 215 |
ModelEvaluatorHeq<Scalar>::createOutArgsImpl() const |
| 216 |
{ |
| 217 |
return prototypeOutArgs_; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
template<class Scalar> |
| 222 |
void ModelEvaluatorHeq<Scalar>::evalModelImpl( |
| 223 |
const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs, |
| 224 |
const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs |
| 225 |
) const |
| 226 |
{ |
| 227 |
using Teuchos::RCP; |
| 228 |
using Teuchos::rcp; |
| 229 |
using Teuchos::rcp_dynamic_cast; |
| 230 |
|
| 231 |
TEUCHOS_ASSERT(nonnull(inArgs.get_x())); |
| 232 |
|
| 233 |
const RCP<Thyra::VectorBase<Scalar> > f_out = outArgs.get_f(); |
| 234 |
const RCP<Thyra::LinearOpBase<Scalar> > W_out = outArgs.get_W_op(); |
| 235 |
const RCP<Thyra::PreconditionerBase<Scalar> > W_prec_out = outArgs.get_W_prec(); |
| 236 |
|
| 237 |
if (is_null(x_ptr_)) { |
| 238 |
x_ptr_ = Teuchos::rcp(new Epetra_Vector(*x_map_)); |
| 239 |
} |
| 240 |
x_ptr_ = Thyra::get_Epetra_Vector(*x_map_,inArgs.get_x()); |
| 241 |
const Epetra_Vector& x = *x_ptr_; |
| 242 |
|
| 243 |
if ( nonnull(f_out) || nonnull(W_out) || nonnull(W_prec_out) ) { |
| 244 |
|
| 245 |
// **************** |
| 246 |
// Get the underlying epetra objects |
| 247 |
// **************** |
| 248 |
|
| 249 |
RCP<Epetra_Vector> f; |
| 250 |
if (nonnull(f_out)) { |
| 251 |
f = Thyra::get_Epetra_Vector(*f_map_,outArgs.get_f()); |
| 252 |
} |
| 253 |
|
| 254 |
RCP<Epetra_CrsMatrix> J; |
| 255 |
if (nonnull(W_out)) { |
| 256 |
RCP<Epetra_Operator> W_epetra = Thyra::get_Epetra_Operator(*W_out); |
| 257 |
J = rcp_dynamic_cast<Epetra_CrsMatrix>(W_epetra); |
| 258 |
TEUCHOS_ASSERT(nonnull(J)); |
| 259 |
} |
| 260 |
|
| 261 |
RCP<Epetra_CrsMatrix> M_inv; |
| 262 |
if (nonnull(W_prec_out)) { |
| 263 |
RCP<Epetra_Operator> M_epetra = Thyra::get_Epetra_Operator(*(W_prec_out->getNonconstRightPrecOp())); |
| 264 |
M_inv = rcp_dynamic_cast<Epetra_CrsMatrix>(M_epetra); |
| 265 |
TEUCHOS_ASSERT(nonnull(M_inv)); |
| 266 |
} |
| 267 |
|
| 268 |
// Zero out the objects that will be filled |
| 269 |
if (nonnull(f)) |
| 270 |
f->PutScalar(0.0); |
| 271 |
if (nonnull(J)) |
| 272 |
J->PutScalar(0.0); |
| 273 |
if (nonnull(M_inv)) |
| 274 |
M_inv->PutScalar(0.0); |
| 275 |
|
| 276 |
// Shared computation for f or J evaluation |
| 277 |
RCP<Epetra_Vector> workVec = rcp(new Epetra_Vector(*x_map_)); |
| 278 |
if (nonnull(f) || nonnull(J)) { |
| 279 |
A_heq_->Multiply(false, x, *workVec); |
| 280 |
workVec->Update(1.0,*ones_,-1.0); |
| 281 |
workVec->Reciprocal(*workVec); |
| 282 |
} |
| 283 |
|
| 284 |
// Computing f |
| 285 |
if (nonnull(f)){ |
| 286 |
*f = *workVec; |
| 287 |
f->Update(-1.0,x,1.0); |
| 288 |
} |
| 289 |
|
| 290 |
// Computing Jacobian |
| 291 |
if (nonnull(J)){ |
| 292 |
*J = *A_heq_; |
| 293 |
workVec->Multiply(1.0,*workVec,*workVec,0.0); |
| 294 |
J->LeftScale(*workVec); |
| 295 |
// Subtract off identity |
| 296 |
int num_local_elements = x_map_->NumMyElements(); |
| 297 |
for (int i=0; i<num_local_elements; i++) { |
| 298 |
int row = x_map_->GID(i); |
| 299 |
Scalar value = -1.0; |
| 300 |
J->SumIntoGlobalValues(row,1,&value,&row); |
| 301 |
} |
| 302 |
J->FillComplete(); |
| 303 |
} |
| 304 |
|
| 305 |
// Preconditioner, set to identity |
| 306 |
if (nonnull(M_inv)){ |
| 307 |
M_inv->ReplaceDiagonalValues(*ones_); |
| 308 |
M_inv->FillComplete(); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
#endif |