PetscMatTools.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #ifndef _PETSCMATTOOLS_HPP_
00031 #define _PETSCMATTOOLS_HPP_
00032 
00033 #include "UblasMatrixInclude.hpp" // needs to be 'first'
00034 #include "PetscTools.hpp"
00035 #include <vector>
00036 #include <petscvec.h>
00037 #include <petscmat.h>
00038 
00042 class PetscMatTools
00043 {
00044 public:
00045 
00054     static void SetElement(Mat matrix, PetscInt row, PetscInt col, double value);
00055 
00064     static void AddToElement(Mat matrix, PetscInt row, PetscInt col, double value);
00065 
00071     static void AssembleFinal(Mat matrix);
00072 
00079     static void AssembleIntermediate(Mat matrix);
00080 
00086     static void Display(Mat matrix);
00087 
00097     static void SetRow(Mat matrix, PetscInt row, double value);
00098 
00108     static void ZeroRowsWithValueOnDiagonal(Mat matrix, std::vector<unsigned>& rRows, double diagonalValue);
00109 
00118     static void ZeroRowsAndColumnsWithValueOnDiagonal(Mat matrix, std::vector<unsigned>& rRowColIndices, double diagonalValue);
00119 
00130     static void ZeroColumn(Mat matrix, PetscInt col);
00131 
00136     static void Zero(Mat matrix);
00137 
00142     static unsigned GetSize(Mat matrix);
00143 
00151     static void GetOwnershipRange(Mat matrix, PetscInt& lo, PetscInt& hi);
00152 
00161     static double GetElement(Mat matrix, PetscInt row, PetscInt col);
00162     
00169     static void SetOption(Mat matrix, MatOption option);
00170 
00178     static Vec GetMatrixRowDistributed(Mat matrix, unsigned rowIndex);
00179 
00190     template<size_t MATRIX_SIZE>
00191     static void AddMultipleValues(Mat matrix, unsigned* matrixRowAndColIndices, c_matrix<double, MATRIX_SIZE, MATRIX_SIZE>& rSmallMatrix)
00192     {
00193         PetscInt matrix_row_indices[MATRIX_SIZE];
00194         PetscInt num_rows_owned = 0;
00195         PetscInt global_row;
00196         PetscInt lo, hi;
00197         GetOwnershipRange(matrix, lo, hi);
00198 
00199         for (unsigned row = 0; row<MATRIX_SIZE; row++)
00200         {
00201             global_row = matrixRowAndColIndices[row];
00202             if (global_row >= lo && global_row < hi)
00203             {
00204                 matrix_row_indices[num_rows_owned++] = global_row;
00205             }
00206         }
00207 
00208         if ( num_rows_owned == MATRIX_SIZE)
00209         {
00210             MatSetValues(matrix,
00211                          num_rows_owned,
00212                          matrix_row_indices,
00213                          MATRIX_SIZE,
00214                          (PetscInt*) matrixRowAndColIndices,
00215                          rSmallMatrix.data(),
00216                          ADD_VALUES);
00217         }
00218         else
00219         {
00220             // We need continuous data, if some of the rows do not belong to the processor their values
00221             // are not passed to MatSetValues
00222             double values[MATRIX_SIZE*MATRIX_SIZE];
00223             unsigned num_values_owned = 0;
00224             for (unsigned row = 0; row < MATRIX_SIZE; row++)
00225             {
00226                 global_row = matrixRowAndColIndices[row];
00227                 if (global_row >= lo && global_row < hi)
00228                 {
00229                     for (unsigned col = 0; col < MATRIX_SIZE; col++)
00230                     {
00231                         values[num_values_owned++] = rSmallMatrix(row, col);
00232                     }
00233                 }
00234             }
00235 
00236             MatSetValues(matrix,
00237                          num_rows_owned,
00238                          matrix_row_indices,
00239                          MATRIX_SIZE,
00240                          (PetscInt*) matrixRowAndColIndices,
00241                          values,
00242                          ADD_VALUES);
00243         }
00244     }
00245 
00246 };
00247 
00248 #endif //_PETSCMATTOOLS_HPP_

Generated on Tue May 31 14:31:42 2011 for Chaste by  doxygen 1.5.5