1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 3 // All Rights reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 18 /// A structure used to pass additional data to f_build_diff and f_apply_diff 19 struct BuildContext { CeedInt dim, space_dim; }; 20 21 /// libCEED Q-function for building quadrature data for a diffusion operator 22 CEED_QFUNCTION(f_build_diff)(void *ctx, const CeedInt Q, 23 const CeedScalar *const *in, CeedScalar *const *out) { 24 BuildContext *bc = (BuildContext *)ctx; 25 // in[0] is Jacobians with shape [dim, nc=dim, Q] 26 // in[1] is quadrature weights, size (Q) 27 // 28 // At every quadrature point, compute qw/det(J).adj(J).adj(J)^T and store 29 // the symmetric part of the result. 30 const CeedScalar *J = in[0], *qw = in[1]; 31 CeedScalar *qd = out[0]; 32 switch (bc->dim + 10*bc->space_dim) { 33 case 11: 34 for (CeedInt i=0; i<Q; i++) { 35 qd[i] = qw[i] / J[i]; 36 } 37 break; 38 case 22: 39 for (CeedInt i=0; i<Q; i++) { 40 // J: 0 2 qd: 0 1 adj(J): J22 -J12 41 // 1 3 1 2 -J21 J11 42 const CeedScalar J11 = J[i+Q*0]; 43 const CeedScalar J21 = J[i+Q*1]; 44 const CeedScalar J12 = J[i+Q*2]; 45 const CeedScalar J22 = J[i+Q*3]; 46 const CeedScalar w = qw[i] / (J11*J22 - J21*J12); 47 qd[i+Q*0] = w * (J12*J12 + J22*J22); 48 qd[i+Q*1] = - w * (J11*J12 + J21*J22); 49 qd[i+Q*2] = w * (J11*J11 + J21*J21); 50 } 51 break; 52 case 33: 53 for (CeedInt i=0; i<Q; i++) { 54 // J: 0 3 6 qd: 0 1 2 55 // 1 4 7 1 3 4 56 // 2 5 8 2 4 5 57 const CeedScalar J11 = J[i+Q*0]; 58 const CeedScalar J21 = J[i+Q*1]; 59 const CeedScalar J31 = J[i+Q*2]; 60 const CeedScalar J12 = J[i+Q*3]; 61 const CeedScalar J22 = J[i+Q*4]; 62 const CeedScalar J32 = J[i+Q*5]; 63 const CeedScalar J13 = J[i+Q*6]; 64 const CeedScalar J23 = J[i+Q*7]; 65 const CeedScalar J33 = J[i+Q*8]; 66 const CeedScalar A11 = J22*J33 - J23*J32; 67 const CeedScalar A12 = J13*J32 - J12*J33; 68 const CeedScalar A13 = J12*J23 - J13*J22; 69 const CeedScalar A21 = J23*J31 - J21*J33; 70 const CeedScalar A22 = J11*J33 - J13*J31; 71 const CeedScalar A23 = J13*J21 - J11*J23; 72 const CeedScalar A31 = J21*J32 - J22*J31; 73 const CeedScalar A32 = J12*J31 - J11*J32; 74 const CeedScalar A33 = J11*J22 - J12*J21; 75 const CeedScalar w = qw[i] / (J11*A11 + J21*A12 + J31*A13); 76 qd[i+Q*0] = w * (A11*A11 + A12*A12 + A13*A13); 77 qd[i+Q*1] = w * (A11*A21 + A12*A22 + A13*A23); 78 qd[i+Q*2] = w * (A11*A31 + A12*A32 + A13*A33); 79 qd[i+Q*3] = w * (A21*A21 + A22*A22 + A23*A23); 80 qd[i+Q*4] = w * (A21*A31 + A22*A32 + A23*A33); 81 qd[i+Q*5] = w * (A31*A31 + A32*A32 + A33*A33); 82 } 83 break; 84 } 85 return 0; 86 } 87 88 /// libCEED Q-function for applying a diff operator 89 CEED_QFUNCTION(f_apply_diff)(void *ctx, const CeedInt Q, 90 const CeedScalar *const *in, CeedScalar *const *out) { 91 BuildContext *bc = (BuildContext *)ctx; 92 // in[0], out[0] have shape [dim, nc=1, Q] 93 const CeedScalar *ug = in[0], *qd = in[1]; 94 CeedScalar *vg = out[0]; 95 switch (bc->dim) { 96 case 1: 97 for (CeedInt i=0; i<Q; i++) { 98 vg[i] = ug[i] * qd[i]; 99 } 100 break; 101 case 2: 102 for (CeedInt i=0; i<Q; i++) { 103 const CeedScalar ug0 = ug[i+Q*0]; 104 const CeedScalar ug1 = ug[i+Q*1]; 105 vg[i+Q*0] = qd[i+Q*0]*ug0 + qd[i+Q*1]*ug1; 106 vg[i+Q*1] = qd[i+Q*1]*ug0 + qd[i+Q*2]*ug1; 107 } 108 break; 109 case 3: 110 for (CeedInt i=0; i<Q; i++) { 111 const CeedScalar ug0 = ug[i+Q*0]; 112 const CeedScalar ug1 = ug[i+Q*1]; 113 const CeedScalar ug2 = ug[i+Q*2]; 114 vg[i+Q*0] = qd[i+Q*0]*ug0 + qd[i+Q*1]*ug1 + qd[i+Q*2]*ug2; 115 vg[i+Q*1] = qd[i+Q*1]*ug0 + qd[i+Q*3]*ug1 + qd[i+Q*4]*ug2; 116 vg[i+Q*2] = qd[i+Q*2]*ug0 + qd[i+Q*4]*ug1 + qd[i+Q*5]*ug2; 117 } 118 break; 119 } 120 return 0; 121 } 122