Skip to content

Commit

Permalink
Updated a couple tests to account for filtering d
Browse files Browse the repository at this point in the history
  • Loading branch information
landreman committed Jun 14, 2019
1 parent 76c7795 commit f8ec988
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 44 deletions.
Binary file not shown.
Binary file modified examples/NCSX_vv_Picard/regcoil_out.NCSX_vv_Picard.reference.nc
Binary file not shown.
12 changes: 9 additions & 3 deletions regcoilPlot
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import numpy as np
from scipy.io import netcdf
from scipy.interpolate import interp1d
import sys
import math
import os

if len(sys.argv) != 2:
print "Error! You must specify 1 argument: the regcoil_out.XXX.nc file."
exit(1)


f = netcdf.netcdf_file(sys.argv[1],'r',mmap=False)
filename = sys.argv[1]
f = netcdf.netcdf_file(filename,'r',mmap=False)
nfp = f.variables['nfp'][()]
ntheta_plasma = f.variables['ntheta_plasma'][()]
ntheta_coil = f.variables['ntheta_coil'][()]
Expand Down Expand Up @@ -178,6 +178,7 @@ for whichPlot in range(4):

plt.tight_layout()

plt.figtext(0.5,0.00,os.path.abspath(filename),ha='center',va='bottom',fontsize=7)

########################################################
# Pick the isaved values to show in the 2D plots
Expand Down Expand Up @@ -249,6 +250,8 @@ plt.tight_layout()
plt.subplots_adjust(top=0.95)
plt.figtext(0.5,0.99,"Blue dots indicate the points that are plotted in later figures",horizontalalignment='center',verticalalignment='top',fontsize='small')

plt.figtext(0.5,0.00,os.path.abspath(filename),ha='center',va='bottom',fontsize=7)

########################################################
# Prepare for future plots
########################################################
Expand Down Expand Up @@ -312,6 +315,7 @@ for js in range(num_abs_M_figures):
plt.tight_layout()
plt.subplots_adjust(top=0.94)
plt.figtext(0.5,0.99,"Magnitude of the magnetization (|M|, [Amperes / meter])"+title_string,horizontalalignment='center',verticalalignment='top',fontsize='small')
plt.figtext(0.5,0.00,os.path.abspath(filename),ha='center',va='bottom',fontsize=7)

########################################################
# Plot d
Expand Down Expand Up @@ -348,6 +352,7 @@ for whichPlot in range(numPlots):
plt.tight_layout()
plt.subplots_adjust(top=0.94)
plt.figtext(0.5,0.99,"Thickness d of the magnetization region [meters].",horizontalalignment='center',verticalalignment='top',fontsize='small')
plt.figtext(0.5,0.00,os.path.abspath(filename),ha='center',va='bottom',fontsize=7)

########################################################
# Plot Bnormal
Expand Down Expand Up @@ -384,6 +389,7 @@ plt.tight_layout()
plt.subplots_adjust(top=0.94)
plt.figtext(0.5,0.99,"Bnormal [Tesla]",horizontalalignment='center',verticalalignment='top',fontsize='small')

plt.figtext(0.5,0.00,os.path.abspath(filename),ha='center',va='bottom',fontsize=7)


########################################################
Expand Down
31 changes: 18 additions & 13 deletions regcoil_init_basis_functions.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ subroutine regcoil_init_basis_functions()
implicit none

integer :: itheta_coil, izeta_coil
integer :: index_coil, imn, iflag
integer :: index_coil, imn, iflag, offset
integer :: tic, toc, countrate
real(dp) :: angle, sinangle, cosangle

Expand All @@ -25,13 +25,14 @@ subroutine regcoil_init_basis_functions()

select case (symmetry_option)
case (1,2)
num_basis_functions = mnmax_magnetization + 1
num_basis_functions = mnmax_magnetization
case (3)
num_basis_functions = mnmax_magnetization * 2 + 1
num_basis_functions = mnmax_magnetization * 2
case default
print *,"Error! Invalid setting for symmetry_option:",symmetry_option
stop
end select
if (include_constant_basis_function) num_basis_functions = num_basis_functions + 1

