EpetraExt_ModelEvaluator.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "EpetraExt_ModelEvaluator.h"
00030 
00031 namespace {
00032 
00033 Teuchos::RefCountPtr<Epetra_Operator>
00034 getLinearOp(
00035   const std::string                                                    &modelEvalDescription
00036   ,const EpetraExt::ModelEvaluator::Derivative                         &deriv
00037   ,const std::string                                                   &derivName
00038   )
00039 {
00040   TEST_FOR_EXCEPTION(
00041     deriv.getDerivativeMultiVector().getMultiVector().get() != NULL, std::logic_error
00042     ,"For model \'" << modelEvalDescription << "\' the derivative \'"
00043     << derivName << "\' is of type Epetra_MultiVector and not of type Epetra_Operator!"
00044     );
00045   return deriv.getLinearOp();
00046 }
00047 
00048 Teuchos::RefCountPtr<Epetra_MultiVector>
00049 getMultiVector(
00050   const std::string                                                    &modelEvalDescription
00051   ,const EpetraExt::ModelEvaluator::Derivative                         &deriv
00052   ,const std::string                                                   &derivName
00053   ,const EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation  mvOrientation
00054   )
00055 {
00056   TEST_FOR_EXCEPTION(
00057     deriv.getLinearOp().get() != NULL, std::logic_error
00058     ,"For model \'" << modelEvalDescription << "\' the derivative \'"
00059     << derivName << "\' is of type Epetra_Operator and not of type Epetra_MultiVector!"
00060     );
00061   Teuchos::RefCountPtr<Epetra_MultiVector>
00062     mv = deriv.getDerivativeMultiVector().getMultiVector();
00063   if(mv.get()) {
00064     TEST_FOR_EXCEPTION(
00065       deriv.getDerivativeMultiVector().getOrientation()!=mvOrientation, std::logic_error
00066       ,"For model \'" << modelEvalDescription << "\' the derivative \'"
00067       << derivName << "\' if not the orientation \'" << toString(mvOrientation)
00068       << "\'"
00069       );
00070   }
00071   return mv;
00072 }
00073 
00074 } // namespace
00075 
00076 namespace EpetraExt {
00077 
00078 //
00079 // ModelEvaluator::InArgs
00080 //
00081 
00082 ModelEvaluator::InArgs::InArgs()
00083 {
00084   std::fill_n(&supports_[0],NUM_E_IN_ARGS_MEMBERS,false);
00085   t_     = 0.0;
00086   alpha_ = 0.0;
00087   beta_  = 0.0;
00088 }
00089 
00090 bool ModelEvaluator::InArgs::supports(EInArgsMembers arg) const
00091 {
00092   TEST_FOR_EXCEPTION(
00093     int(arg)>=NUM_E_IN_ARGS_MEMBERS || int(arg) < 0,std::logic_error
00094     ,"model = \'"<<modelEvalDescription_<<"\': Error, arg="<<arg<<" is invalid!"
00095     );
00096   return supports_[arg];
00097 }
00098 
00099 void ModelEvaluator::InArgs::_setSupports( EInArgsMembers arg, bool supports )
00100 {
00101   TEST_FOR_EXCEPTION(
00102     int(arg)>=NUM_E_IN_ARGS_MEMBERS || int(arg) < 0,std::logic_error
00103     ,"model = \'"<<modelEvalDescription_<<"\':Error, arg="<<arg<<" is invalid!"
00104     );
00105   supports_[arg] = supports;
00106 }
00107 
00108 void ModelEvaluator::InArgs::assert_supports(EInArgsMembers arg) const
00109 {
00110   TEST_FOR_EXCEPTION(
00111     !supports_[arg], std::logic_error
00112     ,"EpetraExt::ModelEvaluator::InArgs::assert_supports(arg): model = \'"<<modelEvalDescription_<<"\': Error, "
00113     "The argument arg = " << arg << " is not supported!"
00114     );
00115 }
00116 
00117 void ModelEvaluator::InArgs::assert_l(int l) const
00118 {
00119   TEST_FOR_EXCEPTION(
00120     !( 0 <= l && l < Np() ), std::logic_error
00121     ,"EpetraExt::ModelEvaluator::InArgs::assert_l(l): model = \'"<<modelEvalDescription_<<"\': Error, "
00122     "The parameter l = " << l << " is not in the range [0,"<<Np()-1<<"]!"
00123     );
00124 }
00125 
00126 //
00127 // ModelEvaluator::OutArgs
00128 //
00129 
00130 ModelEvaluator::OutArgs::OutArgs()
00131 {
00132   std::fill_n(&supports_[0],NUM_E_OUT_ARGS_MEMBERS,false);
00133 }
00134 
00135 bool ModelEvaluator::OutArgs::supports(EOutArgsMembers arg) const
00136 {
00137   TEST_FOR_EXCEPTION(
00138     int(arg)>=NUM_E_OUT_ARGS_MEMBERS || int(arg) < 0,std::logic_error
00139     ,"model = \'"<<modelEvalDescription_<<"\': Error, arg="<<arg<<" is invalid!"
00140     );
00141   return supports_[arg];
00142 }
00143 
00144 const ModelEvaluator::DerivativeSupport&
00145 ModelEvaluator::OutArgs::supports(EOutArgsDfDp arg, int l) const
00146 {
00147   assert_l(l);
00148   return supports_DfDp_[l];
00149 }
00150 
00151 const ModelEvaluator::DerivativeSupport&
00152 ModelEvaluator::OutArgs::supports(EOutArgsDgDx arg, int j) const
00153 {
00154   assert_j(j);
00155   return supports_DgDx_[j];
00156 }
00157 
00158 const ModelEvaluator::DerivativeSupport&
00159 ModelEvaluator::OutArgs::supports(EOutArgsDgDp arg, int j, int l) const
00160 {
00161   assert_j(j);
00162   assert_l(l);
00163   return supports_DgDp_[ j*Np() + l ];
00164 }
00165 
00166 void ModelEvaluator::OutArgs::_setModelEvalDescription( const std::string &modelEvalDescription )
00167 {
00168   modelEvalDescription_ = modelEvalDescription;
00169 }
00170 
00171 void ModelEvaluator::OutArgs::_set_Np_Ng(int Np, int Ng)
00172 {
00173   if(Np) {
00174     supports_DfDp_.resize(Np);
00175     DfDp_.resize(Np);                 std::fill_n(DfDp_.begin(),Np,Derivative());
00176     DfDp_properties_.resize(Np);      std::fill_n(DfDp_properties_.begin(),Np,DerivativeProperties());
00177   }
00178   if(Ng) {
00179     g_.resize(Ng);
00180     supports_DgDx_.resize(Ng);
00181     DgDx_.resize(Ng);                 std::fill_n(DgDx_.begin(),Ng,Derivative());
00182     DgDx_properties_.resize(Ng);      std::fill_n(DgDx_properties_.begin(),Ng,DerivativeProperties());
00183   }
00184   if(Np && Ng) {
00185     const int NpNg = Np*Ng;
00186     supports_DgDp_.resize(NpNg);
00187     DgDp_.resize(NpNg);                 std::fill_n(DgDp_.begin(),NpNg,Derivative());
00188     DgDp_properties_.resize(NpNg);      std::fill_n(DgDp_properties_.begin(),NpNg,DerivativeProperties());
00189   }
00190 }
00191 
00192 void ModelEvaluator::OutArgs::_setSupports( EOutArgsMembers arg, bool supports )
00193 {
00194   TEST_FOR_EXCEPTION(
00195     int(arg)>=NUM_E_OUT_ARGS_MEMBERS || int(arg) < 0,std::logic_error
00196     ,"model = \'"<<modelEvalDescription_<<"\': Error, arg="<<arg<<" is invalid!"
00197     );
00198   supports_[arg] = supports;
00199 }
00200 
00201 void ModelEvaluator::OutArgs::_setSupports( EOutArgsDfDp arg, int l, const DerivativeSupport& supports )
00202 {
00203   assert_l(l);
00204   supports_DfDp_[l] = supports;
00205 }
00206 
00207 void ModelEvaluator::OutArgs::_setSupports( EOutArgsDgDx arg, int j, const DerivativeSupport& supports )
00208 {
00209   assert_j(j);
00210   supports_DgDx_[j] = supports;
00211 }
00212 
00213 void ModelEvaluator::OutArgs::_setSupports( EOutArgsDgDp arg, int j, int l, const DerivativeSupport& supports )
00214 {
00215   assert_j(j);
00216   assert_l(l);
00217   supports_DgDp_[ j*Np() + l ] = supports;
00218 }
00219 
00220 void ModelEvaluator::OutArgs::_set_W_properties( const DerivativeProperties &W_properties )
00221 {
00222   W_properties_ = W_properties;
00223 }
00224 
00225 void ModelEvaluator::OutArgs::_set_DfDp_properties( int l, const DerivativeProperties &properties )
00226 {
00227   assert_supports(OUT_ARG_DfDp,l);
00228   DfDp_properties_[l] = properties;
00229 }
00230 
00231 void ModelEvaluator::OutArgs::_set_DgDx_properties( int j, const DerivativeProperties &properties )
00232 {
00233   assert_supports(OUT_ARG_DgDx,j);
00234   DgDx_properties_[j] = properties;
00235 }
00236 
00237 void ModelEvaluator::OutArgs::_set_DgDp_properties( int j, int l, const DerivativeProperties &properties )
00238 {
00239   assert_supports(OUT_ARG_DgDp,j,l);
00240   DgDp_properties_[ j*Np() + l ] = properties;
00241 }
00242 
00243 void ModelEvaluator::OutArgs::assert_supports(EOutArgsMembers arg) const
00244 {
00245   TEST_FOR_EXCEPTION(
00246     !supports_[arg], std::logic_error
00247     ,"EpetraExt::ModelEvaluator::OutArgs::assert_supports(arg): "
00248     "model = \'"<<modelEvalDescription_<<"\': Error, "
00249     "The argument arg = " << arg << " is not supported!"
00250     );
00251 }
00252 
00253 void ModelEvaluator::OutArgs::assert_supports(EOutArgsDfDp arg, int l) const
00254 {
00255   assert_l(l);
00256   TEST_FOR_EXCEPTION(
00257     supports_DfDp_[l].none(), std::logic_error
00258     ,"Thyra::ModelEvaluator::OutArgs::assert_supports(OUT_ARG_DfDp,l): "
00259     "model = \'"<<modelEvalDescription_<<"\': Error,"
00260     "The argument DfDp(l) with index l = " << l << " is not supported!"
00261     );
00262 }
00263 
00264 void ModelEvaluator::OutArgs::assert_supports(EOutArgsDgDx arg, int j) const
00265 {
00266   assert_j(j);
00267   TEST_FOR_EXCEPTION(
00268     supports_DgDx_[j].none(), std::logic_error
00269     ,"Thyra::ModelEvaluator::OutArgs::assert_supports(OUT_ARG_DgDx,j): "
00270     "model = \'"<<modelEvalDescription_<<"\': Error,"
00271     "The argument DgDx(j) with index j = " << j << " is not supported!"
00272     );
00273 }
00274 
00275 void ModelEvaluator::OutArgs::assert_supports(EOutArgsDgDp arg, int j, int l) const
00276 {
00277   assert_j(j);
00278   TEST_FOR_EXCEPTION(
00279     supports_DgDx_[ j*Np() + l ].none(), std::logic_error
00280     ,"Thyra::ModelEvaluator::OutArgs::assert_supports(OUT_ARG_DgDp,j,l): "
00281     "model = \'"<<modelEvalDescription_<<"\': Error,"
00282     "The argument DgDp(j,l) with indexes j = " << j << " and l = " << l << " is not supported!"
00283     );
00284 }
00285 
00286 void ModelEvaluator::OutArgs::assert_l(int l) const
00287 {
00288   TEST_FOR_EXCEPTION(
00289     Np()==0, std::logic_error
00290     ,"EpetraExt::ModelEvaluator::OutArgs::assert_l(l): model = \'"<<modelEvalDescription_<<"\':  Error, "
00291     "no auxiliary parameters subvectors p(l) are supported!!"
00292     );
00293   TEST_FOR_EXCEPTION(
00294     !( 0 <= l && l < Np() ), std::logic_error
00295     ,"Thyra::ModelEvaluator::OutArgs::assert_l(l): "
00296     "model = \'"<<modelEvalDescription_<<"\': Error, "
00297     "The parameter subvector p(l) index l = " << l << " is not in the range [0,"<<Np()-1<<"]!"
00298     );
00299 }
00300 
00301 void ModelEvaluator::OutArgs::assert_j(int j) const
00302 {
00303   TEST_FOR_EXCEPTION(
00304     Ng()==0, std::logic_error
00305     ,"EpetraExt::ModelEvaluator::OutArgs::assert_j(j): model = \'"<<modelEvalDescription_<<"\':  Error, "
00306     "no auxiliary functions g(j) are supported!!"
00307     );
00308   TEST_FOR_EXCEPTION(
00309     !( 0 <= j && j < Ng() ), std::logic_error
00310     ,"EpetraExt::ModelEvaluator::OutArgs::assert_j(j): model = \'"<<modelEvalDescription_<<"\':  Error, "
00311     "The auxiliary function g(j) index j = " << j << " is not in the range [0,"<<Ng()-1<<"]!"
00312     );
00313 }
00314 
00315 //
00316 // ModelEvaluator
00317 //
00318 
00319 // Destructor
00320 
00321 ModelEvaluator::~ModelEvaluator()
00322 {}
00323 
00324 // Vector maps
00325 
00326 Teuchos::RefCountPtr<const Epetra_Map>
00327 ModelEvaluator::get_p_map(int l) const
00328 { return Teuchos::null; }
00329 
00330 Teuchos::RefCountPtr<const Epetra_Map>
00331 ModelEvaluator::get_g_map(int j) const
00332 { return Teuchos::null; }
00333 
00334 // Initial guesses for variables/parameters
00335 
00336 Teuchos::RefCountPtr<const Epetra_Vector>
00337 ModelEvaluator::get_x_init() const
00338 { return Teuchos::null; }
00339 
00340 Teuchos::RefCountPtr<const Epetra_Vector>
00341 ModelEvaluator::get_x_dot_init() const
00342 { return Teuchos::null; }
00343 
00344 Teuchos::RefCountPtr<const Epetra_Vector>
00345 ModelEvaluator::get_p_init(int l) const
00346 { return Teuchos::null; }
00347 
00348 double ModelEvaluator::get_t_init() const
00349 { return 0.0; }
00350 
00351 // Bounds for variables/parameters
00352 
00353 Teuchos::RefCountPtr<const Epetra_Vector>
00354 ModelEvaluator::get_x_lower_bounds() const
00355 { return Teuchos::null; }
00356 
00357 Teuchos::RefCountPtr<const Epetra_Vector>
00358 ModelEvaluator::get_x_upper_bounds() const
00359 { return Teuchos::null; }
00360 
00361 Teuchos::RefCountPtr<const Epetra_Vector>
00362 ModelEvaluator::get_p_lower_bounds(int l) const
00363 { return Teuchos::null; }
00364 
00365 Teuchos::RefCountPtr<const Epetra_Vector>
00366 ModelEvaluator::get_p_upper_bounds(int l) const
00367 { return Teuchos::null; }
00368 
00369 double ModelEvaluator::get_t_lower_bound() const
00370 { return 0.0; }
00371 
00372 double ModelEvaluator::get_t_upper_bound() const
00373 { return 0.0; }
00374 
00375 // Factory functions for creating derivative objects
00376 
00377 Teuchos::RefCountPtr<Epetra_Operator>
00378 ModelEvaluator::create_W() const
00379 { return Teuchos::null; }
00380 
00381 Teuchos::RefCountPtr<Epetra_Operator>
00382 ModelEvaluator::create_DfDp_op(int l) const
00383 { return Teuchos::null; }
00384 
00385 Teuchos::RefCountPtr<Epetra_Operator>
00386 ModelEvaluator::create_DgDx_op(int j) const
00387 { return Teuchos::null; }
00388 
00389 Teuchos::RefCountPtr<Epetra_Operator>
00390 ModelEvaluator::create_DgDp_op( int j, int l ) const
00391 { return Teuchos::null; }
00392 
00393 } // namespace EpetraExt
00394 
00395 //
00396 // Helper functions
00397 //
00398 
00399 std::string EpetraExt::toString( ModelEvaluator::EDerivativeMultiVectorOrientation orientation )
00400 {
00401   switch(orientation) {
00402     case ModelEvaluator::DERIV_MV_BY_COL:
00403       return "DERIV_MV_BY_COL";
00404     case ModelEvaluator::DERIV_TRANS_MV_BY_ROW:
00405       return "DERIV_TRANS_MV_BY_ROW";
00406     default:
00407       TEST_FOR_EXCEPT(true);
00408   }
00409   return ""; // Should never be called
00410 }
00411 
00412 Teuchos::RefCountPtr<Epetra_Operator>
00413 EpetraExt::get_DfDp_op(
00414   const int                                                            l
00415   ,const ModelEvaluator::OutArgs                                       &outArgs
00416   )
00417 {
00418   std::ostringstream derivName; derivName << "DfDp("<<l<<")";
00419   return getLinearOp(
00420     outArgs.modelEvalDescription()
00421     ,outArgs.get_DfDp(l)
00422     ,derivName.str()
00423     );
00424 }
00425 
00426 Teuchos::RefCountPtr<Epetra_MultiVector>
00427 EpetraExt::get_DfDp_mv(
00428   const int                                                            l
00429   ,const ModelEvaluator::OutArgs                                       &outArgs
00430   )
00431 {
00432   std::ostringstream derivName; derivName << "DfDp("<<l<<")";
00433   return getMultiVector(
00434     outArgs.modelEvalDescription()
00435     ,outArgs.get_DfDp(l)
00436     ,derivName.str()
00437     ,ModelEvaluator::DERIV_MV_BY_COL
00438     );
00439 }
00440 
00441 Teuchos::RefCountPtr<Epetra_MultiVector>
00442 EpetraExt::get_DgDx_mv(
00443   const int                                                            j
00444   ,const ModelEvaluator::OutArgs                                       &outArgs
00445   ,const EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation  mvOrientation
00446   )
00447 {
00448   std::ostringstream derivName; derivName << "DgDx("<<j<<")";
00449   return getMultiVector(
00450     outArgs.modelEvalDescription()
00451     ,outArgs.get_DgDx(j)
00452     ,derivName.str()
00453     ,mvOrientation
00454     );
00455 }
00456 
00457 Teuchos::RefCountPtr<Epetra_MultiVector>
00458 EpetraExt::get_DgDp_mv(
00459   const int                                                            j
00460   ,const int                                                           l
00461   ,const ModelEvaluator::OutArgs                                       &outArgs
00462   ,const EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation  mvOrientation
00463   )
00464 {
00465   std::ostringstream derivName; derivName << "DgDp("<<j<<","<<l<<")";
00466   return getMultiVector(
00467     outArgs.modelEvalDescription()
00468     ,outArgs.get_DgDp(j,l)
00469     ,derivName.str()
00470     ,mvOrientation
00471     );
00472 }

Generated on Thu Sep 18 12:31:43 2008 for EpetraExt by doxygen 1.3.9.1