Skip to content

Commit

Permalink
Merge branch 'develop' into feature/pySTEL
Browse files Browse the repository at this point in the history
  • Loading branch information
lazersos committed Nov 18, 2024
2 parents 310321e + b1786a3 commit 8efa123
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 14 deletions.
18 changes: 18 additions & 0 deletions LIBSTELL/Sources/STELLOPT/stellopt_input_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ MODULE stellopt_input_mod
target_Jstar, sigma_Jstar, NumJstar,&
target_helicity, sigma_helicity, helicity,&
target_helicity_old, sigma_helicity_old, &
target_quasiiso, sigma_quasiiso, &
target_resjac, sigma_resjac, xm_resjac, xn_resjac,&
target_separatrix, sigma_separatrix, &
r_separatrix, z_separatrix, phi_separatrix, &
Expand Down Expand Up @@ -917,6 +918,8 @@ SUBROUTINE init_stellopt_input
helicity = CMPLX(0.0,0.0)
target_helicity_old(:) = 0.0
sigma_helicity_old(:) = bigno
target_quasiiso(:) = 0.0
sigma_quasiiso(:) = bigno
target_resjac(:) = 0.0
sigma_resjac(:) = bigno
xn_resjac(:) = 0
Expand Down Expand Up @@ -1095,6 +1098,7 @@ SUBROUTINE read_stellopt_input(filename, istat)
target_dkes(1) = 0.0; sigma_dkes(1) = bigno
target_dkes(2) = 0.0; sigma_dkes(2) = bigno
target_helicity(1) = 0.0; sigma_helicity(1) = bigno
target_quasiiso(1) = 0.0; sigma_quasiiso(1) = bigno
target_Jstar(1) = 0.0; sigma_Jstar(1) = bigno
target_dkes_Erdiff(1) = 0.0; sigma_dkes_Erdiff(1) = bigno
target_dkes_alpha(1) = 0.0; sigma_dkes_alpha(1) = bigno
Expand Down Expand Up @@ -1689,6 +1693,20 @@ SUBROUTINE write_optimum_namelist(iunit,istat)
'SIGMA_HELICITY_OLD(',ik,') = ',sigma_helicity_old(ik)
END DO
END IF
IF (ANY(sigma_quasiiso < bigno)) THEN
WRITE(iunit,'(A)') '!----------------------------------------------------------------------'
WRITE(iunit,'(A)') '! BOOZER QUASI-ISODYNAMIC METRIC'
WRITE(iunit,'(A)') '!----------------------------------------------------------------------'
n=0
DO ik = 1,UBOUND(sigma_quasiiso,DIM=1)
IF(sigma_quasiiso(ik) < bigno) n=ik
END DO
DO ik = 1, n
IF (sigma_quasiiso(ik) < bigno) WRITE(iunit,"(2(2X,A,I3.3,A,ES22.12E3))") &
'TARGET_QUASIISO(',ik,') = ',target_quasiiso(ik), &
'SIGMA_QUASIISO(',ik,') = ',sigma_quasiiso(ik)
END DO
END IF
IF (ANY(sigma_resjac < bigno)) THEN
WRITE(iunit,'(A)') '!----------------------------------------------------------------------'
WRITE(iunit,'(A)') '! BOOZER Resonant Modes'
Expand Down
4 changes: 4 additions & 0 deletions LIBSTELL/Sources/STELLOPT/stellopt_targets.f90
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ MODULE stellopt_targets
REAL(rprec), DIMENSION(nsd) :: target_magwell, sigma_magwell
REAL(rprec), DIMENSION(nsd) :: target_helicity, sigma_helicity
REAL(rprec), DIMENSION(nsd) :: target_helicity_old, sigma_helicity_old
REAL(rprec), DIMENSION(nsd) :: target_quasiiso, sigma_quasiiso
COMPLEX :: helicity
REAL(rprec), DIMENSION(nsd) :: target_resjac, sigma_resjac, &
xm_resjac, xn_resjac
Expand Down Expand Up @@ -273,6 +274,7 @@ MODULE stellopt_targets
INTEGER, PARAMETER :: jtarget_neo = 603
INTEGER, PARAMETER :: jtarget_Jstar = 604
INTEGER, PARAMETER :: jtarget_helicity = 605
INTEGER, PARAMETER :: jtarget_quasiiso = 6051
INTEGER, PARAMETER :: jtarget_resjac = 606
INTEGER, PARAMETER :: jtarget_txport = 607
INTEGER, PARAMETER :: jtarget_dkes = 608
Expand Down Expand Up @@ -417,6 +419,8 @@ SUBROUTINE write_targets(iunit,var_num)
WRITE(iunit, out_format) 'Trapped Particle J*'
CASE(jtarget_helicity)
WRITE(iunit, out_format) 'Boozer Spectrum Helicity'
CASE(jtarget_quasiiso)
WRITE(iunit, out_format) 'Boozer Quasi-isodynamic metric'
CASE(jtarget_txport)
WRITE(iunit, out_format) 'Turbulent Transport'
CASE(jtarget_orbit)
Expand Down
43 changes: 35 additions & 8 deletions SHARE/make_macports.inc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ endif
#######################################################################
# GENE Options
#######################################################################
LGENE = F
ifneq ("$(wildcard $(GENE_PATH))","")
LGENE = T
GENE_INC = -I$(GENE_PATH)
GENE_DIR = $(GENE_PATH)
LIB_GENE = libgene.a
Expand All @@ -113,66 +114,92 @@ endif
-L$(FFTWHOME)/lib -lfftw3 \
-L$(SLEPC_DIR)/$(PETSC_ARCH)/lib -lslepc \
-L$(PETSC_DIR)/$(PETSC_ARCH)/lib -lpetsc -lX11
else
LGENE = F
endif

