Debian Codegraph @

src/rotate.f90

raw
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-2013 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !
24 ! This module contains all the routines for rotating the particles
25 ! and plotting the rotated axes
26 !
27 module rotation
28 implicit none
29
30 public :: rotate2D,rotate3D,rotate_particles
31 !
32 !--2D rotation (about z axis)
33 !
34 contains
35
36 subroutine rotate2D(xcoords,anglez)
37 real, intent(inout) :: xcoords(2)
38 real, intent(in) :: anglez
39 real :: x, y, r, phi
40
41 x = xcoords(1)
42 y = xcoords(2)
43 !
44 !--rotate about z
45 !
46 r = sqrt(x**2 + y**2)
47 phi = atan2(y,x)
48 phi = phi - anglez
49 x = r*cos(phi)
50 y = r*sin(phi)
51
52 xcoords(1) = x
53 xcoords(2) = y
54
55 return
56 end subroutine rotate2D
57
58 !
59 !--3D rotation (about x, y and z axes)
60 ! This is done in the order z-y-x
61 !
62 subroutine rotate3D(xcoords,anglex,angley,anglez,zobs,dz1)
63 real, intent(inout) :: xcoords(3)
64 real, intent(in) :: anglex, angley, anglez, zobs, dz1
65 real :: x, y, z, r, phi, zfrac
66
67 x = xcoords(1)
68 y = xcoords(2)
69 z = xcoords(3)
70 !
71 !--rotate about z
72 !
73 if (abs(anglez) > tiny(anglez)) then
74 r = sqrt(x**2 + y**2)
75 phi = atan2(y,x)
76 phi = phi - anglez
77 x = r*cos(phi)
78 y = r*sin(phi)
79 endif
80 !
81 !--rotate about y
82 !
83 if (abs(angley) > tiny(angley)) then
84 r = sqrt(z**2 + x**2)
85 phi = atan2(z,x)
86 phi = phi - angley
87 z = r*sin(phi)
88 x = r*cos(phi)
89 endif
90 !
91 !--rotate about x
92 !
93 if (abs(anglex) > tiny(anglex)) then
94 r = sqrt(y**2 + z**2)
95 phi = atan2(z,y)
96 phi = phi - anglex
97 y = r*cos(phi)
98 z = r*sin(phi)
99 endif
100 !
101 !--change perspective according to z depth
102 ! (for straight rotation == parallel projections use dz1= 0 on input)
103 ! zobs is the z position of the observer.
104 !
105 if (abs(dz1) > tiny(dz1)) then
106 zfrac = abs(dz1/(z-zobs))
107 else
108 zfrac = 1.0
109 endif
110 xcoords(1) = x*zfrac
111 xcoords(2) = y*zfrac
112 xcoords(3) = z
113
114 return
115 end subroutine rotate3D
116
117 !
118 !--plots rotated plot axes
119 !
120 subroutine rotate_axes2D(ioption,xmin,xmax,xorigin,anglez)
121 use plotlib, only:plot_arro,plot_sfs,plot_poly
122 integer, intent(in) :: ioption
123 real, intent(in), dimension(2) :: xmin,xmax,xorigin
124 real, intent(in) :: anglez
125 integer :: i,idim
126 real, dimension(2) :: xpttemp
127 real, dimension(2,4) :: xpt
128
129 !
130 ! plot various options for the 3D axes
131 !
132 select case(ioption)
133 case(1)
134 !--rotated axes
135 print*,'plotting rotated (2D) axes...'
136
137 do idim=1,2
138 !--plot to max of each axis
139 xpt(:,2) = 0.
140 xpt(idim,2) = xmax(idim)
141 do i=1,2
142 xpttemp(:) = xpt(:,i) - xorigin(:)
143 call rotate2D(xpttemp(:),anglez)
144 xpt(:,i) = xpttemp(:) + xorigin(:)
145 enddo
146 !--plot each axis as an arrow
147 call plot_arro(xpt(1,1),xpt(2,1),xpt(1,2),xpt(2,2))
148 enddo
149
150 case default
151
152 print*,'plotting rotated (2D) box...'
153
154 !--front face (pts 1->4)
155 xpt(:,1) = xmin(:) ! xmin, ymin
156
157 xpt(1,2) = xmin(1) ! xmin
158 xpt(2,2) = xmax(2) ! ymax
159
160 xpt(1,3) = xmax(1) ! xmax
161 xpt(2,3) = xmax(2) ! ymax
162
163 xpt(1,4) = xmax(1) ! xmax
164 xpt(2,4) = xmin(2) ! ymin
165 !
166 !--now rotate each of these coordinates
167 !
168 do i=1,4
169 xpttemp(:) = xpt(:,i) - xorigin(:)
170 call rotate2D(xpttemp(:),anglez)
171 xpt(:,i) = xpttemp(:) + xorigin(:)
172 enddo
173 !
174 !--now plot box appropriately using points
175 !
176 call plot_sfs(2)
177 call plot_poly(4,xpt(1,1:4),xpt(2,1:4))
178 end select
179
180 return
181 end subroutine rotate_axes2D
182
183 subroutine rotate_axes3D(ioption,iplotx,iploty,xmin,xmax,xorigin, &
184 anglex,angley,anglez,zobs,dz1)
185 use plotlib, only:plot_poly,plot_sfs,plot_arro,plot_line
186 integer, intent(in) :: ioption,iplotx,iploty
187 real, intent(in), dimension(3) :: xmin,xmax,xorigin
188 real, intent(in) :: anglex, angley, anglez, zobs, dz1
189 integer :: i,idim,iline
190 integer, parameter :: nlines = 10
191 real, dimension(3,8) :: xpt
192 real, dimension(3) :: xpttemp
193 real, dimension(2) :: xline,yline
194 real :: dx
195
196 !
197 ! plot various options for the 3D axes
198 !
199 select case(ioption)
200 case(1)
201 !--rotated axes
202 print*,'plotting rotated 3D axes...'
203 xpt = 0.
204 !--origin
205 xpt(1:3,1) = 0.
206
207 do idim=1,3
208 !--plot to max of each axis
209 xpt(:,2) = 0.
210 xpt(idim,2) = xmax(idim)
211 do i=1,2
212 xpttemp(:) = xpt(:,i) - xorigin(:)
213 call rotate3D(xpttemp(:),anglex,angley,anglez,zobs,dz1)
214 xpt(:,i) = xpttemp(:) + xorigin(:)
215 enddo
216 !--plot each axis as an arrow
217 call plot_arro(xpt(iplotx,1),xpt(iploty,1),xpt(iplotx,2),xpt(iploty,2))
218 !! call pgline(2,xpt(iplotx,1:2),xpt(iploty,1:2))
219 enddo
220 case(2)
221 !--rotated box
222 print*,'plotting rotated 3D box...',iplotx,iploty
223
224 !--front face (pts 1->4)
225 xpt(:,1) = xmin(:) ! xmin, ymin
226
227 xpt(1,2) = xmin(1) ! xmin
228 xpt(2,2) = xmax(2) ! ymax
229
230 xpt(1,3) = xmax(1) ! xmax
231 xpt(2,3) = xmax(2) ! ymax
232
233 xpt(1,4) = xmax(1) ! xmax
234 xpt(2,4) = xmin(2) ! ymin
235
236 xpt(3,1:4) = xmin(3) ! zmin
237
238 !--back face (pts 5->8)
239 do i=1,4
240 xpt(1:2,i+4) = xpt(1:2,i)
241 enddo
242 xpt(3,5:8) = xmax(3)
243 !
244 !--now rotate each of these coordinates
245 !
246 do i=1,8
247 xpttemp(:) = xpt(:,i) - xorigin(:)
248 call rotate3D(xpttemp(:),anglex,angley,anglez,zobs,dz1)
249 xpt(:,i) = xpttemp(:) + xorigin(:)
250 enddo
251 !
252 !--now draw lines appropriately through points
253 !
254 call plot_sfs(2)
255 !--front face
256 call plot_poly(4,xpt(iplotx,1:4),xpt(iploty,1:4))
257 !--back face
258 call plot_poly(4,xpt(iplotx,5:8),xpt(iploty,5:8))
259 !--connecting lines ( 1->5, 2->6, 3->7, 4->8 )
260 do i=1,4
261 xline(1) = xpt(iplotx,i)
262 yline(1) = xpt(iploty,i)
263 xline(2) = xpt(iplotx,i+4)
264 yline(2) = xpt(iploty,i+4)
265 call plot_line(2,xline,yline)
266 enddo
267
268 case(3)
269 !--gridded x-y plane
270 print*,'plotting rotated x-y plane...'
271
272 !--lines of constant x
273
274 dx = (xmax(1) - xmin(1))/real(nlines-1)
275 do iline=1,nlines
276 !--all pts at z = 0
277 xpt(3,:) = 0.
278
279 !--start from xmin, plot line from ymin to ymax
280 xpt(1,1:2) = xmin(1) + (iline-1)*dx
281 xpt(2,1) = xmin(2)
282 xpt(2,2) = xmax(2)
283 do i=1,2
284 xpttemp(:) = xpt(:,i) - xorigin(:)
285 call rotate3D(xpttemp(:),anglex,angley,anglez,zobs,dz1)
286 xpt(:,i) = xpttemp(:) + xorigin(:)
287 enddo
288 call plot_line(2,xpt(iplotx,1:2),xpt(iploty,1:2))
289 enddo
290
291 !--lines of constant y
292 dx = (xmax(2) - xmin(2))/real(nlines-1)
293 do iline=1,nlines
294 !--all pts at z = 0
295 xpt(3,:) = 0.
296
297 !--start from ymin, plot line from xmin to xmax
298 xpt(2,1:2) = xmin(2) + (iline-1)*dx
299 xpt(1,1) = xmin(1)
300 xpt(1,2) = xmax(1)
301 do i=1,2
302 xpttemp(:) = xpt(:,i) - xorigin(:)
303 call rotate3D(xpttemp(:),anglex,angley,anglez,zobs,dz1)
304 xpt(:,i) = xpttemp(:) + xorigin(:)
305 enddo
306 call plot_line(2,xpt(iplotx,1:2),xpt(iploty,1:2))
307 enddo
308 case default
309 !--do nothing
310 end select
311
312 end subroutine rotate_axes3D
313
314 subroutine rotate_particles(n,x,y,z,anglex,angley,anglez,v)
315 integer, intent(in) :: n
316 real, intent(inout) :: x(n),y(n),z(n)
317 real, intent(in) :: anglex,angley,anglez
318 real, intent(inout), optional :: v(3,n)
319 real :: ax,ay,az,xi(3)
320 real, parameter :: pi = 4.*atan(1.)
321 integer :: i
322
323 if (abs(anglez)>0. .or. abs(angley)>0. .or. abs(anglex)>0.) then
324 print*, 'Rotating particles around (z,y,x) by',anglez,angley,anglex
325 ax = anglex*pi/180.0 ! convert degrees to radians to pass into rotate
326 ay = angley*pi/180.0
327 az = anglez*pi/180.0
328 do i=1,n
329 xi = (/x(i),y(i),z(i)/)
330 call rotate3D(xi,ax,ay,az,0.,0.)
331 x(i) = xi(1)
332 y(i) = xi(2)
333 z(i) = xi(3)
334 if (present(v)) call rotate3D(v(:,i),ax,ay,az,0.,0.)
335 enddo
336 endif
337
338 end subroutine rotate_particles
339
340 end module rotation