| 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_1DFEM_DEF_HPP |
| 10 |
#define NOX_THYRA_MODEL_EVALUATOR_1DFEM_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_Import.h" |
| 28 |
#include "Epetra_CrsGraph.h" |
| 29 |
#include "Epetra_CrsMatrix.h" |
| 30 |
|
| 31 |
// Nonmember constuctors |
| 32 |
|
| 33 |
template<class Scalar> |
| 34 |
Teuchos::RCP<ModelEvaluator1DPoisson<Scalar> > |
| 35 |
modelEvaluator1DPoisson(const Teuchos::RCP<const Epetra_Comm>& comm, |
| 36 |
const int num_global_elements, |
| 37 |
const Scalar a, |
| 38 |
const Scalar b) |
| 39 |
{ |
| 40 |
return Teuchos::rcp(new ModelEvaluator1DPoisson<Scalar>(comm,num_global_elements,a,b)); |
| 41 |
} |
| 42 |
|
| 43 |
// Constructor |
| 44 |
|
| 45 |
template<class Scalar> |
| 46 |
ModelEvaluator1DPoisson<Scalar>:: |
| 47 |
ModelEvaluator1DPoisson(const Teuchos::RCP<const Epetra_Comm>& comm, |
| 48 |
const int num_global_elements, |
| 49 |
const Scalar a, |
| 50 |
const Scalar b) : |
| 51 |
comm_(comm), |
| 52 |
num_global_elements_(num_global_elements), |
| 53 |
a_(a), |
| 54 |
b_(b), |
| 55 |
showGetInvalidArg_(false) |
| 56 |
{ |
| 57 |
using Teuchos::RCP; |
| 58 |
using Teuchos::rcp; |
| 59 |
using ::Thyra::VectorBase; |
| 60 |
typedef ::Thyra::ModelEvaluatorBase MEB; |
| 61 |
typedef Teuchos::ScalarTraits<Scalar> ST; |
| 62 |
|
| 63 |
TEUCHOS_ASSERT(nonnull(comm_)); |
| 64 |
|
| 65 |
const int num_global_nodes = num_global_elements_ + 1; |
| 66 |
|
| 67 |
// owned space |
| 68 |
x_owned_map_ = rcp(new Epetra_Map(num_global_nodes,0,*comm_)); |
| 69 |
x_space_ = ::Thyra::create_VectorSpace(x_owned_map_); |
| 70 |
|
| 71 |
// ghosted space |
| 72 |
if (comm_->NumProc() == 1) { |
| 73 |
x_ghosted_map_ = x_owned_map_; |
| 74 |
} else { |
| 75 |
|
| 76 |
int num_overlap_nodes; |
| 77 |
int min_overlap_gid; |
| 78 |
num_overlap_nodes = x_owned_map_->NumMyElements() + 2; |
| 79 |
if ( (comm_->MyPID() == 0) || (comm_->MyPID() == (comm_->NumProc() - 1)) ) |
| 80 |
num_overlap_nodes --; |
| 81 |
|
| 82 |
if (comm_->MyPID() == 0) |
| 83 |
min_overlap_gid = x_owned_map_->MinMyGID(); |
| 84 |
else |
| 85 |
min_overlap_gid = x_owned_map_->MinMyGID() - 1; |
| 86 |
|
| 87 |
int* overlap_gids = new int[num_overlap_nodes]; |
| 88 |
|
| 89 |
for (int i = 0; i < num_overlap_nodes; i ++) |
| 90 |
overlap_gids[i] = min_overlap_gid + i; |
| 91 |
|
| 92 |
x_ghosted_map_ = |
| 93 |
Teuchos::rcp(new Epetra_Map(-1, |
| 94 |
num_overlap_nodes, |
| 95 |
overlap_gids, |
| 96 |
0, |
| 97 |
*comm_)); |
| 98 |
|
| 99 |
delete [] overlap_gids; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
importer_ = Teuchos::rcp(new Epetra_Import(*x_ghosted_map_, *x_owned_map_)); |
| 104 |
|
| 105 |
// residual space |
| 106 |
f_owned_map_ = x_owned_map_; |
| 107 |
f_space_ = x_space_; |
| 108 |
|
| 109 |
x0_ = ::Thyra::createMember(x_space_); |
| 110 |
V_S(x0_.ptr(), ST::zero()); |
| 111 |
|
| 112 |
// Initialize the graph for W CrsMatrix object |
| 113 |
W_graph_ = createGraph(); |
| 114 |
|
| 115 |
MEB::InArgsSetup<Scalar> inArgs; |
| 116 |
inArgs.setModelEvalDescription(this->description()); |
| 117 |
inArgs.setSupports(MEB::IN_ARG_x); |
| 118 |
prototypeInArgs_ = inArgs; |
| 119 |
|
| 120 |
MEB::OutArgsSetup<Scalar> outArgs; |
| 121 |
outArgs.setModelEvalDescription(this->description()); |
| 122 |
outArgs.setSupports(MEB::OUT_ARG_f); |
| 123 |
outArgs.setSupports(MEB::OUT_ARG_W_op); |
| 124 |
prototypeOutArgs_ = outArgs; |
| 125 |
|
| 126 |
nominalValues_ = inArgs; |
| 127 |
nominalValues_.set_x(x0_); |
| 128 |
} |
| 129 |
|
| 130 |
// Initializers/Accessors |
| 131 |
|
| 132 |
template<class Scalar> |
| 133 |
Teuchos::RCP<Epetra_CrsGraph> |
| 134 |
ModelEvaluator1DPoisson<Scalar>::createGraph() |
| 135 |
{ |
| 136 |
Teuchos::RCP<Epetra_CrsGraph> W_graph; |
| 137 |
|
| 138 |
// Create the shell for the |
| 139 |
W_graph = Teuchos::rcp(new Epetra_CrsGraph(Copy, *x_owned_map_, 3)); |
| 140 |
|
| 141 |
// Declare required variables |
| 142 |
int num_owned_nodes = x_owned_map_->NumMyElements(); |
| 143 |
|
| 144 |
// Loop Over # of nodes on Processor |
| 145 |
for (int i=0; i < num_owned_nodes; i++) { |
| 146 |
int gid = x_owned_map_->GID(i); |
| 147 |
if (gid == 0) { |
| 148 |
int self[1] = {gid}; |
| 149 |
W_graph->InsertGlobalIndices(gid, 1, self); |
| 150 |
} else if (gid == num_global_elements_) { |
| 151 |
int left[2] = {gid - 1, gid}; |
| 152 |
W_graph->InsertGlobalIndices(gid, 2, left); |
| 153 |
} else { |
| 154 |
int all[3] = {gid - 1, gid, gid + 1}; |
| 155 |
W_graph->InsertGlobalIndices(gid, 3, all); |
| 156 |
} |
| 157 |
} |
| 158 |
W_graph->FillComplete(); |
| 159 |
return W_graph; |
| 160 |
} |
| 161 |
|
| 162 |
template<class Scalar> |
| 163 |
void ModelEvaluator1DPoisson<Scalar>::set_x0(const Teuchos::ArrayView<const Scalar> &x0_in) |
| 164 |
{ |
| 165 |
#ifdef TEUCHOS_DEBUG |
| 166 |
TEUCHOS_ASSERT_EQUALITY(x_space_->dim(), x0_in.size()); |
| 167 |
#endif |
| 168 |
Thyra::DetachedVectorView<Scalar> x0(x0_); |
| 169 |
x0.sv().values()().assign(x0_in); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
template<class Scalar> |
| 174 |
void ModelEvaluator1DPoisson<Scalar>::setShowGetInvalidArgs(bool showGetInvalidArg) |
| 175 |
{ |
| 176 |
showGetInvalidArg_ = showGetInvalidArg; |
| 177 |
} |
| 178 |
|
| 179 |
template<class Scalar> |
| 180 |
void ModelEvaluator1DPoisson<Scalar>:: |
| 181 |
set_W_factory(const Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<Scalar> >& W_factory) |
| 182 |
{ |
| 183 |
W_factory_ = W_factory; |
| 184 |
} |
| 185 |
|
| 186 |
// Public functions overridden from ModelEvaulator |
| 187 |
|
| 188 |
|
| 189 |
template<class Scalar> |
| 190 |
Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > |
| 191 |
ModelEvaluator1DPoisson<Scalar>::get_x_space() const |
| 192 |
{ |
| 193 |
return x_space_; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
template<class Scalar> |
| 198 |
Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > |
| 199 |
ModelEvaluator1DPoisson<Scalar>::get_f_space() const |
| 200 |
{ |
| 201 |
return f_space_; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
template<class Scalar> |
| 206 |
Thyra::ModelEvaluatorBase::InArgs<Scalar> |
| 207 |
ModelEvaluator1DPoisson<Scalar>::getNominalValues() const |
| 208 |
{ |
| 209 |
return nominalValues_; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
template<class Scalar> |
| 214 |
Teuchos::RCP<Thyra::LinearOpBase<Scalar> > |
| 215 |
ModelEvaluator1DPoisson<Scalar>::create_W_op() const |
| 216 |
{ |
| 217 |
Teuchos::RCP<Epetra_CrsMatrix> W_epetra = |
| 218 |
Teuchos::rcp(new Epetra_CrsMatrix(::Copy,*W_graph_)); |
| 219 |
|
| 220 |
return Thyra::nonconstEpetraLinearOp(W_epetra); |
| 221 |
} |
| 222 |
|
| 223 |
template<class Scalar> |
| 224 |
Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 225 |
ModelEvaluator1DPoisson<Scalar>::get_W_factory() const |
| 226 |
{ |
| 227 |
return W_factory_; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
template<class Scalar> |
| 232 |
Thyra::ModelEvaluatorBase::InArgs<Scalar> |
| 233 |
ModelEvaluator1DPoisson<Scalar>::createInArgs() const |
| 234 |
{ |
| 235 |
return prototypeInArgs_; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
// Private functions overridden from ModelEvaulatorDefaultBase |
| 240 |
|
| 241 |
|
| 242 |
template<class Scalar> |
| 243 |
Thyra::ModelEvaluatorBase::OutArgs<Scalar> |
| 244 |
ModelEvaluator1DPoisson<Scalar>::createOutArgsImpl() const |
| 245 |
{ |
| 246 |
return prototypeOutArgs_; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
template<class Scalar> |
| 251 |
void ModelEvaluator1DPoisson<Scalar>::evalModelImpl( |
| 252 |
const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs, |
| 253 |
const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs |
| 254 |
) const |
| 255 |
{ |
| 256 |
using Teuchos::RCP; |
| 257 |
using Teuchos::rcp; |
| 258 |
using Teuchos::rcp_dynamic_cast; |
| 259 |
|
| 260 |
TEUCHOS_ASSERT(nonnull(inArgs.get_x())); |
| 261 |
|
| 262 |
//const Thyra::ConstDetachedVectorView<Scalar> x(inArgs.get_x()); |
| 263 |
|
| 264 |
const RCP<Thyra::VectorBase<Scalar> > f_out = outArgs.get_f(); |
| 265 |
const RCP<Thyra::LinearOpBase<Scalar> > W_out = outArgs.get_W_op(); |
| 266 |
|
| 267 |
if ( nonnull(f_out) || nonnull(W_out) ) { |
| 268 |
|
| 269 |
// **************** |
| 270 |
// Get the underlying epetra objects |
| 271 |
// **************** |
| 272 |
|
| 273 |
RCP<Epetra_Vector> f; |
| 274 |
if (nonnull(f_out)) { |
| 275 |
f = Thyra::get_Epetra_Vector(*f_owned_map_,outArgs.get_f()); |
| 276 |
} |
| 277 |
|
| 278 |
RCP<Epetra_CrsMatrix> J; |
| 279 |
if (nonnull(W_out)) { |
| 280 |
RCP<Epetra_Operator> W_epetra = Thyra::get_Epetra_Operator(*W_out); |
| 281 |
J = rcp_dynamic_cast<Epetra_CrsMatrix>(W_epetra); |
| 282 |
TEUCHOS_ASSERT(nonnull(J)); |
| 283 |
} |
| 284 |
|
| 285 |
// **************** |
| 286 |
// Create ghosted objects |
| 287 |
// **************** |
| 288 |
|
| 289 |
if (is_null(u_ptr)) |
| 290 |
u_ptr = Teuchos::rcp(new Epetra_Vector(*x_ghosted_map_)); |
| 291 |
|
| 292 |
u_ptr->Import(*(Thyra::get_Epetra_Vector(*x_owned_map_,inArgs.get_x())), *importer_, Insert); |
| 293 |
|
| 294 |
Epetra_Vector& u = *u_ptr; |
| 295 |
|
| 296 |
// Zero out the objects that will be filled |
| 297 |
if (nonnull(f)) |
| 298 |
f->PutScalar(0.0); |
| 299 |
if (nonnull(J)) |
| 300 |
J->PutScalar(0.0); |
| 301 |
|
| 302 |
int num_owned_nodes = x_owned_map_->NumMyElements(); |
| 303 |
for (int i=0; i < num_owned_nodes; i++) { |
| 304 |
int gid = x_owned_map_->GID(i); |
| 305 |
if (nonnull(f)) { |
| 306 |
if (gid == 0) { |
| 307 |
(*f)[i] = u[0] - a_; |
| 308 |
} else if (gid == num_global_elements_) { |
| 309 |
(*f)[i] = u[x_ghosted_map_->LID(gid)] - b_; |
| 310 |
} else { |
| 311 |
(*f)[i] = - u[x_ghosted_map_->LID(gid)] |
| 312 |
+ u[x_ghosted_map_->LID(gid - 1)]/2. |
| 313 |
+ u[x_ghosted_map_->LID(gid + 1)]/2.; |
| 314 |
} |
| 315 |
} |
| 316 |
if (nonnull(J)) { |
| 317 |
if (gid == 0) { |
| 318 |
double vals[1] = {1.0}; |
| 319 |
int columns[1] = {gid}; |
| 320 |
J->SumIntoGlobalValues(gid, 1, vals, columns); |
| 321 |
} else if (gid == num_global_elements_) { |
| 322 |
double vals[1] = {1.0}; |
| 323 |
int columns[1] = {gid}; |
| 324 |
J->SumIntoGlobalValues(gid, 1, vals, columns); |
| 325 |
} else { |
| 326 |
double vals[3] = {0.5, -1.0, 0.5}; |
| 327 |
int columns[3] = {gid - 1, gid, gid + 1}; |
| 328 |
J->SumIntoGlobalValues(gid, 3, vals, columns); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if (nonnull(J)) |
| 334 |
J->FillComplete(); |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
} |
| 339 |
|
| 340 |
#endif |