#######################################################################
# COILOPT++ Options
#######################################################################
LCOILOPT = F
ifneq ("$(wildcard $(COILOPT_PATH))","")
LCOILOPT = T
COILOPT_INC = -I$(COILOPT_PATH)
COILOPTPP_DIR = $(COILOPT_PATH)
LIB_COILOPTPP = libcoilopt++.a
COILOPT_LIB = $(COILOPT_PATH)/$(LIB_COILOPTPP) \
-L$(GSLHOME)/lib -lgsl -lgslcblas -lstdc++ -lmpi_cxx
else
LCOILOPT = F
endif

#######################################################################
# TERPSICHORE Options
#######################################################################
LTERPSICHORE= F
ifneq ("$(wildcard $(TERPSICHORE_PATH))","")
LTERPSICHORE= T
TERPSICHORE_INC = -I$(TERPSICHORE_PATH)
TERPSICHORE_DIR = $(TERPSICHORE_PATH)
LIB_TERPSICHORE = libterpsichore.a
TERPSICHORE_LIB = $(TERPSICHORE_DIR)/$(LIB_TERPSICHORE)
else
LTERPSICHORE = F
endif

#######################################################################
# TRAVIS Options
#######################################################################
LTRAVIS= F
ifneq ("$(wildcard $(TRAVIS_PATH))","")
LTRAVIS = T
TRAVIS_DIR = $(TRAVIS_PATH)
LIB_TRAVIS = libtravis64_sopt.a
LIB_MCONF = libmconf64.a
LIB_FADDEEVA = libfaddeeva64.a
TRAVIS_LIB = $(TRAVIS_DIR)lib/$(LIB_TRAVIS) \
$(TRAVIS_DIR)faddeeva_package/lib/$(LIB_FADDEEVA) \
$(TRAVIS_DIR)magconf/lib/$(LIB_MCONF) -lc++
else
LTRAVIS = F
endif

