OpenCAEPoro  v0.5.0
A simulator for multicomponent porous media flow
DenseMat.cpp
Go to the documentation of this file.
1 
12 #include "DenseMat.hpp"
13 
14 void CalEigenSY(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 // void MinEigenS(const int& N, float* a, float* w)
29 //{
30 // MKL_INT info = LAPACKE_ssyevd(LAPACK_COL_MAJOR, 'N', 'U', N, a, N, w);
31 // if (info > 0) {
32 // cout << "failed to compute eigenvalues!" << endl;
33 // }
34 // }
35 
36 void Dcopy(const int& N, double* dst, const double* src)
37 {
38  const int incx = 1, incy = 1;
39  dcopy_(&N, src, &incx, dst, &incy);
40 }
41 
42 double Ddot(int n, double* a, double* b)
43 {
44  const int inca = 1, incb = 1;
45  return ddot_(&n, a, &inca, b, &incb);
46 }
47 
48 // WARNING: absolute sum!
49 double Dnorm1(const int& N, double* x)
50 {
51  const int incx = 1;
52  return dasum_(&N, x, &incx);
53 }
54 
55 double Dnorm2(const int& N, double* x)
56 {
57  const int incx = 1;
58  return dnrm2_(&N, x, &incx);
59 }
60 
61 void Dscalar(const int& n, const double& alpha, double* x)
62 {
63  // x = a x
64  const int incx = 1;
65  dscal_(&n, &alpha, x, &incx);
66 }
67 
68 void Daxpy(const int& n, const double& alpha, const double* x, double* y)
69 {
70  // y= ax +y
71  const int incx = 1, incy = 1;
72  daxpy_(&n, &alpha, x, &incx, y, &incy);
73 }
74 
75 void DaABpbC(const int& m,
76  const int& n,
77  const int& k,
78  const double& alpha,
79  const double* A,
80  const double* B,
81  const double& beta,
82  double* C)
83 {
84  /* C' = alpha B'A' + beta C'
85  * A: m x k
86  * B: k x n
87  * C: m x n
88  * all column majored matrices, no tranpose
89  * A' in col-order in Fortran = A in row-order in C/Cpp
90  */
91 
92  const char transa = 'N', transb = 'N';
93  dgemm_(&transa, &transb, &n, &m, &k, &alpha, B, &n, A, &k, &beta, C, &n);
94 }
95 
96 void myDABpC(const int& m,
97  const int& n,
98  const int& k,
99  const double* A,
100  const double* B,
101  double* C)
102 {
103  // C = AB + C
104  // A: m*n B:n*k C:m*k
105  // all matrix are row majored matrices
106 
107  for (int i = 0; i < m; i++) {
108  for (int j = 0; j < k; j++) {
109  for (int m = 0; m < n; m++) {
110  C[i * k + j] += A[i * n + m] * B[m * k + j];
111  }
112  }
113  }
114 }
115 
116 void myDABpCp(const int& m,
117  const int& n,
118  const int& k,
119  const double* A,
120  const double* B,
121  double* C,
122  const int* flag,
123  const int N)
124 {
125  for (int i = 0; i < m; i++) {
126  for (int j = 0; j < k; j++) {
127  for (int p = 0; p < 3; p++) {
128  if (flag[p] != 0) C[i * k + j] += A[i * n + p] * B[p * k + j];
129  }
130  for (int p = 0; p < 2; p++) {
131  if (flag[p] != 0) {
132  for (int m = 0; m < N; m++) {
133  C[i * k + j] += A[i * n + 3 + p * (N + 1) + m] *
134  B[(3 + p * (N + 1) + m) * k + j];
135  }
136  }
137  }
138  }
139  }
140 }
141 
142 void myDABpCp1(const int& m,
143  const int& n,
144  const int& k,
145  const double* A,
146  const double* B,
147  double* C,
148  const int* flag,
149  const int N)
150 {
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 + p] * B[s * 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 + 3 + p * (N + 1) + m] * B[s * k + j];
165  s++;
166  }
167  }
168  }
169  }
170  }
171 }
172 
173 void myDABpCp2(const int& m,
174  const int& n,
175  const int& k,
176  const double* A,
177  const double* B,
178  double* C,
179  const int* flag,
180  const int N)
181 {
182  for (int i = 0; i < m; i++) {
183  for (int j = 0; j < k; j++) {
184  int s = 0;
185  for (int p = 0; p < 3; p++) {
186  if (flag[p] != 0) {
187  C[i * k + j] += A[i * n + s] * B[p * k + j];
188  s++;
189  }
190  }
191  for (int p = 0; p < 2; p++) {
192  if (flag[p] != 0) {
193  for (int m = 0; m < N; m++) {
194  C[i * k + j] += A[i * n + s] * B[(3 + p * (N + 1) + m) * k + j];
195  s++;
196  }
197  }
198  }
199  }
200  }
201 }
202 
203 void DaAxpby(const int& m,
204  const int& n,
205  const double& a,
206  const double* A,
207  const double* x,
208  const double& b,
209  double* y)
210 {
211  /* y= aAx+by
212  */
213  for (int i = 0; i < m; i++) {
214  y[i] = b * y[i];
215  for (int j = 0; j < n; j++) {
216  y[i] += a * A[i * n + j] * x[j];
217  }
218  }
219 }
220 
221 void LUSolve(const int& nrhs, const int& N, double* A, double* b, int* pivot)
222 {
223  int info;
224 
225  dgesv_(&N, &nrhs, A, &N, pivot, b, &N, &info);
226 
227  if (info < 0) {
228  cout << "Wrong Input !" << endl;
229  } else if (info > 0) {
230  cout << "Singular Matrix !" << endl;
231  }
232 }
233 
234 int SYSSolve(const int& nrhs,
235  const char* uplo,
236  const int& N,
237  double* A,
238  double* b,
239  int* pivot,
240  double* work,
241  const int& lwork)
242 {
243  int info;
244 
245  dsysv_(uplo, &N, &nrhs, A, &N, pivot, b, &N, work, &lwork, &info);
246  if (info < 0) {
247  cout << "Wrong Input !" << endl;
248  } else if (info > 0) {
249  cout << "Singular Matrix !" << endl;
250  }
251 
252  return info;
253 }
254 
255 /*----------------------------------------------------------------------------*/
256 /* Brief Change History of This File */
257 /*----------------------------------------------------------------------------*/
258 /* Author Date Actions */
259 /*----------------------------------------------------------------------------*/
260 /* Shizhe Li Oct/21/2021 Create file */
261 /*----------------------------------------------------------------------------*/
double Ddot(int n, double *a, double *b)
Dot product of two double vectors stored as pointers.
Definition: DenseMat.cpp:42
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:75
void Dscalar(const int &n, const double &alpha, double *x)
Scales a vector by a constant.
Definition: DenseMat.cpp:61
void CalEigenSY(const int &N, float *A, float *w, float *work, const int &lwork)
Calculate the minimal eigenvalue for symmetric matrix with mkl lapack.
Definition: DenseMat.cpp:14
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:203
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:221
void Daxpy(const int &n, const double &alpha, const double *x, double *y)
Constant times a vector plus a vector.
Definition: DenseMat.cpp:68
double Dnorm1(const int &N, double *x)
Computes the L1-norm of a vector.
Definition: DenseMat.cpp:49
int 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:234
void Dcopy(const int &N, double *dst, const double *src)
Calculate the minimal eigenvalue for symmetric matrix with mkl lapack.
Definition: DenseMat.cpp:36
double Dnorm2(const int &N, double *x)
Computes the L2-norm of a vector.
Definition: DenseMat.cpp:55
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)
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.