10 #ifndef Tempus_ModelEvaluatorPairPartIMEX_Basic_impl_hpp
11 #define Tempus_ModelEvaluatorPairPartIMEX_Basic_impl_hpp
13 #include "Thyra_ProductVectorBase.hpp"
14 #include "Thyra_ProductVectorSpaceBase.hpp"
18 template <
typename Scalar>
19 WrapperModelEvaluatorPairPartIMEX_Basic<
21 : timeDer_(Teuchos::null),
22 numExplicitOnlyBlocks_(0),
24 useImplicitModel_(false)
28 template <
typename Scalar>
33 int numExplicitOnlyBlocks,
int parameterIndex)
34 : timeDer_(Teuchos::null),
35 numExplicitOnlyBlocks_(numExplicitOnlyBlocks),
36 parameterIndex_(parameterIndex),
37 useImplicitModel_(false)
45 template <
typename Scalar>
49 int numExplicitOnlyBlocks,
int parameterIndex)
51 timeDer_ = Teuchos::null;
52 numExplicitOnlyBlocks_ = numExplicitOnlyBlocks;
53 parameterIndex_ = parameterIndex;
54 useImplicitModel_ =
false;
55 setExplicitModel(explicitModel);
56 setImplicitModel(implicitModel);
61 template <
typename Scalar>
66 useImplicitModel_ =
true;
67 wrapperImplicitInArgs_ = this->createInArgs();
68 wrapperImplicitOutArgs_ = this->createOutArgs();
69 useImplicitModel_ =
false;
71 RCP<const Thyra::VectorBase<Scalar> > z =
72 explicitModel_->getNominalValues().get_x();
76 !(getIMEXVector(z)->space()->isCompatible(
77 *(implicitModel_->get_x_space()))),
79 "Error - WrapperModelEvaluatorPairIMEX_Basic::initialize()\n"
80 <<
" Explicit and Implicit vector x spaces are incompatible!\n"
81 <<
" Explicit vector x space = "
82 << *(getIMEXVector(z)->space())
83 <<
"\n Implicit vector x space = "
84 << *(implicitModel_->get_x_space()) <<
"\n");
87 RCP<const Thyra::ProductVectorBase<Scalar> > zPVector =
90 zPVector == Teuchos::null, std::logic_error,
91 "Error - WrapperModelEvaluatorPairPartIMEX_Basic::initialize()\n"
92 " was given a VectorBase that could not be cast to a\n"
93 " ProductVectorBase!\n");
95 int numBlocks = zPVector->productSpace()->numBlocks();
97 TEUCHOS_TEST_FOR_EXCEPTION(
98 !(0 <= numExplicitOnlyBlocks_ && numExplicitOnlyBlocks_ < numBlocks),
100 "Error - WrapperModelEvaluatorPairPartIMEX_Basic::initialize()\n"
101 <<
"Invalid number of explicit-only blocks = "
102 << numExplicitOnlyBlocks_
103 <<
"\nShould be in the interval [0, numBlocks) = [0, "
104 << numBlocks <<
")\n");
107 template <
typename Scalar>
112 true, std::logic_error,
113 "Error - WrapperModelEvaluatorPairPartIMEX_Basic<Scalar>::setAppModel\n"
114 " should not be used. One should instead use setExplicitModel,\n"
115 " setImplicitModel, or create a new "
116 "WrapperModelEvaluatorPairPartIMEX.\n");
119 template <
typename Scalar>
124 true, std::logic_error,
125 "Error - WrapperModelEvaluatorPairPartIMEX_Basic<Scalar>::getAppModel\n"
126 " should not be used. One should instead use getExplicitModel,\n"
127 " getImplicitModel, or directly use WrapperModelEvaluatorPairPartIMEX\n"
131 template <
typename Scalar>
135 if (useImplicitModel_ ==
true)
return implicitModel_->get_x_space();
137 return explicitModel_->get_x_space();
140 template <
typename Scalar>
144 if (useImplicitModel_ ==
true)
return implicitModel_->get_g_space(i);
146 return explicitModel_->get_g_space(i);
149 template <
typename Scalar>
153 if (useImplicitModel_ ==
true)
return implicitModel_->get_p_space(i);
155 return explicitModel_->get_p_space(i);
158 template <
typename Scalar>
162 implicitModel_ = model;
165 template <
typename Scalar>
171 using Teuchos::rcp_dynamic_cast;
174 if (full == Teuchos::null) {
175 vector = Teuchos::null;
177 else if (numExplicitOnlyBlocks_ == 0) {
181 RCP<Thyra::ProductVectorBase<Scalar> > blk_full =
184 blk_full == Teuchos::null, std::logic_error,
185 "Error - WrapperModelEvaluatorPairPartIMEX_Basic::getIMEXVector()\n"
186 " was given a VectorBase that could not be cast to a\n"
187 " ProductVectorBase!\n");
188 int numBlocks = blk_full->productSpace()->numBlocks();
191 if (numBlocks == numExplicitOnlyBlocks_ + 1)
192 vector = blk_full->getNonconstVectorBlock(numExplicitOnlyBlocks_);
194 TEUCHOS_TEST_FOR_EXCEPTION(
195 !(numExplicitOnlyBlocks_ == 0 || full == Teuchos::null ||
196 numBlocks == numExplicitOnlyBlocks_ + 1),
197 std::logic_error,
"Error - Invalid values!\n");
203 template <
typename Scalar>
209 using Teuchos::rcp_dynamic_cast;
212 if (full == Teuchos::null) {
213 vector = Teuchos::null;
215 else if (numExplicitOnlyBlocks_ == 0) {
221 RCP<const Thyra::ProductVectorBase<Scalar> > blk_full =
224 blk_full == Teuchos::null, std::logic_error,
225 "Error - WrapperModelEvaluatorPairPartIMEX_Basic::getIMEXVector()\n"
226 " was given a VectorBase that could not be cast to a\n"
227 " ProductVectorBase!\n");
228 int numBlocks = blk_full->productSpace()->numBlocks();
231 if (numBlocks == numExplicitOnlyBlocks_ + 1)
232 vector = blk_full->getVectorBlock(numExplicitOnlyBlocks_);
234 TEUCHOS_TEST_FOR_EXCEPTION(
235 !(numExplicitOnlyBlocks_ == 0 || full == Teuchos::null ||
236 numBlocks == numExplicitOnlyBlocks_ + 1),
237 std::logic_error,
"Error - Invalid values!\n");
243 template <
typename Scalar>
249 using Teuchos::rcp_dynamic_cast;
252 if (numExplicitOnlyBlocks_ == 0 || full == Teuchos::null) {
253 vector = Teuchos::null;
255 else if (numExplicitOnlyBlocks_ == 1) {
258 RCP<Thyra::ProductVectorBase<Scalar> > blk_full =
261 blk_full == Teuchos::null, std::logic_error,
262 "Error - WrapperModelEvaluatorPairPartIMEX_Basic::getExplicitOnlyVector\n"
263 <<
" given a VectorBase that could not be cast to a ProductVectorBase!\n"
264 <<
" full = " << *full <<
"\n");
266 vector = blk_full->getNonconstVectorBlock(0);
270 !((numExplicitOnlyBlocks_ == 0 || full == Teuchos::null) ||
271 (numExplicitOnlyBlocks_ == 1)),
272 std::logic_error,
"Error - Invalid values!\n");
277 template <
typename Scalar>
283 using Teuchos::rcp_dynamic_cast;
285 RCP<const Thyra::VectorBase<Scalar> > vector;
286 if (numExplicitOnlyBlocks_ == 0 || full == Teuchos::null) {
287 vector = Teuchos::null;
289 else if (numExplicitOnlyBlocks_ == 1) {
292 RCP<const Thyra::ProductVectorBase<Scalar> > blk_full =
295 blk_full == Teuchos::null, std::logic_error,
296 "Error - WrapperModelEvaluatorPairPartIMEX_Basic::getExplicitOnlyVector\n"
297 <<
" given a VectorBase that could not be cast to a ProductVectorBase!\n"
298 <<
" full = " << *full <<
"\n");
300 vector = blk_full->getVectorBlock(0);
304 !((numExplicitOnlyBlocks_ == 0 || full == Teuchos::null) ||
305 (numExplicitOnlyBlocks_ == 1)),
306 std::logic_error,
"Error - Invalid values!\n");
311 template <
typename Scalar>
315 if (implicitModel_->Np() == 0) {
316 if (parameterIndex >= 0) {
321 "WrapperModelEvaluatorPairPartIMEX_Basic::setParameterIndex()");
322 *out <<
"Warning -- \n"
323 <<
" Invalid input parameter index = " << parameterIndex <<
"\n"
324 <<
" Should not be set since Np = " << implicitModel_->Np() <<
"\n"
325 <<
" Not setting parameter index." << std::endl;
327 if (parameterIndex_ >= 0) {
332 "WrapperModelEvaluatorPairPartIMEX_Basic::setParameterIndex()");
333 *out <<
"Warning -- \n"
334 <<
" Invalid parameter index = " << parameterIndex_ <<
"\n"
335 <<
" Should not be set since Np = " << implicitModel_->Np() <<
"\n"
336 <<
" Resetting to parameter index to unset state." << std::endl;
337 parameterIndex_ = -1;
341 if (parameterIndex >= 0) {
342 parameterIndex_ = parameterIndex;
344 else if (parameterIndex_ < 0) {
346 for (
int i = 0; i < implicitModel_->Np(); i++) {
347 if ((*implicitModel_->get_p_names(i))[0] ==
"EXPLICIT_ONLY_VECTOR") {
358 template <
typename Scalar>
362 if (useImplicitModel_ ==
true)
return implicitModel_->get_f_space();
364 return explicitModel_->get_f_space();
367 template <
typename Scalar>
372 MEB::InArgsSetup<Scalar> inArgs = this->createInArgs();
373 return std::move(inArgs);
376 template <
typename Scalar>
381 MEB::InArgs<Scalar> implicitInArgs = implicitModel_->getNominalValues();
382 MEB::InArgs<Scalar> explicitInArgs = explicitModel_->getNominalValues();
383 const int np = std::max(implicitInArgs.Np(), explicitInArgs.Np());
384 if (useImplicitModel_ ==
true) {
385 MEB::InArgsSetup<Scalar> inArgs(implicitInArgs);
386 inArgs.setModelEvalDescription(this->description());
388 return std::move(inArgs);
391 MEB::InArgsSetup<Scalar> inArgs(explicitInArgs);
392 inArgs.setModelEvalDescription(this->description());
394 return std::move(inArgs);
397 template <
typename Scalar>
402 MEB::OutArgs<Scalar> implicitOutArgs = implicitModel_->createOutArgs();
403 MEB::OutArgs<Scalar> explicitOutArgs = explicitModel_->createOutArgs();
404 const int np = std::max(implicitOutArgs.Np(), explicitOutArgs.Np());
405 if (useImplicitModel_ ==
true) {
406 MEB::OutArgsSetup<Scalar> outArgs(implicitOutArgs);
407 outArgs.setModelEvalDescription(this->description());
408 outArgs.set_Np_Ng(np, implicitOutArgs.Ng());
409 return std::move(outArgs);
412 MEB::OutArgsSetup<Scalar> outArgs(explicitOutArgs);
413 outArgs.setModelEvalDescription(this->description());
414 outArgs.set_Np_Ng(np, explicitOutArgs.Ng());
415 return std::move(outArgs);
418 template <
typename Scalar>
426 RCP<const Thyra::VectorBase<Scalar> > x = inArgs.
get_x();
427 RCP<Thyra::VectorBase<Scalar> > x_dot =
428 Thyra::createMember(implicitModel_->get_x_space());
429 timeDer_->compute(x, x_dot);
431 MEB::InArgs<Scalar> appImplicitInArgs(wrapperImplicitInArgs_);
432 MEB::OutArgs<Scalar> appImplicitOutArgs(wrapperImplicitOutArgs_);
433 appImplicitInArgs.set_x(x);
434 appImplicitInArgs.set_x_dot(x_dot);
435 for (
int i = 0; i < implicitModel_->Np(); ++i) {
437 if ((inArgs.
get_p(i) != Teuchos::null) && (i != parameterIndex_))
438 appImplicitInArgs.set_p(i, inArgs.
get_p(i));
441 appImplicitOutArgs.set_f(outArgs.
get_f());
442 appImplicitOutArgs.set_W_op(outArgs.
get_W_op());
444 implicitModel_->evalModel(appImplicitInArgs, appImplicitOutArgs);
449 #endif // Tempus_ModelEvaluatorPairPartIMEX_Basic_impl_hpp
virtual Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > getAppModel() const
Get the underlying application ModelEvaluator.
virtual Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int i) const
Get the g space.
virtual void setExplicitModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Evaluation< VectorBase< Scalar > > get_f() const
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getExplicitOnlyVector(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &full) const
Extract explicit-only vector from a full solution vector.
virtual Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
virtual void setParameterIndex(int parameterIndex=-1)
Set the parameter index for explicit-only vector.
virtual void initialize()
Initialize after setting member data.
virtual Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Get the x-solution space.
ModelEvaluator pair for implicit and explicit (IMEX) evaulations.
virtual Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &explicitModel, const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &implicitModel, int numExplicitOnlyBlocks=0, int parameterIndex=-1)
Setup ME when using default constructor – for derived classes.
virtual Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
virtual void setImplicitModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual void setAppModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &me)
Set the underlying application ModelEvaluator.
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getIMEXVector(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &full) const
Extract IMEX vector from a full solution vector.
WrapperModelEvaluatorPairPartIMEX_Basic()
virtual Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
virtual Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int i) const
Get the p space.
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_x() const
virtual void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &in, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &out) const
RCP< const VectorBase< Scalar > > get_p(int l) const