SundanceSparsitySuperset.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 "SundanceSparsitySuperset.hpp"
00032 #include "SundanceEvaluatableExpr.hpp"
00033 
00034 #include "SundanceEvalVector.hpp"
00035 #include "PlayaTabs.hpp"
00036 #include "SundanceOut.hpp"
00037 
00038 
00039 
00040 using namespace Sundance;
00041 using namespace Sundance;
00042 
00043 using namespace Sundance;
00044 using namespace Teuchos;
00045 
00046 
00047 
00048 
00049 SparsitySuperset::SparsitySuperset(const Set<MultipleDeriv>& C,
00050                                    const Set<MultipleDeriv>& V)
00051   : maxOrder_(0),
00052     derivToIndexMap_(),
00053     derivs_(),
00054     states_(),
00055     multiIndex_(),
00056     numConstantDerivs_(0),
00057     numVectorDerivs_(0)
00058 {
00059   for (Set<MultipleDeriv>::const_iterator i=C.begin(); i!=C.end(); i++)
00060     {
00061       addDeriv(*i, ConstantDeriv);
00062     }
00063 
00064   for (Set<MultipleDeriv>::const_iterator i=V.begin(); i!=V.end(); i++)
00065     {
00066       addDeriv(*i, VectorDeriv);
00067     }
00068 }
00069 
00070 
00071 
00072 void SparsitySuperset::addDeriv(const MultipleDeriv& d, 
00073                                const DerivState& state)
00074 {
00075   maxOrder_ = std::max(d.order(), maxOrder_);
00076 
00077   if (containsDeriv(d))
00078     {
00079       const DerivState& oldState = states_[getIndex(d)];
00080       if (state > oldState) 
00081         {
00082           states_[getIndex(d)] = state;
00083           numConstantDerivs_--;
00084           numVectorDerivs_++;
00085         }
00086     }
00087   else
00088     {
00089       int index = derivs_.size();
00090       derivs_.append(d);
00091       states_.append(state);
00092       derivToIndexMap_.put(d, index);
00093       MultiIndex mi;
00094       for (MultipleDeriv::const_iterator i=d.begin(); 
00095            i != d.end(); i++)
00096         {
00097           if (i->isCoordDeriv())
00098             {
00099               MultiIndex m;
00100               int dir = i->coordDerivDir();
00101               m[dir] = 1;
00102               mi = mi + m;
00103             }
00104 
00105         }
00106       multiIndex_.append(mi);
00107       if (state==ConstantDeriv) numConstantDerivs_++;
00108       else numVectorDerivs_++;
00109     }
00110 }
00111 
00112 void SparsitySuperset::addDeriv(const Deriv& d, 
00113                                const DerivState& state)
00114 {
00115   MultipleDeriv md;
00116   md.put(d);
00117   addDeriv(md, state);
00118 }
00119 
00120 
00121 
00122 bool SparsitySuperset::containsDeriv(const MultipleDeriv& d) const
00123 {
00124   return derivToIndexMap_.containsKey(d);
00125 }
00126 
00127 int SparsitySuperset::getIndex(const MultipleDeriv& d) const
00128 {
00129   if (!containsDeriv(d)) return -1;
00130   return derivToIndexMap_.get(d);
00131 }
00132 
00133 
00134 
00135 void SparsitySuperset::print(std::ostream& os,
00136                              const Array<RCP<EvalVector> >& vecResults,
00137                              const Array<double>& constantResults) const 
00138 {
00139   Tabs tabs;
00140 
00141   /* find the maximum size of the std::string reps of the derivatives.
00142    * We'll use this to set the field width for printing derivatives. */
00143   int maxlen = 25;
00144   for (int i=0; i<derivs_.size(); i++)
00145     {
00146       int s = derivs_[i].toString().length();
00147       if (s > maxlen) maxlen = s;
00148     }
00149 
00150 
00151   int vecIndex=0;
00152   int constIndex = 0;
00153   os << tabs << "Results Superset" << std::endl;
00154   for (int i=0; i<derivs_.size(); i++)
00155     {
00156       os << tabs << i << "\t\t";
00157       os.width(maxlen);
00158       os.setf(std::ios_base::left, std::ios_base::adjustfield);
00159       os << derivs_[i].toString() << "\t\t" ;
00160       switch(states_[i])
00161         {
00162         case ZeroDeriv:
00163           os  << "Zero" << std::endl;
00164           break;
00165         case ConstantDeriv:
00166           os << constantResults[constIndex++] << std::endl;
00167           break;
00168         case VectorDeriv:
00169           if (vecResults[vecIndex].get()==0)
00170             {
00171               os << "{Null}";
00172             }
00173           else
00174             {
00175               vecResults[vecIndex]->print(os);
00176             }
00177           vecIndex++;
00178           os << std::endl;
00179           break;
00180         }
00181     }
00182 }
00183 
00184 
00185 
00186 void SparsitySuperset::print(std::ostream& os) const 
00187 {
00188   Tabs tabs;
00189 
00190   /* find the maximum size of the std::string reps of the derivatives.
00191    * We'll use this to set the field width for printing derivatives. */
00192   int maxlen = 25;
00193   for (int i=0; i<derivs_.size(); i++)
00194     {
00195       int s = derivs_[i].toString().length();
00196       if (s > maxlen) maxlen = s;
00197     }
00198 
00199 
00200   os << tabs << "SparsitySuperset" << std::endl;
00201   for (int i=0; i<derivs_.size(); i++)
00202     {
00203       os << tabs << i << "\tderiv=\t";
00204       os.width(maxlen);
00205       os.setf(std::ios_base::left, std::ios_base::adjustfield);
00206       os << derivs_[i].toString() << "\tstate=\t" ;
00207       switch(states_[i])
00208         {
00209         case ZeroDeriv:
00210           os  << "Zero" << std::endl;
00211           break;
00212         case ConstantDeriv:
00213           os << "Constant" << std::endl;
00214           break;
00215         case VectorDeriv:
00216           os << "Vector" << std::endl;
00217           break;
00218         }
00219     }
00220 }
00221 
00222 string SparsitySuperset::toString() const 
00223 {
00224   std::ostringstream ss;
00225   print(ss);
00226   return ss.str();
00227 }
00228 
00229 DerivSet SparsitySuperset::derivSet() const
00230 {
00231   DerivSet rtn;
00232   for (int i=0; i<numDerivs(); i++) 
00233     {
00234       if (state(i) != ZeroDeriv) rtn.put(deriv(i));
00235     }
00236   return rtn;
00237 }

Site Contact