"""The best boundary on the ConStellaration geometrical leaderboard (canyon-ant, 0.98048), drawn from its Fourier series. Data: boundary.json, the submission's SurfaceRZFourier coefficients, copied from the leaderboard's results dataset https://huggingface.co/datasets/proxima-fusion/constellaration-bench-results (geometrical/2026-09-09T21-10-50.069479). R(θ,φ) = Σ r_cos[m][n+N] cos(mθ − Nfp·n·φ), Z(θ,φ) = Σ z_sin[m][n+N] sin(mθ − Nfp·n·φ), as in README.md. The surface is drawn as line art seen from above at an angle, with hidden lines removed: its outline in ink, a few lines of constant θ as context, and in the field colour the poloidal cross-sections whose roundness the score measures, at the two symmetric angles φ = 0 and φ = π/Nfp of each field period (where triangularity is taken). """ import json import math from pathlib import Path import kit HERE = Path(__file__).resolve().parent B = json.loads((HERE / 'boundary.json').read_text()) RC, ZS, NFP = B['r_cos'], B['z_sin'], B['n_field_periods'] MM, NN = len(RC), (len(RC[0]) - 1) // 2 assert NFP == 3 and B['user'] == 'canyon-ant' and round(B['score'], 5) == 0.98048 assert abs(B['score'] - (1 - (B['max_elongation'] - 1) / 9)) < 1e-9, 'score = 1 − (ε_max − 1)/9' assert round(B['max_elongation'], 3) == 1.176 AZ, EL = math.radians(-30), math.radians(45) # the view: one lobe to the lower left, the twist visible on the right NT, NP = 48, 3 * 60 # θ and φ samples over the whole torus RINGS = range(0, NP, NP // (2 * NFP)) # φ = k·π/Nfp LINES = range(0, NT, 6) # lines of constant θ, as context def rz(th, ph): R = Z = 0.0 for m in range(MM): for k in range(2 * NN + 1): a = m * th - NFP * (k - NN) * ph R += RC[m][k] * math.cos(a) Z += ZS[m][k] * math.sin(a) return R, Z def view(x, y, z): """Turned by AZ about the vertical, then tilted by EL: screen right, screen up, depth towards the viewer.""" x, y = x * math.cos(AZ) - y * math.sin(AZ), x * math.sin(AZ) + y * math.cos(AZ) return x, z * math.cos(EL) + y * math.sin(EL), -y * math.cos(EL) + z * math.sin(EL) V = [] for i in range(NP): ph = 2 * math.pi * i / NP row = [] for j in range(NT): R, Z = rz(2 * math.pi * j / NT, ph) row.append(view(R * math.cos(ph), R * math.sin(ph), Z)) V.append(row) v = lambda i, j: V[i % NP][j % NT] # triangles of the surface, bucketed on the screen, for the visibility test CELL = 0.05 tris, buckets = [], {} for i in range(NP): for j in range(NT): for t in ((v(i, j), v(i + 1, j), v(i + 1, j + 1)), (v(i, j), v(i + 1, j + 1), v(i, j + 1))): k = len(tris) tris.append((t, i, j)) x0, x1 = min(p[0] for p in t), max(p[0] for p in t) y0, y1 = min(p[1] for p in t), max(p[1] for p in t) for bx in range(int(math.floor(x0 / CELL)), int(math.floor(x1 / CELL)) + 1): for by in range(int(math.floor(y0 / CELL)), int(math.floor(y1 / CELL)) + 1): buckets.setdefault((bx, by), []).append(k) def hidden(p, i, j): """Is the surface point p, near grid cell (i, j), behind another part of the surface?""" for k in buckets.get((int(math.floor(p[0] / CELL)), int(math.floor(p[1] / CELL))), ()): (a, b, c), ti, tj = tris[k] if min((ti - i) % NP, (i - ti) % NP) <= 2 and min((tj - j) % NT, (j - tj) % NT) <= 2: continue d = (b[1] - c[1]) * (a[0] - c[0]) + (c[0] - b[0]) * (a[1] - c[1]) if abs(d) < 1e-12: continue l1 = ((b[1] - c[1]) * (p[0] - c[0]) + (c[0] - b[0]) * (p[1] - c[1])) / d l2 = ((c[1] - a[1]) * (p[0] - c[0]) + (a[0] - c[0]) * (p[1] - c[1])) / d l3 = 1 - l1 - l2 if min(l1, l2, l3) >= 0 and l1 * a[2] + l2 * b[2] + l3 * c[2] > p[2] + 1e-3: return True return False def mid(p, q): return tuple((a + b) / 2 for a, b in zip(p, q)) def facing(i, j): a, b, c, d = v(i, j), v(i + 1, j), v(i + 1, j + 1), v(i, j + 1) return (c[0] - a[0]) * (d[1] - b[1]) - (c[1] - a[1]) * (d[0] - b[0]) > 0 segs = {'f': [], 'faint': [], 'ink': []} for i in RINGS: # cross-sections: edges (i, j)–(i, j+1) segs['f'] += [(v(i, j), v(i, j + 1), i, j) for j in range(NT)] for j in LINES: # constant θ: edges (i, j)–(i+1, j) segs['faint'] += [(v(i, j), v(i + 1, j), i, j) for i in range(NP)] F = [[facing(i, j) for j in range(NT)] for i in range(NP)] for i in range(NP): # the outline: edges between a front and a back patch for j in range(NT): if F[i][j] != F[i][(j - 1) % NT]: segs['ink'].append((v(i, j), v(i + 1, j), i, j)) if F[i][j] != F[(i - 1) % NP][j]: segs['ink'].append((v(i, j), v(i, j + 1), i, j)) xs = [p[0] for row in V for p in row] ys = [p[1] for row in V for p in row] sc = min(600 / (max(xs) - min(xs)), 250 / (max(ys) - min(ys))) ox, oy = kit.W / 2 - sc * (max(xs) + min(xs)) / 2, 134 + sc * (max(ys) + min(ys)) / 2 px = lambda p: (ox + sc * p[0], oy - sc * p[1]) body = [] for cls, w in (('faint', 1.6), ('ink', 2.4), ('f', 4)): d = ''.join(f'M{kit.f1(px(a)[0])},{kit.f1(px(a)[1])}L{kit.f1(px(b)[0])},{kit.f1(px(b)[1])}' for a, b, i, j in segs[cls] if not hidden(mid(a, b), i, j)) body.append(kit.path(d, cls + '-s', w)) kit.write(__file__, 'engineering-energy', ''.join(body), label='best boundary · max elongation ≈1.176 (circle 1)', title='The best ConStellaration geometrical boundary (canyon-ant, score 0.98048), with its cross-sections') print(f'scale {sc:.0f}, {sum(len(s) for s in segs.values())} segments')