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 #ifndef ex2_surface_h 18 #define ex2_surface_h 19 20 /// A structure used to pass additional data to f_build_diff 21 struct BuildContext { CeedInt dim, space_dim; }; 22 23 /// libCEED Q-function for building quadrature data for a diffusion operator 24 CEED_QFUNCTION(f_build_diff)(void *ctx, const CeedInt Q, 25 const CeedScalar *const *in, CeedScalar *const *out) { 26 struct BuildContext *bc = (struct BuildContext *)ctx; 27 // in[0] is Jacobians with shape [dim, nc=dim, Q] 28 // in[1] is quadrature weights, size (Q) 29 // 30 // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store 31 // the symmetric part of the result. 32 const CeedScalar *J = in[0], *w = in[1]; 33 CeedScalar *q_data = out[0]; 34 35 switch (bc->dim + 10*bc->space_dim) { 36 case 11: 37 CeedPragmaSIMD 38 for (CeedInt i=0; i<Q; i++) { 39 q_data[i] = w[i] / J[i]; 40 } // End of Quadrature Point Loop 41 break; 42 case 22: 43 CeedPragmaSIMD 44 for (CeedInt i=0; i<Q; i++) { 45 // J: 0 2 q_data: 0 2 adj(J): J22 -J12 46 // 1 3 2 1 -J21 J11 47 const CeedScalar J11 = J[i+Q*0]; 48 const CeedScalar J21 = J[i+Q*1]; 49 const CeedScalar J12 = J[i+Q*2]; 50 const CeedScalar J22 = J[i+Q*3]; 51 const CeedScalar qw = w[i] / (J11*J22 - J21*J12); 52 q_data[i+Q*0] = qw * (J12*J12 + J22*J22); 53 q_data[i+Q*1] = qw * (J11*J11 + J21*J21); 54 q_data[i+Q*2] = - qw * (J11*J12 + J21*J22); 55 } // End of Quadrature Point Loop 56 break; 57 case 33: 58 CeedPragmaSIMD 59 for (CeedInt i=0; i<Q; i++) { 60 // Compute the adjoint 61 CeedScalar A[3][3]; 62 for (CeedInt j=0; j<3; j++) 63 for (CeedInt k=0; k<3; k++) 64 // Equivalent code with J as a VLA and no mod operations: 65 // A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1] 66 A[k][j] = J[i+Q*((j+1)%3+3*((k+1)%3))]*J[i+Q*((j+2)%3+3*((k+2)%3))] - 67 J[i+Q*((j+1)%3+3*((k+2)%3))]*J[i+Q*((j+2)%3+3*((k+1)%3))]; 68 69 // Compute quadrature weight / det(J) 70 const CeedScalar qw = w[i] / (J[i+Q*0]*A[0][0] + J[i+Q*1]*A[1][1] + 71 J[i+Q*2]*A[2][2]); 72 73 // Compute geometric factors 74 // Stored in Voigt convention 75 // 0 5 4 76 // 5 1 3 77 // 4 3 2 78 q_data[i+Q*0] = qw * (A[0][0]*A[0][0] + A[0][1]*A[0][1] + A[0][2]*A[0][2]); 79 q_data[i+Q*1] = qw * (A[1][0]*A[1][0] + A[1][1]*A[1][1] + A[1][2]*A[1][2]); 80 q_data[i+Q*2] = qw * (A[2][0]*A[2][0] + A[2][1]*A[2][1] + A[2][2]*A[2][2]); 81 q_data[i+Q*3] = qw * (A[1][0]*A[2][0] + A[1][1]*A[2][1] + A[1][2]*A[2][2]); 82 q_data[i+Q*4] = qw * (A[0][0]*A[2][0] + A[0][1]*A[2][1] + A[0][2]*A[2][2]); 83 q_data[i+Q*5] = qw * (A[0][0]*A[1][0] + A[0][1]*A[1][1] + A[0][2]*A[1][2]); 84 } // End of Quadrature Point Loop 85 break; 86 } 87 return 0; 88 } 89 90 /// libCEED Q-function for applying a diff operator 91 CEED_QFUNCTION(f_apply_diff)(void *ctx, const CeedInt Q, 92 const CeedScalar *const *in, CeedScalar *const *out) { 93 struct BuildContext *bc = (struct BuildContext *)ctx; 94 // in[0], out[0] have shape [dim, nc=1, Q] 95 const CeedScalar *ug = in[0], *q_data = in[1]; 96 CeedScalar *vg = out[0]; 97 98 switch (bc->dim) { 99 case 1: 100 CeedPragmaSIMD 101 for (CeedInt i=0; i<Q; i++) { 102 vg[i] = ug[i] * q_data[i]; 103 } // End of Quadrature Point Loop 104 break; 105 case 2: 106 CeedPragmaSIMD 107 for (CeedInt i=0; i<Q; i++) { 108 // Read spatial derivatives of u 109 const CeedScalar du[2] = {ug[i+Q*0], 110 ug[i+Q*1] 111 }; 112 113 // Read q_data (dXdxdXdx_T symmetric matrix) 114 // Stored in Voigt convention 115 // 0 2 116 // 2 1 117 // *INDENT-OFF* 118 const CeedScalar dXdxdXdx_T[2][2] = {{q_data[i+0*Q], 119 q_data[i+2*Q]}, 120 {q_data[i+2*Q], 121 q_data[i+1*Q]}}; 122 // *INDENT-ON* 123 // j = direction of vg 124 for (int j=0; j<2; j++) 125 vg[i+j*Q] = (du[0] * dXdxdXdx_T[0][j] + 126 du[1] * dXdxdXdx_T[1][j]); 127 } // End of Quadrature Point Loop 128 break; 129 case 3: 130 CeedPragmaSIMD 131 for (CeedInt i=0; i<Q; i++) { 132 // Read spatial derivatives of u 133 const CeedScalar du[3] = {ug[i+Q*0], 134 ug[i+Q*1], 135 ug[i+Q*2] 136 }; 137 138 // Read q_data (dXdxdXdx_T symmetric matrix) 139 // Stored in Voigt convention 140 // 0 5 4 141 // 5 1 3 142 // 4 3 2 143 // *INDENT-OFF* 144 const CeedScalar dXdxdXdx_T[3][3] = {{q_data[i+0*Q], 145 q_data[i+5*Q], 146 q_data[i+4*Q]}, 147 {q_data[i+5*Q], 148 q_data[i+1*Q], 149 q_data[i+3*Q]}, 150 {q_data[i+4*Q], 151 q_data[i+3*Q], 152 q_data[i+2*Q]} 153 }; 154 // *INDENT-ON* 155 // j = direction of vg 156 for (int j=0; j<3; j++) 157 vg[i+j*Q] = (du[0] * dXdxdXdx_T[0][j] + 158 du[1] * dXdxdXdx_T[1][j] + 159 du[2] * dXdxdXdx_T[2][j]); 160 } // End of Quadrature Point Loop 161 break; 162 } 163 return 0; 164 } 165 166 #endif // ex2_surface_h 167