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
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
00212
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_