1cae8b89aSjeremylt /// @file 2cae8b89aSjeremylt /// Test CeedOperatorApplyAdd 3cae8b89aSjeremylt /// \test Test CeedOperatorApplyAdd 4cae8b89aSjeremylt #include <ceed.h> 5cae8b89aSjeremylt #include <math.h> 6*49aac155SJeremy L Thompson #include <stdio.h> 72b730f8bSJeremy L Thompson #include <stdlib.h> 8cae8b89aSjeremylt 9cae8b89aSjeremylt #include "t500-operator.h" 10cae8b89aSjeremylt 11cae8b89aSjeremylt int main(int argc, char **argv) { 12cae8b89aSjeremylt Ceed ceed; 134fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data; 14d1d35e2fSjeremylt CeedBasis basis_x, basis_u; 15cae8b89aSjeremylt CeedQFunction qf_setup, qf_mass; 16cae8b89aSjeremylt CeedOperator op_setup, op_mass; 174fee36f0SJeremy L Thompson CeedVector q_data, x, u, v; 184fee36f0SJeremy L Thompson CeedInt num_elem = 15, p = 5, q = 8; 194fee36f0SJeremy L Thompson CeedInt num_nodes_x = num_elem + 1, num_nodes_u = num_elem * (p - 1) + 1; 204fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * 2], ind_u[num_elem * p]; 21cae8b89aSjeremylt 22cae8b89aSjeremylt CeedInit(argv[1], &ceed); 23cae8b89aSjeremylt 244fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_nodes_x, &x); 254fee36f0SJeremy L Thompson { 264fee36f0SJeremy L Thompson CeedScalar x_array[num_nodes_x]; 274fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1); 284fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 294fee36f0SJeremy L Thompson } 304fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_nodes_u, &u); 314fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_nodes_u, &v); 324fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_elem * q, &q_data); 334fee36f0SJeremy L Thompson 344fee36f0SJeremy L Thompson // Restrictions 35d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) { 36d1d35e2fSjeremylt ind_x[2 * i + 0] = i; 37d1d35e2fSjeremylt ind_x[2 * i + 1] = i + 1; 38cae8b89aSjeremylt } 394fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x); 4015910d16Sjeremylt 41d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) { 424fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) { 434fee36f0SJeremy L Thompson ind_u[p * i + j] = i * (p - 1) + j; 44cae8b89aSjeremylt } 45cae8b89aSjeremylt } 464fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u); 474fee36f0SJeremy L Thompson 484fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q, q}; 494fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data); 50cae8b89aSjeremylt 51cae8b89aSjeremylt // Bases 524fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x); 534fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u); 54cae8b89aSjeremylt 55cae8b89aSjeremylt // QFunctions 56cae8b89aSjeremylt CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 57a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 58cae8b89aSjeremylt CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD); 59cae8b89aSjeremylt CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 60cae8b89aSjeremylt 61cae8b89aSjeremylt CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 62cae8b89aSjeremylt CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 63cae8b89aSjeremylt CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 64cae8b89aSjeremylt CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 65cae8b89aSjeremylt 66cae8b89aSjeremylt // Operators 672b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 684fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 694fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 704fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 71cae8b89aSjeremylt 722b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 734fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data); 744fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 754fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 76cae8b89aSjeremylt 774fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 78cae8b89aSjeremylt 79cae8b89aSjeremylt // Apply with V = 0 804fee36f0SJeremy L Thompson CeedVectorSetValue(u, 1.0); 814fee36f0SJeremy L Thompson CeedVectorSetValue(v, 0.0); 824fee36f0SJeremy L Thompson CeedOperatorApplyAdd(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 83cae8b89aSjeremylt 84cae8b89aSjeremylt // Check output 854fee36f0SJeremy L Thompson { 864fee36f0SJeremy L Thompson const CeedScalar *v_array; 874fee36f0SJeremy L Thompson CeedScalar sum = 0.; 884fee36f0SJeremy L Thompson 894fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 904fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i]; 914fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 922b730f8bSJeremy L Thompson if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum); 934fee36f0SJeremy L Thompson } 94cae8b89aSjeremylt 95cae8b89aSjeremylt // Apply with V = 1 964fee36f0SJeremy L Thompson CeedVectorSetValue(v, 1.0); 974fee36f0SJeremy L Thompson CeedOperatorApplyAdd(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 98cae8b89aSjeremylt 99cae8b89aSjeremylt // Check output 1004fee36f0SJeremy L Thompson { 1014fee36f0SJeremy L Thompson const CeedScalar *v_array; 1024fee36f0SJeremy L Thompson CeedScalar sum = -num_nodes_u; 103cae8b89aSjeremylt 1044fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 1054fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i]; 1064fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 1074fee36f0SJeremy L Thompson if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum); 1084fee36f0SJeremy L Thompson } 1094fee36f0SJeremy L Thompson 1104fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 1114fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 1124fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 1134fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 1144fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 1154fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 1164fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 1174fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 1184fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 119cae8b89aSjeremylt CeedQFunctionDestroy(&qf_setup); 120cae8b89aSjeremylt CeedQFunctionDestroy(&qf_mass); 121cae8b89aSjeremylt CeedOperatorDestroy(&op_setup); 122cae8b89aSjeremylt CeedOperatorDestroy(&op_mass); 123cae8b89aSjeremylt CeedDestroy(&ceed); 124cae8b89aSjeremylt return 0; 125cae8b89aSjeremylt } 126