Memosa-FVM  0.2
SquareTensor.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 _SQUARETENSOR_H_
6 #define _SQUARETENSOR_H_
7 
8 #include "NumType.h"
9 #include "Vector.h"
10 #include <sstream>
11 #include "CRConnectivity.h"
12 
13 
14 template <class T, int N>
16 {
17 public:
18 
19  enum { NSQR = N*N };
20 
24 
26  {}
27 
29  {
30  for(int i=0; i<NSQR; i++)
31  _data[i] = o._data[i];
32  }
33 
34  SquareTensor(const T& s)
35  {
36  for(int i=0; i<N; i++)
37  for(int j=0; j<N; j++)
38  (*this)(i,j) = (i==j) ? s : 0;
39  }
40 
41 
42  static string getTypeName()
43  {
44  return "SquareTensor<" + NumTypeTraits<T>::getTypeName() +
45  "," + intAsString(N) +
46  ">";
47  }
48 
49  static int getDimension() {return 1;}
50 
51  static void getShape(int *shp) { *shp = NSQR;}
52  static int getDataSize()
53  {
55  }
56 
57  // T& operator[](int n) {return _data[n];}
58  T& operator()(int i, int j) {return _data[i*N+j];}
59 
60  //const T& operator[](int n) const {return _data[n];}
61 
62  const T& operator()(int i, int j) const {return _data[i*N+j];}
63 
64  void printFromC(ostream &os) const
65  {
66  os << "[ " ;
67  for(int i=0;i<NSQR;i++)
68  os << _data[i] << " " ;
69  os << "]";
70  }
71 
72  static void write(FILE* fp, const SquareTensor& x)
73  {
74  for(int i=0; i<NSQR; i++)
75  {
76  NumTypeTraits<T>::write(fp,x[i]);
77  fprintf(fp, " ");
78  }
79  }
80 
81  SquareTensor& operator=(const T& s)
82  {
83  for(int i=0; i<N; i++)
84  for(int j=0; j<N; j++)
85  (*this)(i,j) = (i==j) ? s : T(0.);
86  return *this;
87  }
88 
90  {
91  for(int i=0;i<NSQR;i++)
92  _data[i] = o._data[i];
93  return *this;
94  }
95 
97  {
98  SquareTensor r;
99  for(int i=0;i<NSQR;i++)
100  r._data[i]=-_data[i];
101  return r;
102  }
103 
105  {
106  for(int i=0;i<NSQR;i++)
107  _data[i] += o._data[i];
108  return *this;
109  }
110 
112  {
113  for(int i=0;i<N;i++)
114  (*this)(i,i) += s;
115  return *this;
116  }
117 
119  {
120  for(int i=0;i<NSQR;i++)
121  _data[i] -= o._data[i];
122  return *this;
123  }
124 
126  {
127  for(int i=0;i<N;i++)
128  (*this)(i,i) -= s;
129  return *this;
130  }
131 
133  {
134  for(int i=0;i<NSQR;i++)
135  _data[i] /= s;
136  return *this;
137  }
138 
140  {
141  *this *= inverse(o);
142  return *this;
143  }
144 
146  {
147  for(int i=0;i<NSQR;i++)
148  _data[i] *= s;
149  return *this;
150  }
151 
153  {
154  SquareTensor p;
155  for(int i=0;i<N;i++)
156  for(int j=0;j<N;j++)
157  {
158  p(i,j) = 0;
159  for(int k=0;k<N;k++)
160  p(i,j) += (*this)(i,k) * o(k,j);
161  }
162 
163  *this = p;
164  return *this;
165  }
166 
167  void zero()
168  {
169  for(int i=0;i<NSQR;i++) _data[i] = NumTypeTraits<T>::getZero();
170  }
171 
172  T mag2() const
173  {
175  for(int i=0; i<N; i++)
176  r += (*this)(i,i) * (*this)(i,i);
177  return r;
178  }
179 
180  bool operator<(const double tolerance) const
181  {
182  return mag2() < tolerance*tolerance;
183  }
184 
186  {
187  SquareTensor z;
188  z.zero();
189  return z;
190  }
191 
192  static double doubleMeasure(const SquareTensor& x)
193  {
194  double m=0;
195  for (int i=0; i<N; i++)
196  m += NumTypeTraits<T>::doubleMeasure(x(i,i));
197  return m;
198  }
199 
201  {
202  SquareTensor n(getZero());
203  for(int i=0; i<N; i++)
205 
206  return n;
207  }
208 
210  {
211  SquareTensor n(getZero());
212  for(int i=0; i<N; i++)
213  n(i,i) = NumTypeTraits<T>::getUnity();
214 
215  return n;
216  }
217 
218  static void accumulateOneNorm(SquareTensor& sum, const SquareTensor& v)
219  {
220  for(int i=0; i<NSQR; i++)
222  }
223 
224  static void accumulateDotProduct(SquareTensor& sum, const SquareTensor& v0,
225  const SquareTensor& v1)
226  {
227  for(int i=0; i<NSQR; i++)
229  }
230 
231  static void reduceSum(T_Scalar& sum, const This_T& x)
232  {
233  for(int i=0; i<NSQR; i++)
235  }
236 
237  static void safeDivide(SquareTensor& x, const SquareTensor& y)
238  {
239  for(int i=0; i<NSQR; i++)
241  }
242 
243  static void normalize(SquareTensor& x, const SquareTensor& y)
244  {
245  for(int i=0; i<NSQR; i++)
247  }
248 
249  static void setMax(SquareTensor& x, const SquareTensor& y)
250  {
251  for(int i=0; i<NSQR; i++)
253  }
254 
255  private:
257  };
258 
259 template<class T, int N>
260 inline ostream& operator<<(ostream &os,
261  const SquareTensor<T,N> &v)
262 {
263  v.printFromC(os);
264  return os;
265 }
266 
267 
268 template<class T, int N>
271 {
272  return SquareTensor<T,N>(a) += b;
273 }
274 
275  template<class T, int N>
278 {
279  return SquareTensor<T,N>(a) -= b;
280 }
281 
282 template<class T, int N>
285 {
286  return -SquareTensor<T,N>(a);
287 }
288 
289 template<class T, int N>
292 {
293  return SquareTensor<T,N>(a) *= b;
294 }
295 
296 template<class T, int N>
298 operator*(const T s, const SquareTensor<T,N>& a)
299 {
300  return SquareTensor<T,N>(a) *= s;
301 }
302 
303 template<class T, int N>
305 operator*(const SquareTensor<T,N>& a, const T s)
306 {
307  return SquareTensor<T,N>(a) *= s;
308 }
309 
310 template<class T, int N>
313 {
314  Vector<T,N> r;
315  for(int i=0; i<N; i++)
316  {
317  r[i] = 0;
318  for(int j=0; j<N; j++)
319  r[i] += a(i,j)*b[j];
320  }
321  return r;
322 }
323 
324 template<class T, int N>
326 operator/(const SquareTensor<T,N>& a, const T s)
327 {
328  return SquareTensor<T,N>(a) /= s;
329 }
330 
331 template<class T, int N>
334 {
335  return inverse(b)*a;
336 }
337 
338 template<class T, int N>
341 {
342 
343  return inverse(b)*a;
344 }
345 
346 template<class T, int N>
348 operator/(const T s, const SquareTensor<T,N>& a)
349 {
350  SquareTensor<T,N> r(0);
351  for(int i=0; i<N; i++) r(i,i) = s;
352  return inverse(a)*r;
353 }
354 
355 
356 template<class T>
359 {
360  SquareTensor<T,2> inv;
361  T det = a(0,0)*a(1,1)-a(0,1)*a(1,0);
362  inv(0,0) = a(1,1) / det;
363  inv(0,1) = -a(0,1) / det;
364  inv(1,0) = -a(1,0) / det;
365  inv(1,1) = a(0,0) / det;
366 
367  return inv;
368 }
369 
370 
371 template<class T>
374 {
375  SquareTensor<T,3> inv;
376  T det = a(0,0)*(a(1,1)*a(2,2)-a(1,2)*a(2,1))
377  -a(0,1)*(a(1,0)*a(2,2) - a(1,2)*a(2,0))
378  +a(0,2)*(a(1,0)*a(2,1) - a(1,1)*a(2,0));
379 
380  inv(0,0) = (a(1,1)*a(2,2) - a(1,2)*a(2,1)) / det;
381  inv(0,1) = (a(0,2)*a(2,1) - a(0,1)*a(2,2)) / det;
382  inv(0,2) = (a(0,1)*a(1,2) - a(0,2)*a(1,1)) / det;
383  inv(1,0) = (a(1,2)*a(2,0) - a(1,0)*a(2,2)) / det;
384  inv(1,1) = (a(0,0)*a(2,2) - a(0,2)*a(2,0)) / det;
385  inv(1,2) = (a(0,2)*a(1,0) - a(0,0)*a(1,2)) / det;
386  inv(2,0) = (a(1,0)*a(2,1) - a(1,1)*a(2,0)) / det;
387  inv(2,1) = (a(0,1)*a(2,0) - a(0,0)*a(2,1)) / det;
388  inv(2,2) = (a(0,0)*a(1,1) - a(0,1)*a(1,0)) / det;
389 
390  return inv;
391 
392 }
393 
394 
395 template<int N>
396 void setFlatCoeffs(Array<double>& flatCoeffs,
397  const CRConnectivity& flatConnectivity,
398  const Array<SquareTensor<double,N> >& diag,
399  const Array<SquareTensor<double,N> >& offDiag,
400  const CRConnectivity& connectivity)
401 {
402  const Array<int>& myRow = connectivity.getRow();
403  const Array<int>& myCol = connectivity.getCol();
404  const int rowDim = connectivity.getRowDim();
405 
406  for(int i=0; i<rowDim; i++)
407  {
408  for(int ndr=0; ndr<N; ndr++)
409  for(int ndc=0; ndc<N; ndc++)
410  {
411  const int nfr = i*N + ndr;
412  const int nfc = i*N + ndc;
413 
414  const int fp = flatConnectivity.getCoeffPosition(nfc,nfr);
415  flatCoeffs[fp] = diag[i](ndr,ndc);
416  }
417 
418  for(int jp=myRow[i]; jp<myRow[i+1]; jp++)
419  {
420  const int j = myCol[jp];
421 
422  for(int ndr=0; ndr<N; ndr++)
423  for(int ndc=0; ndc<N; ndc++)
424  {
425  const int nfr = i*N + ndr;
426  const int nfc = j*N + ndc;
427  const int fp = flatConnectivity.getCoeffPosition(nfc,nfr);
428  flatCoeffs[fp] = offDiag[jp](ndr,ndc);
429  }
430  }
431  }
432 }
433 
434 
435 
436 /*
437 template<class T>
438 SquareTensor<T,10>
439  ludcmp(const SquareTensor<T,10>a, int n, Vector<T,10> indx, double d){
440  const int NMAX(500);
441  const double TINY(1.0e-20);
442  int i,imax,j,k;
443  double aamax,dum,sum,;
444  Array<T> vv; // vv stores the implicit scaling of each row.
445  d=1.0;
446  for( i=0;i<n;i++){
447  aamax=0.0;
448  for (j=0;j<n;j++){
449  if (abs(a(i,j)) > aamax) {aamax=abs(a(i,j)); }
450  }
451  if (aamax == 0.) {break; } //’singular matrix in ludcmp’
452  vv(i)=1./aamax ;
453 
454  }
455 
456  for( j=0;j<n;j++) {
457  for( i=0;i<j;i++){
458  sum=a(i,j);
459  for(k=0;k<i;k++){
460  sum=sum-a(i,k)*a(k,j);
461  }
462  a(i,j)=sum;
463  }
464  aamax=0.;
465  for(i=j;i<n;i++){
466  sum=a(i,j);
467  for(k=0;k<j;k++){
468  sum=sum-a(i,k)*a(k,j);
469  }
470  a(i,j)=sum;
471  dum=vv(i)*abs(sum);
472  if (dum >= aamax){imax=i;aamax=dum;}
473  }
474  if (j ~= imax){ //Do we need to interchange rows?
475  for(k=0;k<n;k++){ //Yes, do so...
476  dum=a(imax,k);
477  a(imax,k)=a(j,k);
478  a(j,k)=dum;
479  }
480  d=-d; //...and change the parity of d.
481  vv(imax)=vv(j); //! Also interchange the scale factor.
482  }
483  indx(j)=imax;
484  if(a(j,j) == 0.){a(j,j)=TINY;}
485  //If the pivot element is zero the matrix is singular (at least to the precision of the algorithm).
486  // For some applications on singular matrices, it is desirable to substitute TINY for zero.
487  if(j.~= n){ //Now, finally, divide by the pivot element.
488  dum=1./a(j,j);
489  for( i=j+1;i<n;i++) {a(i,j)=a(i,j)*dum;}
490  }
491  } //Go back for the next column in the reduction.
492 
493 
494 }
495 
496 
497 void lubksb(const SquareTensor<T,10>a, int n, Array<T,10> indx, Array<T,10> b)
498 {
499  Array<Int>& indx;
500  REAL a(n,n),b(n);
501  int i,ii,j,ll;
502  double sum;
503  ii=0;
504  for(i=0;i<n;i++){
505  ll=indx(i);
506  sum=b(ll);
507  b(ll)=b(i);
508  if (ii.ne.0){
509  for(j=ii;j<i;j++){
510  sum=sum-a(i,j)*b(j);
511  }
512  }
513  else if (sum ~= 0.){
514  ii=i; //A nonzero element was encountered, so from now on we will
515  } //to do the sums in the loop above.
516  b(i)=sum;
517  }
518  for( i=n-1;i>=0;i--)
519  {//backsubstitution, equation (2.3.7).
520  sum=b(i);
521  for(j=i+1;j<n;j++){
522  sum=sum-a(i,j)*b(j);
523  }
524  b(i)=sum/a(i,i); //Store a component of the solution vector X.
525  }
526 }
527 */
528 
529 #endif
530 
SquareTensor< T, N > operator+(const SquareTensor< T, N > &a, const SquareTensor< T, N > &b)
Definition: SquareTensor.h:270
const Array< int > & getCol() const
const Array< int > & getRow() const
static double doubleMeasure(const SquareTensor &x)
Definition: SquareTensor.h:192
static void reduceSum(T_Scalar &sum, const This_T &x)
Definition: SquareTensor.h:231
SquareTensor< T, N > operator/(const SquareTensor< T, N > &a, const T s)
Definition: SquareTensor.h:326
Definition: Vector.h:19
static void setMax(SquareTensor &x, const SquareTensor &y)
Definition: SquareTensor.h:249
SquareTensor & operator-=(const T s)
Definition: SquareTensor.h:125
static SquareTensor getUnity()
Definition: SquareTensor.h:209
static SquareTensor getNegativeUnity()
Definition: SquareTensor.h:200
SquareTensor & operator+=(const T s)
Definition: SquareTensor.h:111
T _data[NSQR]
Definition: SquareTensor.h:256
SquareTensor & operator=(const SquareTensor &o)
Definition: SquareTensor.h:89
SquareTensor< T, N > operator*(const SquareTensor< T, N > &a, const SquareTensor< T, N > &b)
Definition: SquareTensor.h:291
SquareTensor< T, N > operator-(const SquareTensor< T, N > &a, const SquareTensor< T, N > &b)
Definition: SquareTensor.h:277
SquareTensor operator-()
Definition: SquareTensor.h:96
T mag2() const
Definition: SquareTensor.h:172
SquareTensor< T, 2 > inverse(const SquareTensor< T, 2 > &a)
Definition: SquareTensor.h:358
const T & operator()(int i, int j) const
Definition: SquareTensor.h:62
NumTypeTraits< T >::T_BuiltIn T_BuiltIn
Definition: SquareTensor.h:23
static void safeDivide(SquareTensor &x, const SquareTensor &y)
Definition: SquareTensor.h:237
int getCoeffPosition(const int i, const int j) const
static void getShape(int *shp)
Definition: SquareTensor.h:51
static void accumulateDotProduct(SquareTensor &sum, const SquareTensor &v0, const SquareTensor &v1)
Definition: SquareTensor.h:224
bool operator<(const double tolerance) const
Definition: SquareTensor.h:180
static int getDataSize()
Definition: SquareTensor.h:52
SquareTensor & operator-=(const SquareTensor &o)
Definition: SquareTensor.h:118
SquareTensor & operator+=(const SquareTensor &o)
Definition: SquareTensor.h:104
SquareTensor & operator=(const T &s)
Definition: SquareTensor.h:81
SquareTensor & operator/=(const T s)
Definition: SquareTensor.h:132
string intAsString(const int i)
Definition: Vector.h:11
static void normalize(SquareTensor &x, const SquareTensor &y)
Definition: SquareTensor.h:243
SquareTensor & operator/=(const SquareTensor &o)
Definition: SquareTensor.h:139
SquareTensor & operator*=(const SquareTensor &o)
Definition: SquareTensor.h:152
NumTypeTraits< T >::T_Scalar T_Scalar
Definition: SquareTensor.h:22
static SquareTensor getZero()
Definition: SquareTensor.h:185
SquareTensor< T, N > This_T
Definition: SquareTensor.h:21
static string getTypeName()
Definition: SquareTensor.h:42
ostream & operator<<(ostream &os, const SquareTensor< T, N > &v)
Definition: SquareTensor.h:260
static void accumulateOneNorm(SquareTensor &sum, const SquareTensor &v)
Definition: SquareTensor.h:218
void printFromC(ostream &os) const
Definition: SquareTensor.h:64
SquareTensor(const SquareTensor &o)
Definition: SquareTensor.h:28
void setFlatCoeffs(Array< double > &flatCoeffs, const CRConnectivity &flatConnectivity, const Array< SquareTensor< double, N > > &diag, const Array< SquareTensor< double, N > > &offDiag, const CRConnectivity &connectivity)
Definition: SquareTensor.h:396
SquareTensor & operator*=(const T s)
Definition: SquareTensor.h:145
static int getDimension()
Definition: SquareTensor.h:49
static void write(FILE *fp, const SquareTensor &x)
Definition: SquareTensor.h:72
int getRowDim() const
SquareTensor(const T &s)
Definition: SquareTensor.h:34
T & operator()(int i, int j)
Definition: SquareTensor.h:58