00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
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 
00124 
00125 
00126 
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         
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         
00142 
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         
00150 
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         
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 
00175 
00176 
00177 
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         
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