30 USE stel_kinds,
ONLY: rprec, iprec, cprec
40 REAL(rprec),
PARAMETER :: pi=3.14159265358979323846264338328_rprec
41 REAL(rprec),
PARAMETER :: twopi=6.28318530717958647692528677_rprec
42 REAL(rprec),
PARAMETER :: one = 1.0_rprec, zero = 0.0_rprec
47 REAL(rprec),
PARAMETER :: bsc_k2_def = 1.0e-7_rprec
48 REAL(rprec),
PARAMETER :: bsc_k2inv_def = 1.0e+7_rprec
53 REAL(rprec),
PARAMETER :: bsc_mach_eps = epsilon(one)
58 PRIVATE rprec, iprec, cprec, pi, twopi, one, zero, bsc_k2_def,
59 & bsc_k2inv_def, bsc_mach_eps
67 REAL(rprec) :: bsc_emcut = 0.01_rprec
128 CHARACTER (len=10) :: c_type
129 CHARACTER (len=30) :: s_name
130 CHARACTER (len=80) :: l_name
131 REAL(rprec) :: eps_sq
132 REAL(rprec) :: current
135 REAL(rprec) :: ave_n_area
136 REAL(rprec),
DIMENSION(3) :: xcent, enhat
137 REAL(rprec),
DIMENSION(:,:),
POINTER :: xnod => null()
138 REAL(rprec),
DIMENSION(:,:),
POINTER :: dxnod => null()
139 REAL(rprec),
DIMENSION(:,:),
POINTER :: ehnod => null()
140 REAL(rprec),
DIMENSION(:),
POINTER :: lsqnod => null()
141 REAL(rprec),
DIMENSION(:),
POINTER :: lnod => null()
142 REAL(rprec),
DIMENSION(:),
POINTER :: sens => null()
152 CHARACTER (len=30) :: s_name
153 CHARACTER (len=80) :: l_name
154 INTEGER(iprec) :: ncoil
155 TYPE (bsc_coil),
DIMENSION(:),
POINTER :: coils => null()
164 REAL(rprec),
DIMENSION(3,3) :: rot_matrix
165 REAL(rprec),
DIMENSION(3) :: c_of_rot
166 REAL(rprec),
DIMENSION(3) :: shift
174 INTERFACE ASSIGNMENT (=)
175 MODULE PROCEDURE bsc_coil_to_coil, bsc_coil_a_to_coil_a
182 MODULE PROCEDURE bsc_construct_coil, bsc_construct_coilcoll,
190 MODULE PROCEDURE bsc_destroy_coil, bsc_destroy_coilcoll,
198 MODULE PROCEDURE bsc_rot_shift_pt, bsc_rot_shift_pts,
199 & bsc_rot_shift_coil,
200 & bsc_rot_shift_coil_a, bsc_rot_shift_coilcoll
207 MODULE PROCEDURE bsc_a_coil, bsc_a_coil_a, bsc_a_coilcoll
214 MODULE PROCEDURE bsc_b_coil, bsc_b_coil_a, bsc_b_coilcoll
221 MODULE PROCEDURE bsc_fluxba_coil, bsc_fluxba_coil_a,
222 & bsc_fluxba_coilcoll
254 SUBROUTINE bsc_construct_coil(this,c_type,s_name,l_name,current, &
255 & xnod,rcirc,xcent,enhat,raux,anturns,xsarea,sen)
260 TYPE (bsc_coil),
INTENT (inout) :: this
261 CHARACTER (len=*),
INTENT(in) :: c_type
262 CHARACTER (len=*),
INTENT(in) :: s_name
263 CHARACTER (len=*),
INTENT(in) :: l_name
264 REAL(rprec),
INTENT(in) :: current
265 REAL(rprec),
DIMENSION(:,:),
INTENT(in),
OPTIONAL :: xnod
266 REAL(rprec),
INTENT(in),
OPTIONAL :: rcirc
267 REAL(rprec),
DIMENSION(3),
INTENT(in),
OPTIONAL :: xcent
268 REAL(rprec),
DIMENSION(3),
INTENT(in),
OPTIONAL :: enhat
269 REAL(rprec),
INTENT(in),
OPTIONAL :: raux
272 REAL(rprec),
INTENT(in),
OPTIONAL :: anturns
273 REAL(rprec),
INTENT(in),
OPTIONAL :: xsarea
279 REAL(rprec),
DIMENSION(:),
INTENT(in),
OPTIONAL :: sen
282 INTEGER(iprec) :: n_xnod_1, n_xnod_2, i, n, nm1
283 REAL(rprec) :: enlength
286 REAL(rprec),
ALLOCATABLE,
DIMENSION(:,:) :: xnod_temp
287 REAL(rprec),
ALLOCATABLE,
DIMENSION(:) :: sens_temp
288 REAL(rprec),
DIMENSION(3) :: vec_temp
289 INTEGER(iprec) :: itemp
290 INTEGER(iprec) :: itemp2
291 REAL(rprec) :: lsqnod_temp
301 this % s_name = s_name
302 this % l_name = l_name
303 this % current = current
304 IF (
PRESENT(raux))
THEN
312 CASE (
'fil_loop',
'floop',
'fil_rogo',
'fil_rogo_s')
313 this % c_type = c_type
316 IF (.not.
PRESENT(xnod))
THEN
317 WRITE(*,*)
'FATAL: bsc_construct_coil '
318 WRITE(*,*)
'argument xnod not present for c_type =', c_type
323 n_xnod_1 =
SIZE(xnod,1)
324 n_xnod_2 =
SIZE(xnod,2)
325 IF (n_xnod_1 .ne. 3)
THEN
326 stop .ne.
' FATAL:sub. bsc_construct: n_xnod_1 3'
327 ELSE IF (n_xnod_2 .lt. 2)
THEN
328 stop
' FATAL:sub. bsc_construct: n_xnod_2 < 2'
333 ALLOCATE(xnod_temp(3,n_xnod_2 + 1))
334 IF (
PRESENT(sen))
THEN
335 ALLOCATE(sens_temp(n_xnod_2))
340 xnod_temp(1:3,1) = xnod(1:3,1)
342 vec_temp(1:3) = xnod_temp(1:3,itemp) - xnod(1:3,i)
343 lsqnod_temp = dot_product(vec_temp,vec_temp)
344 IF (lsqnod_temp .eq. zero)
THEN
347 IF (
PRESENT(sen))
THEN
349 sens_temp(itemp) = sum(sen(itemp2:i - 1))
350 & /
SIZE(sen(itemp2:i - 1))
355 xnod_temp(1:3,itemp) = xnod(1:3,i)
364 IF ((c_type .eq.
'fil_loop') .or. (c_type .eq.
'floop'))
THEN
365 vec_temp(1:3) = xnod_temp(1:3,itemp) - xnod(1:3,1)
366 lsqnod_temp = dot_product(vec_temp,vec_temp)
367 IF (lsqnod_temp .eq. zero)
THEN
372 IF (itemp .eq. 1)
THEN
373 stop .eq.
' FATAL:sub. bsc_construct: itemp 1'
377 IF ((c_type .eq.
'fil_loop') .or. (c_type .eq.
'floop'))
THEN
378 IF (itemp .eq. 2)
THEN
379 WRITE (6,*)
' WARNING: bsc_construct: one straight ',
380 &
' filament for coil: ', trim(s_name)
381 WRITE (6,*)
' (this is a straight coil filament)'
384 xnod_temp(1:3,itemp) = xnod(1:3,1)
395 ALLOCATE(this % xnod(3,n))
396 ALLOCATE(this % dxnod(3,nm1))
397 ALLOCATE(this % ehnod(3,nm1))
398 ALLOCATE(this % lsqnod(nm1))
399 ALLOCATE(this % lnod(nm1))
400 ALLOCATE(this % sens(nm1))
405 this % xnod(1:3,1:n) = xnod_temp(1:3,1:n)
406 DEALLOCATE(xnod_temp)
408 IF (
PRESENT(sen))
THEN
409 this % sens(1:nm1) = sens_temp(1:nm1)
416 this % dxnod(1:3,1:nm1) = this % xnod(1:3,2:n)
417 & - this % xnod(1:3,1:nm1)
420 this % lsqnod(1:nm1) = this % dxnod(1,1:nm1)**2 +
421 & this % dxnod(2,1:nm1)**2 + this % dxnod(3,1:nm1)**2
422 IF (any(this % lsqnod(1:nm1) .eq. zero))
423 & stop
'FATAL: bsc_construct_coil : lsqnod must be nonzero'
424 this % eps_sq = bsc_mach_eps *
425 & minval(this % lsqnod(1:nm1))
430 this % lnod(1:nm1) = sqrt(this % lsqnod(1:nm1))
434 this % ehnod(i,1:nm1) = this % dxnod(i,1:nm1) /
438 CASE (
'fil_circ',
'fcirc')
439 this % c_type = c_type
442 IF (.not.
PRESENT(rcirc))
THEN
443 WRITE(6,*)
'FATAL: bsc_construct_coil '
444 WRITE(6,*)
'arg rcirc not present for c_type =', c_type
446 ELSE IF (.not.
PRESENT(xcent))
THEN
447 WRITE(6,*)
'FATAL: bsc_construct_coil '
448 WRITE(6,*)
'arg xcent not present for c_type =', c_type
450 ELSE IF (.not.
PRESENT(enhat))
THEN
451 WRITE(6,*)
'FATAL: bsc_construct_coil '
452 WRITE(6,*)
'arg enhat not present for c_type =', c_type
458 this % xcent(1:3) = xcent(1:3)
462 enlength = sqrt(dot_product(enhat,enhat))
463 IF (enlength .le. 1.e-40_rprec)
THEN
467 WRITE(*,*)
'WARN: bsc_contruct: enhat set to (0,0,1)'
469 this % enhat = enhat / enlength
473 WRITE(*,*)
'FATAL: bsc_contruct: unrecognized c_type = ',c_type
478 IF (this % c_type .eq.
'fil_rogo')
THEN
479 IF (.not.
PRESENT(anturns))
THEN
480 WRITE(6,*)
'FATAL: bsc_construct_coil '
481 WRITE(6,*)
'arg anturns not present for c_type =', c_type
483 ELSE IF (.not.
PRESENT(xsarea))
THEN
484 WRITE(6,*)
'FATAL: bsc_construct_coil '
485 WRITE(6,*)
'arg xsarea not present for c_type =', c_type
489 this % ave_n_area = xsarea * anturns / sum(this % lnod(1:nm1))
492 END SUBROUTINE bsc_construct_coil
497 SUBROUTINE bsc_construct_coilcoll(this,s_name,l_name,ncoil_init)
501 TYPE (bsc_coilcoll),
INTENT (inout) :: this
502 CHARACTER (len=*),
INTENT(in) :: s_name
503 CHARACTER (len=*),
INTENT(in) :: l_name
507 INTEGER(iprec),
INTENT(in),
OPTIONAL :: ncoil_init
510 INTEGER(iprec),
PARAMETER :: ncoil_init_def = 10
511 INTEGER(iprec) :: ncoil_init_use
517 IF (
ASSOCIATED(this % coils))
CALL bsc_destroy(this)
520 this % s_name = s_name
521 this % l_name = l_name
524 IF (
PRESENT(ncoil_init))
THEN
525 ncoil_init_use = max(2,ncoil_init)
527 ncoil_init_use = ncoil_init_def
530 ALLOCATE(this % coils(ncoil_init_use))
532 END SUBROUTINE bsc_construct_coilcoll
554 SUBROUTINE bsc_construct_rs(this,theta,phi,rot_ang, &
560 TYPE (bsc_rs),
INTENT(inout) :: this
561 REAL(rprec),
INTENT(in) :: theta
562 REAL(rprec),
INTENT(in) :: phi
563 REAL(rprec),
INTENT(in) :: rot_ang
566 REAL(rprec),
DIMENSION(3),
OPTIONAL,
INTENT(in) :: c_of_rot
567 REAL(rprec),
DIMENSION(3),
OPTIONAL,
INTENT(in) :: shift
570 REAL(rprec) :: omega(3)
571 REAL(rprec) :: cosrot, sinrot, onemcos
597 omega(1) = sin(theta) * cos(phi)
598 omega(2) = sin(theta) * sin(phi)
599 omega(3) = cos(theta)
601 cosrot = cos(rot_ang); sinrot = sin(rot_ang)
604 this % rot_matrix(1,1) = cosrot + onemcos * omega(1) ** 2
605 this % rot_matrix(1,2) = sinrot * omega(3) +
606 & onemcos * omega(1) * omega(2)
607 this % rot_matrix(1,3) = -sinrot * omega(2) +
608 & onemcos * omega(1) * omega(3)
610 this % rot_matrix(2,1) = -sinrot * omega(3) +
611 & onemcos * omega(1) * omega(2)
612 this % rot_matrix(2,2) = cosrot + onemcos * omega(2) ** 2
613 this % rot_matrix(2,3) = sinrot * omega(1) +
614 & onemcos * omega(2) * omega(3)
616 this % rot_matrix(3,1) = sinrot * omega(2) +
617 & onemcos * omega(1) * omega(3)
618 this % rot_matrix(3,2) = -sinrot * omega(1) +
619 & onemcos * omega(2) * omega(3)
620 this % rot_matrix(3,3) = cosrot + onemcos * omega(3) ** 2
626 IF (
PRESENT(c_of_rot))
THEN
627 this % c_of_rot = c_of_rot
629 this % c_of_rot = zero
633 IF (
PRESENT(shift))
THEN
639 END SUBROUTINE bsc_construct_rs
647 SUBROUTINE bsc_destroy_coil(this)
651 TYPE (bsc_coil),
INTENT(inout) :: this
659 this % current = zero
665 this % ave_n_area = zero
667 IF (
ASSOCIATED(this % xnod))
DEALLOCATE(this % xnod)
668 IF (
ASSOCIATED(this % dxnod))
DEALLOCATE(this % dxnod)
669 IF (
ASSOCIATED(this % ehnod))
DEALLOCATE(this % ehnod)
670 IF (
ASSOCIATED(this % lsqnod))
DEALLOCATE(this % lsqnod)
671 IF (
ASSOCIATED(this % lnod))
DEALLOCATE(this % lnod)
672 IF (
ASSOCIATED(this % sens))
DEALLOCATE(this % sens)
674 END SUBROUTINE bsc_destroy_coil
679 SUBROUTINE bsc_destroy_coil_a(this)
682 TYPE (bsc_coil),
DIMENSION(:),
INTENT(inout) :: this
687 CALL bsc_destroy_coil(this(n))
690 END SUBROUTINE bsc_destroy_coil_a
695 SUBROUTINE bsc_destroy_coilcoll(this)
698 TYPE (bsc_coilcoll),
INTENT(inout) :: this
701 INTEGER(iprec) :: ncoild
710 IF (
ASSOCIATED(this % coils))
THEN
711 ncoild =
SIZE(this % coils)
713 DEALLOCATE(this % coils)
716 END SUBROUTINE bsc_destroy_coilcoll
724 SUBROUTINE bsc_coil_to_coil(left,right)
728 TYPE (bsc_coil),
INTENT (out) :: left
729 TYPE (bsc_coil),
INTENT (in) :: right
732 REAL(rprec),
DIMENSION(:,:),
ALLOCATABLE :: temp2
733 REAL(rprec),
DIMENSION(:),
ALLOCATABLE:: temp1
736 INTEGER(iprec) :: n, nm1
744 left % c_type = right % c_type
745 left % s_name = right % s_name
746 left % l_name = right % l_name
747 left % current = right % current
748 left % eps_sq = right % eps_sq
749 left % raux = right % raux
750 left % rcirc = right % rcirc
751 left % ave_n_area = right % ave_n_area
752 left % xcent = right % xcent
753 left % enhat = right % enhat
757 IF ((right % c_type .eq.
'fil_loop') .or.
758 & (right % c_type .eq.
'floop') .or.
759 & (right % c_type .eq.
'fil_rogo') .or.
760 & (right % c_type .eq.
'fil_rogo_s'))
THEN
763 n =
SIZE(right % xnod,2)
776 temp2(1:3,1:n) = right % xnod(1:3,1:n)
777 IF (
ASSOCIATED(left % xnod))
DEALLOCATE(left % xnod)
778 ALLOCATE(left % xnod(3,n))
779 left % xnod(1:3,1:n) = temp2(1:3,1:n)
781 temp2(1:3,1:nm1) = right % dxnod(1:3,1:nm1)
782 IF (
ASSOCIATED(left % dxnod))
DEALLOCATE(left % dxnod)
783 ALLOCATE(left % dxnod(3,nm1))
784 left % dxnod(1:3,1:nm1) = temp2(1:3,1:nm1)
786 temp2(1:3,1:nm1) = right % ehnod(1:3,1:nm1)
787 IF (
ASSOCIATED(left % ehnod))
DEALLOCATE(left % ehnod)
788 ALLOCATE(left % ehnod(3,nm1))
789 left % ehnod(1:3,1:nm1) = temp2(1:3,1:nm1)
796 temp1(1:nm1) = right % lsqnod(1:nm1)
797 IF (
ASSOCIATED(left % lsqnod))
DEALLOCATE(left % lsqnod)
798 ALLOCATE(left % lsqnod(nm1))
799 left % lsqnod(1:nm1) = temp1(1:nm1)
801 temp1(1:nm1) = right % lnod(1:nm1)
802 IF (
ASSOCIATED(left % lnod))
DEALLOCATE(left % lnod)
803 ALLOCATE(left % lnod(nm1))
804 left % lnod(1:nm1) = temp1(1:nm1)
806 temp1(1:nm1) = right % sens(1:nm1)
807 IF (
ASSOCIATED(left % sens))
DEALLOCATE(left % sens)
808 ALLOCATE(left % sens(nm1))
809 left % sens(1:nm1) = temp1(1:nm1)
815 END SUBROUTINE bsc_coil_to_coil
820 SUBROUTINE bsc_coil_a_to_coil_a(left,right)
823 TYPE (bsc_coil),
DIMENSION(:),
INTENT (out) :: left
824 TYPE (bsc_coil),
DIMENSION(:),
INTENT (in) :: right
827 INTEGER(iprec) :: nleft, nright, i
835 IF (nleft .ne. nright)
THEN
836 WRITE(*,*) .ne.
'FATAL in bsc_coil_a_to_coil_a. nleft nright'
845 END SUBROUTINE bsc_coil_a_to_coil_a
862 SUBROUTINE bsc_append(this, newcoil)
866 TYPE (bsc_coilcoll),
INTENT(inout) :: this
867 TYPE (bsc_coil),
INTENT(in) :: newcoil
870 INTEGER(iprec) :: icoil, ncoild
871 INTEGER(iprec),
PARAMETER :: nincr = 10
873 TYPE (bsc_coil),
DIMENSION(:),
ALLOCATABLE :: coils_temp
878 IF (.NOT.
ASSOCIATED(this % coils))
THEN
881 ncoild =
SIZE(this % coils)
884 IF (this % ncoil + 1 .gt. ncoild)
THEN
887 ALLOCATE(coils_temp(ncoild))
896 coils_temp(icoil) = this % coils(icoil)
901 DEALLOCATE(this % coils)
904 ALLOCATE(this % coils(ncoild + nincr))
905 this % coils(1:ncoild) = coils_temp(1:ncoild)
909 DEALLOCATE(coils_temp)
915 this % ncoil = this % ncoil + 1
916 this % coils(this % ncoil) = newcoil
918 END SUBROUTINE bsc_append
944 SUBROUTINE bsc_rot_shift_pts(this,my_rs,xyz_dim)
949 REAL(rprec),
DIMENSION(:,:),
INTENT(inout) :: this
950 TYPE (bsc_rs),
INTENT(in) :: my_rs
953 INTEGER(iprec),
OPTIONAL,
INTENT(in) :: xyz_dim
956 REAL(rprec),
DIMENSION(:,:),
ALLOCATABLE :: this_temp
957 REAL(rprec),
DIMENSION(3) :: shift_2
958 INTEGER(iprec) :: xyz_dim_use
959 INTEGER(iprec) :: is1, js2, i, j, nm1
967 ALLOCATE(this_temp(is1,js2))
971 IF (
PRESENT(xyz_dim))
THEN
972 SELECT CASE (xyz_dim)
977 WRITE(*,*)
'ERROR: bsc_rot_shift_pts: xyz_dim invalid'
985 WRITE(*,*)
'ERROR: bsc_rot_shift_pts: xyz_dim invalid'
990 WRITE(*,*)
'WARNING: bsc_rot_shift_pts: xyz_dim has not a',
997 ELSE IF (js2 == 3)
THEN
1000 WRITE(*,*)
'ERROR: bsc_rot_shift_pts: points have no ',
1001 &
'dimension of length 3'
1008 shift_2(1:3) = my_rs % c_of_rot(1:3) + my_rs % shift(1:3)
1010 SELECT CASE (xyz_dim_use)
1012 this(1:3,1:js2) = this(1:3,1:js2) -
1013 & spread(my_rs % c_of_rot,2,js2)
1014 this_temp = matmul(my_rs % rot_matrix, this)
1015 this(1:3,1:js2) = this_temp(1:3,1:js2) + spread(shift_2,2,js2)
1018 this(1:is1,1:3) = this(1:is1,1:3) -
1019 & spread(my_rs % c_of_rot,1,is1)
1020 this_temp = matmul(this,transpose(my_rs % rot_matrix))
1021 this(1:is1,1:3) = this_temp(1:is1,1:3) + spread(shift_2,1,is1)
1024 WRITE(*,*)
'FATAL ERROR: bsc_rot_shift_pts: xyz_dim_use is',
1025 &
' not a valid value (1 or 2)'
1030 DEALLOCATE(this_temp)
1032 END SUBROUTINE bsc_rot_shift_pts
1037 SUBROUTINE bsc_rot_shift_pt(this,my_rs)
1042 REAL(rprec),
DIMENSION(3),
INTENT(inout) :: this
1043 TYPE (bsc_rs),
INTENT(in) :: my_rs
1046 REAL(rprec),
DIMENSION(3) :: this_temp
1049 this = this - my_rs % c_of_rot
1050 this_temp = matmul(my_rs % rot_matrix, this)
1051 this = this_temp + my_rs % c_of_rot + my_rs % shift
1053 END SUBROUTINE bsc_rot_shift_pt
1058 SUBROUTINE bsc_rot_shift_coil(this,my_rs)
1063 TYPE (bsc_coil),
INTENT(inout) :: this
1064 TYPE (bsc_rs),
INTENT(in) :: my_rs
1070 SELECT CASE (this % c_type)
1071 CASE (
'fil_loop',
'floop',
'fil_rogo')
1072 CALL bsc_rot_shift_coil_fil_loop(this,my_rs)
1074 CASE (
'fil_circ',
'fcirc')
1075 CALL bsc_rot_shift_coil_fil_circ(this,my_rs)
1078 WRITE(*,*)
'FATAL: bsc_rot_shift_coil: c_type unrecognized:',
1083 END SUBROUTINE bsc_rot_shift_coil
1088 SUBROUTINE bsc_rot_shift_coil_fil_loop(this,my_rs)
1094 TYPE (bsc_coil),
INTENT(inout) :: this
1095 TYPE (bsc_rs),
INTENT(in) :: my_rs
1098 INTEGER(iprec) :: nwire, nm1, i
1102 nwire =
SIZE(this % xnod,2)
1103 nm1 = max(1, nwire-1)
1107 CALL bsc_rot_shift_pts(this % xnod,my_rs,xyz_dim = 1_iprec)
1112 this % dxnod(1:3,1:nm1) = this % xnod(1:3,2:nwire)
1113 & - this % xnod(1:3,1:nm1)
1116 this % ehnod(i,1:nm1) = this % dxnod(i,1:nm1) /
1117 & this % lnod(1:nm1)
1120 END SUBROUTINE bsc_rot_shift_coil_fil_loop
1125 SUBROUTINE bsc_rot_shift_coil_fil_circ(this,my_rs)
1132 TYPE (bsc_coil),
INTENT(inout) :: this
1133 TYPE (bsc_rs),
INTENT(in) :: my_rs
1136 REAL(rprec),
DIMENSION(3,2) :: rot_vectors
1143 rot_vectors(1:3,1) = this % xcent(1:3)
1144 rot_vectors(1:3,2) = this % enhat(1:3) + this % xcent(1:3)
1146 CALL bsc_rot_shift_pts(rot_vectors,my_rs,xyz_dim = 1_iprec)
1148 this % xcent(1:3) = rot_vectors(1:3,1)
1149 this % enhat(1:3) = rot_vectors(1:3,2) - this % xcent(1:3)
1151 END SUBROUTINE bsc_rot_shift_coil_fil_circ
1156 SUBROUTINE bsc_rot_shift_coil_a(this,my_rs)
1161 TYPE (bsc_coil),
DIMENSION(:),
INTENT(inout) :: this
1162 TYPE (bsc_rs),
INTENT(in) :: my_rs
1165 INTEGER(iprec) :: n, nsize
1171 CALL bsc_rot_shift_coil(this(n),my_rs)
1174 END SUBROUTINE bsc_rot_shift_coil_a
1179 SUBROUTINE bsc_rot_shift_coilcoll(this,my_rs)
1183 TYPE (bsc_coilcoll),
INTENT(inout) :: this
1184 TYPE (bsc_rs),
INTENT(in) :: my_rs
1187 CALL bsc_rot_shift_coil_a(this % coils(1:this % ncoil),my_rs)
1189 END SUBROUTINE bsc_rot_shift_coilcoll
1206 SUBROUTINE bsc_a_coil(this,x,a,bsc_k2)
1211 TYPE (bsc_coil),
INTENT(in) :: this
1212 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1213 REAL(rprec),
DIMENSION(3),
INTENT(out) :: a
1216 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1219 REAL(rprec) :: bsc_k2_use
1222 SELECT CASE (this % c_type)
1223 CASE (
'fil_loop',
'floop')
1224 CALL bsc_a_coil_fil_loop(this,x,a)
1226 CASE (
'fil_circ',
'fcirc')
1227 CALL bsc_a_coil_fil_circ(this,x,a)
1231 CALL bsc_b_coil_fil_loop(this,x,a)
1233 a(1:3) = this % ave_n_area * a(1:3)
1238 CALL bsc_b_coil_fil_loop(this,x,a)
1241 WRITE(*,*)
'FATAL: bsc_a_coil: c_type unrecognized:',
1247 IF (
PRESENT(bsc_k2))
THEN
1250 bsc_k2_use = bsc_k2_def
1253 a(1:3) = this % current * bsc_k2_use * a(1:3)
1255 END SUBROUTINE bsc_a_coil
1260 SUBROUTINE bsc_a_coil_fil_loop(this,x,a)
1266 TYPE (bsc_coil),
INTENT(IN) :: this
1267 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1268 REAL(rprec),
DIMENSION(3),
INTENT(out) :: a
1271 INTEGER(iprec) :: n, nm1, i
1272 REAL(rprec),
DIMENSION(SIZE(this % xnod,2)) :: capR
1273 REAL(rprec),
DIMENSION(SIZE(this % xnod,2)-1) :: lnfactor
1276 n =
SIZE(this % xnod,2)
1280 capr = sqrt((x(1) - this % xnod(1,1:n))**2
1281 & + (x(2) - this % xnod(2,1:n))**2
1282 & + (x(3) - this % xnod(3,1:n))**2)
1285 lnfactor(1:nm1) = this % lnod(1:nm1)/(capr(1:nm1) + capr(2:n))
1286 CALL log_eps (lnfactor)
1294 a(i) = dot_product(this % ehnod(i,1:nm1),lnfactor(1:nm1))
1297 END SUBROUTINE bsc_a_coil_fil_loop
1302 SUBROUTINE bsc_a_coil_fil_circ(this,x,a)
1309 TYPE (bsc_coil),
INTENT(in) :: this
1310 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1311 REAL(rprec),
DIMENSION(3),
INTENT(out) :: a
1314 REAL(rprec),
PARAMETER :: two_third = 2._rprec / 3
1315 REAL(rprec),
PARAMETER :: pio16 = pi / 16
1316 REAL(rprec),
DIMENSION(3) :: rprime, rhoprime, rhophat, phiphat
1317 REAL(rprec) :: zprime, fsq, em, emone
1318 REAL(rprec) :: rhopmsq, rhopmag, f, rf, rd, brackg, aphi, radcc
1320 REAL(rprec),
PARAMETER :: c_gb1 = one
1321 REAL(rprec),
PARAMETER :: c_gb2 = 3._rprec / 4
1322 REAL(rprec),
PARAMETER :: c_gb3 = 75._rprec / 128
1323 REAL(rprec),
PARAMETER :: c_gb4 = 245._rprec / 512
1324 REAL(rprec),
PARAMETER :: c_gb5 = 6615._rprec / 16384
1325 REAL(rprec),
PARAMETER :: c_gb6 = 22869._rprec / 65536
1326 REAL(rprec),
PARAMETER :: c_gb7 = 1288287._rprec / 4194304
1331 rprime = x(1:3) - this % xcent(1:3)
1332 zprime = dot_product(rprime,this % enhat)
1333 rhoprime = rprime(1:3) - zprime * this % enhat(1:3)
1334 rhopmsq = dot_product(rhoprime,rhoprime)
1336 IF (rhopmsq .lt. 1.e-30_rprec)
THEN
1337 rhoprime(1) = 1.e-15_rprec
1340 rhopmsq = dot_product(rhoprime,rhoprime)
1342 rhopmag = sqrt(rhopmsq)
1343 rhophat(1:3) = rhoprime(1:3) / rhopmag
1344 radcc = this % rcirc
1347 fsq = one / ((rhopmag + radcc) ** 2 + zprime ** 2)
1349 em = 4 * rhopmag * radcc * fsq
1352 IF (em .gt. bsc_emcut)
THEN
1355 CALL bsc_cei(emone,rf,rd)
1356 brackg = two_third * rd - rf
1358 brackg = pio16 * em * (c_gb1 + em * (c_gb2 + em *
1359 & (c_gb3 + em * (c_gb4 + em * (c_gb5 + em * (c_gb6 + em *
1362 aphi = 4 * radcc * f * brackg
1367 phiphat(1) = this % enhat(2) * rhophat(3) -
1368 & this % enhat(3) * rhophat(2)
1369 phiphat(2) = this % enhat(3) * rhophat(1) -
1370 & this % enhat(1) * rhophat(3)
1371 phiphat(3) = this % enhat(1) * rhophat(2) -
1372 & this % enhat(2) * rhophat(1)
1373 a(1:3) = phiphat(1:3) * aphi
1375 END SUBROUTINE bsc_a_coil_fil_circ
1380 SUBROUTINE bsc_a_coil_a(this,x,a,bsc_k2)
1385 TYPE (bsc_coil),
DIMENSION(:),
INTENT(IN) :: this
1386 REAL(rprec),
DIMENSION(3),
INTENT(IN) :: x
1387 REAL(rprec),
DIMENSION(3),
INTENT(OUT) :: a
1390 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1393 INTEGER(iprec) :: i, n
1394 REAL(rprec),
DIMENSION(3,SIZE(this)) :: aarray
1403 CALL bsc_a_coil(this(i),x,aarray(1:3,i))
1407 a(1:3) = sum(aarray(1:3,1:n),2)
1410 IF (
PRESENT(bsc_k2))
THEN
1411 a(1:3) = a(1:3) * bsc_k2 * bsc_k2inv_def
1415 END SUBROUTINE bsc_a_coil_a
1420 SUBROUTINE bsc_a_coilcoll(this,x,a,bsc_k2)
1424 TYPE (bsc_coilcoll),
INTENT(in) :: this
1425 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1426 REAL(rprec),
DIMENSION(3),
INTENT(out) :: a
1429 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1438 CALL bsc_a_coil_a(this % coils(1:n),x,a(1:3))
1444 IF (
PRESENT(bsc_k2))
THEN
1445 a(1:3) = a(1:3) * bsc_k2 * bsc_k2inv_def
1448 END SUBROUTINE bsc_a_coilcoll
1465 SUBROUTINE bsc_b_coil (this,x,b,bsc_k2)
1470 TYPE (bsc_coil),
INTENT(in) :: this
1471 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1472 REAL(rprec),
DIMENSION(3),
INTENT(out) :: b
1475 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1478 REAL(rprec) :: bsc_k2_use
1482 SELECT CASE (this % c_type)
1483 CASE (
'fil_loop',
'floop')
1484 CALL bsc_b_coil_fil_loop(this,x,b)
1486 CASE (
'fil_circ',
'fcirc')
1487 CALL bsc_b_coil_fil_circ(this,x,b)
1491 WRITE(*,*)
'WARNING: bsc_b_coil: NOT YET IMPLEMENTED',
1496 WRITE(*,*)
'WARNING: bsc_b_coil: NOT YET IMPLEMENTED',
1500 WRITE(*,*)
'FATAL: bsc_b_coil: c_type unrecognized:',
1506 IF (
PRESENT(bsc_k2))
THEN
1509 bsc_k2_use = bsc_k2_def
1512 b(1:3) = this % current * bsc_k2_use * b(1:3)
1514 END SUBROUTINE bsc_b_coil
1520 SUBROUTINE bsc_b_coil_fil_loop (this,x,b)
1527 TYPE (bsc_coil),
INTENT(in) :: this
1528 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1529 REAL(rprec),
DIMENSION(3),
INTENT(out) :: b
1532 INTEGER(iprec) :: nm1, n, i, j, k
1533 REAL(rprec),
DIMENSION(SIZE(this % xnod,2)) :: capR
1534 REAL(rprec),
DIMENSION(3,SIZE(this % xnod,2)) :: capRv
1535 REAL(rprec),
DIMENSION(SIZE(this % xnod,2)-1) :: Rfactor, R1p2
1536 REAL(rprec),
DIMENSION(3,SIZE(this % xnod,2)-1) :: crossv
1539 n =
SIZE(this % xnod,2)
1544 caprv(i,1:n) = x(i) - this % xnod(i,1:n)
1549 capr(1:n) = sqrt(max(this % eps_sq,caprv(1,1:n) * caprv(1,1:n) +
1550 & caprv(2,1:n) * caprv(2,1:n) +
1551 & caprv(3,1:n) * caprv(3,1:n)))
1558 j = mod(i,3_iprec) + 1
1559 k = mod(j,3_iprec) + 1
1560 crossv(i,1:nm1) = this % dxnod(j,1:nm1) * caprv(k,1:nm1)
1561 & - this % dxnod(k,1:nm1) * caprv(j,1:nm1)
1564 r1p2(1:nm1) = capr(1:nm1) + capr(2:n)
1565 rfactor(1:nm1) = 2 * r1p2(1:nm1) / (capr(1:nm1) * capr(2:n) *
1566 & max(r1p2(1:nm1)*r1p2(1:nm1) - this % lsqnod(1:nm1),
1571 b(i) = dot_product(this%sens*crossv(i,1:nm1),rfactor(1:nm1))
1574 END SUBROUTINE bsc_b_coil_fil_loop
1579 SUBROUTINE bsc_b_coil_fil_circ(this,x,b)
1586 TYPE (bsc_coil),
INTENT(in) :: this
1587 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1588 REAL(rprec),
DIMENSION(3),
INTENT(out) :: b
1591 REAL(rprec),
PARAMETER :: third = one / 3
1592 REAL(rprec),
PARAMETER :: sixth = one / 6
1593 REAL(rprec),
PARAMETER :: two_third = 2._rprec / 3
1595 REAL(rprec),
DIMENSION(3) :: rprime, rhoprime, rhophat, phiphat
1596 REAL(rprec) :: zprime, fsq, fcube, em, emone, cfcube, geofac1
1597 REAL(rprec) :: rhopmsq, rhopmag, f, rf, rd, brackg, brackh,
1601 REAL(rprec) :: c_bz0, c_bz1, c_bz2, c_bz3, c_bz4, c_bz5, c_bz6
1602 REAL(rprec) :: cb_bz0, cb_bz1, cb_bz2, cb_bz3, cb_bz4, cb_bz5,
1604 REAL(rprec),
PARAMETER :: c_brho1 = one
1605 REAL(rprec),
PARAMETER :: c_brho2 = 5._rprec / 4
1606 REAL(rprec),
PARAMETER :: c_brho3 = 175._rprec / 128
1607 REAL(rprec),
PARAMETER :: c_brho4 = 735._rprec / 512
1608 REAL(rprec),
PARAMETER :: c_brho5 = 24255._rprec / 16384
1609 REAL(rprec),
PARAMETER :: c_brho6 = 99099._rprec / 65536
1610 REAL(rprec),
PARAMETER :: c_brho7 = 6441435._rprec / 4194304
1611 REAL(rprec),
PARAMETER :: ca_bz1 = 3._rprec / 4
1612 REAL(rprec),
PARAMETER :: ca_bz2 = 75._rprec / 128
1613 REAL(rprec),
PARAMETER :: ca_bz3 = 245._rprec / 512
1614 REAL(rprec),
PARAMETER :: ca_bz4 = 6615._rprec / 16384
1615 REAL(rprec),
PARAMETER :: ca_bz5 = 22869._rprec / 65536
1616 REAL(rprec),
PARAMETER :: ca_bz6 = 1288287._rprec / 4194304
1617 REAL(rprec),
PARAMETER :: pio4 = pi / 4
1618 REAL(rprec),
PARAMETER :: pi3o16 = 3._rprec * pi / 16
1622 rprime = x(1:3) - this % xcent(1:3)
1623 zprime = dot_product(rprime,this % enhat)
1624 rhoprime = rprime(1:3) - zprime * this % enhat(1:3)
1625 rhopmsq = dot_product(rhoprime,rhoprime)
1627 IF (rhopmsq .lt. 1.e-30_rprec)
THEN
1628 rhoprime(1) = 1.e-15_rprec
1631 rhopmsq = dot_product(rhoprime,rhoprime)
1633 rhopmag = sqrt(rhopmsq)
1634 rhophat(1:3) = rhoprime(1:3) / rhopmag
1635 radcc = this % rcirc
1638 fsq = one / ((rhopmag + radcc) ** 2 + zprime ** 2)
1640 em = 4 * rhopmag * radcc * fsq
1642 cfcube = 4 * radcc * f * fsq
1644 geofac1 = (radcc ** 2 + zprime ** 2) / rhopmag
1646 IF (em .gt. bsc_emcut)
THEN
1649 CALL bsc_cei(emone,rf,rd)
1650 brackg = two_third * rd - rf
1651 brackh = sixth * (-(1 + 3 * emone) * rd +
1652 & 3 * (1 + emone) * rf) / emone
1653 brfac1 = brackg + 2 * brackh
1654 brho = cfcube * zprime * brfac1
1655 bz = cfcube * (brackg * (geofac1 + radcc)
1656 & + brackh * (geofac1 - rhopmag))
1658 fac2 = pi3o16 * zprime * cfcube
1659 brho = fac2 * em * (c_brho1 + em * (c_brho2 +
1660 & em * (c_brho3 + em * (c_brho4 + em * (c_brho5 + em *
1661 & (c_brho6 + em * c_brho7))))))
1662 fac1 = cfcube * radcc * fsq * pio4
1663 add_on = zprime ** 2 + (radcc + rhopmag) * (radcc - rhopmag)
1664 cb_bz0 = (radcc + rhopmag) * rhopmag + 2 * add_on
1665 cb_bz1 = cb_bz0 + add_on
1666 cb_bz2 = cb_bz1 + add_on
1667 cb_bz3 = cb_bz2 + add_on
1668 cb_bz4 = cb_bz3 + add_on
1669 cb_bz5 = cb_bz4 + add_on
1670 cb_bz6 = cb_bz5 + add_on
1672 c_bz1 = ca_bz1 * cb_bz1
1673 c_bz2 = ca_bz2 * cb_bz2
1674 c_bz3 = ca_bz3 * cb_bz3
1675 c_bz4 = ca_bz4 * cb_bz4
1676 c_bz5 = ca_bz5 * cb_bz5
1677 c_bz6 = ca_bz6 * cb_bz6
1678 bz = fac1 * (c_bz0 + em * (c_bz1 + em * (c_bz2 +
1679 & em * (c_bz3 + em * (c_bz4 + em * (c_bz5 + em * c_bz6))))))
1683 b(1:3) = brho * rhophat(1:3) + bz * this % enhat(1:3)
1685 END SUBROUTINE bsc_b_coil_fil_circ
1690 SUBROUTINE bsc_b_coil_a(this,x,b,bsc_k2)
1695 TYPE (bsc_coil),
DIMENSION(:),
INTENT(IN) :: this
1696 REAL(rprec),
DIMENSION(3),
INTENT(IN) :: x
1697 REAL(rprec),
DIMENSION(3),
INTENT(OUT) :: b
1700 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1703 INTEGER(iprec) :: i, n
1704 REAL(rprec),
DIMENSION(3,SIZE(this)) :: barray
1710 CALL bsc_b_coil(this(i),x,barray(1:3,i))
1714 b = sum(barray(1:3,1:n),2)
1717 IF (
PRESENT(bsc_k2))
THEN
1718 b(1:3) = b(1:3) * bsc_k2 * bsc_k2inv_def
1721 END SUBROUTINE bsc_b_coil_a
1726 SUBROUTINE bsc_b_coilcoll(this,x,b,bsc_k2)
1731 TYPE (bsc_coilcoll),
INTENT(in) :: this
1732 REAL(rprec),
DIMENSION(3),
INTENT(in) :: x
1733 REAL(rprec),
DIMENSION(3),
INTENT(out) :: b
1736 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1745 CALL bsc_b_coil_a(this % coils(1:n),x,b(1:3))
1751 IF (
PRESENT(bsc_k2))
THEN
1752 b(1:3) = b(1:3) * bsc_k2 * bsc_k2inv_def
1755 END SUBROUTINE bsc_b_coilcoll
1775 SUBROUTINE bsc_fluxba_coil(coil_a,coil_b,len_integerate,flux, &
1786 TYPE (bsc_coil),
INTENT(in) :: coil_a
1787 TYPE (bsc_coil),
INTENT(in) :: coil_b
1788 REAL(rprec),
INTENT(IN) :: len_integerate
1789 REAL(rprec),
INTENT(out) :: flux
1792 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1795 REAL(rprec),
DIMENSION(:,:),
POINTER :: positions => null(),
1796 & tangents => null(), avecs => null()
1797 REAL(rprec),
DIMENSION(:),
POINTER :: sens => null()
1798 INTEGER(iprec) :: i, npoints
1805 CALL bsc_flux_pos(coil_b,len_integerate,positions,tangents,
1806 & avecs,sens,npoints)
1809 SELECT CASE (coil_b % c_type)
1812 CALL bsc_a(coil_a,positions(1:3,i),avecs(1:3,i))
1814 CASE (
'fil_rogo',
'fil_rogo_s')
1816 CALL bsc_b(coil_a,positions(1:3,i),avecs(1:3,i))
1821 CALL bsc_flux_sum(coil_b,positions,tangents,avecs,sens,npoints,
1825 IF (
PRESENT(bsc_k2))
THEN
1826 flux = flux * bsc_k2 * bsc_k2inv_def
1830 DEALLOCATE(avecs,positions,tangents,sens)
1833 END SUBROUTINE bsc_fluxba_coil
1838 SUBROUTINE bsc_fluxba_coil_a(coil_a,coil_b,len_integerate,flux, &
1849 TYPE (bsc_coil),
DIMENSION(:),
INTENT(IN) :: coil_a
1850 TYPE (bsc_coil),
INTENT(in) :: coil_b
1851 REAL(rprec),
INTENT(IN) :: len_integerate
1852 REAL(rprec),
INTENT(out) :: flux
1855 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1858 REAL(rprec),
DIMENSION(:,:),
POINTER :: positions => null(),
1859 & tangents => null(), avecs => null()
1860 REAL(rprec),
DIMENSION(:),
POINTER :: sens => null()
1861 INTEGER(iprec) :: i, npoints
1868 CALL bsc_flux_pos(coil_b,len_integerate,positions,tangents,
1869 & avecs,sens,npoints)
1872 SELECT CASE (coil_b % c_type)
1875 CALL bsc_a(coil_a,positions(1:3,i),avecs(1:3,i))
1877 CASE (
'fil_rogo',
'fil_rogo_s')
1879 CALL bsc_b(coil_a,positions(1:3,i),avecs(1:3,i))
1884 CALL bsc_flux_sum(coil_b,positions,tangents,avecs,sens,npoints,
1888 IF (
PRESENT(bsc_k2))
THEN
1889 flux = flux * bsc_k2 * bsc_k2inv_def
1893 DEALLOCATE(avecs,positions,tangents,sens)
1896 END SUBROUTINE bsc_fluxba_coil_a
1901 SUBROUTINE bsc_fluxba_coilcoll(coil_a,coil_b,len_integerate,flux, &
1912 TYPE (bsc_coilcoll),
INTENT(IN) :: coil_a
1913 TYPE (bsc_coil),
INTENT(in) :: coil_b
1914 REAL(rprec),
INTENT(IN) :: len_integerate
1915 REAL(rprec),
INTENT(out) :: flux
1918 REAL(rprec),
OPTIONAL,
INTENT(in) :: bsc_k2
1921 REAL(rprec),
DIMENSION(:,:),
POINTER :: positions => null(),
1922 & tangents => null(), avecs => null()
1923 REAl(rprec),
DIMENSION(:),
POINTER :: sens => null()
1924 INTEGER(iprec) :: i, npoints
1931 CALL bsc_flux_pos(coil_b,len_integerate,positions,tangents,
1932 & avecs,sens,npoints)
1935 SELECT CASE (coil_b % c_type)
1938 CALL bsc_a(coil_a,positions(1:3,i),avecs(1:3,i))
1940 CASE (
'fil_rogo',
'fil_rogo_s')
1942 CALL bsc_b(coil_a,positions(1:3,i),avecs(1:3,i))
1947 CALL bsc_flux_sum(coil_b,positions,tangents,avecs,sens,npoints,
1951 IF (
PRESENT(bsc_k2))
THEN
1952 flux = flux * bsc_k2 * bsc_k2inv_def
1956 DEALLOCATE(avecs,positions,tangents,sens)
1959 END SUBROUTINE bsc_fluxba_coilcoll
1965 SUBROUTINE bsc_flux_pos(coil_b,len_integrate,positions, &
1966 & tangents,avecs,sens,npoints)
1982 TYPE (bsc_coil),
INTENT(in) :: coil_b
1983 REAL(rprec),
INTENT(in) :: len_integrate
1984 REAL(rprec),
DIMENSION(:,:),
POINTER :: positions, tangents,
1986 REAL(rprec),
DIMENSION(:),
POINTER :: sens
1987 INTEGER(iprec),
INTENT(out) :: npoints
1990 INTEGER(iprec) :: i, j, iseg, npoints_this_segment, n_denom
1991 INTEGER(iprec) :: nseg
1992 REAL(rprec),
DIMENSION(3) :: xhatlocal, yhatlocal, zhatlocal,
1994 REAL(rprec) :: dphi, phi, cphi, sphi, frac_denom
1995 INTEGER(iprec),
PARAMETER :: npcirc = 60
2000 SELECT CASE (coil_b % c_type)
2001 CASE (
'fil_loop',
'floop',
'fil_rogo',
'fil_rogo_s')
2002 IF (len_integrate .le. zero)
THEN
2003 npoints =
SIZE(coil_b % lnod)
2006 DO iseg = 1,
SIZE(coil_b % lnod)
2007 npoints_this_segment = coil_b % lnod(iseg) /
2009 npoints = npoints + npoints_this_segment
2012 CASE (
'fil_circ',
'fcirc')
2013 IF (len_integrate .le. zero)
THEN
2016 npoints = twopi * coil_b % rcirc / len_integrate + 1
2017 npoints = max(npcirc,npoints)
2022 IF (
ASSOCIATED(positions))
DEALLOCATE(positions)
2023 IF (
ASSOCIATED(tangents))
DEALLOCATE(tangents)
2024 IF (
ASSOCIATED(avecs))
DEALLOCATE(avecs)
2025 ALLOCATE(positions(3,npoints),tangents(3,npoints),
2026 & avecs(3,npoints),sens(npoints))
2036 SELECT CASE (coil_b % c_type)
2037 CASE (
'fil_loop',
'floop',
'fil_rogo',
'fil_rogo_s')
2039 nseg =
SIZE(coil_b % lnod)
2041 IF (len_integrate .le. zero)
THEN
2042 npoints_this_segment = 1
2044 npoints_this_segment = coil_b % lnod(iseg) /
2047 IF (npoints_this_segment .eq. 1)
THEN
2048 positions(1:3,i) = coil_b % xnod(1:3,iseg) +
2049 & 0.5 * coil_b % dxnod(1:3,iseg)
2050 tangents(1:3,i) = coil_b % dxnod(1:3,iseg)
2051 sens(i) = coil_b % sens(iseg)
2053 ELSEIF (npoints_this_segment .eq. 2)
THEN
2054 positions(1:3,i) = coil_b % xnod(1:3,iseg) +
2055 & 0.25 * coil_b % dxnod(1:3,iseg)
2056 tangents(1:3,i) = 0.5 * coil_b % dxnod(1:3,iseg)
2057 sens(i) = coil_b % sens(iseg)
2059 positions(1:3,i) = coil_b % xnod(1:3,iseg) +
2060 & 0.75 * coil_b % dxnod(1:3,iseg)
2061 tangents(1:3,i) = 0.5 * coil_b % dxnod(1:3,iseg)
2062 sens(i) = coil_b % sens(iseg)
2065 n_denom = npoints_this_segment - 1
2066 frac_denom = one / n_denom
2067 positions(1:3,i) = coil_b % xnod(1:3,iseg) +
2068 & 0.25 * frac_denom * coil_b % dxnod(1:3,iseg)
2069 tangents(1:3,i) = 0.5 * frac_denom *
2070 & coil_b % dxnod(1:3,iseg)
2071 sens(i) = coil_b % sens(iseg)
2073 DO j = 2,npoints_this_segment - 1
2074 positions(1:3,i) = coil_b % xnod(1:3,iseg) +
2075 & frac_denom * (j-1) * coil_b % dxnod(1:3,iseg)
2076 tangents(1:3,i) = frac_denom *
2077 & coil_b % dxnod(1:3,iseg)
2078 sens(i) = coil_b % sens(iseg)
2081 positions(1:3,i) = coil_b % xnod(1:3,iseg) +
2082 & (1 - 0.25 * frac_denom) * coil_b % dxnod(1:3,iseg)
2083 tangents(1:3,i) = 0.5 * frac_denom *
2084 & coil_b % dxnod(1:3,iseg)
2085 sens(i) = coil_b % sens(iseg)
2089 IF (i - 1 .ne. npoints)
THEN
2090 stop
'ERROR 1 in bsc_flux_pos'
2092 CASE (
'fil_circ',
'fcirc')
2094 CALL bsc_triplet(coil_b % enhat,xhatlocal,yhatlocal,zhatlocal)
2095 dphi = twopi / npoints
2100 rhohat = cphi * xhatlocal + sphi * yhatlocal
2101 phihat = - sphi * xhatlocal + cphi * yhatlocal
2102 positions(1:3,i) = coil_b % xcent(1:3) + coil_b % rcirc *
2104 tangents(1:3,i) = coil_b % rcirc * dphi * phihat
2109 END SUBROUTINE bsc_flux_pos
2114 SUBROUTINE bsc_flux_sum(coil_b,positions,tangents,avecs,sens, &
2123 TYPE (bsc_coil),
INTENT(in) :: coil_b
2124 REAL(rprec),
DIMENSION(:,:),
POINTER :: positions, tangents,
2126 REAL(rprec),
DIMENSION(:),
POINTER :: sens
2127 INTEGER(iprec),
INTENT(in) :: npoints
2128 REAL(rprec),
INTENT(out) :: flux
2138 flux = flux + sens(i)*dot_product(avecs(1:3,i),tangents(1:3,i))
2142 IF (coil_b % c_type .eq.
'fil_rogo')
THEN
2143 flux = flux * coil_b % ave_n_area
2146 END SUBROUTINE bsc_flux_sum
2155 SUBROUTINE bsc_cei(xarg, rf, rd)
2170 REAL(rprec),
INTENT(IN) :: xarg
2171 REAL(rprec),
INTENT(out) :: rf, rd
2174 REAL(rprec) :: xt, yt, zt, sum, x
2175 REAL(rprec) :: alamb, fac, sqrtx, sqrty, sqrtz, avef, recavef,
2178 INTEGER(iprec) :: iter
2179 INTEGER(iprec),
PARAMETER :: niter = 5
2182 REAL(rprec),
PARAMETER :: third = one / 3._rprec
2183 REAL(rprec),
PARAMETER :: c1f = one / 24._rprec
2184 REAL(rprec),
PARAMETER :: c2f = 0.1_rprec
2185 REAL(rprec),
PARAMETER :: c3f = 3._rprec / 44._rprec
2186 REAL(rprec),
PARAMETER :: c4f = one / 14._rprec
2187 REAL(rprec),
PARAMETER :: sixth = one / 6._rprec
2188 REAL(rprec),
PARAMETER :: twelfth = one / 12._rprec
2189 REAL(rprec),
PARAMETER :: c1d = 3._rprec / 14._rprec
2190 REAL(rprec),
PARAMETER :: c2d = one / 6._rprec
2191 REAL(rprec),
PARAMETER :: c3d = 9._rprec / 22._rprec
2192 REAL(rprec),
PARAMETER :: c4d = 3._rprec / 26._rprec
2193 REAL(rprec),
PARAMETER :: c5d = .25_rprec * c3d
2194 REAL(rprec),
PARAMETER :: c6d = 1.5_rprec * c4d
2198 x = min(max(xarg,1.e-12_rprec),one)
2205 yt = fac * (x + alamb)
2206 zt = fac * (one + alamb)
2207 sum = one / (one + alamb)
2214 alamb = sqrtx * (sqrty + sqrtz) + sqrty * sqrtz
2215 sum = sum + fac / (sqrtz * (zt + alamb))
2216 xt = .25_rprec * (xt + alamb)
2217 yt = .25_rprec * (yt + alamb)
2218 zt = .25_rprec * (zt + alamb)
2219 fac = .25_rprec * fac
2225 aved = .2_rprec * (xt + yt + 3._rprec * zt)
2226 recaved = one / aved
2227 delxd = one - xt * recaved
2228 delyd = one - yt * recaved
2229 delzd = one - zt * recaved
2233 edd = ead - 6._rprec * ebd
2234 eed = edd + ecd + ecd
2235 rd = 3._rprec * sum + fac * (one + edd * (- c1d + c5d * edd -
2236 & c6d * delzd * eed) + delzd * (c2d * eed + delzd *
2237 & (- c3d * ecd + delzd * c4d * ead))) * recaved *
2240 avef = third * (xt + yt + zt)
2241 recavef = one / avef
2242 delxf = one - xt * recavef
2243 delyf = one - yt * recavef
2244 delzf = one - zt * recavef
2245 e2f = delxf * delyf - delzf ** 2
2246 e3f = delxf * delyf * delzf
2247 rf = (one + (c1f * e2f - c2f - c3f * e3f) *
2248 & e2f + c4f * e3f) * sqrt(recavef)
2250 END SUBROUTINE bsc_cei
2255 SUBROUTINE bsc_triplet(zlocal,xhatlocal,yhatlocal,zhatlocal)
2263 REAL(rprec),
DIMENSION(3),
INTENT(in) :: zlocal
2264 REAL(rprec),
DIMENSION(3),
INTENT(out) :: xhatlocal, yhatlocal,
2268 REAL(rprec),
DIMENSION(3) :: zloc, xloc
2269 REAL(rprec) :: fac, lsq
2274 lsq = dot_product(zloc,zloc)
2275 IF (lsq .le. 0.)
THEN
2279 fac = 1._rprec / sqrt(lsq)
2280 zhatlocal = fac * zloc
2283 IF (abs(zhatlocal(1)) .le. 0.8_rprec)
THEN
2284 xloc = (/ 1._rprec, 0._rprec, 0._rprec /)
2286 xloc = (/ 0._rprec, 1._rprec, 0._rprec /)
2290 fac = dot_product(xloc,zhatlocal)
2291 xloc = xloc - fac * zhatlocal
2294 lsq = dot_product(xloc,xloc)
2295 fac = 1._rprec / sqrt(lsq)
2296 xhatlocal = fac * xloc
2299 yhatlocal(1) = zhatlocal(2) * xhatlocal(3) -
2300 & zhatlocal(3) * xhatlocal(2)
2301 yhatlocal(2) = zhatlocal(3) * xhatlocal(1) -
2302 & zhatlocal(1) * xhatlocal(3)
2303 yhatlocal(3) = zhatlocal(1) * xhatlocal(2) -
2304 & zhatlocal(2) * xhatlocal(1)
2306 END SUBROUTINE bsc_triplet
2318 SUBROUTINE bsc_mean_r(this,mean_r)
2323 TYPE (bsc_coil),
INTENT(in) :: this
2324 REAL(rprec),
DIMENSION(3),
INTENT(out) :: mean_r
2327 INTEGER :: nwire, nm1, iwire, i
2328 REAL(rprec) :: coil_length
2334 SELECT CASE (this % c_type)
2335 CASE (
'fil_circ',
'fcirc')
2336 mean_r(1:3) = this % xcent(1:3)
2337 CASE (
'fil_loop',
'floop',
'fil_rogo',
'fil_rogo_s')
2338 nwire =
SIZE(this % xnod,2)
2339 nm1 = max(1, nwire-1)
2340 coil_length = sum(this % lnod(1:nm1))
2342 mean_r(i) = dot_product(this % lnod(1:nm1),
2343 & this % xnod(i,1:nm1) + 0.5 * this % dxnod(i,1:nm1)) /
2347 WRITE(*,*)
'FATAL: bsc_mean_r: &
2348 & c_type unrecognized:', this % c_type
2352 END SUBROUTINE bsc_mean_r
2364 SUBROUTINE bsc_mean_xnod(this,mean_xnod)
2369 TYPE (bsc_coil),
INTENT(in) :: this
2370 REAL(rprec),
DIMENSION(3),
INTENT(out) :: mean_xnod
2373 INTEGER :: nwire, nm1, iwire, i
2379 SELECT CASE (this % c_type)
2380 CASE (
'fil_circ',
'fcirc')
2381 mean_xnod(1:3) = this % xcent(1:3)
2382 CASE (
'fil_loop',
'floop',
'fil_rogo',
'fil_rogo_s')
2383 nwire =
SIZE(this % xnod,2)
2384 nm1 = max(1, nwire-1)
2386 mean_xnod(i) = sum(this % xnod(i,1:nm1)) / nm1
2389 WRITE(*,*)
'FATAL: bsc_mean_r: &
2390 & c_type unrecognized:', this % c_type
2394 END SUBROUTINE bsc_mean_xnod
2404 SUBROUTINE bsc_spill_coil(this,identifier)
2408 TYPE (bsc_coil),
INTENT (in) :: this
2409 CHARACTER (len=*) :: identifier
2410 INTEGER(iprec) :: i, n
2414 WRITE(*,*)
'sub spill_coil CALL with -- ',identifier,
' --'
2417 WRITE(*,*)
' c_type = ', this % c_type
2418 WRITE(*,*)
' s_name = ', this % s_name
2419 WRITE(*,*)
' l_name = ', this % l_name
2420 WRITE(*,*)
' eps_sq = ', this % eps_sq
2421 WRITE(*,*)
' current = ', this % current
2422 WRITE(*,*)
' raux = ', this % raux
2423 IF (this % c_type .eq.
'fil_rogo')
THEN
2424 WRITE(*,*)
' ave_n_area = ', this % ave_n_area
2427 SELECT CASE (this % c_type)
2428 CASE (
'fil_loop',
'floop',
'fil_rogo')
2429 IF (
ASSOCIATED(this % xnod))
THEN
2430 n =
SIZE(this % xnod,2)
2431 WRITE(*,*)
' i xnod(1,i), xnod(2,i), xnod(3,i)'
2433 WRITE(*,*) i, this % xnod(1:3,i)
2445 WRITE(*,*)
' xnod is NOT ASSOCIATED'
2447 CASE (
'fil_circ',
'fcirc')
2448 WRITE(*,*)
' rcirc = ', this % rcirc
2449 WRITE(*,*)
' xcent = ', this % xcent(1:3)
2450 WRITE(*,*)
' enhat = ', this % enhat(1:3)
2453 END SUBROUTINE bsc_spill_coil
2458 SUBROUTINE bsc_spill_coilcoll(this,identifier)
2462 TYPE (bsc_coilcoll),
INTENT (in) :: this
2463 CHARACTER (len=*) :: identifier
2464 INTEGER(iprec) :: i, n, ncoild
2469 WRITE(*,*)
'sub spill_coilcoll CALL with -- ',identifier,
' --'
2470 WRITE(*,*)
' s_name = ', this % s_name
2471 WRITE(*,*)
' l_name = ', this % l_name
2472 WRITE(*,*)
' ncoil = ', this % ncoil
2473 ncoild =
SIZE(this % coils)
2474 WRITE(*,*)
' ncoild = ', ncoild
2479 WRITE(*,*) i,
' this coil, s_name = ',this % coils(i) % s_name
2480 IF (
ASSOCIATED(this % coils(i) % xnod))
THEN
2481 n =
SIZE(this % coils(i) % xnod,2)
2482 WRITE(*,*)
' xnod associated. SIZE(2) = ',n
2484 WRITE(*,*)
' xnod is NOT ASSOCIATED'
2494 WRITE(*,*)
'End of spill_coilcoll'
2496 END SUBROUTINE bsc_spill_coilcoll
2502 SUBROUTINE log_eps (x1)
2507 REAL(RPREC),
INTENT(INOUT) :: x1(:)
2511 REAL(rprec),
PARAMETER :: c3 = one/3,
2512 & c5 = one/5, c7 = one/7, c9 = one/9, c11 = one/11
2513 REAL(rprec),
PARAMETER :: threshold = 0.1_rprec
2538 IF (x1(i) <= threshold)
THEN
2545 x1(i) = 2 * x1(i) * (one + x2 * (c3 + x2 * (c5 + x2 * (c7
2546 & + x2 * (c9 + x2 * c11)))))
2553 x1(i) = log((1 + x1(i))/max(1 - x1(i) ,bsc_mach_eps))
2560 END SUBROUTINE log_eps