NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
tsolver-umfpack.ixx
Go to the documentation of this file.
1 
7 #include <cstdio>
8 #include <umfpack.h>
9 namespace numcxx
10 {
11 
13  template<typename T>
14  inline TSolverUMFPACK<T>::TSolverUMFPACK(const std::shared_ptr<TSparseMatrix<T>> pMatrix):
15  pMatrix(pMatrix),
16  Symbolic(nullptr),
17  Numeric(nullptr)
18  {
19  update();
20  }
21 
22  template<typename T>
24  pMatrix(nullptr),
25  Symbolic(nullptr),
26  Numeric(nullptr)
27  {
28  update(Matrix);
29  }
30 
31  template<typename T>
33  {
34  if (Symbolic!=nullptr)
35  {
36  umfpack_di_free_symbolic(&Symbolic);
37  Symbolic=nullptr;
38  }
39  if (Numeric!=nullptr)
40  {
41  umfpack_di_free_numeric(&Numeric);
42  Numeric=nullptr;
43  }
44  }
45 
47  template<typename T>
48  inline std::shared_ptr<TSolverUMFPACK<T>> TSolverUMFPACK<T>::create(const std::shared_ptr<TSparseMatrix<T>> pA)
49  {
50  return std::make_shared<TSolverUMFPACK<T>>(pA);
51  }
52 
54  template<typename T>
56  {
57  if (pMatrix==nullptr)
58  throw std::runtime_error("numcxx: TSolverUMFPACK created without smartpointer");
59  update(*pMatrix);
60  }
61  template<typename T>
62  inline void TSolverUMFPACK<T>::update(const TSparseMatrix<T> &Matrix)
63  {
64  if (Matrix.empty()) return;
65  if (Matrix.pattern_changed())
66  throw std::runtime_error("numcxx: forgot flush() after sparse pattern changed");
67 
68  int n=Matrix.shape(0);
69  int nia=Matrix.pIA->size();
70  int nja=Matrix.pJA->size();
71 
72 
73  // double Control[UMFPACK_CONTROL];
74  // for (int i=0;i<UMFPACK_CONTROL;i++) Control[i]=0.0;
75  // Control[UMFPACK_PIVOT_TOLERANCE]=pivot_tolerance;
76  // Control[UMFPACK_DROPTOL]=drop_tolerance;
77  // Control[UMFPACK_PRL]=0;
78  // double *control=Control;
79  // if (pivot_tolerance<0) control=0;
80  double *control=nullptr;
81 
82  int status;
83  if (Matrix.pattern_changed() || Symbolic==nullptr)
84  {
85  if (Symbolic) umfpack_di_free_symbolic(&Symbolic),Symbolic=nullptr;
86  if (Numeric) umfpack_di_free_numeric(&Numeric),Numeric=nullptr;
87  status=umfpack_di_symbolic (n, n, Matrix.pIA->data(), Matrix.pJA->data(), Matrix.pA->data(), &Symbolic, 0, 0);
88  if (status>1)
89  {
90  char errormsg[80];
91  snprintf(errormsg,80,"numcxx::TSolverUMFPACK::update: umfpack_di_symbolic error %d\n",status);
92  throw std::runtime_error(errormsg);
93  }
94 
95  }
96  if (Numeric!=nullptr)
97  {
98  umfpack_di_free_numeric(&Numeric);
99  Numeric=nullptr;
100  }
101  status =umfpack_di_numeric (Matrix.pIA->data(), Matrix.pJA->data(), Matrix.pA->data(), Symbolic, &Numeric, control, 0) ;
102  if (status>1)
103  {
104  char errormsg[80];
105  snprintf(errormsg,80,"numcxx::TSolverUMFPACK::update: umfpack_di_numeric error %d\n",status);
106  throw std::runtime_error(errormsg);
107  }
109  pIA=Matrix.pIA;
110  pJA=Matrix.pJA;
111  pA=Matrix.pA;
112  }
113 
115  template<typename T>
116  inline void TSolverUMFPACK<T>::solve( TArray<T> & Sol, const TArray<T> & Rhs)
117  {
118  Sol.resize(Rhs.size());
119  double *control=nullptr;
120  int status;
121  status=umfpack_di_solve (UMFPACK_At,pIA->data(), pJA->data(),pA->data(), Sol.data(), Rhs.data(), Numeric, control, 0 ) ;
122  if (status>1)
123  {
124  char errormsg[80];
125  snprintf(errormsg,80,"numcxx::TSolverUMFPACK::update: umfpack_di_solve error %d\n",status);
126  throw std::runtime_error(errormsg);
127  }
128  }
129 
130 
131 }
static std::shared_ptr< TSolverUMFPACK< T > > create(const std::shared_ptr< TSparseMatrix< T >> pA)
Create LU factorization class.
std::shared_ptr< TArray1< int > > pJA
Column indices.
std::shared_ptr< TArray1< int > > pIA
Row pointer of matrix.
Sparse matrix class using CRS storage scheme.
const std::shared_ptr< TSparseMatrix< T > > pMatrix
The corresponding matrix.
TArray is the common template base class for arrays and dense matrices of the numcxx project...
Definition: tarray.hxx:17
size_t size() const
Obtain size of array.
Definition: tarray.hxx:58
void solve(TArray< T > &Sol, const TArray< T > &Rhs)
Solve LU factorized system.
bool pattern_changed() const
Check if pattern has changed after last solver update.
std::shared_ptr< TArray1< int > > pJA
Column indices.
std::shared_ptr< TArray1< T > > pA
Entries.
index shape(int idim) const
Return the shape od the matrix.
void resize(size_t n)
Resize array.
Definition: tarray.ixx:282
Numcxx template library.
Definition: expression.ixx:41
std::shared_ptr< TArray1< T > > pA
Entries.
void * Numeric
Pointer to numeric factorization data.
void * Symbolic
Pointer to symbolic factorization data.
std::shared_ptr< TArray1< int > > pIA
Row pointers.
void update()
Perform actual computation of LU factorization.
T * data() const
Obtain C-pointer of data array.
Definition: tarray.hxx:128