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