system_size = 3 * ns_magnetization * num_basis_functions

Expand All @@ -43,10 +44,14 @@ subroutine regcoil_init_basis_functions()
allocate(basis_functions_zeta_Z(ntheta_coil*nzeta_coil, num_basis_functions),stat=iflag)
if (iflag .ne. 0) stop 'regcoil_build_matrices Allocation error 1!'

! First basis function is the constant function:
! (For stellarator symmetry, this basis function always has amplitude=0 for the zeta and Z components of M, but we include it in the code anyway just so the zeta and Z blocks have the same size as the R block.)
basis_functions_R(:, 1) = 1
basis_functions_zeta_Z(:, 1) = 1
offset = 0
if (include_constant_basis_function) then
! First basis function is the constant function:
! (For stellarator symmetry, this basis function always has amplitude=0 for the zeta and Z components of M, but we include it in the code anyway just so the zeta and Z blocks have the same size as the R block.)
basis_functions_R(:, 1) = 1
basis_functions_zeta_Z(:, 1) = 1
offset = 1
end if

! Remaining basis functions:
! These loops could be made faster
Expand All @@ -62,8 +67,8 @@ subroutine regcoil_init_basis_functions()
angle = xm_magnetization(imn)*theta_coil(itheta_coil)-xn_magnetization(imn)*zeta_coil(izeta_coil)
sinangle = sin(angle)
cosangle = cos(angle)
basis_functions_R(index_coil, imn+1) = sinangle
basis_functions_zeta_Z(index_coil, imn+1) = cosangle
basis_functions_R(index_coil, imn + offset) = sinangle
basis_functions_zeta_Z(index_coil, imn + offset) = cosangle
end do
end do
end do
Expand All @@ -78,10 +83,10 @@ subroutine regcoil_init_basis_functions()
angle = xm_magnetization(imn)*theta_coil(itheta_coil)-xn_magnetization(imn)*zeta_coil(izeta_coil)
sinangle = sin(angle)
cosangle = cos(angle)
basis_functions_R(index_coil, imn + 1) = sinangle
basis_functions_zeta_Z(index_coil, imn + 1) = sinangle
basis_functions_R(index_coil, imn + 1 + mnmax_magnetization) = cosangle
basis_functions_zeta_Z(index_coil, imn + 1 + mnmax_magnetization) = cosangle
basis_functions_R(index_coil, imn + offset) = sinangle
basis_functions_zeta_Z(index_coil, imn + offset) = sinangle
basis_functions_R(index_coil, imn + offset + mnmax_magnetization) = cosangle
basis_functions_zeta_Z(index_coil, imn + offset + mnmax_magnetization) = cosangle
end do
end do
end do
Expand Down
56 changes: 29 additions & 27 deletions regcoil_update_d.f90
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,47 @@ subroutine regcoil_update_d(jd,isaved)
end if

