NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
tprecon-ilu.ixx
Go to the documentation of this file.
1 #include <cassert>
2 namespace numcxx
3 {
4 
5 
6  template<typename T>
7  inline TPreconILU<T>::TPreconILU(const std::shared_ptr<TSparseMatrix<T>> pMatrix)
8  {
9  pInvDiag=std::make_shared<TArray1<T>>(pMatrix->shape(0));
10  pDiagIdx=std::make_shared<TArray1<int>>(pMatrix.shape(0));
11  update(*pMatrix);
12  }
13 
14  template<typename T>
16  {
17  pInvDiag=std::make_shared<TArray1<T>>(A.shape(0));
18  pDiagIdx=std::make_shared<TArray1<int>>(A.shape(0));
19  update(A);
20  }
21 
22 
23  template<typename T>
24  inline std::shared_ptr<TPreconILU<T>> TPreconILU<T>::create(const std::shared_ptr<TSparseMatrix<T>> pA)
25  {
26  return std::make_shared<TPreconILU<T>>(pA);
27  }
28 
30  template<typename T>
32  {
33 
34  int n=M.shape(0);
35  pA=M.pA;
36  pIA=M.pIA;
37  pJA=M.pJA;
38 
39  auto & A=*pA;
40  auto & IA=*pIA;
41  auto & JA=*pJA;
42  auto & XD=*pInvDiag;
43  auto & ID=*pDiagIdx;
44 
45  for (int i=0; i<n; i++)
46  {
47  for (int j=IA(i);j<IA(i+1);j++)
48  if (JA(j)==i)
49  {
50  ID(i)=j;
51  break;
52  }
53  XD(i)=A(ID(i));
54  }
55 
56  for (int i=0; i<n; i++)
57  {
58  XD(i)=1.0/XD(i);
59  for(int j=ID(i)+1;j<IA(i+1);j++)
60  {
61  int k;
62  bool found=false;
63  for(k=IA(JA(j));k<IA(JA(j)+1);k++)
64  {
65  if (JA(k)==i)
66  {
67  found=true;
68  break;
69  }
70  }
71  if (found) XD(JA(j))-=A(k)*XD(i)*A(j);
72  }
73  }
74 
75  M.pattern_changed(false);
76  }
77 
79  template<typename T>
80  inline void TPreconILU<T>::solve(TArray<T> & Sol, const TArray<T> & Rhs) const
81  {
82  int n=pIA->size()-1;
83  auto & A=*pA;
84  auto & IA=*pIA;
85  auto & JA=*pJA;
86  auto & XD=*pInvDiag;
87  auto & ID=*pDiagIdx;
88 
89  Sol.resize(Rhs.size());
90  for (int i=0; i<n; i++)
91  {
92  T x=0.0;
93  for(int j=IA(i);j<ID(i);j++)
94  x+=A(j)*Sol(JA(j));
95  Sol(i)=XD(i)*(Rhs(i)-x);
96  }
97 
98  for (int i=n-1; i>=0; i--)
99  {
100  T x=0.0;
101  for(int j=ID(i)+1;j<IA(i+1);j++)
102  x += Sol(JA(j))*A(j);
103  Sol(i)-=x*XD(i);
104  }
105  }
106 
107 }
std::shared_ptr< TArray1< int > > pJA
Column indices.
Sparse matrix class using CRS storage scheme.
TPreconILU(const std::shared_ptr< TSparseMatrix< T >> pA)
Create Preconditioner.
Definition: tprecon-ilu.ixx:7
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
static std::shared_ptr< TPreconILU< T > > create(const std::shared_ptr< TSparseMatrix< T >> pA)
Create preconditioner.
Definition: tprecon-ilu.ixx:24
bool pattern_changed() const
Check if pattern has changed after last solver update.
virtual void update(void)
Definition: tarray.hxx:280
std::shared_ptr< TArray1< T > > pA
Entries.
void solve(TArray< T > &Sol, const TArray< T > &Rhs) const
Solve preconditioning system.
Definition: tprecon-ilu.ixx:80
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< int > > pIA
Row pointers.