xref: /libCEED/tests/t304-basis.c (revision a2e5d304d0c7d96eecfcbbd32f1ea5194beb84ca)
1 /// @file
2 /// Test Symmetric Schur Decomposition
3 /// \test Test Symmetric Schur Decomposition
4 #include <ceed.h>
5 #include <ceed/backend.h>
6 #include <math.h>
7 
8 int main(int argc, char **argv) {
9   Ceed              ceed;
10   CeedInt           p = 4;
11   CeedScalar        M[16], Q[16], lambda[4], Q_lambda_Qt[16];
12   CeedBasis         basis;
13   const CeedScalar *interpolation, *quadrature_weights;
14 
15   CeedInit(argv[1], &ceed);
16 
17   // Create mass matrix
18   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, p, CEED_GAUSS, &basis);
19   CeedBasisGetInterp(basis, &interpolation);
20   CeedBasisGetQWeights(basis, &quadrature_weights);
21   for (int i = 0; i < p; i++) {
22     for (int j = 0; j < p; j++) {
23       CeedScalar sum = 0;
24       for (int k = 0; k < p; k++) sum += interpolation[p * k + i] * quadrature_weights[k] * interpolation[p * k + j];
25       M[p * i + j] = sum;
26       Q[p * i + j] = sum;
27     }
28   }
29 
30   CeedSymmetricSchurDecomposition(ceed, Q, lambda, p);
31 
32   // Check diagonalization of M
33   for (int i = 0; i < p; i++) {
34     for (int j = 0; j < p; j++) {
35       CeedScalar sum = 0;
36       for (int k = 0; k < p; k++) sum += Q[p * i + k] * lambda[k] * Q[p * j + k];
37       Q_lambda_Qt[p * i + j] = sum;
38     }
39   }
40   for (int i = 0; i < p; i++) {
41     for (int j = 0; j < p; j++) {
42       if (fabs(M[p * i + j] - Q_lambda_Qt[p * i + j]) > 100. * CEED_EPSILON) {
43         // LCOV_EXCL_START
44         printf("Error in diagonalization [%" CeedInt_FMT ", %" CeedInt_FMT "]: %f != %f\n", i, j, M[p * i + j], Q_lambda_Qt[p * i + j]);
45         // LCOV_EXCL_STOP
46       }
47     }
48   }
49 
50   CeedBasisDestroy(&basis);
51   CeedDestroy(&ceed);
52   return 0;
53 }
54