VMEC 8.52
3D Equilibrium solver with nested flux surfaces.
Loading...
Searching...
No Matches
spline_akima.f
Go to the documentation of this file.
1
2 SUBROUTINE spline_akima(x, y, xx, yy, npts, iflag)
3 USE stel_kinds
4 implicit none
5!-----------------------------------------------
6! D u m m y A r g u m e n t s
7!-----------------------------------------------
8 real(rprec), intent(in) :: x
9 real(rprec), intent(out) :: y
10 real(rprec), dimension(npts), intent(in) :: xx
11 real(rprec), dimension(npts), intent(in) :: yy
12 integer, intent(in) :: npts
13 integer, intent(inout) :: iflag
14!-----------------------------------------------
15! L o c a l V a r i a b l e s
16!-----------------------------------------------
17 real(rprec), dimension(-1:size(xx)+2) :: a
18 real(rprec), dimension(-1:size(xx)+2) :: b
19 real(rprec), dimension(-1:size(xx)+2) :: c
20 real(rprec), dimension(-1:size(xx)+2) :: d
21
22 real(rprec), dimension(-1:size(xx)+2) :: xloc
23 real(rprec), dimension(-1:size(xx)+2) :: yloc
24
25 real(rprec), dimension(-1:size(xx)+2) :: m
26 real(rprec), dimension(-1:size(xx)+2) :: t
27 real(rprec), dimension(-1:size(xx)+2) :: dm
28 real(rprec), dimension(-1:size(xx)+2) :: p
29 real(rprec), dimension(-1:size(xx)+2) :: q
30
31 integer :: i
32 integer :: ix
33 integer :: iv
34 integer :: ivm1
35
36 real(rprec) :: cl
37 real(rprec) :: bl
38 real(rprec) :: cr
39 real(rprec) :: br
40
41 real(rprec) :: dx
42
43!-----------------------------------------------
44
45 !WRITE(6,*) x, y
46 !WRITE(6,*)npts
47 !WRITE(6,*) xx
48 !WRITE(6,*) yy
49 !stop 'Test!'
50
51 iflag = 0 !initialization
52
53 ix = size(xx)
54! print *, size(xx)
55! print *, size(yy)
56 if(npts > ix)
57 > stop'spline_akima: more active points requested than available'
58 if(ix /= size(yy)) stop 'size mismatch of xx and yy!'
59
60 iv=npts
61
62 ivm1 = iv-1
63! initialize local variables
64 a = 0._dp ; b = 0._dp ; c = 0._dp ; d = 0._dp
65 xloc = 0._dp ; yloc = 0._dp
66 m = 0._dp ; t = 0._dp ; dm = 0._dp
67 p = 0._dp ; q = 0._dp
68
69 xloc(1:iv)=xx
70 xloc(-1)= 2*xloc(1)-xloc(3)
71 xloc( 0)= xloc(1)+xloc(2)-xloc(3)
72 xloc(iv+2)= 2*xloc(iv)-xloc(iv-2)
73 xloc(iv+1)= xloc(iv)+xloc(iv-1)-xloc(iv-2)
74
75 yloc(1:iv)=yy
76
77! calculate linear derivatives as far as existent
78 m(-1:iv+1) = (yloc(0:iv+2)-yloc(-1:iv+1))/
79 > (xloc(0:iv+2)-xloc(-1:iv+1))
80! values for i=0, -1 and iv, iv+1 by quadratic extrapolation:
81 cl = (m(2)-m(1))/(xloc(3)-xloc(1))
82 bl = m(1) - cl*(xloc(2)-xloc(1))
83 cr = (m(iv-2)-m(iv-1))/(xloc(iv)-xloc(iv-2))
84 br = m(iv-2) - cr*(xloc(iv-1)-xloc(iv-2))
85 yloc( 0)=yloc(1)+bl*(xloc( 0)-xloc(1))+
86 > cl*(xloc( 0)-xloc(1))**2
87 yloc(-1)=yloc(1)+bl*(xloc(-1)-xloc(1))+
88 > cl*(xloc(-1)-xloc(1))**2
89 yloc(iv+1)=yloc(iv)+br*(xloc(iv+1)-xloc(iv))+
90 > cl*(xloc(iv+1)-xloc(iv))**2
91 yloc(iv+2)=yloc(iv)+br*(xloc(iv+2)-xloc(iv))+
92 > cl*(xloc(iv+2)-xloc(iv))**2
93! rest of linear derivatives
94 m(-1) = (yloc(0)-yloc(-1))/(xloc(0)-xloc(-1))
95 m( 0) = (yloc(1)-yloc( 0))/(xloc(1)-xloc( 0))
96 m(iv ) = (yloc(iv+1)-yloc(iv ))/(xloc(iv+1)-xloc(iv ))
97 m(iv+1) = (yloc(iv+2)-yloc(iv+1))/(xloc(iv+2)-xloc(iv+1))
98! calculate weights for derivatives
99 dm(-1:iv)= abs(m(0:iv+1) - m(-1:iv))
100 where (dm /= 0._dp) !exclude division by zero
101 p(1:iv) = dm(1:iv)/(dm(1:iv)+dm(-1:iv-2))
102 end where
103 where (dm /= 0._dp) !exclude division by zero
104 q(1:iv) = dm(-1:iv-2)/(dm(1:iv)+dm(-1:iv-2))
105 end where
106
107 t(1:iv) = p(1:iv)*m(0:iv-1)+q(1:iv)*m(1:iv)
108 where ( p(1:iv)+q(1:iv) < tiny(1._dp)) ! in case of two zeros give equal weight
109 t(1:iv) = 0.5_dp*m(0:iv-1)+0.5_dp*m(1:iv)
110 end where
111! fix coefficients
112 a = yloc
113 b = t
114 c(1:iv-1) = (3*m(1:iv-1)-t(2:iv)-2*t(1:iv-1))/
115 > (xloc(2:iv)-xloc(1:iv-1))
116 d(1:iv-1) = (t(2:iv)+t(1:iv-1)-2*m(1:iv-1))/
117 > (xloc(2:iv)-xloc(1:iv-1))**2
118
119! calculation
120 if(x<xloc(1) .or. x>xloc(iv))then
121 y=0.0
122 iflag=-1
123 return
124 endif
125
126 if(x == xloc(iv)) then
127 y = yy(iv)
128 iflag = 0
129 return
130 endif
131
132 do i=1,iv-1
133 if(x >= xloc(i) .and. x < xloc(i+1))then
134 dx=x-xloc(i)
135 y=a(i)+dx*(b(i)+dx*(c(i)+d(i)*dx))
136 iflag = 0
137 return
138 endif
139 enddo
140
141 END SUBROUTINE spline_akima
142
subroutine spline_akima(x, y, xx, yy, npts, iflag)