| 1 |
! |
| 2 |
!--unit test for field line plotting routine |
| 3 |
! |
| 4 |
program test_fieldlines |
| 5 |
use fieldlines, only:streamlines |
| 6 |
use render, only:render_pix |
| 7 |
implicit none |
| 8 |
integer, parameter :: ipixx = 1000, ipixy = 1000, nc = 100 |
| 9 |
integer :: npixx, npixy,i,j |
| 10 |
real, parameter :: errtol = 1.e-7 |
| 11 |
real, dimension(ipixx,ipixy) :: datpix,vecpixx,vecpixy,datpix1,datpix2,vecpixx1,vecpixy1 |
| 12 |
real :: xmin,xmax,ymin,ymax,zmin,zmax,dxpix |
| 13 |
real :: xi,yi,datmin,datmax,err,dcont |
| 14 |
real, parameter :: pi=3.1415926536 |
| 15 |
real, dimension(6) :: trans |
| 16 |
real, dimension(nc) :: levels |
| 17 |
|
| 18 |
xmin = -0.5 |
| 19 |
xmax = 0.5 |
| 20 |
ymin = -0.5 |
| 21 |
ymax = 0.5 |
| 22 |
|
| 23 |
call pgopen('/xw') |
| 24 |
|
| 25 |
print "(70('-'))" |
| 26 |
print*,'ORSZAG-TANG TEST' |
| 27 |
|
| 28 |
npixx = 400 |
| 29 |
npixy = 400 |
| 30 |
dxpix = (xmax-xmin)/real(npixx) |
| 31 |
do j = 1,npixy |
| 32 |
yi = ymin + (j-0.5)*dxpix |
| 33 |
if (j.le.10) print*,'y = ',yi |
| 34 |
do i = 1,npixx |
| 35 |
xi = xmin + (i-0.5)*dxpix |
| 36 |
vecpixx(i,j) = func_vecx(xi,yi) |
| 37 |
vecpixy(i,j) = func_vecy(xi,yi) |
| 38 |
datpix1(i,j) = func_stream(xi,yi) |
| 39 |
enddo |
| 40 |
enddo |
| 41 |
|
| 42 |
call streamlines(vecpixx(1:npixx,1:npixy),vecpixy(1:npixx,1:npixy), & |
| 43 |
datpix(1:npixx,1:npixy),npixx,npixy,dxpix) |
| 44 |
|
| 45 |
do j = 1,npixy,2 |
| 46 |
yi = ymin + (j-0.5)*2.*dxpix |
| 47 |
do i = 1,npixx,2 |
| 48 |
xi = xmin + (i-0.5)*2.*dxpix |
| 49 |
vecpixx1((i+1)/2,(j+1)/2) = func_vecx(xi,yi) !vecpixx(i,j) |
| 50 |
vecpixy1((i+1)/2,(j+1)/2) = func_vecy(xi,yi) !vecpixy(i,j) |
| 51 |
enddo |
| 52 |
enddo |
| 53 |
npixx = npixx/2 |
| 54 |
npixy = npixy/2 |
| 55 |
dxpix = (xmax-xmin)/real(npixx) |
| 56 |
call streamlines(vecpixx1(1:npixx,1:npixy),vecpixy1(1:npixx,1:npixy), & |
| 57 |
datpix2(1:npixx,1:npixy),npixx,npixy,dxpix) |
| 58 |
npixx = npixx*2 |
| 59 |
npixy = npixy*2 |
| 60 |
dxpix = (xmax-xmin)/real(npixx) |
| 61 |
!do j=1,npixy,2 |
| 62 |
! do i=1,npixx,2 |
| 63 |
! datpix(i,j) = (4.*datpix(i,j) - datpix2((i+1)/2,(j+1)/2))/3. |
| 64 |
! enddo |
| 65 |
!enddo |
| 66 |
|
| 67 |
call pgenv(xmin,xmax,ymin,ymax,1,0) |
| 68 |
trans = 0. |
| 69 |
trans(1) = xmin - 0.5*dxpix |
| 70 |
trans(2) = dxpix |
| 71 |
trans(4) = ymin - 0.5*dxpix |
| 72 |
trans(6) = dxpix |
| 73 |
datmax = maxval(datpix(1:npixx,1:npixy)) |
| 74 |
datmin = minval(datpix(1:npixx,1:npixy)) |
| 75 |
print*,'min,max datpix = ',datmin,datmax |
| 76 |
datpix(1:npixx,1:npixy) = datpix(1:npixx,1:npixy) - 0.5*(datmax + datmin) |
| 77 |
datmax = maxval(datpix(1:npixx,1:npixy)) |
| 78 |
datmin = minval(datpix(1:npixx,1:npixy)) |
| 79 |
print*,'min,max datpix = ',datmin,datmax |
| 80 |
|
| 81 |
print*,'plotting integrated array...' |
| 82 |
! call render_pix(datpix(1:npixx,1:npixy),datmin,datmax,'crap',npixx,npixy, & |
| 83 |
! xmin,ymin,dxpix,0,.true.,.false.,30,.false.) |
| 84 |
call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,datmin,datmax,trans) |
| 85 |
! |
| 86 |
!--set contour levels |
| 87 |
! |
| 88 |
dcont = (datmax-datmin)/real(nc+1) ! even contour levels |
| 89 |
do i=1,nc |
| 90 |
levels(i) = datmin + real(i)*dcont |
| 91 |
enddo |
| 92 |
! |
| 93 |
!--plot contours (use pgcont if pgcons causes trouble) |
| 94 |
! |
| 95 |
call pgcons(datpix(1:npixx,1:npixy),npixx,npixy,1,npixx,1,npixy,levels(1:nc),nc,trans) |
| 96 |
|
| 97 |
! call pgenv(xmin,xmax,ymin,ymax,1,0) |
| 98 |
|
| 99 |
datmax = maxval(datpix1(1:npixx,1:npixy)) |
| 100 |
datmin = minval(datpix1(1:npixx,1:npixy)) |
| 101 |
print*,'min,max datpix1 = ',datmin,datmax |
| 102 |
|
| 103 |
!print*,' plotting exact solution ...' |
| 104 |
! call pgimag(datpix1,ipixx,ipixy,1,npixx,1,npixy,datmin,datmax,trans) |
| 105 |
!call pgcons(datpix1(1:npixx,1:npixy),npixx,npixy,1,npixx,1,npixy,levels(1:nc),nc,trans) |
| 106 |
|
| 107 |
call geterr(datpix(1:npixx,1:npixy),npixx,npixy,datpix1(1:npixx,1:npixy),err) |
| 108 |
print*,'average error in stream line calculation = ',err |
| 109 |
if (npixx.eq.400 .and. npixy.eq.400) then |
| 110 |
if (err < 0.00019) then |
| 111 |
print*,'PASSED: error within limits' |
| 112 |
else |
| 113 |
print*,'FAILED: error too large!' |
| 114 |
endif |
| 115 |
else |
| 116 |
print*,'setup different to usual one' |
| 117 |
endif |
| 118 |
|
| 119 |
!--------------------- |
| 120 |
! dipole field test |
| 121 |
!---------------------- |
| 122 |
|
| 123 |
print "(70('-'))" |
| 124 |
print*,'DIPOLE TEST' |
| 125 |
npixx = 400 |
| 126 |
npixy = 400 |
| 127 |
dxpix = (xmax-xmin)/real(npixx) |
| 128 |
do j = 1,npixy |
| 129 |
yi = ymin + (j-0.5)*dxpix |
| 130 |
do i = 1,npixx |
| 131 |
xi = xmin + (i-0.5)*dxpix |
| 132 |
vecpixx(i,j) = func_vecx_dipole(xi,yi) |
| 133 |
vecpixy(i,j) = func_vecy_dipole(xi,yi) |
| 134 |
datpix1(i,j) = func_stream_dipole(xi,yi) |
| 135 |
enddo |
| 136 |
enddo |
| 137 |
|
| 138 |
call streamlines(vecpixx(1:npixx,1:npixy),vecpixy(1:npixx,1:npixy), & |
| 139 |
datpix(1:npixx,1:npixy),npixx,npixy,dxpix) |
| 140 |
|
| 141 |
call pgenv(xmin,xmax,ymin,ymax,1,0) |
| 142 |
trans = 0. |
| 143 |
trans(1) = xmin - 0.5*dxpix |
| 144 |
trans(2) = dxpix |
| 145 |
trans(4) = ymin - 0.5*dxpix |
| 146 |
trans(6) = dxpix |
| 147 |
datmax = maxval(datpix(1:npixx,1:npixy)) |
| 148 |
datmin = minval(datpix(1:npixx,1:npixy)) |
| 149 |
print*,'min,max datpix = ',datmin,datmax |
| 150 |
datpix(1:npixx,1:npixy) = datpix(1:npixx,1:npixy) - 0.5*(datmax + datmin) |
| 151 |
datmax = maxval(datpix(1:npixx,1:npixy)) |
| 152 |
datmin = minval(datpix(1:npixx,1:npixy)) |
| 153 |
print*,'min,max datpix = ',datmin,datmax |
| 154 |
|
| 155 |
! call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,datmin,datmax,trans) |
| 156 |
! |
| 157 |
!--set contour levels |
| 158 |
! |
| 159 |
dcont = (datmax-datmin)/real(nc+1) ! even contour levels |
| 160 |
do i=1,nc |
| 161 |
levels(i) = datmin + real(i)*dcont |
| 162 |
enddo |
| 163 |
! |
| 164 |
!--plot contours (use pgcont if pgcons causes trouble) |
| 165 |
! |
| 166 |
call pgcons(datpix(1:npixx,1:npixy),npixx,npixy,1,npixx,1,npixy,levels(1:nc),nc,trans) |
| 167 |
|
| 168 |
! call pgenv(xmin,xmax,ymin,ymax,1,0) |
| 169 |
|
| 170 |
datmax = maxval(datpix1(1:npixx,1:npixy)) |
| 171 |
datmin = minval(datpix1(1:npixx,1:npixy)) |
| 172 |
print*,'min,max datpix1 = ',datmin,datmax |
| 173 |
|
| 174 |
! call pgimag(datpix1,ipixx,ipixy,1,npixx,1,npixy,datmin,datmax,trans) |
| 175 |
! call pgcons(datpix1(1:npixx,1:npixy),npixx,npixy,1,npixx,1,npixy,levels(1:nc),nc,trans) |
| 176 |
|
| 177 |
call geterr(datpix(1:npixx,1:npixy),npixx,npixy,datpix1(1:npixx,1:npixy),err) |
| 178 |
print*,'average error in stream line calculation = ',err |
| 179 |
if (npixx.eq.400 .and. npixy.eq.400) then |
| 180 |
if (err < 0.00019) then |
| 181 |
print*,'PASSED: error within limits' |
| 182 |
else |
| 183 |
print*,'FAILED: error too large!' |
| 184 |
endif |
| 185 |
else |
| 186 |
print*,'setup different to usual one' |
| 187 |
endif |
| 188 |
|
| 189 |
|
| 190 |
call pgend |
| 191 |
|
| 192 |
print "(70('-'))" |
| 193 |
|
| 194 |
contains |
| 195 |
|
| 196 |
real function func_vecx(xi,yi) |
| 197 |
implicit none |
| 198 |
real :: xi,yi |
| 199 |
|
| 200 |
func_vecx = -sin(2.*pi*yi) |
| 201 |
|
| 202 |
end function func_vecx |
| 203 |
|
| 204 |
real function func_vecy(xi,yi) |
| 205 |
implicit none |
| 206 |
real :: xi,yi |
| 207 |
|
| 208 |
func_vecy = sin(4.*pi*xi) |
| 209 |
|
| 210 |
end function func_vecy |
| 211 |
|
| 212 |
real function func_stream(xi,yi) |
| 213 |
implicit none |
| 214 |
real :: xi,yi |
| 215 |
|
| 216 |
func_stream = 0.5/pi*(cos(2.*pi*yi) + 0.5*cos(4.*pi*xi)) |
| 217 |
|
| 218 |
end function func_stream |
| 219 |
|
| 220 |
!---------------- |
| 221 |
! dipole |
| 222 |
!---------------- |
| 223 |
real function func_vecx_dipole(xi,yi) |
| 224 |
implicit none |
| 225 |
real :: xi,yi |
| 226 |
real, parameter :: Bdipole = 1.0, Rdipole = 0.3, eps=0.3 |
| 227 |
|
| 228 |
func_vecx_dipole = 3.*Bdipole*Rdipole**3*(Rdipole + xi)*yi/ & |
| 229 |
sqrt((Rdipole + xi)**2 + yi**2 + (eps*Rdipole)**2)**5 |
| 230 |
|
| 231 |
end function func_vecx_dipole |
| 232 |
|
| 233 |
real function func_vecy_dipole(xi,yi) |
| 234 |
implicit none |
| 235 |
real :: xi,yi |
| 236 |
real, parameter :: Bdipole = 1.0, Rdipole = 0.3, eps=0.3 |
| 237 |
|
| 238 |
func_vecy_dipole = Bdipole*Rdipole**3/ & |
| 239 |
sqrt((Rdipole + xi)**2 + yi**2 + (eps*Rdipole)**2)**3 |
| 240 |
|
| 241 |
end function func_vecy_dipole |
| 242 |
|
| 243 |
real function func_stream_dipole(xi,yi) |
| 244 |
implicit none |
| 245 |
real :: xi,yi |
| 246 |
real, parameter :: Bdipole = 1.0, Rdipole = 0.3, eps=0.3 |
| 247 |
|
| 248 |
func_stream_dipole = -Bdipole*Rdipole**3*(Rdipole + xi)/ & |
| 249 |
sqrt((Rdipole + xi)**2 + yi**2 + (eps*Rdipole)**2)**3 |
| 250 |
|
| 251 |
end function func_stream_dipole |
| 252 |
|
| 253 |
|
| 254 |
subroutine geterr(datpix,npixx,npixy,datexact,err) |
| 255 |
implicit none |
| 256 |
integer, intent(in) :: npixx,npixy |
| 257 |
real, dimension(:,:), intent(in) :: datpix |
| 258 |
real, dimension(:,:), intent(in) :: datexact |
| 259 |
real, intent(out) :: err |
| 260 |
integer :: icalc,j,i |
| 261 |
real :: errij |
| 262 |
|
| 263 |
err = 0. |
| 264 |
icalc = 0 |
| 265 |
do j=1,npixy |
| 266 |
do i=1,npixx |
| 267 |
icalc = icalc + 1 |
| 268 |
errij = abs(datpix(i,j)-datexact(i,j)) |
| 269 |
err = err + errij |
| 270 |
enddo |
| 271 |
enddo |
| 272 |
if (icalc.le.0) then |
| 273 |
print*,'cannot calculate error => npix too small' |
| 274 |
err = -1.0 |
| 275 |
else |
| 276 |
err = err/(icalc*maxval(datexact)) |
| 277 |
endif |
| 278 |
|
| 279 |
end subroutine geterr |
| 280 |
|
| 281 |
end program test_fieldlines |