! Fourier-filter d. First we transform from real to Fourier space:
if (filter_d) then
!!$ print *,"d before filtering:"
!!$ do j=1,ntheta_coil
!!$ print "(*(f7.4))",d(j,:)
!!$ end do
d0 = sum(d)/(ntheta_coil*nzeta_coil)
dmnc=0
dmns=0
factor = (2.0d+0) / (ntheta_coil * nzeta_coil)
do izeta = 1, nzeta_coil
do itheta = 1, ntheta_coil
do j = 1, mnmax_magnetization
angle = xm_magnetization(j) * theta_coil(itheta) - xn_magnetization(j) * zeta_coil(izeta)
sinangle = sin(angle)
cosangle = cos(angle)
factor2 = factor
! The next 2 lines ensure inverse Fourier transform(Fourier transform) = identity
if (mod(ntheta_coil,2) == 0 .and. xm_magnetization(j) == (ntheta_coil/2)) factor2 = factor2 / 2
if (mod( nzeta_coil,2) == 0 .and. abs(xn_magnetization(j)) == nfp*(nzeta_coil/2)) factor2 = factor2 / 2
dmnc(j) = dmnc(j) + d(itheta, izeta) * cosangle * factor2
dmns(j) = dmns(j) + d(itheta, izeta) * sinangle * factor2
d0 = sum(d)/(ntheta_coil*nzeta_coil)
dmnc=0
dmns=0
factor = (2.0d+0) / (ntheta_coil * nzeta_coil)
do izeta = 1, nzeta_coil
do itheta = 1, ntheta_coil
do j = 1, mnmax_magnetization
angle = xm_magnetization(j) * theta_coil(itheta) - xn_magnetization(j) * zeta_coil(izeta)
sinangle = sin(angle)
cosangle = cos(angle)
factor2 = factor
! The next 2 lines ensure inverse Fourier transform(Fourier transform) = identity
if (mod(ntheta_coil,2) == 0 .and. xm_magnetization(j) == (ntheta_coil/2)) factor2 = factor2 / 2
if (mod( nzeta_coil,2) == 0 .and. abs(xn_magnetization(j)) == nfp*(nzeta_coil/2)) factor2 = factor2 / 2
dmnc(j) = dmnc(j) + d(itheta, izeta) * cosangle * factor2
dmns(j) = dmns(j) + d(itheta, izeta) * sinangle * factor2
end do
end do
end do
end do
! Now inverse transform:
d = d0
do izeta = 1, nzeta_coil
do itheta = 1, ntheta_coil
do j = 1, mnmax_magnetization
angle = xm_magnetization(j)*theta_coil(itheta)-xn_magnetization(j)*zeta_coil(izeta)
sinangle = sin(angle)
cosangle = cos(angle)
d(itheta,izeta) = d(itheta,izeta) + dmnc(j)*cosangle + dmns(j)*sinangle
! Now inverse transform:
d = d0
do izeta = 1, nzeta_coil
do itheta = 1, ntheta_coil
do j = 1, mnmax_magnetization
angle = xm_magnetization(j)*theta_coil(itheta)-xn_magnetization(j)*zeta_coil(izeta)
sinangle = sin(angle)
cosangle = cos(angle)
d(itheta,izeta) = d(itheta,izeta) + dmnc(j)*cosangle + dmns(j)*sinangle
end do
end do
end do
end do
!!$ print *,"d after filtering:"
!!$ do j=1,ntheta_coil
!!$ print "(*(f7.4))",d(j,:)
!!$ end do
end if

! Take a mixture of the new and old depths:
d = Picard_alpha * d + (1 - Picard_alpha) * last_d
Expand Down
5 changes: 4 additions & 1 deletion regcoil_variables.f90
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ module regcoil_variables
integer :: regularization_d_exponent = 1
real(dp) :: d0
real(dp), dimension(:), allocatable :: dmnc, dmns
logical :: filter_d = .true.
logical :: include_constant_basis_function = .true.

integer, parameter :: max_nports = 10
integer :: nports
Expand All @@ -184,7 +186,8 @@ module regcoil_variables
d_option, nd, target_mu0_M, Anderson_depth, Anderson_alpha, Picard_alpha, min_d, &
write_mgrid, mgrid_ir, mgrid_jz, mgrid_kp, mgrid_rmin, mgrid_rmax, mgrid_zmin, mgrid_zmax, &
include_bnormal_from_TF, net_poloidal_current_Amperes, regularization_d_exponent, &
ports_theta0, ports_zeta0, ports_theta_width, ports_zeta_width, ports_sharpness, ports_magnitude
ports_theta0, ports_zeta0, ports_theta_width, ports_zeta_width, ports_sharpness, ports_magnitude, &
filter_d, include_constant_basis_function

end module regcoil_variables

0 comments on commit f8ec988

Please sign in to comment.