Debian Codegraph @

src/interpolate3D_proj_geom.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-2018 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !----------------------------------------------------------------------
24 !
25 ! Module containing routines required for 3D projections
26 ! in different coordinate systems
27 !
28 !----------------------------------------------------------------------
29
30 module projections3Dgeom
31 use interpolation, only:iroll
32 use projections3D, only:setup_integratedkernel,wfromtable,have_setup_kernel
33 use kernels, only:radkernel,radkernel2
34 use geometry, only:igeom_cartesian,coord_transform,coord_is_length, &
35 coord_transform_limits,igeom_cylindrical,&
36 get_coord_limits,coord_is_periodic
37 implicit none
38
39 public :: interpolate3D_proj_geom, interpolate3D_xsec_geom
40 ! public :: interpolate3D_proj_geom_vec
41
42 character(len=5), parameter :: str = 'cpu s'
43
44 contains
45
46 !--------------------------------------------------------------------------
47 ! subroutine to interpolate from particle data to even grid of pixels
48 !
49 ! The data is smoothed using the SPH summation interpolant,
50 ! that is, we compute the smoothed array according to
51 !
52 ! datsmooth(pixel) = sum_b weight_b dat_b W(r-r_b, h_b)
53 !
54 ! where _b is the quantity at the neighbouring particle b and
55 ! W is the smoothing kernel, for which we use the usual cubic spline
56 !
57 ! ** This version is for 3D projections in alternative coordinate
58 ! ** systems, e.g. \Int rho d\phi
59 !
60 ! The (dimensionless) weight for each particle should be
61 !
62 ! weight = pmass/(rho*h^3)
63 !
64 ! the interface is written in this form to avoid floating exceptions
65 ! on physically scaled data.
66 !
67 ! Input: particle coordinates : x,y,z (npart)
68 ! smoothing lengths : hh (npart)
69 ! weight for each particle : weight (npart)
70 ! scalar data to smooth : dat (npart)
71 !
72 ! Output: smoothed data : datsmooth (npixx,npixy)
73 !
74 ! Written by Daniel Price July 2011
75 !--------------------------------------------------------------------------
76
77 subroutine interpolate3D_proj_geom(x,y,z,hh,weight,dat,itype,npart, &
78 xmin,ymin,datsmooth,npixx,npixy,pixwidthx,pixwidthy,normalise,igeom,&
79 iplotx,iploty,iplotz,ix,xorigin)
80
81 use timing, only:wall_time,print_time
82 integer, intent(in) :: npart,npixx,npixy
83 real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat
84 integer, intent(in), dimension(npart) :: itype
85 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy
86 real, intent(out), dimension(npixx,npixy) :: datsmooth
87 logical, intent(inout) :: normalise
88 integer, intent(in) :: igeom,iplotx,iploty,iplotz
89 integer, dimension(3), intent(in) :: ix
90 real, dimension(3), intent(in) :: xorigin
91 real, dimension(npixx,npixy) :: datnorm
92 integer :: ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax,ip,jp
93 integer :: ixcoord,iycoord,izcoord,ierr,ncpus
94 integer :: iprintinterval, iprintnext
95 !$ integer :: omp_get_num_threads
96 integer(kind=selected_int_kind(10)) :: iprogress,i ! up to 10 digits
97 real, dimension(3) :: xcoord, xpix
98 real, dimension(3), save :: xci, xi
99 !$omp threadprivate(xci,xi)
100 real :: hi,hi1,hi21,radkern,wab,q2,xminpix,yminpix
101 real :: term,termnorm,dx,dx2,dy,dy2,dz
102 real :: xmax,ymax,hmin,horigi
103 real :: t_start,t_end,t_used
104 logical :: iprintprogress,islengthx,islengthy,islengthz
105 character(len=64) :: string
106
107 datsmooth = 0.
108 datnorm = 0.
109 term = 0.
110 string = 'projecting'
111 if (normalise) then
112 string = trim(string)//' (normalised, non-cartesian)'
113 else
114 string = trim(string)//' (non-cartesian)'
115 endif
116 ncpus = 0
117 !$omp parallel
118 !$omp master
119 !$ ncpus = omp_get_num_threads()
120 !$omp end master
121 !$omp end parallel
122
123 if (ncpus > 0) then
124 write (*,"(1x,a,': ',i4,' x ',i4,' on ',i3,' cpus')") trim(string),npixx,npixy,ncpus
125 else
126 write (*,"(1x,a,': ',i4,' x ',i4)") trim(string),npixx,npixy
127 endif
128 if (pixwidthx <= 0. .or. pixwidthy <= 0) then
129 print "(1x,a)",'interpolate3D_proj_geom: error: pixel width <= 0'
130 return
131 endif
132 if (any(hh(1:npart) < 0.)) then
133 print*,'interpolate3D_proj_geom: warning: ignoring some or all particles with h < 0'
134 endif
135
136 !
137 !--get information about the coordinates
138 !
139 call get_coord_info(iplotx,iploty,iplotz,ix(1),igeom,ixcoord,iycoord,izcoord,&
140 islengthx,islengthy,islengthz,ierr)
141 if (ierr /= 0) return
142 !
143 !--if z coordinate is not a length, use normalised interpolation
144 ! (e.g. azimuthally averaged density)
145 !
146 !if (.not.islengthz) normalise = .true.
147 !
148 !--check column density table has actually been setup
149 !
150 if (.not.have_setup_kernel) call setup_integratedkernel
151 !
152 !--print a progress report if it is going to take a long time
153 ! (a "long time" is, however, somewhat system dependent)
154 !
155 iprintprogress = (npart >= 100000) .or. (npixx*npixy > 100000)
156 !$ iprintprogress = .false.
157 !
158 !--loop over particles
159 !
160 iprintinterval = 25
161 if (npart >= 1e6) iprintinterval = 10
162 iprintnext = iprintinterval
163 !
164 !--get starting CPU time
165 !
166 call wall_time(t_start)
167
168 xminpix = xmin - 0.5*pixwidthx
169 yminpix = ymin - 0.5*pixwidthy
170 xmax = xmin + npixx*pixwidthx
171 ymax = ymin + npixy*pixwidthy
172 !
173 !--use a minimum smoothing length on the grid to make
174 ! sure that particles contribute to at least one pixel
175 !
176 hmin = 0.
177 if (islengthx) hmin = 0.5*pixwidthx
178 if (islengthy) hmin = max(hmin,0.5*pixwidthy)
179
180 !$omp parallel default(none) &
181 !$omp shared(hh,z,x,y,weight,dat,itype,datsmooth,npart,hmin) &
182 !$omp shared(xmin,ymin,xmax,ymax,xminpix,yminpix,pixwidthx,pixwidthy) &
183 !$omp shared(npixx,npixy,ixcoord,iycoord,izcoord,islengthx,islengthy,islengthz,igeom) &
184 !$omp shared(datnorm,normalise,radkernel,radkernel2,xorigin) &
185 !$omp shared(iprintprogress,iprintinterval) &
186 !$omp private(hi,horigi,radkern) &
187 !$omp private(hi1,hi21,term,termnorm) &
188 !$omp private(iprogress) &
189 !$omp firstprivate(iprintnext) &
190 !$omp private(q2,dx,dx2,dy,dy2,dz,wab,xcoord,xpix) &
191 !$omp private(i,ipix,jpix,ip,jp,ipixmin,ipixmax,jpixmin,jpixmax,ierr)
192 !$omp do schedule (guided, 2)
193 over_particles: do i=1,npart
194 !
195 !--report on progress
196 !
197 if (iprintprogress) then
198 iprogress = 100*i/npart
199 if (iprogress >= iprintnext) then
200 write(*,"('(',i3,'% -',i12,' particles done)')") iprogress,i
201 iprintnext = iprintnext + iprintinterval
202 endif
203 endif
204 !
205 !--skip particles with itype < 0
206 !
207 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_particles
208 !
209 !--set h related quantities
210 !
211 horigi = hh(i)
212 if (horigi <= 0.) cycle over_particles
213 hi = max(horigi,hmin)
214
215 radkern = radkernel*hi ! radius of the smoothing kernel
216
217 !
218 !--get limits of contribution from particle in cartesian space
219 !
220 xci(1) = x(i) + xorigin(1) ! xci = position in cartesians
221 xci(2) = y(i) + xorigin(2)
222 xci(3) = z(i) + xorigin(3)
223 call get_pixel_limits(xci,xi,radkern,ipixmin,ipixmax,jpixmin,jpixmax,igeom,&
224 npixx,npixy,pixwidthx,pixwidthy,xmin,ymin,ixcoord,iycoord,ierr)
225 if (ierr /= 0) cycle over_particles
226 !
227 !--set kernel related quantities
228 !
229 hi1 = 1./hi
230 hi21 = hi1*hi1
231
232 ! h gives the z length scale (NB: no perspective)
233 if (islengthz) then
234 termnorm = weight(i)*horigi
235 elseif (igeom==igeom_cylindrical) then
236 termnorm = weight(i) !*atan(radkern/xi(ixcoord))/pi !*horigi/xi(ixcoord)
237 else
238 termnorm = weight(i)
239 endif
240 term = termnorm*dat(i)
241
242 !
243 !--loop over pixels, adding the contribution from this particle
244 !
245 if (islengthz) then
246 xcoord(izcoord) = xi(izcoord) ! assume all pixels at same r as particlefor theta-phi
247 else
248 xcoord(izcoord) = 0. ! use phi=0 so get x = r cos(phi) = r
249 endif
250 do jpix = jpixmin,jpixmax
251 jp = iroll(jpix,npixy)
252 xcoord(iycoord) = yminpix + jp*pixwidthy
253
254 do ipix = ipixmin,ipixmax
255 ip = iroll(ipix,npixx)
256 xcoord(ixcoord) = xminpix + ip*pixwidthx
257
258 !--now transform to get location of pixel in cartesians
259 call coord_transform(xcoord,3,igeom,xpix,3,igeom_cartesian)
260
261 !--find distances using cartesians and perform interpolation
262 dy = xpix(iycoord) - xci(iycoord)
263 dx = xpix(ixcoord) - xci(ixcoord)
264 dz = xpix(izcoord) - xci(izcoord) ! z dir important if surface not flat (e.g. r slice)
265
266 dx2 = dx*dx
267 dy2 = dy*dy
268 q2 = (dx2 + dy2 +dz*dz)*hi21
269
270 !
271 !--SPH kernel - integral through cubic spline
272 ! interpolate from a pre-calculated table
273 !
274 if (q2 < radkernel2) then
275 wab = wfromtable(q2)
276 !
277 !--calculate data value at this pixel using the summation interpolant
278 !
279 !$omp atomic
280 datsmooth(ip,jp) = datsmooth(ip,jp) + term*wab
281
282 if (normalise) then
283 !$omp atomic
284 datnorm(ip,jp) = datnorm(ip,jp) + termnorm*wab
285 endif
286 endif
287 !endif
288 enddo
289 enddo
290
291 enddo over_particles
292 !$omp enddo
293 !$omp end parallel
294 !
295 !--normalise dat array
296 !
297 if (normalise) then
298 !--normalise everywhere (required if not using SPH weighting)
299 where (datnorm > tiny(datnorm))
300 datsmooth = datsmooth/datnorm
301 end where
302 endif
303 !
304 !--get/print timings
305 !
306 call wall_time(t_end)
307 t_used = t_end - t_start
308 if (t_used > 5.) call print_time(t_used)
309
310 end subroutine interpolate3D_proj_geom
311
312 !--------------------------------------------------------------------------
313 !
314 ! ** In this version 3D data is interpolated to a single 2D cross section
315 !
316 ! ** Note that the cross section is always taken in the z co-ordinate
317 ! ** so should submit the appropriate arrays as x, y and z.
318 !
319 ! Input: particle coordinates : x1,x2,x3 (npart)
320 ! particle masses : pmass (npart)
321 ! density on particles : rho (npart) - must be computed separately
322 ! smoothing lengths : hh (npart) - could be computed from density
323 ! scalar data to smooth : dat (npart)
324 ! cross section location: zslice
325 !
326 ! Output: smoothed data : datsmooth (npixx,npixy)
327 !
328 ! Daniel Price, Monash University 2nd May 2017
329 !--------------------------------------------------------------------------
330
331 subroutine interpolate3D_xsec_geom(x,y,z,hh,weight,dat,itype,npart,&
332 xmin,ymin,zslice,datsmooth,npixx,npixy,pixwidthx,pixwidthy,normalise,igeom, &
333 iplotx,iploty,iplotz,ix,xorigin)
334
335 use kernels, only:cnormk3D,wfunc
336 integer, intent(in) :: npart,npixx,npixy
337 real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat
338 integer, intent(in), dimension(npart) :: itype
339 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy,zslice
340 real, intent(out), dimension(npixx,npixy) :: datsmooth
341 logical, intent(in) :: normalise
342 integer, intent(in) :: igeom,iplotx,iploty,iplotz
343 integer, dimension(3), intent(in) :: ix
344 real, dimension(3), intent(in) :: xorigin
345 real, dimension(npixx,npixy) :: datnorm
346
347 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax,ip,jp
348 integer :: ixcoord,iycoord,izcoord,ierr
349 real :: hi,hi1,radkern,q2,wab,const,hi21
350 real :: termnorm,term,dx,dx2,dy,dy2,dz,dz2
351 real :: xmax,ymax,xminpix,yminpix
352 real, dimension(3) :: xcoord,xpix,xci,xi
353 logical :: islengthx,islengthy,islengthz
354
355 datsmooth = 0.
356 datnorm = 0.
357 if (normalise) then
358 print*,'taking fast cross section (normalised, non-cartesian)...',zslice
359 else
360 print*,'taking fast cross section (non-cartesian)...',zslice
361 endif
362 if (pixwidthx <= 0. .or. pixwidthy <= 0.) then
363 print*,'interpolate3D_xsec: error: pixel width <= 0'
364 return
365 elseif (npart <= 0) then
366 print*,'interpolate3D_xsec: error: npart = 0'
367 return
368 endif
369 if (any(hh(1:npart) < 0.)) then
370 print*,'interpolate3D_xsec_geom: WARNING: ignoring some or all particles with h < 0'
371 endif
372 const = cnormk3D
373
374 !
375 !--get information about the coordinates
376 !
377 call get_coord_info(iplotx,iploty,iplotz,ix(1),igeom,ixcoord,iycoord,izcoord,&
378 islengthx,islengthy,islengthz,ierr)
379 if (ierr /= 0) return
380 !
381 !--if z coordinate is not a length, quit
382 !
383 if (.not.islengthz) then
384 print*,'interpolate3D_xsec_geom: ERROR xsec not implemented when z is an angle'
385 return
386 endif
387
388 xminpix = xmin - 0.5*pixwidthx
389 yminpix = ymin - 0.5*pixwidthy
390 xmax = xmin + npixx*pixwidthx
391 ymax = ymin + npixy*pixwidthy
392 !
393 !--loop over particles
394 !
395 over_parts: do i=1,npart
396 !
397 !--skip particles with itype < 0
398 !
399 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts
400
401 !
402 !--set h related quantities
403 !
404 hi = hh(i)
405 if (hi <= 0.) cycle over_parts
406 !horigi = hh(i)
407 !if (horigi <= 0.) cycle over_parts
408 !hi = max(horigi,hmin)
409
410 radkern = radkernel*hi ! radius of the smoothing kernel
411 !
412 !--set kernel related quantities
413 !
414 hi1 = 1./hi
415 hi21 = hi1*hi1
416 !
417 !--for each particle, work out distance from the cross section slice.
418 !
419 dz = zslice - z(i)
420 dz2 = dz**2
421 xcoord(izcoord) = 1.
422 !
423 !--if this is < 2h then add the particle's contribution to the pixels
424 ! otherwise skip all this and start on the next particle
425 !
426 if (dz2 < radkernel2) then
427 !
428 !--get limits of contribution from particle in cartesian space
429 !
430 xci(1) = x(i) + xorigin(1)
431 xci(2) = y(i) + xorigin(2)
432 xci(3) = z(i) + xorigin(3)
433 call get_pixel_limits(xci,xi,radkern,ipixmin,ipixmax,jpixmin,jpixmax,igeom,&
434 npixx,npixy,pixwidthx,pixwidthy,xmin,ymin,ixcoord,iycoord,ierr)
435 if (ierr /= 0) cycle over_parts
436
437 termnorm = const*weight(i)
438 term = termnorm*dat(i) !/rescalefac
439 !
440 !--loop over pixels, adding the contribution from this particle
441 !
442 do jpix = jpixmin,jpixmax
443 jp = iroll(jpix,npixy)
444 xcoord(iycoord) = yminpix + jp*pixwidthy
445
446 do ipix = ipixmin,ipixmax
447 ip = iroll(ipix,npixx)
448 xcoord(ixcoord) = xminpix + ip*pixwidthx
449
450 !--now transform to get location of pixel in cartesians
451 call coord_transform(xcoord,3,igeom,xpix,3,igeom_cartesian)
452
453 !--find distances using cartesians and perform interpolation
454 dy = xpix(iycoord) - xci(iycoord)
455 dx = xpix(ixcoord) - xci(ixcoord)
456
457 dx2 = dx*dx
458 dy2 = dy*dy
459 q2 = (dx*dx + dy*dy + dz2)*hi1*hi1
460 !
461 !--SPH kernel - integral through cubic spline
462 ! interpolate from a pre-calculated table
463 !
464 if (q2 < radkernel2) then
465 wab = wfunc(q2)
466 !
467 !--calculate data value at this pixel using the summation interpolant
468 !
469 datsmooth(ip,jp) = datsmooth(ip,jp) + term*wab
470
471 if (normalise) then
472 datnorm(ip,jp) = datnorm(ip,jp) + termnorm*wab
473 endif
474 endif
475 enddo
476 enddo
477 endif ! if particle within 2h of slice
478 enddo over_parts ! over particles
479 !
480 !--normalise dat array
481 !
482 if (normalise) then
483 !--normalise everywhere (required if not using SPH weighting)
484 where (datnorm > tiny(datnorm))
485 datsmooth = datsmooth/datnorm
486 end where
487 endif
488 !datsmooth = datsmooth*rescalefac
489
490 end subroutine interpolate3D_xsec_geom
491
492 !--------------------------------------------------------------------------
493 !
494 ! utility routine for use in above routines
495 !
496 !--------------------------------------------------------------------------
497 subroutine get_coord_info(iplotx,iploty,iplotz,ix1,igeom,ixcoord,iycoord,izcoord,&
498 islengthx,islengthy,islengthz,ierr)
499 integer, intent(in) :: iplotx,iploty,iplotz,ix1,igeom
500 integer, intent(out) :: ixcoord,iycoord,izcoord,ierr
501 logical, intent(out) :: islengthx,islengthy,islengthz
502
503 ierr = 0
504 !
505 !--get plotted coordinates in range 1->ndim
506 !
507 ixcoord = iplotx - ix1 + 1
508 iycoord = iploty - ix1 + 1
509 izcoord = iplotz - ix1 + 1
510 if (ixcoord <= 0 .or. ixcoord > 3) then
511 print*,' ERROR finding x coordinate offset, cannot render'
512 ierr = 1
513 endif
514 if (iycoord <= 0 .or. iycoord > 3) then
515 print*,' ERROR finding y coordinate offset, cannot render'
516 ierr = 2
517 endif
518 if (izcoord <= 0 .or. izcoord > 3) then
519 print*,' ERROR finding y coordinate offset, cannot render'
520 ierr = 3
521 endif
522 !
523 !--check if coordinate is a length (i.e., not an angle)
524 !
525 islengthx = coord_is_length(ixcoord,igeom)
526 islengthy = coord_is_length(iycoord,igeom)
527 islengthz = coord_is_length(izcoord,igeom)
528
529 end subroutine get_coord_info
530
531 !--------------------------------------------------------------------------
532 !
533 ! utility routine for use in above routines
534 ! IN:
535 ! xci - coordinates of particle in transformed space (e.g. r, phi, z)
536 ! radkern - radius of kernel (e.g. 2h)
537 ! OUT:
538 ! xi - cartesian coordinates of particle
539 ! ipixmin,ipixmax,jpixmin,jpixmax - pixel limits
540 !
541 !--------------------------------------------------------------------------
542 subroutine get_pixel_limits(xci,xi,radkern,ipixmin,ipixmax,jpixmin,jpixmax,igeom,&
543 npixx,npixy,pixwidthx,pixwidthy,xmin,ymin,ixcoord,iycoord,ierr)
544 real, intent(in) :: xci(3),radkern,pixwidthx,pixwidthy,xmin,ymin
545 real, intent(out) :: xi(3)
546 integer, intent(out) :: ipixmin,ipixmax,jpixmin,jpixmax,ierr
547 integer, intent(in) :: igeom,npixx,npixy,ixcoord,iycoord
548 real :: xpixmin(3),xpixmax(3)
549
550 ierr = 0
551 !
552 !--get limits of rendering in new coordinate system
553 !
554 call get_coord_limits(radkern,xci,xi,xpixmin,xpixmax,igeom)
555 !
556 !--now work out contributions to pixels in the the transformed space
557 !
558 ipixmax = int((xpixmax(ixcoord) - xmin)/pixwidthx)+1
559 if (ipixmax < 1) ierr = 1
560 jpixmax = int((xpixmax(iycoord) - ymin)/pixwidthy)+1
561 if (jpixmax < 1) ierr = 2
562
563 ipixmin = int((xpixmin(ixcoord) - xmin)/pixwidthx)
564 if (ipixmin > npixx) ierr = 3
565 jpixmin = int((xpixmin(iycoord) - ymin)/pixwidthy)
566 if (jpixmin > npixy) ierr = 4
567
568 if (.not.coord_is_periodic(ixcoord,igeom)) then
569 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
570 if (ipixmax > npixx) ipixmax = npixx ! to pixels in the image
571 endif
572 if (.not.coord_is_periodic(iycoord,igeom)) then
573 if (jpixmin < 1) jpixmin = 1 ! (note that this optimises
574 if (jpixmax > npixy) jpixmax = npixy ! much better than using min/max)
575 endif
576
577 end subroutine get_pixel_limits
578
579 end module projections3Dgeom