Debian Codegraph @

src/interpolation.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-2019 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! Module containing utility routines for SPH kernel interpolation
25 !-----------------------------------------------------------------
26 module interpolation
27 implicit none
28 public :: iroll, set_interpolation_weights, get_n_interp
29 real, parameter, public :: weight_sink = -1.
30 integer, parameter, public :: doub_prec = kind(0.d0)
31 private
32
33 contains
34
35 !--------------------------------------------------------------------------
36 !
37 ! utility to wrap pixel index around periodic domain
38 ! indices that roll beyond the last position are re-introduced at the first
39 !
40 !--------------------------------------------------------------------------
41 pure integer function iroll(i,n)
42 integer, intent(in) :: i,n
43
44 if (i > n) then
45 iroll = mod(i-1,n) + 1
46 elseif (i < 1) then
47 iroll = n + mod(i,n) ! mod is negative
48 else
49 iroll = i
50 endif
51
52 end function iroll
53
54 !--------------------------------------------------------------------------
55 !
56 ! get maximum number of particles for which interpolation weights
57 ! need to be computed
58 !
59 !--------------------------------------------------------------------------
60 integer function get_n_interp(ntypes,npartoftype,UseType,iplotpartoftype,&
61 iamtype_size,rendersinks) result(ninterp)
62 integer, intent(in) :: ntypes,iamtype_size
63 integer, intent(in) :: npartoftype(ntypes)
64 logical, intent(in) :: iplotpartoftype(ntypes),UseType(ntypes),rendersinks
65 integer :: ntoti
66
67 ntoti = sum(npartoftype)
68 ninterp = npartoftype(1) ! by default, only the gas particles
69 if (any(UseType(2:ntypes) .and. iplotpartoftype(2:ntypes)) &
70 .or. iamtype_size > 1 .or. rendersinks) ninterp = ntoti
71
72 end function get_n_interp
73
74 !-------------------------------------------------------------------
75 ! Set interpolation weights for the particles. The weights are
76 ! calculated using:
77 !
78 ! w = m/(rho*h**ndim),
79 !
80 ! where we need to handle a few special scenarios:
81 !
82 ! 1) Firstly, the weight should be calculated in a consistent
83 ! set of units. Safest way is to use the data as originally
84 ! read from the dump file, before any unit scaling was applied.
85 !
86 ! 2) Particle weights are set to zero for particle types not
87 ! used in the rendering.
88 !
89 ! 3) If particle mass not read, it is still possible to perform
90 ! interpolations, but not using the SPH weights. These
91 ! interpolations *must* therefore be normalised.
92 !-------------------------------------------------------------------
93 subroutine set_interpolation_weights(weighti,dati,iamtypei,usetype, &
94 ninterp,npartoftype,masstype,ntypes,ndataplots,irho,ipmass,ih,ndim, &
95 iRescale,idensityweighted,inormalise,units,unit_interp,required, &
96 rendersinks,isinktype)
97 integer, parameter :: doub_prec = selected_real_kind(P=10,R=30)
98 integer, intent(in) :: ninterp,ntypes,ndataplots,isinktype
99 real, dimension(:), intent(out) :: weighti
100 real, dimension(:,:), intent(in) :: dati
101 integer(kind=1), dimension(:), intent(in) :: iamtypei
102 logical, dimension(:), intent(in) :: usetype
103 logical, dimension(0:ndataplots), intent(in) :: required
104 integer, intent(in) :: ih,irho,ipmass,ndim
105 integer, dimension(:), intent(in) :: npartoftype
106 real, dimension(:), intent(in) :: masstype
107 logical, intent(in) :: iRescale,idensityweighted,rendersinks
108 logical, intent(inout) :: inormalise
109 real, dimension(0:ndataplots), intent(in) :: units
110 real(doub_prec), intent(in) :: unit_interp
111 integer :: i2,i1,itype,ipart
112 real(doub_prec) :: dunitspmass,dunitsrho,dunitsh
113
114 !
115 !-- unit_interp is a multiplication factor that can
116 ! be used to scale the "weight" in case the
117 ! units read from the read_data routine
118 ! are inconsistent (this is the case for the SEREN read)
119 !
120 dunitspmass = 1.d0
121 dunitsrho = 1.d0
122 dunitsh = 1.d0
123 if (iRescale) then
124 if (ipmass > 0) dunitspmass = 1.d0/units(ipmass)
125 if (ih > 0) dunitsh = 1.d0/units(ih)
126 if (irho > 0) dunitsrho = 1.d0/units(irho)
127 endif
128 dunitspmass = dunitspmass * unit_interp
129
130 if (ipmass > 0 .and. ipmass <= ndataplots .and. &
131 irho > 0 .and. irho <= ndataplots .and. &
132 ih > 0 .and. ih <= ndataplots .and. &
133 required(ipmass) .and. required(irho) .and. required(ih)) then
134
135 if (size(iamtypei) > 1) then
136 !
137 !--particles with mixed types
138 !
139 !$omp parallel do default(none) &
140 !$omp shared(ninterp,iamtypei,weighti,dati,rendersinks,isinktype) &
141 !$omp shared(usetype,idensityweighted,dunitsrho) &
142 !$omp shared(ipmass,ih,irho,dunitspmass,dunitsh,ndim) &
143 !$omp private(ipart,itype)
144 do ipart=1,ninterp
145 itype = iamtypei(ipart)
146 if (.not.usetype(itype)) then
147 if (rendersinks .and. itype==isinktype) then
148 weighti(ipart) = weight_sink
149 else
150 weighti(ipart) = 0.
151 endif
152 elseif (idensityweighted) then
153 if (dati(ipart,ih) > tiny(dati)) then
154 weighti(ipart) = (dati(ipart,ipmass)*dunitspmass)/ &
155 ((dati(ipart,ih)*dunitsh)**ndim)
156 else
157 weighti(ipart) = 0.
158 endif
159 else
160 if (dati(ipart,irho) > tiny(dati) .and. dati(ipart,ih) > tiny(dati)) then
161 weighti(ipart) = (dati(ipart,ipmass)*dunitspmass)/ &
162 ((dati(ipart,irho)*dunitsrho)*(dati(ipart,ih)*dunitsh)**ndim)
163 else
164 weighti(ipart) = 0.
165 endif
166 endif
167 enddo
168 !$omp end parallel do
169 else
170 !
171 !--particles ordered by type
172 !
173 i2 = 0
174 over_types: do itype=1,ntypes
175 i1 = i2 + 1
176 i2 = i2 + npartoftype(itype)
177 i2 = min(i2,ninterp)
178 if (i1 > i2) cycle over_types
179 !--set weights to zero for particle types not used in the rendering
180 if (.not.usetype(itype)) then
181 if (rendersinks .and. itype==isinktype) then
182 weighti(i1:i2) = weight_sink
183 else
184 weighti(i1:i2) = 0.
185 endif
186 elseif (idensityweighted) then
187 !--for density weighted interpolation use m/h**ndim
188 where(dati(i1:i2,ih) > tiny(dati))
189 weighti(i1:i2) = (dati(i1:i2,ipmass)*dunitspmass)/ &
190 ((dati(i1:i2,ih)*dunitsh)**ndim)
191 elsewhere
192 weighti(i1:i2) = 0.
193 endwhere
194 else
195 !--usual interpolation use m/(rho h**ndim)
196 where(dati(i1:i2,irho) > tiny(dati) .and. dati(i1:i2,ih) > tiny(dati))
197 weighti(i1:i2) = (dati(i1:i2,ipmass)*dunitspmass)/ &
198 ((dati(i1:i2,irho)*dunitsrho)*(dati(i1:i2,ih)*dunitsh)**ndim)
199 elsewhere
200 weighti(i1:i2) = 0.
201 endwhere
202 endif
203 enddo over_types
204 endif
205
206 if (idensityweighted) then
207 print "(a)",' USING DENSITY WEIGHTED INTERPOLATION '
208 inormalise = .true.
209 endif
210
211 elseif (any(masstype(1:ntypes) > 0.) .and. &
212 irho > 0 .and. irho <= ndataplots .and. &
213 ih > 0 .and. ih <= ndataplots .and. &
214 required(irho) .and. required(ih)) then
215
216 if (size(iamtypei) > 1) then
217 !
218 !--particles with mixed types
219 !
220 !$omp parallel do default(none) &
221 !$omp shared(ninterp,iamtypei,weighti,dati,rendersinks,isinktype) &
222 !$omp shared(usetype,idensityweighted,dunitsrho,masstype) &
223 !$omp shared(ih,irho,dunitspmass,dunitsh,ndim) &
224 !$omp private(ipart,itype)
225 do ipart=1,ninterp
226 itype = iamtypei(ipart)
227 if (.not.usetype(itype)) then
228 if (rendersinks .and. itype==isinktype) then
229 weighti(ipart) = weight_sink
230 else
231 weighti(ipart) = 0.
232 endif
233 elseif (idensityweighted) then
234 if (dati(ipart,ih) > tiny(dati)) then
235 weighti(ipart) = (masstype(itype)*dunitspmass)/ &
236 ((dati(ipart,ih)*dunitsh)**ndim)
237 else
238 weighti(ipart) = 0.
239 endif
240 else
241 if (dati(ipart,irho) > tiny(dati) .and. dati(ipart,ih) > tiny(dati)) then
242 weighti(ipart) = (masstype(itype)*dunitspmass)/ &
243 ((dati(ipart,irho)*dunitsrho)*(dati(ipart,ih)*dunitsh)**ndim)
244 else
245 weighti(ipart) = 0.
246 endif
247 endif
248 enddo
249 !$omp end parallel do
250 else
251 !
252 !--particles ordered by type
253 !
254 i2 = 0
255 over_types2: do itype=1,ntypes
256 i1 = i2 + 1
257 i2 = i2 + npartoftype(itype)
258 i2 = min(i2,ninterp)
259 if (i1 > i2) cycle over_types2
260 !--set weights to zero for particle types not used in the rendering
261 if (.not.usetype(itype)) then
262 if (rendersinks .and. itype==isinktype) then
263 weighti(i1:i2) = weight_sink
264 else
265 weighti(i1:i2) = 0.
266 endif
267 else
268 where(dati(i1:i2,irho) > tiny(dati) .and. dati(i1:i2,ih) > tiny(dati))
269 weighti(i1:i2) = masstype(itype)/ &
270 ((dati(i1:i2,irho)*dunitsrho)*(dati(i1:i2,ih)*dunitsh)**ndim)
271 elsewhere
272 weighti(i1:i2) = 0.
273 endwhere
274 endif
275 enddo over_types2
276 endif
277
278 if (idensityweighted) then
279 print "(a)",' USING DENSITY WEIGHTED INTERPOLATION '
280 inormalise = .true.
281 endif
282 else
283 if (required(ih) .and. required(irho) .and. ih > 0 .and. irho > 0) then
284 print "(a)",' WARNING: particle mass not set: using normalised interpolations'
285 endif
286 weighti(1:ninterp) = 1.0
287
288 !--if particle mass has not been set, then must use normalised interpolations
289 inormalise = .true.
290
291 if (size(iamtypei) > 1) then
292 !
293 !--particles with mixed types
294 !
295 !$omp parallel do default(none) &
296 !$omp shared(ninterp,iamtypei,weighti,usetype) &
297 !$omp private(ipart,itype)
298 do ipart=1,ninterp
299 itype = iamtypei(ipart)
300 if (.not.usetype(itype)) weighti(ipart) = 0.
301 enddo
302 !$omp end parallel do
303 endif
304 endif
305
306 end subroutine set_interpolation_weights
307
308 end module interpolation