Debian Codegraph @

src/calc_quantities.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 ! This module allows arbitrary functions to be computed from the
24 ! particle data. Uses the fparser module to parse the functions.
25 !
26 !-----------------------------------------------------------------
27 module calcquantities
28 use labels, only:lenlabel,lenunitslabel
29 use params, only:maxplot,maxhdr
30 implicit none
31 public :: calc_quantities,setup_calculated_quantities
32 public :: calc_quantities_use_x0,get_calc_data_dependencies
33 public :: print_example_quantities
34
35 integer, parameter, private :: maxcalc = 35
36 character(len=lenlabel), dimension(maxcalc) :: calcstring = ' '
37 character(len=lenlabel), dimension(maxcalc) :: calclabel = ' '
38 character(len=lenunitslabel), dimension(maxcalc) :: calcunitslabel = ' '
39
40 integer, parameter, private :: lenvars = 20 ! max length for any variable
41 integer, parameter, private :: nextravars = 5
42 character(len=lenvars), dimension(nextravars), parameter, private :: &
43 extravars=(/'t ','gamma','x0 ','y0 ','z0 '/)
44
45 namelist /calcopts/ calcstring,calclabel,calcunitslabel
46
47 public :: calcopts,calcstring,calclabel,calcunitslabel
48 private
49
50 contains
51
52 !-----------------------------------------------------------------
53 !
54 ! Allow the user to edit the list of function strings used
55 ! to compute additional quantities from the particle data
56 !
57 !-----------------------------------------------------------------
58 subroutine setup_calculated_quantities(ncalc)
59 use settings_data, only:ncolumns
60 use prompting, only:prompt
61 integer, intent(inout) :: ncalc
62 integer :: ncalctot,istart,iend,ipick,ninactive, i
63 logical :: done,first
64 character(len=1) :: charp
65 integer, dimension(maxcalc) :: incolumn
66 ipick = ncolumns + 1
67
68 done = .false.
69 first = .true.
70 !
71 !--on the first call to setup, prefill the list of calculated
72 ! quantities with ALL of the valid examples.
73 !
74 if (ncalc==0) call print_example_quantities(.true.,ncalc)
75 charp = 'a'
76 calcmenu: do while (.not.done)
77 call check_calculated_quantities(ncalc,ncalctot,incolumn,verbose=.true.)
78 ninactive = ncalctot - ncalc
79
80 iend = maxcalc
81 if (ncalctot > 0 .or. .not.first) then
82 charp='q' !'a'
83 if (.not.first) charp = 'q'
84 print*
85 call prompt(' a)dd to, e)dit, c)lear current list, or q)uit/finish? ',&
86 charp,list=(/'a','e','c','q','s','S','Q'/),noblank=.true.)
87 select case(charp)
88 case('a')
89 istart = ncalctot
90 iend = ncalctot+1
91 case('e')
92 if (ninactive > 0 .and. ncalc > 0) then
93 call prompt(' pick a function to edit ',ipick,-ninactive,-1,ncolumns+1,ncolumns+ncalc)
94 elseif (ncalc > 0) then
95 call prompt(' pick a function to edit ',ipick,ncolumns+1,ncolumns+ncalc)
96 endif
97 istart = 0
98 do i=1,ncalctot
99 if (incolumn(i)==ipick) istart = i - 1
100 enddo
101 iend = istart + 1
102 case('c')
103 calcstring(:) = ' '
104 calclabel(:) = ' '
105 calcunitslabel(:) = ' '
106 first = .false.
107 cycle calcmenu
108 case('q','Q','s','S')
109 done = .true.
110 case default
111 istart = 0
112 iend = maxcalc
113 end select
114 else
115 istart = 0
116 iend = 1
117 endif
118
119 if (.not.done) call add_calculated_quantities(istart,iend,ncalc,first,incolumn,verbose=.true.)
120 first = .false.
121 enddo calcmenu
122
123 if (ncalc > 0) then
124 if (ncalc < 10) then
125 print "(a,i1,a)",' setup ',ncalc,' additional quantities'
126 else
127 print "(a,i2,a)",' setup ',ncalc,' additional quantities'
128 endif
129 endif
130
131 end subroutine setup_calculated_quantities
132
133 !-----------------------------------------------------------------
134 !
135 ! utility (private) to add one or more calculated quantities
136 ! to the current list and/or edit previous settings
137 !
138 !-----------------------------------------------------------------
139 subroutine add_calculated_quantities(istart,iend,ncalc,printhelp,incolumn,verbose)
140 use prompting, only:prompt
141 use fparser, only:checkf
142 use settings_data, only:ncolumns,iRescale,required
143 use labels, only:shortstring,headertags,count_non_blank
144 integer, intent(in) :: istart,iend
145 integer, intent(out) :: ncalc
146 logical, intent(in) :: printhelp
147 integer, dimension(maxcalc), intent(in) :: incolumn
148 logical, intent(in) :: verbose
149 integer :: i,j,ntries,ierr,iequal,nvars,nhdr
150 logical :: iask
151 character(len=120) :: string
152 character(len=lenvars), dimension(maxplot+nextravars+maxhdr) :: vars
153
154 i = istart + 1
155 ntries = 0
156 ncalc = istart
157
158 if (i > maxcalc) then
159 print "(/,a,i2,a)",' *** Error, maximum number of calculated quantities (',maxcalc,') reached, cannot add any more.'
160 print "(a)", ' *** If you hit this limit, *please email me* so I can change the default limits!'
161 print "(a)", ' *** (and then edit calc_quantities.f90, changing the parameter "maxcalc" to something higher...)'
162 return
163 endif
164
165 if (printhelp) then
166 print "(/,a)",' Special characters and spaces removed from labels.'
167 print "(a)",' Previous quantities can be used in subsequent calculations.'
168 print "(/,10(a))",' Valid variables: column labels',(', '''//trim(extravars(j))//'''',j=1,nextravars),&
169 ' and header variables:'
170 nhdr = count_non_blank(headertags)
171 if (nhdr > 0) print "(43(2x,4(a32,1x),/))",headertags(1:nhdr)
172 endif
173 call print_example_quantities(verbose)
174
175 overfuncs: do while(ntries < 3 .and. i <= iend .and. i <= maxcalc)
176 if (len_trim(calcstring(i)) /= 0 .or. ncalc > istart) then
177 if (incolumn(i) > 0) then
178 write(*,"(a,i2,a)") '[Column ',incolumn(i),']'
179 else
180 write(*,"(a)") '[Currently inactive]'
181 endif
182 endif
183
184 if (len_trim(calclabel(i)) > 0) then
185 string = trim(calclabel(i))//' = '//trim(calcstring(i))
186 else
187 string = trim(calcstring(i))
188 endif
189 call prompt('Enter function string to calculate (blank for none) ',string)
190 if (len_trim(string)==0) then
191 !
192 !--if editing list and get blank string at prompt,
193 ! remove the entry and shuffle the list appropriately
194 !
195 do j=i,maxcalc-1
196 !print*,' shuffling ',j,' = '//trim(calcstring(j+1))
197 calcstring(j) = trim(calcstring(j+1))
198 if (j+1 < size(calclabel)) then
199 calclabel(j) = calclabel(j+1)
200 endif
201 if (len_trim(calcstring(j))==0) then
202 if (j==i) then
203 exit overfuncs
204 else
205 cycle overfuncs
206 endif
207 endif
208 enddo
209 else
210 !
211 !--set label from what lies to the left of the equal sign
212 ! and the calcstring from what lies to the right
213 ! (if no equals sign, then prompt later for the label)
214 !
215 iequal = index(string,'=')
216 call splitstring(string,calclabel(i),calcstring(i))
217 !
218 !--fill variable array with the list of valid variable
219 ! names for this column
220 !
221 call get_variables(ncolumns+i-1,nvars,vars)
222 !
223 !--check for errors parsing function
224 !
225 ierr = checkf(shortstring(calcstring(i)),vars(1:nvars))
226 if (ierr /= 0 ) then
227 ntries = ntries + 1
228 print "(a,i1,a)",' error parsing function string: try again (',ntries,' of 3)'
229 if (ntries==3) then
230 iask = .false.
231 call prompt(' Cannot parse function (after 3 attempts). Set as inactive function anyway?',iask)
232 if (iask) then
233 ierr = 0
234 else
235 calcstring(i) = ' '
236 endif
237 endif
238 else
239 write(*,"(a)",advance='no') 'Function parses OK: '
240 required(ncolumns+i) = .true.
241 endif
242 if (ierr==0) then
243 !
244 !--prompt for label if not set
245 !
246 if (iequal==0) then
247 call prompt(' Enter label for this quantity ',calclabel(i),noblank=.true.)
248 endif
249 if (iRescale) then
250 call prompt(' Enter units label for this quantity ',calcunitslabel(i))
251 endif
252 !print "(a,a,i2,/)",'Setting '//trim(calclabel(i)), &
253 ! ' = '//trim(calcstring(i))//' in column ',ncolumns+i
254 ncalc = i
255 i = i + 1
256 endif
257 endif
258 enddo overfuncs
259
260 end subroutine add_calculated_quantities
261
262 !---------------------------------------------------------------------
263 !
264 ! utility to split input string into label and function at the
265 ! equals sign
266 !
267 !---------------------------------------------------------------------
268 subroutine splitstring(string,calclabel,calcstring)
269 character(len=*), intent(in) :: string
270 character(len=*), intent(inout) :: calclabel
271 character(len=*), intent(out) :: calcstring
272 integer :: iequal
273
274 iequal = index(string,'=')
275 if (iequal /= 0) then
276 calclabel = string(1:iequal-1)
277 calcstring = string(iequal+1:len_trim(string))
278 else
279 calcstring = trim(string)
280 endif
281
282 !--remove preceding spaces
283 calcstring = trim(adjustl(calcstring))
284
285 end subroutine splitstring
286
287 !---------------------------------------------------------------------
288 !
289 ! utility to give a nice list of examples to follow / cut and paste
290 ! [ this basically replaces what was hardwired into the
291 ! old calc_quantities routine ]
292 !
293 !---------------------------------------------------------------------
294 subroutine print_example_quantities(verbose,ncalc)
295 use labels, only:label,unitslabel,shortlabel,lenlabel,irho,iutherm,ipr,iBfirst,&
296 ix,icv,idivB,ih,iradenergy,iamvec,labelvec,idustfrac,&
297 ideltav,ivx,headertags
298 use settings_data, only:ncolumns,ndim,icoordsnew,ndimV
299 use geometry, only:labelcoord
300 use asciiutils, only:append_number,find_repeated_tags
301 use particle_data, only:headervals
302 use filenames, only:ifileopen
303 logical :: verbose
304 integer, intent(inout), optional :: ncalc
305 logical :: prefill
306 character(len=lenlabel) :: string,ldfracsum
307 integer :: i,j,ivecstart,ierr,ilen,idustfrac1,ndusttypes,nc
308 logical :: gotpmag,gotpressure
309
310 gotpmag = .false.
311 gotpressure = .false.
312 prefill = .false.
313 if (present(ncalc)) then
314 prefill = .true.
315 nc = ncalc
316 else
317 nc = 0
318 endif
319
320 if (verbose) then
321 if (prefill) then
322 print "(/,a)",' Prefilling list with useful quantities from current data...'
323 else
324 print "(/,a)",' e.g. '
325 endif
326 endif
327
328 !--radius
329 string = ' '
330 if (ndim > 0 .and. icoordsnew==1 .and. ncolumns >= ndim) then
331 write(string,"(a)") 'r = sqrt(('// &
332 trim(shortlabel(label(ix(1)),unitslabel(ix(1))))//'-'//trim(labelcoord(1,1))//'0)**2'
333 ilen = len_trim(string)
334 if (ndim > 1) then
335 write(string(ilen+1:),"(a,a,a)",iostat=ierr) &
336 (' + ('//trim(shortlabel(label(ix(i)),unitslabel(ix(i))))// &
337 '-'//trim(labelcoord(i,1))//'0)**2',i=2,ndim),')'
338 else
339 write(string(ilen+1:),"(a)") ')'
340 endif
341 call print_or_prefill(prefill,string,nc,ulab=unitslabel(ix(1)))
342 elseif (ncolumns >= 2 .and. .not.prefill) then
343 !--if ndim=0 give random example to give at least one
344 print "(11x,a)",trim(shortlabel(label(1),unitslabel(1)))//'*'&
345 //trim(shortlabel(label(2),unitslabel(2)))
346 endif
347
348 !
349 !--total dust fraction if multiple dust phases
350 !
351 ldfracsum = ' '
352 if (idustfrac == 0) then
353 ! find number of dust species from how many times the label "dustfrac"
354 ! is repeated. For gas-only calculations, this will give ndusttypes=0
355 call find_repeated_tags('dustfrac',ncolumns,label,idustfrac1,ndusttypes)
356 if (ndusttypes > 1) then
357 string = 'dustfrac = '//trim(shortlabel(label(idustfrac1),unitslabel(idustfrac1)))
358 do i=idustfrac1+1,idustfrac1+ndusttypes-1
359 string = trim(string)//'+'//(trim(shortlabel(label(i),unitslabel(i))))
360 enddo
361 ldfracsum = 'dustfrac'
362 call print_or_prefill(prefill,string,nc)
363 endif
364 elseif (idustfrac > 0) then
365 ndusttypes = 1
366 ldfracsum = shortlabel(label(idustfrac),unitslabel(idustfrac))
367 endif
368
369 !--pressure (requires total dust fraction)
370 string = ' '
371 if (irho > 0 .and. iutherm > 0) then
372 gotpressure = .true.
373 if (len_trim(ldfracsum) > 0) then
374 write(string,"(a)",iostat=ierr) &
375 'pressure = (gamma-1)*(1 - '//trim(ldfracsum)//')*' &
376 //trim(shortlabel(label(irho),unitslabel(irho)))// &
377 '*'//trim(shortlabel(label(iutherm),unitslabel(iutherm)))
378 else
379 write(string,"(a)",iostat=ierr) &
380 'pressure = (gamma-1)*'//trim(shortlabel(label(irho),unitslabel(irho)))// &
381 '*'//trim(shortlabel(label(iutherm),unitslabel(iutherm)))
382 endif
383 if (index(unitslabel(irho),'g/cm^3') > 0 .and. &
384 index(unitslabel(iutherm),'erg/g') > 0) then
385 call print_or_prefill(prefill,string,nc,ulab=' [erg/cm^3]')
386 else
387 call print_or_prefill(prefill,string,nc)
388 endif
389 endif
390 !
391 !--one-fluid dust stuff
392 !
393 if (len_trim(ldfracsum) > 0 .and. irho > 0) then
394 string = ' '
395 !--gas density
396 write(string,"(a)",iostat=ierr) '\rho_{g} = ' &
397 //trim(shortlabel(label(irho),unitslabel(irho))) &
398 //'*(1 - '//trim(ldfracsum)//')'
399 call print_or_prefill(prefill,string,nc,ulab=unitslabel(irho))
400
401 !--(total) dust density
402 write(string,"(a)",iostat=ierr) '\rho_{d} = ' &
403 //trim(shortlabel(label(irho),unitslabel(irho))) &
404 //'*'//trim(ldfracsum)
405 call print_or_prefill(prefill,string,nc,ulab=unitslabel(irho))
406
407 !--densities of each individual dust phase
408 if (ndusttypes > 1) then
409 do i = idustfrac1,idustfrac1+ndusttypes-1
410 string = '\rho_{d,'
411 if (ifileopen > 0) call append_grain_size_label(string,i-idustfrac1+1,headertags,headervals(:,ifileopen),ierr)
412 if (ierr /= 0) call append_number(string,i-idustfrac1+1)
413 string = trim(string)//'} = ' &
414 //trim(shortlabel(label(irho),unitslabel(irho)))//'*'//trim(label(i))
415 call print_or_prefill(prefill,string,nc,ulab=unitslabel(irho))
416 enddo
417 endif
418 !--dust-to-gas ratio
419 write(string,"(a)",iostat=ierr) 'dust-to-gas ratio = ' &
420 //trim(ldfracsum)//'/(1. - '//trim(ldfracsum)//')'
421 call print_or_prefill(prefill,string,nc)
422
423 if (ideltav > 0 .and. ivx > 0 .and. ndimV > 0) then
424 if (ndusttypes==1) then
425 !--gas velocities
426 do i=1,ndimV
427 write(string,"(a)",iostat=ierr) trim(labelvec(ivx))//'_{gas,'//trim(labelcoord(i,icoordsnew))//'} = ' &
428 //trim(shortlabel(label(ivx + i-1),unitslabel(ivx + i-1))) &
429 //' - '//trim(shortlabel(label(idustfrac),unitslabel(idustfrac))) &
430 //'*'//trim(shortlabel(label(ideltav + i-1),unitslabel(ideltav + i-1)))
431 call print_or_prefill(prefill,string,nc,ulab=unitslabel(ivx))
432 enddo
433 !--dust velocities
434 do i=1,ndimV
435 write(string,"(a)",iostat=ierr) trim(labelvec(ivx))//'_{dust,'//trim(labelcoord(i,icoordsnew))//'} = ' &
436 //trim(shortlabel(label(ivx + i-1),unitslabel(ivx + i-1))) &
437 //' + (1 - '//trim(shortlabel(label(idustfrac),unitslabel(idustfrac))) &
438 //')*'//trim(shortlabel(label(ideltav + i-1),unitslabel(ideltav + i-1)))
439 call print_or_prefill(prefill,string,nc,ulab=unitslabel(ivx))
440 enddo
441 else
442 ! Still needs to be implemented...
443 endif
444 endif
445 endif
446
447 !
448 !--magnitudes of all vector quantities (only if cartesian coords are set)
449 !
450 ivecstart = 0
451 if (icoordsnew==1 .and. ndim > 0 .and. ndimV > 0) then
452 do i=1,ncolumns
453 if (iamvec(i) > 0 .and. iamvec(i) <= ncolumns .and. iamvec(i) /= ivecstart) then
454 ivecstart = iamvec(i)
455 string = ' '
456 write(string,"(a)",iostat=ierr) '|'//trim(labelvec(ivecstart))//'| '// &
457 '= sqrt('//trim(shortlabel(label(ivecstart),unitslabel(ivecstart)))//'**2'
458 ilen = len_trim(string)
459 if (ndimV > 1) then
460 write(string(ilen+1:),"(a,a,a)",iostat=ierr) &
461 (' + '//trim(shortlabel(label(j),unitslabel(j)))//'**2',&
462 j=ivecstart+1,ivecstart+ndimV-1),')'
463 else
464 write(string(ilen+1:),"(a)",iostat=ierr) ')'
465 endif
466 call print_or_prefill(prefill,string,nc,ulab=unitslabel(ivecstart))
467 endif
468 enddo
469 endif
470 !--magnetic pressure
471 string = ' '
472 if (ndim > 0 .and. ndimV > 0 .and. iBfirst > 0 .and. icoordsnew==1) then
473 gotpmag = .true.
474 write(string,"(a)",iostat=ierr) &
475 'P_{mag} = ('//trim(shortlabel(label(iBfirst),unitslabel(iBfirst)))//'**2'
476 ilen = len_trim(string)
477 if (ndimV > 1) then
478 write(string(ilen+1:),"(a,a,a)",iostat=ierr) &
479 (' + '//trim(shortlabel(label(i),unitslabel(i))) &
480 //'**2',i=iBfirst+1,iBfirst+ndimV-1),')/(2*mu)'
481 else
482 write(string(ilen+1:),"(a)",iostat=ierr) ')'
483 endif
484 call print_or_prefill(prefill,string,nc)
485 endif
486 !--h*div B / B
487 if (ndim > 0 .and. ndimV > 0 .and. ih > 0 .and. iBfirst > 0 .and. &
488 icoordsnew==1 .and. idivB > 0) then
489 write(string,"(a)",iostat=ierr) &
490 'h|div B|/|B| = '//trim(shortlabel(label(ih),unitslabel(ih)))//'*abs(' &
491 //trim(shortlabel(label(idivB),unitslabel(idivB)))//')/' &
492 //'sqrt(('//trim(shortlabel(label(iBfirst),unitslabel(iBfirst)))//'^2'
493 ilen = len_trim(string)
494 if (ndimV > 1) then
495 write(string(ilen+1:),"(a,a,a)",iostat=ierr) &
496 (' + '//trim(shortlabel(label(i),unitslabel(i)))//'^2',i=iBfirst+1,iBfirst+ndimV-1),'))'
497 else
498 write(string(ilen+1:),"(a)",iostat=ierr) '))'
499 endif
500 call print_or_prefill(prefill,string,nc)
501 endif
502 !--Plasma beta
503 if (ndim > 0 .and. ndimV > 0 .and. iBfirst > 0 .and. gotpmag .and. (gotpressure .or. ipr > 0)) then
504 write(string,"(a)",iostat=ierr) 'plasma \beta = pressure/P_mag'
505 call print_or_prefill(prefill,string,nc,&
506 comment='[ assuming pressure and Pmag calculated ]')
507 endif
508
509 !--gas temperature if cv present
510 if (ndim > 0 .and. iutherm > 0 .and. icv > 0) then
511 string = ' '
512 write(string,"(a)",iostat=ierr) 'T_{gas} = '//trim(shortlabel(label(iutherm),unitslabel(iutherm)))//'/' &
513 //trim(shortlabel(label(icv),unitslabel(icv)))
514 call print_or_prefill(prefill,string,nc,ulab=' [K]')
515 endif
516 !--radiation temperature
517 if (ndim > 0 .and. irho > 0 .and. iradenergy > 0) then
518 string = ' '
519 write(string,"(a)",iostat=ierr) 'T_{rad} = ('//trim(shortlabel(label(irho),unitslabel(irho)))//'*' &
520 //trim(shortlabel(label(iradenergy),unitslabel(iradenergy)))//'/7.5646e-15)**0.25'
521 call print_or_prefill(prefill,string,nc,ulab=' [K]')
522 endif
523
524 if (present(ncalc)) ncalc = nc
525 if (.not.prefill) print "(a)"
526
527 end subroutine print_example_quantities
528
529 !-----------------------------------------------------------------
530 !
531 ! utility (private) to append enumerated grainsize as a label
532 !
533 !-----------------------------------------------------------------
534 subroutine append_grain_size_label(string,idust,tags,vals,ierr)
535 use labels, only:get_label_grain_size,count_non_blank
536 character(len=*), intent(inout) :: string
537 integer, intent(in) :: idust
538 character(len=*), intent(in) :: tags(:)
539 real, intent(in) :: vals(:)
540 integer, intent(out) :: ierr
541 integer :: ntags,nd,i
542
543 ntags = count_non_blank(tags)
544 nd = 0
545 ierr = 1
546 do i=1,ntags
547 if (index(tags(i),'grainsize') > 0) then
548 nd = nd + 1
549 if (nd==idust) then
550 ierr = 0
551 string = trim(string)//get_label_grain_size(vals(i))
552 endif
553 endif
554 enddo
555
556 end subroutine append_grain_size_label
557
558 !-----------------------------------------------------------------
559 !
560 ! utility (private) to get mass of a particular grain species
561 ! from the header tags
562 !
563 !-----------------------------------------------------------------
564 real function get_mass_of_species(string,idust,tags,vals,ierr)
565 use labels, only:count_non_blank
566 character(len=*), intent(inout) :: string
567 integer, intent(in) :: idust
568 character(len=*), intent(in) :: tags(:)
569 real, intent(in) :: vals(:)
570 integer, intent(out) :: ierr
571 integer :: ntags,nd,i
572
573 get_mass_of_species = 0.
574 ntags = count_non_blank(tags)
575 nd = 0
576 ierr = 1
577 do i=1,ntags
578 if (index(tags(i),'mdust_in') > 0) then
579 nd = nd + 1
580 if (nd==idust) then
581 ierr = 0
582 get_mass_of_species = vals(i)
583 endif
584 endif
585 enddo
586
587 end function get_mass_of_species
588
589 !-----------------------------------------------------------------
590 !
591 ! utility (private) to either print the example quantity or
592 ! add it to a prefilled list of calculated quantities
593 !
594 !-----------------------------------------------------------------
595 subroutine print_or_prefill(prefill,string,nc,comment,ulab)
596 logical, intent(in) :: prefill
597 character(len=*), intent(in) :: string
598 integer, intent(inout) :: nc
599 character(len=*), intent(in), optional :: comment,ulab
600 logical :: already_used
601 integer :: i
602
603 if (prefill) then
604 if (len_trim(string) > 0) then ! do not prefill blank strings
605 nc = nc + 1
606 call splitstring(string,calclabel(nc),calcstring(nc))
607 if (present(ulab)) calcunitslabel(nc) = trim(ulab)
608 endif
609 else
610 ! do not print strings already in the list
611 already_used = .false.
612 do i=1,size(calcstring)
613 if (trim(calclabel(i))//' = '//trim(calcstring(i))==trim(string)) already_used = .true.
614 enddo
615 ! append comment if present
616 if (present(comment)) then
617 if (.not.already_used) print "(6x,a)",trim(string)//' [ '//trim(comment)//' ]'
618 else
619 if (.not.already_used) print "(6x,a)",trim(string)
620 endif
621 endif
622
623 end subroutine print_or_prefill
624
625 !-----------------------------------------------------------------
626 !
627 ! utility (private) to print the current list of calculated
628 ! quantities, checking that they parse correctly
629 !
630 !-----------------------------------------------------------------
631 subroutine check_calculated_quantities(ncalcok,ncalctot,incolumn,verbose)
632 use settings_data, only:ncolumns,iRescale
633 use fparser, only:checkf
634 use labels, only:label,unitslabel,shortstring,irhodust_start,irhodust_end
635 integer, intent(out) :: ncalcok,ncalctot
636 integer, dimension(maxcalc), intent(out), optional :: incolumn
637 logical, intent(in), optional :: verbose
638 integer :: i,ierr,nvars,indexinactive
639 character(len=lenvars), dimension(maxplot+nextravars+maxhdr) :: vars
640 logical :: isverbose
641
642 if (present(verbose)) then
643 isverbose = verbose
644 else
645 isverbose = .true.
646 endif
647
648 ncalcok = 0
649 ncalctot = 0
650 indexinactive = 0
651 i = 1
652 if (present(incolumn)) incolumn(:) = 0
653
654 if (all(len_trim(calcstring(:))==0)) return
655 if (isverbose) print "(/,a)", ' Current list of calculated quantities:'
656 do while(i <= maxcalc .and. len_trim(calcstring(i)) /= 0)
657 !
658 !--get the list of valid variable names for this column
659 !
660 call get_variables(ncolumns+ncalcok,nvars,vars)
661 !
662 !--check that the function parses
663 !
664 ierr = checkf(shortstring(calcstring(i)),vars(1:nvars),Verbose=.false.)
665 if (ierr==0) then
666 ncalcok = ncalcok + 1
667 if (present(incolumn)) incolumn(i) = ncolumns + ncalcok
668 !
669 !--set the label for the proposed column here
670 ! so that subsequent calculations can use this variable
671 ! (note that we don't need to set the units label here
672 ! as this is done in the actual calc_quantities call)
673 !
674 label(ncolumns+ncalcok) = trim(calclabel(i))
675 unitslabel(ncolumns+ncalcok) = trim(calcunitslabel(i))
676 if (iRescale) label(ncolumns+ncalcok) = trim(label(ncolumns+ncalcok))//trim(unitslabel(ncolumns+ncalcok))
677 if (isverbose) then
678 if (iRescale) then
679 print "(1x,i2,') ',a50,' [OK]',1x,a)",ncolumns+ncalcok,trim(calclabel(i))//' = '//calcstring(i),trim(calcunitslabel(i))
680 else
681 print "(1x,i2,') ',a50,' [OK]')",ncolumns+ncalcok,trim(calclabel(i))//' = '//calcstring(i)
682 endif
683 endif
684 !
685 !--recognise the dust density in the list of calculated quantities
686 !
687 if (trim(calcstring(i))=='density*dustfrac1') irhodust_start = ncolumns+ncalcok
688 if (calcstring(i)(1:16)=='density*dustfrac') irhodust_end = ncolumns+ncalcok ! overwrite until last density*dustfrac
689 else
690 indexinactive = indexinactive - 1
691 if (isverbose) then
692 print "(i3,') ',a50,' [INACTIVE]')",indexinactive,trim(calclabel(i))//' = '//calcstring(i)
693 endif
694 if (present(incolumn)) incolumn(i) = indexinactive
695 endif
696 ncalctot = i
697 i = i + 1
698 enddo
699 ! handle the case of only one dust species, where only irhodust_end has been set
700 if (irhodust_end > 0 .and. irhodust_start == 0) irhodust_start = irhodust_end
701 if (ncalcok==0 .and. isverbose) print "(a)",' (none)'
702
703 end subroutine check_calculated_quantities
704
705 !-----------------------------------------------------------------
706 !
707 ! utility (private) to check dependencies of calculated
708 ! quantities, so that we only read what is necessary
709 ! from the dump file
710 !
711 !-----------------------------------------------------------------
712 subroutine get_calc_data_dependencies(required)
713 use params, only:maxplot
714 use settings_data, only:debugmode
715 use fparser, only:checkf
716 use labels, only:label,shortlabel,shortstring
717 logical, dimension(0:maxplot), intent(inout) :: required
718 character(len=lenvars), dimension(maxplot+nextravars+maxhdr) :: vars
719 integer, dimension(maxcalc) :: incolumn
720 integer :: ncalcok,ncalctot,nvars,i,j
721
722 call check_calculated_quantities(ncalcok,ncalctot,incolumn,verbose=.false.)
723
724 do i=ncalctot,1,-1 ! go in REVERSE order to get recursive dependencies properly
725 if (incolumn(i) > 0) then
726 if (required(incolumn(i))) then
727 if (debugmode) then
728 print*,'DEBUG: computing dependencies for '//trim(label(incolumn(i)))//&
729 ' = '//trim(shortstring(calcstring(i)))
730 endif
731 !
732 !--get the list of valid variable names for this column
733 !
734 call get_variables(incolumn(i),nvars,vars)
735 !
736 !--check if the string contains any preceding variables
737 !
738 do j=1,incolumn(i)-1
739 !
740 !--this could be smarter here (at the moment we just check for
741 ! matching substrings, but we should check for the use of the
742 ! string as an actual variable -- this is mainly an issue for
743 ! single letter variables like x,y,z etc)
744 !
745 if (index(shortlabel(calcstring(i)),trim(vars(j))) /= 0) then
746 if (debugmode) print*,'DEBUG: -> depends on '//trim(label(j))
747 required(j) = .true.
748 endif
749 enddo
750 endif
751 endif
752 enddo
753
754 end subroutine get_calc_data_dependencies
755
756 !-----------------------------------------------------------------
757 !
758 ! actually compute the extra quantities from the particle data
759 !
760 !-----------------------------------------------------------------
761 subroutine calc_quantities(ifromstep,itostep,dontcalculate)
762 use labels, only:label,unitslabel,labelvec,iamvec,ix,ivx,irho,shortstring, &
763 count_non_blank,headertags
764 use particle_data, only:dat,npartoftype,gamma,time,headervals,maxpart,maxstep,maxcol,iamtype
765 use settings_data, only:ncolumns,ncalc,iRescale,xorigin,debugmode,ndim,required,iverbose, &
766 icoords,icoordsnew,ipartialread,track_string
767 use mem_allocation, only:alloc
768 use settings_units, only:units,units_calc
769 use fparser, only:checkf,parsef,evalf,EvalerrMsg,EvalErrType,rn,initf,endf
770 use params, only:maxplot
771 use timing, only:wall_time,print_time
772 use geomutils, only:change_coords
773 use part_utils, only:get_tracked_particle
774 integer, intent(in) :: ifromstep, itostep
775 logical, intent(in), optional :: dontcalculate
776 integer :: i,j,ncolsnew,ierr,icalc,ntoti,nvars,ncalctot,nused,itrackpart
777 integer :: ndust,nhdr
778 logical :: skip
779 ! real, parameter :: mhonkb = 1.6733e-24/1.38e-16
780 ! real, parameter :: radconst = 7.5646e-15
781 ! real, parameter :: lightspeed = 3.e10 ! in cm/s (cgs)
782 real(kind=rn), dimension(maxplot+nextravars+maxhdr) :: vals,unitvals
783 character(len=lenvars), dimension(maxplot+nextravars+maxhdr) :: vars
784 real, dimension(3) :: x0,v0
785 real :: t1,t2
786
787 !
788 !--allow dummy call to set labels without actually calculating stuff
789 !
790 if (present(dontcalculate)) then
791 skip = dontcalculate
792 else
793 skip = .false.
794 endif
795
796 ierr = 0
797 ncalc = 0
798 call check_calculated_quantities(ncalc,ncalctot,verbose=(.not.skip .and. iverbose > 0))
799
800 if (.not.skip .and. ncalc > 0) then
801 nused = 0
802 if (.not.ipartialread) then
803 !
804 !--need to be careful if data file has been read fully
805 ! as in this case we also assume all calculated quantities
806 ! have been done. So need to make sure that all quantities
807 ! *are* actually calculated in this case.
808 !
809 required(:) = .true.
810 endif
811 do i=1,ncalc
812 if (required(ncolumns+i)) nused = nused + 1
813 enddo
814 if (iverbose > 0) print "(2(a,i2),a,/)",' Calculating ',nused,' of ',ncalctot,' additional quantities...'
815 endif
816 ncolsnew = ncolumns + ncalc
817 if (ncolsnew > maxcol) call alloc(maxpart,maxstep,ncolsnew)
818
819 !
820 !--reset iamvec to zero for calculated columns
821 !
822 iamvec(ncolumns+1:ncolsnew) = 0
823 labelvec(ncolumns+1:ncolsnew) = ' '
824
825 !
826 !--evaluate functions in turn
827 !
828 if (.not.skip .and. ncalc > 0) then
829 call initf(ncalc)
830 !
831 !--compile each function into bytecode
832 !
833 icalc = 1
834 do i=1,maxcalc
835 if (icalc <= ncalc) then
836
837 !
838 !--get the list of valid variable names for this column
839 !
840 call get_variables(ncolumns+icalc-1,nvars,vars)
841 !
842 !--now actually parse the function
843 !
844 call parsef(icalc,shortstring(calcstring(i)),vars(1:nvars),err=ierr,Verbose=.false.)
845 if (ierr==0) then
846 icalc = icalc + 1
847 endif
848 endif
849 enddo
850 !
851 !--evaluate functions from particle data
852 !
853 call wall_time(t1)
854 do i=ifromstep,itostep
855 ntoti = SUM(npartoftype(:,i))
856 ndust = npartoftype(2,i)
857 !
858 !--set origin position
859 !
860 v0(:) = 0.
861 itrackpart = get_tracked_particle(track_string,npartoftype(:,i),iamtype(:,i),dat(:,:,i),irho)
862 if (itrackpart > 0 .and. itrackpart <= ntoti) then
863 x0(:) = 0.
864 if (ix(1) > 0 .and. ix(1) <= ncolumns) then
865 x0(1) = dat(itrackpart,ix(1),i)
866 else
867 print*,'** internal error: tracking particle set but cannot locate x coordinate'
868 endif
869 if (ix(2) > 0 .and. ix(2) <= ncolumns) x0(2) = dat(itrackpart,ix(2),i)
870 if (ix(3) > 0 .and. ix(3) <= ncolumns) x0(3) = dat(itrackpart,ix(3),i)
871 if (i==ifromstep) then
872 print "(a,i10)",' using position of tracked particle ',itrackpart
873 print "(a,3(e11.3),/)",' (x0,y0,z0) = ',dat(itrackpart,ix(1:ndim),i)
874 endif
875 if (ivx > 0 .and. ivx+ndim-1 <= ncolumns) then
876 v0(1:ndim) = dat(itrackpart,ivx:ivx+ndim-1,i)
877 endif
878 else
879 x0(:) = xorigin(:)
880 endif
881
882 do icalc=1,ncalc
883 if (required(ncolumns+icalc)) then
884 if (debugmode) print*,'DEBUG: ',icalc,' calculating '//trim(label(ncolumns+icalc))
885 !
886 !--additional settings allowed in function evaluations
887 ! i.e., time and gamma from dump file and current origin settings
888 ! make sure the number here aligns with the "nextravars" setting
889 !
890 vals(ncolumns+icalc) = time(i)
891 vals(ncolumns+icalc+1) = gamma(i)
892 vals(ncolumns+icalc+2) = x0(1)
893 vals(ncolumns+icalc+3) = x0(2)
894 vals(ncolumns+icalc+4) = x0(3)
895 if (iRescale) then
896 unitvals(ncolumns+icalc) = units(0)
897 unitvals(ncolumns+icalc+1) = 1.
898 unitvals(ncolumns+icalc+2) = units(ix(1))
899 unitvals(ncolumns+icalc+3) = units(ix(2))
900 unitvals(ncolumns+icalc+4) = units(ix(3))
901 endif
902 nhdr = count_non_blank(headertags)
903 do j=1,nhdr
904 vals(ncolumns+icalc+4+j) = headervals(j,i)
905 if (iRescale) unitvals(ncolumns+icalc+4+j) = 1.
906 enddo
907 if (icoordsnew /= icoords .and. ndim > 0 .and. all(ix(1:ndim) > 0)) then
908 !
909 !--if alternative coordinate system is in use, then we need to apply
910 ! the coordinate transformations to the data BEFORE using it
911 ! to calculate additional quantities
912 !
913 do j=1,ntoti
914 vals(1:ncolumns+icalc-1) = dat(j,1:ncolumns+icalc-1,i)
915 call change_coords(vals(1:ncolumns+icalc-1),ncolumns+icalc-1,&
916 ndim,icoords,icoordsnew,x0(1:ndim),v0(1:ndim))
917 !--evaluate function with transformed values
918 dat(j,ncolumns+icalc,i) = real(evalf(icalc,vals(1:ncolumns+icalc+nextravars+nhdr-1)))
919 enddo
920 ! evaluate units of the new function
921 if (iRescale) then
922 unitvals(1:ncolumns+icalc-1) = units(1:ncolumns+icalc-1)
923 call change_coords(unitvals(1:ncolumns+icalc-1),ncolumns+icalc-1,&
924 ndim,icoords,icoordsnew,x0(1:ndim),v0(1:ndim))
925 ! DP: see comment below
926 units_calc(ncolumns+icalc) = units(ncolumns+icalc) !real(evalf(icalc,unitvals(1:ncolumns+icalc+nextravars+nhdr-1)))
927 endif
928 else
929 !!$omp parallel do default(none) private(j,vals,icolumn) shared(dat,i,icalc,ncolumns)
930 do j=1,ntoti
931 vals(1:ncolumns+icalc-1) = dat(j,1:ncolumns+icalc-1,i)
932 dat(j,ncolumns+icalc,i) = real(evalf(icalc,vals(1:ncolumns+icalc+nextravars+nhdr-1)))
933 enddo
934 if (iRescale) then
935 unitvals(1:ncolumns+icalc-1) = units(1:ncolumns+icalc-1)
936 !
937 ! DP: ideally we would fix the line below to compute the exact solution unit scaling directly
938 ! for an arbitrary function string, but we have to take out the additions and
939 ! subtractions, so would have to parse a reduced function, not the original function
940 !
941 ! For now, this is done manually by recognising the dimensionality of certain computed quantities
942 ! in identify_calculated_quantities
943 !
944 units_calc(ncolumns+icalc) = units(ncolumns+icalc) !real(evalf(icalc,unitvals(1:ncolumns+icalc+nextravars+nhdr-1)))
945 endif
946 !!$omp end parallel do
947 endif
948 if (EvalErrType /= 0) then
949 print "(a)",' ERRORS evaluating '//trim(calcstring(icalc))//': ' &
950 //trim(EvalerrMsg())
951 endif
952 !
953 !--identify calculated quantities based on the label
954 !
955 if (i==ifromstep) then
956 call identify_calculated_quantity(label(ncolumns+icalc),ncolumns,ncolumns+icalc)
957 endif
958 else
959 if (debugmode) print*,'DEBUG: ',icalc,' skipping '//trim(label(ncolumns+icalc))//' (not required)'
960 endif
961 enddo
962 enddo
963 call endf
964 call wall_time(t2)
965 if (t2-t1 > 1.) call print_time(t2-t1)
966 endif
967 !
968 !--override units of calculated quantities if necessary
969 !
970 if (iRescale .and. any(abs(units(ncolumns+1:ncolumns+ncalc)-1.0) > tiny(0.)) &
971 .and. .not.skip) then
972 !write(*,"(/a)") ' rescaling data...'
973 do i=ncolumns+1,ncolumns+ncalc
974 if (abs(units(i)-1.0) > tiny(0.) .and. abs(units(i)) > tiny(0.)) then
975 dat(:,i,ifromstep:itostep) = dat(:,i,ifromstep:itostep)*units(i)
976 endif
977 if (index(label(i),trim(unitslabel(i)))==0) label(i) = trim(label(i))//trim(unitslabel(i))
978 enddo
979 elseif (iRescale) then
980 do i=ncolumns+1,ncolumns+ncalc
981 if (index(label(i),trim(unitslabel(i)))==0) label(i) = trim(label(i))//trim(unitslabel(i))
982 if (debugmode) print*,'DEBUG: column ',i,trim(label(i)),' units_calc = ',units_calc(i)
983 enddo
984 endif
985
986 return
987 end subroutine calc_quantities
988
989 !-----------------------------------------------------------------
990 !
991 ! utility (private) to internally identify a calculated quantity
992 ! so that the relevant exact solutions can be plotted for that
993 ! quantity. This is mainly just the radius, but can include
994 ! other things also.
995 !
996 !-----------------------------------------------------------------
997 subroutine identify_calculated_quantity(labelcol,ncolumns,icolumn)
998 use asciiutils, only:lcase
999 use labels, only:irad,ike,ipr,ikappa,itemp,label_synonym,ix,ivx
1000 use settings_data, only:debugmode,iRescale
1001 use settings_units,only:units_calc,units
1002 character(len=*), intent(in) :: labelcol
1003 integer, intent(in) :: ncolumns,icolumn
1004 !
1005 !--identify quantities based on the label
1006 ! only do this if the location flags are not already set
1007 ! (e.g. in the data read) - but DO overwrite if they
1008 ! are calculated quantities as the locations can change
1009 !
1010 select case(label_synonym(labelcol))
1011 case('r','radius','rad')
1012 call assign_column(irad,icolumn,ncolumns,debugmode,'radius')
1013 if (iRescale .and. ix(1) > 0) units_calc(icolumn) = units(ix(1))
1014 case('kinetic energy','ke','1/2 v^2','v^2/2')
1015 call assign_column(ike,icolumn,ncolumns,debugmode,'kinetic energy')
1016 if (iRescale .and. ivx > 0) units_calc(icolumn) = units(ivx)**2
1017 case('pressure','pr','p')
1018 call assign_column(ipr,icolumn,ncolumns,debugmode,'pressure')
1019 case('kappa','opacity')
1020 call assign_column(ikappa,icolumn,ncolumns,debugmode,'opacity')
1021 case('temperature','temp')
1022 call assign_column(itemp,icolumn,ncolumns,debugmode,'temperature')
1023 end select
1024
1025 end subroutine identify_calculated_quantity
1026
1027 !-----------------------------------------------------------------
1028 !
1029 ! helper routine for above
1030 !
1031 !-----------------------------------------------------------------
1032 subroutine assign_column(i,icolumn,ncolumns,debugmode,string)
1033 integer, intent(inout) :: i
1034 integer, intent(in) :: icolumn,ncolumns
1035 logical, intent(in) :: debugmode
1036 character(len=*), intent(in) :: string
1037
1038 if (i <= 0 .or. i > ncolumns) then
1039 i = icolumn
1040 if (debugmode) print "(1x,a,i2,a)",'identifying column ',icolumn,' as the '//trim(string)
1041 endif
1042
1043 end subroutine assign_column
1044
1045 !-----------------------------------------------------------------
1046 !
1047 ! utility (private) to fill the array of variable names
1048 !
1049 !-----------------------------------------------------------------
1050 subroutine get_variables(maxlabel,nvars,variables)
1051 use labels, only:label,shortlabel,unitslabel,headertags,count_non_blank
1052 integer, intent(in) :: maxlabel
1053 integer, intent(out) :: nvars
1054 character(len=*), dimension(:), intent(out) :: variables
1055 integer :: i,nheader
1056 !
1057 !--can use column labels up to the previous quantity calculated
1058 !
1059 variables(:) = ' '
1060 nvars = maxlabel + nextravars
1061
1062 do i=1,maxlabel
1063 variables(i) = trim(adjustl(shortlabel(label(i),unitslabel(i))))
1064 enddo
1065 do i=1,nextravars
1066 variables(maxlabel+i) = trim(extravars(i))
1067 enddo
1068 nheader = count_non_blank(headertags)
1069 nvars = nvars + nheader
1070 do i=1,nheader
1071 variables(maxlabel+nextravars+i) = trim(headertags(i))
1072 enddo
1073
1074 end subroutine get_variables
1075
1076 !-----------------------------------------------------------------
1077 !
1078 ! utility (public) to query whether or not the origin position
1079 ! is actually used in the currently set quantities
1080 !
1081 !-----------------------------------------------------------------
1082 logical function calc_quantities_use_x0()
1083 integer :: i
1084
1085 calc_quantities_use_x0 = .false.
1086 do i=1,maxcalc
1087 if (index(calcstring(i),trim(extravars(3))) > 0) calc_quantities_use_x0 = .true.
1088 if (index(calcstring(i),trim(extravars(4))) > 0) calc_quantities_use_x0 = .true.
1089 if (index(calcstring(i),trim(extravars(5))) > 0) calc_quantities_use_x0 = .true.
1090 enddo
1091
1092 end function calc_quantities_use_x0
1093
1094 end module calcquantities