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