OpenCAEPoro  0.2.0 Sep/22/2022
A simulator for multicomponent porous media flow
DenseMat.cpp
Go to the documentation of this file.
1 
12 #include "DenseMat.hpp"
13 
14 void MinEigenSY(const int& N, float* A, float* w, float* work, const int& lwork)
15 {
16  int info;
17  int iwork[1] = { 0 };
18  int liwork = 1;
19  char uplo{ 'U' };
20  char Nonly{ 'N' };
21 
22  ssyevd_(&Nonly, &uplo, &N, A, &N, w, work, &lwork, iwork, &liwork, &info);
23  if (info > 0) {
24  cout << "failed to compute eigenvalues!" << endl;
25  }
26 }
27 
28 
29 //void MinEigenS(const int& N, float* a, float* w)
30 //{
31 // MKL_INT info = LAPACKE_ssyevd(LAPACK_COL_MAJOR, 'N', 'U', N, a, N, w);
32 // if (info > 0) {
33 // cout << "failed to compute eigenvalues!" << endl;
34 // }
35 //}
36 
37 void Dcopy(const int& N, double* dst, const double* src)
38 {
39  const int incx = 1, incy = 1;
40  dcopy_(&N, src, &incx, dst, &incy);
41 }
42 
43 double Ddot(int n, double* a, double* b)
44 {
45  const int inca = 1, incb = 1;
46  return ddot_(&n, a, &inca, b, &incb);
47 }
48 
49 // WARNING: absolute sum!
50 double Dnorm1(const int& N, double* x)
51 {
52  const int incx = 1;
53  return dasum_(&N, x, &incx);
54 }
55 
56 double Dnorm2(const int& N, double* x)
57 {
58  const int incx = 1;
59  return dnrm2_(&N, x, &incx);
60 }
61 
62 void Dscalar(const int& n, const double& alpha, double* x)
63 {
64  // x = a x
65  const int incx = 1;
66  dscal_(&n, &alpha, x, &incx);
67 }
68 
69 void Daxpy(const int& n, const double& alpha, const double* x, double* y)
70 {
71  // y= ax +y
72  const int incx = 1, incy = 1;
73  daxpy_(&n, &alpha, x, &incx, y, &incy);
74 }
75 
76 void DaABpbC(const int& m, const int& n, const int& k, const double& alpha,
77  const double* A, const double* B, const double& beta, double* C)
78 {
79  /* C' = alpha B'A' + beta C'
80  * A: m x k
81  * B: k x n
82  * C: m x n
83  * all column majored matrices, no tranpose
84  * A' in col-order in Fortran = A in row-order in C/Cpp
85  */
86 
87  const char transa = 'N', transb = 'N';
88  dgemm_(&transa, &transb, &n, &m, &k, &alpha, B, &n, A, &k, &beta, C, &n);
89 }
90 
91 
92 void myDABpC(const int& m, const int& n, const int& k, const double* A, const double* B, double* C)
93 {
94  // C = AB + C
95  // A: m*n B:n*k C:m*k
96  // all matrix are row majored matrices
97 
98  for (int i = 0; i < m; i++) {
99  for (int j = 0; j < k; j++) {
100  for (int m = 0; m < n; m++) {
101  C[i * k + j] += A[i * n + m] * B[m * k + j];
102  }
103  }
104  }
105 }
106 
107 void myDABpCp(const int& m, const int& n, const int& k, const double* A, const double* B, double* C, const int* flag, const int N)
108 {
109  for (int i = 0; i < m; i++) {
110  for (int j = 0; j < k; j++) {
111  for (int p = 0; p < 3; p++) {
112  if (flag[p] != 0)
113  C[i * k + j] += A[i * n + p] * B[p * k + j];
114  }
115  for (int p = 0; p < 2; p++) {
116  if (flag[p] != 0) {
117  for (int m = 0; m < N; m++) {
118  C[i * k + j] += A[i * n + 3 + p * (N + 1) + m] * B[(3 + p * (N + 1) + m) * k + j];
119  }
120  }
121  }
122  }
123  }
124 }
125 
126 void myDABpCp1(const int& m, const int& n, const int& k, const double* A, const double* B, double* C, const int* flag, const int N)
127 {
128 
129  for (int i = 0; i < m; i++) {
130  for (int j = 0; j < k; j++) {
131  int s = 0;
132  for (int p = 0; p < 3; p++) {
133  if (flag[p] != 0) {
134  C[i * k + j] += A[i * n + p] * B[s * k + j];
135  s++;
136  }
137  }
138  for (int p = 0; p < 2; p++) {
139  if (flag[p] != 0) {
140  for (int m = 0; m < N; m++) {
141  C[i * k + j] += A[i * n + 3 + p * (N + 1) + m] * B[s * k + j];
142  s++;
143  }
144  }
145  }
146  }
147  }
148 }
149 
150 void myDABpCp2(const int& m, const int& n, const int& k, const double* A, const double* B, double* C, const int* flag, const int N)
151 {
152  for (int i = 0; i < m; i++) {
153  for (int j = 0; j < k; j++) {
154  int s = 0;
155  for (int p = 0; p < 3; p++) {
156  if (flag[p] != 0) {
157  C[i * k + j] += A[i * n + s] * B[p * k + j];
158  s++;
159  }
160  }
161  for (int p = 0; p < 2; p++) {
162  if (flag[p] != 0) {
163  for (int m = 0; m < N; m++) {
164  C[i * k + j] += A[i * n + s] * B[(3 + p * (N + 1) + m) * k + j];
165  s++;
166  }
167  }
168  }
169  }
170  }
171 }
172 
173 void DaAxpby(const int& m, const int& n, const double& a, const double* A,
174  const double* x, const double& b, double* y)
175 {
176  /* y= aAx+by
177  */
178  for (int i = 0; i < m; i++) {
179  y[i] = b * y[i];
180  for (int j = 0; j < n; j++) {
181  y[i] += a * A[i * n + j] * x[j];
182  }
183  }
184 }
185 
186 void LUSolve(const int& nrhs, const int& N, double* A, double* b, int* pivot)
187 {
188  int info;
189 
190  dgesv_(&N, &nrhs, A, &N, pivot, b, &N, &info);
191 
192  if (info < 0) {
193  cout << "Wrong Input !" << endl;
194  } else if (info > 0) {
195  cout << "Singular Matrix !" << endl;
196  }
197 }
198 
199 void SYSSolve(const int& nrhs, const char* uplo, const int& N, double* A, double* b, int* pivot, double* work, const int& lwork)
200 {
201  int info;
202 
203  dsysv_(uplo, &N, &nrhs, A, &N, pivot, b, &N, work, &lwork, &info);
204  if (info < 0) {
205  cout << "Wrong Input !" << endl;
206  } else if (info > 0) {
207  cout << "Singular Matrix !" << endl;
208  }
209 }
210 
211 
212 /*----------------------------------------------------------------------------*/
213 /* Brief Change History of This File */
214 /*----------------------------------------------------------------------------*/
215 /* Author Date Actions */
216 /*----------------------------------------------------------------------------*/
217 /* Shizhe Li Oct/21/2021 Create file */
218 /*----------------------------------------------------------------------------*/
void SYSSolve(const int &nrhs, const char *uplo, const int &N, double *A, double *b, int *pivot, double *work, const int &lwork)
Calls dsysy to solve the linear system for symm matrices.
Definition: DenseMat.cpp:199
double Ddot(int n, double *a, double *b)
Dot product of two double vectors stored as pointers.
Definition: DenseMat.cpp:43
void DaABpbC(const int &m, const int &n, const int &k, const double &alpha, const double *A, const double *B, const double &beta, double *C)
Computes C' = alpha B'A' + beta C', all matrices are column-major.
Definition: DenseMat.cpp:76
void Dscalar(const int &n, const double &alpha, double *x)
Scales a vector by a constant.
Definition: DenseMat.cpp:62
void DaAxpby(const int &m, const int &n, const double &a, const double *A, const double *x, const double &b, double *y)
Computes y = a A x + b y.
Definition: DenseMat.cpp:173
void LUSolve(const int &nrhs, const int &N, double *A, double *b, int *pivot)
Calls dgesv to solve the linear system for general matrices.
Definition: DenseMat.cpp:186
void Daxpy(const int &n, const double &alpha, const double *x, double *y)
Constant times a vector plus a vector.
Definition: DenseMat.cpp:69
void MinEigenSY(const int &N, float *A, float *w, float *work, const int &lwork)
Calculate the minimal eigenvalue for sysmetric matrix with mkl lapack.
Definition: DenseMat.cpp:14
double Dnorm1(const int &N, double *x)
Computes the L1-norm of a vector.
Definition: DenseMat.cpp:50
void Dcopy(const int &N, double *dst, const double *src)
Calculate the minimal eigenvalue for sysmetric matrix with mkl lapack.
Definition: DenseMat.cpp:37
double Dnorm2(const int &N, double *x)
Computes the L2-norm of a vector.
Definition: DenseMat.cpp:56
Operations about small dense mat.
double dasum_(const int *n, double *x, const int *incx)
Computes the sum of the absolute values of a vector.
int dsysv_(const char *uplo, const int *n, const int *nrhs, double *A, const int *lda, int *ipiv, double *b, const int *ldb, double *work, const int *lwork, int *info)
Computes the solution to system of linear equations A * X = B for symm matrices.
int dgesv_(const int *n, const int *nrhs, double *A, const int *lda, int *ipiv, double *b, const int *ldb, int *info)
Computes the solution to system of linear equations A * X = B for general matrices.
int ssyevd_(char *jobz, char *uplo, const int *n, float *A, const int *lda, float *w, float *work, const int *lwork, int *iwork, const int *liwork, int *info)
Computes the eigenvalues and, optionally, the leftand /or right eigenvectors for SY matrices.
int daxpy_(const int *n, const double *alpha, const double *x, const int *incx, double *y, const int *incy)
Constant times a vector plus a vector.
int dcopy_(const int *n, const double *src, const int *incx, double *dst, const int *incy)
Copies a vector, src, to a vector, dst.
int dgemm_(const char *transa, const char *transb, const int *m, const int *n, const int *k, const double *alpha, const double *A, const int *lda, const double *B, const int *ldb, const double *beta, double *C, const int *ldc)
Performs matrix-matrix operations C : = alpha * op(A) * op(B) + beta * C.
double dnrm2_(const int *n, double *x, const int *incx)
Computes the Euclidean norm of a vector.
void dscal_(const int *n, const double *alpha, double *x, const int *incx)
Scales a vector by a constant.
double ddot_(const int *n, double *a, const int *inca, double *b, const int *incb)
Forms the dot product of two vectors.