PDEOptPDEConstrainedObjBase.hpp
Go to the documentation of this file.
00001 #ifndef PDEOPT_PDECONSTRAINEDOBJBASE_H
00002 #define PDEOPT_PDECONSTRAINEDOBJBASE_H
00003 
00004 #include "PlayaObjectiveBase.hpp"
00005 #include "SundanceExpr.hpp"
00006 #include "SundanceFunctional.hpp"
00007 #include "PDEOptIterCallbackBase.hpp"
00008 
00009 namespace Sundance
00010 {
00011 using namespace Playa;
00012 
00013 
00014 /**
00015  * PDEConstrainedObj is a base class for objective functions of the
00016  * reduced-space variable where the constraint is a PDE in the
00017  * state variables. 
00018  * Objects of this type are suitable for use in adjoint gradient methods.
00019  *
00020  * One constructs such an objective function by giving it a Lagrangian in the
00021  * form of a %Sundance Functional, along with a specification of which functions
00022  * are to be regarded as the adjoint, state, and design variables. 
00023  */
00024 class PDEConstrainedObjBase : public ObjectiveBase
00025 {
00026 public:
00027       
00028   /** Constructor */
00029   PDEConstrainedObjBase(
00030     const Functional& lagrangian,
00031     const Array<Expr>& stateVarVals,
00032     const Array<Expr>& adjointVarVals,
00033     const Expr& designVarVal,
00034     const RCP<IterCallbackBase>& iterCallback,
00035     int verb = 0);
00036       
00037   /** Constructor */
00038   PDEConstrainedObjBase(
00039     const Functional& lagrangian,
00040     const Array<Expr>& stateVarVals,
00041     const Array<Expr>& adjointVarVals,
00042     const Expr& designVarVal,
00043     int verb = 0);
00044 
00045   /** virtual dtor */
00046   virtual ~PDEConstrainedObjBase(){;}
00047 
00048   /** evaluate objective function and gradient */
00049   void  evalGrad(const Vector<double>& x, double& f, 
00050     Vector<double>& grad) const ;
00051 
00052   /** evaluate objective function without gradient. */
00053   void eval(const Vector<double>& x, double& f) const ;
00054 
00055   /** return an initial guess for the design vector  */
00056   Vector<double> getInit() const ;
00057 
00058   /** Hook for anything that needs to be done between the solution
00059    * of the state equations and the evaluation of the functional or
00060    * solution of adjoints. 
00061    *
00062    * Default is a no-op. */
00063   virtual void statePostprocCallback() const {;}
00064 
00065   /** */
00066   virtual void iterationCallback(const Vector<double>& x, int iter) const ;
00067 
00068   /** */
00069   const Mesh& mesh() const {return Lagrangian_.mesh();}
00070 
00071   /** */
00072   int numFuncEvals() const {return numFuncEvals_;}
00073 
00074   /** Solve the state equations, followed by postprocessing.
00075    * At the end of this call, the system is ready for evaluation of
00076    * the objective function or solution of the adjoint equations. */
00077   virtual void solveState(const Vector<double>& x) const = 0 ;
00078 
00079   /** Solve the state equations, then do postprocessing,
00080    * then finally the adjoint equations in <b>reverse</b> order. 
00081    * At the end of this call, the system is ready for evaluation
00082    * of the objective function and its gradient. */
00083   virtual void solveStateAndAdjoint(const Vector<double>& x) const = 0 ;
00084 
00085   /** Set up the state and adjoint equations. This is left to the derived
00086    * class, because we can't know at this level whether the equations
00087    * are linear or nonlinear */
00088   virtual void initEquations(
00089     const Array<Expr>& stateVars,
00090     const Array<Expr>& adjointVars,
00091     const Array<Array<Expr> >& fixedVarsInStateEqns,
00092     const Array<Array<Expr> >& fixedVarsInStateEqnsVals,
00093     const Array<Array<Expr> >& fixedVarsInAdjointEqns,
00094     const Array<Array<Expr> >& fixedVarsInAdjointEqnsVals
00095     ) = 0 ;
00096 
00097   /** Set the scale of the Hessian */
00098   void setHScale(const double& H) {invHScale_ = 1.0/H;}
00099 
00100   /** return an initial approximation to the scale for the 
00101    * inverse of the Hessian */
00102   double getInvHScale() const {return invHScale_;}
00103   
00104 
00105   /** Access to the design variable */
00106   const Expr& designVar() const
00107     {return designVarVal_;}
00108 
00109   /** Access to the state variables */
00110   const Array<Expr>& stateVars() const
00111     {return stateVarVals_;}
00112 
00113   /** Access to the adjoint variables */
00114   const Array<Expr>& adjointVars() const
00115     {return adjointVarVals_;}
00116 
00117 protected:  
00118   /** */
00119   void init(
00120     const Array<Expr>& stateVars,
00121     const Array<Expr>& adjointVars,
00122     const Expr& designVar);
00123 
00124   /** Access to the design variable */
00125   Expr& designVarVal() const
00126     {return designVarVal_;}
00127 
00128   /** Access to the state variables */
00129   Expr& stateVarVals(int i) const
00130     {return stateVarVals_[i];}
00131 
00132   /** Access to the adjoint variables */
00133   Expr& adjointVarVals(int i) const
00134     {return adjointVarVals_[i];}
00135 
00136   const Functional& Lagrangian() const {return Lagrangian_;}
00137       
00138 private:
00139 
00140   Functional Lagrangian_;
00141 
00142   mutable Expr designVarVal_;
00143 
00144   mutable Array<Expr> stateVarVals_;
00145 
00146   mutable Array<Expr> adjointVarVals_;
00147 
00148   FunctionalEvaluator fEval_;
00149 
00150   mutable int numFuncEvals_;
00151 
00152   mutable int numGradEvals_;
00153 
00154   double invHScale_;
00155 
00156   RCP<IterCallbackBase> iterCallback_;
00157 };
00158 
00159 }
00160 
00161 #endif

Site Contact