TSFMatrixLaplacian1D.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // **********************************************************************/
00027  /* @HEADER@ */
00028 
00029 #include "TSFMatrixLaplacian1D.hpp"
00030 #include "TSFEpetraMatrix.hpp"
00031 
00032 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00033 #include "TSFVectorImpl.hpp"
00034 #include "TSFLinearOperatorImpl.hpp"
00035 #endif
00036 
00037 using namespace TSFExtended;
00038 using namespace Teuchos;
00039 
00040 
00041 MatrixLaplacian1D::MatrixLaplacian1D(int nLocalRows, 
00042   const VectorType<double>& type)
00043   : OperatorBuilder<double>(nLocalRows, type), op_()
00044 {
00045   init(nLocalRows, type);
00046 }
00047 
00048 
00049 
00050 
00051 void MatrixLaplacian1D::init(int nLocalRows, 
00052   const VectorType<double>& type)
00053 {
00054   int rank = MPIComm::world().getRank();
00055   int nProc = MPIComm::world().getNProc();
00056   RCP<MatrixFactory<double> > mFact;
00057   mFact = vecType().createMatrixFactory(domain(), range());
00058 
00059   if (domain().dim() == domain().numLocalElements())
00060   {
00061     rank = 0;
00062     nProc = 1;
00063   }
00064 
00065   int lowestLocalRow = nLocalRows * rank;
00066 
00067   IncrementallyConfigurableMatrixFactory* icmf 
00068     = dynamic_cast<IncrementallyConfigurableMatrixFactory*>(mFact.get());
00069   if (icmf)
00070   {
00071     for (int i=0; i<nLocalRows; i++)
00072     {
00073       int row = lowestLocalRow + i;
00074       Array<int> colIndices;
00075       if (rank==0 && i==0)
00076       {
00077         colIndices = tuple(row, row+1);
00078       }
00079       else if (rank==nProc-1 && i==nLocalRows-1)
00080       {
00081         colIndices = tuple(row-1, row);
00082       }
00083       else
00084       {
00085         colIndices = tuple(row-1, row, row+1);
00086       }
00087       icmf->initializeNonzerosInRow(row, colIndices.size(),
00088         &(colIndices[0]));
00089     }
00090     icmf->finalize();
00091   }
00092       
00093   op_ = mFact->createMatrix();
00094 
00095   double h = 1.0/((double) domain().dim() + 1);
00096       
00097   RCP<LoadableMatrix<double> > mat = op_.matrix();
00098 
00099   /* fill in with the Laplacian operator */
00100   for (int i=0; i<nLocalRows; i++)
00101   {
00102     int row = lowestLocalRow + i;
00103     Array<int> colIndices;
00104     Array<double> colVals;
00105     if (rank==0 && i==0)
00106     {
00107       colIndices = tuple(row, row+1);
00108       colVals = tuple(2.0/h/h, -1.0/h/h);
00109     }
00110     else if (rank==nProc-1 && i==nLocalRows-1)
00111     {
00112       colIndices = tuple(row-1, row);
00113       colVals = tuple(-1.0/h/h, 2.0/h/h);
00114     }
00115     else
00116     {
00117       colIndices = tuple(row-1, row, row+1);
00118       colVals = tuple(-1.0/h/h, 2.0/h/h, -1.0/h/h);
00119     }
00120     mat->addToRow(row, colIndices.size(), 
00121       &(colIndices[0]), &(colVals[0]));
00122   }
00123 }

Site Contact