SundanceSymbPreprocessor.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 
00044 #ifndef SUNDANCE_SYMBPREPROCESSOR_H
00045 #define SUNDANCE_SYMBPREPROCESSOR_H
00046 
00047 #include "SundanceExpr.hpp"
00048 #include "SundanceEvaluatableExpr.hpp"
00049 #include "SundanceZeroExpr.hpp"
00050 #include "SundanceDiscreteFuncElement.hpp"
00051 #include "SundanceComputationType.hpp"
00052 
00053 
00054 
00055 namespace Sundance
00056 {
00057 using namespace Sundance;
00058 using namespace Teuchos;
00059 
00060 
00061 
00062 
00063 class Parameter;
00064 
00065 
00066 /** */
00067 class SymbPreprocessor 
00068 {
00069 public:
00070         
00071   /** */
00072   static DerivSet setupVariations(const Expr& expr, 
00073     const Expr& vars,
00074     const Expr& varEvalPts,
00075     const Expr& unks,
00076     const Expr& unkEvalPts,
00077     const Expr& unkParams,
00078     const Expr& unkParamEvalPts,
00079     const Expr& fixedFields,
00080     const Expr& fixedFieldEvalPts, 
00081     const Expr& fixedParams,
00082     const Expr& fixedParamEvalPts, 
00083     const EvalContext& context,
00084     const ComputationType& compType);
00085 
00086   /** */
00087   static DerivSet setupFunctional(const Expr& expr, 
00088     const Expr& fixedParams,
00089     const Expr& fixedParamEvalPts,
00090     const Expr& fixedFields,
00091     const Expr& fixedFieldEvalPts,
00092     const EvalContext& context,
00093   const ComputationType& compType);
00094 
00095   /** */
00096   static DerivSet setupGradient(const Expr& expr, 
00097     const Expr& vars,
00098     const Expr& varEvalPts,
00099     const Expr& fixedParams,
00100     const Expr& fixedParamEvalPts,
00101     const Expr& fixedFields,
00102     const Expr& fixedFieldEvalPts, 
00103     const EvalContext& contex,
00104   const ComputationType& compType);
00105 
00106   /** */
00107   static DerivSet setupSensitivities(const Expr& expr, 
00108     const Expr& tests,
00109     const Expr& unks,
00110     const Expr& unkEvalPts, 
00111     const Expr& unkParams,
00112     const Expr& unkParamEvalPts,
00113     const Expr& fixedParams,
00114     const Expr& fixedParamEvalPts,
00115     const Expr& fixedFields,
00116     const Expr& fixedFieldEvalPts,
00117     const EvalContext& context,
00118   const ComputationType& compType);
00119 
00120   /** */
00121   static DerivSet setupFwdProblem(const Expr& expr, 
00122     const Expr& tests,
00123     const Expr& unks,
00124     const Expr& unkEvalPts, 
00125     const Expr& unkParams,
00126     const Expr& unkParamEvalPts,
00127     const Expr& fixedParams,
00128     const Expr& fixedParamEvalPts,
00129     const Expr& fixedFields,
00130     const Expr& fixedFieldEvalPts,
00131     const EvalContext& context,
00132   const ComputationType& compType);
00133 
00134   /** check the input functions for redundancies and functions
00135    * of unexpected type. Set evaluation points and collect
00136    * a set of function IDs. This function is templated so we can use
00137    * the same code for processing unknown and variational functions */
00138   template <class T>
00139   static Set<int> processInputFuncs(const Expr& u, const Expr& u0)
00140     {
00141       Set<int> idSet;
00142 
00143       for (int i=0; i<u.size(); i++)
00144       {
00145         /* make sure all input functions have the correct type */
00146         const T* uPtr = dynamic_cast<const T*>(u[i].ptr().get());
00147         TEUCHOS_TEST_FOR_EXCEPTION(uPtr==0, std::runtime_error, 
00148           "Unexpected function type error: function " << u[i].toString()
00149           << " is of type=" << typeid(u).name() 
00150           << ", but we expected type=" << typeid(T).name());
00151 
00152         /* Add the function's ID to the ID set. While we're here, check
00153          * to ensure we have no duplicates in the input list. */
00154         int fid = uPtr->fid().dofID();
00155         TEUCHOS_TEST_FOR_EXCEPTION(idSet.contains(fid), std::runtime_error,
00156           "duplicate function in input list " << u.toString());
00157         idSet.put(fid);
00158 
00159 
00160         /* Check that the evaluation point is either a discrete function
00161          * or a zero expression. */
00162         RCP<DiscreteFuncElement> u0Ptr
00163           = rcp_dynamic_cast<DiscreteFuncElement>(u0[i].ptr());
00164         RCP<ZeroExpr> u0ZeroPtr
00165           = rcp_dynamic_cast<ZeroExpr>(u0[i].ptr());
00166         TEUCHOS_TEST_FOR_EXCEPTION(u0Ptr.get()==NULL && u0ZeroPtr.get()==NULL,
00167           std::runtime_error,
00168           "evaluation point " << u0[i].toString() << " for func=" << u[i]
00169           << " is neither a discrete function nor a zero expr");
00170 
00171         /* Set the evaluation point */
00172         if (u0Ptr.get()==NULL)
00173         {
00174           uPtr->substituteZero();
00175         }
00176         else
00177         {
00178           uPtr->substituteFunction(u0Ptr);
00179         }
00180       }
00181 
00182       return idSet;
00183     }
00184 
00185   /** check the input parameters for redundancies and type. Set 
00186    * evaluation points and collect
00187    * a set of parameter IDs. This function is templated so we can use
00188    * the same code for processing unknown and variational parameters */
00189   template <class T>
00190   static Set<int> processInputParams(const Expr& alpha, const Expr& alpha0)
00191     {
00192       Set<int> paramID;
00193 
00194       for (int i=0; i<alpha.size(); i++)
00195       {
00196         /* ensure everyone has the correct type */
00197         const T* aPtr = dynamic_cast<const T*>(alpha[i].ptr().get());
00198         TEUCHOS_TEST_FOR_EXCEPTION(aPtr==0, std::runtime_error,
00199           "list of purported parameters "
00200           "contains a function that is not an unknown parameter:"
00201           << alpha[i].toString());
00202 
00203         int fid = aPtr->fid().dofID();
00204         TEUCHOS_TEST_FOR_EXCEPTION(paramID.contains(fid), std::runtime_error,
00205           "duplicate input parameter in list "
00206           << alpha.toString());
00207         paramID.put(fid);
00208 
00209         RCP<Parameter> a0Ptr
00210           = rcp_dynamic_cast<Parameter>(alpha0[i].ptr());
00211         TEUCHOS_TEST_FOR_EXCEPTION(a0Ptr.get()==NULL,
00212           std::runtime_error,
00213           "parameter evaluation point " << alpha0[i].toString()
00214           << " is not a parameter");
00215         aPtr->substituteFunction(a0Ptr);
00216       }
00217       return paramID;
00218     }
00219 
00220 
00221   /** */
00222   TEUCHOS_TIMER(preprocTimer, "symbolic preprocessing");
00223 };
00224 
00225 /** */
00226 Expr makeZeros(const Expr& e);
00227 
00228 
00229 }
00230 
00231 #endif

Site Contact