Debian Codegraph @

src/units.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2018 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !--------------------------------------------------------------------
24 ! module containing subroutines to do with setting of physical units
25 !--------------------------------------------------------------------
26 module settings_units
27 use params
28 use labels, only:unitslabel,unitslabel_default,lenlabel,&
29 labelzintegration,labelzintegration_default
30 implicit none
31 real, dimension(0:maxplot), public :: units,units_default,units_old,units_calc
32 real, public :: unitzintegration,unitzintegration_default
33 real(doub_prec), public :: unit_interp
34 public :: set_units,read_unitsfile,write_unitsfile,defaults_set_units
35 public :: get_nearest_length_unit,get_nearest_time_unit
36 public :: get_nearest_mass_unit,get_nearest_velocity_unit
37
38 integer, parameter :: nx = 9
39 real(doub_prec), parameter :: unit_length(nx) = &
40 (/1.d0, &
41 1.d2, &
42 2.01168d4, &
43 1.d5, &
44 6.96d10, &
45 1.496d13,&
46 3.086d18,&
47 3.086d21,&
48 3.086d24/)
49
50 character(len=*), parameter :: unit_labels_length(nx) = &
51 (/' [cm] ',&
52 ' [m] ',&
53 ' [furlong]',&
54 ' [km] ',&
55 ' [R_{Sun}]',&
56 ' [au] ',&
57 ' [pc] ',&
58 ' [kpc] ',&
59 ' [Mpc] '/)
60
61 integer, parameter :: nt = 7
62 real(doub_prec), parameter :: unit_time(nt) = &
63 (/1.d-3, &
64 1.d0, &
65 3.6d3, &
66 8.64d4, &
67 3.15568926d7, &
68 3.15568926d13,&
69 3.15568926d16/)
70
71 character(len=*), parameter :: unit_labels_time(nt) = &
72 (/' ms ',&
73 ' s ',&
74 ' hrs ',&
75 ' days',&
76 ' yrs ',&
77 ' Myr ',&
78 ' Gyr '/)
79
80 integer, parameter :: nv = 3
81 real(doub_prec), parameter :: unit_vel(nv) = &
82 (/1.d0, &
83 1.d2, &
84 1.d5/)
85
86 character(len=*), parameter :: unit_labels_vel(nv) = &
87 (/' [cm/s] ',&
88 ' [m/s] ',&
89 ' [km/s] '/)
90
91 integer, parameter :: nm = 6
92 real(doub_prec), parameter :: unit_mass(nm) = &
93 (/1.d0, &
94 1.d3, &
95 8.958d23, & ! Ceres mass
96 5.979d27, & ! Earth mass
97 1.898d30, & ! Jupiter mass
98 1.989d33/) ! Solar mass
99
100 character(len=*), parameter :: unit_labels_mass(nm) = &
101 (/' [g] ',&
102 ' [kg] ',&
103 ' [M_{Ceres}] ',&
104 ' [M_{Earth}] ',&
105 ' [M_{Jup}] ',&
106 ' [M_{Sun}] '/)
107
108 private
109
110 contains
111 !---------------------------------------------------------------
112 !
113 ! initialise the units arrays to some harmless default values
114 !
115 !---------------------------------------------------------------
116 subroutine defaults_set_units
117
118 units(:) = 1.0
119 units_default(:) = 1.0
120 unitzintegration = 1.0
121 unitzintegration_default = 1.0
122 unit_interp = 1.0d0
123 unitslabel(:) = ' '
124 unitslabel_default(:) = ' '
125 labelzintegration = ' '
126 labelzintegration_default = ' '
127
128 end subroutine defaults_set_units
129
130 !-------------------------------------------
131 !
132 ! find the nearest 'sensible' length unit
133 !
134 !-------------------------------------------
135 subroutine get_nearest_length_unit(udist,unit,unitlabel)
136 real(doub_prec), intent(in) :: udist
137 real(doub_prec), intent(out) :: unit
138 character(len=*), intent(out) :: unitlabel
139
140 call get_nearest_unit(nx,unit_length,unit_labels_length,udist,unit,unitlabel)
141
142 end subroutine get_nearest_length_unit
143
144 !-------------------------------------------
145 !
146 ! find the nearest 'sensible' time unit
147 !
148 !-------------------------------------------
149 subroutine get_nearest_time_unit(utime,unit,unitlabel)
150 real(doub_prec), intent(in) :: utime
151 real(doub_prec), intent(out) :: unit
152 character(len=*), intent(out) :: unitlabel
153
154 call get_nearest_unit(nt,unit_time,unit_labels_time,utime,unit,unitlabel)
155
156 end subroutine get_nearest_time_unit
157
158 !-------------------------------------------
159 !
160 ! find the nearest 'sensible' velocity unit
161 !
162 !-------------------------------------------
163 subroutine get_nearest_velocity_unit(uvel,unit,unitlabel)
164 real(doub_prec), intent(in) :: uvel
165 real(doub_prec), intent(out) :: unit
166 character(len=*), intent(out) :: unitlabel
167
168 call get_nearest_unit(nv,unit_vel,unit_labels_vel,uvel,unit,unitlabel)
169
170 end subroutine get_nearest_velocity_unit
171
172 !-------------------------------------------
173 !
174 ! find the nearest 'sensible' mass unit
175 !
176 !-------------------------------------------
177 subroutine get_nearest_mass_unit(umass,unit,unitlabel)
178 real(doub_prec), intent(in) :: umass
179 real(doub_prec), intent(out) :: unit
180 character(len=*), intent(out) :: unitlabel
181
182 call get_nearest_unit(nm,unit_mass,unit_labels_mass,umass,unit,unitlabel)
183
184 end subroutine get_nearest_mass_unit
185
186 !-------------------------------------------------------
187 !
188 ! find the nearest unit from a list of possible units
189 !
190 !-------------------------------------------------------
191 subroutine get_nearest_unit(nu,units,unit_labels,unit_in,unit_out,unitlabel_out)
192 integer, intent(in) :: nu
193 real(doub_prec), intent(in) :: units(nu),unit_in
194 character(len=*), intent(in) :: unit_labels(nu)
195 real(doub_prec), intent(out) :: unit_out
196 character(len=*), intent(out) :: unitlabel_out
197 real(doub_prec) :: err,erri
198 integer :: i
199
200 err = huge(err)
201 do i = 1,nu
202 ! find nearest unit in log space
203 erri = abs(log10(unit_in)-log10(units(i)))
204 if (erri < err) then
205 unit_out = unit_in/units(i)
206 unitlabel_out = unit_labels(i)
207 err = erri
208 endif
209 enddo
210
211 end subroutine get_nearest_unit
212
213 !-------------------------------------------------------
214 !
215 ! set units
216 !
217 !-------------------------------------------------------
218 subroutine set_units(ncolumns,numplot,UnitsHaveChanged)
219 use prompting, only:prompt
220 use labels, only:label,ix,ih,ivx,ipmass,idivB,iamvec,labelvec,headertags,strip_units
221 use settings_data, only:ndim,ndimV,ivegotdata
222 use particle_data, only:headervals,maxstep
223 use asciiutils, only:get_value
224 integer, intent(in) :: ncolumns,numplot
225 logical, intent(out) :: UnitsHaveChanged
226 integer :: icol,ibs,ibc
227 real(doub_prec) :: unitsprev
228 real(doub_prec) :: udist,utime,umass
229 logical :: applytoall,got_label
230 character(len=lenlabel) :: mylabel
231
232 icol = 1
233 ! try to extract code units from the file headers, these only exist in some data reads
234 utime = 0.d0
235 udist = 0.d0
236 umass = 0.d0
237 if (maxstep > 0 .and. ivegotdata) then
238 utime = get_value('utime',headertags,headervals(:,1))
239 udist = get_value('udist',headertags,headervals(:,1))
240 umass = get_value('umass',headertags,headervals(:,1))
241 endif
242
243 do while(icol >= 0)
244 icol = -1
245 call prompt('enter column to change units (-2=reset all,-1=quit,0=time)',icol,-2,numplot)
246 if (icol >= 0) then
247 unitsprev = units(icol)
248 if (icol > ncolumns) then
249 print "(a)",' WARNING: calculated quantities are automatically calculated in physical units '
250 print "(a)",' this means that units set here will be re-scalings of these physical values'
251 endif
252 got_label = .true.
253 if (icol==0 .and. utime > 0.) then
254 ! give hints for possible time units, if utime is read from data file
255 call choose_unit_from_list(units(icol),unitslabel(icol),&
256 nt,unit_labels_time,unit_time,utime,'time')
257 elseif (any(ix==icol) .or. icol==ih .and. udist > 0.) then
258 ! give hints for possible length units, if udist is read from data file
259 call choose_unit_from_list(units(icol),unitslabel(icol),&
260 nx,unit_labels_length,unit_length,udist,'length')
261 elseif (ivx==icol .and. udist > 0. .and. utime > 0.) then
262 ! give hints for velocity units, if both udist and utime read from data file
263 call choose_unit_from_list(units(icol),unitslabel(icol),&
264 nv,unit_labels_vel,unit_vel,udist/utime,'velocity')
265 elseif (ipmass==icol .and. umass > 0.) then
266 ! give hints for mass units, if umass read from data file
267 call choose_unit_from_list(units(icol),unitslabel(icol),&
268 nm,unit_labels_mass,unit_mass,umass,'mass')
269 elseif (icol > 0) then
270 mylabel = strip_units(label(icol),unitslabel(icol))
271 call prompt('enter '//trim(mylabel)//' units (new=old*units)',units(icol))
272 got_label = .false.
273 else ! icol = 0
274 call prompt('enter time units (new=old*units)',units(icol))
275 got_label = .false.
276 endif
277
278 if (abs(units(icol)) > tiny(units)) then ! sanity check the units
279 if (abs(units(icol) - unitsprev) > tiny(units)) UnitsHaveChanged = .true.
280
281 !--prompt for the units label
282 if (.not.got_label) then ! skip if label set in choose_unit_from_list
283 !--suggest a label amendment if none already set
284 if (len_trim(unitslabel(icol))==0) &
285 call suggest_units_label(units(icol), unitslabel(icol))
286
287 call prompt('enter label amendment ',unitslabel(icol))
288 endif
289 else
290 UnitsHaveChanged = .true.
291 units(icol) = 1.0
292 unitslabel(icol) = ' '
293 endif
294 if (UnitsHaveChanged .and. icol > 0) then
295 !
296 !--prompt to apply same units to coordinates and h for consistency
297 !
298 if (any(ix(1:ndim)==icol) .or. (icol==ih .and. ih > 0)) then
299 applytoall = .true.
300 !--try to make prompts apply to whichever situation we have
301 if (ndim==1) then
302 if (icol==ix(1) .and. ih > 0) then
303 call prompt(' Apply these units to h?',applytoall)
304 else
305 call prompt(' Apply these units to '//trim(label(ix(1)))//'?',applytoall)
306 endif
307 elseif (any(ix(1:ndim)==icol) .and. ih > 0) then
308 call prompt(' Apply these units to all coordinates and h?',applytoall)
309 else
310 call prompt(' Apply these units to all coordinates?',applytoall)
311 endif
312 if (applytoall) then
313 units(ix(1:ndim)) = units(icol)
314 unitslabel(ix(1:ndim)) = unitslabel(icol)
315 if (ih > 0) then
316 if (idivb > 0) then
317 ! amend units of div B and curl B
318 ibs = index(unitslabel(idivB),'/')
319 ibc = index(unitslabel(icol),'[')
320 units(idivB:idivB+3) = units(idivB:idivB+3)*units(ih)/units(icol)
321 unitslabel(idivB:idivB+3) = unitslabel(idivB)(1:ibs)//unitslabel(icol)(ibc+1:)
322 endif
323 units(ih) = units(icol)
324 unitslabel(ih) = unitslabel(icol)
325 endif
326 endif
327 !
328 !--set units for z integration in 3D
329 ! so for example can have x,y,z in kpc but column density in g/cm^2
330 !
331 if (abs(unitzintegration-1.0) <= tiny(unitzintegration)) then
332 unitzintegration = units(icol)
333 labelzintegration = unitslabel(icol)
334 endif
335 if (ndim==3) then
336 call prompt(' Enter unit for ''z'' in 3D column integrated plots ',unitzintegration)
337 call prompt(' Enter label for z integration unit (e.g. [cm])',labelzintegration)
338 endif
339 endif
340 !
341 !--also sensible to apply same units to all components of a vector
342 !
343 if (ndimV > 1 .and. iamvec(icol) > 0) then
344 applytoall = .true.
345 call prompt(' Apply these units to all components of '//trim(labelvec(icol))//'?',applytoall)
346 if (applytoall) then
347 where (iamvec(1:ncolumns)==iamvec(icol))
348 units(1:ncolumns) = units(icol)
349 unitslabel(1:ncolumns) = unitslabel(icol)
350 end where
351 endif
352 endif
353 endif
354
355 elseif (icol==-2) then
356 UnitsHaveChanged = .true.
357 print "(/a)",' resetting all units to unity...'
358 units = 1.0
359 unitslabel = ' '
360 unitzintegration = 1.0
361 labelzintegration = ' '
362 endif
363 print*
364 enddo
365
366 end subroutine set_units
367
368 !-------------------------------------------------------
369 !
370 ! select unit from a sensible list of presets
371 !
372 !-------------------------------------------------------
373 subroutine choose_unit_from_list(unit,unitlabel,n,unit_labels,unit_vals,unit_code,tag)
374 use prompting, only:prompt
375 integer, intent(in) :: n
376 real, intent(inout) :: unit
377 character(len=*), intent(inout) :: unitlabel
378 real(doub_prec), intent(in) :: unit_code,unit_vals(n)
379 character(len=*), intent(in) :: unit_labels(n),tag
380 integer :: i,iselect
381 real :: unitsprev
382
383 iselect = n+1
384 ! get default value of iselect if one of the units is already chosen
385 do i=1,n
386 if (abs(unit-unit_code/unit_vals(i)) < tiny(0.)) iselect = i
387 enddo
388 do i=1,n
389 print "(i2,')',a,' ( x ',1pg11.4,')')",i,unit_labels(i),unit_code/unit_vals(i)
390 enddo
391 print "(i2,') custom')",n+1
392 call prompt('Enter choice of '//trim(tag)//' unit ',iselect,1,n+1)
393 if (iselect==n+1) then
394 unitsprev = unit
395 call prompt('Enter custom unit (new=old*unit)',unit)
396 if (unit_code > 0.d0) call suggest_label(unitsprev,unit,unit_code, &
397 unit_vals,unit_labels,unitlabel)
398 call prompt('enter label amendment ',unitlabel)
399 else
400 unit = real(unit_code/unit_vals(iselect))
401 unitlabel = unit_labels(iselect)
402 print "(a,1pg11.4)", ' => '//trim(tag)//' unit is now '//trim(unitlabel)//&
403 ', i.e. '//trim(tag)//' = '//trim(tag)//'_code * ',unit
404 endif
405
406 end subroutine choose_unit_from_list
407
408 !-------------------------------------------------------
409 !
410 ! suggest the units label based on matching list
411 ! of known scalings
412 !
413 !-------------------------------------------------------
414 subroutine suggest_label(unitsprev,unit,ucode,unit_suggest,unit_label_suggest,unitlabel)
415 real, intent(in) :: unitsprev,unit
416 real(doub_prec), intent(in) :: ucode,unit_suggest(:)
417 character(len=*), intent(in) :: unit_label_suggest(:)
418 character(len=*), intent(inout) :: unitlabel
419 integer :: i
420
421 ! do nothing if unit has not changed
422 if (abs(unit - unitsprev) < epsilon(0.)) return
423
424 ! suggest corresponding label to the unit chosen from list
425 do i=1,size(unit_suggest)
426 if (abs(unit/(ucode/unit_suggest(i)) - 1.) < 0.01) unitlabel = unit_label_suggest(i)
427 enddo
428 !print*,' suggested unit label = ',unitlabel
429
430 end subroutine suggest_label
431
432 !-------------------------------------------------------
433 !
434 ! suggest sensible units label for an arbitrary scaling
435 !
436 !-------------------------------------------------------
437 subroutine suggest_units_label(unit,label)
438 real, intent(in) :: unit
439 character(len=*), intent(out) :: label
440 real(doub_prec) :: dunits
441
442 dunits = 1./unit
443 if (dunits > 100 .or. dunits < 1.e-1) then
444 write(label,"(1pe8.1)") dunits
445 else
446 write(label,"(f5.1)") dunits
447 endif
448 label = ' [ x '//trim(adjustl(label))//' ]'
449
450 end subroutine suggest_units_label
451
452 !-------------------------------------------------------
453 !
454 ! save units for all columns to a file
455 !
456 !-------------------------------------------------------
457 subroutine write_unitsfile(unitsfile,ncolumns)
458 character(len=*), intent(in) :: unitsfile
459 integer, intent(in) :: ncolumns
460 integer :: i,ierr
461
462 print "(1x,a)",'saving units to '//trim(unitsfile)
463
464 open(unit=77,file=unitsfile,status='replace',form='formatted',iostat=ierr)
465 if (ierr /=0) then
466 print "(1x,a)",'ERROR: cannot write units file'
467 else
468 write(77,*,iostat=ierr) units(0),';',trim(unitslabel(0)),' ;',unitzintegration,';',trim(labelzintegration)
469 do i=1,ncolumns
470 write(77,*,iostat=ierr) units(i),';',trim(unitslabel(i))
471 if (ierr /= 0) then
472 print "(1x,a)",'ERROR whilst writing units file'
473 close(unit=77)
474 return
475 endif
476 enddo
477 endif
478 close(unit=77)
479
480 return
481
482 end subroutine write_unitsfile
483
484 !-------------------------------------------------------
485 !
486 ! read units for all columns from a file
487 !
488 !-------------------------------------------------------
489 subroutine read_unitsfile(unitsfile,ncolumns,ierr,iverbose)
490 use settings_data, only:ndim
491 character(len=*), intent(in) :: unitsfile
492 integer, intent(in) :: ncolumns
493 integer, intent(out) :: ierr
494 integer, intent(in), optional :: iverbose
495 character(len=2*len(unitslabel)+40) :: line
496 integer :: i,itemp,isemicolon,isemicolon2,isemicolon3,isverbose
497 logical :: ierrzunits,iexist
498
499 if (present(iverbose)) then
500 isverbose= iverbose
501 else
502 isverbose = 1
503 endif
504
505 ierr = 0
506 ierrzunits = .false.
507 inquire(file=unitsfile,exist=iexist)
508 if (.not.iexist) then
509 if (isverbose > 1) print "(1x,a)",trim(unitsfile)//' not found'
510 ierr = 1
511 return
512 endif
513
514 open(unit=78,file=unitsfile,status='old',form='formatted',err=997)
515 if (isverbose > 0) print "(a)",' read '//trim(unitsfile)
516 do i=0,maxplot ! read all units possibly present in file
517 !
518 ! read a line from the file
519 !
520 read(78,"(a)",err=998,end=999) line
521 !
522 ! now get units from the first part of the line
523 !
524 read(line,*,iostat=itemp) units(i)
525 if (itemp /= 0) print*,'ERROR reading units for column ',i
526 if (units(i) > huge(units)) print "(/,a,i2)",' ERROR: UNITS ARE INFINITE FOR COLUMN ',i
527 if (isnan(units(i))) print "(/,a,i2)",' ERROR: UNITS ARE NaN FOR COLUMN ',i
528 !
529 ! units label is what comes after the semicolon
530 !
531 isemicolon = index(line,';')
532 if (i==0) then
533 !--time line also contains unit of z integration
534 isemicolon2 = index(line(isemicolon+1:),';')
535 if (isemicolon2 > 0) then
536 isemicolon2 = isemicolon + isemicolon2
537 unitslabel(i) = trim(line(isemicolon+1:isemicolon2-1))
538 isemicolon3 = isemicolon2 + index(line(isemicolon2+1:),';')
539 read(line(isemicolon2+1:isemicolon3-1),*,iostat=itemp) unitzintegration
540 if (itemp /= 0) then
541 print*,'error reading unit for z integration'
542 ierrzunits = .true.
543 else
544 labelzintegration = trim(line(isemicolon3+1:))
545 endif
546 else
547 ierrzunits = .true.
548 print*,'error: could not read z integration unit from units file'
549 if (isemicolon > 0) then
550 unitslabel(i) = trim(line(isemicolon+1:))
551 else
552 print*,'error reading units label for column ',i
553 endif
554 endif
555 else
556 if (isemicolon > 0) then
557 unitslabel(i) = trim(line(isemicolon+1:))
558 else
559 print*,'error reading units label for column ',i
560 endif
561 endif
562 ! print*,i,'units = ',units(i),'label = ',unitslabel(i)
563 enddo
564 if (ierrzunits .and. ndim==3) then
565 unitzintegration = units(3)
566 labelzintegration = unitslabel(3)
567 endif
568
569 close(unit=78)
570
571 return
572
573 997 continue
574 if (isverbose > 0) print*,trim(unitsfile),' not found'
575 ierr = 1
576 return
577 998 continue
578 print*,'*** error reading units from '//trim(unitsfile)
579 ierr = 2
580 if (ierrzunits .and. ndim==3) then
581 unitzintegration = units(3)
582 labelzintegration = unitslabel(3)
583 endif
584 close(unit=78)
585 return
586 999 continue
587 !--only give error if we really do not have enough columns
588 ! (on first call nextra is not set)
589 if (i <= ncolumns) then
590 print "(1x,a,i2)",'end of file in '//trim(unitsfile)//': units read to column ',i
591 ierr = -1
592 endif
593 if (ierrzunits .and. ndim==3) then
594 unitzintegration = units(3)
595 labelzintegration = unitslabel(3)
596 endif
597
598 close(unit=78)
599 return
600
601 end subroutine read_unitsfile
602
603 end module settings_units