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 
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 
00181     template<size_t MATRIX_SIZE>
00182     static void AddMultipleValues(Mat matrix, unsigned* matrixRowAndColIndices, c_matrix<double, MATRIX_SIZE, MATRIX_SIZE>& rSmallMatrix)
00183     {
00184         PetscInt matrix_row_indices[MATRIX_SIZE];
00185         PetscInt num_rows_owned = 0;
00186         PetscInt global_row;
00187         PetscInt lo, hi;
00188         GetOwnershipRange(matrix, lo, hi);
00189 
00190         for (unsigned row = 0; row<MATRIX_SIZE; row++)
00191         {
00192             global_row = matrixRowAndColIndices[row];
00193             if (global_row >= lo && global_row < hi)
00194             {
00195                 matrix_row_indices[num_rows_owned++] = global_row;
00196             }
00197         }
00198 
00199         if ( num_rows_owned == MATRIX_SIZE)
00200         {
00201             MatSetValues(matrix,
00202                          num_rows_owned,
00203                          matrix_row_indices,
00204                          MATRIX_SIZE,
00205                          (PetscInt*) matrixRowAndColIndices,
00206                          rSmallMatrix.data(),
00207                          ADD_VALUES);
00208         }
00209         else
00210         {
00211             // We need continuous data, if some of the rows do not belong to the processor their values
00212             // are not passed to MatSetValues
00213             double values[MATRIX_SIZE*MATRIX_SIZE];
00214             unsigned num_values_owned = 0;
00215             for (unsigned row = 0; row < MATRIX_SIZE; row++)
00216             {
00217                 global_row = matrixRowAndColIndices[row];
00218                 if (global_row >= lo && global_row < hi)
00219                 {
00220                     for (unsigned col = 0; col < MATRIX_SIZE; col++)
00221                     {
00222                         values[num_values_owned++] = rSmallMatrix(row, col);
00223                     }
00224                 }
00225             }
00226 
00227             MatSetValues(matrix,
00228                          num_rows_owned,
00229                          matrix_row_indices,
00230                          MATRIX_SIZE,
00231                          (PetscInt*) matrixRowAndColIndices,
00232                          values,
00233                          ADD_VALUES);
00234         }
00235     }
00236 
00237 };
00238 
00239 #endif //_PETSCMATTOOLS_HPP_

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5