Rythmos - Transient Integration for Differential Equations Version of the Day
Rythmos_ForwardResponseSensitivityComputerObserver.hpp
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //                     Rythmos Package
00005 //                 Copyright (2006) 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 Todd S. Coffey (tscoffe@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 #ifndef RYTHMOS_FORWARD_RESPONSE_SENSITIVITY_COMPUTER_OBSERVER_HPP
00030 #define RYTHMOS_FORWARD_RESPONSE_SENSITIVITY_COMPUTER_OBSERVER_HPP
00031 
00032 
00033 #include "Rythmos_IntegrationObserverBase.hpp"
00034 #include "Rythmos_ForwardResponseSensitivityComputer.hpp"
00035 #include "Rythmos_ResponseAndFwdSensPoint.hpp"
00036 #include "Rythmos_extractStateAndSens.hpp"
00037 
00038 
00039 namespace Rythmos {
00040 
00041 
00047 template<class Scalar>
00048 class ForwardResponseSensitivityComputerObserver
00049   : public IntegrationObserverBase<Scalar>
00050 {
00051 public:
00052 
00055 
00057   ForwardResponseSensitivityComputerObserver();
00058 
00060   void initialize(
00061     const RCP<const Thyra::ModelEvaluator<Scalar> > &responseFunc,
00062     const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
00063     const int p_index,
00064     const int g_index
00065     );
00066 
00068   const Array<ResponseAndFwdSensPoint<Scalar> >& responseAndFwdSensPoints() const;
00069 
00071 
00074 
00076   virtual RCP<IntegrationObserverBase<Scalar> >
00077   cloneIntegrationObserver() const;
00078 
00080   virtual void resetIntegrationObserver(
00081     const TimeRange<Scalar> &integrationTimeDomain
00082     );
00083 
00085   virtual void observeCompletedTimeStep(
00086     const StepperBase<Scalar> &stepper,
00087     const StepControlInfo<Scalar> &stepCtrlInfo,
00088     const int timeStepIter
00089     );
00090 
00092 
00093 private:
00094 
00095   ForwardResponseSensitivityComputer<Scalar> forwardResponseSensitivityComputer_; 
00096   Array<ResponseAndFwdSensPoint<Scalar> > responseAndFwdSensPoints_;
00097 
00098   RCP<Thyra::VectorBase<Scalar> > g_hat_;
00099   RCP<Thyra::MultiVectorBase<Scalar> > D_g_hat_D_p_;
00100 
00101 };
00102 
00103 
00108 template<class Scalar>
00109 RCP<ForwardResponseSensitivityComputerObserver<Scalar> >
00110 forwardResponseSensitivityComputerObserver()
00111 {
00112   RCP<ForwardResponseSensitivityComputerObserver<Scalar> >
00113     frsco(new ForwardResponseSensitivityComputerObserver<Scalar>());
00114   return frsco;
00115 }
00116 
00117 
00122 template<class Scalar>
00123 RCP<ForwardResponseSensitivityComputerObserver<Scalar> >
00124 forwardResponseSensitivityComputerObserver(
00125   const RCP<const Thyra::ModelEvaluator<Scalar> > &responseFunc,
00126   const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
00127   const int p_index,
00128   const int g_index
00129   )
00130 {
00131   RCP<ForwardResponseSensitivityComputerObserver<Scalar> >
00132     frsco = Rythmos::forwardResponseSensitivityComputerObserver<Scalar>();
00133   frsco->initialize(responseFunc,basePoint,p_index,g_index);
00134   return frsco;
00135 }
00136 
00137 
00138 //
00139 // Implementations
00140 //
00141 
00142 
00143 // Constructors/Initializers/Accessors
00144 
00145 
00146 template<class Scalar>
00147 ForwardResponseSensitivityComputerObserver<Scalar>::ForwardResponseSensitivityComputerObserver()
00148 {}
00149 
00150 
00151 template<class Scalar>
00152 void ForwardResponseSensitivityComputerObserver<Scalar>::initialize(
00153   const RCP<const Thyra::ModelEvaluator<Scalar> > &responseFunc,
00154   const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
00155   const int p_index,
00156   const int g_index
00157   )
00158 {
00159   forwardResponseSensitivityComputer_.setResponseFunction(
00160     responseFunc, basePoint, p_index, g_index );
00161   g_hat_ = forwardResponseSensitivityComputer_.create_g_hat();
00162   D_g_hat_D_p_ = forwardResponseSensitivityComputer_.create_D_g_hat_D_p();
00163 }
00164 
00165 
00166 template<class Scalar>
00167 const Array<ResponseAndFwdSensPoint<Scalar> >&
00168 ForwardResponseSensitivityComputerObserver<Scalar>::responseAndFwdSensPoints() const
00169 {
00170   return responseAndFwdSensPoints_;
00171 }
00172 
00173 
00174 // Overridden from IntegrationObserverBase
00175 
00176 
00177 template<class Scalar>
00178 RCP<IntegrationObserverBase<Scalar> >
00179 ForwardResponseSensitivityComputerObserver<Scalar>::cloneIntegrationObserver() const
00180 {
00181   TEUCHOS_TEST_FOR_EXCEPT(true);
00182   return Teuchos::null;                       
00183 }
00184 
00185 
00186 template<class Scalar>
00187 void ForwardResponseSensitivityComputerObserver<Scalar>::resetIntegrationObserver(
00188   const TimeRange<Scalar> &integrationTimeDomain
00189   )
00190 {
00191   responseAndFwdSensPoints_.clear();
00192 }
00193 
00194 
00195 template<class Scalar>
00196 void ForwardResponseSensitivityComputerObserver<Scalar>::observeCompletedTimeStep(
00197   const StepperBase<Scalar> &stepper,
00198   const StepControlInfo<Scalar> &stepCtrlInfo,
00199   const int timeStepIter
00200   )
00201 {
00202 
00203   using Teuchos::OSTab;
00204   using Teuchos::includesVerbLevel;
00205 
00206   // Setup the output info
00207 
00208   const RCP<FancyOStream> out = this->getOStream();
00209   const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
00210 
00211   const bool trace = 
00212     ( !is_null(out) && includesVerbLevel(verbLevel,Teuchos::VERB_LOW) );
00213 
00214   forwardResponseSensitivityComputer_.setOStream(out);
00215   forwardResponseSensitivityComputer_.setVerbLevel(verbLevel);
00216 
00217   OSTab tab(out);
00218 
00219   if (trace)
00220     *out << "\nEntering ForwardResponseSensitivityComputerObserver<Scalar>::observeCompletedTimeStep(...) ...\n";
00221 
00222   // A) Get x_bar and x_dot_bar for this time step
00223 
00224   const Scalar t = stepper.getStepStatus().time;
00225 
00226   RCP<const Thyra::VectorBase<Scalar> > x_bar, x_bar_dot;
00227 
00228   get_x_and_x_dot( stepper, t, &x_bar, &x_bar_dot );
00229 
00230   RCP<const Thyra::VectorBase<Scalar> > x;
00231   RCP<const Thyra::MultiVectorBase<Scalar> > S;
00232   RCP<const Thyra::VectorBase<Scalar> >  x_dot;
00233   RCP<const Thyra::MultiVectorBase<Scalar> > S_dot;
00234 
00235   extractStateAndSens( x_bar, x_bar_dot, &x, &S, &x_dot, &S_dot );
00236 
00237   // B) Compute and assemble the response and reduced sensitivity
00238 
00239   forwardResponseSensitivityComputer_.computeResponseAndSensitivity(
00240     x_dot.get(), S_dot.get(), *x, *S, t, &*g_hat_, &*D_g_hat_D_p_
00241     );
00242   
00243   // C) Store this point
00244 
00245   responseAndFwdSensPoints_.push_back(
00246     ResponseAndFwdSensPoint<Scalar>(
00247       t, g_hat_->clone_v(), D_g_hat_D_p_->clone_mv()
00248       )
00249     );
00250   
00251   if (trace)
00252     *out << "\nEntering ForwardResponseSensitivityComputerObserver<Scalar>::observeCompletedTimeStep(...) ...\n";
00253 
00254 }
00255 
00256 
00257 } // namespace Rythmos
00258 
00259 
00260 #endif //RYTHMOS_FORWARD_RESPONSE_SENSITIVITY_COMPUTER_OBSERVER_HPP
 All Classes Functions Variables Typedefs Friends