Skip to content

Commit b9e4153

Browse files
Fortran example 2 (#1953)
* Fortran example 2(surface area with diff operator). Adding ex2-surface-f.f90, ex2-surface-f.h, and ex2-surface-f-c.h, trying to translate the c example as accurately as possible and following the format of the first fortran example. Test gave almost exact same results as c example. * Fixed ex2-surface-f-c.h for GPU * Style fix * Update examples/ceed/ex2-surface-f.h Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update examples/ceed/ex2-surface-f.f90 Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update examples/ceed/ex2-surface-f.f90 Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Apply suggestion from @jeremylt Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Apply suggestion from @jeremylt Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Fortran version of example 3 * Fortran example 3, fixed the indexing issue * Update examples/ceed/ex3-volume-f.h Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update examples/ceed/ex3-volume-f-c.h Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * type change fixed in ex3-volume-f-c.h * Aplogies did not see that comment * Update examples/ceed/ex3-volume-f-c.h Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update examples/ceed/ex3-volume-f-c.h Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update examples/ceed/ex3-volume-f.f90 Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * CI style fix * Update examples/ceed/ex3-volume-f-c.h --------- Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org>
1 parent db96194 commit b9e4153

6 files changed

Lines changed: 1761 additions & 0 deletions

File tree

examples/ceed/ex2-surface-f-c.h

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2+
// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3+
//
4+
// SPDX-License-Identifier: BSD-2-Clause
5+
//
6+
// This file is part of CEED: http://github.com/ceed
7+
8+
#include <ceed/types.h>
9+
10+
/// libCEED Q-function for building quadrature data for a diffusion operator
11+
CEED_QFUNCTION(build_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
12+
const long dim = ((const long *)ctx)[0];
13+
const long space_dim = ((const long *)ctx)[1];
14+
15+
// in[0] is Jacobians with shape [dim, dim, Q]
16+
// in[1] is quadrature weights, size (Q)
17+
const CeedScalar *w = in[1];
18+
CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
19+
20+
// At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
21+
// the symmetric part of the result.
22+
switch (dim + 10 * space_dim) {
23+
case 11: {
24+
const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
25+
26+
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { q_data[0][i] = w[i] / J[0][0][i]; } // End of Quadrature Point Loop
27+
} break;
28+
case 22: {
29+
const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
30+
31+
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
32+
// J: 0 2 q_data: 0 2 adj(J): J11 -J01
33+
// 1 3 2 1 -J10 J00
34+
const CeedScalar J00 = J[0][0][i];
35+
const CeedScalar J10 = J[0][1][i];
36+
const CeedScalar J01 = J[1][0][i];
37+
const CeedScalar J11 = J[1][1][i];
38+
const CeedScalar qw = w[i] / (J00 * J11 - J10 * J01);
39+
40+
q_data[0][i] = qw * (J01 * J01 + J11 * J11);
41+
q_data[1][i] = qw * (J00 * J00 + J10 * J10);
42+
q_data[2][i] = -qw * (J00 * J01 + J10 * J11);
43+
} // End of Quadrature Point Loop
44+
} break;
45+
case 33: {
46+
const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
47+
48+
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
49+
// Compute the adjoint
50+
CeedScalar A[3][3];
51+
52+
for (CeedInt j = 0; j < 3; j++) {
53+
for (CeedInt k = 0; k < 3; k++) {
54+
// Equivalent code with J as a VLA and no mod operations:
55+
// A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1]
56+
A[k][j] =
57+
J[(k + 1) % 3][(j + 1) % 3][i] * J[(k + 2) % 3][(j + 2) % 3][i] - J[(k + 2) % 3][(j + 1) % 3][i] * J[(k + 1) % 3][(j + 2) % 3][i];
58+
}
59+
}
60+
61+
// Compute quadrature weight / det(J)
62+
const CeedScalar qw = w[i] / (J[0][0][i] * A[0][0] + J[0][1][i] * A[0][1] + J[0][2][i] * A[0][2]);
63+
64+
// Compute geometric factors
65+
// Stored in Voigt convention
66+
// 0 5 4
67+
// 5 1 3
68+
// 4 3 2
69+
q_data[0][i] = qw * (A[0][0] * A[0][0] + A[0][1] * A[0][1] + A[0][2] * A[0][2]);
70+
q_data[1][i] = qw * (A[1][0] * A[1][0] + A[1][1] * A[1][1] + A[1][2] * A[1][2]);
71+
q_data[2][i] = qw * (A[2][0] * A[2][0] + A[2][1] * A[2][1] + A[2][2] * A[2][2]);
72+
q_data[3][i] = qw * (A[1][0] * A[2][0] + A[1][1] * A[2][1] + A[1][2] * A[2][2]);
73+
q_data[4][i] = qw * (A[0][0] * A[2][0] + A[0][1] * A[2][1] + A[0][2] * A[2][2]);
74+
q_data[5][i] = qw * (A[0][0] * A[1][0] + A[0][1] * A[1][1] + A[0][2] * A[1][2]);
75+
} // End of Quadrature Point Loop
76+
} break;
77+
}
78+
return CEED_ERROR_SUCCESS;
79+
}
80+
81+
/// libCEED Q-function for applying a diff operator
82+
CEED_QFUNCTION(apply_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
83+
const long dim = ((const long *)ctx)[0];
84+
85+
// in[0], out[0] solution gradients with shape [dim, 1, Q]
86+
// in[1] is quadrature data with shape [num_components, Q]
87+
const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
88+
89+
switch (dim) {
90+
case 1: {
91+
const CeedScalar *ug = in[0];
92+
CeedScalar *vg = out[0];
93+
94+
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { vg[i] = ug[i] * q_data[0][i]; } // End of Quadrature Point Loop
95+
} break;
96+
case 2: {
97+
const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
98+
CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
99+
100+
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
101+
// Read q_data (dXdxdXdx_T symmetric matrix)
102+
// Stored in Voigt convention
103+
// 0 2
104+
// 2 1
105+
const CeedScalar dXdxdXdx_T[2][2] = {
106+
{q_data[0][i], q_data[2][i]},
107+
{q_data[2][i], q_data[1][i]}
108+
};
109+
110+
// j = direction of vg
111+
for (int j = 0; j < 2; j++) vg[j][i] = (ug[0][i] * dXdxdXdx_T[0][j] + ug[1][i] * dXdxdXdx_T[1][j]);
112+
} // End of Quadrature Point Loop
113+
} break;
114+
case 3: {
115+
const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
116+
CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
117+
118+
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
119+
// Read q_data (dXdxdXdx_T symmetric matrix)
120+
// Stored in Voigt convention
121+
// 0 5 4
122+
// 5 1 3
123+
// 4 3 2
124+
const CeedScalar dXdxdXdx_T[3][3] = {
125+
{q_data[0][i], q_data[5][i], q_data[4][i]},
126+
{q_data[5][i], q_data[1][i], q_data[3][i]},
127+
{q_data[4][i], q_data[3][i], q_data[2][i]}
128+
};
129+
130+
// j = direction of vg
131+
for (int j = 0; j < 3; j++) vg[j][i] = (ug[0][i] * dXdxdXdx_T[0][j] + ug[1][i] * dXdxdXdx_T[1][j] + ug[2][i] * dXdxdXdx_T[2][j]);
132+
} // End of Quadrature Point Loop
133+
} break;
134+
}
135+
return CEED_ERROR_SUCCESS;
136+
}

0 commit comments

Comments
 (0)