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 #ifndef _PETSCMATTOOLS_HPP_
00030 #define _PETSCMATTOOLS_HPP_
00031 
00032 #include "UblasMatrixInclude.hpp" // needs to be 'first'
00033 #include "PetscTools.hpp"
00034 #include <vector>
00035 #include <petscvec.h>
00036 #include <petscmat.h>
00037 
00041 class PetscMatTools
00042 {
00043 public:
00044 
00053     static void SetElement(Mat matrix, PetscInt row, PetscInt col, double value);
00054 
00063     static void AddToElement(Mat matrix, PetscInt row, PetscInt col, double value);
00064 
00070     static void Finalise(Mat matrix);
00071 
00078     static void SwitchWriteMode(Mat matrix);
00079 
00085     static void Display(Mat matrix);
00086 
00096     static void SetRow(Mat matrix, PetscInt row, double value);
00097 
00107     static void ZeroRowsWithValueOnDiagonal(Mat matrix, std::vector<unsigned>& rRows, double diagonalValue);
00108 
00117     static void ZeroRowsAndColumnsWithValueOnDiagonal(Mat matrix, std::vector<unsigned>& rRowColIndices, double diagonalValue);
00118 
00129     static void ZeroColumn(Mat matrix, PetscInt col);
00130 
00135     static void Zero(Mat matrix);
00136 
00141     static unsigned GetSize(Mat matrix);
00142 
00150     static void GetOwnershipRange(Mat matrix, PetscInt& lo, PetscInt& hi);
00151 
00160     static double GetElement(Mat matrix, PetscInt row, PetscInt col);
00161 
00168     static void SetOption(Mat matrix, MatOption option);
00169 
00177     static Vec GetMatrixRowDistributed(Mat matrix, unsigned rowIndex);
00178 
00186     static bool CheckEquality(const Mat mat1, const Mat mat2, double tol=1e-10);
00187 
00201     static bool CheckSymmetry(const Mat matrix, double tol=1e-10);
00202 
00213     template<size_t MATRIX_SIZE>
00214     static void AddMultipleValues(Mat matrix, unsigned* matrixRowAndColIndices, c_matrix<double, MATRIX_SIZE, MATRIX_SIZE>& rSmallMatrix)
00215     {
00216         PetscInt matrix_row_indices[MATRIX_SIZE];
00217         PetscInt num_rows_owned = 0;
00218         PetscInt global_row;
00219         PetscInt lo, hi;
00220         GetOwnershipRange(matrix, lo, hi);
00221 
00222         for (unsigned row = 0; row<MATRIX_SIZE; row++)
00223         {
00224             global_row = matrixRowAndColIndices[row];
00225             if (global_row >= lo && global_row < hi)
00226             {
00227                 matrix_row_indices[num_rows_owned++] = global_row;
00228             }
00229         }
00230 
00231         if ( num_rows_owned == MATRIX_SIZE)
00232         {
00233             MatSetValues(matrix,
00234                          num_rows_owned,
00235                          matrix_row_indices,
00236                          MATRIX_SIZE,
00237                          (PetscInt*) matrixRowAndColIndices,
00238                          rSmallMatrix.data(),
00239                          ADD_VALUES);
00240         }
00241         else
00242         {
00243             // We need continuous data, if some of the rows do not belong to the processor their values
00244             // are not passed to MatSetValues
00245             double values[MATRIX_SIZE*MATRIX_SIZE];
00246             unsigned num_values_owned = 0;
00247             for (unsigned row = 0; row < MATRIX_SIZE; row++)
00248             {
00249                 global_row = matrixRowAndColIndices[row];
00250                 if (global_row >= lo && global_row < hi)
00251                 {
00252                     for (unsigned col = 0; col < MATRIX_SIZE; col++)
00253                     {
00254                         values[num_values_owned++] = rSmallMatrix(row, col);
00255                     }
00256                 }
00257             }
00258 
00259             MatSetValues(matrix,
00260                          num_rows_owned,
00261                          matrix_row_indices,
00262                          MATRIX_SIZE,
00263                          (PetscInt*) matrixRowAndColIndices,
00264                          values,
00265                          ADD_VALUES);
00266         }
00267     }
00268 };
00269 
00270 #endif //_PETSCMATTOOLS_HPP_
Generated on Thu Dec 22 13:00:07 2011 for Chaste by  doxygen 1.6.3