10 #ifndef TEMPUS_TEST_HARMONIC_OSCILLATOR_MODEL_IMPL_HPP
11 #define TEMPUS_TEST_HARMONIC_OSCILLATOR_MODEL_IMPL_HPP
13 #include "Teuchos_StandardParameterEntryValidators.hpp"
15 #include "Thyra_DefaultSpmdVectorSpace.hpp"
16 #include "Thyra_DetachedVectorView.hpp"
17 #include "Thyra_DetachedMultiVectorView.hpp"
18 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
19 #include "Thyra_DefaultMultiVectorLinearOpWithSolve.hpp"
20 #include "Thyra_DefaultLinearOpSource.hpp"
21 #include "Thyra_VectorStdOps.hpp"
24 namespace Tempus_Test {
26 template <
class Scalar>
29 : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
33 *
out_ <<
"\n\nDamping coeff c = " <<
c_ <<
"\n";
34 *
out_ <<
"Forcing coeff f = " <<
f_ <<
"\n";
35 *
out_ <<
"x coeff k = " <<
k_ <<
"\n";
36 *
out_ <<
"Mass coeff m = " <<
m_ <<
"\n";
46 Thyra::put_scalar(0.0, x_vec_.ptr());
48 Thyra::put_scalar(1.0, x_dot_vec_.ptr());
55 if (use_accel_IC ==
true) {
56 Thyra::put_scalar(-2.0, x_dot_dot_vec_.ptr());
61 Thyra::put_scalar(7.0, x_dot_dot_vec_.ptr());
71 template <
class Scalar>
77 "Error, setupInOutArgs_ must be called first!\n");
80 inArgs.
set_t(exact_t);
86 exact_x_view[0] = t * (1.0 + 0.5 * f_ * t);
89 (c_ - f_) / (c_ * c_) * (1.0 - exp(-c_ * t)) + f_ * t / c_;
92 exact_x_view[0] = 1.0 / sqrt(k_) * sin(sqrt(k_) * t) +
93 f_ / k_ * (1.0 - cos(sqrt(k_) * t));
96 inArgs.
set_x(exact_x);
102 exact_x_dot_view[0] = 1.0 + f_ * t;
104 exact_x_dot_view[0] = (c_ - f_) / c_ * exp(-c_ * t) + f_ / c_;
107 exact_x_dot_view[0] =
108 cos(sqrt(k_) * t) + f_ / sqrt(k_) * sin(sqrt(k_) * t);
117 exact_x_dot_dot_view[0] = f_;
119 exact_x_dot_dot_view[0] = (f_ - c_) * exp(-c_ * t);
122 exact_x_dot_dot_view[0] =
123 f_ * cos(sqrt(k_) * t) - sqrt(k_) * sin(sqrt(k_) * t);
130 template <
class Scalar>
137 template <
class Scalar>
144 template <
class Scalar>
149 "Error, setupInOutArgs_ must be called first!\n");
150 return nominalValues_;
153 template <
class Scalar>
158 this->get_W_factory();
164 matrix_view(0, 0) = 1.0;
166 Thyra::linearOpWithSolve<Scalar>(*W_factory, matrix);
170 template <
class Scalar>
175 Thyra::createMembers(x_space_, vecLength_);
179 template <
class Scalar>
184 Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>();
188 template <
class Scalar>
198 template <
class Scalar>
206 template <
class Scalar>
214 "Error, setupInOutArgs_ must be called first!\n");
220 true, std::logic_error,
221 "\n ERROR: HarmonicOscillatorModel requires x as InArgs.\n");
226 auto myVecLength = x_in_view.
subDim();
239 Scalar neg_sign = 1.0;
244 if (f_out != Teuchos::null) {
246 for (
int i = 0; i < myVecLength; i++) {
249 if (x_dotdot_in != Teuchos::null) {
251 for (
int i = 0; i < myVecLength; i++) {
252 f_out_view[i] = x_dotdot_in_view[i] - f_out_view[i];
255 if (x_dot_in != Teuchos::null) {
257 for (
int i = 0; i < myVecLength; i++) {
258 f_out_view[i] += neg_sign * c_ * x_dot_in_view[i];
261 if (x_in != Teuchos::null) {
262 for (
int i = 0; i < myVecLength; i++) {
263 f_out_view[i] += neg_sign * k_ * x_in_view[i];
269 if (W_out != Teuchos::null) {
275 true, std::logic_error,
276 "\n ERROR: omega = 0 in HarmonicOscillatorModel!\n");
278 matrix_view(0, 0) = omega;
279 if (x_dot_in != Teuchos::null) {
280 matrix_view(0, 0) += neg_sign * c_ * alpha;
282 if (x_in != Teuchos::null) {
283 matrix_view(0, 0) += neg_sign * k_ * beta;
289 if (g_out != Teuchos::null) {
291 g_out_view[0] = Thyra::sum(*x_in) / vecLength_;
295 template <
class Scalar>
300 true, std::logic_error,
301 "\n Error! HarmonicOscillatorModel::get_p_space() is not supported!\n");
302 return Teuchos::null;
305 template <
class Scalar>
310 true, std::logic_error,
311 "\n Error! HarmonicOscillatorModel::get_p_names() is not supported!\n");
312 return Teuchos::null;
315 template <
class Scalar>
320 j != 0, std::logic_error,
321 "\n Error! HarmonicOscillatorModel::get_g_space() only "
322 <<
" supports 1 parameter vector. Supplied index l = " << j <<
"\n");
328 template <
class Scalar>
331 if (isInitialized_)
return;
337 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_x);
338 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_x_dot);
339 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_x_dot_dot);
340 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_t);
341 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_W_x_dot_dot_coeff);
342 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_alpha);
343 inArgs.
setSupports(Thyra::ModelEvaluatorBase::IN_ARG_beta);
351 outArgs.
setSupports(Thyra::ModelEvaluatorBase::OUT_ARG_f);
352 outArgs.
setSupports(Thyra::ModelEvaluatorBase::OUT_ARG_W_op);
360 nominalValues_ = inArgs_;
361 nominalValues_.set_t(0.0);
362 nominalValues_.set_x(x_vec_);
363 nominalValues_.set_x_dot(x_dot_vec_);
364 nominalValues_.set_x_dot_dot(x_dot_dot_vec_);
366 isInitialized_ =
true;
369 template <
class Scalar>
378 if (paramList != Teuchos::null) tmpPL = paramList;
380 this->setMyParamList(tmpPL);
382 c_ = get<Scalar>(*pl,
"Damping coeff c");
383 f_ = get<Scalar>(*pl,
"Forcing coeff f");
384 k_ = get<Scalar>(*pl,
"x coeff k");
385 m_ = get<Scalar>(*pl,
"Mass coeff m");
388 "Error: invalid value of Mass coeff m = "
389 << m_ <<
"! Mass coeff m must be > 0.\n");
393 "Error: invalid value of x coeff k = "
394 << k_ <<
"! x coeff k must be >= 0.\n");
396 if ((k_ > 0.0) && (c_ != 0.0)) {
398 true, std::logic_error,
399 "Error: HarmonicOscillator model only supports x coeff k > 0 when "
400 "Damping coeff c = 0. You have "
401 <<
"specified x coeff k = " << k_ <<
" and Damping coeff c = " << c_
406 template <
class Scalar>
414 Teuchos::setDoubleParameter(
"Damping coeff c", 0.0,
415 "Damping coefficient in model", &*pl);
416 Teuchos::setDoubleParameter(
"Forcing coeff f", -1.0,
417 "Forcing coefficient in model", &*pl);
418 Teuchos::setDoubleParameter(
"x coeff k", 0.0,
"x coefficient in model",
420 Teuchos::setDoubleParameter(
"Mass coeff m", 1.0,
421 "Mass coefficient in model", &*pl);
427 #endif // TEMPUS_TEST_HARMONIC_OSCILLATOR_MODEL_IMPL_HPP
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
bool is_null(const boost::shared_ptr< T > &p)
void set_x_dot_dot(const RCP< const VectorBase< Scalar > > &x_dot_dot)
RCP< const VectorBase< Scalar > > get_x_dot() const
void setModelEvalDescription(const std::string &modelEvalDescription)
Teuchos::RCP< Thyra::VectorBase< Scalar > > x_dot_vec_
void set_Np_Ng(int Np, int Ng)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > g_space_
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Evaluation< VectorBase< Scalar > > get_g(int j) const
Evaluation< VectorBase< Scalar > > get_f() const
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getExactSolution(double t) const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs_bar, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs_bar) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
void setSupports(EInArgsMembers arg, bool supports=true)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int j) const
void set_x(const RCP< const VectorBase< Scalar > > &x)
Teuchos::RCP< Teuchos::FancyOStream > out_
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< Thyra::VectorBase< Scalar > > x_dot_dot_vec_
Teuchos_Ordinal subDim() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int l) const
Teuchos::RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Scalar get_W_x_dot_dot_coeff() const
void set_x_dot(const RCP< const VectorBase< Scalar > > &x_dot)
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const ¶mList)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > x_space_
void setSupports(EOutArgsMembers arg, bool supports=true)
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
HarmonicOscillatorModel(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null, const bool use_accel_IC=false)
Teuchos::RCP< Thyra::VectorBase< Scalar > > x_vec_
RCP< const VectorBase< Scalar > > get_x_dot_dot() const
void setupInOutArgs_() const
void setModelEvalDescription(const std::string &modelEvalDescription)
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_x() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const