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_QDATA_NONE -1 14 #define FORTRAN_VECTOR_ACTIVE -1 15 #define FORTRAN_VECTOR_NONE -2 16 17 static Ceed *Ceed_dict = NULL; 18 static int Ceed_count = 0; 19 static int Ceed_n = 0; 20 static int Ceed_count_max = 0; 21 22 // This test should actually be for the gfortran version, but we don't currently 23 // have a configure system to determine that (TODO). At present, this will use 24 // the smaller integer when run with clang+gfortran=8, for example. (That is 25 // sketchy, but will likely work for users that don't have huge character 26 // strings.) 27 #if __GNUC__ >= 8 28 typedef size_t fortran_charlen_t; 29 #else 30 typedef int fortran_charlen_t; 31 #endif 32 33 #define Splice(a, b) a ## b 34 35 // Fortran strings are generally unterminated and the length is passed as an 36 // extra argument after all the normal arguments. Some compilers (I only know 37 // of Windows) place the length argument immediately after the string parameter 38 // (TODO). 39 // 40 // We can't just NULL-terminate the string in-place because that could overwrite 41 // other strings or attempt to write to read-only memory. This macro allocates 42 // a string to hold the null-terminated version of the string that C expects. 43 #define FIX_STRING(stringname) \ 44 char Splice(stringname, _c)[1024]; \ 45 if (Splice(stringname, _len) > 1023) \ 46 CeedError(NULL, 1, "Fortran string length too long %zd", (size_t)Splice(stringname, _len)); \ 47 strncpy(Splice(stringname, _c), stringname, Splice(stringname, _len)); \ 48 Splice(stringname, _c)[Splice(stringname, _len)] = 0; \ 49 50 #define fCeedInit FORTRAN_NAME(ceedinit,CEEDINIT) 51 void fCeedInit(const char* resource, int *ceed, int *err, 52 fortran_charlen_t resource_len) { 53 FIX_STRING(resource); 54 if (Ceed_count == Ceed_count_max) { 55 Ceed_count_max += Ceed_count_max/2 + 1; 56 CeedRealloc(Ceed_count_max, &Ceed_dict); 57 } 58 59 Ceed *ceed_ = &Ceed_dict[Ceed_count]; 60 *err = CeedInit(resource_c, ceed_); 61 62 if (*err == 0) { 63 *ceed = Ceed_count++; 64 Ceed_n++; 65 } 66 } 67 68 #define fCeedDestroy FORTRAN_NAME(ceeddestroy,CEEDDESTROY) 69 void fCeedDestroy(int *ceed, int *err) { 70 *err = CeedDestroy(&Ceed_dict[*ceed]); 71 72 if (*err == 0) { 73 Ceed_n--; 74 if (Ceed_n == 0) { 75 CeedFree(&Ceed_dict); 76 Ceed_count = 0; 77 Ceed_count_max = 0; 78 } 79 } 80 } 81 82 static CeedVector *CeedVector_dict = NULL; 83 static int CeedVector_count = 0; 84 static int CeedVector_n = 0; 85 static int CeedVector_count_max = 0; 86 87 #define fCeedVectorCreate FORTRAN_NAME(ceedvectorcreate,CEEDVECTORCREATE) 88 void fCeedVectorCreate(int *ceed, int *length, int *vec, int *err) { 89 if (CeedVector_count == CeedVector_count_max) { 90 CeedVector_count_max += CeedVector_count_max/2 + 1; 91 CeedRealloc(CeedVector_count_max, &CeedVector_dict); 92 } 93 94 CeedVector* vec_ = &CeedVector_dict[CeedVector_count]; 95 *err = CeedVectorCreate(Ceed_dict[*ceed], *length, vec_); 96 97 if (*err == 0) { 98 *vec = CeedVector_count++; 99 CeedVector_n++; 100 } 101 } 102 103 #define fCeedVectorSetArray FORTRAN_NAME(ceedvectorsetarray,CEEDVECTORSETARRAY) 104 void fCeedVectorSetArray(int *vec, int *memtype, int *copymode, 105 CeedScalar *array, int *err) { 106 *err = CeedVectorSetArray(CeedVector_dict[*vec], *memtype, *copymode, array); 107 } 108 109 #define fCeedVectorSetValue FORTRAN_NAME(ceedvectorsetvalue,CEEDVECTORSETVALUE) 110 void fCeedVectorSetValue(int *vec, CeedScalar *value, int *err) { 111 *err = CeedVectorSetValue(CeedVector_dict[*vec], *value); 112 } 113 114 #define fCeedVectorGetArray FORTRAN_NAME(ceedvectorgetarray,CEEDVECTORGETARRAY) 115 void fCeedVectorGetArray(int *vec, int *memtype, CeedScalar *array, 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 *ndof, 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, *ndof, *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 *ndof, 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, *ndof, *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 fCeedBasisView FORTRAN_NAME(ceedbasisview, CEEDBASISVIEW) 351 void fCeedBasisView(int *basis, int *err) { 352 *err = CeedBasisView(CeedBasis_dict[*basis], stdout); 353 } 354 355 #define fCeedQRFactorization \ 356 FORTRAN_NAME(ceedqrfactorization, CEEDQRFACTORIZATION) 357 void fCeedQRFactorization(CeedScalar *mat, CeedScalar *tau, int *m, int *n, 358 int *err) { 359 *err = CeedQRFactorization(mat, tau, *m, *n); 360 } 361 362 #define fCeedBasisGetCollocatedGrad \ 363 FORTRAN_NAME(ceedbasisgetcollocatedgrad, CEEDBASISGETCOLLOCATEDGRAD) 364 void fCeedBasisGetCollocatedGrad(int *basis, CeedScalar *colograd1d, 365 int *err) { 366 *err = CeedBasisGetCollocatedGrad(CeedBasis_dict[*basis], colograd1d); 367 } 368 369 #define fCeedBasisApply FORTRAN_NAME(ceedbasisapply, CEEDBASISAPPLY) 370 void fCeedBasisApply(int *basis, int *nelem, int *tmode, int *emode, 371 const CeedScalar *u, CeedScalar *v, int *err) { 372 *err = CeedBasisApply(CeedBasis_dict[*basis], *nelem, *tmode, *emode, u, v); 373 } 374 375 #define fCeedBasisGetNumNodes \ 376 FORTRAN_NAME(ceedbasisgetnumnodes, CEEDBASISGETNUMNODES) 377 void fCeedBasisGetNumNodes(int *basis, int *P, int *err) { 378 *err = CeedBasisGetNumNodes(CeedBasis_dict[*basis], P); 379 } 380 381 #define fCeedBasisGetNumQuadraturePoints \ 382 FORTRAN_NAME(ceedbasisgetnumquadraturepoints, CEEDBASISGETNUMQUADRATUREPOINTS) 383 void fCeedBasisGetNumQuadraturePoints(int *basis, int *Q, int *err) { 384 *err = CeedBasisGetNumQuadraturePoints(CeedBasis_dict[*basis], Q); 385 } 386 387 #define fCeedBasisDestroy FORTRAN_NAME(ceedbasisdestroy,CEEDBASISDESTROY) 388 void fCeedBasisDestroy(int *basis, int *err) { 389 *err = CeedBasisDestroy(&CeedBasis_dict[*basis]); 390 391 if (*err == 0) { 392 CeedBasis_n--; 393 if (CeedBasis_n == 0) { 394 CeedFree(&CeedBasis_dict); 395 CeedBasis_count = 0; 396 CeedBasis_count_max = 0; 397 } 398 } 399 } 400 401 #define fCeedGaussQuadrature FORTRAN_NAME(ceedgaussquadrature, CEEDGAUSSQUADRATURE) 402 void fCeedGaussQuadrature(int *Q, CeedScalar *qref1d, CeedScalar *qweight1d, 403 int *err) { 404 *err = CeedGaussQuadrature(*Q, qref1d, qweight1d); 405 } 406 407 #define fCeedLobattoQuadrature \ 408 FORTRAN_NAME(ceedlobattoquadrature, CEEDLOBATTOQUADRATURE) 409 void fCeedLobattoQuadrature(int *Q, CeedScalar *qref1d, CeedScalar *qweight1d, 410 int *err) { 411 *err = CeedLobattoQuadrature(*Q, qref1d, qweight1d); 412 } 413 414 static CeedQFunction *CeedQFunction_dict = NULL; 415 static int CeedQFunction_count = 0; 416 static int CeedQFunction_n = 0; 417 static int CeedQFunction_count_max = 0; 418 419 struct fContext { 420 void (*f)(void *ctx, int *nq, 421 const CeedScalar *u,const CeedScalar *u1,const CeedScalar *u2, 422 const CeedScalar *u3, 423 const CeedScalar *u4,const CeedScalar *u5,const CeedScalar *u6, 424 const CeedScalar *u7, 425 const CeedScalar *u8,const CeedScalar *u9,const CeedScalar *u10, 426 const CeedScalar *u11, 427 const CeedScalar *u12,const CeedScalar *u13,const CeedScalar *u14, 428 const CeedScalar *u15, 429 CeedScalar *v,CeedScalar *v1, CeedScalar *v2,CeedScalar *v3, 430 CeedScalar *v4,CeedScalar *v5, CeedScalar *v6,CeedScalar *v7, 431 CeedScalar *v8,CeedScalar *v9, CeedScalar *v10,CeedScalar *v11, 432 CeedScalar *v12,CeedScalar *v13, CeedScalar *v14,CeedScalar *v15, int *err); 433 void *innerctx; 434 }; 435 436 static int CeedQFunctionFortranStub(void *ctx, int nq, 437 const CeedScalar *const *u, CeedScalar *const *v) { 438 struct fContext *fctx = ctx; 439 int ierr; 440 441 CeedScalar ctx_=1.0; 442 fctx->f((void*)&ctx_,&nq,u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7], 443 u[8],u[9],u[10],u[11],u[12],u[13],u[14],u[15], 444 v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7], 445 v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],&ierr); 446 return ierr; 447 } 448 449 #define fCeedQFunctionCreateInterior \ 450 FORTRAN_NAME(ceedqfunctioncreateinterior, CEEDQFUNCTIONCREATEINTERIOR) 451 void fCeedQFunctionCreateInterior(int* ceed, int* vlength, 452 void (*f)(void *ctx, int *nq, 453 const CeedScalar *u,const CeedScalar *u1,const CeedScalar *u2, 454 const CeedScalar *u3, 455 const CeedScalar *u4,const CeedScalar *u5,const CeedScalar *u6, 456 const CeedScalar *u7, 457 const CeedScalar *u8,const CeedScalar *u9,const CeedScalar *u10, 458 const CeedScalar *u11, 459 const CeedScalar *u12,const CeedScalar *u13,const CeedScalar *u14, 460 const CeedScalar *u15, 461 CeedScalar *v,CeedScalar *v1, CeedScalar *v2,CeedScalar *v3, 462 CeedScalar *v4,CeedScalar *v5, CeedScalar *v6,CeedScalar *v7, 463 CeedScalar *v8,CeedScalar *v9, CeedScalar *v10,CeedScalar *v11, 464 CeedScalar *v12,CeedScalar *v13, CeedScalar *v14,CeedScalar *v15, 465 int *err), 466 const char *focca, int *qf, int *err, 467 fortran_charlen_t focca_len) { 468 FIX_STRING(focca); 469 if (CeedQFunction_count == CeedQFunction_count_max) { 470 CeedQFunction_count_max += CeedQFunction_count_max/2 + 1; 471 CeedRealloc(CeedQFunction_count_max, &CeedQFunction_dict); 472 } 473 474 CeedQFunction *qf_ = &CeedQFunction_dict[CeedQFunction_count]; 475 *err = CeedQFunctionCreateInterior(Ceed_dict[*ceed], *vlength, 476 CeedQFunctionFortranStub,focca_c, qf_); 477 478 if (*err == 0) { 479 *qf = CeedQFunction_count++; 480 CeedQFunction_n++; 481 } 482 483 struct fContext *fctx; 484 *err = CeedMalloc(1, &fctx); 485 if (*err) return; 486 fctx->f = f; fctx->innerctx = NULL; 487 488 *err = CeedQFunctionSetContext(*qf_, fctx, sizeof(struct fContext)); 489 490 } 491 492 #define fCeedQFunctionAddInput \ 493 FORTRAN_NAME(ceedqfunctionaddinput,CEEDQFUNCTIONADDINPUT) 494 void fCeedQFunctionAddInput(int *qf, const char *fieldname, 495 CeedInt *ncomp, CeedEvalMode *emode, int *err, 496 fortran_charlen_t fieldname_len) { 497 FIX_STRING(fieldname); 498 CeedQFunction qf_ = CeedQFunction_dict[*qf]; 499 500 *err = CeedQFunctionAddInput(qf_, fieldname_c, *ncomp, *emode); 501 } 502 503 #define fCeedQFunctionAddOutput \ 504 FORTRAN_NAME(ceedqfunctionaddoutput,CEEDQFUNCTIONADDOUTPUT) 505 void fCeedQFunctionAddOutput(int *qf, const char *fieldname, 506 CeedInt *ncomp, CeedEvalMode *emode, int *err, 507 fortran_charlen_t fieldname_len) { 508 FIX_STRING(fieldname); 509 CeedQFunction qf_ = CeedQFunction_dict[*qf]; 510 511 *err = CeedQFunctionAddOutput(qf_, fieldname_c, *ncomp, *emode); 512 } 513 514 #define fCeedQFunctionApply \ 515 FORTRAN_NAME(ceedqfunctionapply,CEEDQFUNCTIONAPPLY) 516 //TODO Need Fixing, double pointer 517 void fCeedQFunctionApply(int *qf, int *Q, 518 const CeedScalar *u,const CeedScalar *u1,const CeedScalar *u2, 519 const CeedScalar *u3, 520 const CeedScalar *u4,const CeedScalar *u5,const CeedScalar *u6, 521 const CeedScalar *u7, 522 const CeedScalar *u8,const CeedScalar *u9,const CeedScalar *u10, 523 const CeedScalar *u11, 524 const CeedScalar *u12,const CeedScalar *u13,const CeedScalar *u14, 525 const CeedScalar *u15, 526 CeedScalar *v,CeedScalar *v1, CeedScalar *v2,CeedScalar *v3, 527 CeedScalar *v4,CeedScalar *v5, CeedScalar *v6,CeedScalar *v7, 528 CeedScalar *v8,CeedScalar *v9, CeedScalar *v10,CeedScalar *v11, 529 CeedScalar *v12,CeedScalar *v13, CeedScalar *v14,CeedScalar *v15, int *err) { 530 CeedQFunction qf_ = CeedQFunction_dict[*qf]; 531 const CeedScalar **in; 532 *err = CeedCalloc(16, &in); 533 if (*err) return; 534 in[0] = u; 535 in[1] = u1; 536 in[2] = u2; 537 in[3] = u3; 538 in[4] = u4; 539 in[5] = u5; 540 in[6] = u6; 541 in[7] = u7; 542 in[8] = u8; 543 in[9] = u9; 544 in[10] = u10; 545 in[11] = u11; 546 in[12] = u12; 547 in[13] = u13; 548 in[14] = u14; 549 in[15] = u15; 550 CeedScalar **out; 551 *err = CeedCalloc(16, &out); 552 if (*err) return; 553 out[0] = v; 554 out[1] = v1; 555 out[2] = v2; 556 out[3] = v3; 557 out[4] = v4; 558 out[5] = v5; 559 out[6] = v6; 560 out[7] = v7; 561 out[8] = v8; 562 out[9] = v9; 563 out[10] = v10; 564 out[11] = v11; 565 out[12] = v12; 566 out[13] = v13; 567 out[14] = v14; 568 out[15] = v15; 569 *err = CeedQFunctionApply(qf_, *Q, (const CeedScalar * const*)in, out); 570 if (*err) return; 571 572 *err = CeedFree(&in); 573 if (*err) return; 574 *err = CeedFree(&out); 575 } 576 577 #define fCeedQFunctionDestroy \ 578 FORTRAN_NAME(ceedqfunctiondestroy,CEEDQFUNCTIONDESTROY) 579 void fCeedQFunctionDestroy(int *qf, int *err) { 580 CeedFree(&CeedQFunction_dict[*qf]->ctx); 581 *err = CeedQFunctionDestroy(&CeedQFunction_dict[*qf]); 582 583 if (*err) return; 584 CeedQFunction_n--; 585 if (CeedQFunction_n == 0) { 586 *err = CeedFree(&CeedQFunction_dict); 587 CeedQFunction_count = 0; 588 CeedQFunction_count_max = 0; 589 } 590 } 591 592 static CeedOperator *CeedOperator_dict = NULL; 593 static int CeedOperator_count = 0; 594 static int CeedOperator_n = 0; 595 static int CeedOperator_count_max = 0; 596 597 #define fCeedOperatorCreate \ 598 FORTRAN_NAME(ceedoperatorcreate, CEEDOPERATORCREATE) 599 void fCeedOperatorCreate(int* ceed, 600 int* qf, int* dqf, int* dqfT, int *op, int *err) { 601 if (CeedOperator_count == CeedOperator_count_max) 602 CeedOperator_count_max += CeedOperator_count_max/2 + 1, 603 CeedOperator_dict = 604 realloc(CeedOperator_dict, sizeof(CeedOperator)*CeedOperator_count_max); 605 606 CeedOperator *op_ = &CeedOperator_dict[CeedOperator_count]; 607 608 CeedQFunction dqf_ = NULL, dqfT_ = NULL; 609 if (*dqf != FORTRAN_NULL) dqf_ = CeedQFunction_dict[*dqf ]; 610 if (*dqfT != FORTRAN_NULL) dqfT_ = CeedQFunction_dict[*dqfT]; 611 612 *err = CeedOperatorCreate(Ceed_dict[*ceed], CeedQFunction_dict[*qf], dqf_, 613 dqfT_, op_); 614 if (*err) return; 615 *op = CeedOperator_count++; 616 CeedOperator_n++; 617 } 618 619 #define fCeedOperatorSetField \ 620 FORTRAN_NAME(ceedoperatorsetfield,CEEDOPERATORSETFIELD) 621 void fCeedOperatorSetField(int *op, const char *fieldname, 622 int *r, int *b, int *v, int *err, 623 fortran_charlen_t fieldname_len) { 624 FIX_STRING(fieldname); 625 CeedElemRestriction r_; 626 CeedBasis b_; 627 CeedVector v_; 628 629 CeedOperator op_ = CeedOperator_dict[*op]; 630 631 if (*r == FORTRAN_NULL) { 632 r_ = NULL; 633 } else { 634 r_ = CeedElemRestriction_dict[*r]; 635 } 636 if (*b == FORTRAN_NULL) { 637 b_ = NULL; 638 } else if (*b == FORTRAN_BASIS_COLLOCATED) { 639 b_ = CEED_BASIS_COLLOCATED; 640 } else { 641 b_ = CeedBasis_dict[*b]; 642 } 643 if (*v == FORTRAN_NULL) { 644 v_ = NULL; 645 } else if (*v == FORTRAN_VECTOR_ACTIVE) { 646 v_ = CEED_VECTOR_ACTIVE; 647 } else if (*v == FORTRAN_VECTOR_NONE) { 648 v_ = CEED_VECTOR_NONE; 649 } else { 650 v_ = CeedVector_dict[*v]; 651 } 652 653 *err = CeedOperatorSetField(op_, fieldname_c, r_, b_, v_); 654 } 655 656 #define fCeedOperatorApply FORTRAN_NAME(ceedoperatorapply, CEEDOPERATORAPPLY) 657 void fCeedOperatorApply(int *op, int *ustatevec, 658 int *resvec, int *rqst, int *err) { 659 CeedVector ustatevec_ = *ustatevec == FORTRAN_NULL 660 ? NULL : CeedVector_dict[*ustatevec]; 661 CeedVector resvec_ = *resvec == FORTRAN_NULL 662 ? NULL : CeedVector_dict[*resvec]; 663 664 int createRequest = 1; 665 // Check if input is CEED_REQUEST_ORDERED(-2) or CEED_REQUEST_IMMEDIATE(-1) 666 if (*rqst == -1 || *rqst == -2) { 667 createRequest = 0; 668 } 669 670 if (createRequest && CeedRequest_count == CeedRequest_count_max) { 671 CeedRequest_count_max += CeedRequest_count_max/2 + 1; 672 CeedRealloc(CeedRequest_count_max, &CeedRequest_dict); 673 } 674 675 CeedRequest *rqst_; 676 if (*rqst == -1) rqst_ = CEED_REQUEST_IMMEDIATE; 677 else if (*rqst == -2) rqst_ = CEED_REQUEST_ORDERED; 678 else rqst_ = &CeedRequest_dict[CeedRequest_count]; 679 680 *err = CeedOperatorApply(CeedOperator_dict[*op], 681 ustatevec_, resvec_, rqst_); 682 if (*err) return; 683 if (createRequest) { 684 *rqst = CeedRequest_count++; 685 CeedRequest_n++; 686 } 687 } 688 689 #define fCeedOperatorApplyJacobian \ 690 FORTRAN_NAME(ceedoperatorapplyjacobian, CEEDOPERATORAPPLYJACOBIAN) 691 void fCeedOperatorApplyJacobian(int *op, int *qdatavec, int *ustatevec, 692 int *dustatevec, int *dresvec, int *rqst, int *err) { 693 // TODO Uncomment this when CeedOperatorApplyJacobian is implemented 694 // *err = CeedOperatorApplyJacobian(CeedOperator_dict[*op], CeedVector_dict[*qdatavec], 695 // CeedVector_dict[*ustatevec], CeedVector_dict[*dustatevec], 696 // CeedVector_dict[*dresvec], &CeedRequest_dict[*rqst]); 697 } 698 699 #define fCeedOperatorDestroy \ 700 FORTRAN_NAME(ceedoperatordestroy, CEEDOPERATORDESTROY) 701 void fCeedOperatorDestroy(int *op, int *err) { 702 *err = CeedOperatorDestroy(&CeedOperator_dict[*op]); 703 if (*err) return; 704 CeedOperator_n--; 705 if (CeedOperator_n == 0) { 706 *err = CeedFree(&CeedOperator_dict); 707 CeedOperator_count = 0; 708 CeedOperator_count_max = 0; 709 } 710 } 711