xref: /libCEED/tests/t400-qfunction.c (revision 4411cf47fb395f7f699e30c3c49d97ec6ad92316)
1*4411cf47Sjeremylt /// @file
2*4411cf47Sjeremylt /// Test creation, evaluation, and destruction for qfunction
3*4411cf47Sjeremylt /// \test Test creation, evaluation, and destruction for qfunction
457c64913Sjeremylt #include <ceed.h>
557c64913Sjeremylt 
657c64913Sjeremylt static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in,
757c64913Sjeremylt                  CeedScalar *const *out) {
857c64913Sjeremylt   const CeedScalar *w = in[0];
957c64913Sjeremylt   CeedScalar *qdata = out[0];
1057c64913Sjeremylt   for (CeedInt i=0; i<Q; i++) {
1157c64913Sjeremylt     qdata[i] = w[i];
1257c64913Sjeremylt   }
1357c64913Sjeremylt   return 0;
1457c64913Sjeremylt }
1557c64913Sjeremylt 
1657c64913Sjeremylt static int mass(void *ctx, CeedInt Q, const CeedScalar *const *in,
1757c64913Sjeremylt                 CeedScalar *const *out) {
1857c64913Sjeremylt   const CeedScalar *qdata = in[0], *u = in[1];
1957c64913Sjeremylt   CeedScalar *v = out[0];
2057c64913Sjeremylt   for (CeedInt i=0; i<Q; i++) {
2157c64913Sjeremylt     v[i] = qdata[i] * u[i];
2257c64913Sjeremylt   }
2357c64913Sjeremylt   return 0;
2457c64913Sjeremylt }
2557c64913Sjeremylt 
2657c64913Sjeremylt int main(int argc, char **argv) {
2757c64913Sjeremylt   Ceed ceed;
2857c64913Sjeremylt   CeedQFunction qf_setup, qf_mass;
2957c64913Sjeremylt   CeedInt Q = 8;
3057c64913Sjeremylt   CeedScalar qdata[Q], w[Q], u[Q], v[Q], vv[Q];
3157c64913Sjeremylt 
3257c64913Sjeremylt 
3357c64913Sjeremylt   CeedInit(argv[1], &ceed);
3457c64913Sjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
3557c64913Sjeremylt   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_INTERP);
3657c64913Sjeremylt   CeedQFunctionAddOutput(qf_setup, "qdata", 1, CEED_EVAL_INTERP);
3757c64913Sjeremylt 
3857c64913Sjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
3957c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "qdata", 1, CEED_EVAL_INTERP);
4057c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
4157c64913Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
4257c64913Sjeremylt 
4357c64913Sjeremylt   for (CeedInt i=0; i<Q; i++) {
4457c64913Sjeremylt     CeedScalar x = 2.*i/(Q-1) - 1;
4557c64913Sjeremylt     w[i] = 1 - x*x;
4657c64913Sjeremylt     u[i] = 2 + 3*x + 5*x*x;
4757c64913Sjeremylt     v[i] = w[i] * u[i];
4857c64913Sjeremylt   }
4957c64913Sjeremylt   {
5057c64913Sjeremylt     const CeedScalar *const in[1] = {w};
5157c64913Sjeremylt     CeedScalar *const out[1] = {qdata};
5257c64913Sjeremylt     CeedQFunctionApply(qf_setup, Q, in, out);
5357c64913Sjeremylt   }
5457c64913Sjeremylt   {
5557c64913Sjeremylt     const CeedScalar *const in[2] = {qdata, u};
5657c64913Sjeremylt     CeedScalar *const out[1] = {vv};
5757c64913Sjeremylt     CeedQFunctionApply(qf_mass, Q, in, out);
5857c64913Sjeremylt   }
5957c64913Sjeremylt   for (CeedInt i=0; i<Q; i++) {
6057c64913Sjeremylt     if (v[i] != vv[i]) printf("[%d] v %f != vv %f\n",i, v[i], vv[i]);
6157c64913Sjeremylt   }
6257c64913Sjeremylt   CeedQFunctionDestroy(&qf_setup);
6357c64913Sjeremylt   CeedQFunctionDestroy(&qf_mass);
6457c64913Sjeremylt   CeedDestroy(&ceed);
6557c64913Sjeremylt   return 0;
6657c64913Sjeremylt }
67