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