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
00030 #ifndef _PETSCMATTOOLS_HPP_
00031 #define _PETSCMATTOOLS_HPP_
00032
00033 #include "UblasMatrixInclude.hpp"
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
00221
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_