PetscMatTools.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _PETSCMATTOOLS_HPP_
00030 #define _PETSCMATTOOLS_HPP_
00031
00032 #include "UblasMatrixInclude.hpp"
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
00244
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_