Debian Codegraph @

src/geomutils.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-2019 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! Utility module containing wrapper routines for coordinate
25 ! transformations
26 !-----------------------------------------------------------------
27 module geomutils
28 implicit none
29
30 public :: change_coords, changecoords, changeveccoords
31 public :: set_coordlabels
32
33 private
34
35 contains
36
37 !-----------------------------------------------------------------
38 ! transform all columns of data for a given particle
39 ! to new coordinate system (does both coordinates
40 ! and components of vectors)
41 ! this version uses DOUBLE PRECISION for vals
42 !-----------------------------------------------------------------
43 subroutine change_coords(vals,ncols,ndim,icoords,icoordsnew,x0,v0)
44 use params, only:doub_prec
45 use geometry, only:coord_transform,vector_transform
46 use labels, only:ix,iamvec,ivx
47 integer, intent(in) :: ncols,ndim,icoords,icoordsnew
48 real(kind=doub_prec), dimension(ncols), intent(inout) :: vals
49 real, dimension(ndim), intent(in) :: x0,v0
50 real, dimension(ndim) :: xcoords,xcoordsnew,vec,vecnew
51 integer :: iamvecprev,icol
52
53 !--perform transformations on coordinates
54 xcoords(1:ndim) = vals(ix(1:ndim)) - x0(1:ndim)
55 call coord_transform(xcoords(1:ndim),ndim,icoords,xcoordsnew(1:ndim),ndim,icoordsnew)
56 vals(ix(1:ndim)) = xcoordsnew(1:ndim)
57
58 !--transform all vector quantities to new coord system
59 iamvecprev = 0
60 do icol=1,ncols - ndim + 1
61 if (iamvec(icol) > 0 .and. iamvec(icol) /= iamvecprev) then
62 iamvecprev = iamvec(icol)
63 if (icol==ivx) then
64 vec(1:ndim) = vals(iamvec(icol):iamvec(icol)+ndim-1) - v0(1:ndim)
65 else
66 vec(1:ndim) = vals(iamvec(icol):iamvec(icol)+ndim-1)
67 endif
68 call vector_transform(xcoords,vec,ndim,icoords,vecnew,ndim,icoordsnew)
69 vals(iamvec(icol):iamvec(icol)+ndim-1) = vecnew(1:ndim)
70 endif
71 enddo
72
73 end subroutine change_coords
74
75 !-------------------------------------------------------------------
76 ! interface to coordinate-system transformations
77 !-------------------------------------------------------------------
78 subroutine changecoords(iplotx,iploty,iplotz,xplot,yplot,zplot,ntot,ndim,itrackpart,dat)
79 use geometry, only:coord_transform,labelcoordsys
80 use settings_data, only:xorigin,icoords,icoordsnew,debugmode
81 use labels, only:is_coord,ix
82 integer, intent(in) :: iplotx,iploty,iplotz,ntot,ndim,itrackpart
83 real, dimension(:), intent(inout) :: xplot,yplot,zplot
84 real, dimension(:,:), intent(in) :: dat
85 real, dimension(ndim) :: xcoords,xcoordsnew
86 integer :: j,ixcoord,iycoord,izcoord
87 logical :: iscoordx,iscoordy,iscoordz
88
89 iscoordx = is_coord(iplotx,ndim)
90 iscoordy = is_coord(iploty,ndim)
91 iscoordz = is_coord(iplotz,ndim) ! should always be true if x and y are coords
92 if (iscoordx .or. iscoordy) then
93 if (debugmode) print*,'changing coords from ',trim(labelcoordsys(icoords)), &
94 ' to ',trim(labelcoordsys(icoordsnew))
95 if (itrackpart > 0) print*,'coords relative to particle ',itrackpart
96
97 !--get offsets in range 1->ndim for the case where particle
98 ! coords are not first in plot arrays
99 ixcoord = iplotx - ix(1) + 1
100 if (iscoordx .and. (ixcoord <= 0 .or. ixcoord > ndim)) then
101 print*,'ERROR in x coordinate offset in arrays: cannot change coordinate system'
102 return
103 endif
104 iycoord = iploty - ix(1) + 1
105 if (iscoordy .and. (iycoord <= 0 .or. iycoord > ndim)) then
106 print*,'ERROR in y coordinate offset in arrays: cannot change coordinate system'
107 return
108 endif
109 izcoord = iplotz - ix(1) + 1
110 if (iscoordz .and. (izcoord <= 0 .or. izcoord > ndim)) then
111 print*,'ERROR in z coordinate offset in arrays: cannot change coordinate system'
112 return
113 endif
114
115 do j=1,ntot
116 if (itrackpart > 0 .and. itrackpart <= ntot) then
117 xcoords(1:ndim) = dat(j,ix(1:ndim)) - dat(itrackpart,ix(1:ndim))
118 else
119 xcoords(1:ndim) = dat(j,ix(1:ndim)) - xorigin(1:ndim)
120 endif
121
122 call coord_transform(xcoords(1:ndim),ndim,icoords, &
123 xcoordsnew(1:ndim),ndim,icoordsnew)
124 if (iscoordx) xplot(j) = xcoordsnew(ixcoord)
125 if (iscoordy) yplot(j) = xcoordsnew(iycoord)
126 if (iscoordz) zplot(j) = xcoordsnew(izcoord)
127 enddo
128 endif
129
130 end subroutine changecoords
131
132 !-------------------------------------------------------------------
133 ! interface to coordinate-system transformations for vectors
134 !-------------------------------------------------------------------
135 subroutine changeveccoords(iplot,xploti,ntot,ndim,itrackpart,dat)
136 use geometry, only:vector_transform,labelcoordsys
137 use settings_data, only:xorigin,icoords,icoordsnew,debugmode
138 use labels, only:ivx,iamvec,ix
139 integer, intent(in) :: iplot,ntot,ndim,itrackpart
140 real, dimension(:), intent(inout) :: xploti
141 real, dimension(ndim) :: xcoords,vecnew,vecin
142 real, dimension(:,:), intent(in) :: dat
143 integer :: j
144
145 if (iamvec(iplot) > 0) then
146 if (iplot-iamvec(iplot)+1 <= ndim) then
147 if (debugmode) print*,'changing vector component from ', &
148 trim(labelcoordsys(icoords)),' to ',trim(labelcoordsys(icoordsnew))
149 if (itrackpart > 0 .and. iamvec(iplot)==ivx) then
150 print*,'velocities relative to particle ',itrackpart
151 endif
152 do j=1,ntot
153 if (itrackpart > 0 .and. itrackpart <= ntot) then
154 xcoords(1:ndim) = dat(j,ix(1:ndim)) - dat(itrackpart,ix(1:ndim))
155 if (iamvec(iplot)==ivx) then
156 vecin(1:ndim) = dat(j,iamvec(iplot):iamvec(iplot)+ndim-1) &
157 - dat(itrackpart,iamvec(iplot):iamvec(iplot)+ndim-1)
158 else
159 vecin(1:ndim) = dat(j,iamvec(iplot):iamvec(iplot)+ndim-1)
160 endif
161 else
162 xcoords(1:ndim) = dat(j,ix(1:ndim)) - xorigin(1:ndim)
163 vecin(1:ndim) = dat(j,iamvec(iplot):iamvec(iplot)+ndim-1)
164 endif
165 call vector_transform(xcoords(1:ndim),vecin(1:ndim), &
166 ndim,icoords,vecnew(1:ndim),ndim,icoordsnew)
167 xploti(j) = vecnew(iplot-iamvec(iplot)+1)
168 enddo
169 else
170 print*,'error: can''t convert vector components with ndimV > ndim'
171 endif
172 endif
173
174 return
175 end subroutine changeveccoords
176
177 !----------------------------------------------------------------
178 !
179 ! routine to set labels for vector quantities and spatial
180 ! coordinates depending on the coordinate system used.
181 !
182 !----------------------------------------------------------------
183 subroutine set_coordlabels(numplot)
184 use geometry, only:labelcoord,coord_is_length
185 use labels, only:label,unitslabel,iamvec,labelvec,ix,labeldefault
186 use settings_data, only:icoords,icoordsnew,ndim,iRescale,debugmode
187 integer, intent(in) :: numplot
188 integer :: i
189 integer, save :: icoordsprev = -1
190 !
191 !--sanity check on icoordsnew...
192 ! (should not be zero)
193 !
194 if (icoordsnew <= 0) then
195 if (icoords > 0) then
196 icoordsnew = icoords
197 else
198 icoordsnew = 1
199 endif
200 endif
201 !
202 !--store the previous value of icoordsnew that was used
203 ! last time we adjusted the labels
204 !
205 if (icoordsprev < 0) icoordsprev = icoordsnew
206 !
207 !--set coordinate and vector labels (depends on coordinate system)
208 !
209 if (icoordsnew /= icoords .or. icoordsnew /= icoordsprev) then
210 !
211 !--here we are using a coordinate system that differs from the original
212 ! one read from the code (must change labels appropriately)
213 !
214 if (debugmode) print*,'DEBUG: changing coordinate labels ...'
215 do i=1,ndim
216 if (ix(i) > 0) then
217 label(ix(i)) = labelcoord(i,icoordsnew)
218 if (iRescale .and. coord_is_length(i,icoordsnew)) then
219 label(ix(i)) = trim(label(ix(i)))//trim(unitslabel(ix(i)))
220 endif
221 endif
222 enddo
223 ! elseif (icoordsnew /= icoordsprev) then
224 !!
225 !!--here we are reverting back to the original coordinate system
226 !! so we have to re-read the original labels from the data read
227 !!
228 ! call get_labels
229 endif
230 !
231 !--set vector labels if iamvec is set and the labels are the default
232 !
233 if (icoordsnew > 0) then
234 do i=1,numplot
235 if (iamvec(i) /= 0 .and. &
236 (icoordsnew /= icoords .or. icoordsnew /= icoordsprev &
237 .or. index(label(i),trim(labeldefault)) /= 0)) then
238 if (i-iamvec(i)+1 > 0) then
239 if (icoordsnew==1) then
240 label(i) = trim(labelvec(iamvec(i)))//'_'//trim(labelcoord(i-iamvec(i)+1,icoordsnew))
241 else
242 label(i) = trim(labelvec(iamvec(i)))//'_{'//trim(labelcoord(i-iamvec(i)+1,icoordsnew))//'}'
243 endif
244 else
245 print "(a,i2,a,i2)",' ERROR with vector labels, referencing '// &
246 trim(labelvec(iamvec(i)))//' in column ',i,' iamvec = ',iamvec(i)
247 endif
248 if (iRescale) then
249 label(i) = trim(label(i))//trim(unitslabel(i))
250 endif
251 endif
252 enddo
253 endif
254 icoordsprev = icoordsnew
255
256 return
257 end subroutine set_coordlabels
258
259 end module geomutils