Debian Codegraph @

utils/grid2pdf.f90

raw
1 program grid2pdf
2 use system_commands, only:get_number_arguments,get_argument
3 use readwrite_griddata, only:open_gridfile_r,read_gridcolumn
4 use pdfs, only:pdf_calc,pdf_write,mean_variance
5 implicit none
6 logical, parameter :: usefixedbins = .true.
7 integer, parameter :: nbins = 270
8 integer, parameter :: iunit = 13
9 integer, dimension(3) :: nxgrid
10 integer :: ierr,ncolumns,itransx,ifile,nargs
11 integer :: ntot,i,ndim
12 real :: time,rhologmin,rhologmax,pdfmin,pdfmax,smean,svar,rhomean,rhovar
13 real, dimension(nbins) :: xbin,pdf
14 character(len=120) :: filename,tagline
15 character(len=20) :: informat
16 real, dimension(:), allocatable :: rhogrid
17
18 tagline = 'grid2pdf: a SPLASH utility (c) 2010-2021 Daniel Price'
19
20 call get_number_arguments(nargs)
21 if (nargs.le.0) then
22 print "(a)",trim(tagline)
23 print "(/,a,/)",'Usage: grid2pdf gridfile(s)'
24 stop
25 endif
26
27 do ifile=1,nargs
28 !
29 !--get the filename off the command line
30 !
31 call get_argument(ifile,filename)
32 !
33 !--open the grid data file and read the header information
34 !
35 informat = 'gridbinary'
36 ndim = 3
37 call open_gridfile_r(iunit,filename,informat,ndim,ncolumns,nxgrid,time,ierr)
38 !
39 !--allocate memory for the grid data
40 !
41 ntot = product(nxgrid)
42 if (.not.allocated(rhogrid) .or. ntot.ne.size(rhogrid)) then
43 if (allocated(rhogrid)) deallocate(rhogrid)
44 allocate(rhogrid(ntot))
45 endif
46 !
47 !--read one particular column (in this case, the density)
48 !
49 call read_gridcolumn(iunit,rhogrid,ntot,ierr)
50 !
51 !--close the file
52 !
53 close(iunit)
54 !
55 !--calculate the mean and variance in rho
56 !
57 call mean_variance(rhogrid,ntot,rhomean,rhovar)
58
59 !
60 !--set the parameters for calculating the PDF
61 ! [in this case, we want the PDF of ln (rho)]
62 !
63 rhologmin = -15.
64 rhologmax = 12.
65 where (rhogrid.gt.0.)
66 rhogrid = log(rhogrid)
67 end where
68 !itransx = 6
69 !
70 !--calculate the PDF
71 !
72 call pdf_calc(ntot,rhogrid,rhologmin,rhologmax,nbins,xbin,&
73 pdf,pdfmin,pdfmax,usefixedbins,ierr)
74 !
75 !--calculate the mean and variance in ln(rho)
76 !
77 call mean_variance(rhogrid,ntot,smean,svar)
78
79 print*,' rho mean,var = ',rhomean,rhovar
80 print*,'ln(rho) mean,var = ',smean,svar
81 print*,' svar,rhovar = ',-2.*smean,exp(svar) - 1.
82 !
83 !--check the routine
84 !
85 svar = 0.
86 do i=1,ntot
87 svar = svar + (rhogrid(i) - smean)**2
88 enddo
89 svar = svar/real(ntot-1)
90 print*,'svar = ',svar
91
92 !
93 !--write the PDF to file
94 !
95 if (ierr.eq.0) then
96 call pdf_write(nbins,xbin,pdf,'ln density',trim(filename),trim(tagline))
97 endif
98 enddo
99 !
100 !--clean up/deallocate memory
101 !
102 if (allocated(rhogrid)) deallocate(rhogrid)
103
104 end program grid2pdf