xref: /libCEED/interface/ceed-fortran.c (revision a8de75f0ba69dfecd93c7d0093c041c0285ffd21)
1 // Fortran interface
2 #include <ceed.h>
3 #include <ceed-impl.h>
4 #include <ceed-fortran-name.h>
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #define FORTRAN_REQUEST_IMMEDIATE -1
10 #define FORTRAN_REQUEST_ORDERED -2
11 #define FORTRAN_NULL -3
12 #define FORTRAN_BASIS_COLLOCATED -1
13 #define FORTRAN_VECTOR_ACTIVE -1
14 #define FORTRAN_VECTOR_NONE -2
15 
16 static Ceed *Ceed_dict = NULL;
17 static int Ceed_count = 0;
18 static int Ceed_n = 0;
19 static int Ceed_count_max = 0;
20 
21 // This test should actually be for the gfortran version, but we don't currently
22 // have a configure system to determine that (TODO).  At present, this will use
23 // the smaller integer when run with clang+gfortran=8, for example.  (That is
24 // sketchy, but will likely work for users that don't have huge character
25 // strings.)
26 #if __GNUC__ >= 8
27 typedef size_t fortran_charlen_t;
28 #else
29 typedef int fortran_charlen_t;
30 #endif
31 
32 #define Splice(a, b) a ## b
33 
34 // Fortran strings are generally unterminated and the length is passed as an
35 // extra argument after all the normal arguments.  Some compilers (I only know
36 // of Windows) place the length argument immediately after the string parameter
37 // (TODO).
38 //
39 // We can't just NULL-terminate the string in-place because that could overwrite
40 // other strings or attempt to write to read-only memory.  This macro allocates
41 // a string to hold the null-terminated version of the string that C expects.
42 #define FIX_STRING(stringname)                                          \
43   char Splice(stringname, _c)[1024];                                    \
44   if (Splice(stringname, _len) > 1023)                                  \
45     CeedError(NULL, 1, "Fortran string length too long %zd", (size_t)Splice(stringname, _len)); \
46   strncpy(Splice(stringname, _c), stringname, Splice(stringname, _len)); \
47   Splice(stringname, _c)[Splice(stringname, _len)] = 0;                 \
48 
49 #define fCeedInit FORTRAN_NAME(ceedinit,CEEDINIT)
50 void fCeedInit(const char* resource, int *ceed, int *err,
51                fortran_charlen_t resource_len) {
52   FIX_STRING(resource);
53   if (Ceed_count == Ceed_count_max) {
54     Ceed_count_max += Ceed_count_max/2 + 1;
55     CeedRealloc(Ceed_count_max, &Ceed_dict);
56   }
57 
58   Ceed *ceed_ = &Ceed_dict[Ceed_count];
59   *err = CeedInit(resource_c, ceed_);
60 
61   if (*err == 0) {
62     *ceed = Ceed_count++;
63     Ceed_n++;
64   }
65 }
66 
67 #define fCeedDestroy FORTRAN_NAME(ceeddestroy,CEEDDESTROY)
68 void fCeedDestroy(int *ceed, int *err) {
69   *err = CeedDestroy(&Ceed_dict[*ceed]);
70 
71   if (*err == 0) {
72     Ceed_n--;
73     if (Ceed_n == 0) {
74       CeedFree(&Ceed_dict);
75       Ceed_count = 0;
76       Ceed_count_max = 0;
77     }
78   }
79 }
80 
81 static CeedVector *CeedVector_dict = NULL;
82 static int CeedVector_count = 0;
83 static int CeedVector_n = 0;
84 static int CeedVector_count_max = 0;
85 
86 #define fCeedVectorCreate FORTRAN_NAME(ceedvectorcreate,CEEDVECTORCREATE)
87 void fCeedVectorCreate(int *ceed, int *length, int *vec, int *err) {
88   if (CeedVector_count == CeedVector_count_max) {
89     CeedVector_count_max += CeedVector_count_max/2 + 1;
90     CeedRealloc(CeedVector_count_max, &CeedVector_dict);
91   }
92 
93   CeedVector* vec_ = &CeedVector_dict[CeedVector_count];
94   *err = CeedVectorCreate(Ceed_dict[*ceed], *length, vec_);
95 
96   if (*err == 0) {
97     *vec = CeedVector_count++;
98     CeedVector_n++;
99   }
100 }
101 
102 #define fCeedVectorSetArray FORTRAN_NAME(ceedvectorsetarray,CEEDVECTORSETARRAY)
103 void fCeedVectorSetArray(int *vec, int *memtype, int *copymode,
104                          CeedScalar *array, int *err) {
105   *err = CeedVectorSetArray(CeedVector_dict[*vec], *memtype, *copymode, array);
106 }
107 
108 #define fCeedVectorSetValue FORTRAN_NAME(ceedvectorsetvalue,CEEDVECTORSETVALUE)
109 void fCeedVectorSetValue(int *vec, CeedScalar *value, int *err) {
110   *err = CeedVectorSetValue(CeedVector_dict[*vec], *value);
111 }
112 
113 #define fCeedVectorGetArray FORTRAN_NAME(ceedvectorgetarray,CEEDVECTORGETARRAY)
114 void fCeedVectorGetArray(int *vec, int *memtype, CeedScalar *array,
115                          int64_t *offset,
116                          int *err) {
117   CeedScalar *b;
118   CeedVector vec_ = CeedVector_dict[*vec];
119   *err = CeedVectorGetArray(vec_, *memtype, &b);
120   *offset = b - array;
121 }
122 
123 #define fCeedVectorGetArrayRead \
124     FORTRAN_NAME(ceedvectorgetarrayread,CEEDVECTORGETARRAYREAD)
125 void fCeedVectorGetArrayRead(int *vec, int *memtype, CeedScalar *array,
126                              int64_t *offset, int *err) {
127   const CeedScalar *b;
128   CeedVector vec_ = CeedVector_dict[*vec];
129   *err = CeedVectorGetArrayRead(vec_, *memtype, &b);
130   *offset = b - array;
131 }
132 
133 #define fCeedVectorRestoreArray \
134     FORTRAN_NAME(ceedvectorrestorearray,CEEDVECTORRESTOREARRAY)
135 void fCeedVectorRestoreArray(int *vec, CeedScalar *array,
136                              int64_t *offset, int *err) {
137   *err = CeedVectorRestoreArray(CeedVector_dict[*vec], &array);
138   *offset = 0;
139 }
140 
141 #define fCeedVectorRestoreArrayRead \
142     FORTRAN_NAME(ceedvectorrestorearrayread,CEEDVECTORRESTOREARRAYREAD)
143 void fCeedVectorRestoreArrayRead(int *vec, const CeedScalar *array,
144                                  int64_t *offset, int *err) {
145   *err = CeedVectorRestoreArrayRead(CeedVector_dict[*vec], &array);
146   *offset = 0;
147 }
148 
149 #define fCeedVectorView FORTRAN_NAME(ceedvectorview,CEEDVECTORVIEW)
150 void fCeedVectorView(int *vec, int *err) {
151   *err = CeedVectorView(CeedVector_dict[*vec], "%12.8f", stdout);
152 }
153 
154 #define fCeedVectorDestroy FORTRAN_NAME(ceedvectordestroy,CEEDVECTORDESTROY)
155 void fCeedVectorDestroy(int *vec, int *err) {
156   *err = CeedVectorDestroy(&CeedVector_dict[*vec]);
157 
158   if (*err == 0) {
159     CeedVector_n--;
160     if (CeedVector_n == 0) {
161       CeedFree(&CeedVector_dict);
162       CeedVector_count = 0;
163       CeedVector_count_max = 0;
164     }
165   }
166 }
167 
168 static CeedElemRestriction *CeedElemRestriction_dict = NULL;
169 static int CeedElemRestriction_count = 0;
170 static int CeedElemRestriction_n = 0;
171 static int CeedElemRestriction_count_max = 0;
172 
173 #define fCeedElemRestrictionCreate \
174     FORTRAN_NAME(ceedelemrestrictioncreate, CEEDELEMRESTRICTIONCREATE)
175 void fCeedElemRestrictionCreate(int *ceed, int *nelements,
176                                 int *esize, int *ndof, int *ncomp, int *memtype, int *copymode,
177                                 const int *indices, int *elemrestriction, int *err) {
178   if (CeedElemRestriction_count == CeedElemRestriction_count_max) {
179     CeedElemRestriction_count_max += CeedElemRestriction_count_max/2 + 1;
180     CeedRealloc(CeedElemRestriction_count_max, &CeedElemRestriction_dict);
181   }
182 
183   const int *indices_ = indices;
184 
185   CeedElemRestriction *elemrestriction_ =
186     &CeedElemRestriction_dict[CeedElemRestriction_count];
187   *err = CeedElemRestrictionCreate(Ceed_dict[*ceed], *nelements, *esize, *ndof,
188                                    *ncomp,
189                                    *memtype, *copymode, indices_, elemrestriction_);
190 
191   if (*err == 0) {
192     *elemrestriction = CeedElemRestriction_count++;
193     CeedElemRestriction_n++;
194   }
195 }
196 
197 #define fCeedElemRestrictionCreateIdentity \
198     FORTRAN_NAME(ceedelemrestrictioncreateidentity, CEEDELEMRESTRICTIONCREATEIDENTITY)
199 void fCeedElemRestrictionCreateIdentity(int *ceed, int *nelements,
200                                         int *esize, int *ndof, int *ncomp,
201                                         int *elemrestriction, int *err) {
202   if (CeedElemRestriction_count == CeedElemRestriction_count_max) {
203     CeedElemRestriction_count_max += CeedElemRestriction_count_max/2 + 1;
204     CeedRealloc(CeedElemRestriction_count_max, &CeedElemRestriction_dict);
205   }
206 
207   CeedElemRestriction *elemrestriction_ =
208     &CeedElemRestriction_dict[CeedElemRestriction_count];
209   *err = CeedElemRestrictionCreateIdentity(Ceed_dict[*ceed], *nelements, *esize,
210          *ndof,
211          *ncomp, elemrestriction_);
212 
213   if (*err == 0) {
214     *elemrestriction = CeedElemRestriction_count++;
215     CeedElemRestriction_n++;
216   }
217 }
218 
219 #define fCeedElemRestrictionCreateBlocked \
220     FORTRAN_NAME(ceedelemrestrictioncreateblocked,CEEDELEMRESTRICTIONCREATEBLOCKED)
221 void fCeedElemRestrictionCreateBlocked(int *ceed, int *nelements,
222                                        int *esize, int *blocksize, int *ndof, int *ncomp,
223                                        int *mtype, int *cmode,
224                                        int *blkindices, int *elemrestriction, int *err) {
225 
226   if (CeedElemRestriction_count == CeedElemRestriction_count_max) {
227     CeedElemRestriction_count_max += CeedElemRestriction_count_max/2 + 1;
228     CeedRealloc(CeedElemRestriction_count_max, &CeedElemRestriction_dict);
229   }
230 
231   CeedElemRestriction *elemrestriction_ =
232     &CeedElemRestriction_dict[CeedElemRestriction_count];
233   *err = CeedElemRestrictionCreateBlocked(Ceed_dict[*ceed], *nelements, *esize,
234                                           *blocksize, *ndof, *ncomp, *mtype, *cmode, blkindices,
235                                           elemrestriction_);
236 
237   if (*err == 0) {
238     *elemrestriction = CeedElemRestriction_count++;
239     CeedElemRestriction_n++;
240   }
241 }
242 
243 static CeedRequest *CeedRequest_dict = NULL;
244 static int CeedRequest_count = 0;
245 static int CeedRequest_n = 0;
246 static int CeedRequest_count_max = 0;
247 
248 #define fCeedElemRestrictionApply \
249     FORTRAN_NAME(ceedelemrestrictionapply,CEEDELEMRESTRICTIONAPPLY)
250 void fCeedElemRestrictionApply(int *elemr, int *tmode, int *lmode,
251                                int *uvec, int *ruvec, int *rqst, int *err) {
252   int createRequest = 1;
253   // Check if input is CEED_REQUEST_ORDERED(-2) or CEED_REQUEST_IMMEDIATE(-1)
254   if (*rqst == FORTRAN_REQUEST_IMMEDIATE || *rqst == FORTRAN_REQUEST_ORDERED)
255     createRequest = 0;
256 
257   if (createRequest && CeedRequest_count == CeedRequest_count_max) {
258     CeedRequest_count_max += CeedRequest_count_max/2 + 1;
259     CeedRealloc(CeedRequest_count_max, &CeedRequest_dict);
260   }
261 
262   CeedRequest *rqst_;
263   if      (*rqst == FORTRAN_REQUEST_IMMEDIATE) rqst_ = CEED_REQUEST_IMMEDIATE;
264   else if (*rqst == FORTRAN_REQUEST_ORDERED  ) rqst_ = CEED_REQUEST_ORDERED;
265   else rqst_ = &CeedRequest_dict[CeedRequest_count];
266 
267   *err = CeedElemRestrictionApply(CeedElemRestriction_dict[*elemr], *tmode,
268                                   *lmode, CeedVector_dict[*uvec], CeedVector_dict[*ruvec], rqst_);
269 
270   if (*err == 0 && createRequest) {
271     *rqst = CeedRequest_count++;
272     CeedRequest_n++;
273   }
274 }
275 
276 #define fCeedRequestWait FORTRAN_NAME(ceedrequestwait, CEEDREQUESTWAIT)
277 void fCeedRequestWait(int *rqst, int *err) {
278   // TODO Uncomment this once CeedRequestWait is implemented
279   //*err = CeedRequestWait(&CeedRequest_dict[*rqst]);
280 
281   if (*err == 0) {
282     CeedRequest_n--;
283     if (CeedRequest_n == 0) {
284       CeedFree(&CeedRequest_dict);
285       CeedRequest_count = 0;
286       CeedRequest_count_max = 0;
287     }
288   }
289 }
290 
291 #define fCeedElemRestrictionDestroy \
292     FORTRAN_NAME(ceedelemrestrictiondestroy,CEEDELEMRESTRICTIONDESTROY)
293 void fCeedElemRestrictionDestroy(int *elem, int *err) {
294   *err = CeedElemRestrictionDestroy(&CeedElemRestriction_dict[*elem]);
295 
296   if (*err == 0) {
297     CeedElemRestriction_n--;
298     if (CeedElemRestriction_n == 0) {
299       CeedFree(&CeedElemRestriction_dict);
300       CeedElemRestriction_count = 0;
301       CeedElemRestriction_count_max = 0;
302     }
303   }
304 }
305 
306 static CeedBasis *CeedBasis_dict = NULL;
307 static int CeedBasis_count = 0;
308 static int CeedBasis_n = 0;
309 static int CeedBasis_count_max = 0;
310 
311 #define fCeedBasisCreateTensorH1Lagrange \
312     FORTRAN_NAME(ceedbasiscreatetensorh1lagrange, CEEDBASISCREATETENSORH1LAGRANGE)
313 void fCeedBasisCreateTensorH1Lagrange(int *ceed, int *dim,
314                                       int *ncomp, int *P, int *Q, int *quadmode, int *basis,
315                                       int *err) {
316   if (CeedBasis_count == CeedBasis_count_max) {
317     CeedBasis_count_max += CeedBasis_count_max/2 + 1;
318     CeedRealloc(CeedBasis_count_max, &CeedBasis_dict);
319   }
320 
321   *err = CeedBasisCreateTensorH1Lagrange(Ceed_dict[*ceed], *dim, *ncomp, *P, *Q,
322                                          *quadmode, &CeedBasis_dict[CeedBasis_count]);
323 
324   if (*err == 0) {
325     *basis = CeedBasis_count++;
326     CeedBasis_n++;
327   }
328 }
329 
330 #define fCeedBasisCreateTensorH1 \
331     FORTRAN_NAME(ceedbasiscreatetensorh1, CEEDBASISCREATETENSORH1)
332 void fCeedBasisCreateTensorH1(int *ceed, int *dim, int *ncomp, int *P1d,
333                               int *Q1d, const CeedScalar *interp1d, const CeedScalar *grad1d,
334                               const CeedScalar *qref1d, const CeedScalar *qweight1d, int *basis, int *err) {
335   if (CeedBasis_count == CeedBasis_count_max) {
336     CeedBasis_count_max += CeedBasis_count_max/2 + 1;
337     CeedRealloc(CeedBasis_count_max, &CeedBasis_dict);
338   }
339 
340   *err = CeedBasisCreateTensorH1(Ceed_dict[*ceed], *dim, *ncomp, *P1d, *Q1d,
341                                  interp1d, grad1d,
342                                  qref1d, qweight1d, &CeedBasis_dict[CeedBasis_count]);
343 
344   if (*err == 0) {
345     *basis = CeedBasis_count++;
346     CeedBasis_n++;
347   }
348 }
349 
350 #define fCeedBasisCreateH1 \
351     FORTRAN_NAME(ceedbasiscreateh1, CEEDBASISCREATEH1)
352 void fCeedBasisCreateH1(int *ceed, int *topo, int *ncomp, int *ndof,
353                         int *nqpts, const CeedScalar *interp, const CeedScalar *grad,
354                         const CeedScalar *qref, const CeedScalar *qweight, int *basis, int *err) {
355   if (CeedBasis_count == CeedBasis_count_max) {
356     CeedBasis_count_max += CeedBasis_count_max/2 + 1;
357     CeedRealloc(CeedBasis_count_max, &CeedBasis_dict);
358   }
359 
360   *err = CeedBasisCreateH1(Ceed_dict[*ceed], *topo, *ncomp,
361                            *ndof, *nqpts, interp, grad,
362                            qref, qweight, &CeedBasis_dict[CeedBasis_count]);
363 
364   if (*err == 0) {
365     *basis = CeedBasis_count++;
366     CeedBasis_n++;
367   }
368 }
369 
370 #define fCeedBasisView FORTRAN_NAME(ceedbasisview, CEEDBASISVIEW)
371 void fCeedBasisView(int *basis, int *err) {
372   *err = CeedBasisView(CeedBasis_dict[*basis], stdout);
373 }
374 
375 #define fCeedQRFactorization \
376     FORTRAN_NAME(ceedqrfactorization, CEEDQRFACTORIZATION)
377 void fCeedQRFactorization(CeedScalar *mat, CeedScalar *tau, int *m, int *n,
378                           int *err) {
379   *err = CeedQRFactorization(mat, tau, *m, *n);
380 }
381 
382 #define fCeedBasisGetCollocatedGrad \
383     FORTRAN_NAME(ceedbasisgetcollocatedgrad, CEEDBASISGETCOLLOCATEDGRAD)
384 void fCeedBasisGetCollocatedGrad(int *basis, CeedScalar *colograd1d,
385                                 int *err) {
386   *err = CeedBasisGetCollocatedGrad(CeedBasis_dict[*basis], colograd1d);
387 }
388 
389 #define fCeedBasisApply FORTRAN_NAME(ceedbasisapply, CEEDBASISAPPLY)
390 void fCeedBasisApply(int *basis, int *nelem, int *tmode, int *emode,
391                      const CeedScalar *u, CeedScalar *v, int *err) {
392   *err = CeedBasisApply(CeedBasis_dict[*basis], *nelem, *tmode, *emode, u, v);
393 }
394 
395 #define fCeedBasisGetNumNodes \
396     FORTRAN_NAME(ceedbasisgetnumnodes, CEEDBASISGETNUMNODES)
397 void fCeedBasisGetNumNodes(int *basis, int *P, int *err) {
398   *err = CeedBasisGetNumNodes(CeedBasis_dict[*basis], P);
399 }
400 
401 #define fCeedBasisGetNumQuadraturePoints \
402     FORTRAN_NAME(ceedbasisgetnumquadraturepoints, CEEDBASISGETNUMQUADRATUREPOINTS)
403 void fCeedBasisGetNumQuadraturePoints(int *basis, int *Q, int *err) {
404   *err = CeedBasisGetNumQuadraturePoints(CeedBasis_dict[*basis], Q);
405 }
406 
407 #define fCeedBasisDestroy FORTRAN_NAME(ceedbasisdestroy,CEEDBASISDESTROY)
408 void fCeedBasisDestroy(int *basis, int *err) {
409   *err = CeedBasisDestroy(&CeedBasis_dict[*basis]);
410 
411   if (*err == 0) {
412     CeedBasis_n--;
413     if (CeedBasis_n == 0) {
414       CeedFree(&CeedBasis_dict);
415       CeedBasis_count = 0;
416       CeedBasis_count_max = 0;
417     }
418   }
419 }
420 
421 #define fCeedGaussQuadrature FORTRAN_NAME(ceedgaussquadrature, CEEDGAUSSQUADRATURE)
422 void fCeedGaussQuadrature(int *Q, CeedScalar *qref1d, CeedScalar *qweight1d,
423                           int *err) {
424   *err = CeedGaussQuadrature(*Q, qref1d, qweight1d);
425 }
426 
427 #define fCeedLobattoQuadrature \
428     FORTRAN_NAME(ceedlobattoquadrature, CEEDLOBATTOQUADRATURE)
429 void fCeedLobattoQuadrature(int *Q, CeedScalar *qref1d, CeedScalar *qweight1d,
430                             int *err) {
431   *err = CeedLobattoQuadrature(*Q, qref1d, qweight1d);
432 }
433 
434 static CeedQFunction *CeedQFunction_dict = NULL;
435 static int CeedQFunction_count = 0;
436 static int CeedQFunction_n = 0;
437 static int CeedQFunction_count_max = 0;
438 
439 struct fContext {
440   void (*f)(void *ctx, int *nq,
441             const CeedScalar *u,const CeedScalar *u1,const CeedScalar *u2,
442             const CeedScalar *u3,
443             const CeedScalar *u4,const CeedScalar *u5,const CeedScalar *u6,
444             const CeedScalar *u7,
445             const CeedScalar *u8,const CeedScalar *u9,const CeedScalar *u10,
446             const CeedScalar *u11,
447             const CeedScalar *u12,const CeedScalar *u13,const CeedScalar *u14,
448             const CeedScalar *u15,
449             CeedScalar *v,CeedScalar *v1, CeedScalar *v2,CeedScalar *v3,
450             CeedScalar *v4,CeedScalar *v5, CeedScalar *v6,CeedScalar *v7,
451             CeedScalar *v8,CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
452             CeedScalar *v12,CeedScalar *v13, CeedScalar *v14,CeedScalar *v15, int *err);
453   void *innerctx;
454 };
455 
456 static int CeedQFunctionFortranStub(void *ctx, int nq,
457                                     const CeedScalar *const *u, CeedScalar *const *v) {
458   struct fContext *fctx = ctx;
459   int ierr;
460 
461   CeedScalar ctx_=1.0;
462   fctx->f((void*)&ctx_,&nq,u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7],
463           u[8],u[9],u[10],u[11],u[12],u[13],u[14],u[15],
464           v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],
465           v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],&ierr);
466   return ierr;
467 }
468 
469 #define fCeedQFunctionCreateInterior \
470     FORTRAN_NAME(ceedqfunctioncreateinterior, CEEDQFUNCTIONCREATEINTERIOR)
471 void fCeedQFunctionCreateInterior(int* ceed, int* vlength,
472                                   void (*f)(void *ctx, int *nq,
473                                       const CeedScalar *u,const CeedScalar *u1,const CeedScalar *u2,
474                                       const CeedScalar *u3,
475                                       const CeedScalar *u4,const CeedScalar *u5,const CeedScalar *u6,
476                                       const CeedScalar *u7,
477                                       const CeedScalar *u8,const CeedScalar *u9,const CeedScalar *u10,
478                                       const CeedScalar *u11,
479                                       const CeedScalar *u12,const CeedScalar *u13,const CeedScalar *u14,
480                                       const CeedScalar *u15,
481                                       CeedScalar *v,CeedScalar *v1, CeedScalar *v2,CeedScalar *v3,
482                                       CeedScalar *v4,CeedScalar *v5, CeedScalar *v6,CeedScalar *v7,
483                                       CeedScalar *v8,CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
484                                       CeedScalar *v12,CeedScalar *v13, CeedScalar *v14,CeedScalar *v15,
485                                       int *err),
486                                   const char *focca, int *qf, int *err,
487                                   fortran_charlen_t focca_len) {
488   FIX_STRING(focca);
489   if (CeedQFunction_count == CeedQFunction_count_max) {
490     CeedQFunction_count_max += CeedQFunction_count_max/2 + 1;
491     CeedRealloc(CeedQFunction_count_max, &CeedQFunction_dict);
492   }
493 
494   CeedQFunction *qf_ = &CeedQFunction_dict[CeedQFunction_count];
495   *err = CeedQFunctionCreateInterior(Ceed_dict[*ceed], *vlength,
496                                      CeedQFunctionFortranStub,focca_c, qf_);
497 
498   if (*err == 0) {
499     *qf = CeedQFunction_count++;
500     CeedQFunction_n++;
501   }
502 
503   struct fContext *fctx;
504   *err = CeedMalloc(1, &fctx);
505   if (*err) return;
506   fctx->f = f; fctx->innerctx = NULL;
507 
508   *err = CeedQFunctionSetContext(*qf_, fctx, sizeof(struct fContext));
509 
510 }
511 
512 #define fCeedQFunctionAddInput \
513     FORTRAN_NAME(ceedqfunctionaddinput,CEEDQFUNCTIONADDINPUT)
514 void fCeedQFunctionAddInput(int *qf, const char *fieldname,
515                             CeedInt *ncomp, CeedEvalMode *emode, int *err,
516                             fortran_charlen_t fieldname_len) {
517   FIX_STRING(fieldname);
518   CeedQFunction qf_ = CeedQFunction_dict[*qf];
519 
520   *err = CeedQFunctionAddInput(qf_, fieldname_c, *ncomp, *emode);
521 }
522 
523 #define fCeedQFunctionAddOutput \
524     FORTRAN_NAME(ceedqfunctionaddoutput,CEEDQFUNCTIONADDOUTPUT)
525 void fCeedQFunctionAddOutput(int *qf, const char *fieldname,
526                              CeedInt *ncomp, CeedEvalMode *emode, int *err,
527                              fortran_charlen_t fieldname_len) {
528   FIX_STRING(fieldname);
529   CeedQFunction qf_ = CeedQFunction_dict[*qf];
530 
531   *err = CeedQFunctionAddOutput(qf_, fieldname_c, *ncomp, *emode);
532 }
533 
534 #define fCeedQFunctionApply \
535     FORTRAN_NAME(ceedqfunctionapply,CEEDQFUNCTIONAPPLY)
536 //TODO Need Fixing, double pointer
537 void fCeedQFunctionApply(int *qf, int *Q,
538                          const CeedScalar *u,const CeedScalar *u1,const CeedScalar *u2,
539                          const CeedScalar *u3,
540                          const CeedScalar *u4,const CeedScalar *u5,const CeedScalar *u6,
541                          const CeedScalar *u7,
542                          const CeedScalar *u8,const CeedScalar *u9,const CeedScalar *u10,
543                          const CeedScalar *u11,
544                          const CeedScalar *u12,const CeedScalar *u13,const CeedScalar *u14,
545                          const CeedScalar *u15,
546                          CeedScalar *v,CeedScalar *v1, CeedScalar *v2,CeedScalar *v3,
547                          CeedScalar *v4,CeedScalar *v5, CeedScalar *v6,CeedScalar *v7,
548                          CeedScalar *v8,CeedScalar *v9, CeedScalar *v10,CeedScalar *v11,
549                          CeedScalar *v12,CeedScalar *v13, CeedScalar *v14,CeedScalar *v15, int *err) {
550   CeedQFunction qf_ = CeedQFunction_dict[*qf];
551   const CeedScalar **in;
552   *err = CeedCalloc(16, &in);
553   if (*err) return;
554   in[0] = u;
555   in[1] = u1;
556   in[2] = u2;
557   in[3] = u3;
558   in[4] = u4;
559   in[5] = u5;
560   in[6] = u6;
561   in[7] = u7;
562   in[8] = u8;
563   in[9] = u9;
564   in[10] = u10;
565   in[11] = u11;
566   in[12] = u12;
567   in[13] = u13;
568   in[14] = u14;
569   in[15] = u15;
570   CeedScalar **out;
571   *err = CeedCalloc(16, &out);
572   if (*err) return;
573   out[0] = v;
574   out[1] = v1;
575   out[2] = v2;
576   out[3] = v3;
577   out[4] = v4;
578   out[5] = v5;
579   out[6] = v6;
580   out[7] = v7;
581   out[8] = v8;
582   out[9] = v9;
583   out[10] = v10;
584   out[11] = v11;
585   out[12] = v12;
586   out[13] = v13;
587   out[14] = v14;
588   out[15] = v15;
589   *err = CeedQFunctionApply(qf_, *Q, (const CeedScalar * const*)in, out);
590   if (*err) return;
591 
592   *err = CeedFree(&in);
593   if (*err) return;
594   *err = CeedFree(&out);
595 }
596 
597 #define fCeedQFunctionDestroy \
598     FORTRAN_NAME(ceedqfunctiondestroy,CEEDQFUNCTIONDESTROY)
599 void fCeedQFunctionDestroy(int *qf, int *err) {
600   CeedFree(&CeedQFunction_dict[*qf]->ctx);
601   *err = CeedQFunctionDestroy(&CeedQFunction_dict[*qf]);
602 
603   if (*err) return;
604   CeedQFunction_n--;
605   if (CeedQFunction_n == 0) {
606     *err = CeedFree(&CeedQFunction_dict);
607     CeedQFunction_count = 0;
608     CeedQFunction_count_max = 0;
609   }
610 }
611 
612 static CeedOperator *CeedOperator_dict = NULL;
613 static int CeedOperator_count = 0;
614 static int CeedOperator_n = 0;
615 static int CeedOperator_count_max = 0;
616 
617 #define fCeedOperatorCreate \
618     FORTRAN_NAME(ceedoperatorcreate, CEEDOPERATORCREATE)
619 void fCeedOperatorCreate(int* ceed,
620                          int* qf, int* dqf, int* dqfT, int *op, int *err) {
621   if (CeedOperator_count == CeedOperator_count_max)
622     CeedOperator_count_max += CeedOperator_count_max/2 + 1,
623                               CeedOperator_dict =
624                                 realloc(CeedOperator_dict, sizeof(CeedOperator)*CeedOperator_count_max);
625 
626   CeedOperator *op_ = &CeedOperator_dict[CeedOperator_count];
627 
628   CeedQFunction dqf_  = NULL, dqfT_ = NULL;
629   if (*dqf  != FORTRAN_NULL) dqf_  = CeedQFunction_dict[*dqf ];
630   if (*dqfT != FORTRAN_NULL) dqfT_ = CeedQFunction_dict[*dqfT];
631 
632   *err = CeedOperatorCreate(Ceed_dict[*ceed], CeedQFunction_dict[*qf], dqf_,
633                             dqfT_, op_);
634   if (*err) return;
635   *op = CeedOperator_count++;
636   CeedOperator_n++;
637 }
638 
639 #define fCeedOperatorSetField \
640     FORTRAN_NAME(ceedoperatorsetfield,CEEDOPERATORSETFIELD)
641 void fCeedOperatorSetField(int *op, const char *fieldname,
642                            int *r, int *b, int *v, int *err,
643                            fortran_charlen_t fieldname_len) {
644   FIX_STRING(fieldname);
645   CeedElemRestriction r_;
646   CeedBasis b_;
647   CeedVector v_;
648 
649   CeedOperator op_ = CeedOperator_dict[*op];
650 
651   if (*r == FORTRAN_NULL) {
652     r_ = NULL;
653   } else {
654     r_ = CeedElemRestriction_dict[*r];
655   }
656   if (*b == FORTRAN_NULL) {
657     b_ = NULL;
658   } else if (*b == FORTRAN_BASIS_COLLOCATED) {
659     b_ = CEED_BASIS_COLLOCATED;
660   } else {
661     b_ = CeedBasis_dict[*b];
662   }
663   if (*v == FORTRAN_NULL) {
664     v_ = NULL;
665   } else if (*v == FORTRAN_VECTOR_ACTIVE) {
666     v_ = CEED_VECTOR_ACTIVE;
667   } else if (*v == FORTRAN_VECTOR_NONE) {
668     v_ = CEED_VECTOR_NONE;
669   } else {
670     v_ = CeedVector_dict[*v];
671   }
672 
673   *err = CeedOperatorSetField(op_, fieldname_c, r_, b_, v_);
674 }
675 
676 #define fCeedOperatorApply FORTRAN_NAME(ceedoperatorapply, CEEDOPERATORAPPLY)
677 void fCeedOperatorApply(int *op, int *ustatevec,
678                         int *resvec, int *rqst, int *err) {
679   CeedVector ustatevec_ = *ustatevec == FORTRAN_NULL
680                           ? NULL : CeedVector_dict[*ustatevec];
681   CeedVector resvec_ = *resvec == FORTRAN_NULL
682                        ? NULL : CeedVector_dict[*resvec];
683 
684   int createRequest = 1;
685   // Check if input is CEED_REQUEST_ORDERED(-2) or CEED_REQUEST_IMMEDIATE(-1)
686   if (*rqst == -1 || *rqst == -2) {
687     createRequest = 0;
688   }
689 
690   if (createRequest && CeedRequest_count == CeedRequest_count_max) {
691     CeedRequest_count_max += CeedRequest_count_max/2 + 1;
692     CeedRealloc(CeedRequest_count_max, &CeedRequest_dict);
693   }
694 
695   CeedRequest *rqst_;
696   if (*rqst == -1) rqst_ = CEED_REQUEST_IMMEDIATE;
697   else if (*rqst == -2) rqst_ = CEED_REQUEST_ORDERED;
698   else rqst_ = &CeedRequest_dict[CeedRequest_count];
699 
700   *err = CeedOperatorApply(CeedOperator_dict[*op],
701                            ustatevec_, resvec_, rqst_);
702   if (*err) return;
703   if (createRequest) {
704     *rqst = CeedRequest_count++;
705     CeedRequest_n++;
706   }
707 }
708 
709 #define fCeedOperatorApplyJacobian \
710     FORTRAN_NAME(ceedoperatorapplyjacobian, CEEDOPERATORAPPLYJACOBIAN)
711 void fCeedOperatorApplyJacobian(int *op, int *qdatavec, int *ustatevec,
712                                 int *dustatevec, int *dresvec, int *rqst, int *err) {
713 // TODO Uncomment this when CeedOperatorApplyJacobian is implemented
714 //  *err = CeedOperatorApplyJacobian(CeedOperator_dict[*op], CeedVector_dict[*qdatavec],
715 //             CeedVector_dict[*ustatevec], CeedVector_dict[*dustatevec],
716 //             CeedVector_dict[*dresvec], &CeedRequest_dict[*rqst]);
717 }
718 
719 #define fCeedOperatorDestroy \
720     FORTRAN_NAME(ceedoperatordestroy, CEEDOPERATORDESTROY)
721 void fCeedOperatorDestroy(int *op, int *err) {
722   *err = CeedOperatorDestroy(&CeedOperator_dict[*op]);
723   if (*err) return;
724   CeedOperator_n--;
725   if (CeedOperator_n == 0) {
726     *err = CeedFree(&CeedOperator_dict);
727     CeedOperator_count = 0;
728     CeedOperator_count_max = 0;
729   }
730 }
731