Least-elongated stellarator plasma boundary
Find the least elongated stellarator plasma boundary with aspect ratio at most 4, average triangularity at most −0.5 and edge iota per period at least 0.3.
| 1 | """The best boundary on the ConStellaration geometrical leaderboard (canyon-ant, 0.98048), drawn from its Fourier series. |
| 2 | |
| 3 | Data: boundary.json, the submission's SurfaceRZFourier coefficients, copied from the leaderboard's results dataset |
| 4 | https://huggingface.co/datasets/proxima-fusion/constellaration-bench-results (geometrical/2026-09-09T21-10-50.069479). |
| 5 | R(θ,φ) = Σ r_cos[m][n+N] cos(mθ − Nfp·n·φ), Z(θ,φ) = Σ z_sin[m][n+N] sin(mθ − Nfp·n·φ), as in README.md. |
| 6 | |
| 7 | The surface is drawn as line art seen from above at an angle, with hidden lines removed: its outline in ink, a few |
| 8 | lines of constant θ as context, and in the field colour the poloidal cross-sections whose roundness the score |
| 9 | measures, at the two symmetric angles φ = 0 and φ = π/Nfp of each field period (where triangularity is taken). |
| 10 | """ |
| 11 | import json |
| 12 | import math |
| 13 | from pathlib import Path |
| 14 | |
| 15 | import kit |
| 16 | |
| 17 | HERE = Path(__file__).resolve().parent |
| 18 | B = json.loads((HERE / 'boundary.json').read_text()) |
| 19 | RC, ZS, NFP = B['r_cos'], B['z_sin'], B['n_field_periods'] |
| 20 | MM, NN = len(RC), (len(RC[0]) - 1) // 2 |
| 21 | assert NFP == 3 and B['user'] == 'canyon-ant' and round(B['score'], 5) == 0.98048 |
| 22 | assert abs(B['score'] - (1 - (B['max_elongation'] - 1) / 9)) < 1e-9, 'score = 1 − (ε_max − 1)/9' |
| 23 | assert round(B['max_elongation'], 3) == 1.176 |
| 24 | |
| 25 | AZ, EL = math.radians(-30), math.radians(45) # the view: one lobe to the lower left, the twist visible on the right |
| 26 | NT, NP = 48, 3 * 60 # θ and φ samples over the whole torus |
| 27 | RINGS = range(0, NP, NP // (2 * NFP)) # φ = k·π/Nfp |
| 28 | LINES = range(0, NT, 6) # lines of constant θ, as context |
| 29 | |
| 30 | |
| 31 | def rz(th, ph): |
| 32 | R = Z = 0.0 |
| 33 | for m in range(MM): |
| 34 | for k in range(2 * NN + 1): |
| 35 | a = m * th - NFP * (k - NN) * ph |
| 36 | R += RC[m][k] * math.cos(a) |
| 37 | Z += ZS[m][k] * math.sin(a) |
| 38 | return R, Z |
| 39 | |
| 40 | |
| 41 | def view(x, y, z): |
| 42 | """Turned by AZ about the vertical, then tilted by EL: screen right, screen up, depth towards the viewer.""" |
| 43 | x, y = x * math.cos(AZ) - y * math.sin(AZ), x * math.sin(AZ) + y * math.cos(AZ) |
| 44 | return x, z * math.cos(EL) + y * math.sin(EL), -y * math.cos(EL) + z * math.sin(EL) |
| 45 | |
| 46 | |
| 47 | V = [] |
| 48 | for i in range(NP): |
| 49 | ph = 2 * math.pi * i / NP |
| 50 | row = [] |
| 51 | for j in range(NT): |
| 52 | R, Z = rz(2 * math.pi * j / NT, ph) |
| 53 | row.append(view(R * math.cos(ph), R * math.sin(ph), Z)) |
| 54 | V.append(row) |
| 55 | v = lambda i, j: V[i % NP][j % NT] |
| 56 | |
| 57 | # triangles of the surface, bucketed on the screen, for the visibility test |
| 58 | CELL = 0.05 |
| 59 | tris, buckets = [], {} |
| 60 | for i in range(NP): |
| 61 | for j in range(NT): |
| 62 | 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))): |
| 63 | k = len(tris) |
| 64 | tris.append((t, i, j)) |
| 65 | x0, x1 = min(p[0] for p in t), max(p[0] for p in t) |
| 66 | y0, y1 = min(p[1] for p in t), max(p[1] for p in t) |
| 67 | for bx in range(int(math.floor(x0 / CELL)), int(math.floor(x1 / CELL)) + 1): |
| 68 | for by in range(int(math.floor(y0 / CELL)), int(math.floor(y1 / CELL)) + 1): |
| 69 | buckets.setdefault((bx, by), []).append(k) |
| 70 | |
| 71 | |
| 72 | def hidden(p, i, j): |
| 73 | """Is the surface point p, near grid cell (i, j), behind another part of the surface?""" |
| 74 | for k in buckets.get((int(math.floor(p[0] / CELL)), int(math.floor(p[1] / CELL))), ()): |
| 75 | (a, b, c), ti, tj = tris[k] |
| 76 | if min((ti - i) % NP, (i - ti) % NP) <= 2 and min((tj - j) % NT, (j - tj) % NT) <= 2: |
| 77 | continue |
| 78 | d = (b[1] - c[1]) * (a[0] - c[0]) + (c[0] - b[0]) * (a[1] - c[1]) |
| 79 | if abs(d) < 1e-12: |
| 80 | continue |
| 81 | l1 = ((b[1] - c[1]) * (p[0] - c[0]) + (c[0] - b[0]) * (p[1] - c[1])) / d |
| 82 | l2 = ((c[1] - a[1]) * (p[0] - c[0]) + (a[0] - c[0]) * (p[1] - c[1])) / d |
| 83 | l3 = 1 - l1 - l2 |
| 84 | if min(l1, l2, l3) >= 0 and l1 * a[2] + l2 * b[2] + l3 * c[2] > p[2] + 1e-3: |
| 85 | return True |
| 86 | return False |
| 87 | |
| 88 | |
| 89 | def mid(p, q): |
| 90 | return tuple((a + b) / 2 for a, b in zip(p, q)) |
| 91 | |
| 92 | |
| 93 | def facing(i, j): |
| 94 | a, b, c, d = v(i, j), v(i + 1, j), v(i + 1, j + 1), v(i, j + 1) |
| 95 | return (c[0] - a[0]) * (d[1] - b[1]) - (c[1] - a[1]) * (d[0] - b[0]) > 0 |
| 96 | |
| 97 | |
| 98 | segs = {'f': [], 'faint': [], 'ink': []} |
| 99 | for i in RINGS: # cross-sections: edges (i, j)–(i, j+1) |
| 100 | segs['f'] += [(v(i, j), v(i, j + 1), i, j) for j in range(NT)] |
| 101 | for j in LINES: # constant θ: edges (i, j)–(i+1, j) |
| 102 | segs['faint'] += [(v(i, j), v(i + 1, j), i, j) for i in range(NP)] |
| 103 | F = [[facing(i, j) for j in range(NT)] for i in range(NP)] |
| 104 | for i in range(NP): # the outline: edges between a front and a back patch |
| 105 | for j in range(NT): |
| 106 | if F[i][j] != F[i][(j - 1) % NT]: |
| 107 | segs['ink'].append((v(i, j), v(i + 1, j), i, j)) |
| 108 | if F[i][j] != F[(i - 1) % NP][j]: |
| 109 | segs['ink'].append((v(i, j), v(i, j + 1), i, j)) |
| 110 | |
| 111 | xs = [p[0] for row in V for p in row] |
| 112 | ys = [p[1] for row in V for p in row] |
| 113 | sc = min(600 / (max(xs) - min(xs)), 250 / (max(ys) - min(ys))) |
| 114 | ox, oy = kit.W / 2 - sc * (max(xs) + min(xs)) / 2, 134 + sc * (max(ys) + min(ys)) / 2 |
| 115 | px = lambda p: (ox + sc * p[0], oy - sc * p[1]) |
| 116 | |
| 117 | body = [] |
| 118 | for cls, w in (('faint', 1.6), ('ink', 2.4), ('f', 4)): |
| 119 | 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])}' |
| 120 | for a, b, i, j in segs[cls] if not hidden(mid(a, b), i, j)) |
| 121 | body.append(kit.path(d, cls + '-s', w)) |
| 122 | kit.write(__file__, 'engineering-energy', ''.join(body), label='best boundary · max elongation ≈1.176 (circle 1)', |
| 123 | title='The best ConStellaration geometrical boundary (canyon-ant, score 0.98048), with its cross-sections') |
| 124 | print(f'scale {sc:.0f}, {sum(len(s) for s in segs.values())} segments') |