1 /// @file 2 /// Test assembly of mass matrix operator QFunction 3 /// \test Test assembly of mass matrix operator QFunction 4 #include <ceed.h> 5 #include <stdlib.h> 6 #include <math.h> 7 #include "t510-operator.h" 8 9 int main(int argc, char **argv) { 10 Ceed ceed; 11 CeedElemRestriction Erestrictx, Erestrictu, 12 Erestrictxi, Erestrictui; 13 CeedBasis bx, bu; 14 CeedQFunction qf_setup, qf_mass; 15 CeedOperator op_setup, op_mass; 16 CeedVector qdata, X, A, U, V; 17 CeedInt nelem = 6, P = 3, Q = 4, dim = 2; 18 CeedInt nx = 3, ny = 2; 19 CeedInt ndofs = (nx*2+1)*(ny*2+1), nqpts = nelem*Q*Q; 20 CeedInt indx[nelem*P*P]; 21 CeedScalar x[dim*ndofs], assembledTrue[ndofs]; 22 CeedScalar *u; 23 const CeedScalar *a, *v; 24 25 CeedInit(argv[1], &ceed); 26 27 // DoF Coordinates 28 for (CeedInt i=0; i<nx*2+1; i++) 29 for (CeedInt j=0; j<ny*2+1; j++) { 30 x[i+j*(nx*2+1)+0*ndofs] = (CeedScalar) i / (2*nx); 31 x[i+j*(nx*2+1)+1*ndofs] = (CeedScalar) j / (2*ny); 32 } 33 CeedVectorCreate(ceed, dim*ndofs, &X); 34 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x); 35 36 // Qdata Vector 37 CeedVectorCreate(ceed, nqpts, &qdata); 38 39 // Element Setup 40 for (CeedInt i=0; i<nelem; i++) { 41 CeedInt col, row, offset; 42 col = i % nx; 43 row = i / nx; 44 offset = col*(P-1) + row*(nx*2+1)*(P-1); 45 for (CeedInt j=0; j<P; j++) 46 for (CeedInt k=0; k<P; k++) 47 indx[P*(P*i+k)+j] = offset + k*(nx*2+1) + j; 48 } 49 50 // Restrictions 51 CeedElemRestrictionCreate(ceed, nelem, P*P, ndofs, dim, CEED_MEM_HOST, 52 CEED_USE_POINTER, indx, &Erestrictx); 53 CeedElemRestrictionCreateIdentity(ceed, nelem, P*P, nelem*P*P, dim, 54 &Erestrictxi); 55 56 CeedElemRestrictionCreate(ceed, nelem, P*P, ndofs, 1, CEED_MEM_HOST, 57 CEED_USE_POINTER, indx, &Erestrictu); 58 CeedElemRestrictionCreateIdentity(ceed, nelem, Q*Q, nqpts, 1, &Erestrictui); 59 60 // Bases 61 CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, P, Q, CEED_GAUSS, &bx); 62 CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, P, Q, CEED_GAUSS, &bu); 63 64 // QFunctions 65 CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 66 CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT); 67 CeedQFunctionAddInput(qf_setup, "dx", dim*dim, CEED_EVAL_GRAD); 68 CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 69 70 CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 71 CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 72 CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 73 CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 74 75 // Operators 76 CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup); 77 CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE, bx, 78 CEED_VECTOR_NONE); 79 CeedOperatorSetField(op_setup, "dx", Erestrictx, CEED_NOTRANSPOSE, bx, 80 CEED_VECTOR_ACTIVE); 81 CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE, 82 CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 83 84 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 85 CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE, 86 CEED_BASIS_COLLOCATED, qdata); 87 CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE, bu, 88 CEED_VECTOR_ACTIVE); 89 CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE, bu, 90 CEED_VECTOR_ACTIVE); 91 92 // Apply Setup Operator 93 CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE); 94 95 // Assemble diagonal 96 CeedOperatorAssembleLinearDiagonal(op_mass, &A, CEED_REQUEST_IMMEDIATE); 97 98 // Manually assemble diagonal 99 CeedVectorCreate(ceed, ndofs, &U); 100 CeedVectorSetValue(U, 0.0); 101 CeedVectorCreate(ceed, ndofs, &V); 102 for (int i=0; i<ndofs; i++) { 103 // Set input 104 CeedVectorGetArray(U, CEED_MEM_HOST, &u); 105 u[i] = 1.0; 106 if (i) 107 u[i-1] = 0.0; 108 CeedVectorRestoreArray(U, &u); 109 110 // Compute diag entry for DoF i 111 CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE); 112 113 // Retrieve entry 114 CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v); 115 assembledTrue[i] = v[i]; 116 CeedVectorRestoreArrayRead(V, &v); 117 } 118 119 // Check output 120 CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a); 121 for (int i=0; i<ndofs; i++) 122 if (fabs(a[i] - assembledTrue[i]) > 1E-14) 123 // LCOV_EXCL_START 124 printf("[%d] Error in assembly: %f != %f\n", i, a[i], assembledTrue[i]); 125 // LCOV_EXCL_STOP 126 127 // Cleanup 128 CeedQFunctionDestroy(&qf_setup); 129 CeedQFunctionDestroy(&qf_mass); 130 CeedOperatorDestroy(&op_setup); 131 CeedOperatorDestroy(&op_mass); 132 CeedElemRestrictionDestroy(&Erestrictu); 133 CeedElemRestrictionDestroy(&Erestrictx); 134 CeedElemRestrictionDestroy(&Erestrictui); 135 CeedElemRestrictionDestroy(&Erestrictxi); 136 CeedBasisDestroy(&bu); 137 CeedBasisDestroy(&bx); 138 CeedVectorDestroy(&X); 139 CeedVectorDestroy(&A); 140 CeedVectorDestroy(&qdata); 141 CeedVectorDestroy(&U); 142 CeedVectorDestroy(&V); 143 CeedDestroy(&ceed); 144 return 0; 145 } 146