Memosa-FVM  0.2
SquareMatrixESBGK.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _SQUAREMATRIXESBGK_H_
6 #define _SQUAREMATRIXESBGK_H_
7 
8 #include "Array.h"
9 #include "MatrixJML.h"
10 #include <math.h>
11 #include <omp.h>
12 
13 template<class T>
14 class SquareMatrixESBGK : public MatrixJML<T>
15 {
16  public:
17  typedef Array<T> TArray;
20 
21  SquareMatrixESBGK(const int N):
22  _order(N),
23  _elements(N*N),
24  _sorted(false),
25  _pivotRows(N),
26  _maxVals(N),
28  {_values.zero();}
29 
30  T& getElement(const int i, const int j) {return _values[(i-1)*_order+j-1];}
31  T& operator()(const int i, const int j) {return _values[(i-1)*_order+j-1];}
32  void zero() {_values.zero();}
33 
34  void Solve(TArray& bVec)
35  {//Gaussian Elimination w/ scaled partial pivoting
36  //replaces bVec with the solution vector.
37 
39  (*this).makeCopy(LU);
40  IntArray l(_order);
41  TArray s(_order);
42  TArray x(_order);
43 
44  //find max values in each row if not done yet
45  if(!_sorted)
46  {
47  for(int i=1;i<_order+1;i++)
48  {
49  l[i-1]=i;
50  s[i-1]=fabs((*this)(i,1));
51  for(int j=2;j<_order+1;j++)
52  {
53  if(s[i-1]<fabs((*this)(i,j)))
54  s[i-1]=fabs((*this)(i,j));
55  }
56  }
57  _maxVals=s;
58  }
59 
60  //Forward sweep
61  if(!_sorted)
62  {
63  for(int i=1;i<_order;i++)
64  {
65  //T rmax=0;
66  T rmax=fabs(LU(l[i-1],i)/s[l[i-1]]);
67  int newMax=i;
68  for(int j=i+1;j<_order+1;j++)
69  {
70  T r=fabs(LU(l[j-1],i)/s[l[j-1]]);
71  if(r>rmax)
72  {
73  rmax=r;
74  newMax=j;
75  }
76  }
77 
78  /*
79  int temp=l[i-1];
80  l[i-1]=l[newMax-1];
81  l[newMax-1]=temp;
82  */
83 
84  //#pragma omp parallel for
85  for(int j=i+1;j<_order+1;j++)
86  {
87  T factor=LU(l[j-1],i)/LU(l[i-1],i);
88  LU(l[j-1],i)=factor;
89  if(factor!=factor)
90  cout<<"GE NaN at l[j-1], l[i-1], LU(l[j-1],i), LU(l[i-1],i) "<<l[j-1]<<" "<<l[i-1]<<" "<<LU(l[j-1],i)<<" "<<LU(l[i-1],i)<<endl;
91  bVec[l[j-1]-1]-=factor*bVec[l[i-1]-1];
92  for(int k=i+1;k<_order+1;k++)
93  LU(l[j-1],k)=LU(l[j-1],k)-LU(l[i-1],k)*factor;
94  }
95  }
96  _pivotRows=l;
97  _sorted=true;
98  }
99  else
100  {
101  for(int i=1;i<_order;i++)
102  {
103  for(int j=i+1;j<_order+1;j++)
104  {
105  T factor=LU(_pivotRows[j-1],i)/LU(_pivotRows[i-1],i);
106  LU(l[j-1],i)=factor;
107  bVec[_pivotRows[j-1]-1]-=factor*bVec[_pivotRows[i-1]-1];
108  for(int k=i+1;k<_order+1;k++)
109  {
110  LU(_pivotRows[j-1],k)=LU(_pivotRows[j-1],k)
111  -LU(_pivotRows[i-1],k)*factor;
112  }
113  }
114  }
115  }
116 
117 
118 
119  //back solve
120  /*
121  bVec[_pivotRows[_order-1]-1]=
122  bVec[_pivotRows[_order-1]-1]/LU(_pivotRows[_order-1],_order);
123 
124  if(bVec[_pivotRows[_order-1]-1]!=bVec[_pivotRows[_order-1]-1])
125  cout<<"bvec crashes at order"<<endl;
126  T sum=0.;
127  for(int i=_order-1;i>0;i--)
128  {
129  sum=0.;
130  for(int j=i+1;j<_order+1;j++)
131  sum-=LU(_pivotRows[i-1],j)*bVec[j-1];
132  bVec[_pivotRows[i-1]-1]+=sum;
133  bVec[_pivotRows[i-1]-1]=bVec[_pivotRows[i-1]-1]/LU(_pivotRows[i-1],i);
134  if(bVec[_pivotRows[i-1]-1]!=bVec[_pivotRows[i-1]-1])
135  cout<<"GE Error at "<<i<<endl;
136  }
137  */
138 
139  //************back solve***************//
140 
141  //initalize solution vector
142  const T zero(0.);
143  for(int i=0;i<_order;i++)
144  x[i]=zero;
145 
146  x[_order-1]=bVec[_pivotRows[_order-1]-1]/LU(_pivotRows[_order-1],_order);
147 
148  T sum=0.;
149  for(int i=_order-1;i>0;i--)
150  {
151  sum=bVec[_pivotRows[i-1]-1];
152  for(int j=i+1;j<_order+1;j++)
153  sum=sum-LU(_pivotRows[i-1],j)*x[j-1];
154  x[i-1]=sum/LU(_pivotRows[i-1],i);
155  }
156  bVec=x;
157 
158  //***************************//
159 
160  }
161 
163  {
164  if(o._order!=this->_order)
165  throw CException("Cannot copy matrices of different sizes!");
166 
167  o._sorted=this->_sorted;
168  o._pivotRows=this->_pivotRows;
169  o._maxVals=this->_maxVals;
170  o._values=this->_values;
171  }
172 
173  void printMatrix()
174  {
175  for(int i=1;i<_order+1;i++)
176  {
177  for(int j=1;j<_order+1;j++)
178  cout<<(*this)(i,j)<<" ";
179  cout<<endl;
180  }
181  cout<<endl;
182  }
183 
185  {
186  T trace=0.;
187  for(int i=1;i<_order+1;i++)
188  trace+=fabs((*this)(i,i));
189  return trace;
190  }
191 
192  private:
193  const int _order;
194  const int _elements;
195  bool _sorted;
199 
200 
201 };
202 
203 
204 #endif
virtual void zero()
Definition: Array.h:281
T & getElement(const int i, const int j)
T & operator()(const int i, const int j)
SquareMatrixESBGK(const int N)
void makeCopy(SquareMatrixESBGK< T > &o)
NumTypeTraits< T >::T_Scalar T_Scalar
Tangent fabs(const Tangent &a)
Definition: Tangent.h:312
Definition: Array.h:14
void Solve(TArray &bVec)
Array< int > IntArray