SundanceSymbPreprocessor.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 
00032 
00033 #ifndef SUNDANCE_SYMBPREPROCESSOR_H
00034 #define SUNDANCE_SYMBPREPROCESSOR_H
00035 
00036 #include "SundanceExpr.hpp"
00037 #include "SundanceEvaluatableExpr.hpp"
00038 #include "SundanceZeroExpr.hpp"
00039 #include "SundanceDiscreteFuncElement.hpp"
00040 #include "SundanceComputationType.hpp"
00041 
00042 
00043 
00044 namespace Sundance
00045 {
00046 using namespace Sundance;
00047 using namespace Teuchos;
00048 
00049 
00050 
00051 
00052 class Parameter;
00053 
00054 
00055 /** */
00056 class SymbPreprocessor 
00057 {
00058 public:
00059         
00060   /** */
00061   static DerivSet setupVariations(const Expr& expr, 
00062     const Expr& vars,
00063     const Expr& varEvalPts,
00064     const Expr& unks,
00065     const Expr& unkEvalPts,
00066     const Expr& unkParams,
00067     const Expr& unkParamEvalPts,
00068     const Expr& fixedFields,
00069     const Expr& fixedFieldEvalPts, 
00070     const Expr& fixedParams,
00071     const Expr& fixedParamEvalPts, 
00072     const EvalContext& context,
00073     const ComputationType& compType);
00074 
00075   /** */
00076   static DerivSet setupFunctional(const Expr& expr, 
00077     const Expr& fixedParams,
00078     const Expr& fixedParamEvalPts,
00079     const Expr& fixedFields,
00080     const Expr& fixedFieldEvalPts,
00081     const EvalContext& context,
00082   const ComputationType& compType);
00083 
00084   /** */
00085   static DerivSet setupGradient(const Expr& expr, 
00086     const Expr& vars,
00087     const Expr& varEvalPts,
00088     const Expr& fixedParams,
00089     const Expr& fixedParamEvalPts,
00090     const Expr& fixedFields,
00091     const Expr& fixedFieldEvalPts, 
00092     const EvalContext& contex,
00093   const ComputationType& compType);
00094 
00095   /** */
00096   static DerivSet setupSensitivities(const Expr& expr, 
00097     const Expr& tests,
00098     const Expr& unks,
00099     const Expr& unkEvalPts, 
00100     const Expr& unkParams,
00101     const Expr& unkParamEvalPts,
00102     const Expr& fixedParams,
00103     const Expr& fixedParamEvalPts,
00104     const Expr& fixedFields,
00105     const Expr& fixedFieldEvalPts,
00106     const EvalContext& context,
00107   const ComputationType& compType);
00108 
00109   /** */
00110   static DerivSet setupFwdProblem(const Expr& expr, 
00111     const Expr& tests,
00112     const Expr& unks,
00113     const Expr& unkEvalPts, 
00114     const Expr& unkParams,
00115     const Expr& unkParamEvalPts,
00116     const Expr& fixedParams,
00117     const Expr& fixedParamEvalPts,
00118     const Expr& fixedFields,
00119     const Expr& fixedFieldEvalPts,
00120     const EvalContext& context,
00121   const ComputationType& compType);
00122 
00123   /** check the input functions for redundancies and functions
00124    * of unexpected type. Set evaluation points and collect
00125    * a set of function IDs. This function is templated so we can use
00126    * the same code for processing unknown and variational functions */
00127   template <class T>
00128   static Set<int> processInputFuncs(const Expr& u, const Expr& u0)
00129     {
00130       Set<int> idSet;
00131 
00132       for (int i=0; i<u.size(); i++)
00133       {
00134         /* make sure all input functions have the correct type */
00135         const T* uPtr = dynamic_cast<const T*>(u[i].ptr().get());
00136         TEUCHOS_TEST_FOR_EXCEPTION(uPtr==0, std::runtime_error, 
00137           "Unexpected function type error: function " << u[i].toString()
00138           << " is of type=" << typeid(u).name() 
00139           << ", but we expected type=" << typeid(T).name());
00140 
00141         /* Add the function's ID to the ID set. While we're here, check
00142          * to ensure we have no duplicates in the input list. */
00143         int fid = uPtr->fid().dofID();
00144         TEUCHOS_TEST_FOR_EXCEPTION(idSet.contains(fid), std::runtime_error,
00145           "duplicate function in input list " << u.toString());
00146         idSet.put(fid);
00147 
00148 
00149         /* Check that the evaluation point is either a discrete function
00150          * or a zero expression. */
00151         RCP<DiscreteFuncElement> u0Ptr
00152           = rcp_dynamic_cast<DiscreteFuncElement>(u0[i].ptr());
00153         RCP<ZeroExpr> u0ZeroPtr
00154           = rcp_dynamic_cast<ZeroExpr>(u0[i].ptr());
00155         TEUCHOS_TEST_FOR_EXCEPTION(u0Ptr.get()==NULL && u0ZeroPtr.get()==NULL,
00156           std::runtime_error,
00157           "evaluation point " << u0[i].toString() << " for func=" << u[i]
00158           << " is neither a discrete function nor a zero expr");
00159 
00160         /* Set the evaluation point */
00161         if (u0Ptr.get()==NULL)
00162         {
00163           uPtr->substituteZero();
00164         }
00165         else
00166         {
00167           uPtr->substituteFunction(u0Ptr);
00168         }
00169       }
00170 
00171       return idSet;
00172     }
00173 
00174   /** check the input parameters for redundancies and type. Set 
00175    * evaluation points and collect
00176    * a set of parameter IDs. This function is templated so we can use
00177    * the same code for processing unknown and variational parameters */
00178   template <class T>
00179   static Set<int> processInputParams(const Expr& alpha, const Expr& alpha0)
00180     {
00181       Set<int> paramID;
00182 
00183       for (int i=0; i<alpha.size(); i++)
00184       {
00185         /* ensure everyone has the correct type */
00186         const T* aPtr = dynamic_cast<const T*>(alpha[i].ptr().get());
00187         TEUCHOS_TEST_FOR_EXCEPTION(aPtr==0, std::runtime_error,
00188           "list of purported parameters "
00189           "contains a function that is not an unknown parameter:"
00190           << alpha[i].toString());
00191 
00192         int fid = aPtr->fid().dofID();
00193         TEUCHOS_TEST_FOR_EXCEPTION(paramID.contains(fid), std::runtime_error,
00194           "duplicate input parameter in list "
00195           << alpha.toString());
00196         paramID.put(fid);
00197 
00198         RCP<Parameter> a0Ptr
00199           = rcp_dynamic_cast<Parameter>(alpha0[i].ptr());
00200         TEUCHOS_TEST_FOR_EXCEPTION(a0Ptr.get()==NULL,
00201           std::runtime_error,
00202           "parameter evaluation point " << alpha0[i].toString()
00203           << " is not a parameter");
00204         aPtr->substituteFunction(a0Ptr);
00205       }
00206       return paramID;
00207     }
00208 
00209 
00210   /** */
00211   TEUCHOS_TIMER(preprocTimer, "symbolic preprocessing");
00212 };
00213 
00214 /** */
00215 Expr makeZeros(const Expr& e);
00216 
00217 
00218 }
00219 
00220 #endif

Site Contact