SundanceVertexSort.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 "SundanceVertexSort.hpp"
00043 #include "PlayaExceptions.hpp"
00044 #include "SundanceOut.hpp"
00045 
00046 namespace Sundance
00047 {
00048 using Teuchos::Array;
00049 using Teuchos::tuple;
00050 using std::endl;
00051 
00052 
00053 int mapExSideToMissingVertex(int dim, int exFaceID)
00054 {
00055   static Array<int> map3D = tuple(2, 0, 1, 3);
00056   static Array<int> map2D = tuple(2, 0, 1);
00057   switch(dim)
00058   {
00059     case 3:
00060       return map3D[exFaceID];
00061     case 2:
00062       return map2D[exFaceID];
00063     default:
00064       TEUCHOS_TEST_FOR_EXCEPTION(dim != 3, std::runtime_error,
00065         "dimension " << dim << " not supported "
00066         "in mapExSideToMissingVertex()");
00067   }
00068   return -1; // -Wall;
00069 }
00070 
00071 Array<int> exSideVertPos(int dim, int sideIndex)
00072 {
00073   static Array<Array<int> > faceVerts = tuple<Array<int> >
00074     (
00075       tuple(0,1,3),
00076       tuple(1,2,3),
00077       tuple(0,2,3),
00078       tuple(0,1,2)
00079       );
00080   static Array<Array<int> > edgeVerts = tuple<Array<int> >
00081     (
00082       tuple(0,1),
00083       tuple(1,2),
00084       tuple(0,2)
00085       );
00086   
00087   if (dim==2) return edgeVerts[sideIndex];
00088   else if (dim==3) return faceVerts[sideIndex];
00089   else 
00090   {
00091     TEUCHOS_TEST_FOR_EXCEPT(true);
00092   }
00093   return faceVerts[0]; // -Wall
00094 }
00095 
00096 Array<int> ufcSideVertPos(int dim, int f)
00097 {
00098   static Array<Array<int> > faceVerts = tuple<Array<int> >
00099     (
00100       tuple(1,2,3),
00101       tuple(0,2,3),
00102       tuple(0,1,3),
00103       tuple(0,1,2)
00104       );
00105   static Array<Array<int> > edgeVerts = tuple<Array<int> >
00106     (
00107       tuple(1,2),
00108       tuple(0,2),
00109       tuple(0,1)
00110       );
00111   
00112   if (dim==2) return edgeVerts[f];
00113   else if (dim==3) return faceVerts[f];
00114   else 
00115   {
00116     TEUCHOS_TEST_FOR_EXCEPT(true);
00117   }
00118   return faceVerts[0]; // -Wall
00119 
00120 }
00121 
00122 Array<int> ufcSide(int f, const Array<int>& verts)
00123 {
00124   Array<int> rtn;
00125   for (int i=0; i<verts.size(); i++)
00126   {
00127     if (f==i) continue;
00128     rtn.append(verts[i]);
00129   }
00130   return rtn;
00131 }
00132 
00133 Array<int> exSide(int f, const Array<int>& verts)
00134 {
00135   Array<int> rtn;
00136   Array<int> exPos = exSideVertPos(verts.size()-1, f);
00137   for (int i=0; i<exPos.size(); i++)
00138   {
00139     rtn.append(verts[exPos[i]]);
00140   }
00141   return rtn;
00142 }
00143 
00144 void vertexSort(Array<int>& verts, int* key)
00145 {
00146   Array<std::pair<int,int> > A(verts.size());
00147   for (int i=0; i<verts.size(); i++) 
00148   {
00149     A[i].first = verts[i];
00150     A[i].second = i;
00151   }
00152 
00153   insertionSort(A);
00154 
00155   *key = 0;
00156   int base = verts.size();
00157   int p = 1;
00158   for (int i=verts.size()-1; i>=0; i--) 
00159   {
00160     verts[i] = A[i].first;
00161     *key += p * A[i].second;
00162     p *= base;
00163   }
00164 }
00165 
00166 void getKeyedPerm(int key, Array<int>& digits)
00167 {
00168   int r = key;
00169   int B = digits.size(); 
00170   for (int b=B-1; b>=0; b--)
00171   {
00172     int B_toThe_b = iPow(B,b);
00173     digits[B-b-1] = r/B_toThe_b;
00174     r = r - digits[B-b-1]*B_toThe_b;
00175   }
00176 } 
00177 
00178 /** Compute base^N */
00179 int iPow(int base, int n)
00180 {
00181   int rtn = 1;
00182   for (int i=0; i<n; i++) rtn = rtn*base;
00183   return rtn;
00184 }
00185 
00186 
00187 int exVertPosToUFCVertPos(int meshDim, int permKey, int exVertPos)
00188 {
00189   Array<int> digits(meshDim+1);
00190   getKeyedPerm(permKey, digits);
00191   for (int i=0; i<digits.size(); i++) 
00192   {
00193     if (digits[i] == exVertPos) return i;
00194   }
00195   /* loop should never be completed w/o return */
00196   Out::os() << "vert=" << exVertPos << ", key=" << permKey 
00197             << ", digits = " << digits << endl;
00198   return -1; // indicates an error. Should be checked by client
00199 }
00200 
00201 int exFacetIndexToUFCFacetIndex(int meshDim, int permKey,
00202   int exFacetID)
00203 {
00204   int missingExVert = mapExSideToMissingVertex(meshDim, exFacetID);
00205   int missingUFCVert = exVertPosToUFCVertPos(meshDim, permKey, missingExVert);
00206   return missingUFCVert; // returns -1 if error
00207 }
00208 
00209 
00210 int ufcFacetIndexToExFacetIndex(int meshDim, int ufcFacetIndex)
00211 {
00212   static Array<int> map2D = tuple(1,2,0);
00213   static Array<int> map3D = tuple(1,2,0,3);
00214 
00215   if (meshDim==2)
00216   {
00217     return map2D[ufcFacetIndex];
00218   }
00219   else if (meshDim==3)
00220   {
00221     return map3D[ufcFacetIndex];
00222   }
00223   else 
00224   {
00225     TEUCHOS_TEST_FOR_EXCEPT( meshDim!=3 && meshDim!=2 );
00226   }
00227   TEUCHOS_TEST_FOR_EXCEPT(true);
00228   return -1; // -Wall
00229 }
00230 
00231 }
00232 
00233 
00234 

Site Contact