Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
PetscMatTools.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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{
50public:
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 double GetElement(Mat matrix, PetscInt row, PetscInt col)
static void AddToElement(Mat matrix, PetscInt row, PetscInt col, double value)
static void Zero(Mat matrix)
static void SetRow(Mat matrix, PetscInt row, double value)
static void ZeroRowsWithValueOnDiagonal(Mat matrix, std::vector< unsigned > &rRows, double diagonalValue)
static void SetOption(Mat matrix, MatOption option)
static bool CheckSymmetry(const Mat matrix, double tol=1e-10)
static void ZeroRowsAndColumnsWithValueOnDiagonal(Mat matrix, std::vector< unsigned > rowColIndices, double diagonalValue)
static void GetOwnershipRange(Mat matrix, PetscInt &lo, PetscInt &hi)
static void SwitchWriteMode(Mat matrix)
static void AddMultipleValues(Mat matrix, unsigned *matrixRowAndColIndices, c_matrix< double, MATRIX_SIZE, MATRIX_SIZE > &rSmallMatrix)
static unsigned GetSize(Mat matrix)
static Vec GetMatrixRowDistributed(Mat matrix, unsigned rowIndex)
static void Finalise(Mat matrix)
static void ZeroColumn(Mat matrix, PetscInt col)
static void SetElement(Mat matrix, PetscInt row, PetscInt col, double value)
static bool CheckEquality(const Mat mat1, const Mat mat2, double tol=1e-10)
static void Display(Mat matrix)
static void TurnOffVariableAllocationError(Mat matrix)