#######################################################################
# REGCOIL Options
#######################################################################
LREGCOIL= F
ifneq ("$(wildcard $(REGCOIL_PATH))","")
LREGCOIL= T
REGCOIL_DIR = $(REGCOIL_PATH)
REGCOIL_INC = -I$(REGCOIL_DIR)
LIB_REGCOIL = libregcoil.a
REGCOIL_LIB = $(REGCOIL_DIR)/$(LIB_REGCOIL) -fopenmp
else
LREGCOIL = F
endif

#######################################################################
# SFINCS Options
#######################################################################

LSFINCS = F
ifneq ("$(wildcard $(SFINCS_PATH))","")
LSFINCS = T
SFINCS_DIR = $(SFINCS_PATH)
SFINCS_INC = -I$(SFINCS_DIR)
LIB_SFINCS = libsfincs.a
SFINCS_LIB = $(SFINCS_DIR)/$(LIB_SFINCS) \
-L$(PETSC_DIR)/$(PETSC_ARCH)/lib -lpetsc -lX11
else
LSFINCS = F
endif

#######################################################################
# Available Energy Options
#######################################################################
LAEOPT= F
ifneq ("$(wildcard $(AEOPT_PATH))","")
LAEOPT = T
AEOPT_DIR = $(AEOPT_PATH)
AEOPT_INC = -I$(AEOPT_DIR)
LIB_AEOPT = libtrapAE.a
AEOPT_LIB = $(AEOPT_PATH)/$(LIB_AEOPT)
else
LAEOPT = F
endif

#######################################################################
# LIBSTELL Shared Options
Expand Down
1 change: 1 addition & 0 deletions STELLOPTV2/ObjectList
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ObjectFiles = \
chisq_quasiiso.o \
stellopt_read_cws.o \
stellopt_write_header.o \
chisq_dkes_alpha.o \
Expand Down
8 changes: 8 additions & 0 deletions STELLOPTV2/STELLOPTV2.dep
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ chisq_helicity_ornl.o : \
equil_vals.o \
$(LIB_DIR)/$(LOCTYPE)/read_boozer_mod.o


chisq_quasiiso.o : \
stellopt_runtime.o \
$(LIB_DIR)/$(LOCTYPE)/stellopt_targets.o \
equil_utils.o \
$(LIB_DIR)/$(LOCTYPE)/read_boozer_mod.o \
$(LIB_DIR)/$(LOCTYPE)/vmec_input.o

chisq_resjac.o : \
stellopt_runtime.o \
$(LIB_DIR)/$(LOCTYPE)/stellopt_targets.o \
Expand Down
193 changes: 193 additions & 0 deletions STELLOPTV2/Sources/Chisq/chisq_quasiiso.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
!-----------------------------------------------------------------------
! Subroutine: chisq_quasiiso
! Authors: S. Lazerson ([email protected])
! Date: 10/08/2024
! Description: This chisquared functional replicates the QI
! functional described in:
! https://doi.org/10.1017/S002237782300065X
! and
! https://doi.org/10.1103/PRXEnergy.3.023010
!-----------------------------------------------------------------------
SUBROUTINE chisq_quasiiso(target,sigma,niter,iflag)
!-----------------------------------------------------------------------
! Libraries
!-----------------------------------------------------------------------
USE stellopt_runtime
USE stellopt_targets
USE equil_utils, ONLY: get_equil_iota
USE read_boozer_mod, ONLY: bmnc_b, ixn_b, ixm_b, mnboz_b, ns_b, nfp_b
USE vmec_input, ONLY: mpol, ntor
! USE stel_kinds, ONLY: rprec

!-----------------------------------------------------------------------
! Input/Output Variables
!
!-----------------------------------------------------------------------
IMPLICIT NONE
REAL(rprec), INTENT(in) :: target(nsd)
REAL(rprec), INTENT(in) :: sigma(nsd)
INTEGER, INTENT(in) :: niter
INTEGER, INTENT(inout) :: iflag

