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, Erestrictlini; 13 CeedBasis bx, bu; 14 CeedQFunction qf_setup, qf_mass; 15 CeedOperator op_setup, op_mass; 16 CeedVector qdata, X, A, u, v; 17 const CeedScalar *a, *q; 18 CeedInt nelem = 6, P = 3, Q = 4, dim = 2; 19 CeedInt nx = 3, ny = 2; 20 CeedInt ndofs = (nx*2+1)*(ny*2+1), nqpts = nelem*Q*Q; 21 CeedInt indx[nelem*P*P]; 22 CeedScalar x[dim*ndofs]; 23 24 CeedInit(argv[1], &ceed); 25 26 // DoF Coordinates 27 for (CeedInt i=0; i<nx*2+1; i++) 28 for (CeedInt j=0; j<ny*2+1; j++) { 29 x[i+j*(nx*2+1)+0*ndofs] = (CeedScalar) i / (2*nx); 30 x[i+j*(nx*2+1)+1*ndofs] = (CeedScalar) j / (2*ny); 31 } 32 CeedVectorCreate(ceed, dim*ndofs, &X); 33 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x); 34 35 // Qdata Vector 36 CeedVectorCreate(ceed, nqpts, &qdata); 37 38 // Element Setup 39 for (CeedInt i=0; i<nelem; i++) { 40 CeedInt col, row, offset; 41 col = i % nx; 42 row = i / nx; 43 offset = col*(P-1) + row*(nx*2+1)*(P-1); 44 for (CeedInt j=0; j<P; j++) 45 for (CeedInt k=0; k<P; k++) 46 indx[P*(P*i+k)+j] = offset + k*(nx*2+1) + j; 47 } 48 49 // Restrictions 50 CeedElemRestrictionCreate(ceed, nelem, P*P, ndofs, dim, CEED_MEM_HOST, 51 CEED_USE_POINTER, indx, &Erestrictx); 52 CeedElemRestrictionCreateIdentity(ceed, nelem, P*P, nelem*P*P, dim, 53 &Erestrictxi); 54 55 CeedElemRestrictionCreate(ceed, nelem, P*P, ndofs, 1, CEED_MEM_HOST, 56 CEED_USE_POINTER, indx, &Erestrictu); 57 CeedElemRestrictionCreateIdentity(ceed, nelem, Q*Q, nqpts, 1, &Erestrictui); 58 59 // Bases 60 CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, P, Q, CEED_GAUSS, &bx); 61 CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, P, Q, CEED_GAUSS, &bu); 62 63 // QFunctions 64 CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 65 CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT); 66 CeedQFunctionAddInput(qf_setup, "dx", dim*dim, CEED_EVAL_GRAD); 67 CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 68 69 CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 70 CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 71 CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 72 CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 73 74 // Operators 75 CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup); 76 CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE, bx, 77 CEED_VECTOR_NONE); 78 CeedOperatorSetField(op_setup, "dx", Erestrictx, CEED_NOTRANSPOSE, bx, 79 CEED_VECTOR_ACTIVE); 80 CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE, 81 CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 82 83 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 84 CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE, 85 CEED_BASIS_COLLOCATED, qdata); 86 CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE, bu, 87 CEED_VECTOR_ACTIVE); 88 CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE, bu, 89 CEED_VECTOR_ACTIVE); 90 91 // Apply Setup Operator 92 CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE); 93 94 // Assemble QFunction 95 CeedOperatorAssembleLinearQFunction(op_mass, &A, &Erestrictlini, 96 CEED_REQUEST_IMMEDIATE); 97 98 // Check output 99 CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a); 100 CeedVectorGetArrayRead(qdata, CEED_MEM_HOST, &q); 101 for (CeedInt i=0; i<nqpts; i++) 102 if (fabs(q[i] - a[i]) > 1e-9) 103 // LCOV_EXCL_START 104 printf("Error: A[%d] = %f != %f\n", i, a[i], q[i]); 105 // LCOV_EXCL_STOP 106 CeedVectorRestoreArrayRead(A, &a); 107 CeedVectorRestoreArrayRead(qdata, &q); 108 109 // Apply original Mass Operator 110 CeedVectorCreate(ceed, ndofs, &u); 111 CeedVectorSetValue(u, 1.0); 112 CeedVectorCreate(ceed, ndofs, &v); 113 CeedVectorSetValue(v, 0.0); 114 CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 115 116 // Check output 117 CeedScalar area = 0.0; 118 const CeedScalar *vv; 119 CeedVectorGetArrayRead(v, CEED_MEM_HOST, &vv); 120 for (CeedInt i=0; i<ndofs; i++) 121 area += vv[i]; 122 CeedVectorRestoreArrayRead(v, &vv); 123 if (fabs(area - 1.0) > 1e-14) 124 // LCOV_EXCL_START 125 printf("Error: True operator computed area = %f != 1.0\n", area); 126 // LCOV_EXCL_STOP 127 128 // Switch to new qdata 129 CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a); 130 CeedVectorSetArray(qdata, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)a); 131 CeedVectorRestoreArrayRead(A, &a); 132 133 // Apply new Mass Operator 134 CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 135 136 // Check output 137 area = 0.0; 138 CeedVectorGetArrayRead(v, CEED_MEM_HOST, &vv); 139 for (CeedInt i=0; i<ndofs; i++) 140 area += vv[i]; 141 CeedVectorRestoreArrayRead(v, &vv); 142 if (fabs(area - 1.0) > 1e-10) 143 // LCOV_EXCL_START 144 printf("Error: Linearized operator computed area = %f != 1.0\n", area); 145 // LCOV_EXCL_STOP 146 147 // Cleanup 148 CeedQFunctionDestroy(&qf_setup); 149 CeedQFunctionDestroy(&qf_mass); 150 CeedOperatorDestroy(&op_setup); 151 CeedOperatorDestroy(&op_mass); 152 CeedElemRestrictionDestroy(&Erestrictu); 153 CeedElemRestrictionDestroy(&Erestrictx); 154 CeedElemRestrictionDestroy(&Erestrictui); 155 CeedElemRestrictionDestroy(&Erestrictxi); 156 CeedElemRestrictionDestroy(&Erestrictlini); 157 CeedBasisDestroy(&bu); 158 CeedBasisDestroy(&bx); 159 CeedVectorDestroy(&X); 160 CeedVectorDestroy(&A); 161 CeedVectorDestroy(&qdata); 162 CeedVectorDestroy(&u); 163 CeedVectorDestroy(&v); 164 CeedDestroy(&ceed); 165 return 0; 166 } 167