PlayaMatrixLaplacian1D.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 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 "PlayaMatrixLaplacian1D.hpp"
00043 #include "PlayaEpetraMatrix.hpp"
00044 #include "PlayaIncrementallyConfigurableMatrixFactory.hpp"
00045 
00046 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00047 #include "PlayaVectorImpl.hpp"
00048 #include "PlayaLinearOperatorImpl.hpp"
00049 #endif
00050 
00051 namespace Playa
00052 {
00053 
00054 using namespace Teuchos;
00055 
00056 
00057 MatrixLaplacian1D::MatrixLaplacian1D(int nLocalRows, 
00058   const VectorType<double>& type)
00059   : OperatorBuilder<double>(nLocalRows, type), op_()
00060 {
00061   init(nLocalRows, type);
00062 }
00063 
00064 
00065 
00066 MassMatrix1D::MassMatrix1D(int nLocalRows, 
00067   const VectorType<double>& type)
00068   : OperatorBuilder<double>(nLocalRows, type), op_()
00069 {
00070   init(nLocalRows, type);
00071 }
00072 
00073 
00074 
00075 
00076 void MatrixLaplacian1D::init(int nLocalRows, 
00077   const VectorType<double>& type)
00078 {
00079   int rank = MPIComm::world().getRank();
00080   int nProc = MPIComm::world().getNProc();
00081   RCP<MatrixFactory<double> > mFact;
00082   mFact = vecType().createMatrixFactory(domain(), range());
00083 
00084   if (domain().dim() == domain().numLocalElements())
00085   {
00086     rank = 0;
00087     nProc = 1;
00088   }
00089 
00090   int lowestLocalRow = nLocalRows * rank;
00091 
00092   IncrementallyConfigurableMatrixFactory* icmf 
00093     = dynamic_cast<IncrementallyConfigurableMatrixFactory*>(mFact.get());
00094   if (icmf)
00095   {
00096     for (int i=0; i<nLocalRows; i++)
00097     {
00098       int row = lowestLocalRow + i;
00099       Array<int> colIndices;
00100       if (rank==0 && i==0)
00101       {
00102         colIndices = tuple(row, row+1);
00103       }
00104       else if (rank==nProc-1 && i==nLocalRows-1)
00105       {
00106         colIndices = tuple(row-1, row);
00107       }
00108       else
00109       {
00110         colIndices = tuple(row-1, row, row+1);
00111       }
00112       icmf->initializeNonzerosInRow(row, colIndices.size(),
00113         &(colIndices[0]));
00114     }
00115     icmf->finalize();
00116   }
00117       
00118   op_ = mFact->createMatrix();
00119 
00120   double h = 1.0/((double) domain().dim() + 1);
00121       
00122   RCP<LoadableMatrix<double> > mat = op_.matrix();
00123 
00124   /* fill in with the Laplacian operator */
00125   for (int i=0; i<nLocalRows; i++)
00126   {
00127     int row = lowestLocalRow + i;
00128     Array<int> colIndices;
00129     Array<double> colVals;
00130     if (rank==0 && i==0)
00131     {
00132       colIndices = tuple(row, row+1);
00133       colVals = tuple(2.0/h/h, -1.0/h/h);
00134     }
00135     else if (rank==nProc-1 && i==nLocalRows-1)
00136     {
00137       colIndices = tuple(row-1, row);
00138       colVals = tuple(-1.0/h/h, 2.0/h/h);
00139     }
00140     else
00141     {
00142       colIndices = tuple(row-1, row, row+1);
00143       colVals = tuple(-1.0/h/h, 2.0/h/h, -1.0/h/h);
00144     }
00145     mat->addToRow(row, colIndices.size(), 
00146       &(colIndices[0]), &(colVals[0]));
00147   }
00148 }
00149 
00150 
00151 void MassMatrix1D::init(int nLocalRows, 
00152   const VectorType<double>& type)
00153 {
00154   int rank = MPIComm::world().getRank();
00155   int nProc = MPIComm::world().getNProc();
00156   RCP<MatrixFactory<double> > mFact;
00157   mFact = vecType().createMatrixFactory(domain(), range());
00158 
00159   if (domain().dim() == domain().numLocalElements())
00160   {
00161     rank = 0;
00162     nProc = 1;
00163   }
00164 
00165   int lowestLocalRow = nLocalRows * rank;
00166 
00167   IncrementallyConfigurableMatrixFactory* icmf 
00168     = dynamic_cast<IncrementallyConfigurableMatrixFactory*>(mFact.get());
00169   if (icmf)
00170   {
00171     for (int i=0; i<nLocalRows; i++)
00172     {
00173       int row = lowestLocalRow + i;
00174       Array<int> colIndices;
00175       if (rank==0 && i==0)
00176       {
00177         colIndices = tuple(row, row+1);
00178       }
00179       else if (rank==nProc-1 && i==nLocalRows-1)
00180       {
00181         colIndices = tuple(row-1, row);
00182       }
00183       else
00184       {
00185         colIndices = tuple(row-1, row, row+1);
00186       }
00187       icmf->initializeNonzerosInRow(row, colIndices.size(),
00188         &(colIndices[0]));
00189     }
00190     icmf->finalize();
00191   }
00192       
00193   op_ = mFact->createMatrix();
00194 
00195   double h = 1.0/((double) domain().dim() + 1);
00196       
00197   RCP<LoadableMatrix<double> > mat = op_.matrix();
00198 
00199   /* fill in with the mass operator */
00200   for (int i=0; i<nLocalRows; i++)
00201   {
00202     int row = lowestLocalRow + i;
00203     Array<int> colIndices;
00204     Array<double> colVals;
00205     if (rank==0 && i==0)
00206     {
00207       colIndices = tuple(row, row+1);
00208       colVals = tuple(2.0/3.0, 1.0/3.0);
00209     }
00210     else if (rank==nProc-1 && i==nLocalRows-1)
00211     {
00212       colIndices = tuple(row-1, row);
00213       colVals = tuple(1.0/3.0, 2.0/3.0);
00214     }
00215     else
00216     {
00217       colIndices = tuple(row-1, row, row+1);
00218       colVals = tuple(1.0/3.0, 2.0/3.0, 1.0/3.0);
00219     }
00220     mat->addToRow(row, colIndices.size(), 
00221       &(colIndices[0]), &(colVals[0]));
00222   }
00223 }
00224 
00225 
00226 }

Site Contact