Thyra Version of the Day
Thyra_EpetraModelEvaluator.hpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef THYRA_EPETRA_MODEL_EVALUATOR_HPP
00043 #define THYRA_EPETRA_MODEL_EVALUATOR_HPP
00044 
00045 #include "Thyra_ModelEvaluatorDefaultBase.hpp"
00046 #include "Thyra_EpetraThyraWrappers.hpp"
00047 #include "Thyra_LinearOpWithSolveFactoryBase.hpp"
00048 #include "EpetraExt_ModelEvaluator.h"
00049 #include "Epetra_Map.h"
00050 #include "Teuchos_Array.hpp"
00051 
00052 
00053 namespace Thyra {
00054 
00055 
00175 class EpetraModelEvaluator
00176   : public ModelEvaluatorDefaultBase<double>,
00177     virtual public Teuchos::ParameterListAcceptor
00178 {
00179 public:
00180 
00183 
00185   EpetraModelEvaluator();
00186 
00188   EpetraModelEvaluator(
00189     const RCP<const EpetraExt::ModelEvaluator> &epetraModel,
00190     const RCP<LinearOpWithSolveFactoryBase<double> > &W_factory
00191     );
00192 
00194   void initialize(
00195     const RCP<const EpetraExt::ModelEvaluator> &epetraModel,
00196     const RCP<LinearOpWithSolveFactoryBase<double> > &W_factory
00197     );
00198 
00200   RCP<const EpetraExt::ModelEvaluator> getEpetraModel() const;
00201 
00207   void setNominalValues( const ModelEvaluatorBase::InArgs<double>& nominalValues );
00208   
00216   void setStateVariableScalingVec(
00217     const RCP<const Epetra_Vector> &stateVariableScalingVec
00218     );
00219   
00222   RCP<const Epetra_Vector>
00223   getStateVariableInvScalingVec() const;
00224   
00227   RCP<const Epetra_Vector>
00228   getStateVariableScalingVec() const;
00229   
00232   void setStateFunctionScalingVec(
00233     const RCP<const Epetra_Vector> &stateFunctionScalingVec
00234     );
00235   
00238   RCP<const Epetra_Vector>
00239   getStateFunctionScalingVec() const;
00240 
00242   void uninitialize(
00243     RCP<const EpetraExt::ModelEvaluator> *epetraModel = NULL,
00244     RCP<LinearOpWithSolveFactoryBase<double> > *W_factory = NULL
00245     );
00246   
00248   const ModelEvaluatorBase::InArgs<double>& getFinalPoint() const;
00249 
00251   bool finalPointWasSolved() const;
00252 
00254 
00257 
00259   std::string description() const;
00260 
00262 
00265 
00267   void setParameterList(RCP<Teuchos::ParameterList> const& paramList);
00269   RCP<Teuchos::ParameterList> getNonconstParameterList();
00271   RCP<Teuchos::ParameterList> unsetParameterList();
00273   RCP<const Teuchos::ParameterList> getParameterList() const;
00275   RCP<const Teuchos::ParameterList> getValidParameters() const;
00276 
00278 
00281 
00283   int Np() const;
00285   int Ng() const;
00287   RCP<const VectorSpaceBase<double> > get_x_space() const;
00289   RCP<const VectorSpaceBase<double> > get_f_space() const;
00291   RCP<const VectorSpaceBase<double> > get_p_space(int l) const;
00293   RCP<const Teuchos::Array<std::string> > get_p_names(int l) const;
00295   RCP<const VectorSpaceBase<double> > get_g_space(int j) const;
00297   ModelEvaluatorBase::InArgs<double> getNominalValues() const;
00299   ModelEvaluatorBase::InArgs<double> getLowerBounds() const;
00301   ModelEvaluatorBase::InArgs<double> getUpperBounds() const;
00303   RCP<LinearOpBase<double> > create_W_op() const;
00305   RCP<const LinearOpWithSolveFactoryBase<double> > get_W_factory() const;
00307   ModelEvaluatorBase::InArgs<double> createInArgs() const;
00309   void reportFinalPoint(
00310     const ModelEvaluatorBase::InArgs<double>      &finalPoint
00311     ,const bool                                   wasSolved
00312     );
00313 
00315 
00316   // Made public to simplify implementation but this is harmless to be public.
00317   // Clients should not deal with this type.
00318   enum EStateFunctionScaling { STATE_FUNC_SCALING_NONE, STATE_FUNC_SCALING_ROW_SUM };
00319 
00320 private:
00321 
00324 
00326   RCP<LinearOpBase<double> > create_DfDp_op_impl(int l) const;
00328   RCP<LinearOpBase<double> > create_DgDx_dot_op_impl(int j) const;
00330   RCP<LinearOpBase<double> > create_DgDx_op_impl(int j) const;
00332   RCP<LinearOpBase<double> > create_DgDp_op_impl(int j, int l) const;
00334   ModelEvaluatorBase::OutArgs<double> createOutArgsImpl() const;
00336   void evalModelImpl(
00337     const ModelEvaluatorBase::InArgs<double> &inArgs,
00338     const ModelEvaluatorBase::OutArgs<double> &outArgs
00339     ) const;
00340 
00342 
00343 private:
00344 
00345   // ////////////////////
00346   // Private types
00347 
00348   typedef Teuchos::Array<RCP<const Epetra_Map> > p_map_t;
00349   typedef Teuchos::Array<RCP<const Epetra_Map> > g_map_t;
00350   typedef std::vector<bool> p_map_is_local_t;
00351   typedef std::vector<bool> g_map_is_local_t;
00352 
00353   typedef Teuchos::Array<RCP<const VectorSpaceBase<double> > >
00354   p_space_t;
00355   typedef Teuchos::Array<RCP<const VectorSpaceBase<double> > >
00356   g_space_t;
00357 
00358   // /////////////////////
00359   // Private data members
00360 
00361   RCP<const EpetraExt::ModelEvaluator> epetraModel_;
00362 
00363   RCP<Teuchos::ParameterList> paramList_;
00364 
00365   RCP<LinearOpWithSolveFactoryBase<double> > W_factory_;
00366 
00367   RCP<const Epetra_Map> x_map_;
00368   p_map_t p_map_;
00369   g_map_t g_map_;
00370   p_map_is_local_t p_map_is_local_;
00371   p_map_is_local_t g_map_is_local_;
00372   RCP<const Epetra_Map> f_map_;
00373 
00374   RCP<const VectorSpaceBase<double> > x_space_;
00375   p_space_t p_space_;
00376   RCP<const VectorSpaceBase<double> > f_space_;
00377   g_space_t g_space_;
00378 
00379   mutable ModelEvaluatorBase::InArgs<double> nominalValues_;
00380   mutable ModelEvaluatorBase::InArgs<double> lowerBounds_;
00381   mutable ModelEvaluatorBase::InArgs<double> upperBounds_;
00382   mutable bool nominalValuesAndBoundsAreUpdated_;
00383 
00384   ModelEvaluatorBase::InArgs<double> finalPoint_;
00385 
00386   EStateFunctionScaling stateFunctionScaling_;
00387   mutable RCP<const Epetra_Vector> stateFunctionScalingVec_;
00388 
00389   RCP<const Epetra_Vector> stateVariableScalingVec_; // S_x
00390   mutable RCP<const Epetra_Vector> invStateVariableScalingVec_; // inv(S_x)
00391   mutable EpetraExt::ModelEvaluator::InArgs epetraInArgsScaling_;
00392   mutable EpetraExt::ModelEvaluator::OutArgs epetraOutArgsScaling_;
00393   
00394   mutable RCP<Epetra_Vector> x_unscaled_;
00395   mutable RCP<Epetra_Vector> x_dot_unscaled_;
00396 
00397   mutable ModelEvaluatorBase::InArgs<double> prototypeInArgs_;
00398   mutable ModelEvaluatorBase::OutArgs<double> prototypeOutArgs_;
00399   mutable bool currentInArgsOutArgs_;
00400 
00401   bool finalPointWasSolved_;
00402 
00403   // //////////////////////////
00404   // Private member functions
00405 
00407   void convertInArgsFromEpetraToThyra(
00408     const EpetraExt::ModelEvaluator::InArgs &epetraInArgs,
00409     ModelEvaluatorBase::InArgs<double> *inArgs
00410     ) const;
00411 
00413   void convertInArgsFromThyraToEpetra(
00414     const ModelEvaluatorBase::InArgs<double> &inArgs,
00415     EpetraExt::ModelEvaluator::InArgs *epetraInArgs
00416     ) const;
00417 
00419   void convertOutArgsFromThyraToEpetra(
00420     // Thyra form of the outArgs
00421     const ModelEvaluatorBase::OutArgs<double> &outArgs,
00422     // Epetra form of the unscaled output arguments 
00423     EpetraExt::ModelEvaluator::OutArgs *epetraUnscaledOutArgs,
00424     // The passed-in form of W
00425     RCP<LinearOpBase<double> > *W_op,
00426     RCP<EpetraLinearOp> *efwdW,
00427     // The actual Epetra object passed to the underylying EpetraExt::ModelEvaluator
00428     RCP<Epetra_Operator> *eW
00429     ) const;
00430 
00432   void preEvalScalingSetup(
00433     EpetraExt::ModelEvaluator::InArgs *epetraInArgs,
00434     EpetraExt::ModelEvaluator::OutArgs *epetraUnscaledOutArgs,
00435     const RCP<Teuchos::FancyOStream> &out,
00436     const Teuchos::EVerbosityLevel verbLevel
00437     ) const;
00438 
00440   void postEvalScalingSetup(
00441     const EpetraExt::ModelEvaluator::OutArgs &epetraUnscaledOutArgs,
00442     const RCP<Teuchos::FancyOStream> &out,
00443     const Teuchos::EVerbosityLevel verbLevel
00444     ) const;
00445 
00447   void finishConvertingOutArgsFromEpetraToThyra(
00448     const EpetraExt::ModelEvaluator::OutArgs &epetraOutArgs,
00449     RCP<LinearOpBase<double> > &W_op,
00450     RCP<EpetraLinearOp> &efwdW,
00451     RCP<Epetra_Operator> &eW,
00452     const ModelEvaluatorBase::OutArgs<double> &outArgs // Output!
00453     ) const;
00454   // 2007/08/03: rabartl: Above, I pass many of the RCP objects by non-const
00455   // reference since I don't want the compiler to perform any implicit
00456   // conversions on this RCP objects.
00457 
00459   void updateNominalValuesAndBounds() const;
00460 
00462   void updateInArgsOutArgs() const;
00463 
00465   RCP<EpetraLinearOp> create_epetra_W_op() const;
00466   
00467 };
00468 
00469 
00470 //
00471 // Utility functions
00472 //
00473 
00474 
00478 RCP<EpetraModelEvaluator>
00479 epetraModelEvaluator(
00480   const RCP<const EpetraExt::ModelEvaluator> &epetraModel,
00481   const RCP<LinearOpWithSolveFactoryBase<double> > &W_factory
00482   );
00483 
00484 
00488 ModelEvaluatorBase::EDerivativeMultiVectorOrientation
00489 convert( const EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation &mvOrientation );
00490 
00491 
00495 EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation
00496 convert( const ModelEvaluatorBase::EDerivativeMultiVectorOrientation &mvOrientation );
00497 
00498 
00502 ModelEvaluatorBase::DerivativeProperties
00503 convert( const EpetraExt::ModelEvaluator::DerivativeProperties &derivativeProperties );
00504 
00505 
00509 ModelEvaluatorBase::DerivativeSupport
00510 convert( const EpetraExt::ModelEvaluator::DerivativeSupport &derivativeSupport );
00511 
00512 
00516 EpetraExt::ModelEvaluator::Derivative
00517 convert(
00518   const ModelEvaluatorBase::Derivative<double> &derivative,
00519   const RCP<const Epetra_Map> &fnc_map,
00520   const RCP<const Epetra_Map> &var_map
00521   );
00522 
00523 
00524 } // namespace Thyra
00525 
00526 
00527 #endif // THYRA_EPETRA_MODEL_EVALUATOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines