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 FillableVec* 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 FillableVec* matrixrow = mat->getRow(row);
00122 
00123           FillableVec::const_iterator
00124             row_iter = matrixrow->begin(),
00125             row_end = matrixrow->end();
00126 
00127           int i=0;
00128           for(; row_iter != row_end; ++row_iter, ++i) {
00129             if (i >= len) break;
00130             coefs[i] = row_iter->second;
00131             indices[i] = row_iter->first;
00132           }
00133         }
00134         catch(...) {
00135           //what should we do here???
00136         }
00137 
00138         return( 0 );
00139       }
00140 
00143     static int putValuesIn(FillableMat* mat,
00144                            int numRows, const int* rows,
00145                            int numCols, const int* cols,
00146                            const double* const* values,
00147                            bool sum_into)
00148       {
00149         if (numCols < 1 || numRows < 1) return(0);
00150         if (sum_into) {
00151           for(int i=0; i<numRows; ++i) {
00152             mat->sumInRow(rows[i], cols, values[i], numCols);
00153           }
00154         }
00155         else {
00156           for(int i=0; i<numRows; ++i) {
00157             mat->putRow(rows[i], cols, values[i], numCols);
00158           }
00159         }
00160 
00161         return( 0 );
00162       }
00163 
00168     static int globalAssemble(FillableMat* mat)
00169     {
00170       return(0);
00171     }
00172 
00174     static int matvec(FillableMat* mat,
00175                       fei::Vector* x,
00176                       fei::Vector* y)
00177     {
00178       fei::Vector_Impl<FillableVec>* fvx =
00179         dynamic_cast<fei::Vector_Impl<FillableVec>* >(x);
00180       fei::Vector_Impl<FillableVec>* fvy =
00181         dynamic_cast<fei::Vector_Impl<FillableVec>* >(y);
00182 
00183       if (fvx == NULL || fvy == NULL) {
00184         return(-1);
00185       }
00186 
00187       fei::CSRMat A(*mat);
00188       fei::CSVec csx(*(fvx->getUnderlyingVector()));
00189       fei::CSVec csy(*(fvy->getUnderlyingVector()));
00190 
00191       fei::multiply_CSRMat_CSVec(A, csx, csy);
00192 
00193       return( 0 );
00194     }
00195 
00196   };//struct MatrixTraits
00197 }//namespace fei
00198 
00199 #endif // _fei_MatrixTraits_FillableMat_hpp_
00200 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends