FEI Version of the Day
fei_MatrixTraits_FillableMat.hpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // 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 Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 
00044 #ifndef _fei_MatrixTraits_FillableMat_hpp_
00045 #define _fei_MatrixTraits_FillableMat_hpp_
00046 
00047 //This file defines matrix traits for fei::FillableMat matrices
00048 //
00049 
00050 #include <fei_CSRMat.hpp>
00051 #include <fei_CSVec.hpp>
00052 #include <fei_Vector_Impl.hpp>
00053 
00054 namespace fei {
00055 
00057   template<>
00058   struct MatrixTraits<FillableMat> {
00059 
00061     static const char* typeName()
00062       { return("FillableMat"); }
00063 
00064     static double* getBeginPointer(FillableMat* /*mat*/)
00065       {
00066         return NULL;
00067       }
00068 
00069     static int getOffset(FillableMat* /*mat*/, int /*row*/, int /*col*/)
00070       {
00071         return -1;
00072       }
00073 
00076     static int setValues(FillableMat* mat, double scalar)
00077       {
00078         mat->setValues(scalar);
00079         return(0);
00080       }
00081 
00085     static int getNumLocalRows(FillableMat* mat, int& numRows)
00086     {
00087       numRows = mat->getNumRows();
00088       return(0);
00089     }
00090 
00093     static int getRowLength(FillableMat* mat, int row, int& length)
00094       {
00095         try {
00096           const CSVec* matrixrow = mat->getRow(row);
00097           length = matrixrow->size();
00098         }
00099         catch(...) {
00100           length = 0;
00101         }
00102         return( 0 );
00103       }
00104 
00117     static int copyOutRow(FillableMat* mat,
00118                       int row, int len, double* coefs, int* indices)
00119       {
00120         try {
00121           const CSVec* matrixrow = mat->getRow(row);
00122 
00123           const std::vector<int>& row_indices = matrixrow->indices();
00124           const std::vector<double>& row_coefs = matrixrow->coefs();
00125           const int rowlen = row_indices.size();
00126           for(int i=0; i<rowlen; ++i) {
00127             if (i >= len) break;
00128             coefs[i] = row_coefs[i];
00129             indices[i] = row_indices[i];
00130           }
00131         }
00132         catch(...) {
00133           //what should we do here???
00134         }
00135 
00136         return( 0 );
00137       }
00138 
00141     static int putValuesIn(FillableMat* mat,
00142                            int numRows, const int* rows,
00143                            int numCols, const int* cols,
00144                            const double* const* values,
00145                            bool sum_into)
00146       {
00147         if (numCols < 1 || numRows < 1) return(0);
00148         if (sum_into) {
00149           for(int i=0; i<numRows; ++i) {
00150             mat->sumInRow(rows[i], cols, values[i], numCols);
00151           }
00152         }
00153         else {
00154           for(int i=0; i<numRows; ++i) {
00155             mat->putRow(rows[i], cols, values[i], numCols);
00156           }
00157         }
00158 
00159         return( 0 );
00160       }
00161 
00166     static int globalAssemble(FillableMat* mat)
00167     {
00168       return(0);
00169     }
00170 
00172     static int matvec(FillableMat* mat,
00173                       fei::Vector* x,
00174                       fei::Vector* y)
00175     {
00176       fei::Vector_Impl<CSVec>* fvx =
00177         dynamic_cast<fei::Vector_Impl<CSVec>* >(x);
00178       fei::Vector_Impl<CSVec>* fvy =
00179         dynamic_cast<fei::Vector_Impl<CSVec>* >(y);
00180 
00181       if (fvx == NULL || fvy == NULL) {
00182         return(-1);
00183       }
00184 
00185       fei::CSRMat A(*mat);
00186       fei::CSVec csx(*(fvx->getUnderlyingVector()));
00187       fei::CSVec csy(*(fvy->getUnderlyingVector()));
00188 
00189       fei::multiply_CSRMat_CSVec(A, csx, csy);
00190 
00191       return( 0 );
00192     }
00193 
00194   };//struct MatrixTraits
00195 }//namespace fei
00196 
00197 #endif // _fei_MatrixTraits_FillableMat_hpp_
00198 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends