Debian Codegraph @

src/limits.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-2022 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !----------------------------------------------------------
24 !
25 ! subroutines to do with setting of plot limits from data
26 ! and using only a subset of the particles according to a
27 ! range in parameters
28 !
29 !----------------------------------------------------------
30 module limits
31 use params
32 implicit none
33 real, dimension(maxplot,2) :: lim,range,lim2
34 private :: warn_minmax
35
36 public
37
38 contains
39
40 !----------------------------------------------------------
41 ! set plot limits for all columns
42 ! NB: does not differentiate between particle types at the moment
43 !----------------------------------------------------------
44 subroutine set_limits(ifromstep,itostep,ifromcol,itocol,use_type)
45 use labels, only:label,ix
46 use geometry, only:coord_transform_limits
47 use particle_data, only:npartoftype,dat,maxcol,iamtype
48 use settings_data, only:ndim,icoords,icoordsnew,iverbose,debugmode
49 integer, intent(in) :: ifromstep,itostep,ifromcol,itocol
50 logical, intent(in), optional :: use_type(:)
51 integer :: i,j,k,ntoti,itocoli
52 logical :: first
53
54 if (iverbose > 1 .or. debugmode) print 100,ifromstep,itostep,ifromcol,itocol
55 100 format(/' setting plot limits: steps ',i5,'->',i5,' cols ',i2,'->',i3)
56 !--avoid tripping over array bounds (but these are valid do-nothing calls)
57 if (ifromcol > maxcol .or. maxcol==0) return
58 if (ifromcol > itocol) return
59 itocoli = min(itocol,maxcol)
60
61 !--find limits of particle properties
62 lim(ifromcol:itocol,1) = huge(lim)
63 lim(ifromcol:itocol,2) = -huge(lim)
64 do i=ifromstep,itostep
65 ntoti = sum(npartoftype(:,i))
66 if (allocated(iamtype) .and. present(use_type) .and. size(iamtype(:,1)) > 1) then
67 do j=ifromcol,itocoli
68 do k=1,ntoti
69 if (use_type(iamtype(k,i))) then
70 lim(j,1) = min(lim(j,1),dat(k,j,i))
71 lim(j,2) = max(lim(j,2),dat(k,j,i))
72 endif
73 enddo
74 enddo
75 else
76 do j=ifromcol,itocoli
77 do k=1,ntoti
78 lim(j,1) = min(lim(j,1),dat(k,j,i))
79 lim(j,2) = max(lim(j,2),dat(k,j,i))
80 enddo
81 enddo
82 endif
83 enddo
84 !
85 !--warn if limits are the same
86 !
87 first = .true.
88 do j=ifromcol,itocol
89 call warn_minmax(label(j),lim(j,1),lim(j,2),first)
90 enddo
91
92 lim2(ifromcol:itocol,:) = 0.
93
94 !
95 !--transform coord limits into new coordinate system if coord transform is applied
96 !
97 if (icoordsnew /= icoords .and. ndim > 0) then
98 if (ifromcol <= ix(ndim)) then ! separate if is to avoid referencing ix(0) if ndim=0
99 call coord_transform_limits(lim(ix(1):ix(ndim),1),lim(ix(1):ix(ndim),2), &
100 icoords,icoordsnew,ndim)
101 endif
102 endif
103
104 end subroutine set_limits
105
106 !----------------------------------------------------------
107 ! rescale plot limits for all columns by factor
108 ! e.g. if units have changed
109 !----------------------------------------------------------
110 subroutine rescale_limits(fac)
111 real, intent(in) :: fac(:)
112 integer :: j
113
114 do j=1,min(size(fac),size(lim),size(lim2))
115 lim(j,:) = lim(j,:)*fac(j)
116 lim2(j,:) = lim2(j,:)*fac(j)
117 enddo
118
119 end subroutine rescale_limits
120
121 !----------------------------------------------------------
122 ! save plot limits for all columns to a file
123 !----------------------------------------------------------
124 subroutine write_limits(limitsfile)
125 use settings_data, only:numplot,ndataplots
126 character(len=*), intent(in) :: limitsfile
127 integer :: i
128
129 print*,'saving plot limits to file ',trim(limitsfile)
130
131 open(unit=55,file=limitsfile,status='replace',form='formatted',ERR=998)
132 do i=1,numplot
133 if (rangeset(i) .and. i < ndataplots .and. lim2set(i)) then
134 write(55,"(6(1x,1pe14.6))",err=999) lim(i,1),lim(i,2),range(i,1),range(i,2),lim2(i,1),lim2(i,2)
135 elseif (lim2set(i) .and. i < ndataplots) then
136 write(55,"(6(1x,1pe14.6))",err=999) lim(i,1),lim(i,2),0.,0.,lim2(i,1),lim2(i,2)
137 elseif (rangeset(i) .and. i < ndataplots) then
138 write(55,"(4(1x,1pe14.6))",err=999) lim(i,1),lim(i,2),range(i,1),range(i,2)
139 else
140 write(55,"(2(1x,1pe14.6))",err=999) lim(i,1),lim(i,2)
141 endif
142 enddo
143 close(unit=55)
144
145 return
146
147 998 continue
148 print*,'*** error opening limits file: limits not saved'
149 return
150 999 continue
151 print*,'*** error saving limits'
152 close(unit=55)
153
154 end subroutine write_limits
155
156 !----------------------------------------------------------
157 ! read plot limits for all columns from a file
158 !----------------------------------------------------------
159 subroutine read_limits(limitsfile,ierr)
160 use labels, only:label
161 use settings_data, only:numplot,ncolumns,ncalc
162 use asciiutils, only:ncolumnsline
163 character(len=*), intent(in) :: limitsfile
164 integer, intent(out) :: ierr
165 integer :: i,ncolsline
166 character(len=120) :: line
167 logical :: iexist
168
169 ierr = 0
170
171 inquire(file=limitsfile,exist=iexist)
172 if (.not.iexist) then
173 !print "(1x,a)",trim(limitsfile)//' not found'
174 ierr = 1
175 return
176 endif
177
178 open(unit=54,file=limitsfile,status='old',form='formatted',err=997)
179 print "(a)",' read '//trim(limitsfile)
180 do i=1,numplot
181 read(54,"(a)",err=998,end=999) line
182 ncolsline = ncolumnsline(line)
183 if (ncolsline < 2) then
184 goto 998
185 elseif (ncolsline >= 6) then
186 read(line,*,err=998,end=999) lim(i,1),lim(i,2),range(i,1),range(i,2),lim2(i,1),lim2(i,2)
187 elseif (ncolsline >= 4) then
188 read(line,*,err=998,end=999) lim(i,1),lim(i,2),range(i,1),range(i,2)
189 else
190 read(line,*,err=998,end=999) lim(i,1),lim(i,2)
191 endif
192 call assert_sensible_limits(lim(i,1),lim(i,2))
193 !
194 !--warn if limits are the same
195 !
196 call warn_minmax(label(i),lim(i,1),lim(i,2))
197 enddo
198 close(unit=54)
199 return
200
201 997 continue
202 print*,trim(limitsfile),' not found'
203 ierr = 1
204 return
205 998 continue
206 call print_rangeinfo()
207 call print_lim2info()
208 print*,'*** error reading limits from file'
209 ierr = 2
210 close(unit=54)
211 return
212 999 continue
213 !--only give error if we really do not have enough columns
214 ! (on first call nextra is not set)
215 if (i < ncolumns+ncalc) then
216 print "(a,i3)",' end of file in '//trim(limitsfile)//': limits read to column ',i
217 ierr = -1
218 endif
219
220 !--print info about range restrictions read from file
221 call print_rangeinfo()
222 call print_lim2info()
223 close(unit=54)
224
225 end subroutine read_limits
226
227 !----------------------------------------------------------
228 ! get a subset of the particles by enforcing range restrictions
229 !----------------------------------------------------------
230 subroutine get_particle_subset(icolours,datstep,ncolumns)
231 use labels, only:label
232 integer, intent(inout) :: icolours(:)
233 real, intent(in) :: datstep(:,:)
234 integer, intent(in) :: ncolumns
235 integer :: icol
236
237 if (anyrangeset()) then
238 !--reset colours of all particles (to not hidden) if using range restriction
239 where (icolours(:)==-1000)
240 icolours(:) = 0
241 elsewhere
242 icolours(:) = abs(icolours(:))
243 endwhere
244
245 do icol=1,ncolumns
246 if (rangeset(icol)) then
247 print "(a,1pe10.3,a,1pe10.3,a)",' | using only particles in range ', &
248 range(icol,1),' < '//trim(label(icol))//' < ',range(icol,2),' |'
249 !
250 !--loop over the particles and colour those outside the range
251 ! NB: background colour (0) is set to -1000
252 !
253 where (datstep(:,icol) < range(icol,1) .or. &
254 datstep(:,icol) > range(icol,2))
255 where (icolours==0)
256 icolours = -1000
257 elsewhere
258 icolours = -abs(icolours)
259 end where
260 end where
261 endif
262 enddo
263 endif
264
265 end subroutine get_particle_subset
266
267 !----------------------------------------------------------
268 ! reset all range restrictions to zero
269 !----------------------------------------------------------
270 subroutine reset_all_ranges()
271 use particle_data, only:icolourme
272
273 print "(a)",' removing all range restrictions '
274 where (icolourme(:)==-1000)
275 icolourme(:) = 0
276 elsewhere
277 icolourme(:) = abs(icolourme(:))
278 endwhere
279 range(:,:) = 0.
280
281 end subroutine reset_all_ranges
282
283 !----------------------------------------------------------
284 ! function which returns whether or not a range
285 ! has been set for a given column
286 !----------------------------------------------------------
287 logical function rangeset(icol)
288 integer, intent(in) :: icol
289
290 rangeset = .false.
291 if (abs(range(icol,2)-range(icol,1)) > tiny(range)) rangeset = .true.
292
293 end function rangeset
294
295 !----------------------------------------------------------
296 ! function which returns whether or not lim2
297 ! has been set for a given column
298 !----------------------------------------------------------
299 logical function lim2set(icol)
300 integer, intent(in) :: icol
301
302 lim2set = .false.
303 if (abs(lim2(icol,2)) > tiny(lim2) .or. abs(lim2(icol,1)) > tiny(lim2)) lim2set = .true.
304
305 end function lim2set
306
307 !----------------------------------------------------------
308 ! reset all range restrictions to zero
309 !----------------------------------------------------------
310 subroutine reset_lim2(icol)
311 integer, intent(in) :: icol
312
313 print "(a)",' contour limits same as render limits'
314 if (icol > 0 .and. icol <= maxplot) lim2(icol,:) = 0
315
316 end subroutine reset_lim2
317
318 !----------------------------------------------------------
319 ! function which returns whether or not a range
320 ! has been set for any column
321 !----------------------------------------------------------
322 logical function anyrangeset()
323 use settings_data, only:ndataplots
324 integer :: i
325
326 anyrangeset = .false.
327 do i=1,ndataplots
328 if (rangeset(i)) anyrangeset = .true.
329 enddo
330
331 end function anyrangeset
332
333 !----------------------------------------------------------
334 ! prints info about current range restriction settings
335 !----------------------------------------------------------
336 subroutine print_rangeinfo()
337 use settings_data, only:ndataplots
338 use labels, only:label
339 integer :: i
340
341 if (anyrangeset()) then
342 print "(/,a,/)",'>> current range restrictions set: '
343 do i=1,ndataplots
344 if (rangeset(i)) then
345 print "(a,1pe10.3,a,1pe10.3,a)", &
346 ' ( ',range(i,1),' < '//trim(label(i))//' < ',range(i,2),' )'
347 endif
348 enddo
349 print "(/,2(a,/))",'>> only particles within this range will be plotted ', &
350 ' and/or used in interpolation routines'
351 !else
352 !print "(/,a,/)",'>> no current parameter range restrictions set '
353 endif
354
355 end subroutine print_rangeinfo
356
357 !----------------------------------------------------------
358 ! prints info about current range restriction settings
359 !----------------------------------------------------------
360 subroutine print_lim2info()
361 use settings_data, only:ndataplots
362 use labels, only:label
363 integer :: i
364
365 do i=1,ndataplots
366 if (lim2set(i)) then
367 print "(a,1pe10.3,a,1pe10.3,a)", &
368 ' ( contours use ',lim2(i,1),' < '//trim(label(i))//' < ',lim2(i,2),' )'
369 endif
370 enddo
371
372 end subroutine print_lim2info
373
374 !----------------------------------------------------------
375 ! prints warning if min=max in limits setting
376 !----------------------------------------------------------
377 subroutine warn_minmax(labelx,xmin,xmax,first)
378 character(len=*), intent(in) :: labelx
379 real, intent(in) :: xmin,xmax
380 logical, intent(inout), optional :: first
381
382 if (abs(xmin-xmax) < tiny(xmax)) then
383 if (present(first)) then
384 if (first) print "(a)"
385 first = .false.
386 endif
387 print "(a,a20,a,1pe9.2)",' warning: ',labelx,' min = max = ',xmin
388 endif
389
390 end subroutine warn_minmax
391
392 !----------------------------------------------------------
393 ! Makes sure that variable is within a given range
394 ! If no range specified, ensures that it is within
395 ! the allowed range for the variable type,
396 ! i.e. -0.5*huge(x)->0.5*huge(x)
397 !----------------------------------------------------------
398 subroutine assert_range(x,min,max)
399 real, intent(inout) :: x
400 real, intent(in), optional :: min,max
401 real :: xmin,xmax
402
403 xmin = -0.5*huge(xmin) ! for limits need xmax - xmin to
404 xmax = 0.5*huge(xmax) ! be less than huge(x)
405 if (present(min)) xmin = min
406 if (present(max)) xmax = max
407 if (x < xmin) x = xmin
408 if (x > xmax) x = xmax
409 if (isnan(x)) x = 0.
410
411 end subroutine assert_range
412
413 !----------------------------------------------------------
414 ! Interface to the above, but checks two numbers at once
415 ! and checks that max > min
416 !----------------------------------------------------------
417 subroutine assert_sensible_limits(xmin,xmax)
418 real, intent(inout) :: xmin,xmax
419
420 call assert_range(xmin)
421 call assert_range(xmax)
422
423 end subroutine assert_sensible_limits
424
425 !----------------------------------------------------------
426 ! Interface to the above, but checks two numbers at once
427 ! and checks that max > min
428 !----------------------------------------------------------
429 subroutine fix_equal_limits(xmin,xmax)
430 real, intent(inout) :: xmin,xmax
431
432 call assert_sensible_limits(xmin,xmax)
433 if (abs(xmax - xmin) < tiny(xmin)) then
434 xmax = xmax + 1.0
435 if (xmin > 0.) then
436 xmin = max(xmin - 1.0,0.)
437 else
438 xmin = xmin - 1.0
439 endif
440 endif
441
442 end subroutine fix_equal_limits
443
444 !----------------------------------------------------------
445 ! checks whether all the min and max limits in a
446 ! range of columns are the same
447 !----------------------------------------------------------
448 logical function limits_are_equal(n,iplotx,iploty)
449 integer, intent(in) :: n
450 integer, intent(in), dimension(n) :: iplotx,iploty
451 real :: xlim(2), ylim(2)
452 integer :: j
453
454 limits_are_equal = .true.
455 if (n > 0) then
456 xlim = lim(iplotx(1),:)
457 ylim = lim(iploty(1),:)
458 endif
459 do j=1,2
460 if (any((lim(iplotx(1:n),j) - xlim(j)) > epsilon(0.))) limits_are_equal = .false.
461 if (any((lim(iploty(1:n),j) - ylim(j)) > epsilon(0.))) limits_are_equal = .false.
462 enddo
463
464 end function limits_are_equal
465
466 end module limits