| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2018 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!----------------------------------------------------------------- |
| 24 |
! Standalone module containing subroutines to transform between |
| 25 |
! different co-ordinate systems, for co-ordinates and vectors |
| 26 |
! (e.g. from cartesian to cylindrical polar and vice versa) |
| 27 |
! |
| 28 |
! itype must be one of the following: |
| 29 |
! itype = 1 : cartesian (default) |
| 30 |
! itype = 2 : cylindrical |
| 31 |
! itype = 3 : spherical |
| 32 |
! itype = 4 : toroidal |
| 33 |
! itype = 5 : rotated cartesian |
| 34 |
! |
| 35 |
! Currently handles: |
| 36 |
! |
| 37 |
! cartesian -> cylindrical, spherical polar |
| 38 |
! cylindrical <-> cartesian |
| 39 |
! spherical polar <-> cartesian |
| 40 |
! toroidal r,theta,phi <-> cartesian |
| 41 |
! rotated cartesian <-> cartesian |
| 42 |
! |
| 43 |
! written by Daniel Price 2004-2021 |
| 44 |
! as part of the SPLASH SPH visualisation package |
| 45 |
!----------------------------------------------------------------- |
| 46 |
module geometry |
| 47 |
implicit none |
| 48 |
integer, parameter, public :: maxcoordsys = 8 |
| 49 |
integer, parameter, public :: igeom_cartesian = 1 |
| 50 |
integer, parameter, public :: igeom_cylindrical = 2 |
| 51 |
integer, parameter, public :: igeom_spherical = 3 |
| 52 |
integer, parameter, public :: igeom_toroidal = 4 |
| 53 |
integer, parameter, public :: igeom_rotated = 5 |
| 54 |
integer, parameter, public :: igeom_flaredcyl = 6 |
| 55 |
integer, parameter, public :: igeom_logflared = 7 |
| 56 |
integer, parameter, public :: igeom_planetwake = 8 |
| 57 |
|
| 58 |
character(len=24), dimension(maxcoordsys), parameter, public :: labelcoordsys = & |
| 59 |
(/'cartesian x,y,z ', & |
| 60 |
'cylindrical R,phi,z ', & |
| 61 |
'spherical r,phi,theta', & |
| 62 |
'toroidal r,theta,phi', & |
| 63 |
'rotated x_1,x_2,x_3', & |
| 64 |
'flared cyl R,phi,zdash', & |
| 65 |
'log flared logR,phi,zd', & |
| 66 |
'planet wake t,eta,z '/) |
| 67 |
character(len=6), dimension(3,maxcoordsys), parameter, public :: labelcoord = & |
| 68 |
reshape((/'x ','y ','z ', & |
| 69 |
'R ','\phi ','z ', & |
| 70 |
'r ','\phi ','\theta', & |
| 71 |
'r_t ','\theta','\phi ', & |
| 72 |
'x_1 ','x_2 ','x_3 ', & |
| 73 |
'R ','\phi ','zdash ', & |
| 74 |
'log R ','\phi ','zdash ', & |
| 75 |
't ','\eta ','z '/),shape=(/3,maxcoordsys/)) |
| 76 |
|
| 77 |
public :: coord_transform, vector_transform, coord_transform_limits |
| 78 |
public :: coord_is_length, coord_is_periodic, print_error |
| 79 |
public :: set_rotation_angles, get_coord_limits |
| 80 |
public :: set_flaring_index |
| 81 |
public :: set_planet_wake,planet_wake_t,planet_wake_eta |
| 82 |
public :: planet_wake_phi,planet_wake_r |
| 83 |
|
| 84 |
real, parameter, private :: pi = 4.*atan(1.) |
| 85 |
real, parameter, private :: Rtorus = 1.0 |
| 86 |
real, parameter, public :: small_number = 1.e-15 |
| 87 |
real, parameter, public :: large_number = 1.e30 |
| 88 |
! rotated cartesian |
| 89 |
real, private :: sina = 2./3. |
| 90 |
real, private :: cosa = sqrt(5.)/3. |
| 91 |
real, private :: sinb = 2./sqrt(5.) |
| 92 |
real, private :: cosb = 1./sqrt(5.) |
| 93 |
! flared cylindrical |
| 94 |
real, private :: xref = 1. |
| 95 |
real, private :: beta = 1.5 |
| 96 |
! planet wake |
| 97 |
real, private :: hp_on_rp = 0.05 |
| 98 |
real, private :: rp = 1. |
| 99 |
real, private :: phi_p = 0. |
| 100 |
real, private :: p_index = 1. |
| 101 |
real, private :: q_index = 0.25 |
| 102 |
|
| 103 |
integer, parameter, public :: ierr_invalid_dimsin = 1 |
| 104 |
integer, parameter, public :: ierr_invalid_dimsout = 2 |
| 105 |
integer, parameter, public :: ierr_invalid_dims = 3 |
| 106 |
integer, parameter, public :: ierr_warning_assuming_cartesian = -1 |
| 107 |
integer, parameter, public :: ierr_not_implemented = -2 |
| 108 |
|
| 109 |
abstract interface |
| 110 |
pure real function func(x) |
| 111 |
real, intent(in) :: x |
| 112 |
end function func |
| 113 |
end interface |
| 114 |
private |
| 115 |
|
| 116 |
contains |
| 117 |
!----------------------------------------------------------------- |
| 118 |
! utility that returns whether or not a particular coordinate |
| 119 |
! in a given coordinate system has dimensions of length or not |
| 120 |
!----------------------------------------------------------------- |
| 121 |
pure logical function coord_is_length(ix,igeom) |
| 122 |
integer, intent(in) :: ix,igeom |
| 123 |
|
| 124 |
coord_is_length = .false. |
| 125 |
select case(igeom) |
| 126 |
case(igeom_toroidal, igeom_spherical) |
| 127 |
if (ix==1) coord_is_length = .true. |
| 128 |
case(igeom_cylindrical,igeom_flaredcyl) |
| 129 |
if (ix==1 .or. ix==3) coord_is_length = .true. |
| 130 |
case(igeom_logflared) |
| 131 |
if (ix==3) coord_is_length = .true. |
| 132 |
case(igeom_planetwake) |
| 133 |
if (ix==1 .or. ix==3) coord_is_length = .true. |
| 134 |
case default |
| 135 |
coord_is_length = .true. |
| 136 |
end select |
| 137 |
|
| 138 |
end function coord_is_length |
| 139 |
|
| 140 |
!----------------------------------------------------------------- |
| 141 |
! utility that returns whether or not a particular coordinate |
| 142 |
! in a given coordinate system is periodic |
| 143 |
!----------------------------------------------------------------- |
| 144 |
pure logical function coord_is_periodic(ix,igeom) |
| 145 |
integer, intent(in) :: ix,igeom |
| 146 |
|
| 147 |
coord_is_periodic = .false. |
| 148 |
if ((igeom==igeom_cylindrical .or. igeom==igeom_spherical .or. & |
| 149 |
igeom==igeom_flaredcyl .or. igeom==igeom_logflared .and. ix==2) .or. & |
| 150 |
(igeom==igeom_toroidal .and. ix==3) .or. & |
| 151 |
(igeom==igeom_planetwake .and. ix==2)) then |
| 152 |
coord_is_periodic = .true. |
| 153 |
endif |
| 154 |
|
| 155 |
end function coord_is_periodic |
| 156 |
|
| 157 |
!-------------------------------------------------------- |
| 158 |
! utility to handle error printing so transform routines |
| 159 |
! do not generate verbose output |
| 160 |
!-------------------------------------------------------- |
| 161 |
|
| 162 |
subroutine print_error(ierr) |
| 163 |
integer, intent(in) :: ierr |
| 164 |
|
| 165 |
select case(ierr) |
| 166 |
case(ierr_invalid_dimsin) |
| 167 |
print*,'Error: coord transform: invalid number of dimensions on input' |
| 168 |
case(ierr_invalid_dimsout) |
| 169 |
print*,'Error: coord transform: invalid number of dimensions on output' |
| 170 |
case(ierr_invalid_dims) |
| 171 |
print*,'Error: coord transform: ndimout must be <= ndimin' |
| 172 |
case(ierr_warning_assuming_cartesian) |
| 173 |
print*,'warning: using default cartesian output' |
| 174 |
case(ierr_not_implemented) |
| 175 |
print*,'warning: coord transform not fully implemented' |
| 176 |
case default |
| 177 |
print*,' unknown error' |
| 178 |
end select |
| 179 |
|
| 180 |
end subroutine print_error |
| 181 |
|
| 182 |
!------------------------------------------------------------- |
| 183 |
! utility to set rotation angles for rotated cartesian system |
| 184 |
!------------------------------------------------------------- |
| 185 |
subroutine set_rotation_angles(a,b,sin_a,sin_b,cos_a,cos_b) |
| 186 |
real, intent(in), optional :: a,b,sin_a,sin_b,cos_a,cos_b |
| 187 |
|
| 188 |
if (present(a)) then |
| 189 |
sina = sin(a) |
| 190 |
cosa = cos(a) |
| 191 |
endif |
| 192 |
if (present(b)) then |
| 193 |
sinb = sin(b) |
| 194 |
cosb = cos(b) |
| 195 |
endif |
| 196 |
if (present(sin_a)) then |
| 197 |
sina = sin_a |
| 198 |
cosa = sqrt(1. - sina**2) |
| 199 |
endif |
| 200 |
if (present(sin_b)) then |
| 201 |
sinb = sin_b |
| 202 |
cosb = sqrt(1. - sinb**2) |
| 203 |
endif |
| 204 |
if (present(cos_a)) then |
| 205 |
cosa = cos_a |
| 206 |
if (.not.present(sin_a)) sina = sqrt(1. - cosa**2) |
| 207 |
endif |
| 208 |
if (present(cos_b)) then |
| 209 |
cosb = cos_b |
| 210 |
if (.not.present(cos_b)) sinb = sqrt(1. - cosb**2) |
| 211 |
endif |
| 212 |
|
| 213 |
end subroutine set_rotation_angles |
| 214 |
|
| 215 |
!------------------------------------------------------------- |
| 216 |
! utility to set flaring index and reference radius for |
| 217 |
! flared cylindrical coordinate systems |
| 218 |
!------------------------------------------------------------- |
| 219 |
subroutine set_flaring_index(r_ref,findex) |
| 220 |
real, intent(in) :: r_ref,findex |
| 221 |
|
| 222 |
xref = r_ref |
| 223 |
beta = findex |
| 224 |
|
| 225 |
end subroutine set_flaring_index |
| 226 |
|
| 227 |
!------------------------------------------------------------- |
| 228 |
! utility to set parameters for |
| 229 |
! planet wake coordinate system from Rafikov 2002 |
| 230 |
!------------------------------------------------------------- |
| 231 |
subroutine set_planet_wake(r,phi,p,q,honr) |
| 232 |
real, intent(in) :: r,phi,p,q,honr |
| 233 |
|
| 234 |
rp = r |
| 235 |
phi_p = phi |
| 236 |
p_index = p |
| 237 |
q_index = q |
| 238 |
hp_on_rp = honr |
| 239 |
|
| 240 |
end subroutine set_planet_wake |
| 241 |
|
| 242 |
!------------------------------------------------------------- |
| 243 |
! t coordinate for planet wake, from Rafikov 2002 |
| 244 |
!------------------------------------------------------------- |
| 245 |
pure real function planet_wake_t(r) result(t) |
| 246 |
real, intent(in) :: r |
| 247 |
real :: rr,coeff |
| 248 |
|
| 249 |
rr = r / rp |
| 250 |
coeff = 1.5*hp_on_rp**(-2.5)/(2.**0.25) |
| 251 |
if (rr > 0.) then |
| 252 |
t = coeff*integrate_trap(planet_t_integrand,1.,rr) |
| 253 |
else |
| 254 |
t = large_number |
| 255 |
endif |
| 256 |
|
| 257 |
end function planet_wake_t |
| 258 |
|
| 259 |
!------------------------------------------------------------- |
| 260 |
! inverse of above, i.e. find r(t) by rootfinding |
| 261 |
!------------------------------------------------------------- |
| 262 |
pure real function planet_wake_r(t) result(r) |
| 263 |
real, intent(in) :: t |
| 264 |
real :: rr,coeff,fr,fr_dash,dr |
| 265 |
integer :: it |
| 266 |
|
| 267 |
if (t >= large_number) then |
| 268 |
r = 0. |
| 269 |
return |
| 270 |
endif |
| 271 |
rr = 2. ! initial guess |
| 272 |
coeff = 1.5*hp_on_rp**(-2.5)/(2.**0.25) |
| 273 |
fr = 1e6 ! not converged |
| 274 |
it = 0 |
| 275 |
do while (abs(fr) > 1.e-6*rr .and. it < 30) |
| 276 |
fr = t - coeff*integrate_trap(planet_t_integrand,1.,rr) |
| 277 |
fr_dash = -coeff*planet_t_integrand(rr) |
| 278 |
dr = -fr/fr_dash ! Newton-Raphson |
| 279 |
if (rr + dr < 0.8*rr) then ! take care approaching zero |
| 280 |
rr = 0.8*rr |
| 281 |
! elseif (rr + dr > 1.2*rr) then |
| 282 |
! rr = 1.2*rr |
| 283 |
else |
| 284 |
rr = rr + dr |
| 285 |
endif |
| 286 |
it = it + 1 |
| 287 |
!print*,it,'rr=',rr*rp,' fr = ',fr,fr_dash,fr*rr |
| 288 |
enddo |
| 289 |
r = rr*rp |
| 290 |
|
| 291 |
end function planet_wake_r |
| 292 |
|
| 293 |
!------------------------------------------------------------- |
| 294 |
! eta coordinate for planet wake, from Rafikov 2002 |
| 295 |
!------------------------------------------------------------- |
| 296 |
pure real function planet_wake_eta(r,phi) result(eta) |
| 297 |
real, intent(in) :: r,phi |
| 298 |
real :: phi_w,deltaphi,coeff |
| 299 |
|
| 300 |
phi_w = planet_wake_phiw(r) |
| 301 |
deltaphi = phi - phi_w |
| 302 |
|
| 303 |
! wrap to between -pi and pi |
| 304 |
deltaphi = atan2(sin(deltaphi),cos(deltaphi)) |
| 305 |
|
| 306 |
coeff = 1.5/hp_on_rp |
| 307 |
eta = coeff * deltaphi |
| 308 |
|
| 309 |
end function planet_wake_eta |
| 310 |
|
| 311 |
!------------------------------------------------------------- |
| 312 |
! inverse of above, i.e. phi(r,eta) |
| 313 |
!------------------------------------------------------------- |
| 314 |
pure real function planet_wake_phi(r,eta) result(phi) |
| 315 |
real, intent(in) :: r,eta |
| 316 |
real :: coeff |
| 317 |
|
| 318 |
coeff = 1.5/hp_on_rp |
| 319 |
phi = eta/coeff + planet_wake_phiw(r) |
| 320 |
|
| 321 |
! wrap to -pi,pi |
| 322 |
phi = atan2(sin(phi),cos(phi)) |
| 323 |
|
| 324 |
end function planet_wake_phi |
| 325 |
|
| 326 |
!------------------------------------------------------------- |
| 327 |
! phi_w(r) for planet wake, from Rafikov 2002 |
| 328 |
!------------------------------------------------------------- |
| 329 |
pure real function planet_wake_phiw(r) result(phi_w) |
| 330 |
real, intent(in) :: r |
| 331 |
real :: rr |
| 332 |
|
| 333 |
rr = r / rp |
| 334 |
if (rr <= 0.) then |
| 335 |
phi_w = phi_p |
| 336 |
return |
| 337 |
endif |
| 338 |
phi_w = phi_p + sign(1.,r-rp)/hp_on_rp*((rr**(q_index-0.5))/(q_index-0.5) & |
| 339 |
- (rr**(q_index+1.))/(q_index+1.) & |
| 340 |
- 3./((2.*q_index-1.)*(q_index+1.))) |
| 341 |
|
| 342 |
end function planet_wake_phiw |
| 343 |
|
| 344 |
!------------------------------------------------------------- |
| 345 |
! t(r) for planet wake, from Rafikov 2002 |
| 346 |
!------------------------------------------------------------- |
| 347 |
pure real function planet_t_integrand(x) result(f) |
| 348 |
real, intent(in) :: x |
| 349 |
real :: w,rho |
| 350 |
|
| 351 |
rho = 5.*q_index + p_index |
| 352 |
w = 0.5*rho - 11./4. |
| 353 |
f = (-(1. - x**1.5)*abs(1. - x**1.5)**0.5) * x**w |
| 354 |
!f = (abs(1. - x**1.5)**1.5) * x**w |
| 355 |
|
| 356 |
end function planet_t_integrand |
| 357 |
|
| 358 |
!------------------------------------------------------------------- |
| 359 |
! helper routine to integrate a function using the trapezoidal rule |
| 360 |
!------------------------------------------------------------------- |
| 361 |
pure real function integrate_trap(f,xmin,xmax) result(g) |
| 362 |
real, intent(in) :: xmin,xmax |
| 363 |
procedure(func) :: f |
| 364 |
real :: fx,fprev,dx,x |
| 365 |
integer, parameter :: npts = 128 |
| 366 |
integer :: i |
| 367 |
|
| 368 |
g = 0. |
| 369 |
dx = (xmax-xmin)/(npts) |
| 370 |
fprev = f(xmin) |
| 371 |
do i=2,npts |
| 372 |
x = xmin + i*dx |
| 373 |
fx = f(x) |
| 374 |
g = g + 0.5*dx*(fx + fprev) |
| 375 |
fprev = fx |
| 376 |
enddo |
| 377 |
|
| 378 |
end function integrate_trap |
| 379 |
|
| 380 |
!----------------------------------------------------------------- |
| 381 |
! Subroutine to transform between different co-ordinate systems |
| 382 |
! (e.g. from cartesian to cylindrical polar and vice versa) |
| 383 |
! |
| 384 |
! xin(ndimin) : input co-ordinates, in ndimin dimensions |
| 385 |
! itypein : input co-ordinate type |
| 386 |
! |
| 387 |
! xout(ndimout) : output co-ordinates, in ndimout dimensions |
| 388 |
! itypeout : output co-ordinate type |
| 389 |
! |
| 390 |
!----------------------------------------------------------------- |
| 391 |
pure subroutine coord_transform(xin,ndimin,itypein,xout,ndimout,itypeout,err) |
| 392 |
integer, intent(in) :: ndimin,ndimout,itypein,itypeout |
| 393 |
real, intent(in) :: xin(ndimin) |
| 394 |
real, intent(out) :: xout(ndimout) |
| 395 |
integer, intent(out), optional :: err |
| 396 |
real :: rcyl,phi,xi(3),xouti(3),sintheta |
| 397 |
integer :: ierr |
| 398 |
! |
| 399 |
!--check for errors in input |
| 400 |
! |
| 401 |
ierr = 0 |
| 402 |
if (itypeout==itypein) then |
| 403 |
xout(1:ndimout) = xin(1:ndimout) |
| 404 |
return |
| 405 |
elseif (ndimin < 1.or.ndimin > 3) then |
| 406 |
ierr = ierr_invalid_dimsin |
| 407 |
if (present(err)) err = ierr |
| 408 |
return |
| 409 |
elseif (ndimout < 1.or.ndimout > 3) then |
| 410 |
ierr = ierr_invalid_dimsout |
| 411 |
if (present(err)) err = ierr |
| 412 |
return |
| 413 |
elseif (ndimout > ndimin) then |
| 414 |
ierr = ierr_invalid_dims |
| 415 |
if (present(err)) err = ierr |
| 416 |
return |
| 417 |
endif |
| 418 |
if (itypein /= 1 .and. itypeout /= 1) ierr = ierr_warning_assuming_cartesian |
| 419 |
! |
| 420 |
!--now do transformation |
| 421 |
! |
| 422 |
select case(itypein) |
| 423 |
case(2) |
| 424 |
! |
| 425 |
!--input is cylindrical polars, output is cartesian |
| 426 |
! |
| 427 |
if (ndimout==1) then |
| 428 |
xout(1) = xin(1) |
| 429 |
else ! r,phi,z -> x,y,z |
| 430 |
xout(1) = xin(1)*cos(xin(2)) |
| 431 |
xout(2) = xin(1)*sin(xin(2)) |
| 432 |
if (ndimout > 2) xout(3) = xin(3) |
| 433 |
endif |
| 434 |
case(3) |
| 435 |
! |
| 436 |
!--input is spherical polars, output is cartesian |
| 437 |
! |
| 438 |
select case(ndimout) |
| 439 |
case(1) ! r -> x |
| 440 |
xout(1) = xin(1) |
| 441 |
case(2) ! r,phi -> x,y |
| 442 |
xout(1) = xin(1)*cos(xin(2)) |
| 443 |
xout(2) = xin(1)*sin(xin(2)) |
| 444 |
case(3) ! r,phi,theta -> x,y,z |
| 445 |
sintheta = sin(xin(3)) |
| 446 |
xout(1) = xin(1)*cos(xin(2))*sintheta |
| 447 |
xout(2) = xin(1)*sin(xin(2))*sintheta |
| 448 |
xout(3) = xin(1)*cos(xin(3)) |
| 449 |
end select |
| 450 |
case(4) |
| 451 |
! |
| 452 |
!--input is torus co-ordinates, output is cartesian |
| 453 |
! |
| 454 |
if (ndimin /= 3) then |
| 455 |
xout(1:ndimout) = xin(1:ndimout) |
| 456 |
else |
| 457 |
rcyl = xin(1)*cos(xin(2)) + Rtorus |
| 458 |
xout(1) = rcyl*cos(xin(3)) |
| 459 |
if (ndimout >= 2) xout(2) = rcyl*sin(xin(3)) |
| 460 |
if (ndimout >= 3) xout(3) = xin(1)*sin(xin(2)) |
| 461 |
endif |
| 462 |
|
| 463 |
case(5) |
| 464 |
! |
| 465 |
!--input is rotated cartesian coordinates, output is cartesian |
| 466 |
! |
| 467 |
xi = 0. |
| 468 |
xi(1:ndimin) = xin |
| 469 |
xouti(1) = xi(1)*cosa*cosb - xi(2)*sinb - xi(3)*sina*cosb |
| 470 |
xouti(2) = xi(1)*cosa*sinb + xi(2)*cosb - xi(3)*sina*sinb |
| 471 |
xouti(3) = xi(1)*sina + xi(3)*cosa |
| 472 |
xout(1:ndimout) = xouti |
| 473 |
case(6,7) |
| 474 |
! |
| 475 |
!--input is flared cylindrical polars or log flared, output is cartesian |
| 476 |
! |
| 477 |
rcyl = xin(1) |
| 478 |
if (itypein==7) rcyl = 10**xin(1) ! log flared |
| 479 |
if (ndimout==1) then |
| 480 |
xout(1) = rcyl |
| 481 |
else ! r,phi,zdash -> x,y,z |
| 482 |
xout(1) = rcyl*cos(xin(2)) |
| 483 |
xout(2) = rcyl*sin(xin(2)) |
| 484 |
if (ndimout > 2) xout(3) = xin(3)*(rcyl/xref)**beta |
| 485 |
endif |
| 486 |
case(igeom_planetwake) |
| 487 |
! |
| 488 |
!--input is planet wake, output is cartesian |
| 489 |
! |
| 490 |
rcyl = planet_wake_r(xin(1)) |
| 491 |
if (ndimout==1) then |
| 492 |
xout(1) = rcyl |
| 493 |
else ! t,eta,z -> x,y,z |
| 494 |
phi = planet_wake_phi(rcyl,xin(2)) |
| 495 |
xout(1) = rcyl*cos(phi) |
| 496 |
xout(2) = rcyl*sin(phi) |
| 497 |
if (ndimout > 2) xout(3) = xin(3) |
| 498 |
endif |
| 499 |
! |
| 500 |
!--input is cartesian co-ordinates |
| 501 |
! |
| 502 |
case default |
| 503 |
select case(itypeout) |
| 504 |
case(2) |
| 505 |
! |
| 506 |
!--output is cylindrical |
| 507 |
! |
| 508 |
if (ndimin==1) then |
| 509 |
xout(1) = abs(xin(1)) ! cylindrical r |
| 510 |
else |
| 511 |
xout(1) = sqrt(dot_product(xin(1:2),xin(1:2))) |
| 512 |
if (ndimout >= 2) xout(2) = atan2(xin(2),xin(1)) ! phi |
| 513 |
if (ndimout==3) xout(3) = xin(3) ! z |
| 514 |
endif |
| 515 |
case(3) |
| 516 |
! |
| 517 |
!--output is spherical |
| 518 |
! |
| 519 |
xout(1) = sqrt(dot_product(xin,xin))! r |
| 520 |
if (ndimout >= 2) xout(2) = atan2(xin(2),xin(1)) ! phi |
| 521 |
if (ndimout >= 3) then |
| 522 |
! theta = acos(z/r) |
| 523 |
if (xout(1) > 0.) then |
| 524 |
xout(3) = acos(xin(3)/xout(1)) |
| 525 |
else |
| 526 |
xout(3) = 0. |
| 527 |
endif |
| 528 |
endif |
| 529 |
case(4) |
| 530 |
! |
| 531 |
!--output is torus r,theta,phi co-ordinates |
| 532 |
! |
| 533 |
if (ndimin /= 3) then |
| 534 |
! not applicable if ndim < 3 |
| 535 |
xout(1:ndimout) = xin(1:ndimout) |
| 536 |
else |
| 537 |
rcyl = sqrt(xin(1)**2 + xin(2)**2) |
| 538 |
xout(1) = sqrt(xin(3)**2 + (rcyl - Rtorus)**2) |
| 539 |
if (ndimout >= 2) xout(2) = atan2(xin(3),rcyl-Rtorus) ! asin(xin(3)/xout(1)) |
| 540 |
if (ndimout >= 3) xout(3) = atan2(xin(2),xin(1)) |
| 541 |
endif |
| 542 |
case(5) |
| 543 |
! |
| 544 |
!--output is rotated cartesian x1, x2, x3 |
| 545 |
! |
| 546 |
xi = 0. |
| 547 |
xi(1:ndimin) = xin |
| 548 |
xouti(1) = cosa*(xi(1)*cosb + xi(2)*sinb) + xi(3)*sina |
| 549 |
xouti(2) = xi(2)*cosb - xi(1)*sinb |
| 550 |
xouti(3) = -sina*(xi(1)*cosb + xi(2)*sinb) + xi(3)*cosa |
| 551 |
xout(1:ndimout) = xouti(1:ndimout) |
| 552 |
case(6,7) |
| 553 |
! |
| 554 |
!--output is flared cylindrical |
| 555 |
! |
| 556 |
if (ndimin==1) then |
| 557 |
xout(1) = abs(xin(1)) ! cylindrical r |
| 558 |
else |
| 559 |
xout(1) = sqrt(dot_product(xin(1:2),xin(1:2))) |
| 560 |
if (ndimout >= 2) xout(2) = atan2(xin(2),xin(1)) ! phi |
| 561 |
if (ndimout==3) then |
| 562 |
if (xout(1) > 0.) then |
| 563 |
xout(3) = xin(3)*(xref/xout(1))**beta ! zdash |
| 564 |
else |
| 565 |
xout(3) = xin(3) ! this is arbitrary |
| 566 |
endif |
| 567 |
endif |
| 568 |
endif |
| 569 |
! |
| 570 |
!--output is log flared |
| 571 |
! |
| 572 |
if (itypeout==7) xout(1) = log10(max(xout(1),small_number)) |
| 573 |
|
| 574 |
case(igeom_planetwake) |
| 575 |
! |
| 576 |
!--output is planet wake |
| 577 |
! |
| 578 |
if (ndimin==1) then |
| 579 |
rcyl = abs(xin(1)) |
| 580 |
xout(1) = planet_wake_t(rcyl) |
| 581 |
else |
| 582 |
rcyl = sqrt(dot_product(xin(1:2),xin(1:2))) |
| 583 |
phi = atan2(xin(2),xin(1)) |
| 584 |
xout(1) = planet_wake_t(rcyl) |
| 585 |
if (ndimout >= 2) xout(2) = planet_wake_eta(rcyl,phi) |
| 586 |
if (ndimout==3) xout(3) = xin(3) |
| 587 |
endif |
| 588 |
case default |
| 589 |
! |
| 590 |
! just copy |
| 591 |
! |
| 592 |
xout(1:ndimout) = xin(1:ndimout) |
| 593 |
end select |
| 594 |
end select |
| 595 |
|
| 596 |
if (present(err)) err = ierr |
| 597 |
return |
| 598 |
end subroutine coord_transform |
| 599 |
|
| 600 |
!----------------------------------------------------------------- |
| 601 |
! Subroutine to transform vector components |
| 602 |
! between different co-ordinate systems |
| 603 |
! (e.g. from cartesian to cylindrical polar and vice versa) |
| 604 |
! |
| 605 |
! Arguments: |
| 606 |
! xin(ndimin) : input co-ordinates, in ndimin dimensions |
| 607 |
! vecin(ndimin) : components of vector in input co-ordinate basis |
| 608 |
! itypein : input co-ordinate type |
| 609 |
! |
| 610 |
! vecout(ndimout) : components of vector in output co-ordinate basis |
| 611 |
! itypeout : output co-ordinate type |
| 612 |
! |
| 613 |
! coords must be one of the following: |
| 614 |
! 'cartesian' (default) |
| 615 |
! 'cylindrical' |
| 616 |
! 'spherical' |
| 617 |
! |
| 618 |
! Currently handles: |
| 619 |
! |
| 620 |
! cartesian -> cylindrical, spherical polar |
| 621 |
! cylindrical -> cartesian |
| 622 |
! spherical polar -> cartesian |
| 623 |
! |
| 624 |
!----------------------------------------------------------------- |
| 625 |
pure subroutine vector_transform(xin,vecin,ndimin,itypein,vecout,ndimout,itypeout,err) |
| 626 |
integer, intent(in) :: ndimin,ndimout,itypein,itypeout |
| 627 |
real, intent(in) :: xin(ndimin),vecin(ndimin) |
| 628 |
real, intent(out) :: vecout(ndimout) |
| 629 |
integer, intent(out), optional :: err |
| 630 |
integer :: i,ierr |
| 631 |
real :: dxdx(3,3) |
| 632 |
real :: sinphi,cosphi,sintheta,costheta |
| 633 |
real :: rr,rr1,rcyl,rcyl2,rcyl1,fac,zdash |
| 634 |
|
| 635 |
ierr = 0 |
| 636 |
! |
| 637 |
!--check for errors in input |
| 638 |
! |
| 639 |
if (ndimout > ndimin) then |
| 640 |
ierr = ierr_invalid_dims |
| 641 |
if (present(err)) err = ierr |
| 642 |
return |
| 643 |
elseif (itypein==itypeout) then |
| 644 |
vecout(1:ndimout) = vecin(1:ndimout) |
| 645 |
return |
| 646 |
elseif (ndimin < 1.or.ndimin > 3) then |
| 647 |
ierr = ierr_invalid_dimsin |
| 648 |
if (present(err)) err = ierr |
| 649 |
return |
| 650 |
elseif (ndimout < 1.or.ndimout > 3) then |
| 651 |
ierr = ierr_invalid_dimsout |
| 652 |
if (present(err)) err = ierr |
| 653 |
return |
| 654 |
elseif (itypein==igeom_planetwake .or. itypeout==igeom_planetwake) then |
| 655 |
ierr = ierr_not_implemented |
| 656 |
if (present(err)) err = ierr |
| 657 |
vecout(1:ndimout) = vecin(1:ndimout) |
| 658 |
return |
| 659 |
endif |
| 660 |
! |
| 661 |
!--set Jacobian matrix to zero |
| 662 |
! |
| 663 |
dxdx = 0. |
| 664 |
! |
| 665 |
!--calculate non-zero components of Jacobian matrix for the transformation |
| 666 |
! |
| 667 |
select case(itypein) |
| 668 |
case(6,7) |
| 669 |
! |
| 670 |
!--input is flared cylindrical or log flared, output is cartesian |
| 671 |
! |
| 672 |
rcyl = xin(1) |
| 673 |
sinphi = sin(xin(2)) |
| 674 |
cosphi = cos(xin(2)) |
| 675 |
fac = 1. |
| 676 |
if (itypein==7) then |
| 677 |
rcyl = 10**(xin(1)) |
| 678 |
if (rcyl > small_number) fac = rcyl*log(10.) ! dR/d(log R) |
| 679 |
endif |
| 680 |
dxdx(1,1) = cosphi*fac ! dx/dr |
| 681 |
dxdx(1,2) = -sinphi ! 1/r*dx/dphi |
| 682 |
dxdx(2,1) = sinphi*fac ! dy/dr |
| 683 |
dxdx(2,2) = cosphi ! 1/r*dy/dphi |
| 684 |
dxdx(3,1) = beta*xin(3)*(rcyl**(beta - 1.))/xref**beta*fac ! dz/dR |
| 685 |
if (rcyl > small_number) then |
| 686 |
dxdx(3,3) = 1.*(rcyl/xref)**beta ! dz/dzdash |
| 687 |
else |
| 688 |
dxdx(3,3) = 1. |
| 689 |
endif |
| 690 |
! |
| 691 |
!--rotated cartesian |
| 692 |
! |
| 693 |
case(5) |
| 694 |
call coord_transform(vecin,ndimin,itypein,vecout,ndimout,itypeout,err=ierr) |
| 695 |
if (present(err)) err = ierr |
| 696 |
return |
| 697 |
case(4) |
| 698 |
! |
| 699 |
!--input is toroidal, output cartesian |
| 700 |
! |
| 701 |
dxdx(1,1) = cos(xin(2))*cos(xin(3)) ! dx/dr |
| 702 |
dxdx(1,2) = -sin(xin(2))*cos(xin(3)) ! 1/r dx/dtheta |
| 703 |
dxdx(1,3) = -sin(xin(3)) ! 1/rcyl dx/dphi |
| 704 |
dxdx(2,1) = cos(xin(2))*sin(xin(3)) ! dy/dr |
| 705 |
dxdx(2,2) = -sin(xin(2))*sin(xin(3)) ! 1/r dy/dtheta |
| 706 |
dxdx(2,3) = cos(xin(3)) ! 1/rcyl dy/dphi |
| 707 |
dxdx(3,1) = sin(xin(2)) ! dz/dr |
| 708 |
dxdx(3,2) = cos(xin(2)) ! 1/r dz/dtheta |
| 709 |
! dxdx(3,3) = 0. ! dz/dphi |
| 710 |
case(3) |
| 711 |
! |
| 712 |
!--input is spherical polars, output cartesian |
| 713 |
! |
| 714 |
sinphi = sin(xin(2)) |
| 715 |
cosphi = cos(xin(2)) |
| 716 |
sintheta = sin(xin(3)) |
| 717 |
costheta = cos(xin(3)) |
| 718 |
dxdx(1,1) = cosphi*sintheta ! dx/dr |
| 719 |
dxdx(1,2) = -sinphi ! 1/rcyl dx/dphi |
| 720 |
dxdx(1,3) = cosphi*costheta ! 1/r dx/dtheta |
| 721 |
dxdx(2,1) = sinphi*sintheta ! dy/dr |
| 722 |
dxdx(2,2) = cosphi ! 1/rcyl dy/dphi |
| 723 |
dxdx(2,3) = sinphi*costheta ! 1/r dy/dtheta |
| 724 |
dxdx(3,1) = costheta ! dz/dr |
| 725 |
dxdx(3,3) = -sintheta ! 1/r dz/dtheta |
| 726 |
case(2) |
| 727 |
! |
| 728 |
!--input is cylindrical polars, output cartesian |
| 729 |
! |
| 730 |
sinphi = sin(xin(2)) |
| 731 |
cosphi = cos(xin(2)) |
| 732 |
dxdx(1,1) = cosphi ! dx/dr |
| 733 |
dxdx(1,2) = -sinphi ! 1/r*dx/dphi |
| 734 |
dxdx(2,1) = sinphi ! dy/dr |
| 735 |
dxdx(2,2) = cosphi ! 1/r*dy/dphi |
| 736 |
dxdx(3,3) = 1. ! dz/dz |
| 737 |
! |
| 738 |
!--input is cartesian co-ordinates (default) |
| 739 |
! |
| 740 |
case default |
| 741 |
select case(itypeout) |
| 742 |
case(6,7) |
| 743 |
! |
| 744 |
!--output is flared cylindrical |
| 745 |
! |
| 746 |
rr = sqrt(dot_product(xin(1:min(ndimin,2)),xin(1:min(ndimin,2)))) |
| 747 |
sinphi = 0. |
| 748 |
fac = 1. |
| 749 |
if (rr > small_number) then |
| 750 |
rr1 = 1./rr |
| 751 |
cosphi = xin(1)*rr1 |
| 752 |
if (ndimin >= 2) sinphi = xin(2)*rr1 |
| 753 |
if (itypeout==7) fac = rr1/log(10.) ! d(log R)/dR |
| 754 |
else |
| 755 |
rr1 = 1./xref |
| 756 |
cosphi = 1. |
| 757 |
endif |
| 758 |
dxdx(1,1) = fac*cosphi ! xin(1)*rr1 = dr/dx |
| 759 |
dxdx(1,2) = fac*sinphi ! xin(2)*rr1 = dr/dy |
| 760 |
dxdx(2,1) = -sinphi ! r*dphi/dx |
| 761 |
dxdx(2,2) = cosphi ! r*dphi/dy |
| 762 |
if (ndimin==3 .and. ndimout==3) then |
| 763 |
zdash = xin(3)*(xref*rr1)**beta |
| 764 |
dxdx(3,1) = -beta*cosphi*zdash*rr1 ! dzdash/dx |
| 765 |
dxdx(3,2) = -beta*sinphi*zdash*rr1 ! dzdash/dy |
| 766 |
dxdx(3,3) = 1.*(xref*rr1)**beta ! dzdash/dz |
| 767 |
endif |
| 768 |
case(5) |
| 769 |
call coord_transform(vecin,ndimin,itypein,vecout,ndimout,itypeout,err=ierr) |
| 770 |
return |
| 771 |
case(4) |
| 772 |
! |
| 773 |
! output is toroidal |
| 774 |
! |
| 775 |
rcyl = sqrt(xin(1)**2 + xin(2)**2) |
| 776 |
sinphi = 0. |
| 777 |
if (rcyl > tiny(rcyl)) then |
| 778 |
rcyl1 = 1./rcyl |
| 779 |
cosphi = xin(1)*rcyl1 |
| 780 |
sinphi = xin(2)*rcyl1 |
| 781 |
else |
| 782 |
rcyl1 = 0. |
| 783 |
cosphi = 1. |
| 784 |
endif |
| 785 |
rr = sqrt((rcyl - Rtorus)**2 + xin(3)**2) |
| 786 |
if (rr > tiny(rr)) then |
| 787 |
rr1 = 1./rr |
| 788 |
costheta = (rcyl - Rtorus)*rr1 |
| 789 |
sintheta = xin(3)*rr1 |
| 790 |
else |
| 791 |
rr1 = 0. |
| 792 |
costheta = 1. |
| 793 |
sintheta = 0. |
| 794 |
endif |
| 795 |
dxdx(1,1) = costheta*cosphi ! dr/dx |
| 796 |
dxdx(1,2) = costheta*sinphi ! dr/dy |
| 797 |
dxdx(1,3) = sintheta ! dr/dz |
| 798 |
dxdx(2,1) = -sintheta*cosphi ! dtheta/dx |
| 799 |
dxdx(2,2) = -sintheta*sinphi ! dtheta/dy |
| 800 |
dxdx(2,3) = costheta ! dtheta/dz |
| 801 |
dxdx(3,1) = -sinphi ! dphi/dx |
| 802 |
dxdx(3,2) = cosphi ! dphi/dy |
| 803 |
! dxdx(3,3) = 0. ! dphi/dz |
| 804 |
case(3) |
| 805 |
! |
| 806 |
! output is spherical |
| 807 |
! |
| 808 |
rr = sqrt(dot_product(xin,xin)) |
| 809 |
cosphi = 1. |
| 810 |
sinphi = 0. |
| 811 |
costheta = 1. |
| 812 |
sintheta = 0. |
| 813 |
if (ndimin==3 .and. rr > 0.) costheta = xin(3)/rr |
| 814 |
if (ndimin >= 2) then |
| 815 |
rcyl2 = dot_product(xin(1:2),xin(1:2)) |
| 816 |
if (rcyl2 > 0.) then |
| 817 |
rcyl = sqrt(rcyl2) |
| 818 |
rcyl1 = 1./rcyl |
| 819 |
cosphi = xin(1)*rcyl1 |
| 820 |
sinphi = xin(2)*rcyl1 |
| 821 |
sintheta = rcyl/rr |
| 822 |
else |
| 823 |
rcyl = 0. |
| 824 |
endif |
| 825 |
endif |
| 826 |
dxdx(1,1) = cosphi*sintheta ! dr/dx |
| 827 |
dxdx(1,2) = sinphi*sintheta ! dr/dy |
| 828 |
dxdx(1,3) = costheta ! dr/dz |
| 829 |
dxdx(2,1) = -sinphi !-xin(2)*rcyl1 = rcyl dphi/dx |
| 830 |
dxdx(2,2) = cosphi ! xin(1)*rcyl1 = rcyl dphi/dy |
| 831 |
dxdx(2,3) = 0. |
| 832 |
dxdx(3,1) = costheta*cosphi ! xin(1)*xin(3)*rr1*rcyl1 = r dtheta/dx |
| 833 |
dxdx(3,2) = costheta*sinphi ! xin(2)*xin(3)*rr1*rcyl1 = r dtheta/dy |
| 834 |
dxdx(3,3) = -sintheta ! -rcyl2*rr1*rcyl1 = r dtheta/dz |
| 835 |
case(2) |
| 836 |
! |
| 837 |
!--output is cylindrical |
| 838 |
! |
| 839 |
rr = sqrt(dot_product(xin(1:min(ndimin,2)),xin(1:min(ndimin,2)))) |
| 840 |
sinphi = 0. |
| 841 |
if (rr > tiny(rr)) then |
| 842 |
rr1 = 1./rr |
| 843 |
cosphi = xin(1)*rr1 |
| 844 |
if (ndimin >= 2) sinphi = xin(2)*rr1 |
| 845 |
else |
| 846 |
cosphi = 1. |
| 847 |
endif |
| 848 |
dxdx(1,1) = cosphi ! dr/dx |
| 849 |
dxdx(1,2) = sinphi ! dr/dy |
| 850 |
dxdx(2,1) = -sinphi ! r*dphi/dx |
| 851 |
dxdx(2,2) = cosphi ! r*dphi/dy |
| 852 |
dxdx(3,3) = 1. ! dz/dz |
| 853 |
case default |
| 854 |
ierr = ierr_warning_assuming_cartesian |
| 855 |
vecout(1:ndimout) = vecin(1:ndimout) |
| 856 |
return |
| 857 |
end select |
| 858 |
end select |
| 859 |
! |
| 860 |
!--now perform transformation using Jacobian matrix |
| 861 |
! |
| 862 |
do i=1,ndimout |
| 863 |
vecout(i) = dot_product(dxdx(i,1:ndimin),vecin(1:ndimin)) |
| 864 |
enddo |
| 865 |
|
| 866 |
if (present(err)) err = ierr |
| 867 |
return |
| 868 |
end subroutine vector_transform |
| 869 |
|
| 870 |
!------------------------------------------------------------------ |
| 871 |
! this subroutine attempts to switch plot limits / boundaries |
| 872 |
! between various co-ordinate systems. |
| 873 |
!------------------------------------------------------------------ |
| 874 |
recursive subroutine coord_transform_limits(xmin,xmax,itypein,itypeout,ndim) |
| 875 |
integer, intent(in) :: itypein,itypeout,ndim |
| 876 |
real, dimension(ndim), intent(inout) :: xmin,xmax |
| 877 |
real, dimension(ndim) :: xmaxtemp,xmintemp |
| 878 |
real :: rcyl |
| 879 |
integer :: i |
| 880 |
! |
| 881 |
!--check for errors in input |
| 882 |
! |
| 883 |
if (ndim < 1 .or. ndim > 3) then |
| 884 |
print*,'Error: limits coord transform: ndim invalid on input' |
| 885 |
return |
| 886 |
endif |
| 887 |
! |
| 888 |
!--for transformations that are not to/from cartesian |
| 889 |
! we transform to cartesians as an intermediate step |
| 890 |
! |
| 891 |
if (itypein /= 1 .and. itypeout /= 1) then |
| 892 |
call coord_transform_limits(xmin,xmax,itypein,1,ndim) |
| 893 |
call coord_transform_limits(xmin,xmax,1,itypeout,ndim) |
| 894 |
return |
| 895 |
endif |
| 896 |
!print*,'modifying plot limits for new coordinate system' |
| 897 |
! |
| 898 |
!--by default do nothing |
| 899 |
! |
| 900 |
xmintemp(1:ndim) = xmin(1:ndim) |
| 901 |
xmaxtemp(1:ndim) = xmax(1:ndim) |
| 902 |
|
| 903 |
select case(itypein) |
| 904 |
case(6,7) |
| 905 |
! |
| 906 |
!--flared cylindrical or log flared in, cartesian out |
| 907 |
! |
| 908 |
rcyl = abs(xmax(1)) |
| 909 |
if (itypein==igeom_logflared) rcyl = 10**(abs(xmax(1))) |
| 910 |
xmintemp(:) = -rcyl |
| 911 |
xmaxtemp(:) = rcyl |
| 912 |
if (ndim > 2) then |
| 913 |
xmintemp(3) = xmin(3)*(rcyl/xref)**beta |
| 914 |
xmaxtemp(3) = xmax(3)*(rcyl/xref)**beta |
| 915 |
endif |
| 916 |
|
| 917 |
case(5) |
| 918 |
! |
| 919 |
!--rotated cartesian in, cartesian out |
| 920 |
! |
| 921 |
call coord_transform(xmin,ndim,itypein,xmintemp,ndim,itypeout) |
| 922 |
call coord_transform(xmax,ndim,itypein,xmaxtemp,ndim,itypeout) |
| 923 |
case(4) |
| 924 |
! |
| 925 |
!--toroidal in, cartesian out |
| 926 |
! |
| 927 |
xmintemp(1:min(ndim,2)) = -Rtorus - xmax(1) |
| 928 |
xmaxtemp(1:min(ndim,2)) = Rtorus + xmax(1) |
| 929 |
if (ndim==3) then |
| 930 |
xmintemp(3) = -xmax(1) |
| 931 |
xmaxtemp(3) = xmax(1) |
| 932 |
endif |
| 933 |
case(3) |
| 934 |
! |
| 935 |
!--spherical in, cartesian out |
| 936 |
! |
| 937 |
xmintemp(1:ndim) = -xmax(1) |
| 938 |
xmaxtemp(1:ndim) = xmax(1) |
| 939 |
case(2) |
| 940 |
! |
| 941 |
!--cylindrical in, cartesian out |
| 942 |
! |
| 943 |
xmintemp(1:max(ndim,2)) = -xmax(1) |
| 944 |
xmaxtemp(1:max(ndim,2)) = xmax(1) |
| 945 |
|
| 946 |
case default |
| 947 |
! |
| 948 |
!--input is cartesian |
| 949 |
! |
| 950 |
select case(itypeout) |
| 951 |
case(5) |
| 952 |
! |
| 953 |
!--rotated cartesian |
| 954 |
! |
| 955 |
call coord_transform(xmin,ndim,itypein,xmintemp,ndim,itypeout) |
| 956 |
call coord_transform(xmax,ndim,itypein,xmaxtemp,ndim,itypeout) |
| 957 |
|
| 958 |
case(4) |
| 959 |
! |
| 960 |
!--output is toroidal |
| 961 |
! |
| 962 |
xmintemp(1) = 0. |
| 963 |
xmaxtemp(1) = max(maxval(abs(xmax(1:min(ndim,2))))-Rtorus, & |
| 964 |
maxval(abs(xmin(1:min(ndim,2))))-Rtorus) |
| 965 |
if (ndim >= 2) then |
| 966 |
xmintemp(2) = -0.5*pi |
| 967 |
xmaxtemp(2) = 0.5*pi |
| 968 |
if (ndim >= 3) then |
| 969 |
xmintemp(3) = -pi |
| 970 |
xmaxtemp(3) = pi |
| 971 |
endif |
| 972 |
endif |
| 973 |
! |
| 974 |
!--output is spherical |
| 975 |
! |
| 976 |
case(3) |
| 977 |
!--rmin, rmax |
| 978 |
xmintemp(1) = 0. |
| 979 |
xmaxtemp(1) = max(maxval(abs(xmin(1:ndim))), & |
| 980 |
maxval(abs(xmax(1:ndim)))) |
| 981 |
if (ndim >= 2) then |
| 982 |
xmintemp(2) = -pi |
| 983 |
xmaxtemp(2) = pi |
| 984 |
if (ndim >= 3) then |
| 985 |
xmintemp(3) = 0. |
| 986 |
xmaxtemp(3) = pi |
| 987 |
endif |
| 988 |
endif |
| 989 |
! |
| 990 |
!--output is cylindrical, flared cylindrical or log flared cylindrical |
| 991 |
! |
| 992 |
case(2,6,7) |
| 993 |
!--rmin, rmax |
| 994 |
xmintemp(1) = 0. |
| 995 |
if (ndim >= 2) then |
| 996 |
xmaxtemp(1) = sqrt(max(xmin(1)**2,xmin(2)**2,xmax(1)**2,xmax(2)**2)) |
| 997 |
xmintemp(2) = -pi |
| 998 |
xmaxtemp(2) = pi |
| 999 |
if (itypeout==igeom_flaredcyl .and. ndim >= 3) then |
| 1000 |
xmintemp(3) = xmin(3)*(xref/xmaxtemp(1))**beta |
| 1001 |
xmaxtemp(3) = xmax(3)*(xref/xmaxtemp(1))**beta |
| 1002 |
endif |
| 1003 |
else |
| 1004 |
xmaxtemp(1) = max(abs(xmin(1)),abs(xmax(1))) |
| 1005 |
endif |
| 1006 |
if (itypeout==igeom_logflared) then |
| 1007 |
xmintemp(1) = log10(small_number) ! log zero |
| 1008 |
xmaxtemp(1) = log10(xmaxtemp(1)) |
| 1009 |
endif |
| 1010 |
end select |
| 1011 |
end select |
| 1012 |
|
| 1013 |
do i=1,ndim |
| 1014 |
xmin(i) = min(xmintemp(i),xmaxtemp(i)) |
| 1015 |
xmax(i) = max(xmintemp(i),xmaxtemp(i)) |
| 1016 |
enddo |
| 1017 |
|
| 1018 |
return |
| 1019 |
end subroutine coord_transform_limits |
| 1020 |
|
| 1021 |
!------------------------------------------------------------ |
| 1022 |
! Routine to assist with interpolation in non-cartesian |
| 1023 |
! coordinate systems |
| 1024 |
! |
| 1025 |
! IN: |
| 1026 |
! xin - position in cartesian coords |
| 1027 |
! rad - radius of sphere around xin in cartesians |
| 1028 |
! OUT: |
| 1029 |
! xout - position in desired coord system |
| 1030 |
! xmin, xmax - bounding box of sphere in new coord system |
| 1031 |
! |
| 1032 |
!------------------------------------------------------------ |
| 1033 |
subroutine get_coord_limits(rad,xin,xout,xmin,xmax,itypein) |
| 1034 |
real, intent(in) :: rad,xin(3) |
| 1035 |
real, intent(out) :: xout(3),xmin(3),xmax(3) |
| 1036 |
integer, intent(in) :: itypein |
| 1037 |
real :: r,rcyl,dphi,dtheta,fac |
| 1038 |
|
| 1039 |
select case(itypein) |
| 1040 |
case(igeom_planetwake) ! planet wake |
| 1041 |
rcyl = sqrt(xin(1)**2 + xin(2)**2) |
| 1042 |
dphi = atan2(xin(2),xin(1)) |
| 1043 |
xout(1) = planet_wake_t(rcyl) |
| 1044 |
xout(2) = planet_wake_eta(rcyl,dphi) |
| 1045 |
xout(3) = xin(3) |
| 1046 |
xmin(1) = planet_wake_t(max(rcyl-rad,0.)) ! min "time" |
| 1047 |
xmax(1) = planet_wake_t(rcyl+rad) ! max "time" |
| 1048 |
xmin(2) = -pi |
| 1049 |
xmax(2) = pi |
| 1050 |
xmin(3) = xout(3)-rad |
| 1051 |
xmax(3) = xout(3)-rad |
| 1052 |
case(4) ! toroidal |
| 1053 |
rcyl = sqrt(xin(1)**2 + xin(2)**2) |
| 1054 |
r = sqrt(xin(3)**2 + (rcyl - Rtorus)**2) |
| 1055 |
xout(1) = r |
| 1056 |
xout(2) = atan2(xin(3),rcyl-Rtorus) ! asin(xin(3)/xout(1)) |
| 1057 |
xout(3) = atan2(xin(2),xin(1)) |
| 1058 |
xmin(1) = max(r-rad,0.) |
| 1059 |
xmax(1) = r+rad |
| 1060 |
if (r > 0. .and. xmin(1) > 0.) then |
| 1061 |
dtheta = asin(rad/r) |
| 1062 |
xmin(2) = max(xout(2) - dtheta,0.) |
| 1063 |
xmax(2) = min(xout(2) + dtheta,pi) |
| 1064 |
dphi = atan(rad/r) |
| 1065 |
xmin(3) = xout(3)-dphi |
| 1066 |
xmax(3) = xout(3)+dphi |
| 1067 |
else |
| 1068 |
xmin(2) = 0. |
| 1069 |
xmax(2) = pi |
| 1070 |
xmin(3) = -pi |
| 1071 |
xmax(3) = pi |
| 1072 |
endif |
| 1073 |
case(3) ! spherical |
| 1074 |
r = sqrt(dot_product(xin,xin)) |
| 1075 |
xout(1) = r |
| 1076 |
xout(2) = atan2(xin(2),xin(1)) ! phi |
| 1077 |
if (r > 0.) then |
| 1078 |
xout(3) = acos(xin(3)/r) ! theta = acos(z/r) |
| 1079 |
else |
| 1080 |
xout(3) = 0. |
| 1081 |
endif |
| 1082 |
xmin(1) = max(r - rad,0.) |
| 1083 |
xmax(1) = r + rad |
| 1084 |
if (r > 0. .and. xmin(1) > 0.) then |
| 1085 |
rcyl = sqrt(xin(1)**2 + xin(2)**2) |
| 1086 |
if (rcyl > rad) then |
| 1087 |
dphi = asin(rad/rcyl) |
| 1088 |
xmin(2) = xout(2)-dphi |
| 1089 |
xmax(2) = xout(2)+dphi |
| 1090 |
else |
| 1091 |
xmin(2) = -pi |
| 1092 |
xmax(2) = pi |
| 1093 |
endif |
| 1094 |
dtheta = asin(rad/r) |
| 1095 |
xmin(3) = xout(3)-dtheta |
| 1096 |
xmax(3) = xout(3)+dtheta |
| 1097 |
xmin(3) = max(xmin(3),0.) |
| 1098 |
xmax(3) = min(xmax(3),pi) |
| 1099 |
else |
| 1100 |
xmin(2) = -pi |
| 1101 |
xmax(2) = pi |
| 1102 |
xmin(3) = 0. |
| 1103 |
xmax(3) = pi |
| 1104 |
endif |
| 1105 |
case(2,6,7) ! cylindrical, flared cylindrical |
| 1106 |
r = sqrt(xin(1)**2 + xin(2)**2) |
| 1107 |
fac = 1. |
| 1108 |
if ((itypein==6 .or. itypein==7) .and. r > small_number) fac = (xref/r)**beta |
| 1109 |
xout(1) = r |
| 1110 |
xout(2) = atan2(xin(2),xin(1)) |
| 1111 |
xout(3) = xin(3)*fac |
| 1112 |
xmin(1) = max(r-rad,0.) |
| 1113 |
xmax(1) = r+rad |
| 1114 |
if (r > 0. .and. xmin(1) > 0.) then |
| 1115 |
dphi = atan(rad/r) |
| 1116 |
xmin(2) = xout(2)-dphi |
| 1117 |
xmax(2) = xout(2)+dphi |
| 1118 |
else |
| 1119 |
xmin(2) = -pi |
| 1120 |
xmax(2) = pi |
| 1121 |
endif |
| 1122 |
xmin(3) = xout(3)-rad*fac |
| 1123 |
xmax(3) = xout(3)+rad*fac |
| 1124 |
if (itypein==7) then |
| 1125 |
xout(1) = log10(max(r,small_number)) |
| 1126 |
xmin(1) = log10(max(xmin(1),small_number)) |
| 1127 |
xmax(1) = log10(max(xmax(1),small_number)) |
| 1128 |
endif |
| 1129 |
case default |
| 1130 |
xout = xin |
| 1131 |
xmin = xin - rad |
| 1132 |
xmax = xin + rad |
| 1133 |
end select |
| 1134 |
|
| 1135 |
end subroutine get_coord_limits |
| 1136 |
|
| 1137 |
end module geometry |