11 USE vmec_info,
ONLY: jcurrumnc, jcurrvmnc
16 ntheta=>nu_i, nzeta=>nv_i, mnmax=>mnmax_i, nsv=
17 ohs=>
ohs_i, nfp=>nfp_i
19 USE v3_utilities,
ONLY:
assert
21 USE descriptor_mod,
ONLY: iam
29 INTEGER,
PARAMETER ::
lin = 1
31 INTEGER,
PARAMETER ::
quad = 2
33 INTEGER,
PARAMETER ::
cub = 3
35 REAL (dp),
PARAMETER ::
p5 = 0.5_dp
41 INTEGER :: interp_type
44 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: ri, zi
46 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: r1_vmec, z1_vmec
47 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: ru_vmec, zu_vmec
48 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: rv_vmec, zv_vmec
50 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: r1s_vmec, r1ss_vmec
51 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: z1s_vmec, z1ss_vmec
53 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: r1su_vmec, z1su_vmec
54 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: r1sv_vmec, z1sv_vmec
56 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: rmnc_v, zmns_v
57 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: rmns_v, zmnc_v
59 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: r_ext, z_ext
60 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: ru_ext, zu_ext
61 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: rv_ext, zv_ext
63 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: r1_ves, z1_ves
64 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: ru_ves, zu_ves
65 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:) :: rv_ves, zv_ves
67 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: r_full, z_full
68 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: ru_full, zu_full
69 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: rv_full, zv_full
71 REAL(dp),
DIMENSION(:,:,:),
ALLOCATABLE :: sqrtg_full
73 REAL(dp) :: s_edge, ming, maxg
80 SUBROUTINE grid_extender(wout_file, itype, istat)
82 USE v3_utilities,
ONLY:
assert
83 USE vmec_info,
ONLY: rmnc_i, rmns_i, zmns_i, zmnc_i, &
84 jcurrumnc, jcurrvmnc, jcurrumns, jcurrvmns
85 USE descriptor_mod,
ONLY: siesta_comm
93 CHARACTER(*),
INTENT(in) :: wout_file
94 CHARACTER(*),
INTENT(in) :: itype
95 INTEGER,
INTENT(out) :: istat
97 CHARACTER(LEN=10) :: chfit
98 CHARACTER(LEN=100) :: device
100 INTEGER :: j, m, n, u, v, i, k, js, js1, nsp1
101 REAL(dp) :: rho, vol0, vol1, vols, rho1, rho2, rho3, vp1, s_ext, delv
102 REAL(dp),
DIMENSION(:,:),
ALLOCATABLE :: r12, rs12, zs12, ru12, zu12
103 REAL(dp),
DIMENSION(:),
ALLOCATABLE :: vp
104 REAL(dp),
DIMENSION(:,:,:),
ALLOCATABLE :: tmp, tmp2
105 REAL(dp),
DIMENSION(:),
ALLOCATABLE :: tmp1
109 IF (.NOT.l_vessel)
RETURN
113 i = index(wout_file,
'.', back=.true.)
114 j = index(wout_file,
'_')
116 device = wout_file(j+1:i-1)
118 device = wout_file(1:i)
123 IF (index(itype,
'lin').EQ.1 .OR. index(itype,
'LIN').EQ.1)
THEN
124 interp_type =
lin; chfit =
"linear"
125 ELSE IF (index(itype,
'quad').EQ.1 .OR. index(itype,
'QUAD').EQ.1)
THEN
126 interp_type =
quad; chfit =
"quadratic"
127 ELSE IF (index(itype,
'cub').EQ.1 .OR. index(itype,
'CUB').EQ.1)
THEN
128 interp_type =
cub; chfit =
"cubic"
130 CALL assert(.false.,
'Unknown interpolation <TYPE> specified')
133 ALLOCATE(ri(ntheta,nzeta), zi(ntheta,nzeta))
134 ALLOCATE(r1_ves(ntheta,nzeta), z1_ves(ntheta,nzeta),
135 ru_ves(ntheta,nzeta), zu_ves(ntheta,nzeta),
136 rv_ves(ntheta,nzeta), zv_ves(ntheta,nzeta))
139 four =>
fourier_class(mpol_v, ntor_v, ntheta, nzeta, nfp, lasym)
142 rmnc_v = rmnc_v/four%orthonorm
143 zmns_v = zmns_v/four%orthonorm
149 CALL four%toijsp(rmnc_v, ru_ves,
f_du,
f_cos)
150 CALL four%toijsp(zmns_v, zu_ves,
f_du,
f_sin)
152 CALL four%toijsp(rmnc_v, rv_ves,
f_dv,
f_cos)
153 CALL four%toijsp(zmns_v, zv_ves,
f_dv,
f_sin)
160 vol0 = get_volume(zero)
161 vol1 = get_volume(one)
164 s_edge = sqrt(1 + 2*(vol1 - vol0)/vp1)
167 nse = nint(s_edge*ohs) + 1
170 s_edge = (nsv - 1)*hs_i + 1
173 delv = vol1 - (vol0 +
p5*vp1*(s_edge*s_edge - 1))
174 delv = delv/(s_edge-1)**2
176 ALLOCATE(r_ext(ntheta,nzeta,2:nsv), z_ext(ntheta,nzeta,2:nsv),
177 ru_ext(ntheta,nzeta,2:nsv), zu_ext(ntheta,nzeta,2:nsv),
178 rv_ext(ntheta,nzeta,2:nsv), zv_ext(ntheta,nzeta,2:nsv),
183 s_ext = 1 + (j-1)*hs_i
186 vols = vol0 +
p5*vp1*(s_ext**2-1)
187 vols = vols + delv*(s_ext-1)**2
188 vols = min(vols, vol1)
189 CALL remap_radial(rho, vols)
192 IF (interp_type.EQ.
lin)
THEN
193 r_ext(:,:,j) = rho1*r1_vmec + rho*r1_ves
194 z_ext(:,:,j) = rho1*z1_vmec + rho*z1_ves
195 ru_ext(:,:,j) = rho1*ru_vmec + rho*ru_ves
196 zu_ext(:,:,j) = rho1*zu_vmec + rho*zu_ves
197 rv_ext(:,:,j) = rho1*rv_vmec + rho*rv_ves
198 zv_ext(:,:,j) = rho1*zv_vmec + rho*zv_ves
201 IF (interp_type .eq.
quad)
THEN
203 r_ext(:,:,j) =
p5*r1s_vmec*(1 - rho2 - rho1*rho1)
204 + r1_vmec*(1-rho2) + r1_ves*rho2
205 z_ext(:,:,j) =
p5*z1s_vmec*(1 - rho2 - rho1*rho1)
206 + z1_vmec*(1-rho2) + z1_ves*rho2
207 ru_ext(:,:,j) =
p5*r1su_vmec*(1 - rho2 - rho1*rho1)
208 + ru_vmec*(1-rho2) + ru_ves*rho2
209 zu_ext(:,:,j) =
p5*z1su_vmec*(1 - rho2 - rho1*rho1)
210 + zu_vmec*(1-rho2) + zu_ves*rho2
211 rv_ext(:,:,j) =
p5*r1sv_vmec*(1 - rho2 - rho1*rho1)
212 + rv_vmec*(1-rho2) + rv_ves*rho2
213 zv_ext(:,:,j) =
p5*z1sv_vmec*(1 - rho2 - rho1*rho1)
214 + zv_vmec*(1-rho2) + zv_ves*rho2
215 ELSE IF (interp_type .eq.
cub)
THEN
217 CALL assert(.false.,
'Unknown interpolation <TYPE> specified'
223 ALLOCATE(r_full(ntheta,nzeta,nse), z_full(ntheta,nzeta,nse),
224 ru_full(ntheta,nzeta,nse), zu_full(ntheta,nzeta,nse),
225 rv_full(ntheta,nzeta,nse), zv_full(ntheta,nzeta,nse))
227 r_full(:,:,1:ns) = r1_i
228 r_full(:,:,ns + 1:nse) = r_ext
229 z_full(:,:,1:ns) = z1_i
230 z_full(:,:,ns + 1:nse) = z_ext
231 ru_full(:,:,1:ns) = ru_i
232 ru_full(:,:,ns + 1:nse) = ru_ext
233 zu_full(:,:,1:ns) = zu_i
234 zu_full(:,:,ns + 1:nse) = zu_ext
235 rv_full(:,:,1:ns) = rv_i
236 rv_full(:,:,ns + 1:nse) = rv_ext
237 zv_full(:,:,1:ns) = zv_i
238 zv_full(:,:,ns + 1:nse) = zv_ext
245 1000
FORMAT(
'Using ',i4,
' Total Surfaces.',/,
246 'Number of VMEC surfaces ',i4,/,
247 'Number of extended surfaces ',i4)
255 IF (nsp .NE. ns)
THEN
262 ALLOCATE(r1_i(ntheta,nzeta,ns), z1_i(ntheta,nzeta,ns),
263 ru_i(ntheta,nzeta,ns), zu_i(ntheta,nzeta,ns),
264 rv_i(ntheta,nzeta,ns), zv_i(ntheta,nzeta,ns))
265 r1_i = r_full(:,:,1:ns)
266 z1_i = z_full(:,:,1:ns)
267 ru_i = ru_full(:,:,1:ns)
268 zu_i = zu_full(:,:,1:ns)
269 rv_i = rv_full(:,:,1:ns)
270 zv_i = zv_full(:,:,1:ns)
273 DEALLOCATE(r_full, z_full)
274 DEALLOCATE(ru_full, zu_full)
275 DEALLOCATE(rv_full, zv_full)
277 IF (nsp .NE. ns)
THEN
279 ALLOCATE(tmp(0:mpol,-ntor:ntor,ns), tmp2(0:mpol,-ntor:ntor, ns)
280 tmp(:,:,1:nsp) = rmnc_i
281 tmp2(:,:,1:nsp) = zmns_i
282 DEALLOCATE(rmnc_i, zmns_i)
283 ALLOCATE(rmnc_i(0:mpol,-ntor:ntor,ns),
284 zmns_i(0:mpol,-ntor:ntor,ns), stat=istat)
285 rmnc_i(:,:,1:nsp) = tmp(:,:,1:nsp)
286 zmns_i(:,:,1:nsp) = tmp2(:,:,1:nsp)
292 rmnc_i(:,:,js) = rmnc_i(:,:,js)
294 zmns_i(:,:,js) = zmns_i(:,:,js)
300 tmp(:,:,1:nsp) = jcurrumnc
301 DEALLOCATE(jcurrumnc)
302 ALLOCATE(jcurrumnc(0:mpol,-ntor:ntor,ns))
306 tmp(:,:,1:nsp) = jcurrvmnc
307 DEALLOCATE(jcurrvmnc)
308 ALLOCATE(jcurrvmnc(0:mpol,-ntor:ntor,ns))
315 tmp1(1:nsp) = phipf_i
317 ALLOCATE(phipf_i(ns))
321 tmp1(1:nsp) = chipf_i
323 ALLOCATE(chipf_i(ns))
326 tmp1(1:nsp) = presf_i
327 tmp1(nsp + 1:) = presf_i(nsp)
329 ALLOCATE(presf_i(ns))
334 tmp(:,:,1:nsp) = rmns_i
335 tmp2(:,:,1:nsp) = zmnc_i
336 DEALLOCATE(rmns_i, zmnc_i)
337 ALLOCATE(rmns_i(0:mpol,-ntor:ntor,ns),
338 zmnc_i(0:mpol,-ntor:ntor,ns), stat=istat)
339 rmns_i(:,:,1:nsp) = tmp(:,:,1:nsp)
340 zmnc_i(:,:,1:nsp) = tmp2(:,:,1:nsp)
346 rmns_i(:,:,js) = rmns_i(:,:,js)
348 zmnc_i(:,:,js) = zmnc_i(:,:,js)
353 tmp(:,:,1:nsp)=jcurrumns
354 DEALLOCATE(jcurrumns)
355 ALLOCATE(jcurrumns(0:mpol,-ntor:ntor,ns))
359 tmp(:,:,1:nsp)=jcurrvmns
360 DEALLOCATE(jcurrvmns)
361 ALLOCATE(jcurrvmns(0:mpol,-ntor:ntor,ns))
365 DEALLOCATE(tmp, tmp2)
373 WRITE(1600,*) r1_i(u,v,js), z1_i(u,v,js)
380 ALLOCATE (r12(ntheta,nzeta), &
381 rs12(ntheta,nzeta), &
382 zs12(ntheta,nzeta), &
383 ru12(ntheta,nzeta), &
384 zu12(ntheta,nzeta), &
385 sqrtg_full(ntheta,nzeta,nse), &
387 CALL assert(istat.eq.0,
' Allocation error in grid_extender')
388 sqrtg_full(:,:,1) = 0
391 r12 =
p5*(r1_i(:,:,js) + r1_i(:,:,js1))
392 rs12 = (r1_i(:,:,js) - r1_i(:,:,js1))*ohs
393 zs12 = (z1_i(:,:,js) - z1_i(:,:,js1))*ohs
394 ru12 =
p5*(ru_i(:,:,js) + ru_i(:,:,js1))
395 zu12 =
p5*(zu_i(:,:,js) + zu_i(:,:,js1))
396 sqrtg_full(:,:,js) = r12*(ru12*zs12 - rs12*zu12)
399 DEALLOCATE (r12, rs12, zs12, ru12, zu12)
401 ming = minval(sqrtg_full(:,:,ns:nse) )
402 maxg = maxval(sqrtg_full(:,:,ns:nse))
403 print *,
' IAM: ', iam,
'MING: ', ming,
' MAXG: ',maxg
404 CALL assert(ming*maxg.GT.zero,
' Jacobian changed sign in grid_extender'
410 ming = minval(sqrtg_full(:,:,js) )
411 maxg = maxval(sqrtg_full(:,:,js))
412 WRITE(5000,
'(i4,1p,3E14.4)') js, -vp(js), ming, maxg
415 DEALLOCATE (vp,r_ext, z_ext, ru_ext, zu_ext)
419 DEALLOCATE(r1s_vmec, r1su_vmec, r1sv_vmec, z1s_vmec, z1su_vmec, z1sv_vmec
421 END SUBROUTINE grid_extender
423 SUBROUTINE init_extender
426 ALLOCATE(r1_vmec(ntheta,nzeta),z1_vmec(ntheta,nzeta))
427 ALLOCATE(ru_vmec(ntheta,nzeta),zu_vmec(ntheta,nzeta))
428 ALLOCATE(rv_vmec(ntheta,nzeta),zv_vmec(ntheta,nzeta))
429 r1_vmec =
r1_i(:,:,ns); z1_vmec =
z1_i(:,:,ns)
430 ru_vmec =
ru_i(:,:,ns); zu_vmec(:,:) =
zu_i(:,:,ns)
431 rv_vmec =
rv_i(:,:,ns); zv_vmec(:,:) =
zv_i(:,:,ns)
434 ALLOCATE(r1s_vmec(ntheta,nzeta),z1s_vmec(ntheta,nzeta))
435 ALLOCATE(r1su_vmec(ntheta,nzeta),z1su_vmec(ntheta,nzeta))
436 ALLOCATE(r1sv_vmec(ntheta,nzeta),z1sv_vmec(ntheta,nzeta))
440 r1s_vmec=(r1_vmec -
r1_i(:,:,ns-1))*ohs*fac
441 z1s_vmec=(z1_vmec -
z1_i(:,:,ns-1))*ohs*fac
443 r1su_vmec = (ru_vmec -
ru_i(:,:,ns-1))*ohs*fac
444 z1su_vmec = (zu_vmec -
zu_i(:,:,ns-1))*ohs*fac
446 r1sv_vmec = (rv_vmec -
rv_i(:,:,ns-1))*ohs*fac
447 z1sv_vmec = (zv_vmec -
zv_i(:,:,ns-1))*ohs*fac
449 END SUBROUTINE init_extender
466 REAL(dp),
DIMENSION(:,:),
ALLOCATABLE :: r12
467 REAL(dp),
DIMENSION(:,:),
ALLOCATABLE :: rs12
468 REAL(dp),
DIMENSION(:,:),
ALLOCATABLE :: zs12
469 REAL(dp),
DIMENSION(:,:),
ALLOCATABLE :: ru12
470 REAL(dp),
DIMENSION(:,:),
ALLOCATABLE :: zu12
471 REAL(dp),
DIMENSION(:),
ALLOCATABLE :: vp
474 ALLOCATE(vp(ns - 1:ns), sqrtg_full(ntheta,nzeta,ns - 1:ns))
475 ALLOCATE(r12(ntheta,nzeta), ru12(ntheta,nzeta), rs12(ntheta,nzeta)
476 zu12(ntheta,nzeta), zs12(ntheta,nzeta))
484 rs12 = (
r1_i(:,:,js) -
r1_i(:,:,js1))*ohs
485 zs12 = (
z1_i(:,:,js) -
z1_i(:,:,js1))*ohs
488 sqrtg_full(:,:,js) = r12*(ru12*zs12 - rs12*zu12)
492 vp = twopi*twopi*abs(vp)
495 DEALLOCATE(vp, sqrtg_full, r12, ru12, rs12, zu12, zs12)
499 SUBROUTINE remap_radial(rho, vrho)
502 REAL(dp),
INTENT(INOUT) :: rho
503 REAL(dp),
INTENT(IN) :: vrho
504 REAL(dp) :: rootl, rootr, vol, delv
505 REAL(dp),
PARAMETER :: tol=1.e-6_dp
515 vol = get_volume(rho)
517 IF (abs(delv) .LT. tol*vrho)
EXIT
518 IF (delv .GT. 0)
THEN
523 rho = rootl+(rootr-rho)/2
526 END SUBROUTINE remap_radial
528 REAL(dp) function get_volume (rho)
537 REAL(dp) :: zu, r1, dnorm, odelu
542 dnorm =
p5*twopi/(nzeta*odelu)
543 IF (.NOT.
lasym) odelu = 2*odelu
544 CALL loadsurfacepoints(rho)
547 zu = (zi(i+1,j)-zi(i,j))*odelu
548 r1 =
p5*(ri(i+1,j)+ri(i,j))
549 get_volume = get_volume + r1**2 * zu
552 zu = (zi(1,j)-zi(ntheta,j))*odelu
553 r1 =
p5*(ri(1,j)+ri(ntheta,j))
554 get_volume = get_volume + r1**2*zu
557 get_volume = dnorm*get_volume
559 END FUNCTION get_volume
561 SUBROUTINE loadsurfacepoints(rho)
563 ntheta=>nu_i, nzeta=>nv_i, mnmax=>mnmax_i, nsv=>ns_v
565 REAL(dp),
INTENT(IN) :: rho
566 REAL(dp) :: rho1, rho2, rho3
571 IF (interp_type .EQ.
lin)
THEN
572 ri = rho1*r1_vmec + rho*r1_ves
573 zi = rho1*z1_vmec + rho*z1_ves
576 IF (interp_type .EQ.
quad)
THEN
578 ri =
p5*r1s_vmec*(1 - rho2 - rho1*rho1) &
579 + r1_vmec*(1-rho2) + r1_ves*rho2
580 zi =
p5*z1s_vmec*(1 - rho2 - rho1*rho1) &
581 + z1_vmec*(1-rho2) + z1_ves*rho2
582 ELSE IF (interp_type .EQ.
cub)
THEN
585 CALL assert(.false.,
'Unknown interpolation <TYPE> specified')
588 END SUBROUTINE loadsurfacepoints
597 ntheta=>nu_i, nzeta=>nv_i, mnmax=>mnmax_i
620 OPEN(unit=iou, file=
vessel_file, status=
"old", form=
"formatted",
628 READ(iou,*,
END=100) n, m, rv1, zv1
629 mpol_v = max(m, mpol_v)
630 ntor_v = max(abs(n), ntor_v)
631 mnmax_v = mnmax_v + 1
637 WRITE (*,*) vessel_file
640 ALLOCATE(rmnc_v(0:mpol_v,-ntor_v:ntor_v),
641 zmns_v(0:mpol_v,-ntor_v:ntor_v))
645 ALLOCATE(rmns_v(0:mpol_v,-ntor_v:ntor_v),
646 zmnc_v(0:mpol_v,-ntor_v:ntor_v))
652 READ(iou,*,
END=101) n, m, rv1, zv1
653 CALL assert(m .ge. 0 .and. m .le. mpol_v,
654 'm out of bounds: read_vessel_file')
655 CALL assert(n .ge. -ntor_v .and. n .le. ntor_v,
656 'n out of bounds: read_vessel_file')
663 1000
FORMAT(
'Warning: Failed to open vessel file ',a)
664 1001
FORMAT(
'Warning: Failed to read vessel file ',a)
677 REAL(dp),
INTENT(out) :: average(nsmin:nsmax)
678 REAL(dp),
INTENT(in) :: q3d(ntheta,nzeta,nsmin:nsmax)
679 INTEGER,
INTENT(in) :: nsmin
680 INTEGER,
INTENT(in) :: nsmax
684 REAL(dp),
ALLOCATABLE,
DIMENSION(:,:,:) :: wint
687 ALLOCATE(wint(ntheta,nzeta,nsmin:nsmax))
693 average(i) = sum(q3d(:,:,i)*wint(:,:,i))