SundanceUserDefOpElement.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 "SundanceUserDefOpElement.hpp"
00032 #include "SundanceUserDefOp.hpp"
00033 #include "SundanceUserDefOpEvaluator.hpp"
00034 #include "PlayaTabs.hpp"
00035 #include "SundanceOut.hpp"
00036 
00037 using namespace Sundance;
00038 using namespace Teuchos;
00039 
00040 
00041 UserDefOpElement::UserDefOpElement(const Array<RCP<ScalarExpr> >& args,
00042   const RCP<Sundance::Map<EvalContext, RCP<const UserDefOpCommonEvaluator> > >& evalMap,
00043   const RCP<const UserDefFunctorElement>& functorElement)
00044   : ExprWithChildren(args), 
00045     commonEvaluatorsMap_(evalMap),
00046     functorElement_(functorElement)
00047 {}
00048 
00049 
00050 
00051 void UserDefOpElement::getArgDerivIndices(const Array<int>& orders,
00052   Map<MultiSet<int>, int>& varArgDerivs,
00053   Map<MultiSet<int>, int>& constArgDerivs) const
00054 {
00055   int n = functorElement()->numArgs();
00056 
00057   int counter = 0;
00058 
00059   for (int o=0; o<orders.size(); o++)
00060   {
00061     int order = orders[o];
00062       
00063     if (order==0)
00064     {
00065       varArgDerivs.put(MultiSet<int>(), counter++);
00066     }
00067     else if (order==1)
00068     {
00069       for (int i=0; i<n; i++)
00070       {
00071         varArgDerivs.put(makeMultiSet<int>(i), counter++);
00072       }
00073     }
00074     else if (order==2)
00075     {
00076       for (int i=0; i<n; i++)
00077       {
00078         for (int j=0; j<=i; j++)
00079         {
00080           varArgDerivs.put(makeMultiSet<int>(i,j), counter++);
00081         }
00082       }
00083     }
00084     else
00085     {
00086       TEUCHOS_TEST_FOR_EXCEPTION(order > functorElement()->maxOrder() || order < 0, std::runtime_error,
00087         "order " << order << " not supported by functor " 
00088         << functorElement()->masterName());
00089     }
00090   }
00091 }
00092 
00093 std::ostream& UserDefOpElement::toText(std::ostream& os, bool paren) const 
00094 {
00095   os << functorElement()->name() << "(";
00096   for (int i=0; i<numChildren(); i++)
00097   {
00098     os << child(i).toString();
00099     if (i < numChildren()-1) os << ",";
00100   }
00101   os << ")";
00102   return os;
00103 }
00104 
00105 XMLObject UserDefOpElement::toXML() const
00106 {
00107   XMLObject rtn("UserDefOpElement");
00108   XMLObject args("Arguments");
00109   for (int i=0; i<numChildren(); i++)
00110   {
00111     args.addChild(child(i).toXML());
00112   }
00113   rtn.addChild(args);
00114   rtn.addAttribute("op", functorElement()->name());
00115   return rtn;
00116 }
00117 
00118 
00119 Evaluator* UserDefOpElement::createEvaluator(const EvaluatableExpr* expr,
00120   const EvalContext& context) const
00121 {
00122   const UserDefOpElement* me = dynamic_cast<const UserDefOpElement*>(expr);
00123   TEUCHOS_TEST_FOR_EXCEPTION(me == 0, std::logic_error,
00124     "cast failed in UserDefOpElement::createEvaluator()");
00125   return new UserDefOpEvaluator(me, getCommonEvaluator(context),
00126     context);
00127 }
00128 
00129 
00130 
00131 RCP<const UserDefOpCommonEvaluator> 
00132 UserDefOpElement::getCommonEvaluator(const EvalContext& context) const 
00133 {
00134   if (!commonEvaluatorsMap_->containsKey(context))
00135   {
00136     commonEvaluatorsMap_->put(context, 
00137       rcp(new UserDefOpCommonEvaluator(functorElement()->master(), this, context)));
00138   }
00139   return commonEvaluatorsMap_->get(context);
00140 }

Site Contact