1 /// @file 2 /// Test creation creation, action, and destruction for mass matrix operator 3 /// \test Test creation creation, action, and destruction for mass matrix operator 4 #include <ceed.h> 5 #include <stdlib.h> 6 #include <math.h> 7 8 static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in, 9 CeedScalar *const *out) { 10 const CeedScalar *weight = in[0], *dxdX = in[1]; 11 CeedScalar *rho = out[0]; 12 for (CeedInt i=0; i<Q; i++) { 13 rho[i] = weight[i] * dxdX[i]; 14 } 15 return 0; 16 } 17 18 static int mass(void *ctx, CeedInt Q, const CeedScalar *const *in, 19 CeedScalar *const *out) { 20 const CeedScalar *rho = in[0], *u = in[1]; 21 CeedScalar *v = out[0]; 22 for (CeedInt i=0; i<Q; i++) { 23 v[i] = rho[i] * u[i]; 24 v[Q+i] = rho[i] * u[Q+i]; 25 } 26 return 0; 27 } 28 29 int main(int argc, char **argv) { 30 Ceed ceed; 31 CeedElemRestriction Erestrictx, Erestrictu, Erestrictxi, Erestrictui; 32 CeedBasis bx, bu; 33 CeedQFunction qf_setup, qf_mass; 34 CeedOperator op_setup, op_mass; 35 CeedVector qdata, X, U, V; 36 CeedScalar *hu; 37 const CeedScalar *hv; 38 CeedInt nelem = 15, P = 5, Q = 8; 39 CeedInt Nx = nelem+1, Nu = nelem*(P-1)+1; 40 CeedInt indx[nelem*2], indu[nelem*P]; 41 CeedScalar x[Nx]; 42 CeedScalar sum1, sum2; 43 44 CeedInit(argv[1], &ceed); 45 for (CeedInt i=0; i<Nx; i++) x[i] = (CeedScalar) i / (Nx - 1); 46 for (CeedInt i=0; i<nelem; i++) { 47 indx[2*i+0] = i; 48 indx[2*i+1] = i+1; 49 } 50 // Restrictions 51 CeedElemRestrictionCreate(ceed, nelem, 2, Nx, 1, CEED_MEM_HOST, 52 CEED_USE_POINTER, indx, &Erestrictx); 53 CeedElemRestrictionCreateIdentity(ceed, nelem, 2, nelem*2, 1, &Erestrictxi); 54 55 for (CeedInt i=0; i<nelem; i++) { 56 for (CeedInt j=0; j<P; j++) { 57 indu[P*i+j] = i*(P-1) + j; 58 } 59 } 60 CeedElemRestrictionCreate(ceed, nelem, P, Nu, 2, CEED_MEM_HOST, 61 CEED_USE_POINTER, indu, &Erestrictu); 62 CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Q*nelem, 1, &Erestrictui); 63 64 // Bases 65 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bx); 66 CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, P, Q, CEED_GAUSS, &bu); 67 68 // QFunctions 69 CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup); 70 CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT); 71 CeedQFunctionAddInput(qf_setup, "x", 1, CEED_EVAL_GRAD); 72 CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 73 74 CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass); 75 CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 76 CeedQFunctionAddInput(qf_mass, "u", 2, CEED_EVAL_INTERP); 77 CeedQFunctionAddOutput(qf_mass, "v", 2, CEED_EVAL_INTERP); 78 79 // Operators 80 CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup); 81 82 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 83 84 CeedVectorCreate(ceed, Nx, &X); 85 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x); 86 CeedVectorCreate(ceed, nelem*Q, &qdata); 87 88 CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE, 89 bx, CEED_VECTOR_NONE); 90 CeedOperatorSetField(op_setup, "x", Erestrictx, CEED_NOTRANSPOSE, 91 bx, CEED_VECTOR_ACTIVE); 92 CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE, 93 CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 94 95 CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE, 96 CEED_BASIS_COLLOCATED, qdata); 97 CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_TRANSPOSE, 98 bu, CEED_VECTOR_ACTIVE); 99 CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_TRANSPOSE, 100 bu, CEED_VECTOR_ACTIVE); 101 102 CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE); 103 104 CeedVectorCreate(ceed, 2*Nu, &U); 105 CeedVectorGetArray(U, CEED_MEM_HOST, &hu); 106 for (int i = 0; i < Nu; i++) { 107 hu[2*i] = 1.0; 108 hu[2*i+1] = 2.0; 109 } 110 CeedVectorRestoreArray(U, &hu); 111 CeedVectorCreate(ceed, 2*Nu, &V); 112 CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE); 113 114 // Check output 115 CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv); 116 sum1 = 0.; sum2 = 0.; 117 for (CeedInt i=0; i<Nu; i++) { 118 sum1 += hv[2*i]; 119 sum2 += hv[2*i+1]; 120 } 121 if (fabs(sum1-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum1); 122 if (fabs(sum2-2.)>1e-10) printf("Computed Area: %f != True Area: 2.0\n", sum2); 123 CeedVectorRestoreArrayRead(V, &hv); 124 125 CeedQFunctionDestroy(&qf_setup); 126 CeedQFunctionDestroy(&qf_mass); 127 CeedOperatorDestroy(&op_setup); 128 CeedOperatorDestroy(&op_mass); 129 CeedElemRestrictionDestroy(&Erestrictu); 130 CeedElemRestrictionDestroy(&Erestrictx); 131 CeedElemRestrictionDestroy(&Erestrictui); 132 CeedElemRestrictionDestroy(&Erestrictxi); 133 CeedBasisDestroy(&bu); 134 CeedBasisDestroy(&bx); 135 CeedVectorDestroy(&X); 136 CeedVectorDestroy(&U); 137 CeedVectorDestroy(&V); 138 CeedVectorDestroy(&qdata); 139 CeedDestroy(&ceed); 140 return 0; 141 } 142