!-----------------------------------------------------------------------
! Local Variables
!
!-----------------------------------------------------------------------
INTEGER :: ik, mn, ier, i, l, m, lmin, lmax, i1, i2, lh, lr
INTEGER, PARAMETER :: nalpha = 65 ! must be odd
INTEGER, PARAMETER :: ntheta0 = 5
INTEGER, PARAMETER :: nlambda = 4
REAL(rprec) :: s_temp, iota0, phi, theta, deltaphi, ftemp, norm, &
Bmax, Bmin, Bmir, lambda, theta0, phimin,phimax
REAL(rprec), DIMENSION(:), ALLOCATABLE :: modb, modbs, dl, &
integral_A, integral_B
REAL(rprec), DIMENSION(:,:), ALLOCATABLE :: J_I, J_C

LOGICAL, PARAMETER :: lwrite_out = .False.


!-----------------------------------------------------------------------
! BEGIN SUBROUTINE
!-----------------------------------------------------------------------
IF (iflag < 0) RETURN
ik = COUNT(sigma < bigno)
IF (iflag == 1) WRITE(iunit_out,'(A,2(2X,I3.3))') 'QUASIISO ',ik,4
IF (iflag == 1) WRITE(iunit_out,'(A)') 'TARGET SIGMA VAL K'
IF (niter >= 0) THEN
! Allocate helpers along field line
ALLOCATE(modb(nalpha),modbs(nalpha),dl(nalpha),integral_A(nalpha),&
integral_B(nalpha))
! Allocate J arrays
ALLOCATE(J_C(nlambda,ntheta0),J_I(nlambda,ntheta0))
! Loop over surfaces
DO ik = 1, nsd
IF (ABS(sigma(ik)) >= bigno) CYCLE
! Get iota for the surface
s_temp = DBLE(ik)/DBLE(ns_b)
ier = 0
CALL get_equil_iota(s_temp,iota0,ier)
! Loop over inital values of theta0
J_C = 0.0; J_I = 0.0
DO i = 1, ntheta0
! Calculate the initial distance to follow a field line
deltaphi = pi2/iota0
modb = 0.0
modbs = 0.0
theta0 = pi2*(i-1)/ntheta0
DO l = 1, nalpha
phi = deltaphi*(l-1)/(nalpha-1)
theta = theta0 + iota0*phi
DO mn = 1, mnboz_b
modb(l) = modb(l)+bmnc_b(mn,ik)*COS(DBLE(ixm_b(mn))*theta+DBLE(ixn_b(mn))*phi/nfp_b)
END DO
END DO
IF (lwrite_out) WRITE(320,*) modb
! Find min/max modB values
lmin = MINLOC(modb,DIM=1)
lmax = MAXLOC(modb,DIM=1)
! Recalculate length of fieldline
phimin = deltaphi*(lmin-1)/(nalpha-1)
phimax = deltaphi*(lmax-1)/(nalpha-1)
deltaphi = ABS(phimax-phimin)
! Now recalculate modb centered around lmin
modb = 0
DO l = 1, nalpha
phi = phimin-deltaphi + 2*deltaphi*(l-1)/(nalpha-1)
!phi = deltaphi*(l-lmin-1)/(nalpha-1) - deltaphi/2
theta = theta0 + iota0*phi
DO mn = 1, mnboz_b
modb(l) = modb(l)+bmnc_b(mn,ik)*COS(DBLE(ixm_b(mn))*theta+DBLE(ixn_b(mn))*phi/nfp_b)
END DO
END DO
IF (lwrite_out) WRITE(321,*) modb
! Find the Bmin and half point
lh = FLOOR(nalpha*0.5)+1
lmin = MINLOC(modb,DIM=1)
lmin = MIN(MAX(lmin,2),nalpha-1)
! Now shift lmin to the middle
modb = CSHIFT(modb,lmin-lh) !BOOSH
! Now recalc lh as lmin
lmin = MINLOC(modb,DIM=1)
lmin = MIN(MAX(lmin,2),nalpha-1)
! Squash the array
modbs = modb
DO l = lmin, 1, -1
IF (modbs(l)<modbs(l+1)) modbs(l) = modbs(l+1)
END DO
DO l = lmin,nalpha
IF (modbs(l)<modbs(l-1)) modbs(l) = modbs(l-1)
END DO
IF (lwrite_out) WRITE(322,*) modbs
! Stretch the array
Bmin = MINVAL(modb)
Bmax = MAXVAL(modb)
DO l = 1, lmin
modbs(l) = Bmin + (Bmax-Bmin)*(modbs(l)-Bmin)/(modbs(1)-Bmin)
END DO
DO l = lmin, nalpha
modbs(l) = Bmin + (Bmax-Bmin)*(modbs(l)-Bmin)/(modbs(nalpha)-Bmin)
END DO
IF (lwrite_out) WRITE(323,*) modbs
!WRITE(330+i,*) modbs; CALL FLUSH(330+i)
! We need dl (missing B.grad(phi) term)
dl = modb*deltaphi/(nalpha-1)
!WRITE(340+i,*) dl; CALL FLUSH(340+i)
! Now we evaluate integrals for different values of lambda
! Alpha is enclosing loop
DO m = 1, nlambda
Bmir = Bmin+0.9*(Bmax-Bmin)*DBLE(m)/DBLE(nlambda)
lambda = 1.0/Bmir
i1 = COUNT(modbs(1:lmin)>Bmir) + 1
i2 = COUNT(modbs(lmin:nalpha)<Bmir) + lmin - 1
IF (lwrite_out) print *, nlambda,i1,i2,Bmin,Bmir,Bmax
i1 = MAX(i1,1); i2 = MIN(i2,nalpha)
integral_A = 1.0 - lambda * modb
integral_A = SIGN(1.0_rprec,integral_A)*SQRT(ABS(integral_A))*dl
IF (lwrite_out) WRITE(324,*) integral_A
integral_B = 1.0 - lambda * modbs
integral_B = SQRT(integral_B)*dl
IF (lwrite_out) WRITE(325,*) integral_B
IF (lwrite_out) PRINT *,'A :',integral_A(i1:i2)
IF (lwrite_out) PRINT *,'B :',integral_B(i1:i2)
J_I(m,i) = SUM(integral_A(i1:i2))
J_C(m,i) = SUM(integral_B(i1:i2))
END DO
END DO
IF (lwrite_out) WRITE(326,*) J_I
IF (lwrite_out) WRITE(327,*) J_C
! Construct fuctional value
ftemp = 0.0_rprec
DO m = 1, nlambda
DO i1 = 1, ntheta0
DO i2 = 1, ntheta0
ftemp = ftemp + J_I(m,i1) - J_C(m,i2)
END DO
END DO
END DO
norm = SUM(J_C+J_I)/(nlambda*ntheta0)
mtargets = mtargets + 1
targets(mtargets) = 0.0
sigmas(mtargets) = sigma(ik)
vals(mtargets) = ftemp/norm
IF (iflag == 1) WRITE(iunit_out,'(3ES22.12E3,2X,I3.3)') target(ik),sigma(ik),vals(mtargets),ik
END DO
DEALLOCATE(modb,modbs,dl,integral_A,integral_B,J_C,J_I)
ELSE
! Consistency check
mboz = MAX(6*mpol, 2, mboz)
nboz = MAX(2*ntor-1, 0, nboz)
! CALCULATE mnboz_b becasue we don't know it yet (setup_booz.f)
mnboz_b = (2*nboz+1)*(mboz-1) + (nboz + 1)
DO ik = 1, nsd
IF (ABS(sigma(ik)) < bigno) THEN
lbooz(ik) = .TRUE.
mtargets = mtargets + 1
IF (niter == -2) target_dex(mtargets) = jtarget_quasiiso
END IF
END DO
END IF
RETURN
!-----------------------------------------------------------------------
! END SUBROUTINE
!-----------------------------------------------------------------------
END SUBROUTINE chisq_quasiiso
Loading

0 comments on commit 8efa123

Please sign in to comment.