9 #ifndef NOX_THYRA_MODEL_EVALUATOR_1DFEM_DEF_HPP
10 #define NOX_THYRA_MODEL_EVALUATOR_1DFEM_DEF_HPP
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"
22 #include "Thyra_EpetraThyraWrappers.hpp"
23 #include "Thyra_EpetraLinearOp.hpp"
24 #include "Thyra_get_Epetra_Operator.hpp"
25 #include "Epetra_Comm.h"
26 #include "Epetra_Map.h"
27 #include "Epetra_Vector.h"
28 #include "Epetra_Import.h"
29 #include "Epetra_CrsGraph.h"
30 #include "Epetra_CrsMatrix.h"
32 namespace Tempus_Test {
36 template<
class Scalar>
38 CDR_Model(
const Teuchos::RCP<const Epetra_Comm>& comm,
39 const int num_global_elements,
45 num_global_elements_(num_global_elements),
50 showGetInvalidArg_(false)
54 using ::Thyra::VectorBase;
55 typedef ::Thyra::ModelEvaluatorBase MEB;
56 typedef Teuchos::ScalarTraits<Scalar> ST;
58 TEUCHOS_ASSERT(nonnull(
comm_));
67 if (
comm_->NumProc() == 1) {
71 int OverlapNumMyElements;
73 OverlapNumMyElements =
x_owned_map_->NumMyElements() + 2;
74 if ( (
comm_->MyPID() == 0) || (
comm_->MyPID() == (
comm_->NumProc() - 1)) )
75 OverlapNumMyElements --;
77 if (
comm_->MyPID() == 0)
82 int* OverlapMyGlobalElements =
new int[OverlapNumMyElements];
84 for (
int i = 0; i < OverlapNumMyElements; i ++)
85 OverlapMyGlobalElements[i] = OverlapMinMyGID + i;
88 Teuchos::rcp(
new Epetra_Map(-1,
90 OverlapMyGlobalElements,
94 delete [] OverlapMyGlobalElements;
105 V_S(
x0_.ptr(), ST::zero());
119 (*node_coordinates_)[i] = z_min_ + dx*((double)
x_owned_map_->MinMyGID() + i);
125 MEB::InArgsSetup<Scalar> inArgs;
126 inArgs.setModelEvalDescription(this->description());
127 inArgs.setSupports(MEB::IN_ARG_t);
128 inArgs.setSupports(MEB::IN_ARG_x);
129 inArgs.setSupports( MEB::IN_ARG_beta );
130 inArgs.setSupports( MEB::IN_ARG_x_dot );
131 inArgs.setSupports( MEB::IN_ARG_alpha );
134 MEB::OutArgsSetup<Scalar> outArgs;
135 outArgs.setModelEvalDescription(this->description());
136 outArgs.setSupports(MEB::OUT_ARG_f);
137 outArgs.setSupports(MEB::OUT_ARG_W);
138 outArgs.setSupports(MEB::OUT_ARG_W_op);
139 outArgs.setSupports(MEB::OUT_ARG_W_prec);
150 auto x_dot_init = Thyra::createMember(this->
get_x_space());
151 Thyra::put_scalar(Scalar(0.0),x_dot_init.ptr());
157 template<
class Scalar>
158 Teuchos::RCP<Epetra_CrsGraph>
161 Teuchos::RCP<Epetra_CrsGraph> W_graph;
164 W_graph = Teuchos::rcp(
new Epetra_CrsGraph(Copy, *x_owned_map_, 5));
169 int OverlapNumMyElements = x_ghosted_map_->NumMyElements();
172 for (
int ne=0; ne < OverlapNumMyElements-1; ne++) {
175 for (
int i=0; i< 2; i++) {
176 row=x_ghosted_map_->GID(ne+i);
179 for(
int j=0;j < 2; j++) {
182 if (x_owned_map_->MyGID(row)) {
183 column=x_ghosted_map_->GID(ne+j);
184 W_graph->InsertGlobalIndices(row, 1, &column);
189 W_graph->FillComplete();
193 template<
class Scalar>
197 TEUCHOS_ASSERT_EQUALITY(x_space_->dim(), x0_in.size());
199 Thyra::DetachedVectorView<Scalar> x0(x0_);
200 x0.sv().values()().assign(x0_in);
204 template<
class Scalar>
207 showGetInvalidArg_ = showGetInvalidArg;
210 template<
class Scalar>
212 set_W_factory(
const Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<Scalar> >& W_factory)
214 W_factory_ = W_factory;
220 template<
class Scalar>
221 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
228 template<
class Scalar>
229 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
236 template<
class Scalar>
237 Thyra::ModelEvaluatorBase::InArgs<Scalar>
240 return nominalValues_;
244 template<
class Scalar>
245 Teuchos::RCP<Thyra::LinearOpWithSolveBase<double> >
248 Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<double> > W_factory =
249 this->get_W_factory();
251 TEUCHOS_TEST_FOR_EXCEPTION(is_null(W_factory),std::runtime_error,
"W_factory in CDR_Model has a null W_factory!");
253 Teuchos::RCP<Thyra::LinearOpBase<double> > matrix = this->create_W_op();
255 Teuchos::RCP<Thyra::LinearOpWithSolveBase<double> > W =
256 Thyra::linearOpWithSolve<double>(*W_factory,matrix);
261 template<
class Scalar>
262 Teuchos::RCP<Thyra::LinearOpBase<Scalar> >
265 Teuchos::RCP<Epetra_CrsMatrix> W_epetra =
266 Teuchos::rcp(
new Epetra_CrsMatrix(::Copy,*W_graph_));
268 return Thyra::nonconstEpetraLinearOp(W_epetra);
271 template<
class Scalar>
272 Teuchos::RCP< ::Thyra::PreconditionerBase<Scalar> >
275 Teuchos::RCP<Epetra_CrsMatrix> W_epetra =
276 Teuchos::rcp(
new Epetra_CrsMatrix(::Copy,*W_graph_));
278 const Teuchos::RCP<Thyra::LinearOpBase< Scalar > > W_op =
279 Thyra::nonconstEpetraLinearOp(W_epetra);
281 Teuchos::RCP<Thyra::DefaultPreconditioner<Scalar> > prec =
282 Teuchos::rcp(
new Thyra::DefaultPreconditioner<Scalar>);
284 prec->initializeRight(W_op);
289 template<
class Scalar>
290 Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> >
297 template<
class Scalar>
298 Thyra::ModelEvaluatorBase::InArgs<Scalar>
301 return prototypeInArgs_;
308 template<
class Scalar>
309 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
312 return prototypeOutArgs_;
316 template<
class Scalar>
318 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
319 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
324 using Teuchos::rcp_dynamic_cast;
326 TEUCHOS_ASSERT(nonnull(inArgs.get_x()));
327 TEUCHOS_ASSERT(nonnull(inArgs.get_x_dot()));
331 const RCP<Thyra::VectorBase<Scalar> > f_out = outArgs.get_f();
332 const RCP<Thyra::LinearOpBase<Scalar> > W_out = outArgs.get_W_op();
333 const RCP<Thyra::PreconditionerBase<Scalar> > W_prec_out = outArgs.get_W_prec();
336 if ( nonnull(f_out) || nonnull(W_out) || nonnull(W_prec_out) ) {
342 RCP<Epetra_Vector> f;
343 if (nonnull(f_out)) {
344 f = Thyra::get_Epetra_Vector(*f_owned_map_,outArgs.get_f());
347 RCP<Epetra_CrsMatrix> J;
348 if (nonnull(W_out)) {
349 RCP<Epetra_Operator> W_epetra = Thyra::get_Epetra_Operator(*W_out);
350 J = rcp_dynamic_cast<Epetra_CrsMatrix>(W_epetra);
351 TEUCHOS_ASSERT(nonnull(J));
354 RCP<Epetra_CrsMatrix> M_inv;
355 if (nonnull(W_prec_out)) {
356 RCP<Epetra_Operator> M_epetra = Thyra::get_Epetra_Operator(*(W_prec_out->getNonconstRightPrecOp()));
357 M_inv = rcp_dynamic_cast<Epetra_CrsMatrix>(M_epetra);
358 TEUCHOS_ASSERT(nonnull(M_inv));
359 J_diagonal_ = Teuchos::rcp(
new Epetra_Vector(*x_owned_map_));
367 if (comm_->MyPID() == 0) {
368 RCP<Thyra::VectorBase<Scalar> > x = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> > (inArgs.get_x());
369 (*Thyra::get_Epetra_Vector(*x_owned_map_,x))[0] = 1.0;
373 u_ptr = Teuchos::rcp(
new Epetra_Vector(*x_ghosted_map_));
375 u_ptr->Import(*(Thyra::get_Epetra_Vector(*x_owned_map_,inArgs.get_x())), *importer_, Insert);
377 if (is_null(u_dot_ptr))
378 u_dot_ptr = Teuchos::rcp(
new Epetra_Vector(*x_ghosted_map_));
380 u_dot_ptr->Import(*(Thyra::get_Epetra_Vector(*x_owned_map_,inArgs.get_x_dot())), *importer_, Insert);
382 if (is_null(x_ptr)) {
383 x_ptr = Teuchos::rcp(
new Epetra_Vector(*x_ghosted_map_));
384 x_ptr->Import(*node_coordinates_, *importer_, Insert);
387 Epetra_Vector& u = *u_ptr;
388 Epetra_Vector& u_dot = *u_dot_ptr;
389 Epetra_Vector& x = *x_ptr;
392 int OverlapNumMyElements = x_ghosted_map_->NumMyElements();
398 const auto alpha = inArgs.get_alpha();
399 const auto beta = inArgs.get_beta();
407 M_inv->PutScalar(0.0);
412 for (
int ne=0; ne < OverlapNumMyElements-1; ne++) {
415 for(
int gp=0; gp < 2; gp++) {
422 uu_dot[1]=u_dot[ne+1];
427 for (
int i=0; i< 2; i++) {
428 int row=x_ghosted_map_->GID(ne+i);
431 if (x_owned_map_->MyGID(row)) {
433 (*f)[x_owned_map_->LID(x_ghosted_map_->GID(ne+i))]+=
436 +(a_/basis.
dz*basis.
duu*basis.
phi[i]
438 +k_*basis.
uu*basis.
uu*basis.
phi[i] );
443 for(
int j=0;j < 2; j++) {
444 if (x_owned_map_->MyGID(row)) {
445 int column=x_ghosted_map_->GID(ne+j);
448 alpha * basis.
phi[i] * basis.
phi[j]
452 +2.0*k_*basis.
uu*basis.
phi[j]*basis.
phi[i]
455 ierr=J->SumIntoGlobalValues(row, 1, &jac, &column);
459 if (nonnull(M_inv)) {
460 for(
int j=0;j < 2; j++) {
461 if (x_owned_map_->MyGID(row)) {
462 int column=x_ghosted_map_->GID(ne+j);
467 alpha * basis.
phi[i] * basis.
phi[j]
471 +2.0*k_*basis.
uu*basis.
phi[j]*basis.
phi[i]
474 ierr = M_inv->SumIntoGlobalValues(row, 1, &jac, &column);
485 if (comm_->MyPID() == 0) {
492 ierr = J->ReplaceGlobalValues(0, 1, &jac, &column);
495 ierr = J->ReplaceGlobalValues(0, 1, &jac, &column);
497 if (nonnull(M_inv)) {
500 ierr = M_inv->ReplaceGlobalValues(0, 1, &jac, &column);
503 ierr = M_inv->ReplaceGlobalValues(0, 1, &jac, &column);
510 if (nonnull(M_inv)) {
512 M_inv->ExtractDiagonalCopy(*J_diagonal_);
514 for (
int i=0; i < J_diagonal_->MyLength(); ++i)
515 (*J_diagonal_)[i] = 1.0 / ((*J_diagonal_)[i]);
517 M_inv->PutScalar(0.0);
518 M_inv->ReplaceDiagonalValues(*J_diagonal_);
519 M_inv->FillComplete();
522 TEUCHOS_ASSERT(ierr > -1);
555 if (gp==0) {
eta=-1.0/sqrt(3.0);
wt=1.0;}
556 if (gp==1) {
eta=1.0/sqrt(3.0);
wt=1.0;}
571 for (
int i=0; i < N; i++) {
576 uu_dot += u_dot[i] * phi[i];
577 duu_dot += u_dot[i] * dphide[i];
::Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
void evalModelImpl(const ::Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const ::Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
Teuchos::RCP< const Epetra_Map > x_ghosted_map_
Teuchos::RCP< const ::Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
CDR_Model(const Teuchos::RCP< const Epetra_Comm > &comm, const int num_global_elements, const Scalar z_min, const Scalar z_max, const Scalar a, const Scalar k)
::Thyra::ModelEvaluatorBase::OutArgs< Scalar > prototypeOutArgs_
virtual Teuchos::RCP< Epetra_CrsGraph > createGraph()
Teuchos::RCP< Epetra_CrsGraph > W_graph_
Teuchos::RCP< Epetra_Vector > node_coordinates_
Teuchos::RCP< const Epetra_Map > f_owned_map_
const int num_global_elements_
Teuchos::RCP< ::Thyra::VectorBase< Scalar > > x0_
Teuchos::RCP< ::Thyra::PreconditionerBase< Scalar > > create_W_prec() const
void setShowGetInvalidArgs(bool showGetInvalidArg)
Teuchos::RCP< const Epetra_Map > x_owned_map_
::Thyra::ModelEvaluatorBase::InArgs< Scalar > nominalValues_
void set_W_factory(const Teuchos::RCP< const ::Thyra::LinearOpWithSolveFactoryBase< Scalar > > &W_factory)
void set_x0(const Teuchos::ArrayView< const Scalar > &x0)
void computeBasis(int gp, double *z, double *u, double *u_dot=nullptr)
Teuchos::RCP< const ::Thyra::VectorSpaceBase< Scalar > > x_space_
Teuchos::RCP< ::Thyra::LinearOpBase< Scalar > > create_W_op() const
Teuchos::RCP< Thyra::LinearOpWithSolveBase< double > > create_W() const
::Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
Teuchos::RCP< const ::Thyra::VectorSpaceBase< Scalar > > get_f_space() const
::Thyra::ModelEvaluatorBase::InArgs< Scalar > prototypeInArgs_
Teuchos::RCP< const ::Thyra::VectorSpaceBase< Scalar > > get_x_space() const
const Teuchos::RCP< const Epetra_Comm > comm_
Teuchos::RCP< const ::Thyra::VectorSpaceBase< Scalar > > f_space_
::Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Teuchos::RCP< const Epetra_Import > importer_