Debian Codegraph @

src/fieldlines.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-2016 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !
24 ! module for field line / flux tube plotting in 2 and 3 dimensions
25 !
26 module fieldlines
27 implicit none
28 public :: streamlines,vecplot3D_proj
29 private :: trace2D,interpolate_pt
30
31 private
32
33 contains
34
35 !---------------------------------------------------------------------------
36 !
37 ! This subroutine integrates a 2D vector field to give the stream function
38 ! Plotting the contours of this function gives the field/stream lines
39 !
40 ! The solution is given by
41 !
42 ! A(x,y) = \int v_x(x,y) dy - \int v_y(x,y) dx
43 !
44 ! which we compute, knowing v_x and v_y on a fixed grid of square pixels,
45 ! by a simple trapezoidal integration for each component.
46 !
47 ! For SPH, this means we first interpolate the vector field to
48 ! get v_x and v_y on the two-dimensional grid. This then also works for
49 ! cross sections / projections of 3D vector fields by first interpolating
50 ! to the 2D grid.
51 !
52 !
53 ! Inputs: vecpixx(npixx,npixy) : x component of vector field on fixed grid
54 ! vecpixy(npixx,npixy) : y component of vector field on fixed grid
55 ! xmin, ymin : xmin and ymin of grid
56 ! pixwidth : grid cell size (pixels are square)
57 ! npixx,npixy : number of grid cells (pixels) in x,y
58 !
59 ! Output: datpix(npixx,npixy) : stream function on fixed grid
60 !
61 ! written by Daniel Price [email protected]
62 !
63 ! Oct 2007: uses Simpson's rule instead of trapezoidal
64 !
65 !---------------------------------------------------------------------------
66 subroutine streamlines(vecpixx,vecpixy,datpix,npixx,npixy,pixwidth)
67 integer, intent(in) :: npixx,npixy
68 real, intent(in), dimension(npixx,npixy) :: vecpixx,vecpixy
69 real, intent(in) :: pixwidth
70 real, intent(out), dimension(npixx,npixy) :: datpix
71 real, dimension(npixx,npixy) :: datpix2
72 double precision :: fyj,fyjhalf,term,termi,termj,fxprevi,fyjprev
73 double precision, dimension(npixx) :: fx,fxhalf
74 integer :: i,j
75 !
76 !--check for errors in input
77 !
78 if (pixwidth <= 0.) then
79 print "(1x,a)",'streamlines: error: pixel width <= 0'
80 datpix = 0.
81 return
82 endif
83
84 fyj = 0.
85 fyjhalf = 0.
86 !
87 !--perform the integration forwards
88 !
89 do j=1,npixy
90 do i=1,npixx
91 term = 0.
92 if (i==1) then
93 fyj = 0.
94 fyjhalf = 0.
95 else
96 fyjprev = fyj
97 !--trapezoidal rule in x
98 termj = 0.5*pixwidth*(vecpixy(i-1,j)+vecpixy(i,j))
99 fyj = fyj - termj
100 if (mod(i-1,2)==0) then ! 3, 5, 7, 9 ...
101 !
102 !--for odd points, use trapezoidal solution at half grid points
103 ! to get Simpson's rule
104 !
105 fyjhalf = fyjhalf - pixwidth*(vecpixy(i-2,j)+vecpixy(i,j))
106 term = term + 4./3.*fyj - 1./3.*fyjhalf
107 else
108 !
109 !--for even points, use Simpson's rule up to last odd point
110 ! then finish with a trapezoidal integration over last two points
111 !
112 term = term + 4./3.*fyjprev - 1./3.*fyjhalf - termj
113 endif
114 endif
115 !
116 !--same as above but for integration in y
117 !
118 if (j==1) then
119 fx(i) = 0.
120 fxhalf(i) = 0.
121 fxprevi = 0.
122 else
123 fxprevi = fx(i)
124 termi = 0.5*pixwidth*(vecpixx(i,j-1)+vecpixx(i,j))
125 fx(i) = fx(i) + termi
126 if (mod(j-1,2)==0) then
127 fxhalf(i) = fxhalf(i) + pixwidth*(vecpixx(i,j-2)+vecpixx(i,j))
128 term = term + 4./3.*fx(i) - 1./3.*fxhalf(i)
129 else
130 term = term + 4./3.*fxprevi - 1./3.*fxhalf(i) + termi
131 endif
132 endif
133
134 datpix(i,j) = real(term)
135 enddo
136 enddo
137 !
138 !--perform the integration backwards
139 !
140 datpix2 = 0.
141 do j=npixy,1,-1
142 do i=npixx,1,-1
143 term = 0.
144 if (i==npixx) then
145 fyj = 0.
146 fyjhalf = 0.
147 else
148 fyjprev = fyj
149 !--trapezoidal rule in x
150 termj = 0.5*pixwidth*(vecpixy(i+1,j)+vecpixy(i,j))
151 fyj = fyj + termj
152 if (mod(npixx-i,2)==0) then ! 3, 5, 7, 9 ...
153 !
154 !--for odd points, use trapezoidal solution at half grid points
155 ! to get Simpson's rule
156 !
157 fyjhalf = fyjhalf + pixwidth*(vecpixy(i+2,j)+vecpixy(i,j))
158 term = term + 4./3.*fyj - 1./3.*fyjhalf
159 else
160 !
161 !--for even points, use Simpson's rule up to last odd point
162 ! then finish with a trapezoidal integration over last two points
163 !
164 term = term + 4./3.*fyjprev - 1./3.*fyjhalf + termj
165 endif
166 endif
167 !
168 !--same as above but for integration in y
169 !
170 if (j==npixy) then
171 fx(i) = 0.
172 fxhalf(i) = 0.
173 fxprevi = 0.
174 else
175 fxprevi = fx(i)
176 termi = 0.5*pixwidth*(vecpixx(i,j+1)+vecpixx(i,j))
177 fx(i) = fx(i) - termi
178 if (mod(npixy-j,2)==0) then
179 fxhalf(i) = fxhalf(i) - pixwidth*(vecpixx(i,j+2)+vecpixx(i,j))
180 term = term + 4./3.*fx(i) - 1./3.*fxhalf(i)
181 else
182 term = term + 4./3.*fxprevi - 1./3.*fxhalf(i) - termi
183 endif
184 endif
185
186 datpix2(i,j) = real(term)
187 enddo
188 enddo
189
190 !
191 !--average the two
192 !
193 datpix = 0.5*(datpix + datpix2)
194
195 return
196 end subroutine streamlines
197
198 !---------------------------------------------------------------------------------
199 !
200 ! THE REST OF THIS MODULE IS EITHER OLD, *VERY* EXPERIMENTAL AND/OR UNDOCUMENTED
201 !
202 !---------------------------------------------------------------------------------
203
204 !
205 ! we want to trace the curve through a 2D vector field
206 !
207 subroutine fieldlines2D(npart,x,y,vecx,vecy,h,pmass,rho,xmin,xmax,ymin,ymax)
208 integer, intent(in) :: npart
209 real, intent(in), dimension(npart) :: x,y,vecx,vecy,h,pmass,rho
210 real, intent(in) :: xmin,xmax,ymin,ymax
211 integer :: i,nlines
212 real :: dx,dy,xstart,ystart,ymaxline
213
214 nlines = 10
215 dx = (xmax-xmin)/nlines
216 dy = (ymax-ymin)/nlines
217 ystart = ymin
218 ymaxline = ymin
219
220 do while (ymaxline < ymax)
221 do i=1,nlines
222 xstart = xmin + (i-1)*dx + 0.5*dx
223 print*,' tracing field line ',i,' x, y = ',xstart,ystart
224 call trace2D(xstart,ystart,xmin,xmax,ymin,ymax,ymaxline, &
225 x,y,vecx,vecy,h,pmass,rho,npart)
226 enddo
227 ystart = ymaxline + 0.5*dy
228 enddo
229
230 end subroutine fieldlines2D
231
232 subroutine trace2D(xstart,ystart,xmin,xmax,ymin,ymax,ymaxline, &
233 x,y,vecx,vecy,h,pmass,rho,npart)
234 use plotlib, only:plot_line
235 integer, intent(in) :: npart
236 real, intent(in) :: xstart,ystart,xmin,xmax,ymin,ymax
237 real, intent(inout) :: ymaxline
238 real, dimension(npart), intent(in) :: x,y,vecx,vecy,h,pmass,rho
239 integer :: ipt,npix
240 real, dimension(2) :: xline, yline
241 real :: runit,runit1,dx,dy,vx,vy,pixwidth,sign
242
243 xline(1) = xstart
244 yline(1) = ystart
245 npix = 100
246 pixwidth = (xmax - xmin)/npix
247 ipt = 0
248 sign = 1.0
249
250 do while ((xline(1) >= xmin .and. xline(1) <= xmax) .and. &
251 (yline(1) >= ymin .and. yline(1) <= ymax) .and. ipt < 10*npix)
252
253 ipt = ipt + 1
254 !
255 !--get dx and dy from interpolation of vector field from particles
256 !
257 call interpolate_pt(xline(1),yline(1),vx,vy, &
258 x,y,vecx,vecy,h,pmass,rho,npart)
259 !
260 !--get unit vector in direction of vector
261 !
262 runit = sqrt(vx**2 + vy**2)
263 if (runit > 0) then
264 runit1 = 1./runit
265 dx = vx*runit1*pixwidth
266 dy = vy*runit1*pixwidth
267 else
268 dx = 0.
269 dy = 0.
270 endif
271
272 if (ipt==1 .and. dy < 0.) sign = -1.0
273
274 xline(2) = xline(1) + sign*dx
275 yline(2) = yline(1) + sign*dy
276
277 !!print*,'x, y = ',xline(2),yline(2)
278 ymaxline = max(ymaxline,yline(2))
279 !
280 !--plot line segment
281 !
282 call plot_line(2,xline,yline)
283
284 xline(1) = xline(2)
285 yline(1) = yline(2)
286
287 enddo
288
289 if (ipt >= 10*npix) print*,'WARNING: infinite field line'
290
291 end subroutine trace2D
292
293 !
294 !--interpolate from particles to single point
295 ! would be nice to know neighbours
296 !
297 subroutine interpolate_pt(xpt,ypt,vxpt,vypt,x,y,vecx,vecy,h,pmass,rho,npart)
298 use kernels, only:radkernel2,wfunc,cnormk2D
299 integer, intent(in) :: npart
300 real, dimension(npart), intent(in) :: x,y,vecx,vecy,h,pmass,rho
301 real, intent(in) :: xpt, ypt
302 real, intent(out) :: vxpt, vypt
303 real :: rho1i,term,const,dx,dy,hi1,q2,wab
304 integer :: i
305
306 vxpt = 0.
307 vypt = 0.
308 const = cnormk2D
309
310 do i=1,npart
311 dx = xpt - x(i)
312 dy = ypt - y(i)
313 hi1 = 1./h(i)
314 q2 = (dx*dx + dy*dy)*hi1*hi1
315 !
316 !--if particles are within range, calculate contribution to this pt
317 !
318 if (q2 < radkernel2) then
319 if (rho(i) > 0.) then
320 rho1i = 1./rho(i)
321 else
322 rho1i = 0.
323 endif
324 term = const*pmass(i)*rho1i
325 wab = wfunc(q2)
326
327 vxpt = vxpt + term*vecx(i)*wab
328 vypt = vypt + term*vecy(i)*wab
329 endif
330 enddo
331
332 end subroutine interpolate_pt
333
334 !--------------------------------------------------------------------------
335 ! Visualisation of a 3D vector field in projection
336 ! by means of "iron filings" drawn on particles
337 !
338 ! We draw a line on each particle in the direction of the vector field
339 ! This line is illuminated by reflections from a lighting source, and
340 ! is drawn with an opacity proportional to the field strength, such that
341 ! strong field regions are highlighted.
342 !
343 ! For details of the lighting algorithm, see e.g.
344 ! Stalling, Zoeckler and Hege, 1997, IEEE Trans. Viz. Comp. Graphics, 3, 118-128
345 !
346 ! Added by D. Price, Dec 2011
347 !--------------------------------------------------------------------------
348 subroutine vecplot3D_proj(x,y,z,vx,vy,vz,vecmax,weight,itype,n,dx,zobs,dscreen)
349 use plotlib, only:plot_line,plot_bbuf,plot_ebuf,plot_slw,plot_sci,plot_set_opacity
350 use plotlib, only:plot_qcr,plot_scr,plot_qlw,plot_arro,plot_sah
351 use sort, only:indexx
352 integer, intent(in) :: n
353 real, dimension(n), intent(in) :: x,y,z,vx,vy,vz,weight
354 integer, dimension(n), intent(in) :: itype
355 real, intent(inout) :: vecmax
356 real, intent(in) :: dx,zobs,dscreen
357 integer, dimension(n) :: iorder
358 integer :: i,ipart,np
359 real, dimension(2) :: xpts,ypts
360 real :: vxi,vyi,vzi,dvmag,zfrac,vmax,vmag,frac,ri,gi,bi,term,lw
361 real :: toti,fambient,diffuse,specular,fdiff,fspec,ldotn,vdotr,ldott,vdott
362 integer :: pdiff,nspec,lwold
363 real, dimension(3) :: vunit,lighting,viewangle
364 logical :: white_bg,use3Dperspective
365
366 !
367 !--get the max adaptively if it is not already set
368 !
369 if (vecmax <= 0. .or. vecmax > 0.5*huge(vecmax)) then
370 vmax = 0.
371 do i=1,n
372 if (itype(i) >= 0 .and. weight(i) > 0.) then
373 vmax = max(vx(i)**2 + vy(i)**2 + vz(i)**2,vmax)
374 endif
375 enddo
376 vmax = sqrt(vmax)
377 vecmax = vmax
378 else
379 vmax = vecmax
380 endif
381
382 use3Dperspective = abs(dscreen) > tiny(dscreen)
383
384 !
385 !--work out whether or not we have a white or black
386 ! background colour
387 !
388 !call plot_sah(1,20.,1.0)
389 call plot_qcr(0,ri,gi,bi)
390 white_bg = (ri + gi + bi > 1.5)
391 !
392 !--specify the parameters in the lighting algorithm
393 ! these should differ depending on whether we are drawing
394 ! on a white or black background
395 !
396 if (white_bg) then
397 fambient = 0.
398 fdiff = 0.1
399 fspec = 0.5
400 else
401 fambient = 0.3
402 fdiff = 0.7
403 fspec = 0.8
404 endif
405 pdiff = 4
406 nspec = 12
407 !
408 !--specify the viewing and lighting angles
409 !
410 viewangle = (/0.,0.,1./)
411 !lighting = (/0.3,0.3,1./)
412 lighting = (/0.,0.,1./)
413
414 !--make sure these are normalised
415 !lighting = lighting/sqrt(dot_product(lighting,lighting))
416
417 print*,'plotting 3D field structure: min,max = ',1.e-3*vmax,vmax
418 !
419 !--first sort the particles in z so that we do the opacity in the correct order
420 !
421 call indexx(n,z,iorder)
422 call plot_bbuf
423 call plot_qcr(1,ri,gi,bi)
424
425 np = 0
426 zfrac = 1.
427 call plot_qlw(lwold)
428 lw = 2.*lwold
429 over_particles: do ipart=1,n
430 i = iorder(ipart)
431 if (itype(i) >= 0 .and. weight(i) > 0.) then
432 if (use3Dperspective) then
433 if (z(i) > zobs) cycle over_particles
434 zfrac = abs(dscreen/(z(i)-zobs))
435 endif
436 !if (mod(ipart,10)/=0) cycle over_particles
437 ! lw = min(zfrac,2.5)
438
439 vxi = vx(i)
440 vyi = vy(i)
441 vzi = vz(i)
442 !
443 !--we draw lines on each particle with an
444 ! opacity proportional to the field strength
445 !
446 vmag = sqrt(vxi**2 + vyi**2 + vzi**2)
447 dvmag = 1./vmag
448 vunit = abs((/vxi,vyi,vzi/)*dvmag)
449 frac = min(vmag/vmax,1.0)
450
451 if (frac >= 1.e-3) then
452 !--specify the length of line to draw
453 term = 1.5*dx*dvmag*zfrac
454 !term = term*(vmag/vmax)**0.2
455 xpts(1) = x(i) - vxi*term
456 xpts(2) = x(i) + vxi*term
457 ypts(1) = y(i) - vyi*term
458 ypts(2) = y(i) + vyi*term
459
460 !--draw "halo" in background colour with
461 ! twice the thickness, same opacity
462 call plot_slw(2.*lw)
463 call plot_sci(0)
464 call plot_set_opacity(frac)
465 call plot_line(2,xpts,ypts)
466
467 !--Phong lighting
468 ldott = dot_product(lighting,vunit)
469 ldotn = sqrt(1. - ldott**2)
470 diffuse = fdiff*(ldotn)**pdiff
471
472 vdott = dot_product(viewangle,vunit)
473 vdotr = ldotn*sqrt(1. - vdott**2) - ldott*vdott
474 specular = fspec*(vdotr)**nspec
475 toti = (fambient + diffuse + specular)
476
477 !--draw line with intensity proportional
478 ! to the amount of lighting
479 !call plot_scr(1,toti,toti,toti,max(frac,0.15))
480 call plot_scr(1,toti,toti,toti,max(frac,0.05))
481 call plot_sci(1)
482 call plot_slw(lw)
483 call plot_line(2,xpts,ypts)
484 !call plot_arro(xpts(1),ypts(1),xpts(2),ypts(2))
485 np = np + 1
486 endif
487 endif
488 enddo over_particles
489 !--reset opacity for both foreground and background colour indices
490 call plot_sci(0)
491 call plot_set_opacity(1.0)
492 call plot_sci(1)
493 call plot_scr(1,ri,gi,bi)
494 call plot_set_opacity(1.0)
495 call plot_ebuf
496 call plot_slw(lwold)
497 print*,' plotted ',np,' of ',n,' particles'
498
499 end subroutine vecplot3D_proj
500
501 end module fieldlines