Intrepid
http://trilinos.sandia.gov/packages/docs/r11.2/packages/intrepid/src/Discretization/Integration/Intrepid_AdaptiveSparseGridInterfaceDef.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ************************************************************************
00003 //
00004 //                           Intrepid Package
00005 //                 Copyright (2007) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
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 Pavel Bochev  (pbboche@sandia.gov)
00038 //                    Denis Ridzal  (dridzal@sandia.gov), or
00039 //                    Kara Peterson (kjpeter@sandia.gov)
00040 //
00041 // ************************************************************************
00042 // @HEADER
00043 
00049 namespace Intrepid {
00050 
00051 template<class Scalar, class UserVector>
00052 AdaptiveSparseGridInterface<Scalar,UserVector>::AdaptiveSparseGridInterface(
00053               int dimension,
00054               std::vector<EIntrepidBurkardt> rule1D,
00055               std::vector<EIntrepidGrowth> growth1D,
00056               int maxLevel,
00057               bool isNormalized) {
00058 
00059   TEUCHOS_TEST_FOR_EXCEPTION((dimension!=(int)rule1D.size()||
00060                       dimension!=(int)growth1D.size()),std::out_of_range,
00061     ">>> ERROR (AdaptiveSparseGridInterface): Dimension mismatch for inputs.");
00062 
00063   dimension_    = dimension;
00064   rule1D_       = rule1D;
00065   growth1D_     = growth1D;
00066   maxLevel_     = maxLevel;
00067   isNormalized_ = isNormalized;
00068 }
00069 
00070 template<class Scalar, class UserVector>
00071 void AdaptiveSparseGridInterface<Scalar,UserVector>::init(UserVector & output) {
00072   std::vector<int> index(dimension_,1);
00073   CubatureTensorSorted<Scalar> cubRule(
00074             dimension_,index,rule1D_,growth1D_,isNormalized_);
00075   
00076   // Evaluate the initial contribution to the integral
00077   initialDiff_ = 1.0;
00078   output.Update(-1.0,output);
00079   eval_cubature(output,cubRule);
00080 
00081   // Compute the initial error indicator
00082   initialDiff_ = error_indicator(output);
00083   if (fabs(initialDiff_)<INTREPID_TOL) 
00084     initialDiff_ = 1.0;
00085 }
00086 
00087 template<class Scalar, class UserVector>
00088 bool AdaptiveSparseGridInterface<Scalar,UserVector>::max_level(
00089                                              std::vector<int> index) {
00090   int dimension = (int)index.size();
00091   int sum = 0;
00092   for (int i=0; i<dimension; i++) {
00093     sum += index[i];
00094   }
00095   if (sum <= maxLevel_ + dimension - 1) 
00096     return true;
00097   return false;
00098 }
00099 
00100 template<class Scalar, class UserVector>
00101 void AdaptiveSparseGridInterface<Scalar,UserVector>::eval_cubature(
00102                UserVector & output, 
00103                CubatureTensorSorted<Scalar> & cubRule) {
00104 
00105   //int dimf      = 0;                      // Dimension of the integrand
00106   Scalar weight = 0.0;
00107   std::vector<Scalar> point(dimension_,(Scalar)0.0);
00108   //std::vector<Scalar> f(1,0.0);
00109   Teuchos::RCP<UserVector> f = output.Create(); output.Update(-1.0,output);
00110 
00111   typename std::map<std::vector<Scalar>,int>::iterator it;
00112   for (it=cubRule.begin(); it!=cubRule.end(); it++) {
00113     // Evaluate Function
00114     point.assign((it->first).begin(),(it->first).end()); // Extract point
00115     f->Update(-1.0,*f);
00116     eval_integrand(*f,point);     // Evaluate Integrand at point
00117  
00118     // Update integral
00119     weight = cubRule.getWeight(it->second);
00120     output.Update(weight,*f);
00121   }
00122 }
00123 
00124 template<class Scalar, class UserVector>
00125 void AdaptiveSparseGridInterface<Scalar,UserVector>::getRule(
00126         std::vector<EIntrepidBurkardt> & rule1D) {
00127   rule1D.clear();
00128   rule1D.resize(rule1D_.size());
00129   rule1D = rule1D_;
00130 }
00131 
00132 template<class Scalar, class UserVector>
00133 void AdaptiveSparseGridInterface<Scalar,UserVector>::getGrowth(
00134         std::vector<EIntrepidGrowth> & growth1D) {
00135   growth1D.clear();
00136   growth1D.resize(growth1D_.size());
00137   growth1D = growth1D_;
00138 }
00139 
00140 template<class Scalar, class UserVector>
00141 int AdaptiveSparseGridInterface<Scalar,UserVector>::getDimension(void) {
00142   return dimension_;
00143 }
00144 
00145 template<class Scalar, class UserVector>
00146 bool AdaptiveSparseGridInterface<Scalar,UserVector>::isNormalized() {
00147   return isNormalized_;
00148 }
00149 
00150 template<class Scalar, class UserVector>
00151 Scalar AdaptiveSparseGridInterface<Scalar,UserVector>::getInitialDiff() {
00152   return initialDiff_;
00153 }
00154 
00155 } // end Intrepid namespace
00156 
00157 
00158