Debian Codegraph @

src/analysis.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-2015 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-------------------------------------------------------------------
24 ! module implementing the ability to use SPLASH to produce
25 ! evolution files from a sequence of SPH dump files
26 ! (ie. in order to produce plots of certain quantities vs time)
27 ! Command is "splash calc X" where X is analysis type.
28 !-------------------------------------------------------------------
29 module analysis
30 implicit none
31 public :: isanalysis,open_analysis,close_analysis,write_analysis
32 private
33 integer, private, parameter :: iunit = 89
34 integer, private, parameter :: maxlevels = 20
35 integer, private, parameter :: maxtrack = 32
36 !
37 ! default settings for the density thresholds for massaboverho output
38 !
39 integer, private :: nlevels,nfilesread,nfileout
40 real, dimension(maxlevels), private :: rholevels
41 character(len=64), private :: fileout(maxtrack)
42 real, dimension(:,:), allocatable :: datmean,datvar
43 integer, private :: itracks(maxtrack)
44
45 !
46 ! definitions needed for tdiffuse output
47 !
48 integer, parameter :: ndirs = 6
49 character(len=2) :: dir_label(ndirs) = (/'-x','+x','-y','+y','-z','+z'/)
50
51 ! angles required for direction: -x +x -y +y -z +z
52 real, parameter :: anglex_vals(ndirs) = (/0., 0., -90., 90., 180., 0./)
53 real, parameter :: angley_vals(ndirs) = (/-90., 90., 0., 0., 0., 0./)
54 real, parameter :: anglez_vals(ndirs) = (/0., 0., 0., 0., 0., 0./)
55
56 contains
57
58 !-----------------------------------------------------------------
59 ! utility to check if the choice of analysis type is valid
60 ! and if not, to print the available options
61 !-----------------------------------------------------------------
62 logical function isanalysis(string,noprint)
63 character(len=*), intent(in) :: string
64 logical, intent(in), optional :: noprint
65 logical :: doprint,verbose
66
67 isanalysis = .false.
68 verbose = .true.
69 select case(trim(string))
70 case('energies','energy')
71 isanalysis = .true.
72 case('massaboverho')
73 isanalysis = .true.
74 case('max','maxvals')
75 isanalysis = .true.
76 case('min','minvals')
77 isanalysis = .true.
78 case('diff','diffvals')
79 isanalysis = .true.
80 case('delta','deltavals')
81 isanalysis = .true.
82 case('amp','ampvals')
83 isanalysis = .true.
84 case('mean','meanvals')
85 isanalysis = .true.
86 case('rms','rmsvals')
87 isanalysis = .true.
88 case('vrms','vrmsvals','vwrms','rmsvw')
89 isanalysis = .true.
90 case('rhovar','rhomach')
91 isanalysis = .true.
92 case('kh')
93 isanalysis = .true.
94 case('timeaverage','timeav')
95 isanalysis = .true.
96 case('ratio','minus','plus')
97 isanalysis = .true.
98 case('tracks','track')
99 isanalysis = .true.
100 case('lightcurve')
101 isanalysis = .true.
102 case('extinction')
103 isanalysis = .true.
104 case('tdiffuse')
105 isanalysis = .true.
106 case('none')
107 verbose = .false.
108 end select
109
110 if (present(noprint)) then
111 doprint = .not.noprint
112 else
113 doprint = .true.
114 endif
115
116 if (.not.isanalysis .and. doprint .and. verbose) then
117 print "(a)",' Analysis mode ("splash calc X dumpfiles") on a sequence of dump files: '
118 print "(a)",' splash calc energies : calculate KE,PE,total energy vs time'
119 print "(a)",' output to file called ''energy.out'''
120 print "(a)",' calc massaboverho : mass above a series of density thresholds vs time'
121 print "(a)",' output to file called ''massaboverho.out'''
122 print "(a)",' calc extinction : column density to all sink particles vs time'
123 print "(a)",' output to file called ''extinction.out'''
124 ! print "(a)",' calc rhomach : density variance and RMS velocity dispersion vs. time'
125 ! print "(a)",' output to file called ''rhomach.out'''
126 print "(a)",' calc max : maximum of each column vs. time'
127 print "(a)",' output to file called ''maxvals.out'''
128 print "(a)",' calc min : minimum of each column vs. time'
129 print "(a)",' output to file called ''minvals.out'''
130 print "(a)",' calc diff : (max - min) of each column vs. time'
131 print "(a)",' output to file called ''diffvals.out'''
132 print "(a)",' calc amp : 0.5*(max - min) of each column vs. time'
133 print "(a)",' output to file called ''ampvals.out'''
134 print "(a)",' calc delta : 0.5*(max - min)/mean of each column vs. time'
135 print "(a)",' output to file called ''deltavals.out'''
136 print "(a)",' calc mean : mean of each column vs. time'
137 print "(a)",' output to file called ''meanvals.out'''
138 print "(a)",' calc rms : (mass weighted) root mean square of each column vs. time'
139 print "(a)",' output to file called ''rmsvals.out'''
140 print "(a)",' calc tracks : track particle data vs time for selected particles,'
141 print "(a)",' --track=1,2,3 output to tracks-1.out,tracks-2.out,tracks-3.out'
142 ! print "(a)",' calc vrms : volume weighted root mean square of each column vs. time'
143 ! print "(a)",' output to file called ''rmsvals-vw.out'''
144 print "(/,a)",' the above options all produce a small ascii file with one row per input file.'
145 print "(a)",' the following option produces a file equivalent in size to one input file (in ascii format):'
146 print "(/,a)",' calc timeaverage : time average of *all* entries for every particle'
147 print "(a)",' output to file called ''time_average.out'''
148 print "(/,a)",' calc ratio : ratio of *all* entries in each file compared to first'
149 print "(a)",' output to file called ''ratio.out'''
150 print "(/,a)",' calc plus : add two snapshots together'
151 print "(a)",' output to file called ''plus.out'''
152 elseif (.not.isanalysis .and. doprint) then
153 print "(a)",'Analysis mode:'
154 print "(a)",' splash calc <mode> : type "splash calc" for details'
155 endif
156
157 return
158 end function isanalysis
159
160 !----------------------------------------------------------------
161 ! open output file/ initialise quantities needed for analysis
162 ! over all dump files
163 !----------------------------------------------------------------
164 subroutine open_analysis(analysistype,required,ncolumns,ndim,ndimV,nsinks)
165 use labels, only:ix,ivx,ih,iBfirst,iutherm,irho,ipmass,itemp,ikappa,label,&
166 lenunitslabel,unitslabel,labelzintegration,get_unitlabel_coldens
167 use asciiutils, only:read_asciifile,basename,integer_to_string
168 use filenames, only:rootname,nfiles,tagline,fileprefix,ifileopen
169 use params, only:maxplot
170 use system_utils, only:ienvlist
171 use settings_data, only:iRescale
172 integer, intent(in) :: ncolumns,ndim,ndimV,nsinks
173 character(len=*), intent(in) :: analysistype
174 logical, dimension(0:ncolumns), intent(out) :: required
175 character(len=maxplot*18) :: headerline ! len=maxplot x 18 characters
176 character(len=64) :: levelsfile
177 character(len=maxplot*12) :: fmtstring
178 character(len=lenunitslabel) :: labelt,labelc
179 logical :: iexist,standardheader
180 integer :: ierr,i,lunit
181 !
182 !--the 'required' array is used by the data reads (where implemented)
183 ! to determine whether or not we actually need to read a given column
184 ! from the file -- if not it can be skipped, leading to a faster
185 ! data read. Here we want to specify which columns are required
186 ! for the analysis in question.
187 !
188 print "(/,5('-'),a,/)",'> CALCULATING '//trim(analysistype)//' vs time for all dump files'
189 required(:)=.false.
190 headerline = ' '
191 standardheader = .false.
192 nfileout = 1
193 labelt = ''
194 if (iRescale) then
195 labelt = unitslabel(0)
196 endif
197
198 select case(trim(analysistype))
199 case('energy','energies')
200 !
201 !--for energies need to read particle mass, velocity, utherm and if present,
202 ! magnetic field and density. The obvious limitation here is that we
203 ! cannot calculate the potential energy unless it is dumped and labelled
204 ! (which is not currently implemented).
205 !
206 required(ivx:ivx+ndimV-1) = .true.
207 required(iBfirst:iBfirst+ndimV-1) = .true.
208 required(iutherm) = .true.
209 required(ipmass) = .true.
210 if (iBfirst > 0) required(irho) = .true.
211 required(ix(1:ndim)) = .true.
212 !
213 !--set filename and header line
214 !
215 fileout(1) = 'energy.out'
216 write(headerline,"('#',8(1x,'[',i2.2,1x,a11,']',2x))") &
217 1,'time',2,'ekin',3,'etherm',4,'emag',5,'epot',6,'etot',7,'totmom',8,'totang'
218
219 case('massaboverho')
220 !
221 !--only need to read mass and density from dump files
222 !
223 required(ipmass) = .true.
224 required(irho) = .true.
225 !
226 !--need a user-configurable way of setting the density thresholds:
227 ! implemented by setting them in a file which is read here
228 ! (a new one is created if it doesn't exist)
229 !
230 levelsfile = 'massaboverho.levels'
231 inquire(file=levelsfile,exist=iexist)
232 if (iexist) then
233 call read_asciifile(trim(levelsfile),nlevels,rholevels)
234 print "(a)",' read '//trim(levelsfile)//':'
235 do i=1,nlevels
236 print "(a,i2,a,es9.2)",' level ',i,': rho > ',rholevels(i)
237 enddo
238 print*
239 else
240 print "(a)",' SPLASH ANALYSIS: levels file '//trim(levelsfile)//' not found'
241 print "(a)",' creating one with default levels for mass > rho'
242 print "(a)",' edit this file to set the density levels'
243 open(unit=iunit+1,file=levelsfile,status='new',form='formatted',iostat=ierr)
244 if (ierr /= 0) then
245 stop 'ERROR creating levels file'
246 else
247 nlevels = 10
248 rholevels(1:nlevels) = (/1e-20,1e-19,1e-18,1e-17,1e-16, &
249 1e-15,1e-14,1e-13,1e-12,1e-11/)
250 write(iunit+1,*) rholevels(1:nlevels)
251 close(iunit+1)
252 endif
253 stop
254 endif
255 !
256 !--set filename and header line
257 !
258 fileout(1) = 'massaboverho.out'
259 write(headerline,"('#',1x,'[',i2.2,1x,a12,']',1x,20('[',i2.2,1x,a4,es8.1,a1,']',1x))") &
260 1,'time',(i+1,'M(r>',rholevels(i),')',i=1,nlevels)
261
262 case('max','maxvals')
263 !
264 !--read all columns from dump file
265 !
266 required(:) = .true.
267 !
268 !--set filename and header line
269 !
270 fileout(1) = 'maxvals.out'
271 standardheader = .true.
272
273 case('min','minvals')
274
275 required(:) = .true.
276 fileout(1) = 'minvals.out'
277 standardheader = .true.
278
279 case('diff','diffvals')
280
281 required(:) = .true.
282 fileout(1) = 'diffvals.out'
283 standardheader = .true.
284
285 case('amp','ampvals')
286
287 required(:) = .true.
288 fileout(1) = 'ampvals.out'
289 standardheader = .true.
290
291 case('delta','deltavals','deltas')
292
293 required(:) = .true.
294 fileout(1) = 'deltavals.out'
295 standardheader = .true.
296
297 case('mean','meanvals')
298
299 required(:) = .true.
300 fileout(1) = 'meanvals.out'
301 standardheader = .true.
302
303 case('rms','rmsvals')
304
305 required(:) = .true.
306 fileout(1) = 'rmsvals.out'
307 standardheader = .true.
308
309 case('vrms','vrmsvals','vwrms','rmsvw')
310
311 required(:) = .true.
312 fileout(1) = 'rmsvals-vw.out'
313 standardheader = .true.
314
315 case('rhovar','rhomach')
316 !
317 !--read density, velocity info
318 !
319 required(ipmass) = .true.
320 required(irho) = .true.
321 required(ivx:ivx+ndimV-1) = .true.
322 !
323 !--set filename and header line
324 !
325 fileout(1) = 'rhomach.out'
326 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a12,'']'',2x))')",iostat=ierr) 17
327 write(headerline,fmtstring) 1,'time',2,'rhomean(vw)',3,'rhomean(mw)',4,'varrho(vw)',5,'varrho(mw)',&
328 6,'stddevrho(vw)',7,'stddevrho(mw)',8,'rms v (vw)',9,'rms v (mw)',10,'b (vw)',11,'b (mw)',&
329 12,'s mean(vw)',13,'s mean(mw)',14,'s var(vw)',15,'s var(mw)',16,'s stddev(vw)',17,'s stddev(mw)'
330
331 case('kh')
332 !
333 !--read all columns from dump file
334 !
335 required(irho) = .true.
336 required(ivx:ivx+ndimV-1) = .true.
337 !
338 !--set filename and header line
339 !
340 fileout(1) = 'kh.out'
341 standardheader = .true.
342 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a12,'']'',2x))')",iostat=ierr) 2
343 write(headerline,fmtstring) 1,'time',2,'max(ekiny)'
344
345 case('timeaverage','timeav')
346 !
347 !--read all columns from dump file
348 !
349 required(:) = .true.
350 !
351 !--set filename and header line
352 !
353 fileout(1) = 'time_average.out'
354 if (ncolumns > 0) then
355 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a12,'']''))')",iostat=ierr) 2*ncolumns
356 write(headerline,fmtstring,iostat=ierr) (i,label(i)(1:12),i=1,ncolumns),&
357 (ncolumns+i,'err'//label(i)(1:9),i=1,ncolumns)
358 endif
359 case('ratio','minus','add')
360 !
361 !--read all columns from dump file
362 !
363 required(:) = .true.
364 !
365 !--set filename and header line
366 !
367 fileout(1) = trim(analysistype)//'.out'
368 if (ncolumns > 0 .and. ncolumns /= maxplot) then
369 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a12,'']''))')",iostat=ierr) 2*ncolumns
370 write(headerline,fmtstring,iostat=ierr) (i,label(i)(1:12),i=1,ncolumns),&
371 (ncolumns+i,'err'//label(i)(1:9),i=1,ncolumns)
372 endif
373 case('tracks','track')
374 !
375 !--read all of dump file
376 !
377 required(:) = .true.
378 !
379 !--set filename and header line
380 !
381 fileout(1) = 'tracks.out' ! in case ntracks = 0
382 !
383 !--look for a file "splash.tracks" for particle ids to track
384 !
385 call read_asciifile(trim(fileprefix)//'.tracks',nfileout,itracks,ierr)
386 if (ierr /= 0) then
387 !
388 !--otherwise use --tracks=1,2,3
389 !
390 itracks = ienvlist('SPLASH_TRACK',size(itracks))
391 nfileout = count(itracks > 0)
392 endif
393 if (nfileout > 0) then
394 print "(a,i0,a)",' TRACKING ',nfileout,' PARTICLES'
395 else
396 print "(a)",' Use --track=1,2,3 or splash.tracks file to specify particle IDs to track'
397 endif
398 do i=1,nfileout
399 write(fileout(i),"(a,i0,a)") 'tracks-',itracks(i),'.out'
400 enddo
401 nfileout = 1
402 standardheader = .true.
403
404 case('lightcurve')
405 !
406 !--for lightcurve need to h, mass and utherm
407 !
408 required(ix(1:ndim)) = .true.
409 required(ih) = .true.
410 required(iutherm) = .true.
411 required(ipmass) = .true.
412 required(itemp) = .true.
413 required(ikappa) = .true.
414 !
415 !--set filename and header line
416 !
417 if (nfiles==1) then
418 fileout = 'lightcurve_'//trim(basename(rootname(ifileopen)))//'.out'
419 else
420 fileout = 'lightcurve.out'
421 endif
422 write(headerline,"('#',8(1x,'[',i2.2,1x,a11,']',2x))") &
423 1,'time'//trim(labelt),2,'Luminosity',3,'R_{eff}',4,'T_{eff}',&
424 5,'L_{bol}',6,'R_{bb}',7,'T_c',8,'bad pix %'
425
426 case('extinction')
427 !
428 !--for extinction of stars need h, mass and density
429 !
430 required(ix(1:ndim)) = .true.
431 required(ih) = .true.
432 required(ipmass) = .true.
433 required(irho) = .true.
434 !
435 !--set filename and header line
436 !
437 if (nfiles==1) then
438 fileout = 'extinction_'//trim(basename(rootname(ifileopen)))//'.out'
439 else
440 fileout = 'extinction.out'
441 endif
442 labelc = get_unitlabel_coldens(iRescale,labelzintegration,unitslabel(irho))
443
444 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a15,'']'',2x))')",iostat=ierr) nsinks+1
445 write(headerline,fmtstring) &
446 1,'time'//trim(labelt),(i+1,'sink'//trim(adjustl(integer_to_string(i)))//trim(labelc),i=1,nsinks)
447 case('tdiffuse')
448 !
449 !--similar to extinction, but could be any column
450 !
451 !required(ix(1:ndim)) = .true.
452 !required(ih) = .true.
453 !required(ipmass) = .true.
454 !required(irho) = .true.
455 required(:) = .true.
456 !
457 !--set filename and header line
458 !
459 if (nfiles==1) then
460 fileout = 'tdiffuse_'//trim(basename(rootname(ifileopen)))//'.out'
461 else
462 fileout = 'tdiffuse.out'
463 endif
464
465 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a15,'']'',2x))')",iostat=ierr) ndirs+1
466 write(headerline,fmtstring) &
467 1,'time'//trim(labelt),(i+1,dir_label(i),i=1,ndirs)
468 end select
469
470 if (standardheader) then
471 !
472 !--standard header is time in column 1, with an entry for each column following
473 ! (this is to avoid repeated code above)
474 !
475 write(fmtstring,"('(''#'',1x,',i3,'(''['',i2.2,1x,a12,'']'',2x))')",iostat=ierr) ncolumns+1
476 write(headerline,fmtstring) 1,'time'//trim(labelt),(i+1,label(i)(1:12),i=1,ncolumns)
477 endif
478
479 !
480 !--do not replace the file if it already exists
481 !
482 do i=1,nfileout
483 inquire(file=trim(fileout(i)),exist=iexist)
484 if (iexist) then
485 print "(2(a,/))",' ERROR: analysis file '//trim(fileout(i))//' already exists', &
486 ' delete, move or rename this file and try again'
487 stop
488 endif
489 !
490 !--open the file for output
491 !
492 lunit = iunit+i-1
493 if (nfileout > 1) then
494 open(unit=lunit,file=trim(fileout(i)),status='replace',form='formatted',iostat=ierr)
495 else
496 open(unit=lunit,file=trim(fileout(i)),status='new',form='formatted',iostat=ierr)
497 endif
498 if (ierr /= 0) then
499 print "(a)",' ERROR opening file '//trim(fileout(i))//' for output'
500 stop
501 endif
502 print "(a)",' WRITING '//trim(analysistype)//' vs time to file '//trim(fileout(i))
503 !
504 !--write header if the headerline is set
505 ! (no header is written if headerline is blank)
506 !
507 if (len_trim(headerline) > 0) then
508 write(lunit,"(a)") '# '//trim(tagline)
509 write(lunit,"(a)") '# '//trim(fileout(i))//' produced using "splash calc '//trim(analysistype)// &
510 '" on dump files '//trim(rootname(1))//'->'//trim(rootname(nfiles))
511 write(lunit,"(a)") '# use splash -ev '//trim(fileout(i))//' to plot the contents of this file '
512 write(lunit,"(a)") '#'
513 write(lunit,"(a)") trim(headerline)
514 endif
515 enddo
516 !if (nfileout == 0) then
517 !print "(a)",' ERROR: no output from analysis, missing options?'
518 !stop
519 !endif
520 nfilesread = 0
521
522 end subroutine open_analysis
523
524 !----------------------------------------------------------------
525 ! this is the routine which actually calculates the quantities
526 ! required from each dump file and spits out a line to the
527 ! analysis file. Called once for each dump file.
528 !----------------------------------------------------------------
529 subroutine write_analysis(time,dat,ntot,ntypes,npartoftype,massoftype,&
530 iamtype,ncolumns,ndim,ndimV,analysistype)
531 use labels, only:ix,ivx,iBfirst,iutherm,irho,ipmass,labeltype,label,get_sink_type,&
532 lenunitslabel,get_unitlabel_coldens,unitslabel,labelzintegration
533 use params, only:int1,doub_prec,maxplot
534 use asciiutils, only:ucase,basename
535 use system_utils, only:renvironment,ienvironment
536 use settings_part, only:iplotpartoftype
537 use particle_data, only:time_was_read
538 use part_utils, only:get_positions_of_type
539 use settings_data, only:xorigin,icoords,icoordsnew,track_string,iRescale
540 use geomutils, only:change_coords
541 use part_utils, only:get_tracked_particle
542 use lightcurve, only:get_lightcurve
543 use extinction, only:get_extinction,get_extinction_los
544 use filenames, only:rootname,ifileopen
545 use vectorutils, only:cross_product3D
546 integer, intent(in) :: ntot,ntypes,ncolumns,ndim,ndimV
547 integer, intent(in), dimension(:) :: npartoftype
548 real, intent(in), dimension(:) :: massoftype
549 integer(kind=int1), intent(in), dimension(:) :: iamtype
550 real, intent(in) :: time
551 real, intent(in), dimension(:,:) :: dat
552 character(len=*), intent(in) :: analysistype
553 real(kind=doub_prec), dimension(maxlevels) :: massaboverho
554 integer :: itype,i,j,ierr,ntot1,ncol1,nused,itrack,ifile,npts,isinktype,icol
555 real(kind=doub_prec) :: ekin,emag,etherm,epot,etot,totmom,pmassi,totang
556 real(kind=doub_prec) :: totvol,voli,rhoi,rmsvmw,v2i
557 real(kind=doub_prec) :: rhomeanmw,rhomeanvw,rhovarmw,rhovarvw,bval,bvalmw
558 real(kind=doub_prec) :: smeanmw,smeanvw,svarmw,svarvw,si,ekiny,ekinymax
559 real(kind=doub_prec) :: lmin(maxplot),lmax(maxplot),lmean(maxplot),rmsvali
560 real(kind=doub_prec), dimension(3) :: xmom,angmom,angmomi,ri,vi
561 real :: delta,dn,valmin,valmax,valmean,timei
562 real :: lum,rphoto,tphoto,l_bb,r_bb,t_bb,badfrac
563 character(len=20) :: fmtstring
564 logical :: change_coordsys
565 real :: x0(3),v0(3)
566 real, allocatable :: xpts(:),ypts(:),zpts(:)
567 character(len=lenunitslabel) :: labelt,labelc
568 !
569 ! array with one value for each column
570 !
571 real(kind=doub_prec) :: coltemp(maxplot), vals(maxplot), rmsval(maxplot)
572 real :: coltemps(maxplot)
573
574 labelt = ''
575 labelc = ''
576 if (iRescale) labelt = unitslabel(0)
577
578 nfilesread = nfilesread + 1
579 if (time_was_read(time)) then
580 timei = time
581 print "(/,5('-'),a,', TIME=',es9.2,a,' FILE #',i5,/)",&
582 '> CALCULATING '//trim(ucase(analysistype)),time,trim(labelt),nfilesread
583 else
584 timei = 0.
585 print "(/,5('-'),a,', FILE #',i5,' (TIME NOT READ)'/)",&
586 '> CALCULATING '//trim(ucase(analysistype)),nfilesread
587 endif
588
589 change_coordsys = (icoordsnew /= icoords .and. ndim > 0 .and. all(ix(1:ndim) > 0))
590 x0 = xorigin(:) ! note that it is not currently possible to do splash to ascii
591 v0 = 0. ! with coords set relative to a tracked particle, so just use xorigin
592 ! instead, one can use the --origin flag to make positions and velocities relative to a particle
593
594 if (itracks(1) > 0) then
595 itrack = itracks(1) ! override particle id saved to splash.defaults file if --tracks specified
596 else
597 itrack = get_tracked_particle(track_string,npartoftype,iamtype,dat,irho)
598 endif
599 if (itrack==0) itrack = 1
600
601 select case(trim(analysistype))
602 case('energy','energies')
603 ekin = 0.
604 emag = 0.
605 epot = 0.
606 etherm = 0.
607 etot = 0.
608 xmom = 0.
609 angmom = 0.
610 nused = 0
611 do i=1,ntot
612 itype = igettype(i)
613 if (iplotpartoftype(itype)) then
614 pmassi = particlemass(i,itype)
615
616 !--kinetic energy
617 if (ivx > 0 .and. ivx+ndimV-1 <= ncolumns) then
618 vi(:) = 0.
619 vi(1:ndimV) = dat(i,ivx:ivx+ndimV-1)
620
621 ekin = ekin + pmassi*dot_product(vi,vi)
622
623 !--linear momentum
624 xmom = xmom + pmassi*vi
625
626 !--angular momentum
627 if (ndim >= 1 .and. all(ix(1:ndim) > 0)) then
628 ri(:) = 0.
629 ri(1:ndim) = dat(i,ix(1):ix(ndim))
630 call cross_product3D(ri,vi,angmomi)
631 angmom(:) = angmom(:) + pmassi*angmomi(:)
632 endif
633 endif
634
635 !--thermal energy
636 if (iutherm > 0 .and. iutherm <= ncolumns) then
637 etherm = etherm + pmassi*dat(i,iutherm)
638 endif
639
640 !--magnetic energy
641 if (iBfirst > 0 .and. iBfirst+ndimV-1 <= ncolumns) then
642 emag = emag + pmassi*dot_product(dat(i,iBfirst:iBfirst+ndimV-1),&
643 dat(i,iBfirst:iBfirst+ndimV-1))/dat(i,irho)
644 endif
645 nused = nused + 1
646 endif
647 enddo
648 ekin = 0.5*ekin
649 emag = 0.5*emag
650 etot = ekin + etherm + epot + emag
651 totmom = sqrt(dot_product(xmom(1:ndimV),xmom(1:ndimV)))
652 totang = sqrt(dot_product(angmom,angmom))
653
654 print "(7(/,1x,a6,' = ',es9.2))",'etot',etot,'ekin',ekin,'etherm',etherm,'epot',epot,'emag',emag,'totmom',totmom,'totang',totang
655 !
656 !--write line to output file
657 !
658 write(iunit,"(64(es18.10,1x))") timei,ekin,etherm,emag,epot,etot,totmom,totang
659 if (nused /= ntot) print*,'energies calculated using ',nused,' of ',ntot,' particles'
660
661 case('massaboverho')
662 massaboverho(:) = 0.
663 if (irho > 0 .and. irho <= ncolumns) then
664 !
665 !--warn if particle masses not found
666 !
667 if (ipmass <= 0 .or. ipmass > ncolumns .and. all(massoftype < tiny(massoftype))) then
668 print "(a)",' WARNING in massaboverho analysis!'// &
669 ' masses not read or are zero from dump file'
670 endif
671 !
672 !--calculate mass above each density threshold
673 !
674 do i=1,ntot
675 itype = igettype(i)
676 pmassi = particlemass(i,itype)
677
678 if (itype==1) then
679 !
680 !--gas particles contribute if they are above rho
681 !
682 where(dat(i,irho) >= rholevels(1:nlevels))
683 massaboverho(1:nlevels) = massaboverho(1:nlevels) + pmassi
684 end where
685 elseif (labeltype(itype)=='sink') then
686 !
687 !--sink particles always contribute (ie. they are assumed to
688 ! be above every density threshold)
689 !
690 massaboverho(1:nlevels) = massaboverho(1:nlevels) + pmassi
691 endif
692 enddo
693
694 !
695 !--write output to screen/terminal
696 !
697 do i=1,nlevels
698 print "(1x,'M(rho > ',es9.2,') = ',es9.2)",rholevels(i),massaboverho(i)
699 enddo
700 !
701 !--write line to output file
702 !
703 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) nlevels+1
704 write(iunit,fmtstring) timei,massaboverho(1:nlevels)
705
706 else
707 print "(a)",' ERROR in massaboverho analysis!'// &
708 ' either mass or density not found in dump file'
709 return
710 endif
711 case('max','maxvals')
712 !
713 !--calculate maximum for each column
714 !
715 coltemp(:) = -huge(0.d0) !maxval(dat(1:ntot,i))
716 nused = 0
717 do j=1,ntot
718 itype = igettype(j)
719 if (iplotpartoftype(itype)) then
720 vals(1:ncolumns) = real(dat(j,1:ncolumns),kind=doub_prec)
721 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
722 nused = nused + 1
723 do i=1,ncolumns
724 coltemp(i) = max(coltemp(i),vals(i))
725 enddo
726 endif
727 enddo
728 where (coltemp(:) < -0.5*huge(0.)) coltemp(:) = 0.
729 !
730 !--write output to screen/terminal
731 !
732 do i=1,ncolumns
733 print "(1x,a20,'max = ',es18.10)",label(i),coltemp(i)
734 enddo
735 !
736 !--write line to output file
737 !
738 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) ncolumns+1
739 write(iunit,fmtstring) timei,coltemp(1:ncolumns)
740 if (nused /= ntot) print*,'max calculated using ',nused,' of ',ntot,' particles'
741
742 case('min','minvals')
743 !
744 !--calculate minimum for each column
745 !
746 coltemp(:) = huge(0.d0) !minval(dat(1:ntot,i))
747 nused = 0
748 do j=1,ntot
749 itype = igettype(j)
750 if (iplotpartoftype(itype)) then
751 vals(1:ncolumns) = real(dat(j,1:ncolumns),kind=doub_prec)
752 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
753 nused = nused + 1
754 do i=1,ncolumns
755 coltemp(i) = min(coltemp(i),vals(i))
756 enddo
757 endif
758 enddo
759 !
760 !--write output to screen/terminal
761 !
762 do i=1,ncolumns
763 print "(1x,a20,'min = ',es18.10)",label(i),coltemp(i)
764 enddo
765 !
766 !--write line to output file
767 !
768 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) ncolumns+1
769 write(iunit,fmtstring) timei,coltemp(1:ncolumns)
770 if (nused /= ntot) print*,'min calculated using ',nused,' of ',ntot,' particles'
771
772 case('diff','diffvals','amp','ampvals','delta','deltavals','deltas','tracks')
773 !
774 !--calculate difference between max and min for each column
775 !
776 lmean(:) = 0.
777 lmin(:) = huge(0.d0)
778 lmax(:) = -huge(0.d0)
779 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) ncolumns+1
780
781 do ifile=1,nfileout
782 if (nfileout > 1) itrack = itracks(ifile)
783 nused = 0
784 coltemp = 0.
785 do j=1,ntot
786 itype = igettype(j)
787 if (iplotpartoftype(itype) .or. j==itrack) then
788 vals(1:ncolumns) = real(dat(j,1:ncolumns),kind=doub_prec)
789 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
790 nused = nused + 1
791 do i=1,ncolumns
792 lmin(i) = min(lmin(i), vals(i))
793 lmax(i) = max(lmax(i), vals(i))
794 lmean(i) = lmean(i) + vals(i)
795 enddo
796 if (j==itrack) coltemp = vals
797 endif
798 enddo
799
800 if (trim(analysistype)=='tracks' .or. trim(analysistype)=='track') then
801 if (ifile==1) then
802 do i=1,ncolumns
803 print "(1x,' particle ',i8,': ',a20,' = ',es18.10)",itrack,label(i),coltemp(i)
804 enddo
805 endif
806 !
807 !--write line to output file
808 !
809 write(iunit+ifile-1,fmtstring) timei,coltemp(1:ncolumns)
810 if (nfileout > 1) print "(1x,'>>> ',a,' <<<')",'written to '//trim(fileout(ifile))
811 endif
812 enddo
813 if (nused > 0) lmean(:) = lmean(:)/real(nused)
814
815 select case(trim(analysistype))
816 case('amp','ampvals')
817 do i=1,ncolumns
818 coltemp(i) = 0.5*(lmax(i) - lmin(i))
819 print "(1x,a20,'0.5*(max - min) = ',es18.10)",label(i),coltemp(i)
820 enddo
821 case('delta','deltavals','deltas')
822 do i=1,ncolumns
823 valmean = real(lmean(i))
824 if (valmean > 0.) then
825 coltemp(i) = 0.5*(lmax(i) - lmin(i))/valmean
826 else
827 coltemp(i) = 0.5*(lmax(i) - lmin(i))
828 endif
829 print "(1x,a20,'0.5*(max - min)/mean = ',es18.10)",label(i),coltemp(i)
830 enddo
831 case('diff','diffvals') ! diff, diffvals
832 do i=1,ncolumns
833 coltemp(i) = lmax(i) - lmin(i)
834 print "(1x,a20,'(max - min) = ',es18.10)",label(i),coltemp(i)
835 enddo
836 end select
837 !
838 !--write line to output file
839 !
840 if (trim(analysistype) /= 'tracks' .and. trim(analysistype) /= 'track') then
841 write(iunit,fmtstring) timei,coltemp(1:ncolumns)
842 endif
843
844 if (nused /= ntot) then
845 select case(trim(analysistype))
846 case('diff', 'diffvals')
847 print*,'diff calculated using ',nused,' of ',ntot,' particles'
848 case('amp','ampvals')
849 print*,'amp calculated using ',nused,' of ',ntot,' particles'
850 case('delta','deltavals','deltas')
851 print*,'deltas calculated using ',nused,' of ',ntot,' particles'
852 end select
853 endif
854
855 case('mean','meanvals')
856 !
857 !--calculate mean for each column
858 !
859 coltemp(:) = 0.
860 nused = 0
861 do j=1,ntot
862 itype = igettype(j)
863 if (iplotpartoftype(itype)) then
864 vals(1:ncolumns) = real(dat(j,1:ncolumns),kind=doub_prec)
865 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
866 nused = nused + 1
867 do i=1,ncolumns
868 coltemp(i) = coltemp(i) + vals(i)
869 enddo
870 endif
871 enddo
872 if (nused > 0) then
873 coltemp(:) = coltemp(:)/real(nused)
874 else
875 coltemp(:) = 0.
876 endif
877 !
878 !--write output to screen/terminal
879 !
880 do i=1,ncolumns
881 print "(1x,a20,'mean = ',es18.10)",label(i),coltemp(i)
882 enddo
883 !
884 !--write line to output file
885 !
886 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) ncolumns+1
887 write(iunit,fmtstring) timei,coltemp(1:ncolumns)
888 if (nused /= ntot) print*,'mean calculated using ',nused,' of ',ntot,' particles'
889
890 case('rms','rmsvals')
891 !
892 !--calculate RMS for each column
893 !
894 coltemp(:) = 0.
895 nused = 0
896 do j=1,ntot
897 itype = igettype(j)
898 if (iplotpartoftype(itype)) then
899 vals(1:ncolumns) = real(dat(j,1:ncolumns),kind=doub_prec)
900 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
901 nused = nused + 1
902 do i=1,ncolumns
903 coltemp(i) = coltemp(i) + vals(i)**2
904 enddo
905 endif
906 enddo
907 if (nused > 0) then
908 coltemp(:) = sqrt(coltemp(:)/real(nused))
909 else
910 coltemp(:) = 0.
911 endif
912 !
913 !--write output to screen/terminal
914 !
915 do i=1,ncolumns
916 print "(1x,a20,'rms (mass weighted) = ',es18.10)",label(i),coltemp(i)
917 enddo
918 !
919 !--write line to output file
920 !
921 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) ncolumns+1
922 write(iunit,fmtstring) timei,coltemp(1:ncolumns)
923 if (nused /= ntot) print*,'rms calculated using ',nused,' of ',ntot,' particles'
924
925 case('vrms','vrmsvals','vwrms','rmsvw')
926 if (irho <= 0 .or. irho > ncolumns) then
927 print "(a)",' ERROR in volume weighted rms calculation!'// &
928 ' density not present / not labelled in dump file, skipping...'
929 return
930 endif
931 !
932 !--warn if particle masses not found
933 !
934 if (ipmass <= 0 .or. ipmass > ncolumns .and. all(massoftype < tiny(massoftype))) then
935 print "(a)",' WARNING in volume weighted rms calculation!'// &
936 ' masses not read or are zero from dump file'
937 endif
938 !
939 !--calculate volume-weighted RMS for each column
940 !
941 rmsval(:) = 0.
942 totvol = 0.
943 do j=1,ntot
944 itype = igettype(j)
945 if (iplotpartoftype(itype)) then
946 vals(1:ncolumns) = real(dat(j,1:ncolumns),kind=doub_prec)
947 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
948
949 pmassi = particlemass(j,itype)
950 rhoi = dat(j,irho)
951 if (rhoi > 0.) then
952 voli = pmassi/rhoi
953 else
954 voli = 0.
955 endif
956
957 do i=1,ncolumns
958 rmsval(i) = rmsval(i) + voli*vals(i)**2
959 enddo
960 totvol = totvol + voli
961 endif
962 enddo
963 coltemp(:) = real(sqrt(rmsval(:)/totvol))
964 print "(1x,a,es9.2)",'volume = ',totvol
965 !
966 !--write output to screen/terminal
967 !
968 do i=1,ncolumns
969 print "(1x,a20,'rms (volume weighted) = ',es18.10)",label(i),coltemp(i)
970 enddo
971 !
972 !--write line to output file
973 !
974 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) ncolumns+1
975 write(iunit,fmtstring) timei,coltemp(1:ncolumns)
976
977 case('rhovar','rhomach')
978 if (irho <= 0 .or. irho > ncolumns) then
979 print "(a)",' ERROR in density variance--rms velocity field calculation!'// &
980 ' density not present / not labelled in dump file, skipping...'
981 return
982 endif
983 !
984 !--warn if particle masses not found
985 !
986 if (ipmass <= 0 .or. ipmass > ncolumns .and. all(massoftype < tiny(massoftype))) then
987 print "(a)",' WARNING in volume weighted rms calculation!'// &
988 ' masses not read or are zero from dump file'
989 endif
990
991 if (ivx <= 0 .or. ivx > ncolumns) then
992 print "(a)",' WARNING in volume weighted rms calculation!'// &
993 ' velocities not present / not labelled in dump file'
994 endif
995 !
996 !--calculate mean density and rms velocity values on first pass
997 !
998 rmsvali = 0.
999 rmsvmw = 0.
1000 rhomeanvw = 0.
1001 rhomeanmw = 0.
1002 totvol = 0.
1003 smeanvw = 0.
1004 smeanmw = 0.
1005 do i=1,ntot
1006 itype = igettype(i)
1007 pmassi = particlemass(i,itype)
1008 rhoi = dat(i,irho)
1009 if (rhoi > 0.) then
1010 voli = pmassi/rhoi
1011 else
1012 voli = 0.
1013 endif
1014 rhomeanmw = rhomeanmw + rhoi
1015 rhomeanvw = rhomeanvw + pmassi
1016 si = log(rhoi)
1017 smeanmw = smeanmw + si
1018 smeanvw = smeanvw + voli*si
1019 totvol = totvol + voli
1020
1021 !
1022 !--mean squared velocity
1023 !
1024 if (ivx > 0 .and. ivx <= ncolumns) then
1025 v2i = dot_product(dat(i,ivx:ivx+ndimV-1),dat(i,ivx:ivx+ndimV-1))
1026 rmsvali = rmsvali + voli*v2i
1027 rmsvmw = rmsvmw + v2i
1028 endif
1029 enddo
1030 !
1031 !--use the computed volume for velocity, otherwise won't be normalised correctly
1032 !
1033 rmsvali = sqrt(rmsvali/totvol)
1034 rmsvmw = sqrt(rmsvmw/dble(ntot))
1035
1036 !
1037 !--option to override volume from sum with environment variable
1038 !
1039 voli = renvironment('SPLASH_CALC_VOLUME',errval=-1.0)
1040 if (voli > 0.) then
1041 print "(1x,a,es9.2)",&
1042 'volume from sum(m/rho) = ',totvol
1043 totvol = voli
1044 print "(1x,a,es9.2,/)",&
1045 '**overridden with SPLASH_CALC_VOLUME = ',totvol
1046 else
1047 print "(1x,a,es9.2,/,1x,a,/)",&
1048 'volume from sum(m/rho) = ',totvol,&
1049 '(override this using SPLASH_CALC_VOLUME environment variable)'
1050 endif
1051
1052 rhomeanmw = rhomeanmw/real(ntot)
1053 rhomeanvw = rhomeanvw/totvol
1054 smeanmw = smeanmw/real(ntot)
1055 smeanvw = smeanvw/totvol
1056 !
1057 !--calculate variance on second pass
1058 !
1059 rhovarvw = 0.
1060 rhovarmw = 0.
1061 svarvw = 0.
1062 svarmw = 0.
1063 totvol = 0.
1064 do i=1,ntot
1065 itype = igettype(i)
1066 pmassi = particlemass(i,itype)
1067 rhoi = dat(i,irho)
1068 if (rhoi > 0.) then
1069 voli = pmassi/rhoi
1070 si = log(rhoi)
1071 else
1072 voli = 0.
1073 si = 0.
1074 endif
1075 totvol = totvol + voli
1076 rhovarmw = rhovarmw + (rhoi - rhomeanmw)**2
1077 rhovarvw = rhovarvw + voli*(rhoi - rhomeanvw)**2
1078 svarmw = svarmw + (si - smeanmw)**2
1079 svarvw = svarvw + voli*(si - smeanvw)**2
1080 enddo
1081 rhovarmw = rhovarmw/real(ntot)
1082 rhovarvw = rhovarvw/totvol
1083 svarmw = svarmw/real(ntot)
1084 svarvw = svarvw/totvol
1085
1086 !
1087 !--write output to screen/terminal
1088 !
1089 print "(1x,'mean density (vol. weighted) = ',es11.4,' +/- ',es11.4)",rhomeanvw,sqrt(rhovarvw)
1090 print "(1x,'mean density (mass weighted) = ',es11.4,' +/- ',es11.4)",rhomeanmw,sqrt(rhovarmw)
1091 print "(1x,'density variance (vol. weighted) = ',es11.4)",rhovarvw
1092 print "(1x,'density variance (mass weighted) = ',es11.4)",rhovarmw
1093 print "(1x,'mean ln density (vol. weighted) = ',es11.4,' +/- ',es11.4)",smeanvw,sqrt(svarvw)
1094 print "(1x,' -0.5*var(ln density) = ',es11.4)",-0.5*svarvw
1095 print "(1x,'mean ln density (mass weighted) = ',es11.4,' +/- ',es11.4)",smeanmw,sqrt(svarmw)
1096 print "(1x,'ln density variance (vol. weighted) = ',es11.4)",svarvw
1097 print "(1x,'ln density variance (mass weighted) = ',es11.4)",svarmw
1098 print "(1x,'rms velocity (vol. weighted) = ',es11.4)",rmsvali
1099 print "(1x,'rms velocity (mass weighted) = ',es11.4)",rmsvmw
1100 if (rmsvali > 0.) then
1101 bval = sqrt(svarvw)/rmsvali
1102 else
1103 bval = 0.
1104 endif
1105 if (rmsvmw > 0.) then
1106 bvalmw = sqrt(svarmw)/rmsvmw
1107 else
1108 bvalmw = 0.
1109 endif
1110 print "(1x,'sqrt(sigma^2/v^2)(vol. weighted) = ',f11.3)",bval
1111 print "(1x,'sqrt(sigma^2/v^2)(mass weighted) = ',f11.3)",bvalmw
1112 !
1113 !--write line to output file
1114 !
1115 write(fmtstring,"('(',i3,'(es18.10,1x))')",iostat=ierr) 17
1116 write(iunit,fmtstring) timei,rhomeanvw,rhomeanmw,rhovarvw,rhovarmw,sqrt(rhovarvw),sqrt(rhovarmw),&
1117 rmsvali,rmsvmw,bval,bvalmw,smeanvw,smeanmw,svarvw,svarmw,sqrt(svarvw),sqrt(svarmw)
1118 case('kh')
1119
1120 if (irho <= 0 .or. irho > ncolumns) then
1121 print "(a)",' ERROR in kh calculation!'// &
1122 ' density not present / not labelled in dump file, skipping...'
1123 return
1124 endif
1125 if (ivx <= 0 .or. ivx > ncolumns) then
1126 print "(a)",' WARNING in kh calculation!'// &
1127 ' velocities not present / not labelled in dump file'
1128 endif
1129 !
1130 !--calculate volume-weighted RMS for each column
1131 !
1132 ekinymax = 0.
1133 do i=1,ntot
1134 ekiny = 0.5*dat(i,irho)*dat(i,ivx+1)**2
1135 ekinymax = max(ekiny,ekinymax)
1136 enddo
1137 print "(1x,a,es9.2)",'ekiny(max) = ',ekinymax
1138 write(iunit,"(2(es18.10,1x))") timei,ekinymax
1139
1140 case('timeaverage','timeav')
1141 if (.not.allocated(datmean)) then
1142 allocate(datmean(ntot,ncolumns),stat=ierr)
1143 if (ierr /= 0) stop 'error allocating memory for mean sum in calc'
1144 datmean = 0.
1145 endif
1146 if (.not.allocated(datvar)) then
1147 allocate(datvar(ntot,ncolumns),stat=ierr)
1148 if (ierr /= 0) stop 'error allocating memory for variance sum in calc'
1149 datvar = 0.
1150 endif
1151 ntot1 = size(datmean(:,1))
1152 if (ntot > ntot1) then
1153 print*,' WARNING: nrows = ',ntot,' > nrows from previous dumpfile =',ntot1
1154 print*,' ignoring all rows/particles greater than ',ntot1
1155 elseif (ntot < ntot1) then
1156 print*,' WARNING: nrows = ',ntot,' < nrows from previous dumpfile =',ntot1
1157 print*,' assuming zeros for rows/particles greater than ',ntot
1158 endif
1159 ncol1 = size(datmean(1,:))
1160 if (ncolumns > ncol1) then
1161 print*,' WARNING: ncolumns = ',ncolumns,' > ncolumns from previous dumpfile =',ncol1
1162 print*,' ignoring all rows/particles greater than ',ncol1
1163 elseif (ncolumns < ncol1) then
1164 print*,' WARNING: ncolumns = ',ntot,' < ncolumns from previous dumpfile =',ncol1
1165 print*,' assuming zeros for columns greater than ',ncolumns
1166 endif
1167 ntot1 = min(ntot1,ntot)
1168 ncol1 = min(ncol1,ncolumns)
1169
1170 dn = 1./real(nfilesread)
1171 !
1172 !--compute the mean and variance using Knuth/Welford's compensated
1173 ! summation algorithm to minimise round-off error
1174 ! (see http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance)
1175 !
1176 do j=1,ncol1
1177 do i=1,ntot1
1178 delta = dat(i,j) - datmean(i,j)
1179 datmean(i,j) = datmean(i,j) + delta*dn
1180 datvar(i,j) = datvar(i,j) + delta*(dat(i,j) - datmean(i,j))
1181 enddo
1182 enddo
1183 return
1184 !sum1(:,:) = sum1(:,:) + dat(1:ntot1,1:ncol1)
1185 !sum2(:,:) = sum2(:,:) + dat(1:ntot1,1:ncol1)**2
1186
1187 case('ratio','minus','add')
1188 if (.not.allocated(datmean)) then
1189 allocate(datmean(size(dat(:,1)),size(dat(1,:))),stat=ierr)
1190 if (ierr /= 0) stop 'error allocating temporary memory in calc'
1191 datmean = 0.
1192 endif
1193 if (.not.allocated(datvar)) then
1194 allocate(datvar(size(dat(:,1)),size(dat(1,:))),stat=ierr)
1195 if (ierr /= 0) stop 'error allocating memory in calc'
1196 datvar = 0.
1197 endif
1198 ntot1 = size(datmean(:,1))
1199 if (ntot > ntot1) then
1200 print*,' WARNING: nrows = ',ntot,' > nrows from previous dumpfile =',ntot1
1201 print*,' ignoring all rows/particles greater than ',ntot1
1202 elseif (ntot < ntot1) then
1203 print*,' WARNING: nrows = ',ntot,' < nrows from previous dumpfile =',ntot1
1204 print*,' assuming zeros for rows/particles greater than ',ntot
1205 endif
1206 ncol1 = size(datmean(1,:))
1207 if (size(dat(1,:)) > ncol1) then
1208 print*,' WARNING: ncolumns = ',ncolumns,' > ncolumns from previous dumpfile =',ncol1
1209 print*,' ignoring all rows/particles greater than ',ncol1
1210 elseif (ncolumns < ncol1) then
1211 print*,' WARNING: ncolumns = ',ntot,' < ncolumns from previous dumpfile =',ncol1
1212 print*,' assuming zeros for columns greater than ',ncolumns
1213 endif
1214 ntot1 = min(ntot1,ntot)
1215 ncol1 = min(ncol1,size(dat(1,:)))
1216 if (ntot1 <= 0 .or. ncol1 <= 0) then
1217 print "(a,i2,a,i2,a)",' ERROR: nrows = ',ntot1,' ncolumns = ',ncol1,' aborting...'
1218 return
1219 endif
1220
1221 if (nfilesread <= 1) then
1222 !--store first dump
1223 datmean(1:ntot1,1:ncol1) = dat(1:ntot1,1:ncol1)
1224 else
1225 select case(trim(analysistype))
1226 case('add')
1227 datvar(1:ntot1,1:ncol1) = dat(1:ntot1,1:ncol1) + datmean(1:ntot1,1:ncol1) ! sum of current data and first step
1228 case('minus')
1229 datvar(1:ntot1,1:ncol1) = datmean(1:ntot1,1:ncol1) - dat(1:ntot1,1:ncol1) ! first step minus current data
1230 case default ! ratio
1231 where (abs(datmean(1:ntot1,1:ncol1)) > epsilon(0.))
1232 datvar(1:ntot1,1:ncol1) = dat(1:ntot1,1:ncol1)/datmean(1:ntot1,1:ncol1) ! ratio of current data to first step
1233 elsewhere
1234 datvar(1:ntot1,1:ncol1) = dat(1:ntot1,1:ncol1)/(datmean(1:ntot1,1:ncol1) + epsilon(0.))
1235 end where
1236 end select
1237 valmin = datvar(1,1)
1238 valmax = datvar(1,1)
1239 valmean = 0.
1240 do j=1,ncol1
1241 do i=1,ntot1
1242 valmin = min(datvar(i,j),valmin)
1243 valmax = max(datvar(i,j),valmin)
1244 valmean = valmean + datvar(i,j)
1245 enddo
1246 enddo
1247 valmean = valmean/real(ntot1*ncol1)
1248 print "(/,a,es10.3)",' max '//trim(analysistype)//' = ',valmax
1249 print "(a,es10.3)",' min '//trim(analysistype)//' = ',valmin
1250 print "(a,es10.3,/)",' mean '//trim(analysistype)//' = ',valmean
1251 print "(a)",'----> WRITING '//trim(analysistype)//'.out ...'
1252 if (allocated(datmean) .and. allocated(datvar)) then
1253 write(iunit,"('# ',i4,1x,i4)") ntot1,ncol1
1254 write(fmtstring,"(a,i6,a)",iostat=ierr) '(',ncol1,'(es14.6,1x,))'
1255 do i=1,ntot1
1256 write(iunit,fmtstring) datvar(i,:)
1257 enddo
1258 endif
1259 endif
1260 return
1261 case('lightcurve')
1262 call get_lightcurve(ncolumns,dat,npartoftype,massoftype,iamtype,ndim,ntypes,&
1263 lum,rphoto,tphoto,l_bb,r_bb,t_bb,badfrac,basename(rootname(ifileopen)),ierr)
1264 print "(4(/,1x,a20,' = ',es9.2))",'Luminosity',lum,'photospheric radius ',rphoto,'photospheric temperature',tphoto
1265 !
1266 !--write line to output file
1267 !
1268 if (ierr == 0) write(iunit,"(8(es18.10,1x))") timei,lum,rphoto,tphoto,l_bb,r_bb,t_bb,badfrac
1269
1270 case('extinction')
1271 !
1272 !--get list of sink particle positions
1273 !
1274 isinktype = get_sink_type(ntypes)
1275 call get_positions_of_type(dat,npartoftype,iamtype,isinktype,ix,npts,xpts,ypts,zpts,ierr)
1276 if (ierr /= 0 .or. npts <= 0) then
1277 print*,' ERROR obtaining sink particle positions, aborting...'
1278 return
1279 endif
1280
1281 call get_extinction(ncolumns,dat,npartoftype,massoftype,iamtype,ndim,ntypes,&
1282 npts,xpts,ypts,zpts,coltemps,irho)
1283
1284 labelc = get_unitlabel_coldens(iRescale,labelzintegration,unitslabel(irho))
1285 print "(100(/,1x,a20,i0,' = ',es9.2,a))",('Sigma to sink ',i,coltemps(i),trim(labelc),i=1,npts)
1286 !
1287 !--write line to output file
1288 !
1289 write(iunit,"(100(es18.10,5x))") timei,coltemps(1:npts)
1290
1291 case('tdiffuse')
1292
1293 icol = ienvironment('SPLASH_ANALYSIS_COL')
1294 if (icol <= 0 .or. icol > ncolumns) then
1295 icol = irho
1296 endif
1297
1298 call get_extinction_los(ncolumns,dat,npartoftype,massoftype,iamtype,ndim,ntypes,&
1299 ndirs,anglex_vals,angley_vals,anglez_vals,coltemps,icol)
1300
1301 labelc = get_unitlabel_coldens(iRescale,labelzintegration,unitslabel(icol))
1302 print "(100(/,1x,a20,' = ',es12.4,a))",('Sigma in '//trim(dir_label(i)),coltemps(i),trim(labelc),i=1,ndirs)
1303 !
1304 !--write line to output file
1305 !
1306 write(iunit,"(100(es18.10,5x))") timei,coltemps(1:ndirs)
1307
1308 case default
1309 print "(a)",' ERROR: unknown analysis type in write_analysis routine'
1310 return
1311 end select
1312
1313 if (nfileout==1) print "(/,1x,'>>> ',a,' <<<')",'written to '//trim(fileout(1))
1314
1315 return
1316
1317 contains
1318 !
1319 !--small internal utility to work out the particle type
1320 ! (which depends whether or not mixed types are stored)
1321 !
1322 integer function igettype(i)
1323 integer :: np
1324 integer, intent(in) :: i
1325
1326 if (size(iamtype) > 1) then
1327 igettype = int(iamtype(i))
1328 else
1329 np = 0
1330 igettype = 0
1331 do while (i > np .and. igettype <= ntypes)
1332 igettype = igettype + 1
1333 np = np + npartoftype(igettype)
1334 enddo
1335 endif
1336
1337 end function igettype
1338 !
1339 !--small internal utility to get particle mass
1340 ! (depends on whether or not mass is stored for each particle
1341 ! or only for each type)
1342 !
1343 real function particlemass(i,iparttype)
1344 integer, intent(in) :: i,iparttype
1345
1346 if (ipmass > 0 .and. ipmass <= ncolumns) then
1347 particlemass = dat(i,ipmass)
1348 else
1349 particlemass = massoftype(iparttype)
1350 endif
1351
1352 end function particlemass
1353
1354 end subroutine write_analysis
1355
1356 !---------------------
1357 ! close output file
1358 !---------------------
1359 subroutine close_analysis(analysistype)
1360 character(len=*), intent(in) :: analysistype
1361 integer :: i
1362
1363 select case(trim(analysistype))
1364 case('timeaverage','timeav')
1365 print "(a)",'----> WRITING time_average.out ...'
1366 if (allocated(datmean) .and. allocated(datvar) .and. nfilesread > 0) then
1367 !--get standard deviation from variance (also normalise with 1/n)
1368 datvar(:,:) = sqrt(datvar(:,:))/sqrt(real(nfilesread))
1369 do i=1,size(datmean(:,1))
1370 write(iunit,"(1x,99(es15.7,2x))") datmean(i,:),datvar(i,:)
1371 enddo
1372 endif
1373 end select
1374
1375 if (allocated(datmean)) deallocate(datmean)
1376 if (allocated(datvar)) deallocate(datvar)
1377
1378 do i=1,nfileout
1379 close(iunit+i-1)
1380 enddo
1381
1382 return
1383 end subroutine close_analysis
1384
1385 end module analysis