xref: /libCEED/tests/t510-operator.c (revision 4dccadb61a9bb3ddd06b933b05f6f28773cf32d8)
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 #include "t310-basis.h"
8 
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 
14 static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in,
15                  CeedScalar *const *out) {
16   const CeedScalar *weight = in[0], *J = in[1];
17   CeedScalar *rho = out[0];
18   for (CeedInt i=0; i<Q; i++) {
19     rho[i] = weight[i] * (J[i+Q*0]*J[i+Q*3] - J[i+Q*1]*J[i+Q*2]);
20   }
21   return 0;
22 }
23 
24 static int mass(void *ctx, CeedInt Q, const CeedScalar *const *in,
25                 CeedScalar *const *out) {
26   const CeedScalar *rho = in[0], *u = in[1];
27   CeedScalar *v = out[0];
28   for (CeedInt i=0; i<Q; i++) {
29     v[i] = rho[i] * u[i];
30   }
31   return 0;
32 }
33 
34 int main(int argc, char **argv) {
35   Ceed ceed;
36   CeedElemRestriction Erestrictx, Erestrictu, Erestrictxi, Erestrictui;
37   CeedBasis bx, bu;
38   CeedQFunction qf_setup, qf_mass;
39   CeedOperator op_setup, op_mass;
40   CeedVector qdata, X, U, V;
41   const CeedScalar *hv;
42   CeedInt nelem = 12, dim = 2, P = 6, Q = 4;
43   CeedInt nx = 3, ny = 2;
44   CeedInt row, col, offset;
45   CeedInt Ndofs = (nx*2+1)*(ny*2+1), Nqpts = nelem*Q;
46   CeedInt indx[nelem*P];
47   CeedScalar x[dim*Ndofs];
48   CeedScalar qref[dim*Q], qweight[Q];
49   CeedScalar interp[P*Q], grad[dim*P*Q];
50 
51   CeedInit(argv[1], &ceed);
52 
53   for (CeedInt i=0; i<Ndofs; i++) {
54     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
55     x[i+Ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
56   }
57   for (CeedInt i=0; i<nelem/2; i++) {
58     col = i % nx;
59     row = i / nx;
60     offset = col*2 + row*(nx*2+1)*2;
61 
62     indx[i*2*P +  0] =  2 + offset;
63     indx[i*2*P +  1] =  9 + offset;
64     indx[i*2*P +  2] = 16 + offset;
65     indx[i*2*P +  3] =  1 + offset;
66     indx[i*2*P +  4] =  8 + offset;
67     indx[i*2*P +  5] =  0 + offset;
68 
69     indx[i*2*P +  6] = 14 + offset;
70     indx[i*2*P +  7] =  7 + offset;
71     indx[i*2*P +  8] =  0 + offset;
72     indx[i*2*P +  9] = 15 + offset;
73     indx[i*2*P + 10] =  8 + offset;
74     indx[i*2*P + 11] = 16 + offset;
75   }
76 
77   // Restrictions
78   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, dim, CEED_MEM_HOST,
79                             CEED_USE_POINTER, indx, &Erestrictx);
80   CeedElemRestrictionCreateIdentity(ceed, nelem, P, nelem*P, dim, &Erestrictxi);
81 
82   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, 1, CEED_MEM_HOST,
83                             CEED_USE_POINTER, indx, &Erestrictu);
84   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Nqpts, 1, &Erestrictui);
85 
86 
87   // Bases
88   buildmats(qref, qweight, interp, grad);
89   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
90                     qweight, &bx);
91 
92   buildmats(qref, qweight, interp, grad);
93   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
94                     qweight, &bu);
95 
96   // QFunctions
97   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
98   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
99   CeedQFunctionAddInput(qf_setup, "x", dim, CEED_EVAL_GRAD);
100   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
101 
102   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
103   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
104   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
105   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
106 
107   // Operators
108   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
109 
110   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
111 
112   CeedVectorCreate(ceed, dim*Ndofs, &X);
113   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
114   CeedVectorCreate(ceed, Nqpts, &qdata);
115 
116   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
117                        bx, CEED_VECTOR_NONE);
118   CeedOperatorSetField(op_setup, "x", Erestrictx, CEED_NOTRANSPOSE,
119                        bx, CEED_VECTOR_ACTIVE);
120   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
121                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
122 
123   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
124                        CEED_BASIS_COLLOCATED, qdata);
125   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE,
126                        bu, CEED_VECTOR_ACTIVE);
127   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE,
128                        bu, CEED_VECTOR_ACTIVE);
129 
130   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
131 
132   CeedVectorCreate(ceed, Ndofs, &U);
133   CeedVectorSetValue(U, 0.0);
134   CeedVectorCreate(ceed, Ndofs, &V);
135 
136   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
137 
138   // Check output
139   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
140   for (CeedInt i=0; i<Ndofs; i++)
141     if (fabs(hv[i]) > 1e-14) printf("[%d] v %g != 0.0\n",i, hv[i]);
142   CeedVectorRestoreArrayRead(V, &hv);
143 
144   CeedQFunctionDestroy(&qf_setup);
145   CeedQFunctionDestroy(&qf_mass);
146   CeedOperatorDestroy(&op_setup);
147   CeedOperatorDestroy(&op_mass);
148   CeedElemRestrictionDestroy(&Erestrictu);
149   CeedElemRestrictionDestroy(&Erestrictx);
150   CeedElemRestrictionDestroy(&Erestrictui);
151   CeedElemRestrictionDestroy(&Erestrictxi);
152   CeedBasisDestroy(&bu);
153   CeedBasisDestroy(&bx);
154   CeedVectorDestroy(&X);
155   CeedVectorDestroy(&U);
156   CeedVectorDestroy(&V);
157   CeedVectorDestroy(&qdata);
158   CeedDestroy(&ceed);
159   return 0;
160 }
161