Debian Codegraph @

src/interpolate3D_opacity.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-2024 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 module interpolate3D_opacity
24 use projections3D, only:wfromtable,coltable,setup_integratedkernel,have_setup_kernel
25 use kernels, only:radkernel,radkernel2,cnormk3D,wallint
26 use sort, only:indexx
27 use interpolation, only:weight_sink
28 use timing, only:print_time,wall_time
29 implicit none
30
31 contains
32 !--------------------------------------------------------------------------
33 ! $Id: interpolate3D_opacity.f90,v 1.16 2007/11/20 17:05:35 dprice Exp $
34 !
35 ! subroutine to do a ray trace through the particle data
36 !
37 ! we use the radiation transport equation along a ray, that is
38 ! the change in intensity from one side of a particle to the other is
39 ! given by:
40 !
41 ! I_nu = I_nu(0) exp(-tau_i) + S_nu (1 - exp(-tau_i))
42 !
43 ! where tau_i is the integrated optical depth through the particle,
44 ! and S_nu is the colour calculated from a colour table for the rendered data.
45 ! We calculate an intensity in red, green and blue for colour plots.
46 !
47 ! tau_i = kappa \int rho dz
48 !
49 ! this is calculated using the SPH kernel for rho, so for each pixel
50 ! the optical depth is incremented as the sum
51 !
52 ! tau_i = kappa \sum_j m_j \int W dz
53 !
54 ! where \int W dz is the SPH kernel integrated along one spatial dimension.
55 ! This is interpolated from a pre-calculated table (see module projections3D for this).
56 !
57 ! kappa is the monochromatic mass extinction coefficient
58 ! (particle cross section per unit mass) and is a constant for all particles
59 ! which must be given as input (although see below for calculations of a
60 ! meaningful values for kappa in terms of "surface depth in units of smoothing lengths")
61 !
62 ! Input: particle coordinates : x,y,z (npart) - note that z is only required for perspective
63 ! particle masses : pmass (npmass)
64 ! smoothing lengths : hh (npart)
65 ! weight : m/(h^3 rho) (not used, but skips particles with w <= 0)
66 ! scalar data to smooth : dat (npart)
67 !
68 ! Particle masses can be sent in as either a single scalar (npmass = 1)
69 ! or as an array of length npart (npmass=npart)
70 !
71 ! Settings: zobs, dz1 : settings for 3D projection
72 ! rkappa : particle cross section per unit mass
73 !
74 ! Output: smoothed data : datsmooth (npixx,npixy)
75 ! optical depth on each pixel : tausmooth (npixx,npixy)
76 !
77 !--------------------------------------------------------------------------
78
79 subroutine interp3D_proj_opacity(x,y,z,pmass,npmass,hh,weight,dat,zorig,itype,npart, &
80 xmin,ymin,datsmooth,tausmooth,npixx,npixy,pixwidthx,pixwidthy,zobserver,dscreenfromobserver, &
81 rkappa,zcut,iverbose,exact_rendering,datv,datvpix,badpix)
82
83 real, parameter :: pi=4.*atan(1.)
84 integer, intent(in) :: npart,npixx,npixy,npmass,iverbose
85 real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat,zorig,rkappa
86 real, intent(in), dimension(npmass) :: pmass
87 integer, intent(in), dimension(npart) :: itype
88 logical, intent(in) :: exact_rendering
89 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy,zobserver,dscreenfromobserver,zcut
90 real, dimension(npixx,npixy), intent(out) :: datsmooth,tausmooth
91 ! optional arguments for vector opacity rendering
92 real, dimension(:,:), intent(in), optional :: datv
93 real, dimension(:,:,:), intent(out), optional :: datvpix
94 ! optional argument for checking unresolved pixels in the photosphere
95 real, dimension(npixx,npixy), intent(out), optional :: badpix
96
97 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax,nused,nsink
98 integer, dimension(npart) :: iorder
99 integer(kind=selected_int_kind(12)) :: ipart
100 integer :: nsubgrid,nok
101 real :: hi,hi1,hi21,radkern,q2,wab,pmassav
102
103 !real, dimension(npixx,npixy) :: datnorm
104 real :: hsmooth,dati
105 real :: term,dy,dy2,ypix,zfrac,hav,zcutoff
106 real :: xpixmin,xpixmax,xmax,ypixmin,ypixmax,ymax
107 real :: hmin,hminall,dfac,pixwidthz,pixint,zi,xpixi,zpix
108 real :: fopacity,tau,rkappatemp,xi,yi
109 real :: t_start,t_end,t_used
110 logical :: adjustzperspective,rendersink
111 real, dimension(npixx) :: xpix,dx2i
112 real, allocatable :: datvi(:)
113 real :: xminpix,yminpix
114 character(len=10) :: str
115 logical :: backwards
116
117 datsmooth = 0.
118 term = 0.
119 tausmooth = 0.
120 if (present(datvpix)) datvpix = 0.
121 if (present(badpix)) badpix = 0.
122 if (pixwidthx <= 0. .or. pixwidthy <= 0) then
123 if (iverbose >= -1) print "(1x,a)",'ERROR: pixel width <= 0'
124 return
125 endif
126 if (any(hh(1:npart) <= tiny(0.))) then
127 if (iverbose > 1) print*,'interpolate3D_opacity: warning: ignoring some or all particles with h < 0'
128 endif
129 !--check that npmass is sensible
130 if (npmass < 1 .or. npmass > npart) then
131 if (iverbose >= -1) print*,'interpolate3D_opacity: ERROR in input number of particle masses '
132 return
133 endif
134 !--these values for npmass are not sensible but the routine will still work
135 if (npmass /= 1 .and. npmass /= npart) then
136 if (iverbose >= -1) print*,'WARNING: interpolate3D_opacity: number of particle masses input =',npmass
137 endif
138
139 if (abs(dscreenfromobserver) > tiny(dscreenfromobserver)) then
140 adjustzperspective = .true.
141 zcutoff = zobserver
142 else
143 adjustzperspective = .false.
144 zcutoff = huge(zobserver)
145 endif
146 !
147 !--whether to raytrace backwards from observer, or forwards to observer
148 !
149 backwards = .false.
150 if (present(badpix)) backwards = .true.
151
152 !
153 !--setup kernel table if not already set
154 !
155 if (.not.have_setup_kernel) call setup_integratedkernel
156
157 !
158 !--kappa is the opacity in units of length^2/mass
159 ! sent as an input parameter as it should be kept constant throughout the simulation
160 !
161 ! However we compute a reasonable estimate below based on the current plot so that
162 ! we can give the "actual" optical depth for the current frame in terms of number of
163 ! smoothing lengths. This is purely for diagnostic purposes only.
164 !
165 !--calculate average h
166 hav = sum(hh(1:npart))/real(npart)
167 !--average particle mass
168 pmassav = sum(pmass(1:npmass))/real(npmass)
169 rkappatemp = pi*hav*hav/(pmassav*coltable(0))
170
171 str = ''
172 if (exact_rendering) str = '(exact)'
173 if (iverbose >= 0) print "(1x,a,g9.2,a)",'ray tracing '//trim(str)//&
174 ': surface depth ~ ',rkappatemp/maxval(rkappa),' smoothing lengths'
175 !
176 !--get starting time
177 !
178 call wall_time(t_start)
179 !
180 !--first sort the particles in z so that we do the opacity in the correct order
181 !
182 if (backwards) then
183 call indexx(npart,-z,iorder) ! sort front-to-back
184 else
185 call indexx(npart,z,iorder) ! sort back-to-front
186 endif
187 !
188 !--store x value for each pixel (for optimisation)
189 !
190 xminpix = xmin - 0.5*pixwidthx
191 yminpix = ymin - 0.5*pixwidthy
192 xmax = xmin + npixx*pixwidthx
193 ymax = ymin + npixy*pixwidthy
194 do ipix=1,npixx
195 xpix(ipix) = xminpix + ipix*pixwidthx
196 enddo
197
198 nused = 0
199 nsink = 0
200
201 nsubgrid = 0
202 nok = 0
203 hminall = huge(hminall)
204 hmin = 0.5*max(pixwidthx,pixwidthy)
205 if (iverbose >= 0) then
206 write(*,"(a,45x,a)") ' 0% ',' 100%'
207 write(*,"(1x,a)",advance='no') '|'
208 endif
209
210 !$omp parallel default(none) &
211 !$omp shared(hh,z,x,y,zorig,pmass,dat,datv,weight,itype,datsmooth,npmass,npart) &
212 !$omp shared(xmin,xmax,ymin,ymax,xminpix,yminpix,xpix,pixwidthx,pixwidthy,hmin) &
213 !$omp shared(exact_rendering,backwards,iverbose,radkernel,radkernel2) &
214 !$omp shared(badpix,datvpix) &
215 !$omp shared(npixx,npixy,dscreenfromobserver,zobserver,adjustzperspective) &
216 !$omp shared(zcut,zcutoff,iorder,rkappa,tausmooth) &
217 !$omp private(hi,zfrac,xi,yi,zi,radkern,rendersink) &
218 !$omp private(hi1,hi21,hsmooth,term,dati,datvi,pixwidthz) &
219 !$omp private(ipixmin,ipixmax,jpixmin,jpixmax,xpixmin,xpixmax,ypixmin,ypixmax) &
220 !$omp private(dx2i,q2,xpixi,ypix,zpix,dy,dy2,wab,dfac,pixint) &
221 !$omp private(ipart,i,ipix,jpix,tau,fopacity) &
222 !$omp reduction(+:nused,nsink,nsubgrid,nok) &
223 !$omp reduction(min:hminall)
224 !!$omp do ordered schedule(dynamic)
225 over_particles: do ipart=1,npart
226 !
227 !--render in order from back to front
228 !
229 i = iorder(ipart)
230 !
231 !--skip particles with itype < 0
232 !
233 if (itype(i) < 0) cycle over_particles
234
235 !
236 !--progress bar
237 !
238 !$omp single
239 if (iverbose >= 0 .and. mod(ipart,npart/50)==0) then
240 write(*,"('=')",advance='no')
241 endif
242 !$omp end single
243
244 !
245 !--skip particles with weight < 0
246 ! but not if weight == weight_sink (=-1)
247 !
248 rendersink = .false.
249 if (abs(weight(i) - weight_sink) < tiny(0.)) then
250 rendersink = .true.
251 elseif (weight(i) <= 0.) then
252 cycle over_particles
253 endif
254
255 !
256 !--allow slicing [take only particles with z(unrotated) < zcut]
257 !
258 particle_within_zcut: if (zorig(i) < zcut .and. z(i) < zcutoff) then
259
260 ! count particles within slice
261 !$omp single
262 nused = nused + 1
263 !$omp end single
264 !
265 !--adjust h according to 3D perspective
266 ! need to be careful -- the kernel quantities
267 ! change with z (e.g. radkern, r^2/h^2)
268 ! but *not* the 1/h^2 in tau (because the change in 1/h^2 in tau
269 ! would be cancelled by the corresponding change to h^2 in kappa)
270 !
271 hi = hh(i)
272 if (hi <= 0.) cycle over_particles
273
274 !--this is the term which multiplies tau
275 if (npmass==npart) then
276 term = rkappa(i)*pmass(i)/(hh(i)*hh(i)) ! dimensionless
277 else
278 term = rkappa(i)*pmass(1)/(hh(i)*hh(i))
279 endif
280 !
281 !--sink particles can have weight set to -1
282 ! indicating that we should include them in the rendering
283 !
284 if (rendersink) then
285 dati = dat(i) !pmass(i)/(hh(i)**3) ! define "density" of a sink
286 !$omp single
287 nsink = nsink + 1
288 !$omp end single
289 else
290 dati = dat(i)
291 endif
292 if (present(datv)) datvi = datv(:,i)
293 !
294 !--adjust apparent size of particles if 3D perspective set
295 !
296 if (adjustzperspective) then
297 zfrac = abs(dscreenfromobserver/(z(i)-zobserver))
298 hi = hi*zfrac
299 endif
300
301 !--these are the quantities used in the kernel r^2/h^2
302 radkern = radkernel*hi
303 hi1 = 1./hi
304 hi21 = hi1*hi1
305 !
306 !--determine colour contribution of current point
307 ! (work out position in colour table)
308 !
309 xi = x(i)
310 xpixmin = xi - radkern
311 if (xpixmin > xmax) cycle over_particles
312 xpixmax = xi + radkern
313 if (xpixmax < xmin) cycle over_particles
314
315 yi = y(i)
316 ypixmin = yi - radkern
317 if (ypixmin > ymax) cycle over_particles
318 ypixmax = yi + radkern
319 if (ypixmax < ymin) cycle over_particles
320
321 !--take resolution length as max of h and 1/2 pixel width
322 if (.not.exact_rendering .and. hi < hmin) then
323 hminall = min(hi,hminall)
324 !$omp single
325 nsubgrid = nsubgrid + 1
326 !$omp end single
327 hsmooth = hmin
328 else
329 hsmooth = hi
330 !$omp single
331 nok = nok + 1
332 !$omp end single
333 endif
334
335 !
336 !--quantities for exact 3D wall integrals
337 !
338 dfac = hi**2/(pixwidthx*pixwidthy)
339 pixwidthz = 2.*radkern ! 3D box bounds entire particle in z direction
340 zi = 0.; zpix = 0.
341 !
342 !
343 !--for each particle work out which pixels it contributes to
344 !
345 ipixmin = int((xi - radkern - xmin)/pixwidthx)
346 jpixmin = int((yi - radkern - ymin)/pixwidthy)
347 ipixmax = int((xi + radkern - xmin)/pixwidthx) + 1
348 jpixmax = int((yi + radkern - ymin)/pixwidthy) + 1
349
350 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
351 if (jpixmin < 1) jpixmin = 1 ! to pixels in the image
352 if (ipixmax > npixx) ipixmax = npixx ! (note that this optimises
353 if (jpixmax > npixy) jpixmax = npixy ! much better than using min/max)
354 !
355 !--precalculate an array of dx2 for this particle (optimisation)
356 !
357 do ipix=ipixmin,ipixmax
358 dx2i(ipix) = ((xpix(ipix) - xi)**2)*hi21
359 enddo
360
361 !
362 !--loop over pixels, adding the contribution from this particle
363 !
364 !$omp do schedule(dynamic)
365 do jpix = jpixmin,jpixmax
366 ypix = yminpix + jpix*pixwidthy
367 dy = ypix - yi
368 dy2 = dy*dy*hi21
369 do ipix = ipixmin,ipixmax
370 q2 = dx2i(ipix) + dy2 ! dx2 pre-calculated; dy2 pre-multiplied by hi21
371
372 if (exact_rendering .and. ipixmax-ipixmin <= 10 .and. q2 < radkernel2 + 3.*pixwidthx*pixwidthy*hi21) then
373 xpixi = xminpix + ipix*pixwidthx
374
375 ! Contribution of the cell walls in the xy-plane
376 pixint = 2.*wallint(0.5*pixwidthz,xi,yi,xpixi,ypix,pixwidthx,pixwidthy,hi)
377 !pixint = pixint + wallint(0.5*pixwidthz,xi,yi,xpixi,ypix,pixwidthx,pixwidthy,hi)
378
379 ! Contribution of the cell walls in the xz-plane
380 pixint = pixint + wallint(ypix-yi+0.5*pixwidthy,xi,zi,xpixi,zpix,pixwidthx,pixwidthz,hi)
381 pixint = pixint + wallint(yi-ypix+0.5*pixwidthy,xi,zi,xpixi,zpix,pixwidthx,pixwidthz,hi)
382
383 ! Contribution of the cell walls in the yz-plane
384 pixint = pixint + wallint(xpixi-xi+0.5*pixwidthx,zi,yi,zpix,ypix,pixwidthz,pixwidthy,hi)
385 pixint = pixint + wallint(xi-xpixi+0.5*pixwidthx,zi,yi,zpix,ypix,pixwidthz,pixwidthy,hi)
386
387 wab = pixint*dfac
388
389 tau = wab*term
390 tausmooth(ipix,jpix) = tausmooth(ipix,jpix) + tau
391 !
392 !--render, obscuring previously drawn pixels by relevant amount
393 ! also calculate total optical depth for each pixel
394 !
395 if (backwards) then
396 fopacity = exp(-tausmooth(ipix,jpix))*tau
397 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + fopacity*dati
398 if (present(datv)) datvpix(:,ipix,jpix) = datvpix(:,ipix,jpix) + fopacity*datvi(:)
399 if (present(badpix)) then
400 if (tausmooth(ipix,jpix) < 1. .and. tau >= 0.33) badpix(ipix,jpix) = 1
401 endif
402 else
403 fopacity = 1. - exp(-tau)
404 datsmooth(ipix,jpix) = (1.-fopacity)*datsmooth(ipix,jpix) + fopacity*dati
405 if (present(datv)) datvpix(:,ipix,jpix) = (1.-fopacity)*datvpix(:,ipix,jpix) + fopacity*datvi(:)
406 endif
407 else
408 !
409 !--SPH kernel - integral through cubic spline
410 ! interpolate from a pre-calculated table
411 !
412 if (q2 < radkernel2) then
413 wab = wfromtable(q2)
414
415 tau = wab*term
416 tausmooth(ipix,jpix) = tausmooth(ipix,jpix) + tau
417 !
418 !--render, obscuring previously drawn pixels by relevant amount
419 ! also calculate total optical depth for each pixel
420 !
421 if (backwards) then
422 fopacity = exp(-tausmooth(ipix,jpix))*tau
423 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + fopacity*dati
424 if (present(datv)) datvpix(:,ipix,jpix) = datvpix(:,ipix,jpix) + fopacity*datvi(:)
425 if (present(badpix)) then
426 if (tausmooth(ipix,jpix) < 1. .and. tau >= 0.33) badpix(ipix,jpix) = 1
427 endif
428 else
429 fopacity = 1. - exp(-tau)
430 datsmooth(ipix,jpix) = (1.-fopacity)*datsmooth(ipix,jpix) + fopacity*dati
431 if (present(datv)) datvpix(:,ipix,jpix) = (1.-fopacity)*datvpix(:,ipix,jpix) + fopacity*datvi(:)
432 endif
433 endif
434 endif
435 enddo
436 enddo
437 !$omp end do
438
439 endif particle_within_zcut
440
441 enddo over_particles
442 !$omp end parallel
443 !
444 !--get ending wall time
445 !
446 if (iverbose >= 0) print "('|',/)" ! end the progress bar
447 if (nsink > 99) then
448 if (iverbose >= 0) print*,'rendered ',nsink,' sink particles'
449 elseif (nsink > 0) then
450 if (iverbose >= 0) print "(1x,a,i2,a)",'rendered ',nsink,' sink particles'
451 endif
452 call wall_time(t_end)
453 t_used = t_end - t_start
454 if (t_used > 10. .and. iverbose >= 0) call print_time(t_used)
455 if (zcut < huge(zcut) .and. iverbose >= 0) print*,'slice contains ',nused,' of ',npart,' particles'
456
457 end subroutine interp3D_proj_opacity
458
459 end module interpolate3D_opacity