SundanceVectorCalculus.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 "SundanceVectorCalculus.hpp"
00043 #include "SundanceDerivative.hpp"
00044 #include "SundanceListExpr.hpp"
00045 
00046 
00047 namespace Sundance
00048 {
00049 
00050 Expr gradient(int dim)
00051 {
00052   Array<Expr> rtn(dim);
00053   for (int i=0; i<dim; i++)
00054   {
00055     rtn[i] = new Derivative(i);
00056   }
00057   return new ListExpr(rtn);
00058 }
00059 
00060 Expr div(const Expr& f)
00061 {
00062   Expr rtn = 0.0;
00063   for (int i=0; i<f.size(); i++)
00064   {
00065     Expr d = new Derivative(i);
00066     rtn = rtn + d*f[i];
00067   }
00068   return rtn;
00069 }
00070 
00071 
00072 Expr cross(const Expr& a, const Expr& b)
00073 {
00074   TEUCHOS_TEST_FOR_EXCEPTION(a.size() != b.size(), std::runtime_error,
00075     "mismatched vector sizes in cross(a,b): a.size()=" << a.size()
00076     << ", b.size()=" << b.size());
00077 
00078   TEUCHOS_TEST_FOR_EXCEPTION(a.size() < 2 || a.size() > 3, std::runtime_error,
00079     "cross(a,b) undefined for dim=" << a.size());
00080 
00081   if (a.size()==2)
00082   {
00083     return a[0]*b[1] - a[1]*b[0];
00084   }
00085   else
00086   {
00087     return List(
00088       cross(List(a[1],a[2]), List(b[1],b[2])),
00089       -cross(List(a[0],a[2]), List(b[0],b[2])),
00090       cross(List(a[0],a[1]), List(b[0],b[1]))
00091       );
00092   }
00093 }
00094 
00095 Expr curl(const Expr& f)
00096 {
00097   Expr del = gradient(f.size());
00098 
00099   return cross(del, f);
00100 }
00101 
00102 Expr colonProduct(const Expr& A, const Expr& B)
00103 {
00104   int nA = 0;
00105   int nB = 0;
00106   TEUCHOS_TEST_FOR_EXCEPTION(!isSquareMatrix(A, nA), std::runtime_error,
00107     "Colon product expected argument A=" << A << " to be a square matrix");
00108   TEUCHOS_TEST_FOR_EXCEPTION(!isSquareMatrix(B, nB), std::runtime_error,
00109     "Colon product expected argument B=" << B << " to be a square matrix");
00110 
00111   TEUCHOS_TEST_FOR_EXCEPTION(nA!=nB, std::runtime_error,
00112     "Colon product expected operands A=" << A << " and B=" << B 
00113     << " to have identical sizes");
00114 
00115   Expr rtn = 0.0;
00116   for (int i=0; i<nA; i++)
00117   {
00118     for (int j=0; j<nA; j++)
00119     {
00120       rtn = rtn + A[i][j] * B[i][j];
00121     }
00122   }
00123 
00124   return rtn;
00125 }
00126 
00127 Expr outerProduct(const Expr& A, const Expr& B)
00128 {
00129   int nA = 0;
00130   int nB = 0;
00131   TEUCHOS_TEST_FOR_EXCEPTION(!isVector(A, nA), std::runtime_error,
00132     "Outer product expected argument A=" << A << " to be a vector");
00133   TEUCHOS_TEST_FOR_EXCEPTION(!isVector(B, nB), std::runtime_error,
00134     "Outer product expected argument B=" << B << " to be a vector");
00135 
00136   TEUCHOS_TEST_FOR_EXCEPTION(nA!=nB, std::runtime_error,
00137     "Colon product expected operands A=" << A << " and B=" << B 
00138     << " to have identical sizes");
00139 
00140   Array<Expr> rtn(nA);
00141   for (int i=0; i<nA; i++)
00142   {
00143     rtn[i] = A[i] * B;
00144   }
00145 
00146   return new ListExpr(rtn);
00147 }
00148 
00149 bool isVector(const Expr& x, int& N)
00150 {
00151   N = 0;
00152   if (x.size() == x.totalSize()) 
00153   {
00154     N = x.size();
00155     return true;
00156   }
00157   else
00158   {
00159     return false;
00160   }
00161 }
00162 
00163 
00164 bool isSquareMatrix(const Expr& x, int& N)
00165 {
00166   N = 0;
00167   /* do the simplest checks first */
00168   if (x.size()*x.size() != x.totalSize()) return false;
00169   N = x.size();
00170   for (int i=0; i<N; i++)
00171   {
00172     int M = 0;
00173     if (!isVector(x[i],M)) return false;
00174     if (M != N) return false;
00175   }
00176   return true;
00177 }
00178 
00179 
00180 
00181 
00182 }
00183 

Site Contact