| 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-2023 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!----------------------------------------------------------------- |
| 24 |
! Module containing routines for converting 3D SPH dump |
| 25 |
! files to 3D gridded data. |
| 26 |
!----------------------------------------------------------------- |
| 27 |
module convert_grid |
| 28 |
use params, only:doub_prec |
| 29 |
implicit none |
| 30 |
private |
| 31 |
public :: convert_to_grid |
| 32 |
|
| 33 |
contains |
| 34 |
|
| 35 |
!----------------------------------------------------------------- |
| 36 |
! interpolate 3D SPH data to grid and interface to grid |
| 37 |
! data output routines |
| 38 |
!----------------------------------------------------------------- |
| 39 |
subroutine convert_to_grid(time,dat,ntypes,npartoftype,masstype,itype,ncolumns,filename,& |
| 40 |
outformat,interpolateall,icols,rhogrid,dat3D) |
| 41 |
use labels, only:label,labelvec,irho,ih,ipmass,ix,ivx,iBfirst,get_sink_type |
| 42 |
use limits, only:lim,get_particle_subset |
| 43 |
use settings_units, only:units,unit_interp |
| 44 |
use settings_data, only:ndim,ndimV,UseTypeInRenderings,iRescale,required,debugmode,& |
| 45 |
icoords,icoordsnew,xorigin,iverbose |
| 46 |
use settings_part, only:iplotpartoftype |
| 47 |
use settings_render, only:npix,inormalise=>inormalise_interpolations,& |
| 48 |
idensityweightedinterpolation,exact_rendering |
| 49 |
use settings_xsecrot, only:anglex,angley,anglez |
| 50 |
use rotation, only:rotate3D |
| 51 |
use params, only:int1 |
| 52 |
use interpolation, only:set_interpolation_weights,get_n_interp |
| 53 |
use interpolations3D, only:interpolate3D,interpolate3D_vec |
| 54 |
use interpolations3Dgeom, only:interpolate3Dgeom,interpolate3Dgeom_vec |
| 55 |
use interpolations2D, only:interpolate2D,interpolate2D_vec |
| 56 |
use system_utils, only:renvironment,ienvlist |
| 57 |
use readwrite_griddata, only:open_gridfile_w,write_grid,write_gridlimits |
| 58 |
use particle_data, only:icolourme |
| 59 |
use params, only:int8 |
| 60 |
use geometry, only:coord_is_length,igeom_cartesian, & |
| 61 |
igeom_cylindrical,igeom_spherical, & |
| 62 |
igeom_flaredcyl,igeom_logflared, & |
| 63 |
labelcoord,labelcoordsys |
| 64 |
use asciiutils, only:strip |
| 65 |
use timing, only:wall_time,print_time |
| 66 |
use filenames, only:tagline |
| 67 |
integer, intent(in) :: ntypes,ncolumns |
| 68 |
integer, intent(in), dimension(:) :: npartoftype |
| 69 |
integer(kind=int1), intent(in), dimension(:) :: itype |
| 70 |
real, intent(in) :: time |
| 71 |
real, intent(inout), dimension(:,:) :: dat |
| 72 |
real, intent(in), dimension(:) :: masstype |
| 73 |
character(len=*), intent(in) :: filename,outformat |
| 74 |
logical, intent(in) :: interpolateall |
| 75 |
integer, intent(in), optional :: icols(:) |
| 76 |
real, intent(out), allocatable, optional :: rhogrid(:,:,:),dat3D(:,:,:) |
| 77 |
integer, parameter :: iunit = 89 |
| 78 |
integer :: ierr,i,k,ncolsgrid,ivec,nvec,iloc,j,nzero |
| 79 |
integer :: npixx,ninterp,isinktype |
| 80 |
character(len=40) :: fmtstring |
| 81 |
character(len=64) :: fmtstring1 |
| 82 |
|
| 83 |
real(doub_prec), dimension(:,:,:), allocatable :: datgrid,rhogrid_tmp |
| 84 |
real, dimension(:,:), allocatable :: datgrid2D |
| 85 |
real(doub_prec), dimension(:,:,:,:), allocatable :: datgridvec |
| 86 |
real, dimension(:,:,:), allocatable :: datgridvec2D |
| 87 |
real, dimension(:), allocatable :: weight,x,y,z |
| 88 |
logical, dimension(:), allocatable :: mask |
| 89 |
real, dimension(3) :: xmin,xmax |
| 90 |
real, dimension(3) :: partmin,partmax,partmean,part_integral |
| 91 |
real, dimension(3) :: datmin,datmax,datmean |
| 92 |
real(doub_prec) :: dtime |
| 93 |
real(doub_prec), dimension(3) :: xmind,xmaxd |
| 94 |
integer, dimension(3) :: npixels |
| 95 |
integer(kind=int8), dimension(3) :: npixels8 |
| 96 |
integer, dimension(12) :: icoltogrid |
| 97 |
integer :: ncolstogrid,igeom |
| 98 |
real :: hmin,pixwidth,pixwidthx(3),rhominset,rhomin |
| 99 |
real :: gridmin,gridmax,gridmean,grid_integral |
| 100 |
real :: mtot,mtotgrid,err,t2,t1,xi(3),ax,ay,az |
| 101 |
real, parameter :: pi=4.0*atan(1.0) |
| 102 |
logical :: lowmem,do_output |
| 103 |
logical, dimension(3) :: isperiodic |
| 104 |
character(len=len(labelcoord)), dimension(3) :: xlab |
| 105 |
character(len=120) :: origin |
| 106 |
|
| 107 |
dtime = real(time,kind=doub_prec) |
| 108 |
do_output = .not.(trim(filename)=='none' .or. trim(outformat)=='none') |
| 109 |
! |
| 110 |
!--check for errors in input settings |
| 111 |
! |
| 112 |
if (ndim < 2 .or. ndim > 3) then |
| 113 |
print "(/,a,i2,a,/)",' ERROR: SPH data has ',ndim,' spatial dimensions: cannot convert to 3D grid' |
| 114 |
return |
| 115 |
endif |
| 116 |
|
| 117 |
print "(/,'----->',1x,a,i1,a,/)",'CONVERTING SPH DATA -> ',ndim,'D GRID' |
| 118 |
xmin(1:ndim) = lim(ix(1:ndim),1) |
| 119 |
xmax(1:ndim) = lim(ix(1:ndim),2) |
| 120 |
xlab(:) = (/'x','y','z'/) |
| 121 |
igeom = max(icoordsnew,1) ! ensure it is not zero |
| 122 |
if (igeom /= igeom_cartesian) xlab = strip(labelcoord(:,igeom),'\') |
| 123 |
! |
| 124 |
!--print limits information |
| 125 |
! |
| 126 |
call write_gridlimits(ndim,xmin,xmax,xlab) |
| 127 |
! |
| 128 |
!--get environment variable options |
| 129 |
! |
| 130 |
call get_splash2grid_options(ndim,ncolstogrid,icoltogrid,isperiodic,xlab) |
| 131 |
if (present(icols)) then |
| 132 |
icoltogrid(:) = 0 |
| 133 |
icoltogrid(1:size(icols)) = icols(:) |
| 134 |
ncolstogrid = count(icols > 0) |
| 135 |
endif |
| 136 |
! |
| 137 |
!--for non-Cartesian geometries, default to periodic phi (second coordinate) |
| 138 |
! when the user has not explicitly requested any periodic boundaries |
| 139 |
! |
| 140 |
if (.not.any(isperiodic(1:ndim))) then |
| 141 |
if ((igeom==igeom_cylindrical .or. igeom==igeom_spherical .or. & |
| 142 |
igeom==igeom_flaredcyl .or. igeom==igeom_logflared) & |
| 143 |
.and. abs((xmax(2)-xmin(2)) - 2.*pi) < 1.e-3) then |
| 144 |
print "(a)",' Assuming PERIODIC boundary for non-Cartesian geometry with 2 pi range in '//trim(xlab(2)) |
| 145 |
isperiodic(2) = .true. |
| 146 |
endif |
| 147 |
endif |
| 148 |
! |
| 149 |
!--check for errors |
| 150 |
! |
| 151 |
ierr = 0 |
| 152 |
do i=1,ndim |
| 153 |
if ((xmax(i)-xmin(i)) < tiny(0.)) then |
| 154 |
print "(a)",' ERROR: min = max in '//trim(label(ix(i)))//& |
| 155 |
' coordinate: cannot interpolate to zero-sized grid!' |
| 156 |
ierr = 1 |
| 157 |
endif |
| 158 |
enddo |
| 159 |
|
| 160 |
if (irho <= 0 .or. irho > ncolumns) then |
| 161 |
print "(a)",' ERROR: density not found in data read.' |
| 162 |
ierr = 2 |
| 163 |
endif |
| 164 |
if (ih <= 0 .or. ih > ncolumns) then |
| 165 |
print "(a)",' ERROR: smoothing length not found in data read.' |
| 166 |
ierr = 3 |
| 167 |
endif |
| 168 |
if (ipmass <= 0 .or. ipmass > ncolumns) then |
| 169 |
if (all(masstype(:) < tiny(0.))) then |
| 170 |
print "(a)",' ERROR: particle masses not read as column, and mass per type not set.' |
| 171 |
ierr = 4 |
| 172 |
endif |
| 173 |
endif |
| 174 |
|
| 175 |
if (ierr /= 0) then |
| 176 |
print "(a,i1,a)",' cannot perform SPH interpolation to ',ndim,'D grid, skipping file...' |
| 177 |
return |
| 178 |
endif |
| 179 |
ierr = 0 |
| 180 |
|
| 181 |
! |
| 182 |
!--set number of particles to use in the interpolation routines |
| 183 |
! and allocate memory for weights |
| 184 |
! |
| 185 |
ninterp = get_n_interp(ntypes,npartoftype,UseTypeInRenderings,iplotpartoftype,size(itype),.false.) |
| 186 |
allocate(weight(ninterp),stat=ierr) |
| 187 |
allocate(x(ninterp),y(ninterp),z(ninterp),mask(ninterp),stat=ierr) |
| 188 |
if (ierr /= 0) then |
| 189 |
print*,' ERROR allocating memory for interpolation weights, aborting...' |
| 190 |
return |
| 191 |
endif |
| 192 |
! |
| 193 |
!--set interpolation weights (w = m/(rho*h^ndim) |
| 194 |
! |
| 195 |
isinktype = get_sink_type(ntypes) |
| 196 |
call set_interpolation_weights(weight,dat,itype,(iplotpartoftype .and. UseTypeInRenderings),& |
| 197 |
ninterp,npartoftype,masstype,ntypes,ncolumns,irho,ipmass,ih,ndim,iRescale,& |
| 198 |
idensityweightedinterpolation,inormalise,units,unit_interp,required,.false.,isinktype) |
| 199 |
! |
| 200 |
!--set default mask and apply range restrictions to data |
| 201 |
! |
| 202 |
icolourme(:) = 1 |
| 203 |
call get_particle_subset(icolourme,dat,ncolumns) |
| 204 |
mask = (icolourme(1:ninterp) > 0 .and. weight > 0.) |
| 205 |
|
| 206 |
! |
| 207 |
!--work out how many pixels to use |
| 208 |
! |
| 209 |
npixx = npix |
| 210 |
npixels = ienvlist('SPLASH_TO_GRID_NPIX',3) |
| 211 |
if (product(npixels) > 0) then |
| 212 |
print "(a,2(i5,','),i5)",' Using --npix=',npixels |
| 213 |
else |
| 214 |
if (npixx <= 0) then |
| 215 |
print "(/,a)",' WARNING: number of pixels = 0, using automatic pixel numbers' |
| 216 |
hmin = 0. |
| 217 |
call minmaxmean_part(dat(:,ih:ih),mask,ninterp,partmin,partmax,partmean,nonzero=.true.) |
| 218 |
hmin = partmin(1) |
| 219 |
if (hmin > 0. .and. igeom==igeom_cartesian) then |
| 220 |
print*,'based on the minimum smoothing length of hmin = ',hmin |
| 221 |
npixels8(1:ndim) = int((xmax(1:ndim) - xmin(1:ndim))/hmin,kind=int8) + 1 |
| 222 |
if (ndim==3) then |
| 223 |
print "(a,i6,2(' x',i6),a)",' requires ',npixels8(1:ndim),' pixels to capture the full resolution' |
| 224 |
if (product(npixels8(1:ndim)) > 512**3 .or. product(npixels8(1:ndim)) <= 0) then |
| 225 |
npixx = 512 |
| 226 |
print "(a,i4)",' but this is ridiculous, so instead we choose ',npixx |
| 227 |
else |
| 228 |
npixx = int(npixels8(1)) |
| 229 |
endif |
| 230 |
else |
| 231 |
print "(a,i6,1(' x',i6),a)",' requires ',npixels8(1:ndim),' pixels to capture the full resolution' |
| 232 |
if (product(npixels8(1:ndim)) > 1024**ndim .or. product(npixels8(1:ndim)) <= 0) then |
| 233 |
npixx = 1024 |
| 234 |
print "(a,i4)",' but this is very large, so instead we choose ',npixx |
| 235 |
else |
| 236 |
npixx = int(npixels8(1)) |
| 237 |
endif |
| 238 |
endif |
| 239 |
else |
| 240 |
npixx = 512 |
| 241 |
if (hmin <= 0.) then |
| 242 |
print "(a)",' ...but cannot get auto pixel number because hmin = 0' |
| 243 |
else |
| 244 |
print "(a)",' ...but cannot get auto pixel number because of non-cartesian geometry' |
| 245 |
endif |
| 246 |
print "(a)",' so instead we choose npixels = ',npixx |
| 247 |
endif |
| 248 |
endif |
| 249 |
print "(a,/)",' Set this manually using --npix=100,100,100' |
| 250 |
pixwidth = (xmax(1)-xmin(1))/npixx |
| 251 |
do i=1,ndim |
| 252 |
if (coord_is_length(i,igeom)) then |
| 253 |
npixels(i) = int((xmax(i)-xmin(i) - 0.5*pixwidth)/pixwidth) + 1 |
| 254 |
else |
| 255 |
npixels(i) = npixx |
| 256 |
endif |
| 257 |
enddo |
| 258 |
endif |
| 259 |
pixwidthx(:) = (xmax(:) - xmin(:))/npixels(:) |
| 260 |
! |
| 261 |
!--work out how many columns will be written to file |
| 262 |
! |
| 263 |
nvec = 0 |
| 264 |
if (ncolstogrid > 0) then |
| 265 |
ncolsgrid = ncolstogrid |
| 266 |
elseif (interpolateall) then |
| 267 |
ncolsgrid = 0 |
| 268 |
do i=1,ncolumns |
| 269 |
if (.not.any(ix(1:ndim)==i) .and. i /= ih .and. i /= ipmass) then |
| 270 |
ncolsgrid = ncolsgrid + 1 |
| 271 |
endif |
| 272 |
enddo |
| 273 |
else |
| 274 |
if (ndimV==ndim) then |
| 275 |
if (ivx > 0 .and. ivx+ndimV-1 <= ncolumns) nvec = nvec + 1 |
| 276 |
if (iBfirst > 0 .and. iBfirst+ndimV-1 <= ncolumns) nvec = nvec + 1 |
| 277 |
endif |
| 278 |
ncolsgrid = 1 + ndimV*nvec |
| 279 |
endif |
| 280 |
|
| 281 |
! |
| 282 |
!--use low memory mode for large grids |
| 283 |
! |
| 284 |
if (trim(outformat)=='gridascii2') then |
| 285 |
lowmem = .false. |
| 286 |
else |
| 287 |
lowmem = .true. |
| 288 |
endif |
| 289 |
|
| 290 |
if (lowmem .and. nvec > 0) & |
| 291 |
print "(a,/)",' [doing velocity field components separately (low memory mode)]' |
| 292 |
! |
| 293 |
!--allocate memory for the grid |
| 294 |
! |
| 295 |
if (allocated(datgrid)) deallocate(datgrid) |
| 296 |
if (allocated(datgrid2D)) deallocate(datgrid2D) |
| 297 |
|
| 298 |
if (ndim==3) then |
| 299 |
write(*,"(a,i5,2(' x',i5),a)",advance='no') ' >>> allocating memory for ',npixels(1:ndim),' grid ...' |
| 300 |
allocate(datgrid(npixels(1),npixels(2),npixels(3)),stat=ierr) |
| 301 |
elseif (ndim==2) then |
| 302 |
write(*,"(a,i5,1(' x',i5),a)",advance='no') ' >>> allocating memory for ',npixels(1:ndim),' grid ...' |
| 303 |
allocate(datgrid2D(npixels(1),npixels(2)),stat=ierr) |
| 304 |
endif |
| 305 |
if (ierr /= 0) then |
| 306 |
write(*,*) 'FAILED: NOT ENOUGH MEMORY' |
| 307 |
call deallocate_memory() |
| 308 |
return |
| 309 |
else |
| 310 |
write(*,*) 'OK' |
| 311 |
endif |
| 312 |
|
| 313 |
! |
| 314 |
!--open grid file for output (also checks format is OK) |
| 315 |
! |
| 316 |
if (do_output) then |
| 317 |
origin='"splash to '//trim(outformat)//'" on file '//trim(filename) |
| 318 |
xmind = xmin; xmaxd = xmax ! convert to double precision |
| 319 |
call open_gridfile_w(iunit,filename,outformat,ndim,ncolsgrid,npixels(1:ndim),& |
| 320 |
xmind(1:ndim),xmaxd(1:ndim),dtime,ierr) |
| 321 |
if (ierr /= 0) then |
| 322 |
print "(a)",' ERROR: could not open grid file for output, skipping...' |
| 323 |
call deallocate_memory() |
| 324 |
return |
| 325 |
endif |
| 326 |
endif |
| 327 |
|
| 328 |
fmtstring1 = "(12x,a20,1x,' min ',1x,' max ',1x,' mean ')" |
| 329 |
fmtstring = "(22x,a10,1x,3(es10.2,1x))" |
| 330 |
! |
| 331 |
!--interpolate density to the 3D grid |
| 332 |
! |
| 333 |
print "(/,a,i1,a)",' interpolating density to ',ndim,'D grid...' |
| 334 |
if (debugmode) print*,'DEBUG: density in column ',irho,' vals = ',dat(1:10,irho) |
| 335 |
|
| 336 |
call minmaxmean_part(dat(1:ninterp,irho:irho),mask,ninterp,partmin,partmax,partmean) |
| 337 |
print fmtstring1,trim(label(irho)) |
| 338 |
print fmtstring,' on parts:',partmin(1),partmax(1),partmean(1) |
| 339 |
|
| 340 |
if (ipmass > 0.) then |
| 341 |
mtot = sum(dat(1:ninterp,ipmass),mask=(icolourme(1:ninterp) > 0 .and. weight(1:ninterp) > 0.)) |
| 342 |
print "(9x,a23,1x,es10.4)",'total mass on parts:',mtot |
| 343 |
endif |
| 344 |
|
| 345 |
if (ndim==3) then |
| 346 |
mtotgrid = 0. |
| 347 |
call wall_time(t1) |
| 348 |
x = dat(1:ninterp,ix(1)) |
| 349 |
y = dat(1:ninterp,ix(2)) |
| 350 |
z = dat(1:ninterp,ix(3)) |
| 351 |
if (igeom /= igeom_cartesian) then |
| 352 |
if (ipmass > 0) then |
| 353 |
call get_mass_in_box(ninterp,x,y,z,dat(1:ninterp,ipmass),mask,mtot,icoords,igeom,xmin,xmax) |
| 354 |
print "(9x,a23,1x,es10.4,/)",'mass on parts in box:',mtot |
| 355 |
endif |
| 356 |
|
| 357 |
call interpolate3Dgeom(igeom,dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),dat(1:ninterp,ix(3)),& |
| 358 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,irho),icolourme,ninterp,& |
| 359 |
xmin,datgrid,npixels,pixwidthx,xorigin,inormalise,isperiodic) |
| 360 |
call grid_total_mass_geom(datgrid,npixels,pixwidthx,xmin,igeom,mtotgrid) |
| 361 |
else |
| 362 |
if (abs(anglez)>0. .or. abs(angley)>0. .or. abs(anglex)>0.) then |
| 363 |
print*, 'Rotating particles around (z,y,x) by',anglez,angley,anglex |
| 364 |
print*, 'WARNING: This does not rotate vector components' |
| 365 |
ax = anglex*pi/180.0 ! convert degrees to radians to pass into rotate |
| 366 |
ay = angley*pi/180.0 |
| 367 |
az = anglez*pi/180.0 |
| 368 |
do i=1,ninterp |
| 369 |
xi = (/x(i),y(i),z(i)/) |
| 370 |
call rotate3D(xi,ax,ay,az,0.,0.) |
| 371 |
x(i) = xi(1) |
| 372 |
y(i) = xi(2) |
| 373 |
z(i) = xi(3) |
| 374 |
enddo |
| 375 |
endif |
| 376 |
if (ipmass > 0) then |
| 377 |
call get_mass_in_box(ninterp,x,y,z,dat(1:ninterp,ipmass),mask,mtot,icoords,igeom,xmin,xmax) |
| 378 |
print "(9x,a23,1x,es10.4,/)",'mass on parts in box:',mtot |
| 379 |
endif |
| 380 |
|
| 381 |
call interpolate3D(x,y,z,& |
| 382 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,irho),icolourme,ninterp,& |
| 383 |
xmin(1),xmin(2),xmin(3),datgrid,npixels(1),npixels(2),npixels(3),& |
| 384 |
pixwidthx(1),pixwidthx(2),pixwidthx(3),inormalise,& |
| 385 |
isperiodic(1),isperiodic(2),isperiodic(3)) |
| 386 |
mtotgrid = sum(datgrid)*product(pixwidthx) |
| 387 |
if (iRescale) mtotgrid = mtotgrid /( units(irho) * units(ih)**3) |
| 388 |
endif |
| 389 |
if (present(rhogrid)) rhogrid = datgrid |
| 390 |
rhogrid_tmp = datgrid |
| 391 |
call wall_time(t2) |
| 392 |
if (t2 - t1 > 1.) call print_time(t2-t1) |
| 393 |
! |
| 394 |
!--set minimum density on the grid |
| 395 |
! |
| 396 |
call minmaxmean_grid(datgrid,npixels,gridmin,gridmax,gridmean,nonzero=.true.) |
| 397 |
else |
| 398 |
call interpolate2D(dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),& |
| 399 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,irho),icolourme,ninterp,& |
| 400 |
xmin(1),xmin(2),datgrid2D,npixels(1),npixels(2),& |
| 401 |
pixwidth,pixwidth,inormalise,exact_rendering,isperiodic(1),isperiodic(2),iverbose) |
| 402 |
! |
| 403 |
!--set minimum density on the grid |
| 404 |
! |
| 405 |
call minmaxmean_grid2D(datgrid2D,npixels,gridmin,gridmax,gridmean,nonzero=.true.) |
| 406 |
mtot = sum(datgrid2D)*pixwidth**2*npixels(1)*npixels(2) |
| 407 |
endif |
| 408 |
|
| 409 |
print fmtstring1,trim(label(irho)) |
| 410 |
print fmtstring,' on grid :',gridmin,gridmax,gridmean |
| 411 |
! |
| 412 |
!--print error if mass is not conserved |
| 413 |
! |
| 414 |
if (mtotgrid > 0.) then |
| 415 |
print "(9x,a23,1x,es10.4,/)",'total mass on grid:',mtotgrid |
| 416 |
err = 100.*(mtotgrid - mtot)/mtot |
| 417 |
if (abs(err) > 1) print "(/,a,1pg8.1,a,/)",' WARNING! MASS NOT CONSERVED BY ',err,& |
| 418 |
'% BY INTERPOLATION' |
| 419 |
endif |
| 420 |
|
| 421 |
rhomin = gridmin |
| 422 |
rhominset = renvironment('SPLASH_TO_GRID_RHOMIN',errval=-1.) |
| 423 |
|
| 424 |
print* |
| 425 |
if (rhominset >= 0.) then |
| 426 |
rhomin = rhominset |
| 427 |
print*,'enforcing minimum density on grid = ',rhomin |
| 428 |
print*,'(based on --rhomin setting)' |
| 429 |
elseif (rhomin > 0.) then |
| 430 |
print*,'enforcing minimum density on grid = ',rhomin |
| 431 |
print "(a)",' ** set --rhomin=minval to manually set this (e.g. to zero) **' |
| 432 |
endif |
| 433 |
|
| 434 |
if (rhomin > 0.) then |
| 435 |
nzero = 0 |
| 436 |
if (ndim==3) then |
| 437 |
!$omp parallel do private(k,j,i) reduction(+:nzero) schedule(static) |
| 438 |
do k=1,npixels(3) |
| 439 |
do j=1,npixels(2) |
| 440 |
do i=1,npixels(1) |
| 441 |
if (datgrid(i,j,k) <= tiny(datgrid)) then |
| 442 |
datgrid(i,j,k) = rhomin |
| 443 |
nzero = nzero + 1 |
| 444 |
endif |
| 445 |
enddo |
| 446 |
enddo |
| 447 |
enddo |
| 448 |
else |
| 449 |
!$omp parallel do private(j,i) reduction(+:nzero) schedule(static) |
| 450 |
do j=1,npixels(2) |
| 451 |
do i=1,npixels(1) |
| 452 |
if (datgrid2D(i,j) <= tiny(datgrid2D)) then |
| 453 |
datgrid2D(i,j) = rhomin |
| 454 |
nzero = nzero + 1 |
| 455 |
endif |
| 456 |
enddo |
| 457 |
enddo |
| 458 |
endif |
| 459 |
print "(a,i0,a)",' minimum density enforced on ',nzero,' grid cells' |
| 460 |
else |
| 461 |
print*,'minimum density NOT enforced' |
| 462 |
endif |
| 463 |
|
| 464 |
! |
| 465 |
!--write density to grid data file |
| 466 |
! |
| 467 |
print* |
| 468 |
if (ndim==3 .and. do_output) then |
| 469 |
if (lowmem .or. interpolateall .or. ncolstogrid > 0) then |
| 470 |
call write_grid(iunit,filename,outformat,ndim,1,npixels,trim(label(irho)),& |
| 471 |
labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,dat=datgrid,& |
| 472 |
tagline=tagline,origin=origin) |
| 473 |
endif |
| 474 |
elseif (do_output) then |
| 475 |
call write_grid(iunit,filename,outformat,ndim,1,npixels,trim(label(irho)),& |
| 476 |
labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,dat2D=datgrid2D,& |
| 477 |
tagline=tagline,origin=origin) |
| 478 |
endif |
| 479 |
! |
| 480 |
!--use density weighted interpolation for remaining quantities |
| 481 |
! |
| 482 |
idensityweightedinterpolation = .true. |
| 483 |
call set_interpolation_weights(weight,dat,itype,(iplotpartoftype .and. UseTypeInRenderings),& |
| 484 |
ninterp,npartoftype,masstype,ntypes,ncolumns,irho,ipmass,ih,ndim,iRescale,& |
| 485 |
idensityweightedinterpolation,inormalise,units,unit_interp,required,.false.,isinktype) |
| 486 |
|
| 487 |
! |
| 488 |
!--interpolate remaining quantities to the 3D grid |
| 489 |
! |
| 490 |
if (interpolateall .or. ncolstogrid > 0) then |
| 491 |
if (ncolstogrid > 0 .and. .not.present(icols)) then |
| 492 |
print "(/,a,i2,a)",' Interpolating ',ncolstogrid,' columns to grid from --grid setting:' |
| 493 |
print "(' got --grid=',10(i2,1x))",icoltogrid(1:ncolstogrid) |
| 494 |
endif |
| 495 |
|
| 496 |
do i=1,ncolumns |
| 497 |
if ((ncolstogrid > 0 .and. any(icoltogrid==i) .and. i /= irho) .or. & |
| 498 |
(interpolateall .and. & |
| 499 |
.not.any(ix(:)==i) .and. i /= ih .and. i /= ipmass .and. i /= irho)) then |
| 500 |
|
| 501 |
print "(/,a)",' interpolating '//trim(label(i)) |
| 502 |
print fmtstring1,trim(label(i)) |
| 503 |
call minmaxmean_part(dat(1:ninterp,i:i),mask,ninterp,partmin,partmax,partmean) |
| 504 |
print fmtstring,' on parts:',partmin(1),partmax(1),partmean(1) |
| 505 |
|
| 506 |
if (iszero(partmin,partmax,1)) then |
| 507 |
datgrid = 0. |
| 508 |
else |
| 509 |
if (ndim==3) then |
| 510 |
if (igeom /= igeom_cartesian) then |
| 511 |
call interpolate3Dgeom(igeom,dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),dat(1:ninterp,ix(3)),& |
| 512 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,i),icolourme,ninterp,& |
| 513 |
xmin,datgrid,npixels,pixwidthx,xorigin,.true.,isperiodic) |
| 514 |
else |
| 515 |
call interpolate3D(x,y,z,& |
| 516 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,i),icolourme,ninterp,& |
| 517 |
xmin(1),xmin(2),xmin(3),datgrid,npixels(1),npixels(2),npixels(3),& |
| 518 |
pixwidthx(1),pixwidthx(2),pixwidthx(3),.true.,isperiodic(1),isperiodic(2),isperiodic(3)) |
| 519 |
endif |
| 520 |
if (present(dat3D)) dat3D = datgrid |
| 521 |
else |
| 522 |
call interpolate2D(dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),& |
| 523 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,i),icolourme,ninterp,& |
| 524 |
xmin(1),xmin(2),datgrid2D,npixels(1),npixels(2),& |
| 525 |
pixwidth,pixwidth,.true.,exact_rendering,isperiodic(1),isperiodic(2),iverbose) |
| 526 |
endif |
| 527 |
endif |
| 528 |
|
| 529 |
! |
| 530 |
!--write gridded data to file |
| 531 |
! |
| 532 |
if (ndim==3) then |
| 533 |
call minmaxmean_grid(datgrid,npixels,gridmin,gridmax,gridmean,.false.) |
| 534 |
print fmtstring,' on grid :',gridmin,gridmax,gridmean |
| 535 |
if (do_output) call write_grid(iunit,filename,outformat,ndim,1,npixels,trim(label(i)),& |
| 536 |
labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,dat=datgrid) |
| 537 |
else |
| 538 |
call minmaxmean_grid2D(datgrid2D,npixels,gridmin,gridmax,gridmean,.false.) |
| 539 |
print fmtstring,' on grid :',gridmin,gridmax,gridmean |
| 540 |
if (do_output) call write_grid(iunit,filename,outformat,ndim,1,npixels,trim(label(i)),& |
| 541 |
labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,dat2D=datgrid2D) |
| 542 |
endif |
| 543 |
endif |
| 544 |
enddo |
| 545 |
|
| 546 |
else |
| 547 |
|
| 548 |
if (nvec > 0) then |
| 549 |
|
| 550 |
print "(/,a,i2,a)",' set --grid=',irho,' to interpolate density ONLY and skip remaining columns' |
| 551 |
print "(a,i2,a)", ' --grid=6,8,10 to select particular columns' |
| 552 |
|
| 553 |
if (.not.lowmem) then |
| 554 |
if (ndim==3) then |
| 555 |
write(*,"(/,a,i5,2(' x',i5),a)",advance='no') ' >>> allocating memory for ',npixels(1:ndim),' x 3 grid ...' |
| 556 |
allocate(datgridvec(3,npixels(1),npixels(2),npixels(3)),stat=ierr) |
| 557 |
else |
| 558 |
write(*,"(/,a,i5,1(' x',i5),a)",advance='no') ' >>> allocating memory for ',npixels(1:ndim),' x 3 grid ...' |
| 559 |
allocate(datgridvec2D(2,npixels(1),npixels(2)),stat=ierr) |
| 560 |
endif |
| 561 |
if (ierr /= 0) then |
| 562 |
write(*,*) 'FAILED: NOT ENOUGH MEMORY' |
| 563 |
return |
| 564 |
else |
| 565 |
write(*,*) 'OK' |
| 566 |
endif |
| 567 |
endif |
| 568 |
|
| 569 |
! |
| 570 |
!--interpolate velocity field and magnetic fields and write to file |
| 571 |
! |
| 572 |
over_vec: do ivec=1,nvec |
| 573 |
select case(ivec) |
| 574 |
case(1) |
| 575 |
iloc = ivx |
| 576 |
print "(a,i1,a)",' interpolating velocity field to ',ndim,'D grid...' |
| 577 |
case(2) |
| 578 |
iloc = iBfirst |
| 579 |
print "(a,i1,a)",' interpolating magnetic field to ',ndim,'D grid...' |
| 580 |
case default |
| 581 |
iloc = 0 |
| 582 |
exit over_vec |
| 583 |
end select |
| 584 |
if (iloc <= 0 .or. iloc >= ncolumns) cycle over_vec |
| 585 |
|
| 586 |
if (lowmem) then |
| 587 |
|
| 588 |
do i=iloc,iloc+ndimV-1 |
| 589 |
print "(/,a)",' interpolating '//trim(label(i)) |
| 590 |
print fmtstring1,trim(label(i)) |
| 591 |
if (ipmass > 0) then |
| 592 |
call minmaxmean_part(dat(1:ninterp,i:i),mask,ninterp,partmin,partmax,& |
| 593 |
partmean,mass=dat(1:ninterp,ipmass),part_integral=part_integral) |
| 594 |
print fmtstring,' on parts:',partmin(1),partmax(1),partmean(1) |
| 595 |
print "(9x,a23,1x,es10.2,/)",' sum(m*'//trim(label(i))//'):',part_integral(1) |
| 596 |
else |
| 597 |
call minmaxmean_part(dat(1:ninterp,i:i),mask,ninterp,partmin,partmax,partmean) |
| 598 |
print fmtstring,' on parts:',partmin(1),partmax(1),partmean(1) |
| 599 |
endif |
| 600 |
|
| 601 |
if (iszero(partmin,partmax,1)) then |
| 602 |
datgrid = 0. |
| 603 |
else |
| 604 |
if (ndim==3) then |
| 605 |
if (igeom /= igeom_cartesian) then |
| 606 |
call interpolate3Dgeom(igeom,dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),dat(1:ninterp,ix(3)),& |
| 607 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,i),icolourme,ninterp,& |
| 608 |
xmin,datgrid,npixels,pixwidthx,xorigin,.true.,isperiodic) |
| 609 |
else |
| 610 |
call interpolate3D(x,y,z,& |
| 611 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,i),icolourme,ninterp,& |
| 612 |
xmin(1),xmin(2),xmin(3),datgrid,npixels(1),npixels(2),npixels(3),& |
| 613 |
pixwidthx(1),pixwidthx(2),pixwidthx(3),.true.,isperiodic(1),isperiodic(2),isperiodic(3)) |
| 614 |
endif |
| 615 |
else |
| 616 |
call interpolate2D(dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),& |
| 617 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,i),icolourme,ninterp,& |
| 618 |
xmin(1),xmin(2),datgrid2D,npixels(1),npixels(2),& |
| 619 |
pixwidth,pixwidth,.true.,exact_rendering,isperiodic(1),isperiodic(2),iverbose) |
| 620 |
endif |
| 621 |
endif |
| 622 |
|
| 623 |
! |
| 624 |
!--write gridded data to file |
| 625 |
! |
| 626 |
if (ndim==3) then |
| 627 |
call minmaxmean_grid(datgrid,npixels,gridmin,gridmax,gridmean,.false.,& |
| 628 |
rho=rhogrid_tmp,grid_integral=grid_integral) |
| 629 |
print fmtstring,' on grid :',gridmin,gridmax,gridmean |
| 630 |
grid_integral = grid_integral*product(pixwidthx) |
| 631 |
print "(9x,a23,1x,es10.2,/)",' int(rho*'//trim(label(i))//')dV:',grid_integral |
| 632 |
|
| 633 |
call write_grid(iunit,filename,outformat,ndim,1,npixels,trim(label(i)),& |
| 634 |
labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,& |
| 635 |
dat=datgrid,tagline=tagline,origin=origin) |
| 636 |
else |
| 637 |
call minmaxmean_grid2D(datgrid2D,npixels,gridmin,gridmax,gridmean,.false.) |
| 638 |
print fmtstring,' on grid :',gridmin,gridmax,gridmean |
| 639 |
call write_grid(iunit,filename,outformat,ndim,1,npixels,trim(label(i)),& |
| 640 |
labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,& |
| 641 |
dat2D=datgrid2D,tagline=tagline,origin=origin) |
| 642 |
endif |
| 643 |
enddo |
| 644 |
else |
| 645 |
|
| 646 |
print fmtstring1,trim(labelvec(iloc)) |
| 647 |
call minmaxmean_part(dat(1:ninterp,iloc:iloc+ndimV-1),mask,ninterp,partmin,partmax,partmean) |
| 648 |
do i=1,ndimV |
| 649 |
print fmtstring,' on parts:',partmin(i),partmax(i),partmean(i) |
| 650 |
enddo |
| 651 |
|
| 652 |
if (iszero(partmin,partmax,ndimV)) then |
| 653 |
datgridvec = 0. |
| 654 |
else |
| 655 |
if (ndim==3) then |
| 656 |
if (igeom /= igeom_cartesian) then |
| 657 |
call interpolate3Dgeom_vec(igeom,dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),dat(1:ninterp,ix(3)),& |
| 658 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,iloc:iloc+ndimV-1),icolourme,ninterp,& |
| 659 |
xmin,datgridvec,npixels,pixwidthx,xorigin,.true.,isperiodic) |
| 660 |
else |
| 661 |
call interpolate3D_vec(x,y,z,& |
| 662 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,iloc:iloc+ndimV-1),icolourme,ninterp,& |
| 663 |
xmin(1),xmin(2),xmin(3),datgridvec,npixels(1),npixels(2),npixels(3),& |
| 664 |
pixwidthx(1),pixwidthx(2),pixwidthx(3),.true.,isperiodic(1),isperiodic(2),isperiodic(3)) |
| 665 |
endif |
| 666 |
else |
| 667 |
call interpolate2D_vec(dat(1:ninterp,ix(1)),dat(1:ninterp,ix(2)),& |
| 668 |
dat(1:ninterp,ih),weight(1:ninterp),dat(1:ninterp,iloc),dat(1:ninterp,iloc+1), & |
| 669 |
icolourme,ninterp,xmin(1),xmin(2),datgridvec2D(1,:,:),datgridvec2D(2,:,:), & |
| 670 |
npixels(1),npixels(2),pixwidth,pixwidth,.true.,exact_rendering,isperiodic(1),isperiodic(2)) |
| 671 |
endif |
| 672 |
endif |
| 673 |
|
| 674 |
if (ndim==3) then |
| 675 |
call minmaxmean_gridvec(datgridvec,npixels,ndimV,datmin,datmax,datmean) |
| 676 |
else |
| 677 |
call minmaxmean_gridvec2D(datgridvec2D,npixels,ndimV,datmin,datmax,datmean) |
| 678 |
endif |
| 679 |
do i=1,ndimV |
| 680 |
print fmtstring,' on grid :',datmin(i),datmax(i),datmean(i) |
| 681 |
enddo |
| 682 |
! |
| 683 |
!--write result to grid file |
| 684 |
! |
| 685 |
if (ndim==3 .and. do_output) then |
| 686 |
call write_grid(iunit,filename,outformat,ndim,ndimV,npixels,& |
| 687 |
label(irho),labelcoordsys(igeom),xlab,dtime,pixwidthx,xmin,xmax,ierr,& |
| 688 |
dat=datgrid,dat3D=datgridvec,label3D=label(iloc:iloc+ndimV),tagline=tagline,origin=origin) |
| 689 |
elseif (do_output) then |
| 690 |
do i=1,ndimV |
| 691 |
call write_grid(iunit,filename,outformat,ndim,ndimV,npixels,& |
| 692 |
label(iloc+i-1),labelcoordsys(igeom),xlab,dtime,pixwidthx,& |
| 693 |
xmin,xmax,ierr,dat2D=datgridvec2D(i,:,:),tagline=tagline,origin=origin) |
| 694 |
enddo |
| 695 |
endif |
| 696 |
endif |
| 697 |
print* |
| 698 |
enddo over_vec |
| 699 |
endif |
| 700 |
! else |
| 701 |
! print "(/,a)",' skipping remaining quantities (from SPLASH_TO_GRID_DENSITY_ONLY setting)' |
| 702 |
endif |
| 703 |
|
| 704 |
close(iunit) |
| 705 |
|
| 706 |
call deallocate_memory() |
| 707 |
return |
| 708 |
|
| 709 |
contains |
| 710 |
!------------------------------------------------------------ |
| 711 |
! manual mop-up of allocatable memory |
| 712 |
!------------------------------------------------------------ |
| 713 |
subroutine deallocate_memory() |
| 714 |
|
| 715 |
if (allocated(datgrid)) deallocate(datgrid) |
| 716 |
if (allocated(datgrid2D)) deallocate(datgrid2D) |
| 717 |
if (allocated(datgridvec)) deallocate(datgridvec) |
| 718 |
if (allocated(datgridvec2D)) deallocate(datgridvec2D) |
| 719 |
if (allocated(weight)) deallocate(weight) |
| 720 |
if (allocated(x)) deallocate(x) |
| 721 |
if (allocated(y)) deallocate(y) |
| 722 |
if (allocated(z)) deallocate(z) |
| 723 |
|
| 724 |
end subroutine deallocate_memory |
| 725 |
|
| 726 |
end subroutine convert_to_grid |
| 727 |
|
| 728 |
!------------------------------------------------------------ |
| 729 |
! get options for splash to grid from environment variables |
| 730 |
!------------------------------------------------------------ |
| 731 |
subroutine get_splash2grid_options(ndim,ncolstogrid,icoltogrid,isperiodic,xlab) |
| 732 |
use system_utils, only:lenvironment,renvironment,envlist,lenvstring,ienvstring |
| 733 |
use labels, only:irho |
| 734 |
integer, intent(in) :: ndim |
| 735 |
integer, intent(out) :: ncolstogrid,icoltogrid(:) |
| 736 |
logical, intent(out) :: isperiodic(3) |
| 737 |
character(len=1), intent(in) :: xlab(3) |
| 738 |
integer :: nstring,i,icol |
| 739 |
character(len=30), dimension(12) :: strings |
| 740 |
! |
| 741 |
!--SPLASH_TO_GRID can be set to comma separated list of columns |
| 742 |
! in order to select particular quantities for interpolation to grid |
| 743 |
! |
| 744 |
ncolstogrid = 0 |
| 745 |
icoltogrid(:) = 0 |
| 746 |
call envlist('SPLASH_TO_GRID',nstring,strings) |
| 747 |
if (nstring > 0) then |
| 748 |
do i=1,nstring |
| 749 |
icol = ienvstring(strings(i)) |
| 750 |
if (ienvstring(strings(i)) > 0) then |
| 751 |
ncolstogrid = ncolstogrid + 1 |
| 752 |
icoltogrid(ncolstogrid) = icol |
| 753 |
endif |
| 754 |
enddo |
| 755 |
endif |
| 756 |
! |
| 757 |
!--for backwards compatibility, support the SPLASH_TO_GRID_DENSITY_ONLY option |
| 758 |
! but only if SPLASH_TO_GRID is not set |
| 759 |
! |
| 760 |
if (ncolstogrid==0 .and. lenvironment('SPLASH_TO_GRID_DENSITY_ONLY')) then |
| 761 |
ncolstogrid = 1 |
| 762 |
icoltogrid(1) = irho |
| 763 |
endif |
| 764 |
! |
| 765 |
!--whether or not to wrap particle contributions across boundaries |
| 766 |
! |
| 767 |
isperiodic(:) = .false. |
| 768 |
call envlist('SPLASH_TO_GRID_PERIODIC',nstring,strings) |
| 769 |
if (nstring > ndim) then |
| 770 |
print "(a)",' ERROR in --periodic setting' |
| 771 |
nstring = ndim |
| 772 |
endif |
| 773 |
do i=1,nstring |
| 774 |
isperiodic(i) = lenvstring(strings(i)) |
| 775 |
enddo |
| 776 |
if (nstring==1) isperiodic(2:ndim) = isperiodic(1) |
| 777 |
|
| 778 |
if (all(isperiodic(1:ndim))) then |
| 779 |
print "(/,a)",' using PERIODIC boundaries (from --periodic setting)' |
| 780 |
elseif (isperiodic(1) .or. isperiodic(2) .or. isperiodic(3)) then |
| 781 |
print* |
| 782 |
do i=1,ndim |
| 783 |
if (isperiodic(i)) then |
| 784 |
print "(a)",' using PERIODIC boundaries in '//xlab(i)//' (from --periodic flag)' |
| 785 |
else |
| 786 |
print "(a)",' using NON-PERIODIC bounds in '//xlab(i)//' (from --periodic flag)' |
| 787 |
endif |
| 788 |
enddo |
| 789 |
else |
| 790 |
print "(/,a)",' using NON-PERIODIC boundaries: use --periodic=yes for periodic' |
| 791 |
if (ndim==3) then |
| 792 |
print "(a,/)",' or --periodic=yes,no,yes for mixed' |
| 793 |
else |
| 794 |
print "(a,/)",' or --periodic=yes,no for mixed' |
| 795 |
endif |
| 796 |
endif |
| 797 |
|
| 798 |
end subroutine |
| 799 |
|
| 800 |
!----------------------------------------------- |
| 801 |
! calculate max and min and mean values on grid |
| 802 |
!----------------------------------------------- |
| 803 |
subroutine minmaxmean_grid(datgrid,npixels,gridmin,gridmax,gridmean,nonzero,rho,grid_integral) |
| 804 |
real(doub_prec), dimension(:,:,:), intent(in) :: datgrid |
| 805 |
integer, dimension(3), intent(in) :: npixels |
| 806 |
real, intent(out) :: gridmin,gridmax,gridmean |
| 807 |
logical, intent(in) :: nonzero |
| 808 |
real(doub_prec), dimension(:,:,:), intent(in), optional :: rho |
| 809 |
real, intent(out), optional :: grid_integral |
| 810 |
|
| 811 |
real :: dati,gridint |
| 812 |
integer :: i,j,k |
| 813 |
|
| 814 |
gridmax = -huge(gridmax) |
| 815 |
gridmin = huge(gridmin) |
| 816 |
gridmean = 0. |
| 817 |
gridint = 0. |
| 818 |
!$omp parallel do schedule(static) default(shared) & |
| 819 |
!$omp reduction(min:gridmin) & |
| 820 |
!$omp reduction(max:gridmax) reduction(+:gridmean,gridint) & |
| 821 |
!$omp private(k,j,i,dati) |
| 822 |
do k=1,npixels(3) |
| 823 |
do j=1,npixels(2) |
| 824 |
do i=1,npixels(1) |
| 825 |
dati = datgrid(i,j,k) |
| 826 |
gridmax = max(gridmax,dati) |
| 827 |
if (nonzero) then |
| 828 |
if (dati > tiny(0.)) gridmin = min(gridmin,dati) |
| 829 |
else |
| 830 |
gridmin = min(gridmin,dati) |
| 831 |
endif |
| 832 |
gridmean = gridmean + dati |
| 833 |
if (present(grid_integral) .and. present(rho)) then |
| 834 |
gridint = gridint + dati*rho(i,j,k) |
| 835 |
endif |
| 836 |
enddo |
| 837 |
enddo |
| 838 |
enddo |
| 839 |
if (gridmin >= huge(gridmin)) gridmin = 0. |
| 840 |
gridmean = gridmean/product(npixels(1:3)) |
| 841 |
if (present(grid_integral)) grid_integral = gridint |
| 842 |
|
| 843 |
end subroutine minmaxmean_grid |
| 844 |
|
| 845 |
!---------------------------------------------------- |
| 846 |
! calculate max and min and mean values on grid (2D) |
| 847 |
!---------------------------------------------------- |
| 848 |
subroutine minmaxmean_grid2D(datgrid,npixels,gridmin,gridmax,gridmean,nonzero) |
| 849 |
real, dimension(:,:), intent(in) :: datgrid |
| 850 |
integer, dimension(2), intent(in) :: npixels |
| 851 |
real, intent(out) :: gridmin,gridmax,gridmean |
| 852 |
logical, intent(in) :: nonzero |
| 853 |
real :: dati |
| 854 |
integer :: i,j |
| 855 |
|
| 856 |
gridmax = -huge(gridmax) |
| 857 |
gridmin = huge(gridmin) |
| 858 |
gridmean = 0. |
| 859 |
!$omp parallel do schedule(static) & |
| 860 |
!$omp reduction(min:gridmin) & |
| 861 |
!$omp reduction(max:gridmax) reduction(+:gridmean) & |
| 862 |
!$omp private(j,i,dati) |
| 863 |
do j=1,npixels(2) |
| 864 |
do i=1,npixels(1) |
| 865 |
dati = datgrid(i,j) |
| 866 |
gridmax = max(gridmax,dati) |
| 867 |
if (nonzero) then |
| 868 |
if (dati > tiny(0.)) gridmin = min(gridmin,dati) |
| 869 |
else |
| 870 |
gridmin = min(gridmin,dati) |
| 871 |
endif |
| 872 |
gridmean = gridmean + dati |
| 873 |
enddo |
| 874 |
enddo |
| 875 |
if (gridmin >= huge(gridmin)) gridmin = 0. |
| 876 |
gridmean = gridmean/product(npixels(1:2)) |
| 877 |
|
| 878 |
end subroutine minmaxmean_grid2D |
| 879 |
|
| 880 |
!----------------------------------------------- |
| 881 |
! calculate max and min and mean values on grid |
| 882 |
! (for vector quantities) |
| 883 |
!----------------------------------------------- |
| 884 |
subroutine minmaxmean_gridvec(datgridvec,npixels,jlen,gridmin,gridmax,gridmean) |
| 885 |
real(doub_prec), dimension(:,:,:,:), intent(in) :: datgridvec |
| 886 |
integer, dimension(3), intent(in) :: npixels |
| 887 |
integer, intent(in) :: jlen |
| 888 |
real, dimension(jlen), intent(out) :: gridmin,gridmax,gridmean |
| 889 |
real :: dati |
| 890 |
integer :: ivec,i,j,k |
| 891 |
|
| 892 |
gridmax(:) = -huge(gridmax) |
| 893 |
gridmin(:) = huge(gridmin) |
| 894 |
gridmean(:) = 0. |
| 895 |
!$omp parallel do schedule(static) & |
| 896 |
!$omp reduction(min:gridmin) & |
| 897 |
!$omp reduction(max:gridmax) reduction(+:gridmean) & |
| 898 |
!$omp private(k,j,i,dati) |
| 899 |
do k=1,npixels(3) |
| 900 |
do j=1,npixels(2) |
| 901 |
do i=1,npixels(1) |
| 902 |
do ivec=1,jlen |
| 903 |
dati = datgridvec(ivec,i,j,k) |
| 904 |
gridmax(ivec) = max(gridmax(ivec),dati) |
| 905 |
gridmin(ivec) = min(gridmin(ivec),dati) |
| 906 |
gridmean(ivec) = gridmean(ivec) + dati |
| 907 |
enddo |
| 908 |
enddo |
| 909 |
enddo |
| 910 |
enddo |
| 911 |
!$omp end parallel do |
| 912 |
where (gridmin >= huge(gridmin)) gridmin = 0. |
| 913 |
gridmean(1:jlen) = gridmean(1:jlen)/real(product(npixels(1:3))) |
| 914 |
|
| 915 |
end subroutine minmaxmean_gridvec |
| 916 |
|
| 917 |
!----------------------------------------------- |
| 918 |
! calculate max and min and mean values on grid |
| 919 |
! (for vector quantities) |
| 920 |
!----------------------------------------------- |
| 921 |
subroutine minmaxmean_gridvec2D(datgridvec,npixels,jlen,gridmin,gridmax,gridmean) |
| 922 |
real, dimension(:,:,:), intent(in) :: datgridvec |
| 923 |
integer, dimension(2), intent(in) :: npixels |
| 924 |
integer, intent(in) :: jlen |
| 925 |
real, dimension(jlen), intent(out) :: gridmin,gridmax,gridmean |
| 926 |
real :: dati |
| 927 |
integer :: ivec,i,j |
| 928 |
|
| 929 |
gridmax(:) = -huge(gridmax) |
| 930 |
gridmin(:) = huge(gridmin) |
| 931 |
gridmean(:) = 0. |
| 932 |
!$omp parallel do schedule(static) & |
| 933 |
!$omp reduction(min:gridmin) & |
| 934 |
!$omp reduction(max:gridmax) reduction(+:gridmean) & |
| 935 |
!$omp private(j,i,dati) |
| 936 |
do j=1,npixels(2) |
| 937 |
do i=1,npixels(1) |
| 938 |
do ivec=1,jlen |
| 939 |
dati = datgridvec(ivec,i,j) |
| 940 |
gridmax(ivec) = max(gridmax(ivec),dati) |
| 941 |
gridmin(ivec) = min(gridmin(ivec),dati) |
| 942 |
gridmean(ivec) = gridmean(ivec) + dati |
| 943 |
enddo |
| 944 |
enddo |
| 945 |
enddo |
| 946 |
!$omp end parallel do |
| 947 |
gridmean(1:jlen) = gridmean(1:jlen)/real(product(npixels(1:2))) |
| 948 |
|
| 949 |
end subroutine minmaxmean_gridvec2D |
| 950 |
|
| 951 |
!---------------------------------------------------- |
| 952 |
! calculate max and min and mean values on particles |
| 953 |
!---------------------------------------------------- |
| 954 |
subroutine minmaxmean_part(dat,mask,npart,partmin,partmax,partmean,nonzero,mass,part_integral) |
| 955 |
real, dimension(:,:), intent(in) :: dat |
| 956 |
logical, dimension(:), intent(in) :: mask |
| 957 |
integer, intent(in) :: npart |
| 958 |
real, dimension(3), intent(out) :: partmin,partmax,partmean |
| 959 |
logical, intent(in), optional :: nonzero |
| 960 |
real, dimension(:), intent(in), optional :: mass |
| 961 |
real, dimension(3), intent(out), optional :: part_integral |
| 962 |
real :: partval |
| 963 |
integer :: np,jlen,i,j |
| 964 |
logical :: usenonzero |
| 965 |
|
| 966 |
usenonzero = .false. |
| 967 |
if (present(nonzero)) usenonzero = nonzero |
| 968 |
|
| 969 |
partmax(:) = -huge(partmax) |
| 970 |
partmin(:) = huge(partmin) |
| 971 |
partmean(:) = 0. |
| 972 |
np = 0 |
| 973 |
jlen = min(size(dat(1,:)),3) |
| 974 |
if (present(part_integral)) part_integral(:) = 0. |
| 975 |
|
| 976 |
!--could do this in parallel but reduction on arrays |
| 977 |
! does not seem to work in ifort |
| 978 |
!!$omp parallel do default(none) schedule(static) & |
| 979 |
!!$omp shared(dat,weight,jlen,npart,usenonzero) & |
| 980 |
!!$omp reduction(min:partmin) & |
| 981 |
!!$omp reduction(max:partmax) & |
| 982 |
!!$omp reduction(+:partmean,np) & |
| 983 |
!!$omp private(i,j,partval) |
| 984 |
do i=1,npart |
| 985 |
!--only count particles used in the rendering |
| 986 |
if (mask(i)) then |
| 987 |
np = np + 1 |
| 988 |
do j=1,jlen |
| 989 |
partval = dat(i,j) |
| 990 |
if (usenonzero) then |
| 991 |
if (partval > tiny(0.)) partmin(j) = min(partmin(j),partval) |
| 992 |
else |
| 993 |
partmin(j) = min(partmin(j),partval) |
| 994 |
endif |
| 995 |
partmax(j) = max(partmax(j),partval) |
| 996 |
partmean(j) = partmean(j) + partval |
| 997 |
if (present(mass) .and. present(part_integral)) then |
| 998 |
part_integral(j) = part_integral(j) + mass(i)*partval |
| 999 |
endif |
| 1000 |
enddo |
| 1001 |
endif |
| 1002 |
enddo |
| 1003 |
!!$omp end parallel do |
| 1004 |
|
| 1005 |
if (np > 0) then |
| 1006 |
partmean(:) = partmean(:)/real(np) |
| 1007 |
endif |
| 1008 |
|
| 1009 |
end subroutine minmaxmean_part |
| 1010 |
|
| 1011 |
!---------------------------------------------------- |
| 1012 |
! calculate max and min and mean values on particles |
| 1013 |
!---------------------------------------------------- |
| 1014 |
logical function iszero(partmin,partmax,ndim) |
| 1015 |
real, dimension(:), intent(in) :: partmin,partmax |
| 1016 |
integer, intent(in) :: ndim |
| 1017 |
|
| 1018 |
if (all(abs(partmin(1:ndim)) < tiny(0.)) .and. & |
| 1019 |
all(abs(partmax(1:ndim)) < tiny(0.))) then |
| 1020 |
iszero = .true. |
| 1021 |
print "(a)",' min=max=0 on particles: skipping pointless interpolation and setting dat = 0.' |
| 1022 |
else |
| 1023 |
iszero = .false. |
| 1024 |
endif |
| 1025 |
|
| 1026 |
end function iszero |
| 1027 |
|
| 1028 |
!---------------------------------------------------- |
| 1029 |
! total mass on particles in the grid volume |
| 1030 |
!---------------------------------------------------- |
| 1031 |
subroutine get_mass_in_box(ninterp,x,y,z,m,mask,mtot,icoords,igeom,xmin,xmax) |
| 1032 |
use geometry, only:coord_transform,igeom_cartesian,igeom_cylindrical,igeom_spherical |
| 1033 |
integer, intent(in) :: ninterp |
| 1034 |
real, intent(in) :: x(ninterp),y(ninterp),z(ninterp),m(ninterp) |
| 1035 |
logical, intent(inout) :: mask(ninterp) |
| 1036 |
real, intent(out) :: mtot |
| 1037 |
integer, intent(in) :: icoords,igeom |
| 1038 |
real, intent(in) :: xmin(3),xmax(3) |
| 1039 |
real :: xc(3),xi(3),tot_volume |
| 1040 |
real, parameter :: pi = 4.*atan(1.) |
| 1041 |
integer :: i |
| 1042 |
|
| 1043 |
if (igeom == igeom_cartesian) then |
| 1044 |
mask = (mask .and. (x >= xmin(1) .and. x <= xmax(1)) .and. & |
| 1045 |
(y >= xmin(2) .and. y <= xmax(2)) .and. & |
| 1046 |
(z >= xmin(3) .and. z <= xmax(3))) |
| 1047 |
mtot = sum(m(1:ninterp),mask=mask) |
| 1048 |
else |
| 1049 |
mtot = 0. |
| 1050 |
do i=1,ninterp |
| 1051 |
if (mask(i)) then |
| 1052 |
xi = [x(i),y(i),z(i)] |
| 1053 |
call coord_transform(xi,3,icoords,xc,3,igeom) |
| 1054 |
if (xc(1) >= xmin(1) .and. xc(1) <= xmax(1) .and. & |
| 1055 |
xc(2) >= xmin(2) .and. xc(2) <= xmax(2) .and. & |
| 1056 |
xc(3) >= xmin(3) .and. xc(3) <= xmax(3)) then |
| 1057 |
mtot = mtot + m(i) |
| 1058 |
endif |
| 1059 |
endif |
| 1060 |
enddo |
| 1061 |
select case(igeom) |
| 1062 |
case(igeom_cylindrical) |
| 1063 |
tot_volume = pi*(xmax(1)**2 - xmin(1)**2)*(xmax(3) - xmin(3)) |
| 1064 |
case(igeom_spherical) |
| 1065 |
tot_volume = 4./3.*pi*(xmax(1)**3 - xmin(1)**3) |
| 1066 |
case default |
| 1067 |
tot_volume = 0. |
| 1068 |
end select |
| 1069 |
if (tot_volume > 0.) print "(20x,a,es10.4,/)",'grid volume: ',tot_volume |
| 1070 |
endif |
| 1071 |
|
| 1072 |
end subroutine get_mass_in_box |
| 1073 |
|
| 1074 |
!---------------------------------------------------- |
| 1075 |
! total mass on grid with correct volume elements for |
| 1076 |
! non-Cartesian geometries (R dR d(phi) dz or r^2 sin(theta) dr d(phi) d(theta)) |
| 1077 |
!---------------------------------------------------- |
| 1078 |
subroutine grid_total_mass_geom(rhogrid,npixels,pixwidthx,xmin,igeom,mtotgrid) |
| 1079 |
use params, only:doub_prec |
| 1080 |
use geometry, only:igeom_cylindrical,igeom_spherical |
| 1081 |
integer, intent(in) :: npixels(3),igeom |
| 1082 |
real, intent(in) :: pixwidthx(3),xmin(3) |
| 1083 |
real(doub_prec), intent(in) :: rhogrid(npixels(1),npixels(2),npixels(3)) |
| 1084 |
real, intent(out) :: mtotgrid |
| 1085 |
integer :: i,j,k |
| 1086 |
real :: r,theta,vol |
| 1087 |
|
| 1088 |
mtotgrid = 0. |
| 1089 |
select case(igeom) |
| 1090 |
case(igeom_cylindrical) |
| 1091 |
do k = 1, npixels(3) |
| 1092 |
do j = 1, npixels(2) |
| 1093 |
do i = 1, npixels(1) |
| 1094 |
r = real(xmin(1) + (i - 0.5)*pixwidthx(1), kind=doub_prec) |
| 1095 |
vol = r * real(pixwidthx(1)*pixwidthx(2)*pixwidthx(3), kind=doub_prec) |
| 1096 |
mtotgrid = mtotgrid + rhogrid(i,j,k)*vol |
| 1097 |
enddo |
| 1098 |
enddo |
| 1099 |
enddo |
| 1100 |
case(igeom_spherical) |
| 1101 |
do k = 1, npixels(3) |
| 1102 |
theta = xmin(3) + (k - 0.5)*pixwidthx(3) |
| 1103 |
do j = 1, npixels(2) |
| 1104 |
do i = 1, npixels(1) |
| 1105 |
r = real(xmin(1) + (i - 0.5)*pixwidthx(1), kind=doub_prec) |
| 1106 |
vol = r*r*sin(theta) * real(product(pixwidthx), kind=doub_prec) |
| 1107 |
mtotgrid = mtotgrid + rhogrid(i,j,k)*vol |
| 1108 |
enddo |
| 1109 |
enddo |
| 1110 |
enddo |
| 1111 |
case default |
| 1112 |
mtotgrid = sum(rhogrid)*product(real(pixwidthx, kind=doub_prec)) |
| 1113 |
end select |
| 1114 |
|
| 1115 |
end subroutine grid_total_mass_geom |
| 1116 |
|
| 1117 |
end module convert_grid |