1 /// @file 2 /// Test creation, action, and destruction for mass matrix operator 3 /// \test Test creation, action, and destruction for mass matrix operator 4 #include <ceed.h> 5 #include <stdlib.h> 6 #include <math.h> 7 #include "t320-basis.h" 8 #include "t510-operator.h" 9 10 int main(int argc, char **argv) { 11 Ceed ceed; 12 CeedElemRestriction elem_restr_x, elem_restr_u, elem_restr_qd_i; 13 CeedBasis basis_x, basis_u; 14 CeedQFunction qf_setup, qf_mass; 15 CeedOperator op_setup, op_mass; 16 CeedVector q_data, X, U, V; 17 const CeedScalar *hv; 18 CeedInt num_elem = 12, dim = 2, P = 6, Q = 4; 19 CeedInt nx = 3, ny = 2; 20 CeedInt row, col, offset; 21 CeedInt num_dofs = (nx*2+1)*(ny*2+1), num_qpts = num_elem*Q; 22 CeedInt ind_x[num_elem*P]; 23 CeedScalar x[dim*num_dofs]; 24 CeedScalar q_ref[dim*Q], q_weight[Q]; 25 CeedScalar interp[P*Q], grad[dim*P*Q]; 26 27 CeedInit(argv[1], &ceed); 28 29 for (CeedInt i=0; i<num_dofs; i++) { 30 x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1)); 31 x[i+num_dofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1)); 32 } 33 for (CeedInt i=0; i<num_elem/2; i++) { 34 col = i % nx; 35 row = i / nx; 36 offset = col*2 + row*(nx*2+1)*2; 37 38 ind_x[i*2*P + 0] = 2 + offset; 39 ind_x[i*2*P + 1] = 9 + offset; 40 ind_x[i*2*P + 2] = 16 + offset; 41 ind_x[i*2*P + 3] = 1 + offset; 42 ind_x[i*2*P + 4] = 8 + offset; 43 ind_x[i*2*P + 5] = 0 + offset; 44 45 ind_x[i*2*P + 6] = 14 + offset; 46 ind_x[i*2*P + 7] = 7 + offset; 47 ind_x[i*2*P + 8] = 0 + offset; 48 ind_x[i*2*P + 9] = 15 + offset; 49 ind_x[i*2*P + 10] = 8 + offset; 50 ind_x[i*2*P + 11] = 16 + offset; 51 } 52 53 // Restrictions 54 CeedElemRestrictionCreate(ceed, num_elem, P, dim, num_dofs, dim*num_dofs, 55 CEED_MEM_HOST, 56 CEED_USE_POINTER, ind_x, &elem_restr_x); 57 58 CeedElemRestrictionCreate(ceed, num_elem, P, 1, 1, num_dofs, CEED_MEM_HOST, 59 CEED_USE_POINTER, ind_x, &elem_restr_u); 60 CeedInt strides_qd[3] = {1, Q, Q}; 61 CeedElemRestrictionCreateStrided(ceed, num_elem, Q, 1, num_qpts, strides_qd, 62 &elem_restr_qd_i); 63 64 // Bases 65 buildmats(q_ref, q_weight, interp, grad); 66 CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, q_ref, 67 q_weight, &basis_x); 68 69 buildmats(q_ref, q_weight, interp, grad); 70 CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, q_ref, 71 q_weight, &basis_u); 72 73 // QFunctions 74 CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 75 CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT); 76 CeedQFunctionAddInput(qf_setup, "dx", dim*dim, CEED_EVAL_GRAD); 77 CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 78 79 CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 80 CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 81 CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 82 CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 83 84 // Operators 85 CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, 86 &op_setup); 87 88 CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, 89 &op_mass); 90 91 CeedVectorCreate(ceed, dim*num_dofs, &X); 92 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x); 93 CeedVectorCreate(ceed, num_qpts, &q_data); 94 95 CeedOperatorSetField(op_setup, "_weight", CEED_ELEMRESTRICTION_NONE, basis_x, 96 CEED_VECTOR_NONE); 97 CeedOperatorSetField(op_setup, "dx", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE); 98 CeedOperatorSetField(op_setup, "rho", elem_restr_qd_i, CEED_BASIS_COLLOCATED, 99 CEED_VECTOR_ACTIVE); 100 101 CeedOperatorSetField(op_mass, "rho", elem_restr_qd_i, CEED_BASIS_COLLOCATED, 102 q_data); 103 CeedOperatorSetField(op_mass, "u", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE); 104 CeedOperatorSetField(op_mass, "v", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE); 105 106 CeedOperatorApply(op_setup, X, q_data, CEED_REQUEST_IMMEDIATE); 107 108 CeedVectorCreate(ceed, num_dofs, &U); 109 CeedVectorSetValue(U, 0.0); 110 CeedVectorCreate(ceed, num_dofs, &V); 111 112 CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE); 113 114 // Check output 115 CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv); 116 for (CeedInt i=0; i<num_dofs; i++) 117 if (fabs(hv[i]) > 1e-14) printf("[%d] v %g != 0.0\n",i, hv[i]); 118 CeedVectorRestoreArrayRead(V, &hv); 119 120 CeedQFunctionDestroy(&qf_setup); 121 CeedQFunctionDestroy(&qf_mass); 122 CeedOperatorDestroy(&op_setup); 123 CeedOperatorDestroy(&op_mass); 124 CeedElemRestrictionDestroy(&elem_restr_u); 125 CeedElemRestrictionDestroy(&elem_restr_x); 126 CeedElemRestrictionDestroy(&elem_restr_qd_i); 127 CeedBasisDestroy(&basis_u); 128 CeedBasisDestroy(&basis_x); 129 CeedVectorDestroy(&X); 130 CeedVectorDestroy(&U); 131 CeedVectorDestroy(&V); 132 CeedVectorDestroy(&q_data); 133 CeedDestroy(&ceed); 134 return 0; 135 } 136