FEApp_ModelEvaluator.cpp

Go to the documentation of this file.
00001 // $Id: FEApp_ModelEvaluator.cpp,v 1.7 2008/08/01 22:57:12 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/example/FEApp/FEApp_ModelEvaluator.cpp,v $ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #include "FEApp_ModelEvaluator.hpp"
00033 #include "Teuchos_ScalarTraits.hpp"
00034 #include "Teuchos_TestForException.hpp"
00035 
00036 FEApp::ModelEvaluator::ModelEvaluator(
00037   const Teuchos::RCP<FEApp::Application>& app_,
00038   const Teuchos::RCP< Teuchos::Array<std::string> >& free_param_names) 
00039   : app(app_),
00040     param_names(free_param_names)
00041 {
00042   // Initialize Sacado parameter vector
00043   sacado_param_vec = Teuchos::rcp(new Sacado::ScalarParameterVector);
00044   if (param_names != Teuchos::null)
00045     app->getParamLib()->fillVector(*param_names, *sacado_param_vec);
00046 
00047   // Create Epetra map for parameter vector
00048   const Epetra_Comm& comm = app->getMap()->Comm();
00049   epetra_param_map = Teuchos::rcp(new Epetra_LocalMap(sacado_param_vec->size(),
00050                   0, comm));
00051 
00052   // Create Epetra vector for parameters
00053   epetra_param_vec = Teuchos::rcp(new Epetra_Vector(*epetra_param_map));
00054   
00055   // Set parameters
00056   for (unsigned int i=0; i<sacado_param_vec->size(); i++)
00057     (*epetra_param_vec)[i] = (*sacado_param_vec)[i].baseValue;
00058                   
00059 }
00060 
00061 // Overridden from EpetraExt::ModelEvaluator
00062 
00063 Teuchos::RCP<const Epetra_Map>
00064 FEApp::ModelEvaluator::get_x_map() const
00065 {
00066   return app->getMap();
00067 }
00068 
00069 Teuchos::RCP<const Epetra_Map>
00070 FEApp::ModelEvaluator::get_f_map() const
00071 {
00072   return app->getMap();
00073 }
00074 
00075 Teuchos::RCP<const Epetra_Map>
00076 FEApp::ModelEvaluator::get_p_map(int l) const
00077 {
00078   TEST_FOR_EXCEPTION(l != 0, Teuchos::Exceptions::InvalidParameter,
00079          std::endl << 
00080          "Error!  FEApp::ModelEvaluator::get_p_map() only " <<
00081          " supports 1 parameter vector.  Supplied index l = " << 
00082          l << std::endl);
00083 
00084   return epetra_param_map;
00085 }
00086 
00087 Teuchos::RCP<const Teuchos::Array<std::string> >
00088 FEApp::ModelEvaluator::get_p_names(int l) const
00089 {
00090   TEST_FOR_EXCEPTION(l != 0, Teuchos::Exceptions::InvalidParameter,
00091          std::endl << 
00092          "Error!  FEApp::ModelEvaluator::get_p_names() only " <<
00093          " supports 1 parameter vector.  Supplied index l = " << 
00094          l << std::endl);
00095   return param_names;
00096 }
00097 
00098 Teuchos::RCP<const Epetra_Vector>
00099 FEApp::ModelEvaluator::get_x_init() const
00100 {
00101   return app->getInitialSolution();
00102 }
00103 
00104 Teuchos::RCP<const Epetra_Vector>
00105 FEApp::ModelEvaluator::get_p_init(int l) const
00106 {
00107   TEST_FOR_EXCEPTION(l != 0, Teuchos::Exceptions::InvalidParameter,
00108          std::endl << 
00109          "Error!  FEApp::ModelEvaluator::get_p_init() only " <<
00110          " supports 1 parameter vector.  Supplied index l = " << 
00111          l << std::endl);
00112   
00113   return epetra_param_vec;
00114 }
00115 
00116 Teuchos::RCP<Epetra_Operator>
00117 FEApp::ModelEvaluator::create_W() const
00118 {
00119   return app->createW();
00120 }
00121 
00122 EpetraExt::ModelEvaluator::InArgs
00123 FEApp::ModelEvaluator::createInArgs() const
00124 {
00125   InArgsSetup inArgs;
00126   inArgs.setModelEvalDescription(this->description());
00127   inArgs.setSupports(IN_ARG_x,true);
00128   inArgs.set_Np(1); // 1 parameter vector
00129   if (app->isTransient()) {
00130     inArgs.setSupports(IN_ARG_t,true);
00131     inArgs.setSupports(IN_ARG_x_dot,true);
00132     inArgs.setSupports(IN_ARG_alpha,true);
00133     inArgs.setSupports(IN_ARG_beta,true);
00134   }
00135   return inArgs;
00136 }
00137 
00138 EpetraExt::ModelEvaluator::OutArgs
00139 FEApp::ModelEvaluator::createOutArgs() const
00140 {
00141   OutArgsSetup outArgs;
00142   outArgs.setModelEvalDescription(this->description());
00143   outArgs.set_Np_Ng(1, 0); // 1 parameter vector
00144   outArgs.setSupports(OUT_ARG_f,true);
00145   outArgs.setSupports(OUT_ARG_W,true);
00146   outArgs.setSupports(OUT_ARG_DfDp, 0, DerivativeSupport(DERIV_MV_BY_COL));
00147   outArgs.set_W_properties(
00148     DerivativeProperties(
00149       DERIV_LINEARITY_UNKNOWN ,DERIV_RANK_FULL ,true)
00150     );
00151   return outArgs;
00152 }
00153 
00154 void 
00155 FEApp::ModelEvaluator::evalModel(const InArgs& inArgs, 
00156          const OutArgs& outArgs) const
00157 {
00158   //
00159   // Get the input arguments
00160   //
00161   const Epetra_Vector& x = *inArgs.get_x();
00162   const Epetra_Vector *x_dot = NULL;
00163   double alpha = 0.0;
00164   double beta = 1.0;
00165   if (app->isTransient()) {
00166     x_dot = inArgs.get_x_dot().get();
00167     if (x_dot) {
00168       alpha = inArgs.get_alpha();
00169       beta = inArgs.get_beta();
00170     }
00171   }
00172   const Epetra_Vector *p = inArgs.get_p(0).get();
00173   if (p != NULL) {
00174     for (unsigned int i=0; i<sacado_param_vec->size(); i++)
00175       (*sacado_param_vec)[i].baseValue = (*p)[i];
00176   }
00177 
00178   //
00179   // Get the output arguments
00180   //
00181   //Teuchos::RCP<Epetra_Vector> f_out = outArgs.get_f();
00182   EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector> f_out = outArgs.get_f();
00183   Teuchos::RCP<Epetra_Operator> W_out = outArgs.get_W();
00184   Teuchos::RCP<Epetra_MultiVector> dfdp_out;
00185   if (outArgs.Np() > 0)
00186     dfdp_out = outArgs.get_DfDp(0).getMultiVector();
00187   
00188   //
00189   // Compute the functions
00190   //
00191   if(W_out != Teuchos::null) {
00192     if (f_out.getType() == EVAL_TYPE_EXACT ||
00193   f_out.getType() == EVAL_TYPE_APPROX_DERIV)
00194       app->computeGlobalJacobian(alpha, beta, x_dot, x, sacado_param_vec.get(),
00195          f_out.get(), *W_out);
00196     else
00197       app->computeGlobalPreconditioner(alpha, beta, x_dot, x, 
00198                sacado_param_vec.get(), f_out.get(), 
00199                *W_out);
00200   }
00201   else if (dfdp_out != Teuchos::null) {
00202     Teuchos::Array<int> p_indexes = 
00203       outArgs.get_DfDp(0).getDerivativeMultiVector().getParamIndexes();
00204     unsigned int n_params = p_indexes.size();
00205     Teuchos::Array<std::string> p_names(n_params);
00206     for (unsigned int i=0; i<n_params; i++)
00207       p_names[i] = (*param_names)[p_indexes[i]];
00208     Sacado::ScalarParameterVector p_vec;
00209     app->getParamLib()->fillVector(p_names, p_vec);
00210     for (unsigned int i=0; i<p_vec.size(); i++)
00211       p_vec[i].baseValue = (*p)[p_indexes[i]];
00212   
00213     app->computeGlobalTangent(0.0, 0.0, false, x_dot, x, &p_vec,
00214             NULL, NULL, f_out.get(), NULL, dfdp_out.get());
00215   }
00216   else if(f_out != Teuchos::null ) {
00217     app->computeGlobalResidual(x_dot, x, sacado_param_vec.get(), *f_out);
00218   }
00219 }

Generated on Wed May 12 21:59:03 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7