V3FIT
bongrid.f
1  SUBROUTINE bongrid(irho, ians)
2 
3 c First time through, form the THETA-ZETAH grid
4 c Every time through, evaluate B and QSAFETY on the grid for this RHO.
5 c Here, m is the poloidal mode number and n (and nh, for n-hat) is the
6 c toroidal mode number.
7 
8 C-----------------------------------------------
9 C M o d u l e s
10 C-----------------------------------------------
11  USE parambs
12  USE vmec0
13  IMPLICIT NONE
14 C-----------------------------------------------
15 C D u m m y A r g u m e n t s
16 C-----------------------------------------------
17  INTEGER irho, ians
18 C-----------------------------------------------
19 C L o c a l P a r a m e t e r s
20 C-----------------------------------------------
21  REAL(rprec), PARAMETER :: zero = 0, one = 1
22 C-----------------------------------------------
23 C L o c a l V a r i a b l e s
24 C-----------------------------------------------
25  INTEGER :: i, j, nh, m, mbuse1, imax, jmax
26  INTEGER, SAVE :: ihere = 0
27  INTEGER :: ij_max(2)
28  REAL(rprec), SAVE :: twopi, dth, dzetah, dthm, dzetahm
29  REAL(rprec) :: b
30 C-----------------------------------------------
31 
32 
33  IF (ihere .eq. 0) THEN
34 
35 c ALLOCATE ALL of the variables needed on the theta-zeta grid
36 
37  CALL allocate_angles
38 
39 c-----------------------------------------------------------------------
40 c Form the THETA-ZETAH grid.
41 
42  twopi = 8*atan(one)
43  dth = twopi/nthetah
44  dzetah = twopi/nzetah
45 
46  DO i = 1, nthetah
47  theta(i) = (i - 1)*dth
48  END DO
49  DO j = 1, nzetah
50  zetah(j) = (j - 1)*dzetah
51  END DO
52 
53 ! Form a finer mesh to evaluate fine grained bmax.
54 
55  dthm = dth/(nthetahm-1)
56  dzetahm = dzetah/(nzetahm-1)
57 
58 ! load sin and cosine arrays
59 
60  DO j = 1, nzetah
61  DO nh = 0, nbuse
62  sinnj(nh,j) = sin(zetasign*nh*zetah(j))
63  cosnj(nh,j) = cos(zetasign*nh*zetah(j))
64  ENDDO
65  ENDDO
66  DO i = 1, nthetah
67  DO m = -mbuse, mbuse
68  sinmi(m,i) = sin(m*theta(i))
69  cosmi(m,i) = cos(m*theta(i))
70  END DO
71  END DO
72  ihere = 1
73  ENDIF !end of ihere initial evaluation
74 
75 c Now evaluate B on the theta-zetah grid for this RHO using the EPSILONs just
76 c found. Loop over theta and phihat, summing up ALL the (m,nh) terms in B.
77 
78  DO j = 1, nzetah
79  DO i = 1, nthetah
80  b = zero
81  DO m = -mbuse, mbuse
82  DO nh = 0, nbuse
83  b = b + amnfit(irho,m,nh)*
84  1 (cosmi(m,i)*cosnj(nh,j)-sinmi(m,i)*sinnj(nh,j))
85  END DO
86  END DO
87  bfield(i,j) = abs(b)
88  END DO
89  END DO
90 
91 c find MAX of b on global mesh
92 
93  ij_max = maxloc(bfield)
94  imax = ij_max(1)
95  jmax = ij_max(2)
96 
97 c USE the theta and zeta from this search as the center of a finer search
98 
99 c first form the grid
100 
101  thetam(1) = theta(imax) - dth/2
102  zetahm(1) = zetah(jmax) - dzetah/2
103 
104  DO i = 2, nthetahm
105  thetam(i) = thetam(i-1) + dthm
106  ENDDO
107  DO j = 2, nzetahm
108  zetahm(j) = zetahm(j-1) + dzetahm
109  ENDDO
110 
111 c load the sines and cosines on the finer mesh
112 
113  DO j = 1, nzetahm
114  DO nh = 0, nbuse
115  sinnjm(nh,j) = sin(zetasign*nh*zetahm(j))
116  cosnjm(nh,j) = cos(zetasign*nh*zetahm(j))
117  ENDDO
118  ENDDO
119  DO i = 1, nthetahm
120  DO m = -mbuse, mbuse
121  sinmim(m,i) = sin(m*thetam(i))
122  cosmim(m,i) = cos(m*thetam(i))
123  END DO
124  END DO
125 
126 c evaluate b on the finer mesh
127 
128  DO j = 1, nzetahm
129  DO i = 1, nthetahm
130  b = zero
131  DO m = -mbuse, mbuse
132  DO nh = 0, nbuse
133  b = b + amnfit(irho,m,nh)*
134  1 (cosmim(m,i)*cosnjm(nh,j)-sinmim(m,i)*sinnjm(nh,j))
135  END DO
136  END DO
137  bfieldm(i,j) = abs(b)
138  END DO
139  END DO
140 
141 
142 c- evaluate bmax1(irho), thetamax(irho), b2avg(irho), and zetahmax(irho)
143 c based on finer mesh evaluation
144 
145  ij_max = maxloc(bfieldm)
146  imax = ij_max(1)
147  jmax = ij_max(2)
148  thetamax(irho) = thetam(imax)
149  zetahmax(irho) = zetahm(jmax)
150  bmax1(irho) = bfieldm(imax,jmax)
151 
152 c evaluate jacobian. Leave off flux surface quantites. Evaluate
153 c the SUM for later USE. Note that the value is not scaled to
154 c Bmax.
155 
156  gsqrt_b(:nthetah,:nzetah) = one/bfield(:nthetah,:nzetah)**2
157  sum_gsqrt_b = sum(gsqrt_b)
158 
159 c find b2avg LAB--boozer paper uses both bzero2 (1/leading coefficient of 1/b**2 expansion
160 c and <b**2>. They are the same (in boozer coordinates). Both result from
161 c flux surface averages. I will use <b2> everywhere
162 
163  b2avg(irho) = sum(bfield**2 * gsqrt_b)/sum_gsqrt_b
164 
165 c Scale the array BFIELD so that it CONTAINS B/BMAX instead of B.
166 
167  bfield(:nthetah,:nzetah) = bfield(:nthetah,:nzetah)/bmax1(irho)
168  WHERE(bfield .gt. one) bfield = one
169  b2obm = bfield**2
170 
171 c pressure related derivatives are needed
172 c first calculate difference denominators for pressure related derivatives
173 c difference array has differences on the full mesh
174 
175 c USE a parabolic fit near rho = 0 to get derivatives at 1st half mesh
176 
177  IF(irho .eq. 1) THEN
178  drho = (rhoar(2)**2 - rhoar(1)**2)/(2*rhoar(1))
179 
180 c USE slope of last two points at outer rho point
181 
182  ELSEIF(irho .eq. irup) THEN
183  drho = 0.5_dp*(d_rho(irho)+d_rho(irho-1))
184 
185 c ALL other points
186 
187  ELSE
188  drho = d_rho(irho) + 0.5_dp*(d_rho(irho+1)+d_rho(irho-1))
189  ENDIF
190 
191 c evaluate Electron temperature gradients in Kev
192 
193  IF (irho .ne. 1 .and. irho .ne. irup) temperho1 =
194  1 (tempe1(irho+1)-tempe1(irho-1))/drho
195  IF (irho .eq. 1) temperho1 = (tempe1(irho+1)-tempe1(irho))/drho
196  IF (irho .eq. irup) temperho1=(tempe1(irho)-tempe1(irho-1))/drho
197 
198 c evaluate Ion temperature gradients in Kev
199 
200  IF (irho .ne. 1 .and. irho .ne. irup) tempirho1 =
201  1 (tempi1(irho+1)-tempi1(irho-1))/drho
202  IF (irho .eq. 1)tempirho1 = (tempi1(irho+1)-tempi1(irho))/drho
203  IF (irho .eq. irup)temperho1=(tempi1(irho)-tempi1(irho-1))/drho
204 
205 c evaluate electron density gradients in 10**20 m-3
206 
207  IF (irho .ne. 1 .and. irho .ne. irup) densrho1 =
208  1 (dense(irho+1)-dense(irho-1))/drho
209  IF (irho .eq. 1) densrho1 = (dense(irho+1)-dense(irho))/drho
210  IF (irho .eq. irup) densrho1 = (dense(irho)-dense(irho-1))/drho
211 
212 c WRITE out the lower order Boozer coefficients
213 
214  mbuse1 = mbuse
215  IF (mbuse > 5) mbuse1 = 5 !mbuse1 > 5 will not fit on page
216 
217  WRITE (ians, 400, advance='no')
218  400 FORMAT(/' nh ')
219  DO m = -mbuse1, mbuse1
220  WRITE (ians, 402, advance='no') m
221  END DO
222  402 FORMAT(' m=',i2,' ')
223  WRITE (ians, '(a1)') ' '
224 c
225  DO nh = 0, nbuse
226  WRITE (ians, 406) nh, (amnfit(irho,m,nh),m=(-mbuse1),mbuse1)
227  END DO
228  406 FORMAT(1x,i2,1p,13e10.3)
229 
230 c Calculate the fraction trapped and fraction passing for the "equivalent"
231 
232  CALL tok_fraction(fttok(irho))
233 
234  fptok(irho) = one - fttok(irho)
235 
236  END SUBROUTINE bongrid