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

Site Contact