-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadCellArray.f90
276 lines (235 loc) · 7.39 KB
/
readCellArray.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
program readCellArray
use definitions
use rotateIndicesModule
implicit none
integer :: i, j, k, icell, jcell, kcell, nxmap, nymap, i0, j0, k0, lmax, nx, ny, nz
integer(1) :: izone
real(kind=RealKind) :: xnew, ynew, znew, x0, y0, zslice, z0
integer, dimension(3) :: baseGridSize
integer, dimension(2,2,2) :: is, js, ks
integer sfstart, sffinfo, sfselect, sfginfo, sfrdata, sfendacc, sfend, sfcreate, sfwdata
real*4, dimension(:,:), pointer :: mapArray
character(60) :: dirname
dirname = '/path/to/K15-06-64/3.6-rot/'
sd_id = sfstart(trim(dirname)//'cellArray0016.h4', dfacc_read)
nxmap = 4096
nymap = nxmap
izone = 2
zslice = 0.506896972656250
status = sffinfo(sd_id, n_datasets, n_file_attrs)
start = 0
edges(1) = 3
stride = 1
sds_id = sfselect(sd_id, 0)
status = sfrdata(sds_id, start, stride, edges, baseGridSize)
status = sfendacc(sds_id)
write(*,*) 'base grid size =', baseGridSize
nx = baseGridSize(1)
ny = baseGridSize(2)
nz = baseGridSize(3)
sds_id = sfselect(sd_id, 1)
status = sfginfo(sds_id,sds_name,rank,dim_sizes,data_type,n_attrs)
if (status.ne.0 .or. rank.ne.1) then
write(*,*) 'error reading ...'
stop
endif
ncosmic = dim_sizes(1)
edges(1) = dim_sizes(1)
allocate (cellArrayLevel(ncosmic))
allocate (cellArrayHI(ncosmic))
allocate(cellArrayTemp(ncosmic))
allocate(cellArrayDensity(ncosmic))
status = sfrdata(sds_id, start, stride, edges, cellArrayLevel)
status = sfendacc(sds_id)
sds_id = sfselect(sd_id, 2)
status = sfrdata(sds_id, start, stride, edges, cellArrayHI)
status = sfendacc(sds_id)
sds_id = sfselect(sd_id, 5)
status = sfrdata(sds_id, start, stride, edges, cellArrayTemp)
status = sfendacc(sds_id)
sds_id = sfselect(sd_id, 6)
status = sfrdata(sds_id, start, stride, edges, cellArrayDensity)
status = sfendacc(sds_id)
status = sfend(sd_id)
print*, ncosmic
write(*,*) cellArrayLevel(1)
write(*,*) cellArrayHI(1)
write(*,*) cellArrayTemp(1)
write(*,*) cellArrayDensity(1)
! open(14,file=trim(dirname)//'cellArray.dat',status='replace',form='unformatted')
! write(14) (cellArrayLevel(i),i=1,ncosmic)
! write(14) (cellArrayHI(i),i=1,ncosmic)
! write(14) (cellArrayTemp(i),i=1,ncosmic)
! write(14) (cellArrayDensity(i),i=1,ncosmic)
! close(14)
allocate(baseGrid%cell(nx,ny,nz))
icosmic = 0
lmax = 0
do i = 1, nx
do j = 1, ny
do k = 1, nz
baseGrid%cell(i,j,k)%refined = .false.
call createFullyThreadedStructure(baseGrid%cell(i,j,k),0,lmax)
enddo
enddo
enddo
write(*,*) 'maximum level of refinement =', lmax
deallocate(cellArrayLevel,cellArrayHI)
stop
! densestCell => baseGrid%cell(1,1,1)
! do i = 1, nx
! x0 = (float(i)-0.5)/float(nx)
! do j = 1, ny
! y0 = (float(j)-0.5)/float(ny)
! do k = 1, nz
! z0 = (float(k)-0.5)/float(nz)
! call findDensestCell(baseGrid%cell(i,j,k),x0,y0,z0,1./float(nx))
! enddo
! enddo
! enddo
! print*, 'peak:', densestCell%HI, xbase, ybase, zbase
! ------------------------ plot HI physical density in a slice ----------------
do i = 1, 2
do j = 1, 2
do k = 1, 2
call rotateIndices(i,j,k,2,2,2,izone,is(i,j,k),js(i,j,k),ks(i,j,k))
enddo
enddo
enddo
allocate(mapArray(nxmap,nymap))
do i = 1, nxmap
print*, 'doing', i, ' out of', nxmap
x0 = (float(i)-0.5)/float(nxmap)
i0 = int(x0*nx) + 1
do j = 1, nymap
y0 = (float(j)-0.5)/float(nymap)
j0 = int(y0*ny) + 1
xnew = x0*float(nx) - float(i0-1)
ynew = y0*float(ny) - float(j0-1)
mapArray(i,j) = 0.
k0 = int(zslice*nz) + 1
znew = zslice*float(nz) - float(k0-1)
call rotateIndices(i0,j0,k0,nx,ny,nz,izone,icell,jcell,kcell)
call sliceCell(baseGrid%cell(icell,jcell,kcell),mapArray(i,j), &
xnew,ynew,znew,is,js,ks)
enddo
enddo
sd_id = sfstart(trim(dirname)//'map.h4',dfacc_create)
edges(1) = nxmap
edges(2) = nymap
sds_id = sfcreate(sd_id,'map',dfnt_float32,2,edges)
start = 0
stride = 1
status = sfwdata(sds_id,start,stride,edges,mapArray)
status = sfendacc(sds_id)
status = sfend(sd_id)
deallocate(mapArray)
contains
recursive subroutine createFullyThreadedStructure(currentCell,level,lmax)
use definitions
implicit none
type(zoneType), target :: currentCell
integer, intent(in) :: level
integer, intent(inout) :: lmax
integer :: i, j, k
icosmic = icosmic + 1
lmax = max(cellArrayLevel(icosmic),lmax)
if (cellArrayLevel(icosmic).eq.level) then
currentCell%HI = dble(cellArrayHI(icosmic))
else
if (cellArrayLevel(icosmic).gt.level) then
allocate(currentCell%cell(2,2,2))
currentCell%refined = .true.
icosmic = icosmic - 1
do i = 1, 2
do j = 1, 2
do k = 1, 2
currentCell%cell(i,j,k)%refined = .false.
call createFullyThreadedStructure(currentCell%cell(i,j,k),level+1,lmax)
enddo
enddo
enddo
else
write(*,*) 'error in levels', icosmic, cellArrayLevel(icosmic), level
stop
endif
endif
end subroutine createFullyThreadedStructure
recursive subroutine sliceCell(currentCell,currentPixel, &
x0,y0,z0,is,js,ks)
use definitions
implicit none
type(zoneType) :: currentCell
real*4, intent(inout) :: currentPixel
real(kind=RealKind), intent(in) :: x0, y0, z0
integer, dimension(2,2,2), intent(in) :: is, js, ks
integer :: i, j, k
real(kind=RealKind) :: xnew, ynew, znew
if (currentCell%refined) then
if (x0.lt.0.5) then
i = 1
xnew = 2.*x0
else
i = 2
xnew = 2.*x0 - 1.
endif
if (y0.lt.0.5) then
j = 1
ynew = 2.*y0
else
j = 2
ynew = 2.*y0 - 1.
endif
if (z0.lt.0.5) then
k = 1
znew = 2.*z0
else
k = 2
znew = 2.*z0 - 1.
endif
call sliceCell(currentCell%cell(is(i,j,k),js(i,j,k),ks(i,j,k)),currentPixel, &
xnew,ynew,znew,is,js,ks)
else
currentPixel = currentCell%HI
endif
end subroutine sliceCell
recursive subroutine findDensestCell(currentCell,x0,y0,z0,cellSize)
use definitions
implicit none
type(zoneType), target :: currentCell
real(kind=RealKind), intent(in) :: x0, y0, z0, cellSize
integer :: i, j, k
real(kind=RealKind) :: xnew, ynew, znew
if (currentCell%refined) then
do i = 1, 2
if (i.eq.1) then
xnew = x0 - 0.25*cellSize
else
xnew = x0 + 0.25*cellSize
endif
do j = 1, 2
if (j.eq.1) then
ynew = y0 - 0.25*cellSize
else
ynew = y0 + 0.25*cellSize
endif
do k = 1, 2
if (k.eq.1) then
znew = z0 - 0.25*cellSize
else
znew = z0 + 0.25*cellSize
endif
call findDensestCell(currentCell%cell(i,j,k),xnew,ynew,znew,cellSize/2.)
enddo
enddo
enddo
else
if (currentCell%HI.gt.densestCell%HI) then
densestCell => currentCell
xbase = x0
ybase = y0
zbase = z0
endif
endif
end subroutine findDensestCell
end program readCellArray