SundanceDiscreteFuncEvaluator.cpp
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 #include "SundanceDiscreteFuncEvaluator.hpp"
00032 #include "SundanceEvalManager.hpp"
00033 #include "SundanceCoordExpr.hpp"
00034 #include "SundanceZeroExpr.hpp"
00035 #include "SundanceSpatiallyConstantExpr.hpp"
00036 #include "SundanceSymbolicFuncElement.hpp"
00037 #include "SundanceDiscreteFuncElement.hpp"
00038 #include "SundanceSet.hpp"
00039 #include "PlayaTabs.hpp"
00040 #include "SundanceOut.hpp"
00041 
00042 using namespace Sundance;
00043 using namespace Sundance;
00044 
00045 using namespace Sundance;
00046 using namespace Teuchos;
00047 
00048 
00049 
00050 DiscreteFuncElementEvaluator
00051 ::DiscreteFuncElementEvaluator(const DiscreteFuncElement* expr, 
00052                                const EvalContext& context)
00053   : SubtypeEvaluator<DiscreteFuncElement>(expr, context), 
00054     mi_(this->sparsity()->numDerivs()),
00055     miToIndexMap_(),
00056     stringReps_()
00057 {
00058   Tabs tabs;
00059   SUNDANCE_VERB_MEDIUM(tabs << "initializing discrete func evaluator for " 
00060                     << expr->toString());
00061 
00062   SUNDANCE_VERB_MEDIUM(tabs << "return sparsity " << std::endl << *(this->sparsity()));
00063 
00064   static Array<string> coordNames;
00065   if (coordNames.size() != 3)
00066     {
00067       coordNames.resize(3);
00068       coordNames[0] = "x";
00069       coordNames[1] = "y";
00070       coordNames[2] = "z";
00071     }
00072   std::string funcName = expr->name();
00073 
00074   for (int i=0; i<this->sparsity()->numDerivs(); i++)
00075     {
00076       /* Make sure that every derivative we're to evaluate is either
00077       * zero-order or a spatial derivative */
00078       if (this->sparsity()->deriv(i).order()==0) 
00079         {
00080           mi_[i] = MultiIndex();
00081         }
00082       else 
00083         {
00084       
00085           TEUCHOS_TEST_FOR_EXCEPTION(!this->sparsity()->isSpatialDeriv(i), std::logic_error,
00086                              "DiscreteFuncElementEvaluator ctor found "
00087                              "an entry in the sparsity superset that is not "
00088                              "a spatial derivative. "
00089                              "The bad entry is " << this->sparsity()->deriv(i) 
00090                              << ". The superset is " 
00091                              << *(this->sparsity)());
00092 
00093           mi_[i] = this->sparsity()->multiIndex(i);
00094         }
00095       addVectorIndex(i,i);
00096       TEUCHOS_TEST_FOR_EXCEPTION(miToIndexMap_.containsKey(mi_[i]), std::logic_error,
00097                          "DiscreteFuncElementEvaluator ctor detected a "
00098                          "duplicate multiindex");
00099 
00100       miToIndexMap_.put(mi_[i], i);
00101 
00102       if (mi_[i].order()==0)
00103         {
00104           stringReps_.append(funcName);
00105         }
00106       else
00107         {
00108           int dir = mi_[i].firstOrderDirection();
00109           std::string deriv = "D[" + funcName + ", " + coordNames[dir] + "]";
00110           stringReps_.append(deriv);
00111         }
00112     }
00113 
00114   
00115 }
00116 
00117 bool DiscreteFuncElementEvaluator::hasMultiIndex(const MultiIndex& mi) const
00118 {
00119   Tabs tabs;
00120   bool rtn = miToIndexMap_.containsKey(mi);
00121   SUNDANCE_VERB_MEDIUM(tabs << "checking for mi=" << mi << " for " 
00122                        << expr()->toString()
00123                        << std::endl << tabs 
00124                        << " sparsity " << std::endl << *(this->sparsity()));
00125   
00126   return rtn;
00127 }
00128 
00129 int DiscreteFuncElementEvaluator::miIndex(const MultiIndex& mi) const
00130 {
00131   return miToIndexMap_.get(mi);
00132 }
00133 
00134 
00135 void DiscreteFuncElementEvaluator
00136 ::internalEval(const EvalManager& mgr,
00137                Array<double>& constantResults,
00138                Array<RCP<EvalVector> >& vectorResults) const 
00139 {
00140   Tabs tabs(0);
00141   SUNDANCE_MSG1(mgr.verb(),
00142     tabs << "DiscreteFuncElementEvaluator::eval: expr=" 
00143     << expr()->toString());
00144 
00145   vectorResults.resize(mi_.size());
00146   for (int i=0; i<mi_.size(); i++)
00147     {
00148       Tabs tab2;
00149       vectorResults[i] = mgr.popVector();
00150       TEUCHOS_TEST_FOR_EXCEPTION(!vectorResults[i]->isValid(), 
00151                          std::logic_error,
00152                          "invalid evaluation vector allocated in "
00153                          "DiscreteFuncElementEvaluator::internalEval()");
00154       SUNDANCE_MSG2(mgr.verb(),tab2<< "setting string rep " << stringReps_[i]);
00155       vectorResults[i]->setString(stringReps_[i]);
00156     }
00157   mgr.evalDiscreteFuncElement(expr(), mi_, vectorResults);
00158   mgr.stack().setVecSize(vectorResults[0]->length());
00159   
00160   if (mgr.verb() > 1)
00161     {
00162       Out::os() << tabs << "results " << std::endl;
00163       mgr.showResults(Out::os(), sparsity(), vectorResults,
00164                             constantResults);
00165     }
00166   SUNDANCE_MSG1(mgr.verb(), tabs << "DiscreteFuncEvaluator::eval() done"); 
00167 }
00168 

Site Contact