SundanceDiscreteFunctionStub.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 "SundanceDiscreteFunctionStub.hpp"
00043 #include "SundanceDiscreteFuncElement.hpp"
00044 #include "SundanceSpectralBasis.hpp"
00045 #include "SundanceSpectralExpr.hpp"
00046 
00047 
00048 using namespace Sundance;
00049 using namespace Sundance;
00050 
00051 using namespace Sundance;
00052 using namespace Teuchos;
00053 
00054 
00055 
00056 DiscreteFunctionStub::DiscreteFunctionStub(const std::string& name, 
00057   int tensorOrder,
00058   int dim, 
00059   const RCP<DiscreteFuncDataStub>& data,
00060   int listIndex)
00061   : ListExpr(), data_(data)
00062 {
00063   initTensor(name, tensorOrder, dim, data,  listIndex);
00064 }
00065 
00066 void DiscreteFunctionStub::initTensor(const std::string& name, 
00067   int tensorOrder,
00068   int dim, 
00069   const RCP<DiscreteFuncDataStub>& data,
00070   int listIndex)
00071 {
00072   FunctionIdentifier myFid = makeFuncID(tensorOrder);
00073   if (tensorOrder==0)
00074   {
00075     append(new DiscreteFuncElement(data, name, "", myFid, listIndex));
00076   }
00077   else if (tensorOrder==1)
00078   {
00079     for (int d=0; d<dim; d++)
00080     {
00081       std::string suffix="[" + Teuchos::toString(d) + "]";
00082       FunctionIdentifier fid = myFid.createComponent(d);
00083       append(new DiscreteFuncElement(data, name, suffix, fid, listIndex));
00084     }
00085   }
00086   else 
00087   {
00088     TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "tensor order = " << tensorOrder
00089       << " not supported");
00090   }
00091 }
00092 
00093 
00094 void DiscreteFunctionStub::initTensorSpectral(const std::string& name, 
00095   const SpectralBasis& sbasis, 
00096   int tensorOrder,
00097   int dim, 
00098   const RCP<DiscreteFuncDataStub>& data,
00099   int listIndexOffset)
00100 {
00101   Array<FunctionIdentifier> cFid(sbasis.nterms());
00102   Array<int> listIndex(sbasis.nterms());
00103 
00104   for (int n=0; n<sbasis.nterms(); n++)
00105   {
00106     cFid[n] = makeFuncID(tensorOrder);
00107     listIndex[n] = listIndexOffset + n;
00108   }
00109   
00110   if (tensorOrder==0 || dim==1)
00111   {
00112     Array<Expr> coeffs(sbasis.nterms());
00113     for (int n=0; n<sbasis.nterms(); n++)
00114     {
00115       std::string suffix="";
00116       if (sbasis.nterms()>1) suffix = "[" + Teuchos::toString(n) + "]";
00117       coeffs[n] = new DiscreteFuncElement(data, name, suffix, cFid[n], listIndex[n]);
00118     }
00119     append(new SpectralExpr(sbasis, coeffs));
00120   }
00121   else if (tensorOrder==1)
00122   {
00123     for (int d=0; d<dim; d++)
00124     {
00125       std::string suffix="[" + Teuchos::toString(d) + "]";
00126       Array<Expr> coeffs(sbasis.nterms());
00127       for (int n=0; n<sbasis.nterms(); n++)
00128       {
00129         FunctionIdentifier fid = cFid[n].createComponent(d);
00130         if (sbasis.nterms()>1) suffix += "[" + Teuchos::toString(n) + "]";
00131         coeffs[n]= new DiscreteFuncElement(data, name, suffix, fid, listIndex[n]);
00132       }
00133       append(new SpectralExpr(sbasis, coeffs));
00134     }
00135   }
00136   else 
00137   {
00138     TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "tensor order = " << tensorOrder
00139       << " not supported");
00140   }
00141 }
00142 
00143 
00144 
00145 
00146 DiscreteFunctionStub::DiscreteFunctionStub(const Array<string>& name, 
00147   const Array<std::pair<int,int> >& tensorStructure,
00148   const RCP<DiscreteFuncDataStub>& data)
00149   : ListExpr(), data_(data)
00150 {
00151   TEUCHOS_TEST_FOR_EXCEPT(name.size() != tensorStructure.size() && name.size()!=1);
00152 
00153   if (tensorStructure.size()==1)
00154   {
00155     int tensorOrder = tensorStructure[0].first;
00156     int dim = tensorStructure[0].second;
00157     initTensor(name[0], tensorOrder, dim, data, 0);
00158   }
00159   else
00160   {
00161     for (int i=0; i<tensorStructure.size(); i++)
00162     {
00163       std::string nm;
00164       if (name.size()==1) nm = name[0] + "[" + Teuchos::toString(i) + "]";
00165       else nm = name[i];
00166       append(new DiscreteFunctionStub(
00167                nm,
00168                tensorStructure[i].first,
00169                tensorStructure[i].second,
00170                data, i));
00171     }
00172   }
00173 }
00174 
00175 
00176 
00177 DiscreteFunctionStub::DiscreteFunctionStub(const std::string& name, 
00178   const SpectralBasis& sbasis, int tensorOrder, int dim,
00179   const RCP<DiscreteFuncDataStub>& data,
00180   int listIndex)
00181   : ListExpr(), data_(data)
00182 {
00183   initTensorSpectral(name, sbasis, tensorOrder, dim, data, listIndex);
00184 }
00185 
00186 
00187 
00188 DiscreteFunctionStub::DiscreteFunctionStub(const Array<string>& name, 
00189   const SpectralBasis& sbasis,  
00190   const Array<std::pair<int,int> >& tensorStructure,
00191   const RCP<DiscreteFuncDataStub>& data)
00192   : ListExpr(), data_(data)
00193 {
00194   TEUCHOS_TEST_FOR_EXCEPT(name.size() != tensorStructure.size());
00195    if (tensorStructure.size()==1)
00196   {
00197     int tensorOrder = tensorStructure[0].first;
00198     int dim = tensorStructure[0].second;
00199     initTensorSpectral(name[0], sbasis, tensorOrder, dim, data, 0);
00200   }
00201   else
00202   {
00203     for (int i=0; i<tensorStructure.size(); i++)
00204     {
00205       std::string nm;
00206       if (name.size()==1) nm = name[0] + "[" + Teuchos::toString(i) + "]";
00207       else nm = name[i];
00208       append(new DiscreteFunctionStub(
00209                nm,
00210                sbasis,
00211                tensorStructure[i].first,
00212                tensorStructure[i].second,
00213                data, i*sbasis.nterms()));
00214     }
00215   }
00216 }
00217 
00218 

Site Contact