Chaste  Release::3.4
PetscMatTools.hpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef _PETSCMATTOOLS_HPP_
37 #define _PETSCMATTOOLS_HPP_
38 
39 #include "UblasMatrixInclude.hpp" // needs to be 'first'
40 #include "PetscTools.hpp"
41 #include <vector>
42 #include <petscvec.h>
43 #include <petscmat.h>
44 
49 {
50 public:
51 
60  static void SetElement(Mat matrix, PetscInt row, PetscInt col, double value);
61 
70  static void AddToElement(Mat matrix, PetscInt row, PetscInt col, double value);
71 
77  static void Finalise(Mat matrix);
78 
85  static void SwitchWriteMode(Mat matrix);
86 
92  static void Display(Mat matrix);
93 
103  static void SetRow(Mat matrix, PetscInt row, double value);
104 
114  static void ZeroRowsWithValueOnDiagonal(Mat matrix, std::vector<unsigned>& rRows, double diagonalValue);
115 
125  static void ZeroRowsAndColumnsWithValueOnDiagonal(Mat matrix, std::vector<unsigned> rowColIndices, double diagonalValue);
126 
137  static void ZeroColumn(Mat matrix, PetscInt col);
138 
143  static void Zero(Mat matrix);
144 
149  static unsigned GetSize(Mat matrix);
150 
158  static void GetOwnershipRange(Mat matrix, PetscInt& lo, PetscInt& hi);
159 
168  static double GetElement(Mat matrix, PetscInt row, PetscInt col);
169 
176  static void SetOption(Mat matrix, MatOption option);
177 
185  static Vec GetMatrixRowDistributed(Mat matrix, unsigned rowIndex);
186 
194  static bool CheckEquality(const Mat mat1, const Mat mat2, double tol=1e-10);
195 
210  static bool CheckSymmetry(const Mat matrix, double tol=1e-10);
211 
224  static void TurnOffVariableAllocationError(Mat matrix);
225 
236  template<size_t MATRIX_SIZE>
237  static void AddMultipleValues(Mat matrix, unsigned* matrixRowAndColIndices, c_matrix<double, MATRIX_SIZE, MATRIX_SIZE>& rSmallMatrix)
238  {
239  PetscInt matrix_row_indices[MATRIX_SIZE];
240  PetscInt num_rows_owned = 0;
241  PetscInt global_row;
242  PetscInt lo, hi;
243  GetOwnershipRange(matrix, lo, hi);
244 
245  for (unsigned row = 0; row<MATRIX_SIZE; row++)
246  {
247  global_row = matrixRowAndColIndices[row];
248  if (global_row >= lo && global_row < hi)
249  {
250  matrix_row_indices[num_rows_owned++] = global_row;
251  }
252  }
253 
254  if ( num_rows_owned == MATRIX_SIZE)
255  {
256  MatSetValues(matrix,
257  num_rows_owned,
258  matrix_row_indices,
259  MATRIX_SIZE,
260  (PetscInt*) matrixRowAndColIndices,
261  rSmallMatrix.data(),
262  ADD_VALUES);
263  }
264  else
265  {
266  // We need continuous data, if some of the rows do not belong to the processor their values
267  // are not passed to MatSetValues
268  double values[MATRIX_SIZE*MATRIX_SIZE];
269  unsigned num_values_owned = 0;
270  for (unsigned row = 0; row < MATRIX_SIZE; row++)
271  {
272  global_row = matrixRowAndColIndices[row];
273  if (global_row >= lo && global_row < hi)
274  {
275  for (unsigned col = 0; col < MATRIX_SIZE; col++)
276  {
277  values[num_values_owned++] = rSmallMatrix(row, col);
278  }
279  }
280  }
281 
282  MatSetValues(matrix,
283  num_rows_owned,
284  matrix_row_indices,
285  MATRIX_SIZE,
286  (PetscInt*) matrixRowAndColIndices,
287  values,
288  ADD_VALUES);
289  }
290  }
291 };
292 
293 #endif //_PETSCMATTOOLS_HPP_
static void AddMultipleValues(Mat matrix, unsigned *matrixRowAndColIndices, c_matrix< double, MATRIX_SIZE, MATRIX_SIZE > &rSmallMatrix)
static void TurnOffVariableAllocationError(Mat matrix)
static void GetOwnershipRange(Mat matrix, PetscInt &lo, PetscInt &hi)
static void AddToElement(Mat matrix, PetscInt row, PetscInt col, double value)
static Vec GetMatrixRowDistributed(Mat matrix, unsigned rowIndex)
static void Zero(Mat matrix)
static void Display(Mat matrix)
static double GetElement(Mat matrix, PetscInt row, PetscInt col)
static bool CheckEquality(const Mat mat1, const Mat mat2, double tol=1e-10)
static void SetElement(Mat matrix, PetscInt row, PetscInt col, double value)
static void ZeroRowsWithValueOnDiagonal(Mat matrix, std::vector< unsigned > &rRows, double diagonalValue)
static void ZeroRowsAndColumnsWithValueOnDiagonal(Mat matrix, std::vector< unsigned > rowColIndices, double diagonalValue)
static void SwitchWriteMode(Mat matrix)
static bool CheckSymmetry(const Mat matrix, double tol=1e-10)
static void SetOption(Mat matrix, MatOption option)
static void Finalise(Mat matrix)
static unsigned GetSize(Mat matrix)
static void SetRow(Mat matrix, PetscInt row, double value)
static void ZeroColumn(Mat matrix, PetscInt col)