// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only // The side-chain field's direct sums (SideChainFieldMetal.h), compiled when first used from this source, which // CMakeLists.txt copies into the binary after bragg_integration/BraggExp.h - with fast math off and no a*b+c // contracted, as on the host, so that every operation below rounds as EvaluatorCPU::Contribution's does. #include using namespace metal; #pragma clang fp contract(off) struct Atom { // FieldAtom float x, y, z, b, occ; int element; }; struct Params { uint rows, n_ops, first; }; // SideChainField.cpp's SinCos2Pi(): sin and cos of 2 pi v, v taken to [-1/2, 1/2] by its nearest integer, the half // angle by its Taylor series, then the double-angle formulas. static void sincos_2pi(float v, thread float &sn, thread float &cs) { const float x = 3.14159265f * (v - rint(v)); const float x2 = x * x; const float s = x * (1 + x2 * (-1.f / 6 + x2 * (1.f / 120 + x2 * (-1.f / 5040 + x2 * (1.f / 362880 + x2 * (-1.f / 39916800)))))); const float c = 1 + x2 * (-1.f / 2 + x2 * (1.f / 24 + x2 * (-1.f / 720 + x2 * (1.f / 40320 + x2 * (-1.f / 3628800 + x2 * (1.f / 479001600)))))); sn = 2 * s * c; cs = c * c - s * s; } // One thread per row m and state first + s: the state's atoms in order, each over the operators in order. kernel void contribution(device const float *hx [[buffer(0)]], device const float *hy [[buffer(1)]], device const float *hz [[buffer(2)]], device const float *ht [[buffer(3)]], device const float *f0 [[buffer(4)]], device const float *stol2 [[buffer(5)]], device const Atom *atoms [[buffer(6)]], device const int *start [[buffer(7)]], constant Params &p [[buffer(8)]], device float2 *out [[buffer(9)]], uint2 gid [[thread_position_in_grid]]) { const uint m = gid.x, s = gid.y; if (m >= p.rows) return; const float st = stol2[m]; float re = 0, im = 0; for (int a = start[p.first + s]; a < start[p.first + s + 1]; a++) { const Atom at = atoms[a]; const float fa = f0[uint(at.element) * p.rows + m] * at.occ * BraggExp(-at.b * st); for (uint k = 0; k < p.n_ops; k++) { const uint o = k * p.rows + m; const float v = hx[o] * at.x + hy[o] * at.y + hz[o] * at.z + ht[o]; float sn, cs; sincos_2pi(v, sn, cs); re += fa * cs; im += fa * sn; } } out[s * p.rows + m] = float2(re, im); }