gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
linearSystemPETSc.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "GmshConfig.h"
7 #include <string.h>
8 
9 #if defined(HAVE_PETSC)
10 #include "petsc.h"
11 #include "linearSystemPETSc.h"
12 #include "fullMatrix.h"
13 #include <stdlib.h>
14 #include "GmshMessage.h"
15 #include "linearSystemPETSc.hpp"
16 
17 template class linearSystemPETSc<double>;
18 
19 #ifdef PETSC_USE_COMPLEX
21 
22 // this specialization will cast to a double (take the real part) if "val" is a
23 // double wheras Petsc is built in complex mode.
24 template <>
25 void linearSystemPETSc<double>::getFromRightHandSide(int row, double &val) const
26 {
27  PetscScalar *tmp;
28  _check(VecGetArray(_b, &tmp));
29  PetscScalar s = tmp[row];
30  _check(VecRestoreArray(_b, &tmp));
31  val = s.real();
32 }
33 
34 // this specialization will cast to a double (take the real part) if "val" is a
35 // double wheras Petsc is built in complex mode.
36 template <>
37 void linearSystemPETSc<double>::getFromSolution(int row, double &val) const
38 {
39  PetscScalar *tmp;
40  _check(VecGetArray(_x, &tmp));
41  PetscScalar s = tmp[row];
42  _check(VecRestoreArray(_x, &tmp));
43  val = s.real();
44 }
45 #endif
46 
47 template <>
48 int linearSystemPETSc<fullMatrix<double> >::_getBlockSizeFromParameters() const
49 {
50  if(_parameters.find("blockSize") == _parameters.end())
51  Msg::Error("'blockSize' parameters must be set for linearSystemPETScBlock");
52  int blockSize =
53  strtol(_parameters.find("blockSize")->second.c_str(), nullptr, 10);
54  return blockSize;
55 }
56 
57 template <>
58 void linearSystemPETSc<fullMatrix<double> >::addToMatrix(
59  int row, int col, const fullMatrix<double> &val)
60 {
61  if(!_entriesPreAllocated) preAllocateEntries();
62  PetscInt i = row, j = col;
63 #ifdef PETSC_USE_COMPLEX
64  fullMatrix<std::complex<double> > modval(val.size1(), val.size2());
65  for(int ii = 0; ii < val.size1(); ii++) {
66  for(int jj = 0; jj < val.size1(); jj++) {
67  modval(ii, jj) = val(ii, jj);
68  }
69  }
70  MatSetValuesBlocked(_a, 1, &i, 1, &j, modval.getDataPtr(), ADD_VALUES);
71 #else
72  MatSetValuesBlocked(_a, 1, &i, 1, &j, val.getDataPtr(), ADD_VALUES);
73 #endif
74 
75  _valuesNotAssembled = true;
76 }
77 
78 template <>
79 void linearSystemPETSc<fullMatrix<double> >::addToRightHandSide(
80  int row, const fullMatrix<double> &val, int ith)
81 {
82  if(!_entriesPreAllocated) preAllocateEntries();
83  int blockSize;
84  _check(MatGetBlockSize(_a, &blockSize));
85  for(int ii = 0; ii < blockSize; ii++) {
86  PetscInt i = row * blockSize + ii;
87  PetscScalar v = val(ii, 0);
88  VecSetValues(_b, 1, &i, &v, ADD_VALUES);
89  }
90 }
91 
92 template <>
93 void linearSystemPETSc<fullMatrix<double> >::getFromRightHandSide(
94  int row, fullMatrix<double> &val) const
95 {
96  int blockSize;
97  _check(MatGetBlockSize(_a, &blockSize));
98  for(int i = 0; i < blockSize; i++) {
99  int ii = row * blockSize + i;
100 #ifdef PETSC_USE_COMPLEX
101  PetscScalar s;
102  VecGetValues(_b, 1, &ii, &s);
103  val(i, 0) = s.real();
104 #else
105  VecGetValues(_b, 1, &ii, &val(i, 0));
106 #endif
107  }
108 }
109 
110 template <>
111 void linearSystemPETSc<fullMatrix<double> >::addToSolution(
112  int row, const fullMatrix<double> &val)
113 {
114  int blockSize;
115  _check(MatGetBlockSize(_a, &blockSize));
116  for(int ii = 0; ii < blockSize; ii++) {
117  PetscInt i = row * blockSize + ii;
118  PetscScalar v = val(ii, 0);
119  VecSetValues(_x, 1, &i, &v, ADD_VALUES);
120  }
121 }
122 
123 template <>
124 void linearSystemPETSc<fullMatrix<double> >::getFromSolution(
125  int row, fullMatrix<double> &val) const
126 {
127  int blockSize;
128  _check(MatGetBlockSize(_a, &blockSize));
129  for(int i = 0; i < blockSize; i++) {
130  int ii = row * blockSize + i;
131 #ifdef PETSC_USE_COMPLEX
132  PetscScalar s;
133  VecGetValues(_x, 1, &ii, &s);
134  val(i, 0) = s.real();
135 #else
136  VecGetValues(_x, 1, &ii, &val(i, 0));
137 #endif
138  }
139 }
140 
141 template class linearSystemPETSc<fullMatrix<double> >;
142 #endif // HAVE_PETSC
linearSystemPETSc.hpp
linearSystemPETSc
Definition: linearSystemPETSc.h:150
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
linearSystemPETSc.h
GmshMessage.h
linearSystemPETSc::getFromRightHandSide
void getFromRightHandSide(int row, scalar &val) const
Definition: linearSystemPETSc.h:163
fullMatrix< double >
fullMatrix::size2
int size2() const
Definition: fullMatrix.h:275
fullMatrix::size1
int size1() const
Definition: fullMatrix.h:274
fullMatrix::getDataPtr
const scalar * getDataPtr() const
Definition: fullMatrix.h:287
linearSystemPETSc::getFromSolution
void getFromSolution(int row, scalar &val) const
Definition: linearSystemPETSc.h:164
fullMatrix.h