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, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, 76 &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, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, 85 &op_mass); 86 CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE, 87 CEED_BASIS_COLLOCATED, qdata); 88 CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE, bu, 89 CEED_VECTOR_ACTIVE); 90 CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE, bu, 91 CEED_VECTOR_ACTIVE); 92 93 // Apply Setup Operator 94 CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE); 95 96 // Assemble QFunction 97 CeedOperatorAssembleLinearQFunction(op_mass, &A, &Erestrictlini, 98 CEED_REQUEST_IMMEDIATE); 99 100 // Check output 101 CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a); 102 CeedVectorGetArrayRead(qdata, CEED_MEM_HOST, &q); 103 for (CeedInt i=0; i<nqpts; i++) 104 if (fabs(q[i] - a[i]) > 1e-9) 105 // LCOV_EXCL_START 106 printf("Error: A[%d] = %f != %f\n", i, a[i], q[i]); 107 // LCOV_EXCL_STOP 108 CeedVectorRestoreArrayRead(A, &a); 109 CeedVectorRestoreArrayRead(qdata, &q); 110 111 // Apply original Mass Operator 112 CeedVectorCreate(ceed, ndofs, &u); 113 CeedVectorSetValue(u, 1.0); 114 CeedVectorCreate(ceed, ndofs, &v); 115 CeedVectorSetValue(v, 0.0); 116 CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 117 118 // Check output 119 CeedScalar area = 0.0; 120 const CeedScalar *vv; 121 CeedVectorGetArrayRead(v, CEED_MEM_HOST, &vv); 122 for (CeedInt i=0; i<ndofs; i++) 123 area += vv[i]; 124 CeedVectorRestoreArrayRead(v, &vv); 125 if (fabs(area - 1.0) > 1e-14) 126 // LCOV_EXCL_START 127 printf("Error: True operator computed area = %f != 1.0\n", area); 128 // LCOV_EXCL_STOP 129 130 // Switch to new qdata 131 CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a); 132 CeedVectorSetArray(qdata, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)a); 133 CeedVectorRestoreArrayRead(A, &a); 134 135 // Apply new Mass Operator 136 CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 137 138 // Check output 139 area = 0.0; 140 CeedVectorGetArrayRead(v, CEED_MEM_HOST, &vv); 141 for (CeedInt i=0; i<ndofs; i++) 142 area += vv[i]; 143 CeedVectorRestoreArrayRead(v, &vv); 144 if (fabs(area - 1.0) > 1e-10) 145 // LCOV_EXCL_START 146 printf("Error: Linearized operator computed area = %f != 1.0\n", area); 147 // LCOV_EXCL_STOP 148 149 // Cleanup 150 CeedQFunctionDestroy(&qf_setup); 151 CeedQFunctionDestroy(&qf_mass); 152 CeedOperatorDestroy(&op_setup); 153 CeedOperatorDestroy(&op_mass); 154 CeedElemRestrictionDestroy(&Erestrictu); 155 CeedElemRestrictionDestroy(&Erestrictx); 156 CeedElemRestrictionDestroy(&Erestrictui); 157 CeedElemRestrictionDestroy(&Erestrictxi); 158 CeedElemRestrictionDestroy(&Erestrictlini); 159 CeedBasisDestroy(&bu); 160 CeedBasisDestroy(&bx); 161 CeedVectorDestroy(&X); 162 CeedVectorDestroy(&A); 163 CeedVectorDestroy(&qdata); 164 CeedVectorDestroy(&u); 165 CeedVectorDestroy(&v); 166 CeedDestroy(&ceed); 167 return 0; 168 } 169