SundanceSumOfIntegrals.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 "SundanceSumOfIntegrals.hpp"
00043 #include "SundanceSpatiallyConstantExpr.hpp"
00044 #include "SundanceSpectralPreprocessor.hpp"
00045 #include "PlayaTabs.hpp"
00046 
00047 using namespace Sundance;
00048 using namespace Teuchos;
00049 
00050 SumOfIntegrals::SumOfIntegrals(const RCP<CellFilterStub>& region,
00051   const Expr& expr,
00052   const RCP<QuadratureFamilyStub>& quad,
00053   const WatchFlag& watch)
00054   : ScalarExpr(), rqcToExprMap_()
00055 {
00056    addTerm(region, expr, quad, ParametrizedCurve::returnDummyCurve() , watch, 1);
00057 }
00058 
00059 SumOfIntegrals::SumOfIntegrals(const RCP<CellFilterStub>& region,
00060   const Expr& expr,
00061   const RCP<QuadratureFamilyStub>& quad,
00062   const ParametrizedCurve& curve,
00063   const WatchFlag& watch)  : ScalarExpr(), rqcToExprMap_()
00064 {
00065   addTerm(region, expr, quad, curve , watch, 1);
00066 }
00067 
00068 
00069 Expr SumOfIntegrals::filterSpectral(const Expr& expr) const 
00070 {
00071   return SpectralPreprocessor::projectSpectral(expr);
00072 }
00073 
00074 
00075 
00076 void SumOfIntegrals::addTerm(const RCP<CellFilterStub>& regionPtr,
00077   const Expr& expr,
00078   const RCP<QuadratureFamilyStub>& quadPtr, 
00079   const ParametrizedCurve& paramCurve,
00080   const WatchFlag& watch, int sign)
00081 {
00082   Expr ex = filterSpectral(expr);
00083 
00084   RegionQuadCombo rqc(regionPtr, quadPtr, paramCurve ,watch);
00085 
00086   if (rqcToExprMap_.containsKey(rqc))
00087   {
00088     Expr e = rqcToExprMap_.get(rqc);
00089     rqcToExprMap_.put(rqc, e + sign*ex);
00090   }
00091   else
00092   {
00093     rqcToExprMap_.put(rqc, sign*ex);
00094   }
00095 }
00096 
00097 
00098 void SumOfIntegrals::merge(const SumOfIntegrals* other, int sign) 
00099 {
00100   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00101          i=other->rqcToExprMap_.begin(); i!=other->rqcToExprMap_.end(); i++)
00102   {
00103     const RegionQuadCombo& rqc = i->first;
00104     const Expr& e = i->second;
00105     addTerm(rqc.domain(), e, rqc.quad(), rqc.paramCurve() , rqc.watch(), sign);
00106   }
00107 }
00108 
00109 void SumOfIntegrals::multiplyByConstant(const SpatiallyConstantExpr* expr) 
00110 {
00111   double a = expr->value();
00112   Sundance::Map<RegionQuadCombo, Expr> newMap;
00113   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00114          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00115   {
00116     Expr e = i->second;
00117     newMap.put(i->first, a*e);
00118   }
00119   rqcToExprMap_ = newMap;
00120 }
00121 
00122 void SumOfIntegrals::changeSign()
00123 {
00124   Sundance::Map<RegionQuadCombo, Expr> newMap;
00125   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00126          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00127   {
00128     Expr e = i->second;
00129     newMap.put(i->first, -e);
00130   }
00131   rqcToExprMap_ = newMap;
00132 }
00133 
00134 Set<int> SumOfIntegrals::funcsOnRegion(const OrderedHandle<CellFilterStub>& d, const Set<int>& funcSet) const 
00135 {
00136   Set<int> rtn;
00137   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00138          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00139   {
00140     const RegionQuadCombo& rqc = i->first;
00141     if (OrderedHandle<CellFilterStub>(rqc.domain()) != d) continue;
00142     Expr e = i->second;
00143     e.ptr()->accumulateFuncSet(rtn, funcSet);
00144   }
00145   return rtn;
00146 }
00147 
00148 
00149 bool SumOfIntegrals::integralHasTestFunctions(const OrderedHandle<CellFilterStub>& d) const 
00150 {
00151   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00152          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00153   {
00154     const RegionQuadCombo& rqc = i->first;
00155     if (OrderedHandle<CellFilterStub>(rqc.domain()) != d) continue;
00156     Expr e = i->second;
00157     if (e.hasTestFunctions()) return true;
00158   }
00159   return false;
00160 }
00161 
00162 
00163 
00164 RCP<CellFilterStub> SumOfIntegrals::nullRegion() const
00165 {
00166   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00167          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00168   {
00169     const RegionQuadCombo& rqc = i->first;
00170     if (!rqc.domain()->isNullRegion())
00171     {
00172       return rqc.domain()->makeNullRegion();
00173     }
00174   }
00175   
00176   TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
00177                      "SumOfIntegrals::nullRegion() called on a sum "
00178                      "of integrals with no non-null regions");
00179 
00180   return RCP<CellFilterStub>();
00181 }
00182 
00183 bool SumOfIntegrals::isIndependentOf(const Expr& u) const
00184 {
00185   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00186          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00187   {
00188     Expr e = i->second;
00189     if (!e.isIndependentOf(u)) return false;
00190   }
00191   return true;
00192 }
00193 
00194 bool SumOfIntegrals::isLinearForm(const Expr& u) const
00195 {
00196   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00197          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00198   {
00199     Expr e = i->second;
00200     if (!e.isLinearForm(u)) return false;
00201   }
00202   return true;
00203 }
00204 
00205 bool SumOfIntegrals::isQuadraticForm(const Expr& u) const
00206 {
00207   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00208          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00209   {
00210     Expr e = i->second;
00211     if (!e.isQuadraticForm(u)) return false;
00212   }
00213   return true;
00214 }
00215 
00216 
00217 bool SumOfIntegrals::everyTermHasTestFunctions() const
00218 {
00219   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00220          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00221   {
00222     Expr e = i->second;
00223     if (!e.everyTermHasTestFunctions()) return false;
00224   }
00225   return true;
00226 }
00227 
00228 
00229 bool SumOfIntegrals::isLinearInTests() const
00230 {
00231   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00232          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00233   {
00234     Expr e = i->second;
00235     if (!e.isLinearInTests()) return false;
00236   }
00237   return true;
00238 }
00239 
00240 bool SumOfIntegrals::hasTestFunctions() const
00241 {
00242   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00243          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00244   {
00245     Expr e = i->second;
00246     if (e.hasTestFunctions()) return true;
00247   }
00248   return false;
00249 }
00250 
00251 
00252 
00253 std::ostream& SumOfIntegrals::toText(std::ostream& os, bool paren) const
00254 {
00255   os << "Sum of Integrals[" << std::endl;
00256   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00257          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00258   {
00259     const RegionQuadCombo& rqc = i->first;
00260     Expr e = i->second;
00261     os << "Integral[" << std::endl;
00262     os << "rqc=" << rqc.toString() << std::endl;
00263     os << "expr=" << e.toString() << std::endl;
00264     os << "]" << std::endl;
00265   }
00266   os << "]" << std::endl;
00267 
00268   return os;
00269 }
00270 
00271 
00272 XMLObject SumOfIntegrals::toXML() const 
00273 {
00274   XMLObject rtn("SumOfIntegrals");
00275   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00276          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00277   {
00278     const RegionQuadCombo& rqc = i->first;
00279     Expr e = i->second;
00280     XMLObject child("Integral");
00281     rtn.addChild(child);
00282     child.addChild(rqc.quad()->toXML());
00283     child.addChild(rqc.domain()->toXML());
00284     child.addChild(rqc.watch().toXML());
00285     child.addChild(e.toXML());
00286   }
00287 
00288   return rtn;
00289 }
00290 
00291 
00292 bool SumOfIntegrals::lessThan(const ScalarExpr* other) const
00293 {
00294   const SumOfIntegrals* f = dynamic_cast<const SumOfIntegrals*>(other);
00295   TEUCHOS_TEST_FOR_EXCEPTION(f==0, std::logic_error, "cast should never fail at this point");
00296   
00297   return rqcToExprMap_ < f->rqcToExprMap_;
00298 }
00299 
00300 bool SumOfIntegrals::hasWatchedTerm() const 
00301 {
00302   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00303          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00304   {
00305     const RegionQuadCombo& rqc = i->first;
00306     if (rqc.watch().isActive()) return true;
00307   }
00308   return false;
00309 }
00310 
00311 
00312 
00313 int SumOfIntegrals::eqnSetSetupVerb() const 
00314 {
00315   int rtn = 0;
00316   for (Sundance::Map<RegionQuadCombo, Expr>::const_iterator 
00317          i=rqcToExprMap_.begin(); i!=rqcToExprMap_.end(); i++)
00318   {
00319     const RegionQuadCombo& rqc = i->first;
00320     int v = rqc.watch().param("equation set setup");
00321     if (v > rtn) rtn = v;
00322   }
00323   return rtn;
00324 }
00325 
00326 

Site Contact