Debian Codegraph @

tests/test_interpolate3D.f90

raw
1 !
2 !--unit test for interpolation routines
3 !
4 program test_interpolation
5 use projections3D
6 use xsections3D
7 implicit none
8 integer, parameter :: idimx = 100
9 integer, parameter :: idim = idimx**3
10 integer, parameter :: ipixx = 1000, ipixy = 1000
11 integer :: npart,npartx,nparty,npartz
12 integer :: npixx, npixy,i
13 real, parameter :: errtol = 1.e-7
14 real, dimension(idim) :: x,y,z,pmass,h,rho
15 real, dimension(idim) :: dat,weight
16 integer, dimension(idim) :: itype
17 real, dimension(ipixx,ipixy) :: datpix
18 real, dimension(0:maxcoltable) :: q,w
19 real :: xmin,xmax,ymin,ymax,zmin,zmax
20 real :: columndens,dxpix,err,dens,datmax
21 real :: trans(6)
22 logical :: ifastrender,normalise
23
24 xmin = -0.5
25 xmax = 0.5
26 ymin = -0.5
27 ymax = 0.5
28 zmin = -0.5
29 zmax = 0.5
30 itype = 0
31 ifastrender = .true.
32 print*,'accelerated rendering = ',ifastrender
33 call pgopen('?')
34 ! call pgenv(xmin,xmax,ymin,ymax,0,0)
35 ! call pglabel('x','y',' ')
36 ! call pgpt(npart,x,y,1)
37 ! call pgenv(xmin,xmax,ymin,ymax,0,0)
38 ! call pglabel('y','z',' ')
39 ! call pgpt(npart,y,z,1)
40 ! call pgenv(xmin,xmax,ymin,ymax,0,0)
41 ! call pglabel('x','z',' ')
42 ! call pgpt(npart,x,z,1)
43 !
44 !--setup integrated kernel table
45 !
46 call setup_integratedkernel
47 !
48 !--check value of the integration at q=zero (can do this analytically)
49 !
50 if (abs(coltable(0)-1.5/3.1415926536).lt.errtol) then
51 print*,'CENTRAL KERNEL TABLE OK'
52 else
53 print*,'coltable(0) = ',coltable(0),' should be ',2.*0.75/3.1415926536
54 print*,'error = ',abs(coltable(0)-1.5/3.1415926536)
55 print*,'ERROR: CENTRAL INTEGRATED KERNEL VALUE WRONG'
56 endif
57 !
58 !--plot integrated kernel
59 !
60 call pgenv(0.,2.,0.,coltable(0)*1.1,0,0)
61 call pglabel('r','int W',' ')
62 print*,'radkernel = ',radkernel
63
64 do i=0,50
65 q(i) = i*radkernel/50.
66 !print*,q(i)
67 w(i) = wfromtable(q(i)*q(i))
68 enddo
69 call pgsci(3)
70 call pgline(50,q,w)
71
72 ! do i=1,maxcoltable
73 ! q(i) = sqrt((i-1)*radkernel*radkernel*dmaxcoltable)
74 ! enddo
75 ! call pgsci(2)
76 ! call pgline(maxcoltable,q,coltable)
77 call pgsci(1)
78 !call pgpage
79 !
80 !--setup one particle
81 !
82 print*,'SINGLE PARTICLE TEST'
83 npart = 1
84 npixx = 10
85 npixy = 10
86 x(1) = 0.5*(xmin + xmax)
87 y(1) = 0.5*(ymin + ymax)
88 z(1) = 0.5*(zmin + zmax)
89 rho(1) = 1.0
90 pmass(1) = 2.0
91 h(1) = 0.35*xmax
92 weight(1) = 1./1.5**3
93 dat(1) = rho(1)
94 dxpix = (xmax-xmin)/real(npixx)
95 normalise = .false.
96 datpix = 0.
97 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
98 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
99 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,.false.)
100 call pgenv(xmin,xmax,ymin,ymax,1,0)
101 trans = 0.
102 trans(1) = xmin - 0.5*dxpix
103 trans(2) = dxpix
104 trans(4) = ymin - 0.5*dxpix
105 trans(6) = dxpix
106 datmax = maxval(datpix(1:npixx,1:npixy))
107 print*,'max datpix = ',datmax,maxloc(datpix(1:npixx,1:npixy))
108 if (abs(datmax-0.035367373).gt.errtol) then
109 print*,'FAILED: central maximum wrong, error = ',abs(datmax-0.035367373)
110 else
111 print*,'OK: central maximum seems fine'
112 endif
113 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,datmax,trans)
114
115 print*,'TEST WITH ACCELERATION'
116 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
117 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
118 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,.true.)
119 call pgenv(xmin,xmax,ymin,ymax,1,0)
120 trans = 0.
121 trans(1) = xmin - 0.5*dxpix
122 trans(2) = dxpix
123 trans(4) = ymin - 0.5*dxpix
124 trans(6) = dxpix
125 datmax = maxval(datpix(1:npixx,1:npixy))
126 print*,'max datpix = ',datmax,maxloc(datpix(1:npixx,1:npixy))
127 if (abs(datmax-0.035367373).gt.errtol) then
128 print*,'FAILED: central maximum wrong, error = ',abs(datmax-0.035367373)
129 else
130 print*,'OK: central maximum seems fine'
131 endif
132 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,datmax,trans)
133 !
134 !--setup two overlapping particles
135 !
136 print*,'TWO PARTICLE TEST'
137 npart = 2
138 npixx = 1000
139 npixy = 1000
140 x(1) = -0.25
141 x(2) = 0.25
142 y(2) = 0.5*(ymin + ymax)
143 z(2) = 0.5*(zmin + zmax)
144 rho(2) = 1.0
145 pmass(2) = 2.0
146 h(1:2) = 0.5*xmax
147 weight(2) = 1./1.5**3
148 dat(2) = rho(2)
149 dxpix = (xmax-xmin)/real(npixx)
150 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
151 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
152 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,ifastrender)
153 call pgenv(xmin,xmax,ymin,ymax,1,0)
154 trans = 0.
155 trans(1) = xmin - 0.5*dxpix
156 trans(2) = dxpix
157 trans(4) = ymin - 0.5*dxpix
158 trans(6) = dxpix
159 datmax = maxval(datpix(1:npixx,1:npixy))
160 print*,'max datpix = ',datmax,maxloc(datpix(1:npixx,1:npixy))
161
162 !!print*,'datpix = ',datpix(1:npixx,1:npixy)
163 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,datmax,trans)
164 !
165 !--set up a cubic lattice of particles
166 !
167 print*,'NORMAL LATTICE TEST'
168 npartx = 50
169 nparty = 50
170 npartz = 50
171 npart = npartx*nparty*npartz
172 npixx = 500
173 npixy = 500
174 dxpix = (xmax-xmin)/real(npixx)
175 call setgrid(npartx,nparty,npartz,x,y,z,pmass,rho,h,weight,xmin,xmax,ymin,ymax,zmin,zmax)
176 !
177 !--now call interpolation routine to pixels
178 !
179 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
180 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
181 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,ifastrender)
182 !
183 !--check output
184 !
185 dens = rho(1)
186 columndens = dens*(zmax-zmin)
187 call geterr(datpix(1:npixx,1:npixy),npixx,npixy,columndens,err)
188 print "(70('-'))"
189 print*,'average error in column density interpolation = ',err
190 if (err.gt.0.05) then
191 print*,'FAILED: average error > usual'
192 else
193 print*,'OK: average error same as usual'
194 endif
195
196 call pgenv(xmin,xmax,ymin,ymax,0,0)
197 ! call pgpixl(datpix,ipixx,ipixy,1,npixx,1,npixy,xmin,xmax,ymin,ymax)
198 trans = 0.
199 trans(1) = xmin - 0.5*dxpix
200 trans(2) = dxpix
201 trans(4) = ymin - 0.5*dxpix
202 trans(6) = dxpix
203 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,1.0,trans)
204 !
205 !--NORMALISED VERSION OF ABOVE
206 !
207 normalise = .true.
208 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
209 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
210 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,ifastrender)
211 !
212 !--check output
213 !
214 dens = rho(1)
215 call geterr(datpix(1:npixx,1:npixy),npixx,npixy,dens,err)
216 print "(70('-'))"
217 print*,'average error in <density> interpolation = ',err
218 print*,' dens = ',dens,' datpix = ',datpix(1:10,1:10)
219 if (err.gt.0.05) then
220 print*,'FAILED: average error > usual'
221 else
222 print*,'OK: average error same as usual'
223 endif
224 call pgenv(xmin,xmax,ymin,ymax,0,0)
225 ! call pgpixl(datpix,ipixx,ipixy,1,npixx,1,npixy,xmin,xmax,ymin,ymax)
226 trans = 0.
227 trans(1) = xmin - 0.5*dxpix
228 trans(2) = dxpix
229 trans(4) = ymin - 0.5*dxpix
230 trans(6) = dxpix
231 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,1.0,trans)
232 !
233 !--take cross section at midplane and check density
234 !
235 print "(70('-'))"
236 call interpolate3D_fastxsec(x(1:npart),y(1:npart),z(1:npart), &
237 h(1:npart),weight(1:npart),dat(1:npart),itype(1:npart),npart,&
238 xmin,ymin,0.0,datpix(1:npixx,1:npixy),npixx,npixy,dxpix,.false.)
239
240 call geterr(datpix(1:npixx,1:npixy),npixx,npixy,dens,err)
241 print*,'average error in non-normalised xsec interpolation = ',err
242 print "(70('-'))"
243
244
245 call pgenv(xmin,xmax,ymin,ymax,0,0)
246 ! call pgpixl(datpix,ipixx,ipixy,1,npixx,1,npixy,xmin,xmax,ymin,ymax)
247 ! trans = 0.
248 ! trans(1) = xmin - 0.5*dxpix
249 ! trans(2) = dxpix
250 ! trans(4) = ymin - 0.5*dxpix
251 ! trans(6) = dxpix
252 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,1.0,trans)
253 ! call pgend
254 !
255 !--take normalised cross section at midplane and check density
256 !
257 call interpolate3D_fastxsec(x(1:npart),y(1:npart),z(1:npart), &
258 h(1:npart),weight(1:npart),dat(1:npart),itype(1:npart),npart,&
259 xmin,ymin,0.0,datpix(1:npixx,1:npixy),npixx,npixy,dxpix,.true.)
260
261 call geterr(datpix(1:npixx,1:npixy),npixx,npixy,dens,err)
262 print*,'average error in normalised xsec interpolation = ',err
263 call pgenv(xmin,xmax,ymin,ymax,0,0)
264 call pgimag(datpix,ipixx,ipixy,1,npixx,1,npixy,0.0,1.0,trans)
265
266 print*,'closing PGPLOT'
267 call pgend
268
269 print "(70('-'))"
270
271 print*,'SPEED CHECKS...'
272
273 normalise = .true.
274 npixx = 1
275 npixy = 1
276 npartx = idimx
277 nparty = idimx
278 npartz = idimx
279 npart = npartx*nparty*npartz
280 call setgrid(npartx,nparty,npartz,x,y,z,pmass,rho,h,weight,xmin,xmax,ymin,ymax,zmin,zmax)
281
282 dxpix = (xmax-xmin)/real(npixx)
283 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
284 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
285 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,ifastrender)
286
287 call geterr(datpix(1:npixx,1:npixy),npixx,npixy,columndens,err)
288 print*,'average error in projection = ',err
289
290 npixx = 1000
291 npixy = 1000
292 npartx = 2
293 nparty = 2
294 npartz = 2
295 npart = npartx*nparty*npartz
296 call setgrid(npartx,nparty,npartz,x,y,z,pmass,rho,h,weight,xmin,xmax,ymin,ymax,zmin,zmax)
297
298 dxpix = (xmax-xmin)/real(npixx)
299 call interpolate3D_projection(x(1:npart),y(1:npart),z(1:npart),h(1:npart), &
300 weight(1:npart),dat(1:npart),itype(1:npart),npart,xmin,ymin, &
301 datpix(1:npixx,1:npixy),npixx,npixy,dxpix,dxpix,normalise,0.,0.,ifastrender)
302
303 call geterr(datpix(1:npixx,1:npixy),npixx,npixy,columndens,err)
304 print*,'average error in projection = ',err
305
306
307
308 contains
309
310 subroutine setgrid(npartx,nparty,npartz,x,y,z,pmass,rho,h,weight,xmin,xmax,ymin,ymax,zmin,zmax)
311 implicit none
312 integer , intent(in) :: npartx,nparty,npartz
313 real, dimension(:), intent(out) :: x,y,z,pmass,rho,h,weight
314 real, intent(in) :: xmin,xmax,ymin,ymax,zmin,zmax
315 integer :: ipart,k,j,i
316 real :: dx,dy,dz,ypos,zpos
317 real :: totmass,massp,vol,dens,h0
318
319 dz = (zmax-zmin)/real(npartz - 1)
320 dy = (ymax-ymin)/real(nparty - 1)
321 dx = (xmax-xmin)/real(npartx - 1)
322 ipart = 0
323
324 do k=1,npartz
325 zpos = zmin + (k-1)*dz
326 do j=1,nparty
327 ypos = ymin + (j-1)*dy
328 do i=1,npartx
329 ipart = ipart + 1
330 x(ipart) = xmin + (i-1)*dx
331 y(ipart) = ypos
332 z(ipart) = zpos
333 ! print*,ipart,'x,y,z=',x(ipart),y(ipart),z(ipart)
334 enddo
335 enddo
336 enddo
337
338 npart = npartx*nparty*npartz
339 !
340 !--set other properties
341 !
342 totmass = 3.1415926536
343 massp = totmass/real(npart)
344 vol = (xmax-xmin)*(ymax-ymin)*(zmax-zmin)
345 dens = totmass/vol
346 h0 = 1.5*(massp/dens)**(1./3)
347 print*,' testing ',npart,' particles in a cube configuration'
348 print*,' dx = ',dx,' dy = ',dy,' dz = ',dz
349 print*,' mass = ',massp,' dens = ',dens,' h = ',h0
350 print*,' approx density = ',massp/(dx*dy*dz)
351 do i = 1,npart
352 pmass(i) = massp
353 rho(i) = dens
354 h(i) = h0
355 dat(i) = rho(i)
356 weight(i) = pmass(i)/(rho(i)*h(i)**3)
357 enddo
358
359 end subroutine setgrid
360
361 subroutine geterr(datpix,npixx,npixy,datexact,err)
362 implicit none
363 integer, intent(in) :: npixx,npixy
364 real, dimension(:,:), intent(in) :: datpix
365 real, intent(in) :: datexact
366 real, intent(out) :: err
367 integer :: icalc,j,i
368 real :: erri
369
370 err = 0.
371 icalc = 0
372 do j=2,npixy-1
373 do i=2,npixx-1
374 icalc = icalc + 1
375 erri = abs(datpix(i,j)-datexact)/datexact
376 err = err + erri
377 !if (erri.gt.0.05) print*,i,j,' xsec dens = ',datpix(i,j),' should be ',dens
378 enddo
379 enddo
380 if (icalc.le.0) then
381 print*,'cannot calculate error => npix too small'
382 err = -1.0
383 else
384 err = err/real(icalc)
385 endif
386
387 end subroutine geterr
388
389 end program test_interpolation