Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
41 changes: 26 additions & 15 deletions src/bnorml.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!

Expand All @@ -355,3 +341,28 @@ 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.
! 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
target = 1 + modulo(Nt-jj,Nt) + (Nz-kk)*Nt
field(target) = -field(source)
enddo
enddo
end subroutine mirror_normal_field
32 changes: 28 additions & 4 deletions src/casing.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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

!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!

Expand Down Expand Up @@ -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)
Expand All @@ -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

!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!

Expand Down
108 changes: 108 additions & 0 deletions tests/surfacecurrent_first.f90
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions tests/virtual_casing_symmetry.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
! 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)
! 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)
enddo
enddo
end program test_virtual_casing_symmetry
Loading