diff --git a/dcon/bal.f b/dcon/bal.f index f31b6f031..00f833ffd 100644 --- a/dcon/bal.f +++ b/dcon/bal.f @@ -1,540 +1,440 @@ -c----------------------------------------------------------------------- -c file bal.f. -c determines high-n ideal marginal MHD ballooning stability. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c code organization. -c----------------------------------------------------------------------- -c 0. bal_mod. -c 1. bal_scan. -c 2. bal_prep. -c 3. bal_der. -c 4. bal_int. -c 5. bal_pse. -c----------------------------------------------------------------------- -c subprogram 0. bal_mod. -c module declarations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - MODULE bal_mod - USE dcon_mod - IMPLICIT NONE - - INTEGER, PRIVATE :: ipsi - REAL(r8), PRIVATE :: di,alpha,theta0=0 - REAL(r8), DIMENSION(2,2), PRIVATE :: v0 - TYPE(spline_type), PRIVATE :: bf,bg - - CONTAINS -c----------------------------------------------------------------------- -c subprogram 1. bal_scan. -c computes ballooning stability criterion. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_scan -c----------------------------------------------------------------------- -c open output files. -c----------------------------------------------------------------------- - IF(out_bal1)CALL ascii_open(out_bal1_unit,"bal1.out","UNKNOWN") - IF(out_bal2)CALL ascii_open(out_bal2_unit,"bal2.out","UNKNOWN") - IF(bin_bal1)CALL bin_open(bin_bal1_unit,"bal1.bin","UNKNOWN", - $ "REWIND","none") - IF(bin_bal2)CALL bin_open(bin_bal2_unit,"bal2.bin","UNKNOWN", - $ "REWIND","none") -c----------------------------------------------------------------------- -c test ballooning stability. -c----------------------------------------------------------------------- - DO ipsi=0,mpsi - CALL bal_prep - IF(di <= 0 .AND. di >= -1e4 .AND. sq%xs(ipsi) <= 1)CALL bal_int - CALL spline_dealloc(bf) - CALL spline_dealloc(bg) - ENDDO -c----------------------------------------------------------------------- -c close output files. -c----------------------------------------------------------------------- - IF(out_bal1)CALL ascii_close(out_bal1_unit) - IF(out_bal2)CALL ascii_close(out_bal2_unit) - IF(bin_bal1)CALL bin_close(bin_bal1_unit) - IF(bin_bal2)CALL bin_close(bin_bal2_unit) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_scan -c----------------------------------------------------------------------- -c subprogram 2. bal_prep. -c computes coefficients for the ideal marginal ballooning equations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_prep - - LOGICAL, PARAMETER :: inverse_flag=.FALSE. - INTEGER :: itheta,info - INTEGER, DIMENSION(3) :: ipiv - REAL(r8) :: chi1,det,eta,fexp,p1,psifac,q,q1,r,rfac,theta, - $ twopif,v1 - REAL(r8), DIMENSION(2,2) :: c0,d0bar,d,v10 - REAL(r8), DIMENSION(2,2,0:mtheta) :: d1 - REAL(r8), DIMENSION(0:mtheta) :: b1,bsq,dbdb0,dbdb1,dbdb2, - $ kappan,kappas,jac,jacfac - REAL(r8), DIMENSION(3,3) :: v,w,temp - TYPE(spline_type) :: spl0,spl1,spl2,spl3 -c----------------------------------------------------------------------- -c compute surface quantities. -c----------------------------------------------------------------------- - psifac=sq%xs(ipsi) - twopif=sq%fs(ipsi,1) - p1=sq%fs1(ipsi,2) - v1=sq%fs(ipsi,3) - q=sq%fs(ipsi,4) - q1=sq%fs1(ipsi,4) - chi1=twopi*psio - v=0 - w=0 -c----------------------------------------------------------------------- -c compute coordinates and jacobian. -c----------------------------------------------------------------------- - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - theta=rzphi%ys(itheta) - rfac=SQRT(rzphi%f(1)) - eta=twopi*(theta+rzphi%f(2)) - r=ro+rfac*COS(eta) - jac(itheta)=rzphi%f(4) -c----------------------------------------------------------------------- -c compute contravariant basis vectors. -c----------------------------------------------------------------------- - v(1,1)=rzphi%fx(1)/(2*rfac*jac(itheta)) - v(1,2)=rzphi%fx(2)*twopi*rfac/jac(itheta) - v(1,3)=rzphi%fx(3)*r/jac(itheta) - v(2,1)=rzphi%fy(1)/(2*rfac*jac(itheta)) - v(2,2)=(1+rzphi%fy(2))*twopi*rfac/jac(itheta) - v(2,3)=rzphi%fy(3)*r/jac(itheta) - v(3,3)=twopi*r/jac(itheta) -c----------------------------------------------------------------------- -c compute covariant basis vectors, inverse method. -c----------------------------------------------------------------------- - IF(inverse_flag)THEN - temp=TRANSPOSE(v)*jac(itheta) - w=RESHAPE((/1,0,0,0,1,0,0,0,1/),(/3,3/)) - CALL dgetrf(3,3,temp,3,ipiv,info) - CALL dgetrs('N',3,3,temp,3,ipiv,w,3,info) -c----------------------------------------------------------------------- -c compute covariant basis vectors, direct method. -c----------------------------------------------------------------------- - ELSE - w(1,1)=(1+rzphi%fy(2))*twopi**2*rfac*r - $ /jac(itheta) - w(1,2)=-rzphi%fy(1)*pi*r/(rfac*jac(itheta)) - w(2,1)=-rzphi%fx(2)*twopi**2*r*rfac/jac(itheta) - w(2,2)=rzphi%fx(1)*pi*r/(rfac*jac(itheta)) - w(3,1)=(rzphi%fx(2)*rzphi%fy(3) - $ -rzphi%fx(3)*(1+rzphi%fy(2))) - $ *twopi*r*rfac/jac(itheta) - w(3,2)=(rzphi%fx(3)*rzphi%fy(1) - $ -rzphi%fx(1)*rzphi%fy(3)) - $ *r/(2*rfac*jac(itheta)) - w(3,3)=1/(twopi*r) - ENDIF -c----------------------------------------------------------------------- -c store physical quantities. -c----------------------------------------------------------------------- - b1(itheta)=SUM((v(2,:)+q*v(3,:))*v(1,:))*chi1 - bsq(itheta)=SUM((v(2,:)+q*v(3,:))**2)*chi1**2 - dbdb0(itheta)=SUM(w(1,:)**2)*q1**2 - dbdb1(itheta)=-SUM((w(3,:)-q*w(2,:))*w(1,:))*2*q1 - dbdb2(itheta)=SUM((w(3,:)-q*w(2,:))**2) - ENDDO -c----------------------------------------------------------------------- -c compute curvature terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl0,mtheta,2) - spl0%xs=rzphi%ys - spl0%fs(:,1)=1/bsq - spl0%fs(:,2)=jac*b1/bsq - CALL spline_fit(spl0,"periodic") - kappas=-spl0%fs1(:,1)*twopif/(2*jac) - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - kappan(itheta)=((p1/bsq(itheta)-rzphi%fx(4)/jac(itheta))/chi1 - $ +twopif*q1/(bsq(itheta)*jac(itheta))+spl0%fs1(itheta,2) - $ /jac(itheta))/2 - ENDDO - CALL spline_dealloc(spl0) -c----------------------------------------------------------------------- -c compute coefficients for ballooning equation. -c----------------------------------------------------------------------- - CALL spline_alloc(bf,mtheta,5) - bf%xs=rzphi%ys - jacfac=jac/chi1 - bf%fs(:,1)=dbdb0/(bsq*jacfac) - bf%fs(:,2)=dbdb1/(2*dbdb0) - bf%fs(:,3)=(dbdb2-dbdb1**2/(4*dbdb0))/(bsq*jacfac) - bf%fs(:,4)=2*kappan*p1/chi1*jacfac - bf%fs(:,5)=-2*kappas*p1/chi1*q1*jacfac - CALL spline_fit(bf,"periodic") -c----------------------------------------------------------------------- -c diagnose bf. -c----------------------------------------------------------------------- - bf%title=(/"theta ","dbdb0 ","dbdb1 ","dbdb2 ","kappas","kappan"/) - CALL spline_write1(bf,out_bal1,bin_bal1, - $ out_bal1_unit,bin_bal1_unit,.TRUE.) -c----------------------------------------------------------------------- -c initialize bg. -c----------------------------------------------------------------------- - CALL spline_alloc(spl0,mtheta,1) - spl0%xs=rzphi%ys - spl0%fs(:,1)=-p1*q1*twopif/(bsq*chi1**2) - CALL spline_fit(spl0,"periodic") - CALL spline_int(spl0) - CALL spline_alloc(bg,mtheta,5) - bg%xs=rzphi%ys - bg%fs(:,5)=spl0%fs(:,1)-spl0%fsi(mtheta,1) - CALL spline_dealloc(spl0) -c----------------------------------------------------------------------- -c compute c0. -c----------------------------------------------------------------------- - CALL spline_alloc(spl1,mtheta,4) - spl1%xs=rzphi%ys - DO itheta=0,mtheta - c0(1,1)=.5 - c0(1,2)=1/bf%fs(itheta,1) - c0(2,1)=-bf%fs(itheta,4) - c0(2,2)=-.5 -c----------------------------------------------------------------------- -c compute d0. -c----------------------------------------------------------------------- - spl1%fs(itheta,1)=c0(1,1)+c0(1,2)*bg%fs(itheta,5) - spl1%fs(itheta,2)=c0(1,2) - spl1%fs(itheta,3)=c0(2,1) - $ +(c0(2,2)-c0(1,1)-c0(1,2)*bg%fs(itheta,5))*bg%fs(itheta,5) - spl1%fs(itheta,4)=-spl1%fs(itheta,1) -c----------------------------------------------------------------------- -c compute d1. -c----------------------------------------------------------------------- - fexp=-c0(1,2)*bf%fs(itheta,2)*2 - d1(1,1,itheta)=fexp*bg%fs(itheta,5) - d1(1,2,itheta)=fexp - d1(2,1,itheta)=-fexp*bg%fs(itheta,5)**2 - d1(2,2,itheta)=-d1(1,1,itheta) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic splines and write output. -c----------------------------------------------------------------------- - CALL spline_fit(spl1,"periodic") - CALL spline_int(spl1) - d0bar(1,1)=spl1%fsi(mtheta,1) - d0bar(1,2)=spl1%fsi(mtheta,2) - d0bar(2,1)=spl1%fsi(mtheta,3) - d0bar(2,2)=spl1%fsi(mtheta,4) -c----------------------------------------------------------------------- -c compute mercier criterion and alpha. -c----------------------------------------------------------------------- - di=d0bar(1,1)*d0bar(2,2)-d0bar(1,2)*d0bar(2,1) - IF(di > 0)THEN - CALL spline_dealloc(spl1) - RETURN - ENDIF - alpha=SQRT(-di) -c----------------------------------------------------------------------- -c compute zeroth-order eigenfunctions. -c----------------------------------------------------------------------- - v0(1,1)=1 - v0(1,2)=1 - v0(2,1)=-(d0bar(1,1)-alpha)/d0bar(1,2) - v0(2,2)=-(d0bar(1,1)+alpha)/d0bar(1,2) -c----------------------------------------------------------------------- -c store derivatives for first-order terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl2,mtheta,4) - spl2%xs=rzphi%ys - DO itheta=0,mtheta - spl2%fs(itheta,1) - $ =(spl1%fs(itheta,1)-alpha)*v0(1,1) - $ +spl1%fs(itheta,2)*v0(2,1) - spl2%fs(itheta,2) - $ =spl1%fs(itheta,3)*v0(1,1) - $ +(spl1%fs(itheta,4)-alpha)*v0(2,1) - spl2%fs(itheta,3) - $ =(spl1%fs(itheta,1)+alpha)*v0(1,2) - $ +spl1%fs(itheta,2)*v0(2,2) - spl2%fs(itheta,4) - $ =spl1%fs(itheta,3)*v0(1,2) - $ +(spl1%fs(itheta,4)+alpha)*v0(2,2) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic spines and integrate. -c----------------------------------------------------------------------- - CALL spline_fit(spl2,"periodic") - CALL spline_int(spl2) - spl2%fs=spl2%fsi -c----------------------------------------------------------------------- -c store derivatives for second-order terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl3,mtheta,4) - spl3%xs=rzphi%ys - DO itheta=0,mtheta - spl3%fs(itheta,1) - $ =(spl1%fs1(itheta,1)+1-alpha)*spl2%fs(itheta,1) - $ +spl1%fs(itheta,2)*spl2%fs(itheta,2) - $ +d1(1,1,itheta)*v0(1,1)+d1(1,2,itheta)*v0(2,1) - spl3%fs(itheta,2) - $ =spl1%fs(itheta,3)*spl2%fs(itheta,1) - $ +(spl1%fs(itheta,4)+1-alpha)*spl2%fs(itheta,2) - $ +d1(2,1,itheta)*v0(1,1)+d1(2,2,itheta)*v0(2,1) - spl3%fs(itheta,3) - $ =(spl1%fs(itheta,1)+1+alpha)*spl2%fs(itheta,3) - $ +spl1%fs(itheta,2)*spl2%fs(itheta,4) - $ +d1(1,1,itheta)*v0(1,2)+d1(1,2,itheta)*v0(2,2) - spl3%fs(itheta,4) - $ =spl1%fs(itheta,3)*spl2%fs(itheta,3) - $ +(spl1%fs(itheta,4)+1+alpha)*spl2%fs(itheta,4) - $ +d1(2,1,itheta)*v0(1,2)+d1(2,2,itheta)*v0(2,2) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic spines and integrate. -c----------------------------------------------------------------------- - CALL spline_fit(spl3,"periodic") - CALL spline_int(spl3) -c----------------------------------------------------------------------- -c compute first order constants, first eigenfunction. -c----------------------------------------------------------------------- - d(2,1)=d0bar(2,1) - d(1,2)=d0bar(1,2) - d(1,1)=d0bar(1,1)+1-alpha - d(2,2)=d0bar(2,2)+1-alpha - det=d(1,1)*d(2,2)-d(1,2)*d(2,1) - v10(1,1)=(d(1,2)*spl3%fsi(mtheta,2)-d(2,2)*spl3%fsi(mtheta,1))/det - v10(2,1)=(d(2,1)*spl3%fsi(mtheta,1)-d(1,1)*spl3%fsi(mtheta,2))/det -c----------------------------------------------------------------------- -c compute first order constants, second eigenfunction. -c----------------------------------------------------------------------- - d(1,1)=d0bar(1,1)+1+alpha - d(2,2)=d0bar(2,2)+1+alpha - det=d(1,1)*d(2,2)-d(1,2)*d(2,1) - v10(1,2)=(d(1,2)*spl3%fsi(mtheta,4)-d(2,2)*spl3%fsi(mtheta,3))/det - v10(2,2)=(d(2,1)*spl3%fsi(mtheta,3)-d(1,1)*spl3%fsi(mtheta,4))/det -c----------------------------------------------------------------------- -c add constants to v1 and fit to cubic splines. -c----------------------------------------------------------------------- - bg%fs(:,1)=spl2%fs(:,1)+v10(1,1) - bg%fs(:,2)=spl2%fs(:,2)+v10(2,1) - bg%fs(:,3)=spl2%fs(:,3)+v10(1,2) - bg%fs(:,4)=spl2%fs(:,4)+v10(2,2) - CALL spline_fit(bg,"periodic") -c----------------------------------------------------------------------- -c diagnose bg. -c----------------------------------------------------------------------- -c bg%title=(/" bg1 "," bg2 "," bg2 "," bg2 "," bg2 "/) -c CALL spline_write1(bg,out_bal1,bin_bal1, -c $ out_bal1_unit,bin_bal1_unit,.FALSE.) -c----------------------------------------------------------------------- -c deallocate local splines. -c----------------------------------------------------------------------- - CALL spline_dealloc(spl1) - CALL spline_dealloc(spl2) - CALL spline_dealloc(spl3) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_prep -c----------------------------------------------------------------------- -c subprogram 3. bal_der -c differential equations for the marginal ballooning. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_der(neq,theta,y,dy) - - INTEGER, INTENT(IN) :: neq - REAL(r8), INTENT(IN) :: theta - REAL(r8), DIMENSION(neq), INTENT(IN) :: y - REAL(r8), DIMENSION(neq), INTENT(OUT) :: dy - - REAL(r8) :: dbdb,kappaw,thfac -c----------------------------------------------------------------------- -c compute coefficients. -c----------------------------------------------------------------------- - CALL spline_eval(bf,theta,0) - thfac=theta-theta0 - dbdb=bf%f(1)*(thfac+bf%f(2))**2+bf%f(3) - kappaw=bf%f(4)+thfac*bf%f(5) -c----------------------------------------------------------------------- -c compute derivatives. -c----------------------------------------------------------------------- - dy(1)=y(2)/dbdb - dy(2)=-y(1)*kappaw -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_der -c----------------------------------------------------------------------- -c subprogram 4. bal_int -c integrates the ideal marginal ballooning equations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_int - - INTEGER, PARAMETER :: neq=2,liw=20,lrw=22+neq*16 - INTEGER, DIMENSION(liw) :: iwork=0 - INTEGER :: iopt,iside,istate,itask,itol,jac,mf - INTEGER :: istep,nstep=HUGE(0),icross - REAL(r8), PARAMETER :: tol=1e-5,thmin=1,hu_min=1e-10 - REAL(r8) :: thmax,atol,ca1,ca2,det,rtol,theta,thout - REAL(r8), DIMENSION(lrw) :: rwork=0 - REAL(r8), DIMENSION(neq) :: y,dy - REAL(r8), DIMENSION(2,2) :: u -c----------------------------------------------------------------------- -c write formats. -c----------------------------------------------------------------------- - 10 FORMAT(/2x,"ist",2x,"nqu",1x,"icr",4x,"theta",6x,"dtheta",4x, - $ "asinh y1",2x,"asinh ca1",2x,"asinh ca2"/) - 20 FORMAT(i5,2i4,1x,1p,5e11.3) -c----------------------------------------------------------------------- -c initialize independent variables. -c----------------------------------------------------------------------- - thmax=SQRT(MAXVAL(ABS(bf%fs(:,3)/bf%fs(:,1))))*10*thmax0 - thmax=MIN(thmax,100._r8) - theta=-thmax - thout=thmax - istep=0 -c----------------------------------------------------------------------- -c compute equilibrium quantities and asymptotic solutions. -c----------------------------------------------------------------------- - CALL bal_pse(theta,u) - y(1)=u(1,2)*sinh(1.) - y(2)=u(2,2)*sinh(1.) -c----------------------------------------------------------------------- -c set up integrator parameters. -c----------------------------------------------------------------------- - icross=0 - iside=1 - istate=1 - itask=5 - mf=10 - rwork(1)=thout - iopt=1 - itol=1 - rtol=tol - atol=tol*tol -c----------------------------------------------------------------------- -c write header. -c----------------------------------------------------------------------- - IF(out_bal2)WRITE(out_bal2_unit,10) -c----------------------------------------------------------------------- -c check for crossings. -c----------------------------------------------------------------------- - DO - IF(iside*y(1) < 0)THEN - icross=icross+1 - iside=-iside - ENDIF -c----------------------------------------------------------------------- -c compute and print asymptotic coefficient. -c----------------------------------------------------------------------- - IF(out_bal2 .OR. bin_bal2)THEN - IF(ABS(theta) > thmin)THEN - CALL bal_pse(theta,u) - det=u(1,1)*u(2,2)-u(1,2)*u(2,1) - ca1=(y(1)*u(2,2)-y(2)*u(1,2))/det - ca2=(y(2)*u(1,1)-y(1)*u(2,1))/det - CALL bal_der(neq,theta,y,dy) - IF(out_bal2) - $ WRITE(out_bal2_unit,20)istep,iwork(14),icross, - $ theta,rwork(11),asinh(y(1)),asinh(ca1),asinh(ca2) - IF(bin_bal2) - $ WRITE(bin_bal2_unit)REAL(theta,4), - $ REAL(asinh(y(1)),4),REAL(asinh(y(2)),4), - $ REAL(y(2)/dy(1),4),REAL(dy(2)/y(1),4), - $ REAL(asinh(ca1),4),REAL(asinh(ca2),4),REAL(u,4) - ELSE - ca1=0 - ca2=0 - det=0 - ENDIF - ENDIF -c----------------------------------------------------------------------- -c advance. -c----------------------------------------------------------------------- - IF(istep >= nstep .OR. theta >= thmax .OR. istate < 0)EXIT - istep=istep+1 - CALL lsode(bal_der,neq,y,theta,thout,itol,rtol,atol,itask, - $ istate,iopt,rwork,lrw,iwork,liw,jac,mf) - IF(rwork(11) < hu_min)EXIT - ENDDO -c----------------------------------------------------------------------- -c write trailer. -c----------------------------------------------------------------------- - IF(out_bal2)WRITE(out_bal2_unit,10) - IF(bin_bal2)WRITE(bin_bal2_unit) -c----------------------------------------------------------------------- -c compute and store final asymptotic coefficients. -c----------------------------------------------------------------------- - IF(theta >= thmax)THEN - CALL bal_pse(theta,u) - det=u(1,1)*u(2,2)-u(1,2)*u(2,1) - ca1=(y(1)*u(2,2)-y(2)*u(1,2))/det - ca2=(y(2)*u(1,1)-y(1)*u(2,1))/det - locstab%fs(ipsi,4:5)=(/ca1,ca2/) - ENDIF -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_int -c----------------------------------------------------------------------- -c subprogram 5. bal_pse. -c computes alpha series solutions. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_pse(theta,u) - - REAL(r8), INTENT(IN) :: theta - REAL(r8), INTENT(OUT), DIMENSION(2,2) :: u - - REAL(r8) :: dtheta,thfac - REAL(r8), DIMENSION(2,2) :: v -c----------------------------------------------------------------------- -c compute alpha series. -c----------------------------------------------------------------------- - CALL spline_eval(bg,theta,0) - dtheta=theta-theta0 - v(1,1)=v0(1,1)+bg%f(1)/dtheta - v(2,1)=v0(2,1)+bg%f(2)/dtheta - v(1,2)=v0(1,2)+bg%f(3)/dtheta - v(2,2)=v0(2,2)+bg%f(4)/dtheta -c----------------------------------------------------------------------- -c compute transformed solutions. -c----------------------------------------------------------------------- - u(1,1)=v(1,1) - u(1,2)=v(1,2) - u(2,1)=bg%f(5)*v(1,1)+v(2,1) - u(2,2)=bg%f(5)*v(1,2)+v(2,2) -c----------------------------------------------------------------------- -c multiply by alphas. -c----------------------------------------------------------------------- - thfac=ABS(dtheta)**(alpha+.5)/dtheta - u(1,1)=u(1,1)*thfac - u(2,1)=u(2,1)*thfac*dtheta - u(1,2)=u(1,2)/(thfac*dtheta) - u(2,2)=u(2,2)/thfac -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_pse - END MODULE bal_mod + module bal_mod + use dcon_mod + use, intrinsic :: ieee_arithmetic + implicit none + + integer, private :: ipsi + real(r8), private :: theta_max + type(spline_type), private :: bf + + contains + + subroutine bal_scan + real(r8) :: di, h, psi, delta_prime + real(r8), dimension(0:mpsi) :: volume + type(spline_type) :: volume_spline + + volume = 0.0_r8 + if (bal_flag) then + call spline_alloc(volume_spline,mpsi,1) + volume_spline%xs = sq%xs + volume_spline%fs(:,1) = sq%fs(:,3) + volume_spline%fs1(:,1) = sq%fs1(:,3) + call spline_int(volume_spline) + volume = max(volume_spline%fsi(:,1),0.0_r8) + call spline_dealloc(volume_spline) + end if + + do ipsi = 0, mpsi + psi = sq%xs(ipsi) + call bal_prep(di,h,0.0_r8) + locstab%fs(ipsi,1) = di * psi + locstab%fs(ipsi,2) = (di + (h - 0.5_r8)**2) * psi + locstab%fs(ipsi,3) = h + locstab%fs(ipsi,4) = ieee_value(0.0_r8,ieee_quiet_nan) + locstab%fs(ipsi,5) = ieee_value(0.0_r8,ieee_quiet_nan) + locstab%fs(ipsi,6) = ieee_value(0.0_r8,ieee_quiet_nan) + if (bal_flag .and. psi <= 1.0_r8) then + call bal_int(delta_prime) + locstab%fs(ipsi,4) = delta_prime + end if + call spline_dealloc(bf) + if (bal_flag) call bal_alpha_first_boundary(volume(ipsi)) + end do + end subroutine bal_scan + + subroutine bal_prep(di,h,corr_pprime) + real(r8), intent(out) :: di, h + real(r8), intent(in) :: corr_pprime + + integer :: itheta + real(r8) :: psi, twopif, p1, v1, q, q1, chi1 + real(r8) :: theta, rho, eta, ceta, seta, r, jac + real(r8) :: drfac_dp, eta_dp, dr_dp + real(r8) :: dv21_dp, dv22_dp, dv23_dp, dv33_dp + real(r8) :: iper_ref, local_scale, cscale + real(r8) :: abar_w, bbar_w, response_denom + real(r8) :: dbase(3), dB(3), bvec(3) + real(r8) :: gradpsi(3), gradtheta(3), gradzeta(3) + real(r8) :: v(3,3), w(3,3) + real(r8) :: d0bar(2,2) + real(r8), dimension(0:mtheta) :: bsq, gradpsi_sq, dpsisq, jacarr + real(r8), dimension(0:mtheta) :: bdot_theta_phi, iper, htheta + real(r8), dimension(0:mtheta) :: dbsq_dtheta, kappas + real(r8), dimension(0:mtheta) :: jac_psi, bsq_psi + real(r8), dimension(0:mtheta) :: spl1_vals, spl1_deriv + real(r8), dimension(0:mtheta) :: a0, a1, a2 + real(r8), dimension(0:mtheta) :: kappaw0, kappaw1 + real(r8), dimension(0:mtheta) :: aw, bw, aperp + real(r8), dimension(0:mtheta) :: diper_dtheta, iper_perturbed + real(r8), dimension(0:mtheta) :: jacchi, pchi, sigma + real(r8), dimension(0:mtheta) :: m012, m021 + real(r8), dimension(0:mtheta,3) :: hfs + real(r8), dimension(0:mtheta,4) :: n0 + real(r8), dimension(0:mtheta,7) :: coeff + real(r8), dimension(0:mtheta,4) :: f0, fx0, fy0, fxy0 + real(r8), dimension(0:mtheta) :: rho0, eta0, r0 + real(r8), dimension(0:mtheta,3,3) :: v0 + real(r8), dimension(0:mtheta,3) :: vzp0, vpt0 + type(spline_type) :: work + + psi = sq%xs(ipsi) + twopif = sq%fs(ipsi,1) + p1 = sq%fs1(ipsi,2) + v1 = sq%fs(ipsi,3) + q = sq%fs(ipsi,4) + q1 = sq%fs1(ipsi,4) + chi1 = twopi * psio + + do itheta = 0, mtheta + theta = rzphi%ys(itheta) + call bicube_eval(rzphi, rzphi%xs(ipsi), theta, 2) + f0(itheta,:) = rzphi%f + fx0(itheta,:) = rzphi%fx + fy0(itheta,:) = rzphi%fy + fxy0(itheta,:) = rzphi%fxy + + rho = sqrt(f0(itheta,1)) + eta = twopi * (theta + f0(itheta,2)) + ceta = cos(eta) + seta = sin(eta) + r = ro + rho * ceta + jac = f0(itheta,4) + rho0(itheta) = rho + eta0(itheta) = eta + r0(itheta) = r + jacarr(itheta) = jac + + v = 0.0_r8 + v(1,1) = fx0(itheta,1) / (2.0_r8*rho*jac) + v(1,2) = fx0(itheta,2) * twopi*rho / jac + v(1,3) = fx0(itheta,3) * r / jac + v(2,1) = fy0(itheta,1) / (2.0_r8*rho*jac) + v(2,2) = (1.0_r8 + fy0(itheta,2)) * twopi*rho / jac + v(2,3) = fy0(itheta,3) * r / jac + v(3,3) = twopi*r / jac + + w = 0.0_r8 + w(1,1) = (1.0_r8 + fy0(itheta,2))*twopi**2*rho*r/jac + w(1,2) = -fy0(itheta,1)*pi*r/(rho*jac) + w(2,1) = -fx0(itheta,2)*twopi**2*r*rho/jac + w(2,2) = fx0(itheta,1)*pi*r/(rho*jac) + w(3,1) = (fx0(itheta,2)*fy0(itheta,3) - + $fx0(itheta,3)*(1.0_r8 + fy0(itheta,2)))*twopi*r*rho/jac + w(3,2) = (fx0(itheta,3)*fy0(itheta,1) - + $fx0(itheta,1)*fy0(itheta,3))*r/(2.0_r8*rho*jac) + w(3,3) = 1.0_r8/(twopi*r) + + gradpsi = w(1,:) + gradtheta = w(2,:) + gradzeta = w(3,:) + bvec = (v(2,:) + q*v(3,:))*chi1 + bsq(itheta) = dot_product(bvec,bvec) + gradpsi_sq(itheta) = dot_product(gradpsi,gradpsi) + dpsisq(itheta) = (twopi*r)**2 * + $(v(2,1)**2 + v(2,2)**2) + iper(itheta) = dot_product(q*gradtheta-gradzeta,gradpsi) / + $gradpsi_sq(itheta) + htheta(itheta) = fy0(itheta,3)/twopi + bdot_theta_phi(itheta) = dot_product(bvec,v(1,:)) + v0(itheta,:,:) = v + vzp0(itheta,:) = v(2,:) + vpt0(itheta,:) = v(3,:) + end do + + call periodic_lowpass_derivative(bsq, rzphi%ys, 16, dbsq_dtheta) + kappas = dbsq_dtheta*twopif/(2.0_r8*jacarr*bsq**2) + + do itheta = 0, mtheta + jac_psi(itheta) = fx0(itheta,4) + drfac_dp = fx0(itheta,1)/(2.0_r8*rho0(itheta)) + eta_dp = twopi*fx0(itheta,2) + dr_dp = drfac_dp*cos(eta0(itheta)) - + $rho0(itheta)*sin(eta0(itheta))*eta_dp + + dv21_dp = (fxy0(itheta,1)/(2.0_r8*rho0(itheta)) - + $fx0(itheta,1)*fy0(itheta,1)/(4.0_r8*f0(itheta,1)* + $rho0(itheta)))/jacarr(itheta) - + $v0(itheta,2,1)*jac_psi(itheta)/jacarr(itheta) + dv22_dp = (fxy0(itheta,2)*twopi*rho0(itheta) + + $(1.0_r8+fy0(itheta,2))*twopi*drfac_dp)/jacarr(itheta) - + $v0(itheta,2,2)*jac_psi(itheta)/jacarr(itheta) + dv23_dp = (fxy0(itheta,3)*r0(itheta) + + $fy0(itheta,3)*dr_dp)/jacarr(itheta) - + $v0(itheta,2,3)*jac_psi(itheta)/jacarr(itheta) + dv33_dp = twopi*dr_dp/jacarr(itheta) - + $v0(itheta,3,3)*jac_psi(itheta)/jacarr(itheta) + + dbase = (/dv21_dp,dv22_dp,dv23_dp/) + + $q*(/0.0_r8,0.0_r8,dv33_dp/) + q1*v0(itheta,3,:) + dB = dbase*chi1 + bsq_psi(itheta) = 2.0_r8*dot_product( + $vzp0(itheta,:)+q*vpt0(itheta,:),dB)*chi1 + end do + + spl1_vals = jacarr*bdot_theta_phi/sqrt(bsq) + call periodic_lowpass_derivative(spl1_vals, rzphi%ys, 32, + $spl1_deriv) + kappaw0 = -(jac_psi/jacarr + 0.5_r8*bsq_psi/bsq)/chi1 + + $spl1_deriv/(jacarr*sqrt(bsq)) + twopif*q1/(bsq*jacarr) + +c corr_pprime also drives the I_per response. This is the +c corr_qprime=0 specialization of Julia's response calculation. + aw = twopi**2*r0**2/(chi1**2*gradpsi_sq) + bw = twopif**2/(chi1**2*gradpsi_sq) + hfs(:,1) = (q+htheta)*aw + hfs(:,2) = (q+htheta)*bw + hfs(mtheta,1:2) = hfs(0,1:2) + call spline_alloc(work,mtheta,2) + work%xs = rzphi%ys + work%fs = hfs(:,1:2) + call spline_fit(work,"periodic") + call spline_int(work) + abar_w = work%fsi(mtheta,1)/q + bbar_w = work%fsi(mtheta,2)/q + call spline_dealloc(work) + response_denom = 1.0_r8+bbar_w + aperp = (q+htheta)*((aw-abar_w) - + $abar_w/response_denom*(bw-bbar_w)) + diper_dtheta = aperp*corr_pprime + diper_dtheta(mtheta) = diper_dtheta(0) + call spline_alloc(work,mtheta,1) + work%xs = rzphi%ys + work%fs(:,1) = diper_dtheta + call spline_fit(work,"periodic") + call spline_int(work) + iper_perturbed = work%fsi(:,1) + call spline_dealloc(work) + + iper_ref = iper(0) + iper = iper - iper_ref + iper_perturbed + p1 = p1+corr_pprime + kappaw0 = kappaw0-kappas*iper_perturbed + a0 = 1.0_r8/(chi1**2*gradpsi_sq) + + $gradpsi_sq/bsq*iper**2 + a1 = gradpsi_sq/bsq*(2.0_r8*q1*iper) + a2 = gradpsi_sq/bsq*q1**2 + jacchi = jacarr/chi1 + pchi = p1/chi1 + kappaw1 = q1*kappas + + coeff(:,1) = a0 + coeff(:,2) = a1 + coeff(:,3) = a2 + coeff(:,4) = kappaw0 + coeff(:,5) = kappaw1 + coeff(:,6) = jacchi + coeff(:,7) = pchi + + theta_max = 0.0_r8 + do itheta = 0, mtheta + if (abs(a2(itheta)) > epsilon(1.0_r8)) then + cscale = a0(itheta)-a1(itheta)**2/(4.0_r8*a2(itheta)) + local_scale = sqrt(abs(cscale/a2(itheta))) + if (ieee_is_finite(local_scale)) + $theta_max = max(theta_max,local_scale) + end if + end do + if (theta_max <= 0.0_r8) theta_max = 16.5_r8 + theta_max = min(16.5_r8,10.0_r8*theta_max) + theta_max = max(theta_max,1.0e-2_r8) + + sigma = -(twopif*p1*q1)/(chi1**2*bsq) + m012 = jacchi/a2 + m021 = -2.0_r8*jacchi*pchi*kappaw0 + n0(:,1) = 0.5_r8 + m012*sigma + n0(:,2) = m012 + n0(:,3) = m021 - sigma - m012*sigma**2 + n0(:,4) = -0.5_r8 - m012*sigma + n0(mtheta,:) = n0(0,:) + + call spline_alloc(work,mtheta,4) + work%xs = rzphi%ys + work%fs = n0 + call spline_fit(work,"periodic") + call spline_int(work) + d0bar(1,1) = work%fsi(mtheta,1) + d0bar(1,2) = work%fsi(mtheta,2) + d0bar(2,1) = work%fsi(mtheta,3) + d0bar(2,2) = work%fsi(mtheta,4) + call spline_dealloc(work) + di = d0bar(1,1)*d0bar(2,2)-d0bar(1,2)*d0bar(2,1) + + hfs(:,1) = bsq/dpsisq*jacarr/v1 + hfs(:,2) = 1.0_r8/dpsisq*jacarr/v1 + hfs(:,3) = bsq*jacarr/v1 + hfs(mtheta,:) = hfs(0,:) + call spline_alloc(work,mtheta,3) + work%xs = rzphi%ys + work%fs = hfs + call spline_fit(work,"periodic") + call spline_int(work) + h = twopif*p1*v1/(q1*chi1**3)*(work%fsi(mtheta,2) - + $work%fsi(mtheta,1)/work%fsi(mtheta,3)) + call spline_dealloc(work) + + coeff(mtheta,:) = coeff(0,:) + call spline_alloc(bf,mtheta,7) + bf%xs = rzphi%ys + bf%fs = coeff + call spline_fit(bf,"periodic") + end subroutine bal_prep + + subroutine periodic_lowpass_derivative(vals,xs,nkeep,deriv) + real(r8), intent(in) :: vals(0:), xs(0:) + integer, intent(in) :: nkeep + real(r8), intent(out) :: deriv(0:) + integer :: n, kmax, j, l, k + real(r8) :: period, omega, phase, ak, bk + + n = ubound(vals,1) + period = xs(n)-xs(0) + kmax = min(nkeep,(n-1)/2) + deriv = 0.0_r8 + do k = 1, kmax + ak = 0.0_r8 + bk = 0.0_r8 + do l = 0, n-1 + phase = twopi*real(k*l,r8)/real(n,r8) + ak = ak + vals(l)*cos(phase) + bk = bk + vals(l)*sin(phase) + end do + ak = 2.0_r8*ak/real(n,r8) + bk = 2.0_r8*bk/real(n,r8) + omega = twopi*real(k,r8)/period + do j = 0, n-1 + phase = twopi*real(k*j,r8)/real(n,r8) + deriv(j) = deriv(j) + omega*(-ak*sin(phase)+bk*cos(phase)) + end do + end do + deriv(n) = deriv(0) + end subroutine periodic_lowpass_derivative + + subroutine bal_der(neq,theta,y,dy) + integer, intent(in) :: neq + real(r8), intent(in) :: theta + real(r8), intent(in) :: y(neq) + real(r8), intent(out) :: dy(neq) + real(r8) :: f, kappaw, g, theta_wrapped + + theta_wrapped = modulo(theta-bf%xs(0), + $bf%xs(bf%mx)-bf%xs(0))+bf%xs(0) + call spline_eval(bf,theta_wrapped,0) + f = (bf%f(1)+theta*bf%f(2)+theta**2*bf%f(3))/bf%f(6) + kappaw = bf%f(4)-theta*bf%f(5) + g = -2.0_r8*bf%f(6)*bf%f(7)*kappaw + dy(1) = y(2)/f + dy(2) = g*y(1) + end subroutine bal_der + + subroutine bal_integrate_side(theta_start,theta_end,y,success) + integer, parameter :: neq=2, liw=20, lrw=22+neq*16 + real(r8), intent(in) :: theta_start, theta_end + real(r8), intent(inout) :: y(neq) + logical, intent(out) :: success + integer :: istate, itask, itol, iopt, jac, mf + integer :: iwork(liw) + real(r8) :: theta, rtol, atol, rwork(lrw) + + iwork = 0 + rwork = 0.0_r8 + theta = theta_start + itol = 1 + rtol = 1.0e-8_r8 + atol = 1.0e-16_r8 + itask = 1 + istate = 1 + iopt = 1 + iwork(6) = 100000 + rwork(7) = 1.0e-10_r8 + jac = 0 + mf = 10 + call lsode(bal_der,neq,y,theta,theta_end,itol,rtol,atol,itask, + $istate,iopt,rwork,lrw,iwork,liw,jac,mf) + success = istate == 2 + end subroutine bal_integrate_side + + subroutine bal_int(delta_prime) + real(r8), parameter :: match_point=1.0e-3_r8 + real(r8), intent(out) :: delta_prime + real(r8) :: yl(2), yr(2) + logical :: ok_left, ok_right + + yl = (/0.0_r8,1.0_r8/) + yr = (/0.0_r8,-1.0_r8/) + call bal_integrate_side(-theta_max,-match_point,yl,ok_left) + call bal_integrate_side(theta_max,match_point,yr,ok_right) + if (ok_left .and. ok_right .and. + $abs(yl(1)) > tiny(1.0_r8) .and. + $abs(yr(1)) > tiny(1.0_r8)) then + delta_prime = yr(2)/yr(1)-yl(2)/yl(1) + else + delta_prime = ieee_value(0.0_r8,ieee_quiet_nan) + end if + end subroutine bal_int + + subroutine bal_delta_at_scale(scale,delta_prime) + real(r8), intent(in) :: scale + real(r8), intent(out) :: delta_prime + real(r8) :: corr_pprime, di_dummy, h_dummy + + corr_pprime = sq%fs1(ipsi,2)*(scale-1.0_r8) + call bal_prep(di_dummy,h_dummy,corr_pprime) + call bal_int(delta_prime) + call spline_dealloc(bf) + end subroutine bal_delta_at_scale + + subroutine bal_alpha_first_boundary(volume) + real(r8), intent(in) :: volume + integer, parameter :: n_scan=24 + real(r8), parameter :: pole_cap=3.0_r8 + real(r8), parameter :: max_alpha_scale=8.0_r8 + real(r8), parameter :: alpha_scale_tol=1.0e-3_r8 + integer :: iscan + real(r8) :: alpha_ref, scale, scale_prev, step + real(r8) :: d, d_prev, d_anchor, lo, hi, mid, dm, sign_lo + logical :: have_anchor + + if (sq%xs(ipsi) < 0.1_r8 .or. + $sq%xs(ipsi) > min(0.99_r8,sq%xs(mpsi))) return + alpha_ref = -2.0_r8*sq%fs1(ipsi,2)*sq%fs(ipsi,3) / + $psio**2*sqrt(volume/(twopi*max(ro,epsilon(1.0_r8)))) + locstab%fs(ipsi,6) = alpha_ref + step = max_alpha_scale/real(n_scan,r8) + have_anchor = .false. + d_prev = ieee_value(0.0_r8,ieee_quiet_nan) + scale_prev = 0.0_r8 + + do iscan = 0, n_scan + scale = step*real(iscan,r8) + call bal_delta_at_scale(scale,d) + if (.not. have_anchor) then + if (ieee_is_finite(d) .and. d /= 0.0_r8) then + have_anchor = .true. + d_anchor = abs(d) + end if + else if (ieee_is_finite(d_prev) .and. ieee_is_finite(d) .and. + $sign(1.0_r8,d) /= sign(1.0_r8,d_prev) .and. + $max(abs(d_prev),abs(d)) <= pole_cap*d_anchor) then + lo = scale_prev + hi = scale + sign_lo = sign(1.0_r8,d_prev) + do while (abs(hi-lo) > alpha_scale_tol) + mid = 0.5_r8*(lo+hi) + call bal_delta_at_scale(mid,dm) + if (ieee_is_finite(dm) .and. + $sign(1.0_r8,dm) == sign_lo) then + lo = mid + else + hi = mid + end if + end do + locstab%fs(ipsi,5) = alpha_ref*0.5_r8*(lo+hi) + return + end if + d_prev = d + scale_prev = scale + end do + end subroutine bal_alpha_first_boundary + + end module bal_mod diff --git a/dcon/dcon.F b/dcon/dcon.F index bd125dc9b..291286017 100644 --- a/dcon/dcon.F +++ b/dcon/dcon.F @@ -18,7 +18,6 @@ PROGRAM dcon USE equil_mod USE equil_out_mod USE bal_mod - USE mercier_mod USE ode_mod USE free_mod USE resist_mod @@ -39,8 +38,9 @@ PROGRAM dcon REAL(r8), DIMENSION(:), ALLOCATABLE :: psitmp COMPLEX(r8), DIMENSION(:,:), ALLOCATABLE :: xtmp - NAMELIST/dcon_control/bal_flag,mat_flag,ode_flag,vac_flag, - $ mer_flag,res_flag,fft_flag,node_flag,mthvac,sing_start,nn, + NAMELIST/dcon_control/bal_flag,mer_flag, + $ mat_flag,ode_flag,vac_flag,res_flag,fft_flag,node_flag, + $ mthvac,sing_start,nn, $ delta_mlow,delta_mhigh,delta_mband,thmax0,nstep,ksing, $ tol_nr,tol_r,crossover,ucrit,singfac_min,singfac_max, $ cyl_flag,dmlim,lim_flag,sas_flag,sing_order,sort_type, @@ -60,7 +60,7 @@ PROGRAM dcon c format statements. c----------------------------------------------------------------------- 10 FORMAT(/'ipsi',2x,'psifac',6x,'f',7x,'mu0 p',5x,'dvdpsi', - $ 6x,'q',10x,'di',9x,'dr',8x,'ca1'/) + $ 6x,'q',10x,'di',9x,'dr',6x,'bal_dp'/) 20 FORMAT(i4,1p,8e11.3) 30 FORMAT(/3x,"mlow",1x,"mhigh",1x,"mpert",1x,"mband",3x,"nn",2x, $ "lim_fl",2x,"dmlim",7x,"qlim",6x,"psilim"//5i6,l6,1p,3e11.3/) @@ -131,17 +131,14 @@ PROGRAM dcon c----------------------------------------------------------------------- c prepare local stability criteria. c----------------------------------------------------------------------- - CALL spline_alloc(locstab,mpsi,5) + CALL spline_alloc(locstab,mpsi,6) locstab%xs=sq%xs locstab%fs=0 locstab%name="locstb" - locstab%title=(/" di "," dr "," h "," ca1 "," ca2 "/) - IF (mer_flag) THEN - WRITE(*,*)"Evaluating Mercier criterion" - CALL mercier_scan - ENDIF - IF(bal_flag)THEN - IF(verbose) WRITE(*,*)"Evaluating ballooning criterion" + locstab%title=(/" di "," dr "," h ","bal_dp","alpcrt", + $ " alpha"/) + IF(bal_flag .OR. mer_flag)THEN + IF(verbose) WRITE(*,*)"Evaluating local ballooning stability" CALL bal_scan ENDIF CALL spline_fit(locstab,"extrap") @@ -164,7 +161,9 @@ PROGRAM dcon $ REAL(asinh(locstab%fs(ipsi,1)/sq%xs(ipsi)),4), $ REAL(asinh(locstab%fs(ipsi,2)/sq%xs(ipsi)),4), $ REAL(asinh(locstab%fs(ipsi,3)),4), - $ REAL(asinh(locstab%fs(ipsi,4)),4) + $ REAL(asinh(locstab%fs(ipsi,4)),4), + $ REAL(locstab%fs(ipsi,6),4), + $ REAL(locstab%fs(ipsi,5),4) ENDDO WRITE(out_unit,10) WRITE(bin_unit) diff --git a/dcon/dcon_mod.f b/dcon/dcon_mod.f index 58488c34d..0b50eaf2c 100644 --- a/dcon/dcon_mod.f +++ b/dcon/dcon_mod.f @@ -59,10 +59,10 @@ MODULE dcon_mod LOGICAL :: netcdf_out=.TRUE. LOGICAL :: bal_flag=.FALSE. + LOGICAL :: mer_flag=.FALSE. LOGICAL :: mat_flag=.FALSE. LOGICAL :: ode_flag=.FALSE. LOGICAL :: vac_flag=.FALSE. - LOGICAL :: mer_flag=.FALSE. LOGICAL :: crit_break=.TRUE. LOGICAL :: node_flag=.FALSE. diff --git a/dcon/dcon_netcdf.f b/dcon/dcon_netcdf.f index fcff70d3b..368803664 100644 --- a/dcon/dcon_netcdf.f +++ b/dcon/dcon_netcdf.f @@ -58,7 +58,8 @@ SUBROUTINE dcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) INTEGER :: i, ncid, $ i_dim, m_dim, mo_dim, p_dim, ep_dim, i_id, m_id, mo_id, p_id, - $ f_id, q_id, dv_id, mu_id, di_id, dr_id, ca_id, + $ f_id, q_id, dv_id, mu_id, di_id, dr_id, bal_dp_id, + $ bal_alpha_id, bal_alpha_ref_id, $ wp_id, wpv_id, wv_id, wvv_id, wt_id, wtv_id, wt0_id, $ pd_dim, pd_id, df_id, ep_id, ew_id, eq_id REAL(r4) :: cpusec, wallsec @@ -171,7 +172,12 @@ SUBROUTINE dcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) $ "Safety Factor") ) CALL check( nf90_def_var(ncid, "di", nf90_double, p_dim, di_id) ) CALL check( nf90_def_var(ncid, "dr", nf90_double, p_dim, dr_id) ) - CALL check( nf90_def_var(ncid, "ca1", nf90_double, p_dim, ca_id)) + CALL check(nf90_def_var(ncid,"ballooning_Delta_prime", + $ nf90_double,p_dim,bal_dp_id)) + CALL check(nf90_def_var(ncid,"alpha_critical",nf90_double, + $ p_dim,bal_alpha_id)) + CALL check(nf90_def_var(ncid,"alpha",nf90_double, + $ p_dim,bal_alpha_ref_id)) CALL check( nf90_def_var(ncid, "W_p_eigenvector", nf90_double, $ (/m_dim, mo_dim, i_dim/), wp_id) ) CALL check( nf90_def_var(ncid, "W_p_eigenvalue", nf90_double, @@ -235,7 +241,9 @@ SUBROUTINE dcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) CALL check( nf90_put_var(ncid,q_id, sq%fs(:,4))) CALL check( nf90_put_var(ncid,di_id, locstab%fs(:,1)/sq%xs(:))) CALL check( nf90_put_var(ncid,dr_id, locstab%fs(:,2)/sq%xs(:))) - CALL check( nf90_put_var(ncid,ca_id, locstab%fs(:,4))) + CALL check(nf90_put_var(ncid,bal_dp_id,locstab%fs(:,4))) + CALL check(nf90_put_var(ncid,bal_alpha_id,locstab%fs(:,5))) + CALL check(nf90_put_var(ncid,bal_alpha_ref_id,locstab%fs(:,6))) IF(size_edge > 0)THEN CALL check( nf90_put_var(ncid,ew_id, RESHAPE((/REAL(dw_edge), $ AIMAG(dw_edge)/),(/size_edge,2/))) ) @@ -273,4 +281,4 @@ SUBROUTINE dcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) RETURN END SUBROUTINE dcon_netcdf_out - END MODULE dcon_netcdf_mod \ No newline at end of file + END MODULE dcon_netcdf_mod diff --git a/dcon/mercier.f b/dcon/mercier.f deleted file mode 100644 index ac312ad14..000000000 --- a/dcon/mercier.f +++ /dev/null @@ -1,105 +0,0 @@ -c----------------------------------------------------------------------- -c file mercier.f. -c computes mercier criterion and related quantities. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c code organization. -c----------------------------------------------------------------------- -c 0. mercier_mod. -c 1. mercier_scan. -c----------------------------------------------------------------------- -c subprogram 0. mercier_mod. -c module declarations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - MODULE mercier_mod - USE dcon_mod - IMPLICIT NONE - - CONTAINS -c----------------------------------------------------------------------- -c subprogram 1. mercier_scan. -c evaluates mercier criterion and related quantities. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE mercier_scan - - INTEGER :: ipsi,itheta - REAL(r8) :: bsq,chi1,di,dpsisq,eta,h,jac,p1,psifac,q,q1,r, - $ rfac,term,theta,twopif,v1,v2,v21,v22,v23,v33 - REAL(r8), DIMENSION(:), POINTER :: avg - TYPE(spline_type), TARGET :: ff -c----------------------------------------------------------------------- -c prepare spline types. -c----------------------------------------------------------------------- - CALL spline_alloc(ff,mtheta,5) - ff%xs=rzphi%ys -c----------------------------------------------------------------------- -c compute surface quantities. -c----------------------------------------------------------------------- - DO ipsi=0,mpsi - psifac=sq%xs(ipsi) - twopif=sq%fs(ipsi,1) - p1=sq%fs1(ipsi,2) - v1=sq%fs(ipsi,3) - v2=sq%fs1(ipsi,3) - q=sq%fs(ipsi,4) - q1=sq%fs1(ipsi,4) - chi1=twopi*psio -c----------------------------------------------------------------------- -c evaluate coordinates and jacobian. -c----------------------------------------------------------------------- - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - theta=rzphi%ys(itheta) - rfac=SQRT(rzphi%f(1)) - eta=twopi*(theta+rzphi%f(2)) - r=ro+rfac*COS(eta) - jac=rzphi%f(4) -c----------------------------------------------------------------------- -c evaluate other local quantities. -c----------------------------------------------------------------------- - v21=rzphi%fy(1)/(2*rfac*jac) - v22=(1+rzphi%fy(2))*twopi*rfac/jac - v23=rzphi%fy(3)*r/jac - v33=twopi*r/jac - bsq=chi1**2*(v21**2+v22**2+(v23+q*v33)**2) - dpsisq=(twopi*r)**2*(v21**2+v22**2) -c----------------------------------------------------------------------- -c evaluate integrands. -c----------------------------------------------------------------------- - ff%fs(itheta,1)=bsq/dpsisq - ff%fs(itheta,2)=1/dpsisq - ff%fs(itheta,3)=1/bsq - ff%fs(itheta,4)=1/(bsq*dpsisq) - ff%fs(itheta,5)=bsq - ff%fs(itheta,:)=ff%fs(itheta,:)*jac/v1 - ENDDO -c----------------------------------------------------------------------- -c integrate quantities with respect to theta. -c----------------------------------------------------------------------- - CALL spline_fit(ff,"periodic") - CALL spline_int(ff) - avg => ff%fsi(mtheta,:) -c----------------------------------------------------------------------- -c evaluate mercier criterion and related quantities. -c----------------------------------------------------------------------- - term=twopif*p1*v1/(q1*chi1**3)*avg(2) - di=-.25+term*(1-term)+p1*(v1/(q1*chi1**2))**2*avg(1) - $ *(p1*(avg(3)+(twopif/chi1)**2*avg(4))-v2/v1) - h=twopif*p1*v1/(q1*chi1**3)*(avg(2)-avg(1)/avg(5)) - locstab%fs(ipsi,1)=di*locstab%xs(ipsi) - locstab%fs(ipsi,2)=(di+(h-0.5)**2)*locstab%xs(ipsi) - locstab%fs(ipsi,3)=h - ENDDO - CALL spline_dealloc(ff) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE mercier_scan - END MODULE mercier_mod diff --git a/draw/drawdcon.in b/draw/drawdcon.in index 81a4ae4a6..11c8c9e42 100644 --- a/draw/drawdcon.in +++ b/draw/drawdcon.in @@ -14,7 +14,9 @@ variable names 5 ArcSinh DI 6 ArcSinh DR 7 ArcSinh H -8 ArcSinh ca1 +8 ArcSinh ballooning Delta-prime +9 alpha +10 alpha critical ix iy title 1 2 "f = R BT @@ -22,4 +24,5 @@ ix iy title 1 4 "Safety Factor 1 5 "Mercier Criterion, DI > 0 => Unstable 1 6 "GGJ Resistive Criterion, DR > 0 => Unstable -1 8 "High-n Ballooning, CA1 < 0 => Unstable +1 8 "High-n Ballooning Delta-prime > 0 => Unstable +1 9 10 "Infinite-n Ballooning: alpha > alpha critical => Unstable diff --git a/draw/drawdcon2.in b/draw/drawdcon2.in index 979514347..0e14ae8b3 100644 --- a/draw/drawdcon2.in +++ b/draw/drawdcon2.in @@ -19,7 +19,9 @@ variable names 5 ArcSinh DI 6 ArcSinh DR 7 ArcSinh H -8 ArcSinh ca1 +8 ArcSinh ballooning Delta-prime +9 alpha +10 alpha critical ix iy title 1 2 "f = R BT @@ -27,4 +29,5 @@ ix iy title 1 4 "Safety Factor 1 5 "Mercier Criterion, DI > 0 => Unstable 1 6 "GGJ Resistive Criterion, DR > 0 => Unstable -1 8 "High-n Ballooning, CA1 < 0 => Unstable +1 8 "High-n Ballooning Delta-prime > 0 => Unstable +1 9 10 "Infinite-n Ballooning: alpha > alpha critical => Unstable diff --git a/install/DEPENDENCIES.inc b/install/DEPENDENCIES.inc index 4a06e1f8f..03742128f 100644 --- a/install/DEPENDENCIES.inc +++ b/install/DEPENDENCIES.inc @@ -108,13 +108,12 @@ # DCON ../dcon/bal.o: ../dcon/dcon_mod.o -../dcon/dcon.o: ../dcon/bal.o ../dcon/free.o ../dcon/mercier.o ../dcon/ode.o ../dcon/resist.o ../equil/equil.o ../equil/equil_out.o ../pentrc/pentrc_interface.o +../dcon/dcon.o: ../dcon/bal.o ../dcon/free.o ../dcon/ode.o ../dcon/resist.o ../equil/equil.o ../equil/equil_out.o ../pentrc/pentrc_interface.o ../dcon/dcon_mod.o: ../equil/equil.o ../equil/equil_out.o ../equil/global.o ../equil/spline.o ../pentrc/pentrc_interface.o ../dcon/dcon_netcdf.o: ../dcon/dcon_mod.o ../dcon/sing.o ../equil/local.o ../dcon/debug.o: ../equil/local.o ../dcon/fourfit.o: ../dcon/dcon_mod.o ../equil/fspline.o ../equil/global.o ../pentrc/dcon_interface.o ../pentrc/energy.o ../pentrc/inputs.o ../pentrc/pentrc_interface.o ../pentrc/pitch.o ../pentrc/torque.o ../pentrc/utilities.o ../dcon/free.o: ../dcon/dcon_netcdf.o ../dcon/fourfit.o ../dcon/ode_output.o ../dcon/sing.o ../equil/global.o ../vacuum/vacuum_ma.o -../dcon/mercier.o: ../dcon/dcon_mod.o ../dcon/ode.o: ../dcon/debug.o ../dcon/fourfit.o ../dcon/free.o ../dcon/ode_output.o ../dcon/ode_output.o: ../dcon/dcon_mod.o ../dcon/sing.o ../dcon/resist.o: ../dcon/dcon_mod.o @@ -122,14 +121,14 @@ # RDCON ../rdcon/bal.o: ../rdcon/dcon_mod.o -../rdcon/dcon.o: ../equil/equil.o ../equil/equil_out.o ../rdcon/bal.o ../rdcon/free.o ../rdcon/gal.o ../rdcon/lib_interface.o ../rdcon/mercier.o ../rdcon/ode.o ../rdcon/resist.o +../rdcon/dcon.o: ../equil/equil.o ../equil/equil_out.o ../rdcon/bal.o ../rdcon/free.o ../rdcon/gal.o ../rdcon/lib_interface.o ../rdcon/mre.o ../rdcon/ode.o ../rdcon/resist.o ../rdcon/dcon_mod.o: ../equil/equil.o ../equil/equil_out.o ../equil/global.o ../equil/spline.o ../rdcon/version.inc ../rdcon/debug.o: ../equil/local.o ../rdcon/fourfit.o: ../equil/fspline.o ../equil/global.o ../rdcon/dcon_mod.o ../rdcon/toolbox.o ../rdcon/free.o: ../rdcon/ode.o ../rdcon/rdcon_netcdf.o ../vacuum/vacuum_ma.o ../rdcon/gal.o: ../equil/jacobi.o ../rdcon/free.o ../rdcon/sing.o ../rdcon/lib_interface.o: ../equil/global.o ../equil/inverse.o ../equil/local.o ../equil/utils.o -../rdcon/mercier.o: ../rdcon/dcon_mod.o +../rdcon/mre.o: ../rdcon/dcon_mod.o ../rdcon/ode.o: ../rdcon/debug.o ../rdcon/ode_output.o ../rdcon/ode_output.o: ../rdcon/dcon_mod.o ../rdcon/sing.o ../rdcon/rdcon_netcdf.o: ../equil/local.o ../rdcon/dcon_mod.o @@ -144,11 +143,10 @@ ../stride/debug.o: ../equil/local.o ../stride/fourfit.o: ../equil/fspline.o ../equil/global.o ../stride/dcon_mod.o ../stride/free.o: ../stride/ode.o ../stride/stride_netcdf.o ../vacuum/vacuum_ma.o -../stride/mercier.o: ../stride/dcon_mod.o ../stride/ode.o: ../stride/debug.o ../stride/riccati.o ../stride/sing.o ../stride/zvode1.o ../stride/riccati.o: ../stride/sing.o ../stride/zvode1.o ../stride/sing.o: ../stride/fourfit.o -../stride/stride.o: ../equil/equil.o ../equil/equil_out.o ../stride/bal.o ../stride/free.o ../stride/mercier.o ../stride/ode.o +../stride/stride.o: ../equil/equil.o ../equil/equil_out.o ../stride/bal.o ../stride/free.o ../stride/ode.o ../stride/stride_netcdf.o: ../stride/dcon_mod.o # Match diff --git a/install/RULES.inc b/install/RULES.inc index 392ed4818..43cbcd3da 100644 --- a/install/RULES.inc +++ b/install/RULES.inc @@ -150,4 +150,4 @@ CCXDRAW = $(CC) $(X11_INC_DIR) $(CFLAGS) -DUNIX -O2 ../xdraw/%.o: ../xdraw/%.c $(CCXDRAW) -c $< -o $@ ../xdraw/%.o: ../xdraw/%.C - $(CCXDRAW) -c $< -o $@ \ No newline at end of file + $(CCXDRAW) -c $< -o $@ diff --git a/rdcon/bal.f b/rdcon/bal.f index 3eb3bc693..ec5fc0b88 100644 --- a/rdcon/bal.f +++ b/rdcon/bal.f @@ -1,540 +1,440 @@ -c----------------------------------------------------------------------- -c file bal.f. -c determines high-n ideal marginal MHD ballooning stability. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c code organization. -c----------------------------------------------------------------------- -c 0. rdcon_bal_mod. -c 1. bal_scan. -c 2. bal_prep. -c 3. bal_der. -c 4. bal_int. -c 5. bal_pse. -c----------------------------------------------------------------------- -c subprogram 0. rdcon_bal_mod. -c module declarations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - MODULE rdcon_bal_mod - USE rdcon_mod - IMPLICIT NONE - - INTEGER, PRIVATE :: ipsi - REAL(r8), PRIVATE :: di,alpha,theta0=0 - REAL(r8), DIMENSION(2,2), PRIVATE :: v0 - TYPE(spline_type), PRIVATE :: bf,bg - - CONTAINS -c----------------------------------------------------------------------- -c subprogram 1. bal_scan. -c computes ballooning stability criterion. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_scan -c----------------------------------------------------------------------- -c open output files. -c----------------------------------------------------------------------- - IF(out_bal1)CALL ascii_open(out_bal1_unit,"bal1.out","UNKNOWN") - IF(out_bal2)CALL ascii_open(out_bal2_unit,"bal2.out","UNKNOWN") - IF(bin_bal1)CALL bin_open(bin_bal1_unit,"bal1.bin","UNKNOWN", - $ "REWIND","none") - IF(bin_bal2)CALL bin_open(bin_bal2_unit,"bal2.bin","UNKNOWN", - $ "REWIND","none") -c----------------------------------------------------------------------- -c test ballooning stability. -c----------------------------------------------------------------------- - DO ipsi=0,mpsi - CALL bal_prep - IF(di <= 0 .AND. di >= -1e4 .AND. sq%xs(ipsi) <= 1)CALL bal_int - CALL spline_dealloc(bf) - CALL spline_dealloc(bg) - ENDDO -c----------------------------------------------------------------------- -c close output files. -c----------------------------------------------------------------------- - IF(out_bal1)CALL ascii_close(out_bal1_unit) - IF(out_bal2)CALL ascii_close(out_bal2_unit) - IF(bin_bal1)CALL bin_close(bin_bal1_unit) - IF(bin_bal2)CALL bin_close(bin_bal2_unit) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_scan -c----------------------------------------------------------------------- -c subprogram 2. bal_prep. -c computes coefficients for the ideal marginal ballooning equations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_prep - - LOGICAL, PARAMETER :: inverse_flag=.FALSE. - INTEGER :: itheta,info - INTEGER, DIMENSION(3) :: ipiv - REAL(r8) :: chi1,det,eta,fexp,p1,psifac,q,q1,r,rfac,theta, - $ twopif,v1 - REAL(r8), DIMENSION(2,2) :: c0,d0bar,d,v10 - REAL(r8), DIMENSION(2,2,0:mtheta) :: d1 - REAL(r8), DIMENSION(0:mtheta) :: b1,bsq,dbdb0,dbdb1,dbdb2, - $ kappan,kappas,jac,jacfac - REAL(r8), DIMENSION(3,3) :: v,w,temp - TYPE(spline_type) :: spl0,spl1,spl2,spl3 -c----------------------------------------------------------------------- -c compute surface quantities. -c----------------------------------------------------------------------- - psifac=sq%xs(ipsi) - twopif=sq%fs(ipsi,1) - p1=sq%fs1(ipsi,2) - v1=sq%fs(ipsi,3) - q=sq%fs(ipsi,4) - q1=sq%fs1(ipsi,4) - chi1=twopi*psio - v=0 - w=0 -c----------------------------------------------------------------------- -c compute coordinates and jacobian. -c----------------------------------------------------------------------- - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - theta=rzphi%ys(itheta) - rfac=SQRT(rzphi%f(1)) - eta=twopi*(theta+rzphi%f(2)) - r=ro+rfac*COS(eta) - jac(itheta)=rzphi%f(4) -c----------------------------------------------------------------------- -c compute contravariant basis vectors. -c----------------------------------------------------------------------- - v(1,1)=rzphi%fx(1)/(2*rfac*jac(itheta)) - v(1,2)=rzphi%fx(2)*twopi*rfac/jac(itheta) - v(1,3)=rzphi%fx(3)*r/jac(itheta) - v(2,1)=rzphi%fy(1)/(2*rfac*jac(itheta)) - v(2,2)=(1+rzphi%fy(2))*twopi*rfac/jac(itheta) - v(2,3)=rzphi%fy(3)*r/jac(itheta) - v(3,3)=twopi*r/jac(itheta) -c----------------------------------------------------------------------- -c compute covariant basis vectors, inverse method. -c----------------------------------------------------------------------- - IF(inverse_flag)THEN - temp=TRANSPOSE(v)*jac(itheta) - w=RESHAPE((/1,0,0,0,1,0,0,0,1/),(/3,3/)) - CALL dgetrf(3,3,temp,3,ipiv,info) - CALL dgetrs('N',3,3,temp,3,ipiv,w,3,info) -c----------------------------------------------------------------------- -c compute covariant basis vectors, direct method. -c----------------------------------------------------------------------- - ELSE - w(1,1)=(1+rzphi%fy(2))*twopi**2*rfac*r - $ /jac(itheta) - w(1,2)=-rzphi%fy(1)*pi*r/(rfac*jac(itheta)) - w(2,1)=-rzphi%fx(2)*twopi**2*r*rfac/jac(itheta) - w(2,2)=rzphi%fx(1)*pi*r/(rfac*jac(itheta)) - w(3,1)=(rzphi%fx(2)*rzphi%fy(3) - $ -rzphi%fx(3)*(1+rzphi%fy(2))) - $ *twopi*r*rfac/jac(itheta) - w(3,2)=(rzphi%fx(3)*rzphi%fy(1) - $ -rzphi%fx(1)*rzphi%fy(3)) - $ *r/(2*rfac*jac(itheta)) - w(3,3)=1/(twopi*r) - ENDIF -c----------------------------------------------------------------------- -c store physical quantities. -c----------------------------------------------------------------------- - b1(itheta)=SUM((v(2,:)+q*v(3,:))*v(1,:))*chi1 - bsq(itheta)=SUM((v(2,:)+q*v(3,:))**2)*chi1**2 - dbdb0(itheta)=SUM(w(1,:)**2)*q1**2 - dbdb1(itheta)=-SUM((w(3,:)-q*w(2,:))*w(1,:))*2*q1 - dbdb2(itheta)=SUM((w(3,:)-q*w(2,:))**2) - ENDDO -c----------------------------------------------------------------------- -c compute curvature terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl0,mtheta,2) - spl0%xs=rzphi%ys - spl0%fs(:,1)=1/bsq - spl0%fs(:,2)=jac*b1/bsq - CALL spline_fit(spl0,"periodic") - kappas=-spl0%fs1(:,1)*twopif/(2*jac) - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - kappan(itheta)=((p1/bsq(itheta)-rzphi%fx(4)/jac(itheta))/chi1 - $ +twopif*q1/(bsq(itheta)*jac(itheta))+spl0%fs1(itheta,2) - $ /jac(itheta))/2 - ENDDO - CALL spline_dealloc(spl0) -c----------------------------------------------------------------------- -c compute coefficients for ballooning equation. -c----------------------------------------------------------------------- - CALL spline_alloc(bf,mtheta,5) - bf%xs=rzphi%ys - jacfac=jac/chi1 - bf%fs(:,1)=dbdb0/(bsq*jacfac) - bf%fs(:,2)=dbdb1/(2*dbdb0) - bf%fs(:,3)=(dbdb2-dbdb1**2/(4*dbdb0))/(bsq*jacfac) - bf%fs(:,4)=2*kappan*p1/chi1*jacfac - bf%fs(:,5)=-2*kappas*p1/chi1*q1*jacfac - CALL spline_fit(bf,"periodic") -c----------------------------------------------------------------------- -c diagnose bf. -c----------------------------------------------------------------------- - bf%title=(/"theta ","dbdb0 ","dbdb1 ","dbdb2 ","kappas","kappan"/) - CALL spline_write1(bf,out_bal1,bin_bal1, - $ out_bal1_unit,bin_bal1_unit,.TRUE.) -c----------------------------------------------------------------------- -c initialize bg. -c----------------------------------------------------------------------- - CALL spline_alloc(spl0,mtheta,1) - spl0%xs=rzphi%ys - spl0%fs(:,1)=-p1*q1*twopif/(bsq*chi1**2) - CALL spline_fit(spl0,"periodic") - CALL spline_int(spl0) - CALL spline_alloc(bg,mtheta,5) - bg%xs=rzphi%ys - bg%fs(:,5)=spl0%fs(:,1)-spl0%fsi(mtheta,1) - CALL spline_dealloc(spl0) -c----------------------------------------------------------------------- -c compute c0. -c----------------------------------------------------------------------- - CALL spline_alloc(spl1,mtheta,4) - spl1%xs=rzphi%ys - DO itheta=0,mtheta - c0(1,1)=.5 - c0(1,2)=1/bf%fs(itheta,1) - c0(2,1)=-bf%fs(itheta,4) - c0(2,2)=-.5 -c----------------------------------------------------------------------- -c compute d0. -c----------------------------------------------------------------------- - spl1%fs(itheta,1)=c0(1,1)+c0(1,2)*bg%fs(itheta,5) - spl1%fs(itheta,2)=c0(1,2) - spl1%fs(itheta,3)=c0(2,1) - $ +(c0(2,2)-c0(1,1)-c0(1,2)*bg%fs(itheta,5))*bg%fs(itheta,5) - spl1%fs(itheta,4)=-spl1%fs(itheta,1) -c----------------------------------------------------------------------- -c compute d1. -c----------------------------------------------------------------------- - fexp=-c0(1,2)*bf%fs(itheta,2)*2 - d1(1,1,itheta)=fexp*bg%fs(itheta,5) - d1(1,2,itheta)=fexp - d1(2,1,itheta)=-fexp*bg%fs(itheta,5)**2 - d1(2,2,itheta)=-d1(1,1,itheta) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic splines and write output. -c----------------------------------------------------------------------- - CALL spline_fit(spl1,"periodic") - CALL spline_int(spl1) - d0bar(1,1)=spl1%fsi(mtheta,1) - d0bar(1,2)=spl1%fsi(mtheta,2) - d0bar(2,1)=spl1%fsi(mtheta,3) - d0bar(2,2)=spl1%fsi(mtheta,4) -c----------------------------------------------------------------------- -c compute mercier criterion and alpha. -c----------------------------------------------------------------------- - di=d0bar(1,1)*d0bar(2,2)-d0bar(1,2)*d0bar(2,1) - IF(di > 0)THEN - CALL spline_dealloc(spl1) - RETURN - ENDIF - alpha=SQRT(-di) -c----------------------------------------------------------------------- -c compute zeroth-order eigenfunctions. -c----------------------------------------------------------------------- - v0(1,1)=1 - v0(1,2)=1 - v0(2,1)=-(d0bar(1,1)-alpha)/d0bar(1,2) - v0(2,2)=-(d0bar(1,1)+alpha)/d0bar(1,2) -c----------------------------------------------------------------------- -c store derivatives for first-order terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl2,mtheta,4) - spl2%xs=rzphi%ys - DO itheta=0,mtheta - spl2%fs(itheta,1) - $ =(spl1%fs(itheta,1)-alpha)*v0(1,1) - $ +spl1%fs(itheta,2)*v0(2,1) - spl2%fs(itheta,2) - $ =spl1%fs(itheta,3)*v0(1,1) - $ +(spl1%fs(itheta,4)-alpha)*v0(2,1) - spl2%fs(itheta,3) - $ =(spl1%fs(itheta,1)+alpha)*v0(1,2) - $ +spl1%fs(itheta,2)*v0(2,2) - spl2%fs(itheta,4) - $ =spl1%fs(itheta,3)*v0(1,2) - $ +(spl1%fs(itheta,4)+alpha)*v0(2,2) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic spines and integrate. -c----------------------------------------------------------------------- - CALL spline_fit(spl2,"periodic") - CALL spline_int(spl2) - spl2%fs=spl2%fsi -c----------------------------------------------------------------------- -c store derivatives for second-order terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl3,mtheta,4) - spl3%xs=rzphi%ys - DO itheta=0,mtheta - spl3%fs(itheta,1) - $ =(spl1%fs1(itheta,1)+1-alpha)*spl2%fs(itheta,1) - $ +spl1%fs(itheta,2)*spl2%fs(itheta,2) - $ +d1(1,1,itheta)*v0(1,1)+d1(1,2,itheta)*v0(2,1) - spl3%fs(itheta,2) - $ =spl1%fs(itheta,3)*spl2%fs(itheta,1) - $ +(spl1%fs(itheta,4)+1-alpha)*spl2%fs(itheta,2) - $ +d1(2,1,itheta)*v0(1,1)+d1(2,2,itheta)*v0(2,1) - spl3%fs(itheta,3) - $ =(spl1%fs(itheta,1)+1+alpha)*spl2%fs(itheta,3) - $ +spl1%fs(itheta,2)*spl2%fs(itheta,4) - $ +d1(1,1,itheta)*v0(1,2)+d1(1,2,itheta)*v0(2,2) - spl3%fs(itheta,4) - $ =spl1%fs(itheta,3)*spl2%fs(itheta,3) - $ +(spl1%fs(itheta,4)+1+alpha)*spl2%fs(itheta,4) - $ +d1(2,1,itheta)*v0(1,2)+d1(2,2,itheta)*v0(2,2) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic spines and integrate. -c----------------------------------------------------------------------- - CALL spline_fit(spl3,"periodic") - CALL spline_int(spl3) -c----------------------------------------------------------------------- -c compute first order constants, first eigenfunction. -c----------------------------------------------------------------------- - d(2,1)=d0bar(2,1) - d(1,2)=d0bar(1,2) - d(1,1)=d0bar(1,1)+1-alpha - d(2,2)=d0bar(2,2)+1-alpha - det=d(1,1)*d(2,2)-d(1,2)*d(2,1) - v10(1,1)=(d(1,2)*spl3%fsi(mtheta,2)-d(2,2)*spl3%fsi(mtheta,1))/det - v10(2,1)=(d(2,1)*spl3%fsi(mtheta,1)-d(1,1)*spl3%fsi(mtheta,2))/det -c----------------------------------------------------------------------- -c compute first order constants, second eigenfunction. -c----------------------------------------------------------------------- - d(1,1)=d0bar(1,1)+1+alpha - d(2,2)=d0bar(2,2)+1+alpha - det=d(1,1)*d(2,2)-d(1,2)*d(2,1) - v10(1,2)=(d(1,2)*spl3%fsi(mtheta,4)-d(2,2)*spl3%fsi(mtheta,3))/det - v10(2,2)=(d(2,1)*spl3%fsi(mtheta,3)-d(1,1)*spl3%fsi(mtheta,4))/det -c----------------------------------------------------------------------- -c add constants to v1 and fit to cubic splines. -c----------------------------------------------------------------------- - bg%fs(:,1)=spl2%fs(:,1)+v10(1,1) - bg%fs(:,2)=spl2%fs(:,2)+v10(2,1) - bg%fs(:,3)=spl2%fs(:,3)+v10(1,2) - bg%fs(:,4)=spl2%fs(:,4)+v10(2,2) - CALL spline_fit(bg,"periodic") -c----------------------------------------------------------------------- -c diagnose bg. -c----------------------------------------------------------------------- -c bg%title=(/" bg1 "," bg2 "," bg2 "," bg2 "," bg2 "/) -c CALL spline_write1(bg,out_bal1,bin_bal1, -c $ out_bal1_unit,bin_bal1_unit,.FALSE.) -c----------------------------------------------------------------------- -c deallocate local splines. -c----------------------------------------------------------------------- - CALL spline_dealloc(spl1) - CALL spline_dealloc(spl2) - CALL spline_dealloc(spl3) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_prep -c----------------------------------------------------------------------- -c subprogram 3. bal_der -c differential equations for the marginal ballooning. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_der(neq,theta,y,dy) - - INTEGER, INTENT(IN) :: neq - REAL(r8), INTENT(IN) :: theta - REAL(r8), DIMENSION(neq), INTENT(IN) :: y - REAL(r8), DIMENSION(neq), INTENT(OUT) :: dy - - REAL(r8) :: dbdb,kappaw,thfac -c----------------------------------------------------------------------- -c compute coefficients. -c----------------------------------------------------------------------- - CALL spline_eval(bf,theta,0) - thfac=theta-theta0 - dbdb=bf%f(1)*(thfac+bf%f(2))**2+bf%f(3) - kappaw=bf%f(4)+thfac*bf%f(5) -c----------------------------------------------------------------------- -c compute derivatives. -c----------------------------------------------------------------------- - dy(1)=y(2)/dbdb - dy(2)=-y(1)*kappaw -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_der -c----------------------------------------------------------------------- -c subprogram 4. bal_int -c integrates the ideal marginal ballooning equations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_int - - INTEGER, PARAMETER :: neq=2,liw=20,lrw=22+neq*16 - INTEGER, DIMENSION(liw) :: iwork=0 - INTEGER :: iopt,iside,istate,itask,itol,jac,mf - INTEGER :: istep,nstep=HUGE(0),icross - REAL(r8), PARAMETER :: tol=1e-5,thmin=1,hu_min=1e-10 - REAL(r8) :: thmax,atol,ca1,ca2,det,rtol,theta,thout - REAL(r8), DIMENSION(lrw) :: rwork=0 - REAL(r8), DIMENSION(neq) :: y,dy - REAL(r8), DIMENSION(2,2) :: u -c----------------------------------------------------------------------- -c write formats. -c----------------------------------------------------------------------- - 10 FORMAT(/2x,"ist",2x,"nqu",1x,"icr",4x,"theta",6x,"dtheta",4x, - $ "asinh y1",2x,"asinh ca1",2x,"asinh ca2"/) - 20 FORMAT(i5,2i4,1x,1p,5e11.3) -c----------------------------------------------------------------------- -c initialize independent variables. -c----------------------------------------------------------------------- - thmax=SQRT(MAXVAL(ABS(bf%fs(:,3)/bf%fs(:,1))))*10*thmax0 - thmax=MIN(thmax,100._r8) - theta=-thmax - thout=thmax - istep=0 -c----------------------------------------------------------------------- -c compute equilibrium quantities and asymptotic solutions. -c----------------------------------------------------------------------- - CALL bal_pse(theta,u) - y(1)=u(1,2)*sinh(1.) - y(2)=u(2,2)*sinh(1.) -c----------------------------------------------------------------------- -c set up integrator parameters. -c----------------------------------------------------------------------- - icross=0 - iside=1 - istate=1 - itask=5 - mf=10 - rwork(1)=thout - iopt=1 - itol=1 - rtol=tol - atol=tol*tol -c----------------------------------------------------------------------- -c write header. -c----------------------------------------------------------------------- - IF(out_bal2)WRITE(out_bal2_unit,10) -c----------------------------------------------------------------------- -c check for crossings. -c----------------------------------------------------------------------- - DO - IF(iside*y(1) < 0)THEN - icross=icross+1 - iside=-iside - ENDIF -c----------------------------------------------------------------------- -c compute and print asymptotic coefficient. -c----------------------------------------------------------------------- - IF(out_bal2 .OR. bin_bal2)THEN - IF(ABS(theta) > thmin)THEN - CALL bal_pse(theta,u) - det=u(1,1)*u(2,2)-u(1,2)*u(2,1) - ca1=(y(1)*u(2,2)-y(2)*u(1,2))/det - ca2=(y(2)*u(1,1)-y(1)*u(2,1))/det - CALL bal_der(neq,theta,y,dy) - IF(out_bal2) - $ WRITE(out_bal2_unit,20)istep,iwork(14),icross, - $ theta,rwork(11),asinh(y(1)),asinh(ca1),asinh(ca2) - IF(bin_bal2) - $ WRITE(bin_bal2_unit)REAL(theta,4), - $ REAL(asinh(y(1)),4),REAL(asinh(y(2)),4), - $ REAL(y(2)/dy(1),4),REAL(dy(2)/y(1),4), - $ REAL(asinh(ca1),4),REAL(asinh(ca2),4),REAL(u,4) - ELSE - ca1=0 - ca2=0 - det=0 - ENDIF - ENDIF -c----------------------------------------------------------------------- -c advance. -c----------------------------------------------------------------------- - IF(istep >= nstep .OR. theta >= thmax .OR. istate < 0)EXIT - istep=istep+1 - CALL lsode(bal_der,neq,y,theta,thout,itol,rtol,atol,itask, - $ istate,iopt,rwork,lrw,iwork,liw,jac,mf) - IF(rwork(11) < hu_min)EXIT - ENDDO -c----------------------------------------------------------------------- -c write trailer. -c----------------------------------------------------------------------- - IF(out_bal2)WRITE(out_bal2_unit,10) - IF(bin_bal2)WRITE(bin_bal2_unit) -c----------------------------------------------------------------------- -c compute and store final asymptotic coefficients. -c----------------------------------------------------------------------- - IF(theta >= thmax)THEN - CALL bal_pse(theta,u) - det=u(1,1)*u(2,2)-u(1,2)*u(2,1) - ca1=(y(1)*u(2,2)-y(2)*u(1,2))/det - ca2=(y(2)*u(1,1)-y(1)*u(2,1))/det - locstab%fs(ipsi,4:5)=(/ca1,ca2/) - ENDIF -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_int -c----------------------------------------------------------------------- -c subprogram 5. bal_pse. -c computes alpha series solutions. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_pse(theta,u) - - REAL(r8), INTENT(IN) :: theta - REAL(r8), INTENT(OUT), DIMENSION(2,2) :: u - - REAL(r8) :: dtheta,thfac - REAL(r8), DIMENSION(2,2) :: v -c----------------------------------------------------------------------- -c compute alpha series. -c----------------------------------------------------------------------- - CALL spline_eval(bg,theta,0) - dtheta=theta-theta0 - v(1,1)=v0(1,1)+bg%f(1)/dtheta - v(2,1)=v0(2,1)+bg%f(2)/dtheta - v(1,2)=v0(1,2)+bg%f(3)/dtheta - v(2,2)=v0(2,2)+bg%f(4)/dtheta -c----------------------------------------------------------------------- -c compute transformed solutions. -c----------------------------------------------------------------------- - u(1,1)=v(1,1) - u(1,2)=v(1,2) - u(2,1)=bg%f(5)*v(1,1)+v(2,1) - u(2,2)=bg%f(5)*v(1,2)+v(2,2) -c----------------------------------------------------------------------- -c multiply by alphas. -c----------------------------------------------------------------------- - thfac=ABS(dtheta)**(alpha+.5)/dtheta - u(1,1)=u(1,1)*thfac - u(2,1)=u(2,1)*thfac*dtheta - u(1,2)=u(1,2)/(thfac*dtheta) - u(2,2)=u(2,2)/thfac -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_pse - END MODULE rdcon_bal_mod + module rdcon_bal_mod + use rdcon_mod + use, intrinsic :: ieee_arithmetic + implicit none + + integer, private :: ipsi + real(r8), private :: theta_max + type(spline_type), private :: bf + + contains + + subroutine bal_scan + real(r8) :: di, h, psi, delta_prime + real(r8), dimension(0:mpsi) :: volume + type(spline_type) :: volume_spline + + volume = 0.0_r8 + if (bal_flag) then + call spline_alloc(volume_spline,mpsi,1) + volume_spline%xs = sq%xs + volume_spline%fs(:,1) = sq%fs(:,3) + volume_spline%fs1(:,1) = sq%fs1(:,3) + call spline_int(volume_spline) + volume = max(volume_spline%fsi(:,1),0.0_r8) + call spline_dealloc(volume_spline) + end if + + do ipsi = 0, mpsi + psi = sq%xs(ipsi) + call bal_prep(di,h,0.0_r8) + locstab%fs(ipsi,1) = di * psi + locstab%fs(ipsi,2) = (di + (h - 0.5_r8)**2) * psi + locstab%fs(ipsi,3) = h + locstab%fs(ipsi,4) = ieee_value(0.0_r8,ieee_quiet_nan) + locstab%fs(ipsi,5) = ieee_value(0.0_r8,ieee_quiet_nan) + locstab%fs(ipsi,6) = ieee_value(0.0_r8,ieee_quiet_nan) + if (bal_flag .and. psi <= 1.0_r8) then + call bal_int(delta_prime) + locstab%fs(ipsi,4) = delta_prime + end if + call spline_dealloc(bf) + if (bal_flag) call bal_alpha_first_boundary(volume(ipsi)) + end do + end subroutine bal_scan + + subroutine bal_prep(di,h,corr_pprime) + real(r8), intent(out) :: di, h + real(r8), intent(in) :: corr_pprime + + integer :: itheta + real(r8) :: psi, twopif, p1, v1, q, q1, chi1 + real(r8) :: theta, rho, eta, ceta, seta, r, jac + real(r8) :: drfac_dp, eta_dp, dr_dp + real(r8) :: dv21_dp, dv22_dp, dv23_dp, dv33_dp + real(r8) :: iper_ref, local_scale, cscale + real(r8) :: abar_w, bbar_w, response_denom + real(r8) :: dbase(3), dB(3), bvec(3) + real(r8) :: gradpsi(3), gradtheta(3), gradzeta(3) + real(r8) :: v(3,3), w(3,3) + real(r8) :: d0bar(2,2) + real(r8), dimension(0:mtheta) :: bsq, gradpsi_sq, dpsisq, jacarr + real(r8), dimension(0:mtheta) :: bdot_theta_phi, iper, htheta + real(r8), dimension(0:mtheta) :: dbsq_dtheta, kappas + real(r8), dimension(0:mtheta) :: jac_psi, bsq_psi + real(r8), dimension(0:mtheta) :: spl1_vals, spl1_deriv + real(r8), dimension(0:mtheta) :: a0, a1, a2 + real(r8), dimension(0:mtheta) :: kappaw0, kappaw1 + real(r8), dimension(0:mtheta) :: aw, bw, aperp + real(r8), dimension(0:mtheta) :: diper_dtheta, iper_perturbed + real(r8), dimension(0:mtheta) :: jacchi, pchi, sigma + real(r8), dimension(0:mtheta) :: m012, m021 + real(r8), dimension(0:mtheta,3) :: hfs + real(r8), dimension(0:mtheta,4) :: n0 + real(r8), dimension(0:mtheta,7) :: coeff + real(r8), dimension(0:mtheta,4) :: f0, fx0, fy0, fxy0 + real(r8), dimension(0:mtheta) :: rho0, eta0, r0 + real(r8), dimension(0:mtheta,3,3) :: v0 + real(r8), dimension(0:mtheta,3) :: vzp0, vpt0 + type(spline_type) :: work + + psi = sq%xs(ipsi) + twopif = sq%fs(ipsi,1) + p1 = sq%fs1(ipsi,2) + v1 = sq%fs(ipsi,3) + q = sq%fs(ipsi,4) + q1 = sq%fs1(ipsi,4) + chi1 = twopi * psio + + do itheta = 0, mtheta + theta = rzphi%ys(itheta) + call bicube_eval(rzphi, rzphi%xs(ipsi), theta, 2) + f0(itheta,:) = rzphi%f + fx0(itheta,:) = rzphi%fx + fy0(itheta,:) = rzphi%fy + fxy0(itheta,:) = rzphi%fxy + + rho = sqrt(f0(itheta,1)) + eta = twopi * (theta + f0(itheta,2)) + ceta = cos(eta) + seta = sin(eta) + r = ro + rho * ceta + jac = f0(itheta,4) + rho0(itheta) = rho + eta0(itheta) = eta + r0(itheta) = r + jacarr(itheta) = jac + + v = 0.0_r8 + v(1,1) = fx0(itheta,1) / (2.0_r8*rho*jac) + v(1,2) = fx0(itheta,2) * twopi*rho / jac + v(1,3) = fx0(itheta,3) * r / jac + v(2,1) = fy0(itheta,1) / (2.0_r8*rho*jac) + v(2,2) = (1.0_r8 + fy0(itheta,2)) * twopi*rho / jac + v(2,3) = fy0(itheta,3) * r / jac + v(3,3) = twopi*r / jac + + w = 0.0_r8 + w(1,1) = (1.0_r8 + fy0(itheta,2))*twopi**2*rho*r/jac + w(1,2) = -fy0(itheta,1)*pi*r/(rho*jac) + w(2,1) = -fx0(itheta,2)*twopi**2*r*rho/jac + w(2,2) = fx0(itheta,1)*pi*r/(rho*jac) + w(3,1) = (fx0(itheta,2)*fy0(itheta,3) - + $fx0(itheta,3)*(1.0_r8 + fy0(itheta,2)))*twopi*r*rho/jac + w(3,2) = (fx0(itheta,3)*fy0(itheta,1) - + $fx0(itheta,1)*fy0(itheta,3))*r/(2.0_r8*rho*jac) + w(3,3) = 1.0_r8/(twopi*r) + + gradpsi = w(1,:) + gradtheta = w(2,:) + gradzeta = w(3,:) + bvec = (v(2,:) + q*v(3,:))*chi1 + bsq(itheta) = dot_product(bvec,bvec) + gradpsi_sq(itheta) = dot_product(gradpsi,gradpsi) + dpsisq(itheta) = (twopi*r)**2 * + $(v(2,1)**2 + v(2,2)**2) + iper(itheta) = dot_product(q*gradtheta-gradzeta,gradpsi) / + $gradpsi_sq(itheta) + htheta(itheta) = fy0(itheta,3)/twopi + bdot_theta_phi(itheta) = dot_product(bvec,v(1,:)) + v0(itheta,:,:) = v + vzp0(itheta,:) = v(2,:) + vpt0(itheta,:) = v(3,:) + end do + + call periodic_lowpass_derivative(bsq, rzphi%ys, 16, dbsq_dtheta) + kappas = dbsq_dtheta*twopif/(2.0_r8*jacarr*bsq**2) + + do itheta = 0, mtheta + jac_psi(itheta) = fx0(itheta,4) + drfac_dp = fx0(itheta,1)/(2.0_r8*rho0(itheta)) + eta_dp = twopi*fx0(itheta,2) + dr_dp = drfac_dp*cos(eta0(itheta)) - + $rho0(itheta)*sin(eta0(itheta))*eta_dp + + dv21_dp = (fxy0(itheta,1)/(2.0_r8*rho0(itheta)) - + $fx0(itheta,1)*fy0(itheta,1)/(4.0_r8*f0(itheta,1)* + $rho0(itheta)))/jacarr(itheta) - + $v0(itheta,2,1)*jac_psi(itheta)/jacarr(itheta) + dv22_dp = (fxy0(itheta,2)*twopi*rho0(itheta) + + $(1.0_r8+fy0(itheta,2))*twopi*drfac_dp)/jacarr(itheta) - + $v0(itheta,2,2)*jac_psi(itheta)/jacarr(itheta) + dv23_dp = (fxy0(itheta,3)*r0(itheta) + + $fy0(itheta,3)*dr_dp)/jacarr(itheta) - + $v0(itheta,2,3)*jac_psi(itheta)/jacarr(itheta) + dv33_dp = twopi*dr_dp/jacarr(itheta) - + $v0(itheta,3,3)*jac_psi(itheta)/jacarr(itheta) + + dbase = (/dv21_dp,dv22_dp,dv23_dp/) + + $q*(/0.0_r8,0.0_r8,dv33_dp/) + q1*v0(itheta,3,:) + dB = dbase*chi1 + bsq_psi(itheta) = 2.0_r8*dot_product( + $vzp0(itheta,:)+q*vpt0(itheta,:),dB)*chi1 + end do + + spl1_vals = jacarr*bdot_theta_phi/sqrt(bsq) + call periodic_lowpass_derivative(spl1_vals, rzphi%ys, 32, + $spl1_deriv) + kappaw0 = -(jac_psi/jacarr + 0.5_r8*bsq_psi/bsq)/chi1 + + $spl1_deriv/(jacarr*sqrt(bsq)) + twopif*q1/(bsq*jacarr) + +c corr_pprime also drives the I_per response. This is the +c corr_qprime=0 specialization of Julia's response calculation. + aw = twopi**2*r0**2/(chi1**2*gradpsi_sq) + bw = twopif**2/(chi1**2*gradpsi_sq) + hfs(:,1) = (q+htheta)*aw + hfs(:,2) = (q+htheta)*bw + hfs(mtheta,1:2) = hfs(0,1:2) + call spline_alloc(work,mtheta,2) + work%xs = rzphi%ys + work%fs = hfs(:,1:2) + call spline_fit(work,"periodic") + call spline_int(work) + abar_w = work%fsi(mtheta,1)/q + bbar_w = work%fsi(mtheta,2)/q + call spline_dealloc(work) + response_denom = 1.0_r8+bbar_w + aperp = (q+htheta)*((aw-abar_w) - + $abar_w/response_denom*(bw-bbar_w)) + diper_dtheta = aperp*corr_pprime + diper_dtheta(mtheta) = diper_dtheta(0) + call spline_alloc(work,mtheta,1) + work%xs = rzphi%ys + work%fs(:,1) = diper_dtheta + call spline_fit(work,"periodic") + call spline_int(work) + iper_perturbed = work%fsi(:,1) + call spline_dealloc(work) + + iper_ref = iper(0) + iper = iper - iper_ref + iper_perturbed + p1 = p1+corr_pprime + kappaw0 = kappaw0-kappas*iper_perturbed + a0 = 1.0_r8/(chi1**2*gradpsi_sq) + + $gradpsi_sq/bsq*iper**2 + a1 = gradpsi_sq/bsq*(2.0_r8*q1*iper) + a2 = gradpsi_sq/bsq*q1**2 + jacchi = jacarr/chi1 + pchi = p1/chi1 + kappaw1 = q1*kappas + + coeff(:,1) = a0 + coeff(:,2) = a1 + coeff(:,3) = a2 + coeff(:,4) = kappaw0 + coeff(:,5) = kappaw1 + coeff(:,6) = jacchi + coeff(:,7) = pchi + + theta_max = 0.0_r8 + do itheta = 0, mtheta + if (abs(a2(itheta)) > epsilon(1.0_r8)) then + cscale = a0(itheta)-a1(itheta)**2/(4.0_r8*a2(itheta)) + local_scale = sqrt(abs(cscale/a2(itheta))) + if (ieee_is_finite(local_scale)) + $theta_max = max(theta_max,local_scale) + end if + end do + if (theta_max <= 0.0_r8) theta_max = 16.5_r8 + theta_max = min(16.5_r8,10.0_r8*theta_max) + theta_max = max(theta_max,1.0e-2_r8) + + sigma = -(twopif*p1*q1)/(chi1**2*bsq) + m012 = jacchi/a2 + m021 = -2.0_r8*jacchi*pchi*kappaw0 + n0(:,1) = 0.5_r8 + m012*sigma + n0(:,2) = m012 + n0(:,3) = m021 - sigma - m012*sigma**2 + n0(:,4) = -0.5_r8 - m012*sigma + n0(mtheta,:) = n0(0,:) + + call spline_alloc(work,mtheta,4) + work%xs = rzphi%ys + work%fs = n0 + call spline_fit(work,"periodic") + call spline_int(work) + d0bar(1,1) = work%fsi(mtheta,1) + d0bar(1,2) = work%fsi(mtheta,2) + d0bar(2,1) = work%fsi(mtheta,3) + d0bar(2,2) = work%fsi(mtheta,4) + call spline_dealloc(work) + di = d0bar(1,1)*d0bar(2,2)-d0bar(1,2)*d0bar(2,1) + + hfs(:,1) = bsq/dpsisq*jacarr/v1 + hfs(:,2) = 1.0_r8/dpsisq*jacarr/v1 + hfs(:,3) = bsq*jacarr/v1 + hfs(mtheta,:) = hfs(0,:) + call spline_alloc(work,mtheta,3) + work%xs = rzphi%ys + work%fs = hfs + call spline_fit(work,"periodic") + call spline_int(work) + h = twopif*p1*v1/(q1*chi1**3)*(work%fsi(mtheta,2) - + $work%fsi(mtheta,1)/work%fsi(mtheta,3)) + call spline_dealloc(work) + + coeff(mtheta,:) = coeff(0,:) + call spline_alloc(bf,mtheta,7) + bf%xs = rzphi%ys + bf%fs = coeff + call spline_fit(bf,"periodic") + end subroutine bal_prep + + subroutine periodic_lowpass_derivative(vals,xs,nkeep,deriv) + real(r8), intent(in) :: vals(0:), xs(0:) + integer, intent(in) :: nkeep + real(r8), intent(out) :: deriv(0:) + integer :: n, kmax, j, l, k + real(r8) :: period, omega, phase, ak, bk + + n = ubound(vals,1) + period = xs(n)-xs(0) + kmax = min(nkeep,(n-1)/2) + deriv = 0.0_r8 + do k = 1, kmax + ak = 0.0_r8 + bk = 0.0_r8 + do l = 0, n-1 + phase = twopi*real(k*l,r8)/real(n,r8) + ak = ak + vals(l)*cos(phase) + bk = bk + vals(l)*sin(phase) + end do + ak = 2.0_r8*ak/real(n,r8) + bk = 2.0_r8*bk/real(n,r8) + omega = twopi*real(k,r8)/period + do j = 0, n-1 + phase = twopi*real(k*j,r8)/real(n,r8) + deriv(j) = deriv(j) + omega*(-ak*sin(phase)+bk*cos(phase)) + end do + end do + deriv(n) = deriv(0) + end subroutine periodic_lowpass_derivative + + subroutine bal_der(neq,theta,y,dy) + integer, intent(in) :: neq + real(r8), intent(in) :: theta + real(r8), intent(in) :: y(neq) + real(r8), intent(out) :: dy(neq) + real(r8) :: f, kappaw, g, theta_wrapped + + theta_wrapped = modulo(theta-bf%xs(0), + $bf%xs(bf%mx)-bf%xs(0))+bf%xs(0) + call spline_eval(bf,theta_wrapped,0) + f = (bf%f(1)+theta*bf%f(2)+theta**2*bf%f(3))/bf%f(6) + kappaw = bf%f(4)-theta*bf%f(5) + g = -2.0_r8*bf%f(6)*bf%f(7)*kappaw + dy(1) = y(2)/f + dy(2) = g*y(1) + end subroutine bal_der + + subroutine bal_integrate_side(theta_start,theta_end,y,success) + integer, parameter :: neq=2, liw=20, lrw=22+neq*16 + real(r8), intent(in) :: theta_start, theta_end + real(r8), intent(inout) :: y(neq) + logical, intent(out) :: success + integer :: istate, itask, itol, iopt, jac, mf + integer :: iwork(liw) + real(r8) :: theta, rtol, atol, rwork(lrw) + + iwork = 0 + rwork = 0.0_r8 + theta = theta_start + itol = 1 + rtol = 1.0e-8_r8 + atol = 1.0e-16_r8 + itask = 1 + istate = 1 + iopt = 1 + iwork(6) = 100000 + rwork(7) = 1.0e-10_r8 + jac = 0 + mf = 10 + call lsode(bal_der,neq,y,theta,theta_end,itol,rtol,atol,itask, + $istate,iopt,rwork,lrw,iwork,liw,jac,mf) + success = istate == 2 + end subroutine bal_integrate_side + + subroutine bal_int(delta_prime) + real(r8), parameter :: match_point=1.0e-3_r8 + real(r8), intent(out) :: delta_prime + real(r8) :: yl(2), yr(2) + logical :: ok_left, ok_right + + yl = (/0.0_r8,1.0_r8/) + yr = (/0.0_r8,-1.0_r8/) + call bal_integrate_side(-theta_max,-match_point,yl,ok_left) + call bal_integrate_side(theta_max,match_point,yr,ok_right) + if (ok_left .and. ok_right .and. + $abs(yl(1)) > tiny(1.0_r8) .and. + $abs(yr(1)) > tiny(1.0_r8)) then + delta_prime = yr(2)/yr(1)-yl(2)/yl(1) + else + delta_prime = ieee_value(0.0_r8,ieee_quiet_nan) + end if + end subroutine bal_int + + subroutine bal_delta_at_scale(scale,delta_prime) + real(r8), intent(in) :: scale + real(r8), intent(out) :: delta_prime + real(r8) :: corr_pprime, di_dummy, h_dummy + + corr_pprime = sq%fs1(ipsi,2)*(scale-1.0_r8) + call bal_prep(di_dummy,h_dummy,corr_pprime) + call bal_int(delta_prime) + call spline_dealloc(bf) + end subroutine bal_delta_at_scale + + subroutine bal_alpha_first_boundary(volume) + real(r8), intent(in) :: volume + integer, parameter :: n_scan=24 + real(r8), parameter :: pole_cap=3.0_r8 + real(r8), parameter :: max_alpha_scale=8.0_r8 + real(r8), parameter :: alpha_scale_tol=1.0e-3_r8 + integer :: iscan + real(r8) :: alpha_ref, scale, scale_prev, step + real(r8) :: d, d_prev, d_anchor, lo, hi, mid, dm, sign_lo + logical :: have_anchor + + if (sq%xs(ipsi) < 0.1_r8 .or. + $sq%xs(ipsi) > min(0.99_r8,sq%xs(mpsi))) return + alpha_ref = -2.0_r8*sq%fs1(ipsi,2)*sq%fs(ipsi,3) / + $psio**2*sqrt(volume/(twopi*max(ro,epsilon(1.0_r8)))) + locstab%fs(ipsi,6) = alpha_ref + step = max_alpha_scale/real(n_scan,r8) + have_anchor = .false. + d_prev = ieee_value(0.0_r8,ieee_quiet_nan) + scale_prev = 0.0_r8 + + do iscan = 0, n_scan + scale = step*real(iscan,r8) + call bal_delta_at_scale(scale,d) + if (.not. have_anchor) then + if (ieee_is_finite(d) .and. d /= 0.0_r8) then + have_anchor = .true. + d_anchor = abs(d) + end if + else if (ieee_is_finite(d_prev) .and. ieee_is_finite(d) .and. + $sign(1.0_r8,d) /= sign(1.0_r8,d_prev) .and. + $max(abs(d_prev),abs(d)) <= pole_cap*d_anchor) then + lo = scale_prev + hi = scale + sign_lo = sign(1.0_r8,d_prev) + do while (abs(hi-lo) > alpha_scale_tol) + mid = 0.5_r8*(lo+hi) + call bal_delta_at_scale(mid,dm) + if (ieee_is_finite(dm) .and. + $sign(1.0_r8,dm) == sign_lo) then + lo = mid + else + hi = mid + end if + end do + locstab%fs(ipsi,5) = alpha_ref*0.5_r8*(lo+hi) + return + end if + d_prev = d + scale_prev = scale + end do + end subroutine bal_alpha_first_boundary + + end module rdcon_bal_mod diff --git a/rdcon/dcon.f b/rdcon/dcon.f index ff2f55d3a..87beeddb1 100644 --- a/rdcon/dcon.f +++ b/rdcon/dcon.f @@ -23,7 +23,7 @@ MODULE rdcon_run_mod USE equil_mod USE equil_out_mod USE rdcon_bal_mod - USE rdcon_mercier_mod + USE rdcon_mre_mod USE rdcon_ode_mod USE rdcon_free_mod USE rdcon_resist_mod @@ -226,8 +226,9 @@ SUBROUTINE dcon_run COMPLEX(r8), DIMENSION(:,:), ALLOCATABLE :: mx0 COMPLEX(r8), DIMENSION(:), ALLOCATABLE :: vx0 - NAMELIST/rdcon_control/bal_flag,mat_flag,ode_flag,vac_flag, - $ res_flag,fft_flag,node_flag,mthvac,sing_start,nn, + NAMELIST/rdcon_control/bal_flag, + $ mat_flag,ode_flag,vac_flag,res_flag,fft_flag,node_flag, + $ mthvac,sing_start,nn, $ delta_mlow,delta_mhigh,delta_mband,thmax0,nstep,ksing, $ tol_nr,tol_r,crossover,ucrit,singfac_min,singfac_max, $ cyl_flag,dmlim,lim_flag,sas_flag,sing_order,sort_type, @@ -245,7 +246,7 @@ SUBROUTINE dcon_run c format statements. c----------------------------------------------------------------------- 10 FORMAT(/"ipsi",3x,"psifac",6x,"f",7x,"mu0 p",5x,"dvdpsi", - $ 6x,"q",10x,"di",9x,"dr",8x,"ca1"/) + $ 6x,"q",10x,"di",9x,"dr",6x,"bal_dp"/) 20 FORMAT(i4,1p,8e11.3) 30 FORMAT(/3x,"mlow",1x,"mhigh",1x,"mpert",1x,"mband",3x,"nn",2x, $ "lim_fl",2x,"dmlim",7x,"qlim",6x,"psilim"//5i6,l6,1p,3e11.3/) @@ -335,12 +336,12 @@ SUBROUTINE dcon_run c----------------------------------------------------------------------- c prepare local stability criteria. c----------------------------------------------------------------------- - CALL spline_alloc(locstab,mpsi,5) + CALL spline_alloc(locstab,mpsi,6) locstab%xs=sq%xs locstab%fs=0 locstab%name="locstb" - locstab%title=(/" di "," dr "," h "," ca1 "," ca2 "/) - IF(verbose) WRITE(*,*)"Evaluating Mercier criterion" + locstab%title=(/" di "," dr "," h ","bal_dp","alpcrt", + $ " alpha"/) c----------------------------------------------------------------------- c optionally compute modified Rutherford equation (MRE) terms. c----------------------------------------------------------------------- @@ -371,11 +372,9 @@ SUBROUTINE dcon_run mreterms%name="mreterms" IF(verbose) WRITE(*,*)"Evaluating MRE terms" ENDIF - CALL mercier_scan - IF(bal_flag)THEN - IF(verbose) WRITE(*,*)"Evaluating ballooning criterion" - CALL bal_scan - ENDIF + IF(MRE_flag)CALL mre_scan + IF(verbose) WRITE(*,*)"Evaluating local stability" + CALL bal_scan CALL spline_fit(locstab,"extrap") c----------------------------------------------------------------------- c output surface quantities. @@ -397,7 +396,9 @@ SUBROUTINE dcon_run $ REAL(asinh(locstab%fs(ipsi,2)/sq%xs(ipsi)),4), $ REAL(asinh(locstab%fs(ipsi,3)),4), $ REAL(asinh(locstab%fs(ipsi,4)),4), - $ REAL(-sq%fs1(ipsi,1)/twopi,4) + $ REAL(-sq%fs1(ipsi,1)/twopi,4), + $ REAL(locstab%fs(ipsi,6),4), + $ REAL(locstab%fs(ipsi,5),4) ENDDO WRITE(out_unit,10) WRITE(bin_unit) @@ -533,4 +534,3 @@ SUBROUTINE dcon_interface_run(eqin) ENDIF CALL dcon_run END SUBROUTINE dcon_interface_run - diff --git a/rdcon/makefile_db b/rdcon/makefile_db index 31c32ae0d..f81dd96e6 100644 --- a/rdcon/makefile_db +++ b/rdcon/makefile_db @@ -8,6 +8,9 @@ export FFLAGS # for sub-makes .f.o: $(F90) -c $*.f +.f90.o: + $(F90) -c $*.f90 + LIBDIR = ../lib LIBS = \ @@ -22,7 +25,7 @@ OBJS = \ toolbox.o \ jacobi.o \ gal.o \ - mercier.o \ + mre.o \ bal.o \ fourfit.o \ sing1.o \ @@ -50,7 +53,7 @@ dcon_mod.o: ../equil/spline_mod.mod ../equil/global_mod.mod \ ../equil/equil_mod.mod ../equil/equil_out_mod.mod toolbox.o: dcon_mod.o jacobi.o: ../equil/local.o -mercier.o: dcon_mod.o +mre.o: dcon_mod.o bal.o: dcon_mod.o fourfit.o: ../equil/fspline_mod.mod dcon_mod.o toolbox.o sing1.o: fourfit.o @@ -61,7 +64,7 @@ free.o: ode.o resist.o: dcon_mod.o toolbox.o gal.o: jacobi.o sing.o free.o lib_interface.o: ../equil/local.o ../equil/global.o ../equil/utils.o ../equil/inverse.o -dcon.o: bal.o mercier.o ode.o resist.o gal.o lib_interface.o +dcon.o: bal.o mre.o ode.o resist.o gal.o lib_interface.o clean: rm -f *.o *.mod *.out *.bin sum.dat *~ *.diff rdcon diff --git a/rdcon/makefile_lib b/rdcon/makefile_lib index 675ae6327..4e8378c7c 100644 --- a/rdcon/makefile_lib +++ b/rdcon/makefile_lib @@ -8,6 +8,9 @@ export FFLAGS # for sub-makes .f.o: $(F90) -c $*.f +.f90.o: + $(F90) -c $*.f90 + LIBDIR = ../lib LIBS = \ @@ -22,7 +25,7 @@ OBJS = \ toolbox.o \ jacobi.o \ gal.o \ - mercier.o \ + mre.o \ bal.o \ fourfit.o \ sing1.o \ @@ -49,7 +52,7 @@ dcon_mod.o: ../equil/spline_mod.mod ../equil/global_mod.mod \ ../equil/equil_mod.mod ../equil/equil_out_mod.mod toolbox.o: dcon_mod.o jacobi.o: ../equil/local.o -mercier.o: dcon_mod.o +mre.o: dcon_mod.o bal.o: dcon_mod.o fourfit.o: ../equil/fspline_mod.mod dcon_mod.o toolbox.o sing1.o: fourfit.o @@ -60,7 +63,7 @@ free.o: ode.o resist.o: dcon_mod.o toolbox.o gal.o: jacobi.o sing.o free.o lib_interface.o: ../equil/local.o ../equil/global.o ../equil/utils.o ../equil/inverse.o -dcon.o: bal.o mercier.o ode.o resist.o gal.o lib_interface.o +dcon.o: bal.o mre.o ode.o resist.o gal.o lib_interface.o clean: rm -f *.o *.mod *.out *.bin sum.dat *~ *.diff rdcon diff --git a/rdcon/mercier.f b/rdcon/mre.f similarity index 92% rename from rdcon/mercier.f rename to rdcon/mre.f index a2f942a1a..24bbb5e06 100644 --- a/rdcon/mercier.f +++ b/rdcon/mre.f @@ -1,36 +1,36 @@ c----------------------------------------------------------------------- -c file mercier.f. -c computes mercier criterion and related quantities. +c file mre.f. +c computes modified Rutherford equation diagnostic quantities. c----------------------------------------------------------------------- c----------------------------------------------------------------------- c code organization. c----------------------------------------------------------------------- -c 0. rdcon_mercier_mod. -c 1. mercier_scan. +c 0. rdcon_mre_mod. +c 1. mre_scan. c----------------------------------------------------------------------- -c subprogram 0. rdcon_mercier_mod. +c subprogram 0. rdcon_mre_mod. c module declarations. c----------------------------------------------------------------------- c----------------------------------------------------------------------- c declarations. c----------------------------------------------------------------------- - MODULE rdcon_mercier_mod + MODULE rdcon_mre_mod USE rdcon_mod IMPLICIT NONE CONTAINS c----------------------------------------------------------------------- -c subprogram 1. mercier_scan. -c evaluates mercier criterion and related quantities. +c subprogram 1. mre_scan. +c evaluates modified Rutherford equation diagnostic quantities. c----------------------------------------------------------------------- c----------------------------------------------------------------------- c declarations. c----------------------------------------------------------------------- - SUBROUTINE mercier_scan + SUBROUTINE mre_scan INTEGER :: ipsi,itheta - REAL(r8) :: bsq,chi1,di,dpsisq,eta,h,jac,p1,psifac,q,q1,r,z, - $ rfac,term,theta,twopif,v1,v2,v21,v22,v23,v33, + REAL(r8) :: bsq,chi1,dpsisq,eta,jac,p1,psifac,q,q1,r,z, + $ rfac,theta,twopif,v1,v21,v22,v23,v33, $ bt,f1,Dnc,Dnc_prefac,Wc_prefac,eps_loc,Jpara,ftr,M, $ Hbs_prefac,mufrac,taua_prefac,taur_prefac, $ rmean_loc,amean_loc,deltatop_loc,deltabot_loc,delta_loc, @@ -60,7 +60,6 @@ SUBROUTINE mercier_scan f1=sq%fs1(ipsi,1)/twopi ! df/dpsi, p1=sq%fs1(ipsi,2) v1=sq%fs(ipsi,3) ! d(volume inside flux surface)/d(psi) - v2=sq%fs1(ipsi,3) q=sq%fs(ipsi,4) q1=sq%fs1(ipsi,4) chi1=twopi*psio ! d(poloidal flux)/dpsi, chi=poloidal flux @@ -151,16 +150,6 @@ SUBROUTINE mercier_scan CALL spline_int(ff) avg => ff%fsi(mtheta,:) c----------------------------------------------------------------------- -c evaluate mercier criterion and related quantities. -c----------------------------------------------------------------------- - term=twopif*p1*v1/(q1*chi1**3)*avg(2) - di=-.25+term*(1-term)+p1*(v1/(q1*chi1**2))**2*avg(1) - $ *(p1*(avg(3)+(twopif/chi1)**2*avg(4))-v2/v1) - h=twopif*p1*v1/(q1*chi1**3)*(avg(2)-avg(1)/avg(5)) - locstab%fs(ipsi,1)=di*locstab%xs(ipsi) - locstab%fs(ipsi,2)=(di+(h-0.5)**2)*locstab%xs(ipsi) - locstab%fs(ipsi,3)=h -c----------------------------------------------------------------------- c MRE term calculations c----------------------------------------------------------------------- IF(MRE_flag)THEN @@ -289,5 +278,5 @@ SUBROUTINE mercier_scan c terminate. c----------------------------------------------------------------------- RETURN - END SUBROUTINE mercier_scan - END MODULE rdcon_mercier_mod + END SUBROUTINE mre_scan + END MODULE rdcon_mre_mod diff --git a/rdcon/rdcon_netcdf.f b/rdcon/rdcon_netcdf.f index c0e06add9..a0a1bcb65 100644 --- a/rdcon/rdcon_netcdf.f +++ b/rdcon/rdcon_netcdf.f @@ -58,7 +58,8 @@ SUBROUTINE rdcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) INTEGER :: i, ncid, $ i_dim, m_dim, mo_dim, p_dim, i_id, m_id, mo_id, p_id, - $ f_id, q_id, dv_id, mu_id, di_id, dr_id, h_id, ca_id, + $ f_id, q_id, dv_id, mu_id, di_id, dr_id, h_id, bal_dp_id, + $ bal_alpha_id, bal_alpha_ref_id, $ wp_id, wpv_id, wv_id, wvv_id, wt_id, wtv_id, wt0_id, $ l_dim, l_id, coil_dim, coil_id, dpc_id, dc_id, $ lp_dim, lp_id, r_dim, r_id, rp_dim, rp_id, pr_id, qr_id, @@ -234,7 +235,12 @@ SUBROUTINE rdcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) CALL check( nf90_def_var(ncid, "di", nf90_double, p_dim, di_id) ) CALL check( nf90_def_var(ncid, "dr", nf90_double, p_dim, dr_id) ) CALL check( nf90_def_var(ncid, "h", nf90_double, p_dim, h_id) ) - CALL check( nf90_def_var(ncid, "ca1", nf90_double, p_dim, ca_id)) + CALL check(nf90_def_var(ncid,"ballooning_Delta_prime", + $ nf90_double,p_dim,bal_dp_id)) + CALL check(nf90_def_var(ncid,"alpha_critical",nf90_double, + $ p_dim,bal_alpha_id)) + CALL check(nf90_def_var(ncid,"alpha",nf90_double, + $ p_dim,bal_alpha_ref_id)) IF(MRE_flag)THEN CALL check( nf90_def_var(ncid, $ "Hbs_prefac", nf90_double, p_dim, hbs_id)) @@ -417,7 +423,9 @@ SUBROUTINE rdcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) CALL check( nf90_put_var(ncid,di_id, locstab%fs(:,1)/sq%xs(:))) CALL check( nf90_put_var(ncid,dr_id, locstab%fs(:,2)/sq%xs(:))) CALL check( nf90_put_var(ncid,h_id, locstab%fs(:,3))) - CALL check( nf90_put_var(ncid,ca_id, locstab%fs(:,4))) + CALL check(nf90_put_var(ncid,bal_dp_id,locstab%fs(:,4))) + CALL check(nf90_put_var(ncid,bal_alpha_id,locstab%fs(:,5))) + CALL check(nf90_put_var(ncid,bal_alpha_ref_id,locstab%fs(:,6))) IF(MRE_flag)THEN CALL check( nf90_put_var(ncid,hbs_id, mreterms%fs(:,1))) @@ -545,4 +553,4 @@ SUBROUTINE rdcon_netcdf_out(wp,wv,wt,wt0,ep,ev,et) RETURN END SUBROUTINE rdcon_netcdf_out - END MODULE rdcon_netcdf_mod \ No newline at end of file + END MODULE rdcon_netcdf_mod diff --git a/stride/bal.f b/stride/bal.f old mode 100755 new mode 100644 index c9c9c316b..70a2e1d87 --- a/stride/bal.f +++ b/stride/bal.f @@ -1,540 +1,440 @@ -c----------------------------------------------------------------------- -c file bal.f. -c determines high-n ideal marginal MHD ballooning stability. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c code organization. -c----------------------------------------------------------------------- -c 0. stride_bal_mod. -c 1. bal_scan. -c 2. bal_prep. -c 3. bal_der. -c 4. bal_int. -c 5. bal_pse. -c----------------------------------------------------------------------- -c subprogram 0. bal_mod. -c module declarations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - MODULE stride_bal_mod - USE stride_dcon_mod - IMPLICIT NONE - - INTEGER, PRIVATE :: ipsi - REAL(r8), PRIVATE :: di,alpha,theta0=0 - REAL(r8), DIMENSION(2,2), PRIVATE :: v0 - TYPE(spline_type), PRIVATE :: bf,bg - - CONTAINS -c----------------------------------------------------------------------- -c subprogram 1. bal_scan. -c computes ballooning stability criterion. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_scan -c----------------------------------------------------------------------- -c open output files. -c----------------------------------------------------------------------- - IF(out_bal1)CALL ascii_open(out_bal1_unit,"bal1.out","UNKNOWN") - IF(out_bal2)CALL ascii_open(out_bal2_unit,"bal2.out","UNKNOWN") - IF(bin_bal1)CALL bin_open(bin_bal1_unit,"bal1.bin","UNKNOWN", - $ "REWIND","none") - IF(bin_bal2)CALL bin_open(bin_bal2_unit,"bal2.bin","UNKNOWN", - $ "REWIND","none") -c----------------------------------------------------------------------- -c test ballooning stability. -c----------------------------------------------------------------------- - DO ipsi=0,mpsi - CALL bal_prep - IF(di <= 0 .AND. di >= -1e4 .AND. sq%xs(ipsi) <= 1)CALL bal_int - CALL spline_dealloc(bf) - CALL spline_dealloc(bg) - ENDDO -c----------------------------------------------------------------------- -c close output files. -c----------------------------------------------------------------------- - IF(out_bal1)CALL ascii_close(out_bal1_unit) - IF(out_bal2)CALL ascii_close(out_bal2_unit) - IF(bin_bal1)CALL bin_close(bin_bal1_unit) - IF(bin_bal2)CALL bin_close(bin_bal2_unit) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_scan -c----------------------------------------------------------------------- -c subprogram 2. bal_prep. -c computes coefficients for the ideal marginal ballooning equations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_prep - - LOGICAL, PARAMETER :: inverse_flag=.FALSE. - INTEGER :: itheta,info - INTEGER, DIMENSION(3) :: ipiv - REAL(r8) :: chi1,det,eta,fexp,p1,psifac,q,q1,r,rfac,theta, - $ twopif,v1 - REAL(r8), DIMENSION(2,2) :: c0,d0bar,d,v10 - REAL(r8), DIMENSION(2,2,0:mtheta) :: d1 - REAL(r8), DIMENSION(0:mtheta) :: b1,bsq,dbdb0,dbdb1,dbdb2, - $ kappan,kappas,jac,jacfac - REAL(r8), DIMENSION(3,3) :: v,w,temp - TYPE(spline_type) :: spl0,spl1,spl2,spl3 -c----------------------------------------------------------------------- -c compute surface quantities. -c----------------------------------------------------------------------- - psifac=sq%xs(ipsi) - twopif=sq%fs(ipsi,1) - p1=sq%fs1(ipsi,2) - v1=sq%fs(ipsi,3) - q=sq%fs(ipsi,4) - q1=sq%fs1(ipsi,4) - chi1=twopi*psio - v=0 - w=0 -c----------------------------------------------------------------------- -c compute coordinates and jacobian. -c----------------------------------------------------------------------- - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - theta=rzphi%ys(itheta) - rfac=SQRT(rzphi%f(1)) - eta=twopi*(theta+rzphi%f(2)) - r=ro+rfac*COS(eta) - jac(itheta)=rzphi%f(4) -c----------------------------------------------------------------------- -c compute contravariant basis vectors. -c----------------------------------------------------------------------- - v(1,1)=rzphi%fx(1)/(2*rfac*jac(itheta)) - v(1,2)=rzphi%fx(2)*twopi*rfac/jac(itheta) - v(1,3)=rzphi%fx(3)*r/jac(itheta) - v(2,1)=rzphi%fy(1)/(2*rfac*jac(itheta)) - v(2,2)=(1+rzphi%fy(2))*twopi*rfac/jac(itheta) - v(2,3)=rzphi%fy(3)*r/jac(itheta) - v(3,3)=twopi*r/jac(itheta) -c----------------------------------------------------------------------- -c compute covariant basis vectors, inverse method. -c----------------------------------------------------------------------- - IF(inverse_flag)THEN - temp=TRANSPOSE(v)*jac(itheta) - w=RESHAPE((/1,0,0,0,1,0,0,0,1/),(/3,3/)) - CALL dgetrf(3,3,temp,3,ipiv,info) - CALL dgetrs('N',3,3,temp,3,ipiv,w,3,info) -c----------------------------------------------------------------------- -c compute covariant basis vectors, direct method. -c----------------------------------------------------------------------- - ELSE - w(1,1)=(1+rzphi%fy(2))*twopi**2*rfac*r - $ /jac(itheta) - w(1,2)=-rzphi%fy(1)*pi*r/(rfac*jac(itheta)) - w(2,1)=-rzphi%fx(2)*twopi**2*r*rfac/jac(itheta) - w(2,2)=rzphi%fx(1)*pi*r/(rfac*jac(itheta)) - w(3,1)=(rzphi%fx(2)*rzphi%fy(3) - $ -rzphi%fx(3)*(1+rzphi%fy(2))) - $ *twopi*r*rfac/jac(itheta) - w(3,2)=(rzphi%fx(3)*rzphi%fy(1) - $ -rzphi%fx(1)*rzphi%fy(3)) - $ *r/(2*rfac*jac(itheta)) - w(3,3)=1/(twopi*r) - ENDIF -c----------------------------------------------------------------------- -c store physical quantities. -c----------------------------------------------------------------------- - b1(itheta)=SUM((v(2,:)+q*v(3,:))*v(1,:))*chi1 - bsq(itheta)=SUM((v(2,:)+q*v(3,:))**2)*chi1**2 - dbdb0(itheta)=SUM(w(1,:)**2)*q1**2 - dbdb1(itheta)=-SUM((w(3,:)-q*w(2,:))*w(1,:))*2*q1 - dbdb2(itheta)=SUM((w(3,:)-q*w(2,:))**2) - ENDDO -c----------------------------------------------------------------------- -c compute curvature terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl0,mtheta,2) - spl0%xs=rzphi%ys - spl0%fs(:,1)=1/bsq - spl0%fs(:,2)=jac*b1/bsq - CALL spline_fit(spl0,"periodic") - kappas=-spl0%fs1(:,1)*twopif/(2*jac) - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - kappan(itheta)=((p1/bsq(itheta)-rzphi%fx(4)/jac(itheta))/chi1 - $ +twopif*q1/(bsq(itheta)*jac(itheta))+spl0%fs1(itheta,2) - $ /jac(itheta))/2 - ENDDO - CALL spline_dealloc(spl0) -c----------------------------------------------------------------------- -c compute coefficients for ballooning equation. -c----------------------------------------------------------------------- - CALL spline_alloc(bf,mtheta,5) - bf%xs=rzphi%ys - jacfac=jac/chi1 - bf%fs(:,1)=dbdb0/(bsq*jacfac) - bf%fs(:,2)=dbdb1/(2*dbdb0) - bf%fs(:,3)=(dbdb2-dbdb1**2/(4*dbdb0))/(bsq*jacfac) - bf%fs(:,4)=2*kappan*p1/chi1*jacfac - bf%fs(:,5)=-2*kappas*p1/chi1*q1*jacfac - CALL spline_fit(bf,"periodic") -c----------------------------------------------------------------------- -c diagnose bf. -c----------------------------------------------------------------------- - bf%title=(/"theta ","dbdb0 ","dbdb1 ","dbdb2 ","kappas","kappan"/) - CALL spline_write1(bf,out_bal1,bin_bal1, - $ out_bal1_unit,bin_bal1_unit,.TRUE.) -c----------------------------------------------------------------------- -c initialize bg. -c----------------------------------------------------------------------- - CALL spline_alloc(spl0,mtheta,1) - spl0%xs=rzphi%ys - spl0%fs(:,1)=-p1*q1*twopif/(bsq*chi1**2) - CALL spline_fit(spl0,"periodic") - CALL spline_int(spl0) - CALL spline_alloc(bg,mtheta,5) - bg%xs=rzphi%ys - bg%fs(:,5)=spl0%fs(:,1)-spl0%fsi(mtheta,1) - CALL spline_dealloc(spl0) -c----------------------------------------------------------------------- -c compute c0. -c----------------------------------------------------------------------- - CALL spline_alloc(spl1,mtheta,4) - spl1%xs=rzphi%ys - DO itheta=0,mtheta - c0(1,1)=.5 - c0(1,2)=1/bf%fs(itheta,1) - c0(2,1)=-bf%fs(itheta,4) - c0(2,2)=-.5 -c----------------------------------------------------------------------- -c compute d0. -c----------------------------------------------------------------------- - spl1%fs(itheta,1)=c0(1,1)+c0(1,2)*bg%fs(itheta,5) - spl1%fs(itheta,2)=c0(1,2) - spl1%fs(itheta,3)=c0(2,1) - $ +(c0(2,2)-c0(1,1)-c0(1,2)*bg%fs(itheta,5))*bg%fs(itheta,5) - spl1%fs(itheta,4)=-spl1%fs(itheta,1) -c----------------------------------------------------------------------- -c compute d1. -c----------------------------------------------------------------------- - fexp=-c0(1,2)*bf%fs(itheta,2)*2 - d1(1,1,itheta)=fexp*bg%fs(itheta,5) - d1(1,2,itheta)=fexp - d1(2,1,itheta)=-fexp*bg%fs(itheta,5)**2 - d1(2,2,itheta)=-d1(1,1,itheta) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic splines and write output. -c----------------------------------------------------------------------- - CALL spline_fit(spl1,"periodic") - CALL spline_int(spl1) - d0bar(1,1)=spl1%fsi(mtheta,1) - d0bar(1,2)=spl1%fsi(mtheta,2) - d0bar(2,1)=spl1%fsi(mtheta,3) - d0bar(2,2)=spl1%fsi(mtheta,4) -c----------------------------------------------------------------------- -c compute mercier criterion and alpha. -c----------------------------------------------------------------------- - di=d0bar(1,1)*d0bar(2,2)-d0bar(1,2)*d0bar(2,1) - IF(di > 0)THEN - CALL spline_dealloc(spl1) - RETURN - ENDIF - alpha=SQRT(-di) -c----------------------------------------------------------------------- -c compute zeroth-order eigenfunctions. -c----------------------------------------------------------------------- - v0(1,1)=1 - v0(1,2)=1 - v0(2,1)=-(d0bar(1,1)-alpha)/d0bar(1,2) - v0(2,2)=-(d0bar(1,1)+alpha)/d0bar(1,2) -c----------------------------------------------------------------------- -c store derivatives for first-order terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl2,mtheta,4) - spl2%xs=rzphi%ys - DO itheta=0,mtheta - spl2%fs(itheta,1) - $ =(spl1%fs(itheta,1)-alpha)*v0(1,1) - $ +spl1%fs(itheta,2)*v0(2,1) - spl2%fs(itheta,2) - $ =spl1%fs(itheta,3)*v0(1,1) - $ +(spl1%fs(itheta,4)-alpha)*v0(2,1) - spl2%fs(itheta,3) - $ =(spl1%fs(itheta,1)+alpha)*v0(1,2) - $ +spl1%fs(itheta,2)*v0(2,2) - spl2%fs(itheta,4) - $ =spl1%fs(itheta,3)*v0(1,2) - $ +(spl1%fs(itheta,4)+alpha)*v0(2,2) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic spines and integrate. -c----------------------------------------------------------------------- - CALL spline_fit(spl2,"periodic") - CALL spline_int(spl2) - spl2%fs=spl2%fsi -c----------------------------------------------------------------------- -c store derivatives for second-order terms. -c----------------------------------------------------------------------- - CALL spline_alloc(spl3,mtheta,4) - spl3%xs=rzphi%ys - DO itheta=0,mtheta - spl3%fs(itheta,1) - $ =(spl1%fs1(itheta,1)+1-alpha)*spl2%fs(itheta,1) - $ +spl1%fs(itheta,2)*spl2%fs(itheta,2) - $ +d1(1,1,itheta)*v0(1,1)+d1(1,2,itheta)*v0(2,1) - spl3%fs(itheta,2) - $ =spl1%fs(itheta,3)*spl2%fs(itheta,1) - $ +(spl1%fs(itheta,4)+1-alpha)*spl2%fs(itheta,2) - $ +d1(2,1,itheta)*v0(1,1)+d1(2,2,itheta)*v0(2,1) - spl3%fs(itheta,3) - $ =(spl1%fs(itheta,1)+1+alpha)*spl2%fs(itheta,3) - $ +spl1%fs(itheta,2)*spl2%fs(itheta,4) - $ +d1(1,1,itheta)*v0(1,2)+d1(1,2,itheta)*v0(2,2) - spl3%fs(itheta,4) - $ =spl1%fs(itheta,3)*spl2%fs(itheta,3) - $ +(spl1%fs(itheta,4)+1+alpha)*spl2%fs(itheta,4) - $ +d1(2,1,itheta)*v0(1,2)+d1(2,2,itheta)*v0(2,2) - ENDDO -c----------------------------------------------------------------------- -c fit to cubic spines and integrate. -c----------------------------------------------------------------------- - CALL spline_fit(spl3,"periodic") - CALL spline_int(spl3) -c----------------------------------------------------------------------- -c compute first order constants, first eigenfunction. -c----------------------------------------------------------------------- - d(2,1)=d0bar(2,1) - d(1,2)=d0bar(1,2) - d(1,1)=d0bar(1,1)+1-alpha - d(2,2)=d0bar(2,2)+1-alpha - det=d(1,1)*d(2,2)-d(1,2)*d(2,1) - v10(1,1)=(d(1,2)*spl3%fsi(mtheta,2)-d(2,2)*spl3%fsi(mtheta,1))/det - v10(2,1)=(d(2,1)*spl3%fsi(mtheta,1)-d(1,1)*spl3%fsi(mtheta,2))/det -c----------------------------------------------------------------------- -c compute first order constants, second eigenfunction. -c----------------------------------------------------------------------- - d(1,1)=d0bar(1,1)+1+alpha - d(2,2)=d0bar(2,2)+1+alpha - det=d(1,1)*d(2,2)-d(1,2)*d(2,1) - v10(1,2)=(d(1,2)*spl3%fsi(mtheta,4)-d(2,2)*spl3%fsi(mtheta,3))/det - v10(2,2)=(d(2,1)*spl3%fsi(mtheta,3)-d(1,1)*spl3%fsi(mtheta,4))/det -c----------------------------------------------------------------------- -c add constants to v1 and fit to cubic splines. -c----------------------------------------------------------------------- - bg%fs(:,1)=spl2%fs(:,1)+v10(1,1) - bg%fs(:,2)=spl2%fs(:,2)+v10(2,1) - bg%fs(:,3)=spl2%fs(:,3)+v10(1,2) - bg%fs(:,4)=spl2%fs(:,4)+v10(2,2) - CALL spline_fit(bg,"periodic") -c----------------------------------------------------------------------- -c diagnose bg. -c----------------------------------------------------------------------- -c bg%title=(/" bg1 "," bg2 "," bg2 "," bg2 "," bg2 "/) -c CALL spline_write1(bg,out_bal1,bin_bal1, -c $ out_bal1_unit,bin_bal1_unit,.FALSE.) -c----------------------------------------------------------------------- -c deallocate local splines. -c----------------------------------------------------------------------- - CALL spline_dealloc(spl1) - CALL spline_dealloc(spl2) - CALL spline_dealloc(spl3) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_prep -c----------------------------------------------------------------------- -c subprogram 3. bal_der -c differential equations for the marginal ballooning. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_der(neq,theta,y,dy) - - INTEGER, INTENT(IN) :: neq - REAL(r8), INTENT(IN) :: theta - REAL(r8), DIMENSION(neq), INTENT(IN) :: y - REAL(r8), DIMENSION(neq), INTENT(OUT) :: dy - - REAL(r8) :: dbdb,kappaw,thfac -c----------------------------------------------------------------------- -c compute coefficients. -c----------------------------------------------------------------------- - CALL spline_eval(bf,theta,0) - thfac=theta-theta0 - dbdb=bf%f(1)*(thfac+bf%f(2))**2+bf%f(3) - kappaw=bf%f(4)+thfac*bf%f(5) -c----------------------------------------------------------------------- -c compute derivatives. -c----------------------------------------------------------------------- - dy(1)=y(2)/dbdb - dy(2)=-y(1)*kappaw -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_der -c----------------------------------------------------------------------- -c subprogram 4. bal_int -c integrates the ideal marginal ballooning equations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_int - - INTEGER, PARAMETER :: neq=2,liw=20,lrw=22+neq*16 - INTEGER, DIMENSION(liw) :: iwork=0 - INTEGER :: iopt,iside,istate,itask,itol,jac,mf - INTEGER :: istep,nstep=HUGE(0),icross - REAL(r8), PARAMETER :: tol=1e-5,thmin=1,hu_min=1e-10 - REAL(r8) :: thmax,atol,ca1,ca2,det,rtol,theta,thout - REAL(r8), DIMENSION(lrw) :: rwork=0 - REAL(r8), DIMENSION(neq) :: y,dy - REAL(r8), DIMENSION(2,2) :: u -c----------------------------------------------------------------------- -c write formats. -c----------------------------------------------------------------------- - 10 FORMAT(/2x,"ist",2x,"nqu",1x,"icr",4x,"theta",6x,"dtheta",4x, - $ "asinh y1",2x,"asinh ca1",2x,"asinh ca2"/) - 20 FORMAT(i5,2i4,1x,1p,5e11.3) -c----------------------------------------------------------------------- -c initialize independent variables. -c----------------------------------------------------------------------- - thmax=SQRT(MAXVAL(ABS(bf%fs(:,3)/bf%fs(:,1))))*10*thmax0 - thmax=MIN(thmax,100._r8) - theta=-thmax - thout=thmax - istep=0 -c----------------------------------------------------------------------- -c compute equilibrium quantities and asymptotic solutions. -c----------------------------------------------------------------------- - CALL bal_pse(theta,u) - y(1)=u(1,2)*sinh(1.) - y(2)=u(2,2)*sinh(1.) -c----------------------------------------------------------------------- -c set up integrator parameters. -c----------------------------------------------------------------------- - icross=0 - iside=1 - istate=1 - itask=5 - mf=10 - rwork(1)=thout - iopt=1 - itol=1 - rtol=tol - atol=tol*tol -c----------------------------------------------------------------------- -c write header. -c----------------------------------------------------------------------- - IF(out_bal2)WRITE(out_bal2_unit,10) -c----------------------------------------------------------------------- -c check for crossings. -c----------------------------------------------------------------------- - DO - IF(iside*y(1) < 0)THEN - icross=icross+1 - iside=-iside - ENDIF -c----------------------------------------------------------------------- -c compute and print asymptotic coefficient. -c----------------------------------------------------------------------- - IF(out_bal2 .OR. bin_bal2)THEN - IF(ABS(theta) > thmin)THEN - CALL bal_pse(theta,u) - det=u(1,1)*u(2,2)-u(1,2)*u(2,1) - ca1=(y(1)*u(2,2)-y(2)*u(1,2))/det - ca2=(y(2)*u(1,1)-y(1)*u(2,1))/det - CALL bal_der(neq,theta,y,dy) - IF(out_bal2) - $ WRITE(out_bal2_unit,20)istep,iwork(14),icross, - $ theta,rwork(11),asinh(y(1)),asinh(ca1),asinh(ca2) - IF(bin_bal2) - $ WRITE(bin_bal2_unit)REAL(theta,4), - $ REAL(asinh(y(1)),4),REAL(asinh(y(2)),4), - $ REAL(y(2)/dy(1),4),REAL(dy(2)/y(1),4), - $ REAL(asinh(ca1),4),REAL(asinh(ca2),4),REAL(u,4) - ELSE - ca1=0 - ca2=0 - det=0 - ENDIF - ENDIF -c----------------------------------------------------------------------- -c advance. -c----------------------------------------------------------------------- - IF(istep >= nstep .OR. theta >= thmax .OR. istate < 0)EXIT - istep=istep+1 - CALL lsode(bal_der,neq,y,theta,thout,itol,rtol,atol,itask, - $ istate,iopt,rwork,lrw,iwork,liw,jac,mf) - IF(rwork(11) < hu_min)EXIT - ENDDO -c----------------------------------------------------------------------- -c write trailer. -c----------------------------------------------------------------------- - IF(out_bal2)WRITE(out_bal2_unit,10) - IF(bin_bal2)WRITE(bin_bal2_unit) -c----------------------------------------------------------------------- -c compute and store final asymptotic coefficients. -c----------------------------------------------------------------------- - IF(theta >= thmax)THEN - CALL bal_pse(theta,u) - det=u(1,1)*u(2,2)-u(1,2)*u(2,1) - ca1=(y(1)*u(2,2)-y(2)*u(1,2))/det - ca2=(y(2)*u(1,1)-y(1)*u(2,1))/det - locstab%fs(ipsi,4:5)=(/ca1,ca2/) - ENDIF -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_int -c----------------------------------------------------------------------- -c subprogram 5. bal_pse. -c computes alpha series solutions. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE bal_pse(theta,u) - - REAL(r8), INTENT(IN) :: theta - REAL(r8), INTENT(OUT), DIMENSION(2,2) :: u - - REAL(r8) :: dtheta,thfac - REAL(r8), DIMENSION(2,2) :: v -c----------------------------------------------------------------------- -c compute alpha series. -c----------------------------------------------------------------------- - CALL spline_eval(bg,theta,0) - dtheta=theta-theta0 - v(1,1)=v0(1,1)+bg%f(1)/dtheta - v(2,1)=v0(2,1)+bg%f(2)/dtheta - v(1,2)=v0(1,2)+bg%f(3)/dtheta - v(2,2)=v0(2,2)+bg%f(4)/dtheta -c----------------------------------------------------------------------- -c compute transformed solutions. -c----------------------------------------------------------------------- - u(1,1)=v(1,1) - u(1,2)=v(1,2) - u(2,1)=bg%f(5)*v(1,1)+v(2,1) - u(2,2)=bg%f(5)*v(1,2)+v(2,2) -c----------------------------------------------------------------------- -c multiply by alphas. -c----------------------------------------------------------------------- - thfac=ABS(dtheta)**(alpha+.5)/dtheta - u(1,1)=u(1,1)*thfac - u(2,1)=u(2,1)*thfac*dtheta - u(1,2)=u(1,2)/(thfac*dtheta) - u(2,2)=u(2,2)/thfac -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE bal_pse - END MODULE stride_bal_mod + module stride_bal_mod + use stride_dcon_mod + use, intrinsic :: ieee_arithmetic + implicit none + + integer, private :: ipsi + real(r8), private :: theta_max + type(spline_type), private :: bf + + contains + + subroutine bal_scan + real(r8) :: di, h, psi, delta_prime + real(r8), dimension(0:mpsi) :: volume + type(spline_type) :: volume_spline + + volume = 0.0_r8 + if (bal_flag) then + call spline_alloc(volume_spline,mpsi,1) + volume_spline%xs = sq%xs + volume_spline%fs(:,1) = sq%fs(:,3) + volume_spline%fs1(:,1) = sq%fs1(:,3) + call spline_int(volume_spline) + volume = max(volume_spline%fsi(:,1),0.0_r8) + call spline_dealloc(volume_spline) + end if + + do ipsi = 0, mpsi + psi = sq%xs(ipsi) + call bal_prep(di,h,0.0_r8) + locstab%fs(ipsi,1) = di * psi + locstab%fs(ipsi,2) = (di + (h - 0.5_r8)**2) * psi + locstab%fs(ipsi,3) = h + locstab%fs(ipsi,4) = ieee_value(0.0_r8,ieee_quiet_nan) + locstab%fs(ipsi,5) = ieee_value(0.0_r8,ieee_quiet_nan) + locstab%fs(ipsi,6) = ieee_value(0.0_r8,ieee_quiet_nan) + if (bal_flag .and. psi <= 1.0_r8) then + call bal_int(delta_prime) + locstab%fs(ipsi,4) = delta_prime + end if + call spline_dealloc(bf) + if (bal_flag) call bal_alpha_first_boundary(volume(ipsi)) + end do + end subroutine bal_scan + + subroutine bal_prep(di,h,corr_pprime) + real(r8), intent(out) :: di, h + real(r8), intent(in) :: corr_pprime + + integer :: itheta + real(r8) :: psi, twopif, p1, v1, q, q1, chi1 + real(r8) :: theta, rho, eta, ceta, seta, r, jac + real(r8) :: drfac_dp, eta_dp, dr_dp + real(r8) :: dv21_dp, dv22_dp, dv23_dp, dv33_dp + real(r8) :: iper_ref, local_scale, cscale + real(r8) :: abar_w, bbar_w, response_denom + real(r8) :: dbase(3), dB(3), bvec(3) + real(r8) :: gradpsi(3), gradtheta(3), gradzeta(3) + real(r8) :: v(3,3), w(3,3) + real(r8) :: d0bar(2,2) + real(r8), dimension(0:mtheta) :: bsq, gradpsi_sq, dpsisq, jacarr + real(r8), dimension(0:mtheta) :: bdot_theta_phi, iper, htheta + real(r8), dimension(0:mtheta) :: dbsq_dtheta, kappas + real(r8), dimension(0:mtheta) :: jac_psi, bsq_psi + real(r8), dimension(0:mtheta) :: spl1_vals, spl1_deriv + real(r8), dimension(0:mtheta) :: a0, a1, a2 + real(r8), dimension(0:mtheta) :: kappaw0, kappaw1 + real(r8), dimension(0:mtheta) :: aw, bw, aperp + real(r8), dimension(0:mtheta) :: diper_dtheta, iper_perturbed + real(r8), dimension(0:mtheta) :: jacchi, pchi, sigma + real(r8), dimension(0:mtheta) :: m012, m021 + real(r8), dimension(0:mtheta,3) :: hfs + real(r8), dimension(0:mtheta,4) :: n0 + real(r8), dimension(0:mtheta,7) :: coeff + real(r8), dimension(0:mtheta,4) :: f0, fx0, fy0, fxy0 + real(r8), dimension(0:mtheta) :: rho0, eta0, r0 + real(r8), dimension(0:mtheta,3,3) :: v0 + real(r8), dimension(0:mtheta,3) :: vzp0, vpt0 + type(spline_type) :: work + + psi = sq%xs(ipsi) + twopif = sq%fs(ipsi,1) + p1 = sq%fs1(ipsi,2) + v1 = sq%fs(ipsi,3) + q = sq%fs(ipsi,4) + q1 = sq%fs1(ipsi,4) + chi1 = twopi * psio + + do itheta = 0, mtheta + theta = rzphi%ys(itheta) + call bicube_eval(rzphi, rzphi%xs(ipsi), theta, 2) + f0(itheta,:) = rzphi%f + fx0(itheta,:) = rzphi%fx + fy0(itheta,:) = rzphi%fy + fxy0(itheta,:) = rzphi%fxy + + rho = sqrt(f0(itheta,1)) + eta = twopi * (theta + f0(itheta,2)) + ceta = cos(eta) + seta = sin(eta) + r = ro + rho * ceta + jac = f0(itheta,4) + rho0(itheta) = rho + eta0(itheta) = eta + r0(itheta) = r + jacarr(itheta) = jac + + v = 0.0_r8 + v(1,1) = fx0(itheta,1) / (2.0_r8*rho*jac) + v(1,2) = fx0(itheta,2) * twopi*rho / jac + v(1,3) = fx0(itheta,3) * r / jac + v(2,1) = fy0(itheta,1) / (2.0_r8*rho*jac) + v(2,2) = (1.0_r8 + fy0(itheta,2)) * twopi*rho / jac + v(2,3) = fy0(itheta,3) * r / jac + v(3,3) = twopi*r / jac + + w = 0.0_r8 + w(1,1) = (1.0_r8 + fy0(itheta,2))*twopi**2*rho*r/jac + w(1,2) = -fy0(itheta,1)*pi*r/(rho*jac) + w(2,1) = -fx0(itheta,2)*twopi**2*r*rho/jac + w(2,2) = fx0(itheta,1)*pi*r/(rho*jac) + w(3,1) = (fx0(itheta,2)*fy0(itheta,3) - + $fx0(itheta,3)*(1.0_r8 + fy0(itheta,2)))*twopi*r*rho/jac + w(3,2) = (fx0(itheta,3)*fy0(itheta,1) - + $fx0(itheta,1)*fy0(itheta,3))*r/(2.0_r8*rho*jac) + w(3,3) = 1.0_r8/(twopi*r) + + gradpsi = w(1,:) + gradtheta = w(2,:) + gradzeta = w(3,:) + bvec = (v(2,:) + q*v(3,:))*chi1 + bsq(itheta) = dot_product(bvec,bvec) + gradpsi_sq(itheta) = dot_product(gradpsi,gradpsi) + dpsisq(itheta) = (twopi*r)**2 * + $(v(2,1)**2 + v(2,2)**2) + iper(itheta) = dot_product(q*gradtheta-gradzeta,gradpsi) / + $gradpsi_sq(itheta) + htheta(itheta) = fy0(itheta,3)/twopi + bdot_theta_phi(itheta) = dot_product(bvec,v(1,:)) + v0(itheta,:,:) = v + vzp0(itheta,:) = v(2,:) + vpt0(itheta,:) = v(3,:) + end do + + call periodic_lowpass_derivative(bsq, rzphi%ys, 16, dbsq_dtheta) + kappas = dbsq_dtheta*twopif/(2.0_r8*jacarr*bsq**2) + + do itheta = 0, mtheta + jac_psi(itheta) = fx0(itheta,4) + drfac_dp = fx0(itheta,1)/(2.0_r8*rho0(itheta)) + eta_dp = twopi*fx0(itheta,2) + dr_dp = drfac_dp*cos(eta0(itheta)) - + $rho0(itheta)*sin(eta0(itheta))*eta_dp + + dv21_dp = (fxy0(itheta,1)/(2.0_r8*rho0(itheta)) - + $fx0(itheta,1)*fy0(itheta,1)/(4.0_r8*f0(itheta,1)* + $rho0(itheta)))/jacarr(itheta) - + $v0(itheta,2,1)*jac_psi(itheta)/jacarr(itheta) + dv22_dp = (fxy0(itheta,2)*twopi*rho0(itheta) + + $(1.0_r8+fy0(itheta,2))*twopi*drfac_dp)/jacarr(itheta) - + $v0(itheta,2,2)*jac_psi(itheta)/jacarr(itheta) + dv23_dp = (fxy0(itheta,3)*r0(itheta) + + $fy0(itheta,3)*dr_dp)/jacarr(itheta) - + $v0(itheta,2,3)*jac_psi(itheta)/jacarr(itheta) + dv33_dp = twopi*dr_dp/jacarr(itheta) - + $v0(itheta,3,3)*jac_psi(itheta)/jacarr(itheta) + + dbase = (/dv21_dp,dv22_dp,dv23_dp/) + + $q*(/0.0_r8,0.0_r8,dv33_dp/) + q1*v0(itheta,3,:) + dB = dbase*chi1 + bsq_psi(itheta) = 2.0_r8*dot_product( + $vzp0(itheta,:)+q*vpt0(itheta,:),dB)*chi1 + end do + + spl1_vals = jacarr*bdot_theta_phi/sqrt(bsq) + call periodic_lowpass_derivative(spl1_vals, rzphi%ys, 32, + $spl1_deriv) + kappaw0 = -(jac_psi/jacarr + 0.5_r8*bsq_psi/bsq)/chi1 + + $spl1_deriv/(jacarr*sqrt(bsq)) + twopif*q1/(bsq*jacarr) + +c corr_pprime also drives the I_per response. This is the +c corr_qprime=0 specialization of Julia's response calculation. + aw = twopi**2*r0**2/(chi1**2*gradpsi_sq) + bw = twopif**2/(chi1**2*gradpsi_sq) + hfs(:,1) = (q+htheta)*aw + hfs(:,2) = (q+htheta)*bw + hfs(mtheta,1:2) = hfs(0,1:2) + call spline_alloc(work,mtheta,2) + work%xs = rzphi%ys + work%fs = hfs(:,1:2) + call spline_fit(work,"periodic") + call spline_int(work) + abar_w = work%fsi(mtheta,1)/q + bbar_w = work%fsi(mtheta,2)/q + call spline_dealloc(work) + response_denom = 1.0_r8+bbar_w + aperp = (q+htheta)*((aw-abar_w) - + $abar_w/response_denom*(bw-bbar_w)) + diper_dtheta = aperp*corr_pprime + diper_dtheta(mtheta) = diper_dtheta(0) + call spline_alloc(work,mtheta,1) + work%xs = rzphi%ys + work%fs(:,1) = diper_dtheta + call spline_fit(work,"periodic") + call spline_int(work) + iper_perturbed = work%fsi(:,1) + call spline_dealloc(work) + + iper_ref = iper(0) + iper = iper - iper_ref + iper_perturbed + p1 = p1+corr_pprime + kappaw0 = kappaw0-kappas*iper_perturbed + a0 = 1.0_r8/(chi1**2*gradpsi_sq) + + $gradpsi_sq/bsq*iper**2 + a1 = gradpsi_sq/bsq*(2.0_r8*q1*iper) + a2 = gradpsi_sq/bsq*q1**2 + jacchi = jacarr/chi1 + pchi = p1/chi1 + kappaw1 = q1*kappas + + coeff(:,1) = a0 + coeff(:,2) = a1 + coeff(:,3) = a2 + coeff(:,4) = kappaw0 + coeff(:,5) = kappaw1 + coeff(:,6) = jacchi + coeff(:,7) = pchi + + theta_max = 0.0_r8 + do itheta = 0, mtheta + if (abs(a2(itheta)) > epsilon(1.0_r8)) then + cscale = a0(itheta)-a1(itheta)**2/(4.0_r8*a2(itheta)) + local_scale = sqrt(abs(cscale/a2(itheta))) + if (ieee_is_finite(local_scale)) + $theta_max = max(theta_max,local_scale) + end if + end do + if (theta_max <= 0.0_r8) theta_max = 16.5_r8 + theta_max = min(16.5_r8,10.0_r8*theta_max) + theta_max = max(theta_max,1.0e-2_r8) + + sigma = -(twopif*p1*q1)/(chi1**2*bsq) + m012 = jacchi/a2 + m021 = -2.0_r8*jacchi*pchi*kappaw0 + n0(:,1) = 0.5_r8 + m012*sigma + n0(:,2) = m012 + n0(:,3) = m021 - sigma - m012*sigma**2 + n0(:,4) = -0.5_r8 - m012*sigma + n0(mtheta,:) = n0(0,:) + + call spline_alloc(work,mtheta,4) + work%xs = rzphi%ys + work%fs = n0 + call spline_fit(work,"periodic") + call spline_int(work) + d0bar(1,1) = work%fsi(mtheta,1) + d0bar(1,2) = work%fsi(mtheta,2) + d0bar(2,1) = work%fsi(mtheta,3) + d0bar(2,2) = work%fsi(mtheta,4) + call spline_dealloc(work) + di = d0bar(1,1)*d0bar(2,2)-d0bar(1,2)*d0bar(2,1) + + hfs(:,1) = bsq/dpsisq*jacarr/v1 + hfs(:,2) = 1.0_r8/dpsisq*jacarr/v1 + hfs(:,3) = bsq*jacarr/v1 + hfs(mtheta,:) = hfs(0,:) + call spline_alloc(work,mtheta,3) + work%xs = rzphi%ys + work%fs = hfs + call spline_fit(work,"periodic") + call spline_int(work) + h = twopif*p1*v1/(q1*chi1**3)*(work%fsi(mtheta,2) - + $work%fsi(mtheta,1)/work%fsi(mtheta,3)) + call spline_dealloc(work) + + coeff(mtheta,:) = coeff(0,:) + call spline_alloc(bf,mtheta,7) + bf%xs = rzphi%ys + bf%fs = coeff + call spline_fit(bf,"periodic") + end subroutine bal_prep + + subroutine periodic_lowpass_derivative(vals,xs,nkeep,deriv) + real(r8), intent(in) :: vals(0:), xs(0:) + integer, intent(in) :: nkeep + real(r8), intent(out) :: deriv(0:) + integer :: n, kmax, j, l, k + real(r8) :: period, omega, phase, ak, bk + + n = ubound(vals,1) + period = xs(n)-xs(0) + kmax = min(nkeep,(n-1)/2) + deriv = 0.0_r8 + do k = 1, kmax + ak = 0.0_r8 + bk = 0.0_r8 + do l = 0, n-1 + phase = twopi*real(k*l,r8)/real(n,r8) + ak = ak + vals(l)*cos(phase) + bk = bk + vals(l)*sin(phase) + end do + ak = 2.0_r8*ak/real(n,r8) + bk = 2.0_r8*bk/real(n,r8) + omega = twopi*real(k,r8)/period + do j = 0, n-1 + phase = twopi*real(k*j,r8)/real(n,r8) + deriv(j) = deriv(j) + omega*(-ak*sin(phase)+bk*cos(phase)) + end do + end do + deriv(n) = deriv(0) + end subroutine periodic_lowpass_derivative + + subroutine bal_der(neq,theta,y,dy) + integer, intent(in) :: neq + real(r8), intent(in) :: theta + real(r8), intent(in) :: y(neq) + real(r8), intent(out) :: dy(neq) + real(r8) :: f, kappaw, g, theta_wrapped + + theta_wrapped = modulo(theta-bf%xs(0), + $bf%xs(bf%mx)-bf%xs(0))+bf%xs(0) + call spline_eval(bf,theta_wrapped,0) + f = (bf%f(1)+theta*bf%f(2)+theta**2*bf%f(3))/bf%f(6) + kappaw = bf%f(4)-theta*bf%f(5) + g = -2.0_r8*bf%f(6)*bf%f(7)*kappaw + dy(1) = y(2)/f + dy(2) = g*y(1) + end subroutine bal_der + + subroutine bal_integrate_side(theta_start,theta_end,y,success) + integer, parameter :: neq=2, liw=20, lrw=22+neq*16 + real(r8), intent(in) :: theta_start, theta_end + real(r8), intent(inout) :: y(neq) + logical, intent(out) :: success + integer :: istate, itask, itol, iopt, jac, mf + integer :: iwork(liw) + real(r8) :: theta, rtol, atol, rwork(lrw) + + iwork = 0 + rwork = 0.0_r8 + theta = theta_start + itol = 1 + rtol = 1.0e-8_r8 + atol = 1.0e-16_r8 + itask = 1 + istate = 1 + iopt = 1 + iwork(6) = 100000 + rwork(7) = 1.0e-10_r8 + jac = 0 + mf = 10 + call lsode(bal_der,neq,y,theta,theta_end,itol,rtol,atol,itask, + $istate,iopt,rwork,lrw,iwork,liw,jac,mf) + success = istate == 2 + end subroutine bal_integrate_side + + subroutine bal_int(delta_prime) + real(r8), parameter :: match_point=1.0e-3_r8 + real(r8), intent(out) :: delta_prime + real(r8) :: yl(2), yr(2) + logical :: ok_left, ok_right + + yl = (/0.0_r8,1.0_r8/) + yr = (/0.0_r8,-1.0_r8/) + call bal_integrate_side(-theta_max,-match_point,yl,ok_left) + call bal_integrate_side(theta_max,match_point,yr,ok_right) + if (ok_left .and. ok_right .and. + $abs(yl(1)) > tiny(1.0_r8) .and. + $abs(yr(1)) > tiny(1.0_r8)) then + delta_prime = yr(2)/yr(1)-yl(2)/yl(1) + else + delta_prime = ieee_value(0.0_r8,ieee_quiet_nan) + end if + end subroutine bal_int + + subroutine bal_delta_at_scale(scale,delta_prime) + real(r8), intent(in) :: scale + real(r8), intent(out) :: delta_prime + real(r8) :: corr_pprime, di_dummy, h_dummy + + corr_pprime = sq%fs1(ipsi,2)*(scale-1.0_r8) + call bal_prep(di_dummy,h_dummy,corr_pprime) + call bal_int(delta_prime) + call spline_dealloc(bf) + end subroutine bal_delta_at_scale + + subroutine bal_alpha_first_boundary(volume) + real(r8), intent(in) :: volume + integer, parameter :: n_scan=24 + real(r8), parameter :: pole_cap=3.0_r8 + real(r8), parameter :: max_alpha_scale=8.0_r8 + real(r8), parameter :: alpha_scale_tol=1.0e-3_r8 + integer :: iscan + real(r8) :: alpha_ref, scale, scale_prev, step + real(r8) :: d, d_prev, d_anchor, lo, hi, mid, dm, sign_lo + logical :: have_anchor + + if (sq%xs(ipsi) < 0.1_r8 .or. + $sq%xs(ipsi) > min(0.99_r8,sq%xs(mpsi))) return + alpha_ref = -2.0_r8*sq%fs1(ipsi,2)*sq%fs(ipsi,3) / + $psio**2*sqrt(volume/(twopi*max(ro,epsilon(1.0_r8)))) + locstab%fs(ipsi,6) = alpha_ref + step = max_alpha_scale/real(n_scan,r8) + have_anchor = .false. + d_prev = ieee_value(0.0_r8,ieee_quiet_nan) + scale_prev = 0.0_r8 + + do iscan = 0, n_scan + scale = step*real(iscan,r8) + call bal_delta_at_scale(scale,d) + if (.not. have_anchor) then + if (ieee_is_finite(d) .and. d /= 0.0_r8) then + have_anchor = .true. + d_anchor = abs(d) + end if + else if (ieee_is_finite(d_prev) .and. ieee_is_finite(d) .and. + $sign(1.0_r8,d) /= sign(1.0_r8,d_prev) .and. + $max(abs(d_prev),abs(d)) <= pole_cap*d_anchor) then + lo = scale_prev + hi = scale + sign_lo = sign(1.0_r8,d_prev) + do while (abs(hi-lo) > alpha_scale_tol) + mid = 0.5_r8*(lo+hi) + call bal_delta_at_scale(mid,dm) + if (ieee_is_finite(dm) .and. + $sign(1.0_r8,dm) == sign_lo) then + lo = mid + else + hi = mid + end if + end do + locstab%fs(ipsi,5) = alpha_ref*0.5_r8*(lo+hi) + return + end if + d_prev = d + scale_prev = scale + end do + end subroutine bal_alpha_first_boundary + + end module stride_bal_mod diff --git a/stride/dcon_mod.f b/stride/dcon_mod.f index c586fb721..27f07027d 100755 --- a/stride/dcon_mod.f +++ b/stride/dcon_mod.f @@ -59,10 +59,10 @@ MODULE stride_dcon_mod LOGICAL :: bin_sol=.FALSE. LOGICAL :: bal_flag=.FALSE. + LOGICAL :: mer_flag=.FALSE. LOGICAL :: mat_flag=.FALSE. LOGICAL :: ode_flag=.FALSE. LOGICAL :: vac_flag=.FALSE. - LOGICAL :: mer_flag=.FALSE. LOGICAL :: crit_break=.TRUE. LOGICAL :: node_flag=.FALSE. diff --git a/stride/mercier.f b/stride/mercier.f deleted file mode 100755 index 61af037ca..000000000 --- a/stride/mercier.f +++ /dev/null @@ -1,105 +0,0 @@ -c----------------------------------------------------------------------- -c file mercier.f. -c computes mercier criterion and related quantities. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c code organization. -c----------------------------------------------------------------------- -c 0. stride_mercier_mod. -c 1. mercier_scan. -c----------------------------------------------------------------------- -c subprogram 0. mercier_mod. -c module declarations. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - MODULE stride_mercier_mod - USE stride_dcon_mod - IMPLICIT NONE - - CONTAINS -c----------------------------------------------------------------------- -c subprogram 1. mercier_scan. -c evaluates mercier criterion and related quantities. -c----------------------------------------------------------------------- -c----------------------------------------------------------------------- -c declarations. -c----------------------------------------------------------------------- - SUBROUTINE mercier_scan - - INTEGER :: ipsi,itheta - REAL(r8) :: bsq,chi1,di,dpsisq,eta,h,jac,p1,psifac,q,q1,r, - $ rfac,term,theta,twopif,v1,v2,v21,v22,v23,v33 - REAL(r8), DIMENSION(:), POINTER :: avg - TYPE(spline_type), TARGET :: ff -c----------------------------------------------------------------------- -c prepare spline types. -c----------------------------------------------------------------------- - CALL spline_alloc(ff,mtheta,5) - ff%xs=rzphi%ys -c----------------------------------------------------------------------- -c compute surface quantities. -c----------------------------------------------------------------------- - DO ipsi=0,mpsi - psifac=sq%xs(ipsi) - twopif=sq%fs(ipsi,1) - p1=sq%fs1(ipsi,2) - v1=sq%fs(ipsi,3) - v2=sq%fs1(ipsi,3) - q=sq%fs(ipsi,4) - q1=sq%fs1(ipsi,4) - chi1=twopi*psio -c----------------------------------------------------------------------- -c evaluate coordinates and jacobian. -c----------------------------------------------------------------------- - DO itheta=0,mtheta - CALL bicube_eval(rzphi,rzphi%xs(ipsi),rzphi%ys(itheta),1) - theta=rzphi%ys(itheta) - rfac=SQRT(rzphi%f(1)) - eta=twopi*(theta+rzphi%f(2)) - r=ro+rfac*COS(eta) - jac=rzphi%f(4) -c----------------------------------------------------------------------- -c evaluate other local quantities. -c----------------------------------------------------------------------- - v21=rzphi%fy(1)/(2*rfac*jac) - v22=(1+rzphi%fy(2))*twopi*rfac/jac - v23=rzphi%fy(3)*r/jac - v33=twopi*r/jac - bsq=chi1**2*(v21**2+v22**2+(v23+q*v33)**2) - dpsisq=(twopi*r)**2*(v21**2+v22**2) -c----------------------------------------------------------------------- -c evaluate integrands. -c----------------------------------------------------------------------- - ff%fs(itheta,1)=bsq/dpsisq - ff%fs(itheta,2)=1/dpsisq - ff%fs(itheta,3)=1/bsq - ff%fs(itheta,4)=1/(bsq*dpsisq) - ff%fs(itheta,5)=bsq - ff%fs(itheta,:)=ff%fs(itheta,:)*jac/v1 - ENDDO -c----------------------------------------------------------------------- -c integrate quantities with respect to theta. -c----------------------------------------------------------------------- - CALL spline_fit(ff,"periodic") - CALL spline_int(ff) - avg => ff%fsi(mtheta,:) -c----------------------------------------------------------------------- -c evaluate mercier criterion and related quantities. -c----------------------------------------------------------------------- - term=twopif*p1*v1/(q1*chi1**3)*avg(2) - di=-.25+term*(1-term)+p1*(v1/(q1*chi1**2))**2*avg(1) - $ *(p1*(avg(3)+(twopif/chi1)**2*avg(4))-v2/v1) - h=twopif*p1*v1/(q1*chi1**3)*(avg(2)-avg(1)/avg(5)) - locstab%fs(ipsi,1)=di*locstab%xs(ipsi) - locstab%fs(ipsi,2)=(di+(h-0.5)**2)*locstab%xs(ipsi) - locstab%fs(ipsi,3)=h - ENDDO - CALL spline_dealloc(ff) -c----------------------------------------------------------------------- -c terminate. -c----------------------------------------------------------------------- - RETURN - END SUBROUTINE mercier_scan - END MODULE stride_mercier_mod diff --git a/stride/stride.F b/stride/stride.F index 10cd23f45..4a2dc387b 100644 --- a/stride/stride.F +++ b/stride/stride.F @@ -18,7 +18,6 @@ PROGRAM stride USE equil_mod USE equil_out_mod USE stride_bal_mod - USE stride_mercier_mod USE stride_ode_mod USE stride_free_mod USE omp_lib @@ -39,8 +38,9 @@ PROGRAM stride INTEGER :: ithread, mainlevelThreads - NAMELIST/stride_control/bal_flag,mat_flag,ode_flag,vac_flag, - $ mer_flag,fft_flag,node_flag,mthvac,sing_start,nn, + NAMELIST/stride_control/bal_flag,mer_flag, + $ mat_flag,ode_flag,vac_flag,fft_flag,node_flag,mthvac, + $ sing_start,nn, $ delta_mlow,delta_mhigh,delta_mband,thmax0,nstep,ksing, $ tol_nr,tol_r,crossover,ucrit,singfac_min,singfac_max, $ cyl_flag,dmlim,lim_flag,sas_flag,sing_order, @@ -63,7 +63,7 @@ PROGRAM stride c format statements. c----------------------------------------------------------------------- 10 FORMAT(/'ipsi',2x,'psifac',6x,'f',7x,'mu0 p',5x,'dvdpsi', - $ 6x,'q',10x,'di',9x,'dr',8x,'ca1'/) + $ 6x,'q',10x,'di',9x,'dr',6x,'bal_dp'/) 20 FORMAT(i3,1p,5e10.3,3e11.3) 30 FORMAT(/3x,"mlow",1x,"mhigh",1x,"mpert",1x,"mband",3x,"nn",2x, $ "lim_fl",2x,"dmlim",7x,"qlim",6x,"psilim"//5i6,l6,1p,3e11.3/) @@ -172,17 +172,14 @@ PROGRAM stride c prepare local stability criteria. c----------------------------------------------------------------------- CALL SYSTEM_CLOCK(COUNT=sTime) - CALL spline_alloc(locstab,mpsi,5) + CALL spline_alloc(locstab,mpsi,6) locstab%xs=sq%xs locstab%fs=0 locstab%name="locstb" - locstab%title=(/" di "," dr "," h "," ca1 "," ca2 "/) - IF (mer_flag) THEN - WRITE(*,*)"Evaluating Mercier criterion" - CALL mercier_scan - ENDIF - IF(bal_flag)THEN - WRITE(*,*)"Evaluating ballooning criterion" + locstab%title=(/" di "," dr "," h ","bal_dp","alpcrt", + $ " alpha"/) + IF(bal_flag .OR. mer_flag)THEN + WRITE(*,*)"Evaluating local ballooning stability" CALL bal_scan ENDIF CALL spline_fit(locstab,spline_str) diff --git a/stride/stride_netcdf.f b/stride/stride_netcdf.f index 939ebf075..ee5789116 100644 --- a/stride/stride_netcdf.f +++ b/stride/stride_netcdf.f @@ -59,7 +59,8 @@ SUBROUTINE stride_netcdf_out(wp,wv,wt,epi,evi,eti,dp, INTEGER :: i, ncid, $ i_dim, m_dim, mo_dim, p_dim, i_id, m_id, mo_id, p_id, - $ f_id, q_id, dv_id, mu_id, di_id, dr_id, ca_id, + $ f_id, q_id, dv_id, mu_id, di_id, dr_id, bal_dp_id, + $ bal_alpha_id, bal_alpha_ref_id, $ wp_id, wpv_id, wv_id, wvv_id, wt_id, wtv_id, $ r_dim, rp_dim, l_dim, lp_dim, r_id, rp_id, l_id, lp_id, $ pr_id, qr_id, dp_id, ap_id, bp_id, gp_id, dpp_id, @@ -224,7 +225,12 @@ SUBROUTINE stride_netcdf_out(wp,wv,wt,epi,evi,eti,dp, CALL check( nf90_def_var(ncid, "q", nf90_double, p_dim, q_id) ) CALL check( nf90_def_var(ncid, "di", nf90_double, p_dim, di_id) ) CALL check( nf90_def_var(ncid, "dr", nf90_double, p_dim, dr_id) ) - CALL check( nf90_def_var(ncid, "ca1", nf90_double, p_dim, ca_id)) + CALL check(nf90_def_var(ncid,"ballooning_Delta_prime", + $ nf90_double,p_dim,bal_dp_id)) + CALL check(nf90_def_var(ncid,"alpha_critical",nf90_double, + $ p_dim,bal_alpha_id)) + CALL check(nf90_def_var(ncid,"alpha",nf90_double, + $ p_dim,bal_alpha_ref_id)) IF(ode_flag .AND. vac_flag)THEN !shift to .OR. CALL check( nf90_def_var(ncid, "W_p_eigenvector", nf90_double, $ (/m_dim, mo_dim, i_dim/), wp_id) ) @@ -291,7 +297,9 @@ SUBROUTINE stride_netcdf_out(wp,wv,wt,epi,evi,eti,dp, CALL check( nf90_put_var(ncid,q_id, sq%fs(:,4))) CALL check( nf90_put_var(ncid,di_id, locstab%fs(:,1)/sq%xs(:))) CALL check( nf90_put_var(ncid,dr_id, locstab%fs(:,2)/sq%xs(:))) - CALL check( nf90_put_var(ncid,ca_id, locstab%fs(:,4))) + CALL check(nf90_put_var(ncid,bal_dp_id,locstab%fs(:,4))) + CALL check(nf90_put_var(ncid,bal_alpha_id,locstab%fs(:,5))) + CALL check(nf90_put_var(ncid,bal_alpha_ref_id,locstab%fs(:,6))) IF(debug_flag) PRINT *," - Putting matrix variables in netcdf" IF(ode_flag .AND. vac_flag)THEN !shift to .OR.