From cc783043d54ddb827734f5f84ed1091b078cdfa0 Mon Sep 17 00:00:00 2001 From: Christopher Albert Date: Fri, 23 Oct 2026 09:07:34 +0200 Subject: [PATCH 1/2] Fix plasma-side free-boundary initialization and periodic field mirroring --- CMakeLists.txt | 1 + src/CMakeLists.txt | 9 +++ src/bnorml.f90 | 33 ++++----- src/casing.f90 | 32 +++++++-- tests/surfacecurrent_first.f90 | 108 ++++++++++++++++++++++++++++++ tests/virtual_casing_symmetry.f90 | 28 ++++++++ 6 files changed, 192 insertions(+), 19 deletions(-) create mode 100644 tests/surfacecurrent_first.f90 create mode 100644 tests/virtual_casing_symmetry.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index d8ef131e..217e0e96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,6 +146,7 @@ set (CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/build/modules) #add_library(spec_lib "") #add_executable(xspec "") +include(CTest) add_subdirectory(src) if(SKBUILD) add_subdirectory("Utilities/python_wrapper") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 387457d8..e85705ee 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -298,3 +298,12 @@ target_link_libraries(xspec PUBLIC spec) set_target_properties(xspec PROPERTIES POSITION_INDEPENDENT_CODE ON) install(TARGETS xspec spec) + +if(BUILD_TESTING) + add_executable(spec_surfacecurrent_test ../tests/surfacecurrent_first.f90) + target_link_libraries(spec_surfacecurrent_test PRIVATE spec) + add_test(NAME surfacecurrent_first COMMAND spec_surfacecurrent_test) + add_executable(spec_virtual_casing_symmetry_test ../tests/virtual_casing_symmetry.f90) + target_link_libraries(spec_virtual_casing_symmetry_test PRIVATE spec) + add_test(NAME virtual_casing_symmetry COMMAND spec_virtual_casing_symmetry_test) +endif() diff --git a/src/bnorml.f90 b/src/bnorml.f90 index 4aaffbc1..3c2078b1 100644 --- a/src/bnorml.f90 +++ b/src/bnorml.f90 @@ -324,21 +324,7 @@ subroutine bnorml( mn, Ntz, efmn, ofmn ) !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-! ! In stellerator symmetric geometry, the virtual casing field also exhibits symmetriy which we can exploit if (YESstellsym) then - ! We are dealing with half open intervals in theta and phi [0, 2pi[ so we need to skip the first row and column when mirroring - if (Nz.eq.1) then - do jj = 0, Nt/2; - jk = 1 + jj - ijreal(Ntz + 1 - jk) = -ijreal(jk+1) - enddo - endif - - do kk = 0, Nzwithsym-2; - do jj = 0, Nt-1; - jk = 1 + jj + kk*Nt - - ijreal(Ntz + 1 - jk) = -ijreal(jk+Nt+1) - enddo - enddo + call mirror_normal_field(Nt, Nz, Nzwithsym, ijreal) endif !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-! @@ -355,3 +341,20 @@ subroutine bnorml( mn, Ntz, efmn, ofmn ) !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-! end subroutine bnorml + +! Complete an odd field under (theta,zeta) -> (-theta,-zeta). +! The theta=0 column maps to itself, without advancing the toroidal row. +subroutine mirror_normal_field(Nt, Nz, Nzwithsym, field) + implicit none + integer, intent(in) :: Nt, Nz, Nzwithsym + real, intent(inout) :: field(Nt*Nz) + integer :: jj, kk, source, target + + do kk = 1, Nz-Nzwithsym + do jj = 0, Nt-1 + source = 1 + jj + kk*Nt + target = 1 + modulo(Nt-jj,Nt) + (Nz-kk)*Nt + field(target) = -field(source) + enddo + enddo +end subroutine mirror_normal_field diff --git a/src/casing.f90 b/src/casing.f90 index fa9afb6f..580c0e74 100644 --- a/src/casing.f90 +++ b/src/casing.f90 @@ -355,7 +355,7 @@ subroutine surfacecurrent( teta, zeta, pxyz, jj) INTEGER :: ii, mi, ni, ll, ideriv REAL :: dR(0:3), dZ(0:3), gBut, gBuz, gtt, gtz, gzz, sqrtg, Blt, Blz, czeta, szeta, arg, carg, sarg - REAL :: XXt, XXz, YYt, YYz, ZZt, ZZz, ds + REAL :: XXt, XXz, YYt, YYz, ZZt, ZZz, ds, radial_factor !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-! @@ -468,9 +468,9 @@ subroutine surfacecurrent( teta, zeta, pxyz, jj) dZ(3) = dZ(3) - ni * ( - iZbc(ii,Nvol) * sarg + iZbs(ii,Nvol) * carg ) if (first_free_bound) then - do ll = 0, Lrad(Mvol) ! omit the possible current sheet due to a jump in tangential field at the plasma boundary; Zhu 20190603; - gBut = gBut - ( Aze(Mvol,ideriv,ii)%s(ll) * carg + Azo(Mvol,ideriv,ii)%s(ll) * sarg ) * TT(ll,1,1) ! contravariant; Jacobian comes later; - gBuz = gBuz + ( Ate(Mvol,ideriv,ii)%s(ll) * carg + Ato(Mvol,ideriv,ii)%s(ll) * sarg ) * TT(ll,1,1) + do ll = 0, Lrad(Nvol) ! use the plasma side, consistently with the symmetric branch; + gBut = gBut - ( Aze(Nvol,ideriv,ii)%s(ll) * carg + Azo(Nvol,ideriv,ii)%s(ll) * sarg ) * TT(ll,1,1) ! contravariant; Jacobian comes later; + gBuz = gBuz + ( Ate(Nvol,ideriv,ii)%s(ll) * carg + Ato(Nvol,ideriv,ii)%s(ll) * sarg ) * TT(ll,1,1) enddo else do ll = 0, Lrad(Mvol) @@ -484,6 +484,30 @@ subroutine surfacecurrent( teta, zeta, pxyz, jj) endif ! end of if( YESstellsym ) ; 08 Feb 16; end select ! end of select case( Igeometry ) ; 09 Mar 17; + + if (Igeometry.eq.3 .and. first_free_bound) then + ! The initial field comes from the plasma volume. Its contravariant + ! components must use that volume's radial map, not the vacuum map. + dR(1) = zero + dZ(1) = zero + do ii = 1, mn + mi = im(ii) + arg = mi * teta - in(ii) * zeta + carg = cos(arg) + sarg = sin(arg) + radial_factor = half + if (Nvol.eq.1) then + radial_factor = mi * half + if (mi.eq.0) radial_factor = one + endif + dR(1) = dR(1) + (iRbc(ii,Nvol)-iRbc(ii,Nvol-1)) * carg * radial_factor + dZ(1) = dZ(1) + (iZbs(ii,Nvol)-iZbs(ii,Nvol-1)) * sarg * radial_factor + if (.not.YESstellsym) then + dR(1) = dR(1) + (iRbs(ii,Nvol)-iRbs(ii,Nvol-1)) * sarg * radial_factor + dZ(1) = dZ(1) + (iZbc(ii,Nvol)-iZbc(ii,Nvol-1)) * carg * radial_factor + endif + enddo + endif !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-! diff --git a/tests/surfacecurrent_first.f90 b/tests/surfacecurrent_first.f90 new file mode 100644 index 00000000..32b9b02e --- /dev/null +++ b/tests/surfacecurrent_first.f90 @@ -0,0 +1,108 @@ +! An analytic helical coordinate map with different plasma/vacuum radial gaps. +! Check the actual surfacecurrent routine, including both Fourier parities. +program test_surfacecurrent_first + use constants, only: zero, one, half + use inputlist, only: Igeometry, Nvol, Lrad + use allglobal, only: Mvol, mn, im, in, YESstellsym, NOTstellsym, & + first_free_bound, iRbc, iZbs, iRbs, iZbc, Ate, Aze, Ato, Azo, TT + implicit none + integer :: sym, initial, v, j + real :: radius(0:4), eps, eta, theta, phi, q, c, s, dr, r + real :: rt, zt, rp, zp, rs, zs, jac, gu, gv, bt, bz + real :: et(3), ep(3), expected(3), got(3), position(3), expected_position(3) + + Igeometry = 3 + Nvol = 3 + Mvol = 4 + mn = 2 + Lrad = 2 + allocate(im(mn), in(mn), TT(0:2,0:1,0:3)) + allocate(iRbc(mn,0:Mvol), iZbs(mn,0:Mvol)) + allocate(iRbs(mn,0:Mvol), iZbc(mn,0:Mvol)) + allocate(Ate(Mvol,0:0,mn), Aze(Mvol,0:0,mn)) + allocate(Ato(Mvol,0:0,mn), Azo(Mvol,0:0,mn)) + do v = 1, Mvol + do j = 1, mn + allocate(Ate(v,0,j)%s(0:2), Aze(v,0,j)%s(0:2)) + allocate(Ato(v,0,j)%s(0:2), Azo(v,0,j)%s(0:2)) + Ate(v,0,j)%s = zero + Aze(v,0,j)%s = zero + Ato(v,0,j)%s = zero + Azo(v,0,j)%s = zero + enddo + enddo + im = [0, 1] + in = [0, 10] + TT = zero + TT(1,:,1) = one ! T_1'(s)=1 at both annular faces. + radius = [0.0, 0.2, 0.4, 0.7, 0.8] + Ate(Nvol,0,1)%s(1) = one + Aze(Nvol,0,1)%s(1) = -0.25 + Ate(Mvol,0,1)%s(1) = 4.0 + Aze(Mvol,0,1)%s(1) = -2.0 + Ato(Nvol,0,2)%s(0:1) = [-0.03, 0.03] + Azo(Nvol,0,2)%s(0:1) = [-0.02, 0.02] + Ato(Mvol,0,2)%s(0:1) = [0.4, 0.4] + Azo(Mvol,0,2)%s(0:1) = [0.5, 0.5] + theta = 0.37 + phi = 0.02 + q = theta - 10 * phi + c = cos(q) + s = sin(q) + do sym = 0, 1 + YESstellsym = sym.eq.1 + NOTstellsym = .not.YESstellsym + eps = zero + eta = zero + if (NOTstellsym) then + eps = 0.1 + eta = 0.04 + endif + iRbc = zero + iZbs = zero + iRbs = zero + iZbc = zero + iRbc(1,:) = 3.6 + iRbc(2,:) = radius + iZbs(2,:) = -radius + iRbs(2,:) = eps * radius + iZbc(2,:) = eta * radius + r = 3.6 + radius(Nvol) * (c + eps*s) + rt = radius(Nvol) * (-s + eps*c) + zt = radius(Nvol) * (-c - eta*s) + rp = -10 * rt + zp = -10 * zt + et = [rt*cos(phi), rt*sin(phi), zt] + ep = [rp*cos(phi)-r*sin(phi), rp*sin(phi)+r*cos(phi), zp] + expected_position = [r*cos(phi), r*sin(phi), radius(Nvol)*(-s+eta*c)] + do initial = 0, 1 + first_free_bound = initial.eq.1 + dr = half * (radius(Mvol)-radius(Nvol)) + gu = 2.0 + gv = 4.0 + if (first_free_bound) then + dr = half * (radius(Nvol)-radius(Nvol-1)) + gu = 0.25 + gv = one + endif + if (NOTstellsym) then + if (first_free_bound) then + gu = gu - 0.02*s + gv = gv + 0.03*s + else + gu = gu - 0.5*s + gv = gv + 0.4*s + endif + endif + rs = dr * (c + eps*s) + zs = dr * (-s + eta*c) + jac = r * (zs*rt-rs*zt) + bt = (gu*dot_product(et,et)+gv*dot_product(et,ep))/jac + bz = (gu*dot_product(et,ep)+gv*dot_product(ep,ep))/jac + expected = bz*et-bt*ep + call surfacecurrent(theta, phi, position, got) + if (maxval(abs(got-expected)).gt.1.e-11) error stop 'surface current mismatch' + if (maxval(abs(position-expected_position)).gt.1.e-13) error stop 'position mismatch' + enddo + enddo +end program test_surfacecurrent_first diff --git a/tests/virtual_casing_symmetry.f90 b/tests/virtual_casing_symmetry.f90 new file mode 100644 index 00000000..58d9c5f1 --- /dev/null +++ b/tests/virtual_casing_symmetry.f90 @@ -0,0 +1,28 @@ +! An independently prescribed Fourier field on even and odd periodic grids. +program test_virtual_casing_symmetry + use constants, only: pi2 + implicit none + integer :: Nt, Nz, Nzwithsym, jj, kk, index + real :: theta, zeta + real, allocatable :: expected(:), field(:) + + do Nt = 7, 8 + do Nz = 1, 8 + allocate(expected(Nt*Nz), field(Nt*Nz)) + do kk = 0, Nz-1 + zeta = pi2*kk/Nz + do jj = 0, Nt-1 + theta = pi2*jj/Nt + index = 1 + jj + kk*Nt + expected(index) = sin(theta)*cos(zeta) + 0.3*cos(2*theta)*sin(zeta) + enddo + enddo + Nzwithsym = min(Nz, (Nz+3)/2) + field = 0.0 + field(1:Nt*Nzwithsym) = expected(1:Nt*Nzwithsym) + call mirror_normal_field(Nt, Nz, Nzwithsym, field) + if (maxval(abs(field-expected)).gt.2.e-14) error stop 'periodic symmetry mismatch' + deallocate(expected, field) + enddo + enddo +end program test_virtual_casing_symmetry From 5e8d3e4fe9432de05a0356581fca1c1ac3d882da Mon Sep 17 00:00:00 2001 From: Christopher Albert Date: Sun, 25 Oct 2026 21:32:10 +0100 Subject: [PATCH 2/2] Restore axisymmetric (Nz=1) completion of the virtual-casing normal field For Nz=1, bnorml evaluates only theta indices 0..(Nt+1)/2 and relies on the stellarator-symmetry mirror to fill the rest of the row. Fork PR #1 (d390088e) replaced the mirror by mirror_normal_field, whose toroidal loop is empty for Nz=1, so the upper half of the row kept stale values. This changed the free-boundary regression cases G3V08L3Fr and G3V08L3FrGrid (Ntor=0), e.g. Rij average difference 0.40 at tol 1e-10. mirror_normal_field now fills theta index jj > (Nt+1)/2 with -field(Nt-jj) for Nz=1. Unlike the upstream branch, it does not negate the computed theta=pi sample. The symmetry unit test now poisons exactly the samples bnorml does not evaluate, which fails without this change. Oracle independent of the references: a build that evaluates every grid point by brute force (Nzwithsym=Nz, no Nz=1 skip, no mirroring) reproduces the existing G3V08L3Fr and G3V08L3FrGrid references within 1e-10, as does this fix, so the references stay unchanged. All 8 CI reference cases pass with -std=legacy. --- src/bnorml.f90 | 8 ++++++++ tests/virtual_casing_symmetry.f90 | 9 +++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/bnorml.f90 b/src/bnorml.f90 index 3c2078b1..7c8a352c 100644 --- a/src/bnorml.f90 +++ b/src/bnorml.f90 @@ -344,12 +344,20 @@ end subroutine bnorml ! Complete an odd field under (theta,zeta) -> (-theta,-zeta). ! The theta=0 column maps to itself, without advancing the toroidal row. +! For Nz=1, bnorml evaluates only theta indices 0..(Nt+1)/2 of the single row. subroutine mirror_normal_field(Nt, Nz, Nzwithsym, field) implicit none integer, intent(in) :: Nt, Nz, Nzwithsym real, intent(inout) :: field(Nt*Nz) integer :: jj, kk, source, target + if (Nz.eq.1) then + do jj = (Nt+1)/2 + 1, Nt-1 + field(1+jj) = -field(1+Nt-jj) + enddo + return + endif + do kk = 1, Nz-Nzwithsym do jj = 0, Nt-1 source = 1 + jj + kk*Nt diff --git a/tests/virtual_casing_symmetry.f90 b/tests/virtual_casing_symmetry.f90 index 58d9c5f1..9b264e97 100644 --- a/tests/virtual_casing_symmetry.f90 +++ b/tests/virtual_casing_symmetry.f90 @@ -18,8 +18,13 @@ program test_virtual_casing_symmetry enddo enddo Nzwithsym = min(Nz, (Nz+3)/2) - field = 0.0 - field(1:Nt*Nzwithsym) = expected(1:Nt*Nzwithsym) + ! Poison every sample that bnorml does not evaluate. + field = huge(1.0) + if (Nz.eq.1) then + field(1:(Nt+1)/2+1) = expected(1:(Nt+1)/2+1) + else + field(1:Nt*Nzwithsym) = expected(1:Nt*Nzwithsym) + endif call mirror_normal_field(Nt, Nz, Nzwithsym, field) if (maxval(abs(field-expected)).gt.2.e-14) error stop 'periodic symmetry mismatch' deallocate(expected, field)