Debian Codegraph @

src/timestepping.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-2023 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 module timestepping
24 implicit none
25 public :: timestep_loop
26
27 private
28
29 contains
30 !
31 ! This subroutine drives the main plotting loop
32 !
33 subroutine timestep_loop(ipicky,ipickx,irender,icontourplot,ivecplot)
34 use filenames, only:nsteps,ifileopen
35 use particle_data, only:iamtype,npartoftype,masstype,time,gamma,headervals,&
36 dat,time_was_read
37 use settings_data, only:istartatstep,iendatstep,nfreq,DataIsBuffered, &
38 iUsesteplist,isteplist,ncolumns,ipartialread
39 use settings_page, only:interactive,nstepsperpage,iColourEachStep,iChangeStyles,nomenu
40 use timestep_plotting, only:initialise_plotting,plotstep
41 use plotlib, only:plot_close
42 integer, intent(in) :: ipicky,ipickx,irender,icontourplot,ivecplot
43 integer :: ipos, istep, ilocindat, iadvance, istepsonpage, istepprev, irender_tmp
44 logical :: ipagechange
45
46 call initialise_plotting(ipicky,ipickx,irender,icontourplot,ivecplot)
47
48 !----------------------------------------------------------------------------
49 ! loop over timesteps (flexible to allow going forwards/backwards in
50 ! interactive mode)
51 !
52 ! bookkeeping is as follows:
53 ! ipos : current step number or position in steplist array
54 ! ilocindat : current or requested location in the dat array
55 ! (usually 1, but can be > 1 if more than one step per file
56 ! or data is buffered to memory)
57 ! (requested location is sent to get_nextstep which skips files
58 ! or steps appropriately and sets actual location)
59 ! istep : current step number
60 ! (as if all steps were in memory in sequential order)
61 ! istepsonpage : number of steps which have been plotted on current page
62 ! this is used because steps are coloured/marked differently
63 ! from this routine (call to colour_timestep)
64 !
65 !----------------------------------------------------------------------------
66 ipos = istartatstep
67 iadvance = nfreq ! amount to increment timestep by (changed in interactive)
68 istepsonpage = 0
69 istep = istartatstep
70 istepprev = 0
71 !--if the current file has only been partially read,
72 ! make sure we read the file again now that we may have different plotting options
73 if (ipartialread) ifileopen = 0
74 irender_tmp = irender
75
76 over_timesteps: do while (ipos <= iendatstep)
77
78 ipos = max(ipos,1) !--can't go further back than ipos=1
79
80 if (iUseStepList) then
81 istep = isteplist(ipos)
82 if (istep > nsteps) then
83 print*,'ERROR: step > nsteps in step list, setting step = last'
84 istep = nsteps
85 elseif (istep <= 0) then
86 !--this should never happen
87 stop 'internal error: corrupted step list: please send bug report'
88 endif
89 else
90 istep = ipos
91 if (istep >= nsteps) then
92 istep = nsteps
93 iendatstep = istep
94 ipos = min(ipos,iendatstep)
95 endif
96 endif
97
98 !
99 !--make sure we have data for this timestep
100 !
101 if (DataIsBuffered) then
102 !--if data is in memory, we just go to the position in dat
103 if (istep > nsteps) then
104 print*,'error: step # > nsteps, setting step = last'
105 istep = nsteps
106 endif
107 ilocindat = istep
108 else
109 !--otherwise read file containing this step into memory and get position in dat array
110 ! (note that nsteps can change in get_nextstep, so may need to re-evaluate
111 ! whether we are on the last step or not, and adjust iendatstep to last step)
112 !
113 call get_nextstep(istep,ilocindat)
114
115 if (.not.iUseStepList .and. istep >= nsteps) then
116 !--reset step position to last useable timestep (ie. nsteps)
117 istep = nsteps
118 iendatstep = istep
119 !--use interactive halt at last step (ie. set position = last position)
120 if (interactive) then
121 ipos = min(ipos,iendatstep)
122 !--if istep has changed, may need to re-read step
123 ! (get_nextstep does nothing if istep is the same)
124 call get_nextstep(istep,ilocindat)
125 endif
126 endif
127 !--this is a general "catch all" when step cannot be located
128 if (ilocindat <= 0) then
129 print*,'ERROR: could not locate timestep'
130 exit over_timesteps
131 endif
132 endif
133
134 !
135 !--write timestepping log
136 !
137 if (iadvance /= 0) then
138 if (.not.time_was_read(time(ilocindat))) then
139 print "(5('-'),' t = (not read), dump #',i5,1x,18('-'))", istep
140 elseif (time(ilocindat) < 1.e-2 .or. time(ilocindat) > 1.e2) then
141 print "(5('-'),' t = ',es9.2,', dump #',i5,1x,18('-'))", time(ilocindat),istep
142 else
143 print "(5('-'),' t = ',f8.2,', dump #',i5,1x,18('-'))", time(ilocindat),istep
144 endif
145 endif
146
147 istepsonpage = istepsonpage + 1
148 ! if ((nstepsperpage > 1 .and. istepsonpage > 1 .and. istepsonpage <= nstepsperpage).or.nstepsperpage==0) then
149 if ((nstepsperpage > 1 .and. istepsonpage <= nstepsperpage) .or. nstepsperpage==0) then
150 ipagechange = .false.
151 else
152 istepsonpage = 1
153 ipagechange = .true.
154 endif
155 !--colour the timestep if appropriate
156 if ((nstepsperpage /= 1) .and. (iColourEachStep .or. iChangeStyles)) then
157 call colour_timestep(istepsonpage,iColourEachStep,iChangeStyles)
158 else
159 !--otherwise set default colours for each particle type
160 ! (do not call if repeating same step so interactive colours stick for same step)
161 if (istep /= istepprev) call colourparts_default(npartoftype(:,ilocindat),iamtype(:,ilocindat))
162 istepprev = istep
163 endif
164
165 ! print*,'ipos = ',ipos,' istep = ',istep,' iposindat = ',ilocindat
166 call plotstep(ipos,istep,istepsonpage,irender_tmp,icontourplot,ivecplot,&
167 iamtype(:,ilocindat),npartoftype(:,ilocindat),masstype(:,ilocindat),&
168 dat(:,:,ilocindat),time(ilocindat),gamma(ilocindat),&
169 headervals(:,ilocindat),ipagechange,iadvance)
170 !
171 !--increment timestep -- iadvance can be changed interactively
172 !
173 if (iadvance==-666) exit over_timesteps ! this is the interactive quit signal
174 ipos = ipos + iadvance ! if ipos goes over iendatstep, this ends the loop
175
176 enddo over_timesteps
177
178 if (.not.interactive) then
179 if (nomenu) then
180 !--gracefully exit
181 print "(/,a,/)",'Finished plotting: Many thankyous for your kind custom.'
182 else ! prepare to return to main menu
183 print*,'press return to finish'
184 read*
185 !--if somehow the data has become corrupted (e.g. last file full of rubbish)
186 ! read in the first dump again
187 if (ncolumns <= 0) then
188 print*,'data is corrupted: re-reading first data file'
189 call get_nextstep(1,ilocindat)
190 endif
191 endif
192 endif
193
194 call plot_close
195
196 end subroutine timestep_loop
197
198 !-------------------------------------------------------------------
199 ! works out whether or not we need to read another dump into memory
200 !-------------------------------------------------------------------
201 recursive subroutine get_nextstep(istep,ilocindat)
202 use filenames, only:nstepsinfile,nfiles,ifileopen,iposopen,nsteps
203 use getdata, only:get_data
204 use settings_data, only:buffer_steps_in_file
205 integer, intent(in) :: istep
206 integer, intent(out) :: ilocindat
207 integer :: ifile,nstepstotal,nstepsprev,iposinfile
208
209 !
210 !--request is for step istep
211 ! need to determine which file step i is in,
212 ! whether or not it is already in memory (and read it if not)
213 ! and finally determine position of requested step in dat array
214 !
215 ifile = 0
216 nstepstotal = 0
217 nstepsprev = 0
218 iposinfile = 1 ! this should always be overwritten anyway
219 do while (nstepstotal < istep .and. ifile < nfiles)
220 ifile = ifile + 1
221 nstepsprev = nstepstotal
222 nstepstotal = nstepstotal + nstepsinfile(ifile)
223 enddo
224
225 if (nstepstotal >= istep) then
226 !--set position in dat array depending on how many steps are in the file
227 iposinfile = istep - nstepsprev
228 else
229 !--this is where we cannot locate the timestep in the data (not enough steps)
230 ! ie. ifile > nfiles
231 print*,'reached last useable timestep'
232 ilocindat = 0
233 return
234 endif
235
236 !print*,'step ',istep,' in file ',ifile,' nsteps = ',nsteps
237 !print*,'position in file = ',iposinfile
238 if (istep > nsteps) then
239 ilocindat = 0
240 return
241 endif
242 ilocindat = iposinfile
243
244 !
245 !--if data is not stored in memory, read next step from file
246 ! At the moment assumes number of steps in each file are the same
247 !
248
249 !--neither or these two error conditions should occur
250 if (ifile > nfiles) then
251 print*,'*** get_nextstep: error: ifile > nfiles'
252 elseif (ifile < 1) then
253 print*,'*** get_nextstep: error: request for file < 1'
254 elseif (ifile /= ifileopen) then
255 !
256 !--read next data file and determine position in file
257 !
258 call get_data(ifile,.true.)
259 !
260 !--because nstepsinfile is predicted for files which have
261 ! not been opened, we may have the situation where
262 ! iposinfile does not point to a real timestep (ie. iposinfile > nstepsinfile).
263 ! In this case we query the step again with our better knowledge of nstepsinfile.
264 !
265 if (iposinfile > nstepsinfile(ifile)) then
266 print*,'not enough steps in file... trying next file'
267 call get_nextstep(istep,ilocindat)
268 endif
269 elseif (.not.buffer_steps_in_file .and. nstepsinfile(ifile) > 1) then
270 if (iposinfile <= nstepsinfile(ifile) .and. iposinfile /= iposopen) then
271 ! if all steps have not been read from the file, read just the next one
272 call get_data(ifile,.true.,iposinfile=iposinfile)
273 ilocindat = 1
274 else
275 ilocindat = 1
276 endif
277 endif
278
279 end subroutine get_nextstep
280
281 !-------------------------------------------------------------
282 ! colours all the particles a given colour for this timestep
283 ! and/or changes the marker type for type 1 particles
284 !-------------------------------------------------------------
285 subroutine colour_timestep(istep,iChangeColours,iChangeStyles)
286 use particle_data, only:icolourme
287 use settings_part, only:linecolourthisstep,linestylethisstep,imarktype
288 use settings_page, only:maxlinestyle,modlinestyle,maxcolour,modcolour,linepalette
289 use plotlib, only:plotlib_maxlinestyle,plotlib_maxlinecolour
290 integer, intent(in) :: istep
291 logical, intent(in) :: iChangeColours, iChangeStyles
292 integer :: icolour,imarkernumber
293
294 if (iChangeColours) then
295 if (allocated(icolourme)) then
296 icolour = istep + 1
297 icolour = (icolour-2)/modcolour + 1
298 if (icolour > plotlib_maxlinecolour) then
299 print "(a,i2,a)",'warning: step colour > ',plotlib_maxlinecolour,': re-using colours'
300 icolour = mod(icolour-1,plotlib_maxlinecolour) + 1
301 endif
302 icolour = mod(icolour-1,min(maxcolour,plotlib_maxlinecolour)) + 1
303 icolourme = icolour
304 if (linepalette < 0) then
305 ! for custom line colours do not use foreground/background colours - allow these to be changed
306 icolour = icolour + 1
307 icolourme = icolour
308 linecolourthisstep = icolour
309 else
310 linecolourthisstep = icolour
311 endif
312 else
313 print "(a)",'***error: array not allocated in colour_timestep***'
314 endif
315 endif
316 if (iChangeStyles) then
317 !--modlinestyle should not be greater than max line styles in plotting library
318 linestylethisstep = mod((istep-1)/modlinestyle,min(maxlinestyle,plotlib_maxlinestyle)) + 1
319 imarkernumber = istep
320 select case(imarkernumber)
321 case(1)
322 imarktype(1) = 4
323 case(2)
324 imarktype(1) = 17
325 case(3)
326 imarktype(1) = 2
327 case(4)
328 imarktype(1) = 3
329 case(5:16)
330 imarktype(1) = imarkernumber
331 case(17:)
332 imarktype(1) = imarkernumber + 1
333 end select
334 endif
335
336 end subroutine colour_timestep
337
338 !---------------------------------------------------------------------------------------
339 ! colours all the particles using the default colour for their type
340 !---------------------------------------------------------------------------------------
341 subroutine colourparts_default(npartoftype,iamtype)
342 use params, only:int1
343 use settings_data, only:ntypes
344 use particle_data, only:icolourme
345 use settings_part, only:idefaultcolourtype
346 integer, dimension(:), intent(in) :: npartoftype
347 integer(kind=int1), dimension(:), intent(in) :: iamtype
348 integer :: i,index1,index2,itype
349
350 if (size(iamtype) > 1) then
351 do i=1,sum(npartoftype(1:ntypes))
352 itype = iamtype(i)
353 if (itype > 0 .and. itype <= ntypes) then
354 if (idefaultcolourtype(itype) >= 0) then
355 icolourme(i) = idefaultcolourtype(itype)
356 endif
357 endif
358 enddo
359 else
360 index1 = 1
361 do itype=1,ntypes
362 index2 = index1 + npartoftype(itype) - 1
363 if (idefaultcolourtype(itype) >= 0) then
364 icolourme(index1:index2) = idefaultcolourtype(itype)
365 endif
366 index1 = index2 + 1
367 enddo
368 endif
369
370 end subroutine colourparts_default
371
372 end module timestepping