SundancePointwiseUserDefFunctor.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 "SundancePointwiseUserDefFunctor.hpp"
00043 #include "PlayaTabs.hpp"
00044 #include "SundanceOut.hpp"
00045 
00046 using namespace Sundance;
00047 using namespace Sundance;
00048 
00049 using namespace Sundance;
00050 using namespace Teuchos;
00051 
00052 
00053 PointwiseUserDefFunctor0::PointwiseUserDefFunctor0(const std::string& name, 
00054                                                    int domainDim, 
00055                                                    int rangeDim)
00056   :  UserDefFunctor(name, domainDim, rangeDim)
00057 {}
00058 
00059 void PointwiseUserDefFunctor0::evaluationCallback(int nPoints, int maxDiffOrder,
00060                                     const double** in,
00061                                     double** out) const 
00062 {
00063   TEUCHOS_TEST_FOR_EXCEPTION(maxDiffOrder > 0, std::runtime_error,
00064                      "diff order = " << maxDiffOrder 
00065                      << " not supported for functor "
00066                      << name());
00067 
00068   static Array<double> x;
00069   x.resize(domainDim());
00070   
00071   static Array<double> f;
00072   f.resize(rangeDim());
00073 
00074   for (int i=0; i<nPoints; i++)
00075     {
00076       for (int j=0; j<domainDim(); j++) x[j] = in[j][i];
00077       const double* xp = &(x[0]);
00078       double* fp = &(f[0]);
00079       eval0(xp, fp);
00080       for (int j=0; j<rangeDim(); j++) out[j][i] = fp[j];
00081     }
00082 }
00083 
00084 PointwiseUserDefFunctor1::PointwiseUserDefFunctor1(const std::string& name, 
00085                                                    int domainDim, 
00086                                                    int rangeDim)
00087   : PointwiseUserDefFunctor0(name, domainDim, rangeDim)
00088 {}
00089 
00090 void PointwiseUserDefFunctor1::evaluationCallback(int nPoints, int maxDiffOrder,
00091                                     const double** in,
00092                                     double** out) const 
00093 {
00094   TEUCHOS_TEST_FOR_EXCEPTION(maxDiffOrder > 1, std::runtime_error,
00095                      "diff order = " << maxDiffOrder 
00096                      << " not supported for functor "
00097                      << name());
00098 
00099   static Array<double> x;
00100   x.resize(domainDim());
00101   
00102   static Array<double> f;
00103   if (maxDiffOrder==1) f.resize(rangeDim() * (1 + domainDim()) );
00104   else f.resize(rangeDim());
00105   double* fp = &(f[0]);
00106 
00107   for (int i=0; i<nPoints; i++)
00108     {
00109       for (int j=0; j<domainDim(); j++) x[j] = in[j][i];
00110       const double* xp = &(x[0]);
00111 
00112       if (maxDiffOrder==1) 
00113         {
00114           double* dfp = &(f[rangeDim()]);
00115           eval1(xp, fp, dfp);
00116         }
00117       else eval0(xp, fp);
00118       for (int j=0; j<f.size(); j++) out[j][i] = fp[j];
00119     }
00120 }
00121 
00122 
00123 void PointwiseUserDefFunctor1::eval0(const double* in, double* out) const 
00124 {
00125   static Array<double> dummy;
00126   dummy.resize(domainDim() * rangeDim());
00127 
00128   eval1(in, out, &(dummy[0]));
00129 }
00130 
00131 
00132 
00133 PointwiseUserDefFunctor2::PointwiseUserDefFunctor2(const std::string& name, 
00134                                                    int domainDim, 
00135                                                    int rangeDim)
00136   : PointwiseUserDefFunctor1(name, domainDim, rangeDim)
00137 {}
00138 
00139 void PointwiseUserDefFunctor2::evaluationCallback(int nPoints, int maxDiffOrder,
00140                                                   const double** in,
00141                                                   double** out) const 
00142 {
00143   TEUCHOS_TEST_FOR_EXCEPTION(maxDiffOrder > 2 || maxDiffOrder < 0, std::runtime_error,
00144                      "diff order = " << maxDiffOrder 
00145                      << " not supported for functor "
00146                      << name());
00147 
00148   int nTotal = 1;
00149   int numFirst = domainDim();
00150   int numSecond = domainDim()*(domainDim()+1)/2;
00151 
00152   static Array<double> x;
00153   x.resize(domainDim());
00154   
00155   static Array<double> f;
00156 
00157   if (maxDiffOrder > 0) nTotal += numFirst;
00158   if (maxDiffOrder > 1) nTotal += numSecond;
00159 
00160   f.resize(rangeDim() * nTotal);
00161 
00162   double* fp = &(f[0]);
00163 
00164   for (int i=0; i<nPoints; i++)
00165     {
00166       for (int j=0; j<domainDim(); j++) x[j] = in[j][i];
00167       const double* xp = &(x[0]);
00168 
00169       if (maxDiffOrder==0)
00170         {
00171           eval0(xp, fp);
00172         }
00173       else if (maxDiffOrder==1)
00174         {
00175           double* dfp = &(f[rangeDim()]);
00176           eval1(xp, fp, dfp);
00177         }
00178       else if (maxDiffOrder==2)
00179         {
00180           double* dfp = &(f[rangeDim()]);
00181           double* d2fp = &(f[rangeDim()*(1 + domainDim())]);
00182           eval2(xp, fp, dfp, d2fp);
00183         }
00184       else
00185         {
00186           TEUCHOS_TEST_FOR_EXCEPT(true);
00187         }
00188       for (int j=0; j<f.size(); j++) out[j][i] = fp[j];
00189     }
00190 }
00191 
00192 
00193 void PointwiseUserDefFunctor2::eval0(const double* in, double* f) const 
00194 {
00195   static Array<double> dummy1;
00196   static Array<double> dummy2;
00197   dummy1.resize(rangeDim() *  domainDim());
00198   dummy2.resize(rangeDim() *  domainDim()*(domainDim()+1)/2);
00199 
00200   eval2(in, f, &(dummy1[0]), &(dummy2[0]));
00201 }
00202 
00203 
00204 void PointwiseUserDefFunctor2::eval1(const double* in, double* f, double* df) const 
00205 {
00206   static Array<double> dummy2;
00207   dummy2.resize(rangeDim() *  domainDim()*(domainDim()+1)/2);
00208 
00209   eval2(in, f, df, &(dummy2[0]));
00210 }

Site Contact