Debian Codegraph @

src/get_data.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2016 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !
24 ! wrapper for the main data read
25 ! ensures that same procedure occurs on initial read as from menu option
26 !
27 ! drives reading of all files listed on command line
28 !
29 ! Arguments:
30 ! ireadfile : if < 0, reads from all files
31 ! if > 0, reads only from the filename rootname(ireadfile)
32 ! if = 0, no data read, just call labelling and exact_params
33 !
34 module getdata
35 implicit none
36 public :: get_data, get_labels
37 integer, private :: ncolumnsfirst
38
39 private
40
41 contains
42
43 subroutine get_data(ireadfile,gotfilenames,firsttime,iposinfile)
44 use asciiutils, only:ucase
45 use filenames, only:rootname,nstepsinfile,nfiles,nsteps,maxfile,ifileopen,iposopen
46 use limits, only:set_limits
47 use settings_data, only:ncolumns,iendatstep,ncalc,ivegotdata, &
48 DataisBuffered,iCalcQuantities,iverbose, &
49 iRescale,required,ipartialread,lowmemorymode,debugmode
50 use settings_data, only:buffer_steps_in_file
51 use particle_data, only:dat,maxcol
52 use prompting, only:prompt
53 use calcquantities, only:calc_quantities
54 use timing, only:wall_time,print_time
55 use geomutils, only:set_coordlabels
56 use adjustdata, only:adjust_data_codeunits
57 use readdata, only:read_data
58 integer, intent(in) :: ireadfile
59 logical, intent(in) :: gotfilenames
60 logical, intent(in), optional :: firsttime
61 integer, intent(in), optional :: iposinfile
62 logical :: setlimits,isfirsttime,verbose
63 logical, parameter :: dotiming = .true.
64 integer :: i,istart,ierr,ipos,nsteps_read
65 real :: t1,t2
66
67 if (.not.gotfilenames) then
68 if (nfiles <= 0 .or. nfiles > maxfile) nfiles = 1
69 call prompt('Enter number of files to read ',nfiles,1,maxfile)
70 do i=1,nfiles
71 call prompt('Enter filename to read',rootname(i),noblank=.true.)
72 enddo
73 endif
74 !
75 !--set everything to zero initially
76 !
77 ncolumns = 0
78 ncalc = 0
79 nsteps = 0
80 istart = 1
81 ierr = 0
82 ivegotdata = .false.
83 ifileopen = ireadfile
84 DataIsBuffered = .false.
85 ipartialread = .false.
86 isfirsttime = .false.
87 if (present(firsttime)) isfirsttime = firsttime
88 if (debugmode) then
89 iverbose = 2
90 elseif (isfirsttime) then
91 iverbose = 1
92 else
93 iverbose = 0
94 endif
95
96 ! ipos is the offset for *which* timestep to read from files containing multiple steps
97 ipos = 1
98 if (present(iposinfile)) then
99 if (iposinfile > 0) ipos = iposinfile
100 endif
101 iposopen = 0
102 !
103 !--nstepsinfile is initialised to negative
104 ! this is set progressively as files are read
105 ! for non-buffered data file 1 is read and the rest are assumed to be the same
106 ! then these files are corrected as they are read. By initialising nstepsinfile
107 ! to negative, this means that if we get dud files (with nstepsinfile=0) we
108 ! know that this is really the file contents (not just an initialised value of nstepsinfile)
109 ! and can skip the file on the second encounter (see timestepping.f90)
110 !
111 if (isfirsttime) then
112 nstepsinfile(:) = -1
113 ncolumnsfirst = 0
114 required = .true.
115 if (lowmemorymode) required = .false.
116 call endian_info()
117 endif
118
119 if (ireadfile <= 0) then
120 !
121 !--read all steps from the data file
122 !
123 nstepsinfile(1:nfiles) = 0
124 required = .true.
125 print "(/a)",' reading ALL dumpfiles into memory'
126 !call endian_info()
127
128 do i=1,nfiles
129 call read_data(rootname(i),istart,ipos,nstepsinfile(i))
130
131 istart = istart + nstepsinfile(i) ! number of next step in data array
132 if (nstepsinfile(i) > 0 .and. ncolumnsfirst==0 .and. ncolumns > 0) then
133 ncolumnsfirst = ncolumns
134 elseif (nstepsinfile(i) > 0 .and. ncolumns /= ncolumnsfirst) then
135 print "(a,i2,a,i2,a)",' WARNING: file contains ',ncolumns, &
136 ' columns (',ncolumnsfirst,' previously)'
137 ncolumns = max(ncolumns,ncolumnsfirst)
138 endif
139 enddo
140 nsteps = istart - 1
141 if (nsteps > 0) then
142 ivegotdata = .true.
143 DataIsBuffered = .true.
144 else
145 ncolumns = 0
146 endif
147 print "(a,i6,a,i3)",' >> Finished data read, nsteps = ',nsteps,' ncolumns = ',ncolumns
148
149 !
150 !--set labels (and units) for each column of data
151 !
152 !print "(/a)",' setting plot labels...'
153 if (ivegotdata .and. ncolumns > 0) then
154 call get_labels
155 call adjust_data_codeunits
156 !
157 !--do some basic sanity checks
158 !
159 call check_data_read()
160 call rescale_data(isfirsttime,nsteps)
161 endif
162
163 !
164 !--reset coordinate and vector labels (depending on coordinate system)
165 ! Need to do this BEFORE calculating quantities
166 !
167 if (ivegotdata) call set_coordlabels(ncolumns)
168 !
169 !--calculate various additional quantities
170 !
171 if (nsteps >= 1 .and. iCalcQuantities) then
172 call calc_quantities(1,nsteps)
173 endif
174 !
175 !--set plot limits
176 !
177 if (ierr > 0 .and. ivegotdata .and. nstepsinfile(1) >= 1) then
178 call set_limits(1,nsteps,1,ncolumns+ncalc)
179 endif
180
181 elseif (ireadfile > 0) then
182 !
183 !--read from a single file only
184 !
185 nstepsinfile(ireadfile) = 0
186 !if (isfirsttime) print "(/a)",' reading single dumpfile'
187
188 if (dotiming) call wall_time(t1)
189 call read_data(rootname(ireadfile),istart,ipos,nstepsinfile(ireadfile))
190 !
191 !--do some basic sanity checks
192 !
193 if (debugmode) print "(a,i3)",' DEBUG: ncolumns from data read = ',ncolumns
194 if (debugmode) print "(a,i3)",' DEBUG: nsteps in file = ',nstepsinfile(ireadfile)
195 if (buffer_steps_in_file) then
196 nsteps_read = nstepsinfile(ireadfile)
197 else
198 nsteps_read = 1
199 iposopen = ipos
200 endif
201
202 !--try different endian if failed the first time
203 !if (nstepsinfile(ireadfile)==0) then
204 ! print "(a)",' trying different endian'
205 ! call read_data_otherendian(rootname(ireadfile),istart,nstepsinfile(ireadfile))
206 !endif
207 if (dotiming) then
208 call wall_time(t2)
209 if (t2-t1 > 1.) then
210 if (ipartialread) then
211 call print_time(t2-t1,'time for (partial) data read = ')
212 print*
213 else
214 call print_time(t2-t1,'time for data read = ')
215 print*
216 endif
217 endif
218 ! do i=1,ncolumns+ncalc
219 ! print*,' required(',i,') = ',required(i)
220 ! enddo
221 endif
222 !!print*,'nsteps in file = ',nstepsinfile(ireadfile)
223 if (ANY(nstepsinfile(1:ireadfile) > 0)) ivegotdata = .true.
224 if (.not.ivegotdata) ncolumns = 0
225 !
226 !--set ncolumns on first step only
227 !
228 if (ivegotdata .and. ncolumnsfirst==0 .and. ncolumns > 0) then
229 ncolumnsfirst = ncolumns
230 endif
231 !--override ncolumns from file and warn if different to first file
232 if (ncolumnsfirst > 0 .and. nstepsinfile(ireadfile) > 0) then
233 verbose = any(required(min(ncolumnsfirst,ncolumns)+1:))
234 if (ncolumns /= ncolumnsfirst) then
235 if (verbose) write(*,"(1x,a,i2,a,i2,a)",advance='NO') 'WARNING: file has ',ncolumns, &
236 ' columns (',ncolumnsfirst,' previously)'
237 if (ncolumns < ncolumnsfirst) then
238 if (verbose) write(*,"(a,i2,/)") ', data=0 for columns > ',ncolumns
239 dat(:,ncolumns+1:min(ncolumnsfirst,maxcol),1:nstepsinfile(ireadfile)) = 0.
240 elseif (ncolumns > ncolumnsfirst) then
241 if (verbose) write(*,"(a,i2,a)") ', columns > ',ncolumnsfirst,' ignored'
242 if (verbose) print "(10x,a,/)",'(read this file first to use this data)'
243 else
244 if (verbose) write (*,*)
245 endif
246 ncolumns = ncolumnsfirst
247 endif
248 endif
249
250 !
251 !--assume there are the same number of steps in the other files
252 ! which have not been read
253 !
254 do i=1,nfiles
255 if (nstepsinfile(i)==-1) then
256 nstepsinfile(i) = nstepsinfile(ireadfile)
257 endif
258 enddo
259 nsteps = sum(nstepsinfile(1:nfiles))
260 !
261 !--set labels (and units) for each column of data
262 ! allow this to be overridden by the presence of a splash.columns file
263 !
264 !!print "(/a)",' setting plot labels...'
265 if (ivegotdata .and. ncolumns > 0) then
266 call get_labels
267 if (ivegotdata) call set_coordlabels(ncolumns)
268
269 call adjust_data_codeunits
270 call check_data_read()
271 call rescale_data(isfirsttime,nsteps_read)
272 endif
273
274 !
275 !--reset coordinate and vector labels (depending on coordinate system)
276 ! Need to do this BEFORE calculating quantities
277 !
278 if (ivegotdata) call set_coordlabels(ncolumns)
279
280 !
281 !--calculate various additional quantities
282 !
283 if (nsteps_read > 0 .and. iCalcQuantities) then
284 if (ipartialread .and. .not.any(required(ncolumns+1:))) then
285 !--for partial data reads do a "pretend" call to calc quantities
286 ! just to get ncalc and column labels right
287 call calc_quantities(1,nsteps_read,dontcalculate=.true.)
288 else
289 call calc_quantities(1,nsteps_read)
290 endif
291 endif
292 !
293 !--only set limits if reading the first file for the first time
294 !
295 setlimits = (ireadfile==1 .and. ivegotdata .and. nstepsinfile(1) >= 1)
296 if (.not.present(firsttime)) then
297 setlimits = .false.
298 elseif (.not.firsttime) then
299 setlimits = .false.
300 endif
301
302 if (setlimits) then
303 call set_limits(1,nsteps_read,1,ncolumns+ncalc)
304 !--also set iendatstep the first time around
305 iendatstep = nsteps
306 endif
307 endif
308
309 end subroutine get_data
310
311 !----------------------------------------------------------------------
312 !
313 ! The following is a wrapper routine for the call to set_labels which
314 ! overrides the label setting from the splash.columns file if present.
315 ! Also adds the units label if the data has been rescaled.
316 !
317 !----------------------------------------------------------------------
318 subroutine get_labels
319 use asciiutils, only:read_asciifile
320 use filenames, only:fileprefix
321 use labels, only:label
322 use settings_data, only:ncolumns
323 use particle_data, only:maxcol
324 use params, only:maxplot
325 use readdata, only:set_labels
326 logical :: iexist
327 integer :: nlabelsread
328
329 call set_labels
330 !
331 !--check that label settings are sensible, fix where possible
332 !
333 call check_labels
334
335 !
336 !--look for a .columns file to override the default column labelling
337 !
338 inquire(file=trim(fileprefix)//'.columns',exist=iexist)
339 nlabelsread = 0
340 if (iexist) then
341 call read_asciifile(trim(fileprefix)//'.columns',nlabelsread,label(1:min(ncolumns,maxcol,maxplot)))
342 if (nlabelsread < ncolumns) &
343 print "(a,i3)",' end of file in '//trim(fileprefix)//'.columns file: labels read to column ',nlabelsread
344 endif
345
346 end subroutine get_labels
347
348 !----------------------------------------------------------------------
349 !
350 ! Apply physical units to data
351 !
352 !----------------------------------------------------------------------
353 subroutine rescale_data(firsttime,nsteps_read)
354 use filenames, only:unitsfile
355 use labels, only:label,unitslabel,unitslabel_default,labelzintegration,labelzintegration_default,&
356 map_shifted_columns,labelorig,labelreq
357 use settings_data, only:ncolumns,iRescale,idefaults_file_read,iverbose,debugmode,enforce_code_units
358 use settings_units, only:units,units_calc,units_default,unitzintegration,unitzintegration_default,read_unitsfile
359 use particle_data, only:maxcol,dat,time
360 use params, only:maxplot
361 logical, intent(in) :: firsttime
362 integer, intent(in) :: nsteps_read
363 integer :: i,j,icol,ierr
364 integer :: imap(maxplot)
365
366 !
367 ! turn physical units on by default if:
368 ! 1) the data read has set the physical units
369 ! 2) they have not been switched off
370 !
371 if (firsttime .and. any(abs(units_default(0:ncolumns)-1.0) > tiny(units))) then
372 units = units_default
373 unitslabel = unitslabel_default
374 unitzintegration = unitzintegration_default
375 labelzintegration = labelzintegration_default
376 if (.not.idefaults_file_read .and. .not.enforce_code_units) iRescale = .true.
377 endif
378
379 !
380 !--override default units by reading .units file
381 !
382 if (firsttime) call read_unitsfile(trim(unitsfile),ncolumns,ierr,iverbose)
383
384 if (firsttime) then
385 labelorig = ''
386 labelreq = ''
387 endif
388
389 !
390 !--apply physical units to data
391 !
392 if (iRescale .and. any(abs(units(0:ncolumns)-1.0) > tiny(units))) then
393 if (debugmode) write(*,"(a)") ' rescaling data...'
394 units_calc = units
395 imap = map_shifted_columns()
396 do j=1,nsteps_read
397 do i=1,min(ncolumns,maxcol)
398 icol = imap(i)
399 !print*,i,' imap=',icol
400 !if (icol /= i) print*,' shift data WAS ',i,units(i),unitslabel(i),' USING ',icol,units(icol),unitslabel(icol)
401 if (abs(units(icol)-1.0) > tiny(units) .and. abs(units(icol)) > tiny(units)) then
402 dat(:,i,j) = dat(:,i,j)*units(icol)
403 endif
404 enddo
405 enddo
406 do i=1,nsteps_read
407 if (time(i) > -0.5*huge(0.)) time(i) = time(i)*units(0)
408 enddo
409 endif
410 !
411 !--add units labels to labels
412 !
413 if (iRescale) then
414 do i=1,min(ncolumns,maxcol,maxplot)
415 if (index(label(i),trim(unitslabel(i)))==0) label(i) = trim(label(i))//trim(unitslabel(i))
416 enddo
417 endif
418
419 end subroutine rescale_data
420
421 !----------------------------------------------------------------
422 !
423 ! utility to check that label settings are sensible
424 !
425 !----------------------------------------------------------------
426 subroutine check_labels
427 use settings_data, only:ndim,ndimV,ncolumns,iverbose
428 use labels, only:ix,irho,ih,ipmass
429 use particle_data, only:masstype
430 integer :: i,ndimset
431
432 if (ndim /= 0 .and. ncolumns > 0) then
433 if (ndim < 0 .or. ndim > 3) then
434 print "(a)",' ERROR with ndim setting in data read, using ndim=3'
435 ndim = 3
436 endif
437 if (ndimV < 0 .or. ndimV > 3) then
438 print "(a)",' ERROR with ndimV setting in data read, using ndimV=3'
439 ndimV = 3
440 endif
441 if (ndim >= 2 .and. any(ix(2:ndim)==ix(1))) then
442 print "(a)",' WARNING: error in ix setting in set_labels: fixing '
443 ix(1) = max(ix(1),1)
444 do i=2,ndim
445 ix(i) = i
446 enddo
447 endif
448 if (ndim >= 1) then
449 do i=1,ndim
450 if (ix(i) <= 0) then
451 ix(i) = i
452 print "(a)",' WARNING: ndim > 0 but zero ix setting in set_labels: fixing '
453 endif
454 enddo
455 endif
456 ndimset = 0
457 do i=1,3
458 if (ix(i) /= 0) ndimset = ndimset + 1
459 enddo
460 if (ndimset /= ndim) then
461 print "(2(a,i2))",' ERROR: labels for ',ndimset,&
462 ' coordinates set but got ndim = ',ndim
463 endif
464 if (irho > ncolumns .or. irho < 0) then
465 print "(a)",' ERROR with irho setting in data read'
466 irho = 0
467 endif
468 if (ih > ncolumns .or. ih < 0) then
469 print "(a)",' ERROR with ih setting in data read '
470 ih = 0
471 endif
472 if (ipmass > ncolumns .or. ipmass < 0) then
473 print "(a)",' ERROR with ipmass setting in data read'
474 ipmass = 0
475 endif
476 if (iverbose >= 1) then
477 if (irho==0 .or. ih==0) then
478 print "(4(/,a))",' WARNING: Rendering capabilities cannot be enabled', &
479 ' until positions of density, smoothing length and particle', &
480 ' masses are known (specified using the integer variables ', &
481 ' irho,ih and ipmass in the read_data routine)'
482 elseif (irho > 0 .and. ih > 0 .and. ipmass==0 .and. all(masstype(:,:) < tiny(0.))) then
483 print "(2(/,a))",' WARNING: Particle masses not read as array but mass not set:', &
484 ' RENDERING WILL NOT WORK! '
485 endif
486 endif
487 endif
488
489 end subroutine check_labels
490
491 !----------------------------------------------------------------
492 !
493 ! utility to check things about the data read
494 !
495 !----------------------------------------------------------------
496 subroutine check_data_read
497 use params, only:maxplot,maxparttypes
498 use settings_data, only:ncolumns,ndim,ndimV,ntypes,ivegotdata
499 use particle_data, only:npartoftype,iamtype,dat
500 use labels, only:labeltype
501 integer :: i,j,ntoti,nunknown,itype
502 integer, dimension(maxparttypes) :: noftype
503
504 if (ncolumns < 0) then
505 print "(a)",' ERROR: ncolumns < 0 in data read'
506 ncolumns = 0
507 elseif (ncolumns > maxplot) then
508 print "(/,71('*'),/,'*',a,i3,a,'*',/,71('*'))",&
509 ' ERROR: ncolumns > ',maxplot,' in data read: cannot list all columns in menu '
510 ncolumns = maxplot
511 endif
512
513 if (ndim > 3) then; print "(a)",' ERROR: ndim > 3 in data read, setting ndim = 3'; ndim = 3; endif
514 if (ndim < 0) then; print "(a)",' ERROR: ndim < 0 in data read, setting ndim = 0'; ndim = 0; endif
515 if (ndimV > 3) then; print "(a)",' ERROR: ndimV > 3 in data read, setting ndimV = 3'; ndimV = 3; endif
516 if (ndimV < 0) then; print "(a)",' ERROR: ndimV < 0 in data read, setting ndimV = 0'; ndimV = 0; endif
517 if (ntypes < 0) then; print "(a)",' ERROR: ntypes < 0 in data read'; ntypes = 0; endif
518
519 if (allocated(npartoftype)) then
520 if (size(npartoftype(:,1)) < ntypes) then
521 print "(a)",' ERROR: too many particle types for allocated array size in data read'
522 ntypes = size(npartoftype(:,1))
523 endif
524 do i=1,ntypes
525 do j=1,size(npartoftype(i,:))
526 if (npartoftype(i,j) < 0) then
527 print "(a)",' ERROR: number of '//trim(labeltype(i))//' particles < 0 in data read'
528 npartoftype(i,j) = 0
529 endif
530 enddo
531 enddo
532 ntoti = sum(npartoftype(:,1))
533 if (ntoti > size(dat(:,1,1))) then
534 print "(2(a,i10),a)",' ERROR: size of dat array (',size(dat(:,1,1)),&
535 ') too small for number of particles (',ntoti,')'
536 ivegotdata = .false.
537 endif
538 !if (debugmode) then
539 !
540 !--for mixed type storage, check that the number of particles
541 ! of each type adds up to npartoftype
542 !
543 ntoti = sum(npartoftype(:,1))
544 noftype(:) = 0
545 nunknown = 0
546 if (size(iamtype(:,1)) >= ntoti) then
547 do i=1,ntoti
548 itype = iamtype(i,1)
549 if (itype > 0 .and. itype <= ntypes) then
550 noftype(itype) = noftype(itype) + 1
551 else
552 nunknown = nunknown + 1
553 endif
554 enddo
555 do itype=1,ntypes
556 if (npartoftype(itype,1) /= noftype(itype)) then
557 print "(a,i10,a,i10)",' ERROR in data read: got ',noftype(itype),' '//trim(labeltype(itype))// &
558 ' particles from iamtype, but npartoftype = ',npartoftype(itype,1)
559 endif
560 enddo
561 if (nunknown > 0) then
562 print "(a,i10,a)",' ERROR in data read: got ',nunknown, &
563 ' particles of unknown type in iamtype array from data read'
564 endif
565 endif
566 !endif
567 endif
568
569 end subroutine check_data_read
570
571 !-------------------------------------
572 !
573 ! simple utility to spit out native
574 ! endian-ness
575 !
576 !-------------------------------------
577
578 subroutine endian_info
579 logical :: bigendian
580
581 bigendian = IACHAR(TRANSFER(1,"a")) == 0
582
583 if (bigendian) then
584 print "(a)",' native byte order on this machine is BIG endian'
585 !--we no longer warn for little endian, as this is now most common
586 !else
587 ! print "(a)",' native byte order on this machine is LITTLE endian'
588 endif
589
590 end subroutine endian_info
591
592 end module getdata