Debian Codegraph @

src/allocate.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-2012 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 module mem_allocation
24 implicit none
25
26 contains
27 !----------------------------------------------------------------------------
28 !
29 ! memory allocation/reallocation for main data arrays
30 !
31 ! the global parameters maxpart, maxstep and maxcol are set to
32 ! the dimensions allocated
33 !
34 !----------------------------------------------------------------------------
35 subroutine alloc(npartin,nstep,ncolumnsin,mixedtypes)
36 use particle_data
37 integer, intent(in) :: npartin,nstep,ncolumnsin
38 logical, intent(in), optional :: mixedtypes
39 integer :: maxpartold,maxstepold,maxcolold
40 integer :: ierr,ncolumns
41 logical :: reallocate,reallocate_part,reallocate_step,reallocate_itype
42 integer, dimension(:), allocatable :: icolourmetemp
43 integer(kind=int1), dimension(:,:), allocatable :: iamtypetemp
44 integer, dimension(:,:), allocatable :: npartoftypetemp
45 real, dimension(:,:), allocatable :: masstypetemp
46 real, dimension(:), allocatable :: timetemp, gammatemp
47 real, dimension(:,:,:), allocatable :: dattemp
48 real, dimension(:,:), allocatable :: headervalstemp
49 !
50 !--check for errors in input
51 !
52 if (npartin <= 0) then
53 print*,'allocate: error in input, npartin = ',npartin
54 return
55 endif
56 if (nstep <= 0) then
57 print*,'allocate: error in input, nstep = ',nstep
58 return
59 endif
60 if (ncolumnsin < 0) then
61 print*,'allocate: error in input, ncolumns = ',ncolumnsin
62 return
63 elseif (ncolumnsin==0) then
64 print*,'WARNING: allocate: ncolumns = 0 in input'
65 endif
66 !--do nothing if array sizes are the same
67 if (npartin==maxpart .and. ncolumnsin==maxcol .and. nstep==maxstep) then
68 return
69 endif
70 !
71 !--save array sizes
72 !
73 if (npartin < maxpart) print "(a)",' WARNING: # particles < previous in allocate'
74 if (nstep < maxstep) print "(a)",' WARNING: # steps < previous in allocate'
75 !--at the moment ncolumns cannot be decreased (due to calc_quantities)
76 if (ncolumnsin < maxcol) then
77 ncolumns = maxcol
78 !!print "(a)",' WARNING: # columns < previous in allocate'
79 else
80 ncolumns = ncolumnsin
81 endif
82
83 maxpartold = min(maxpart,npartin)
84 maxstepold = min(maxstep,nstep)
85 maxcolold = min(maxcol,ncolumns)
86
87 reallocate = .false.
88 reallocate_part = .false.
89 reallocate_step = .false.
90 reallocate_itype = .false.
91 !
92 !--if re-allocating, copy arrays to temporary versions
93 !
94 ierr = 0
95 if (allocated(dat)) then
96 reallocate = .true.
97 if (maxpart /= npartin) reallocate_part = .true.
98 if (maxstep /= nstep) reallocate_step = .true.
99
100 if (npartin > 1000000) print 10,'> reallocating memory:',npartin,nstep,ncolumns
101 10 format (a,' parts = ',i10,' steps = ',i6,' cols = ',i4)
102 allocate(dattemp(maxpartold,maxcolold,maxstepold), stat=ierr)
103 if (ierr /= 0) stop 'error allocating memory (dattemp)'
104
105 if (reallocate_part) then
106 allocate(icolourmetemp(maxpartold),stat=ierr)
107 if (ierr /= 0) stop 'error allocating memory (icolourmetemp)'
108 icolourmetemp(1:maxpartold) = icolourme(1:maxpartold)
109 deallocate(icolourme)
110 endif
111
112 dattemp = dat
113 deallocate(dat)
114
115 if (allocated(iamtype)) then ! should always be true
116 !--if iamtype has meaningful contents and reallocation is necessary
117 reallocate_itype = (reallocate_part .or. reallocate_step) .and. (size(iamtype(:,1))==maxpartold)
118 if (reallocate_itype) then
119 allocate(iamtypetemp(maxpartold,maxstepold), stat=ierr)
120 if (ierr /= 0) stop 'error allocating memory (iamtypetemp)'
121 iamtypetemp(1:maxpartold,1:maxstepold) = iamtype(1:maxpartold,1:maxstepold)
122 deallocate(iamtype)
123
124 elseif (present(mixedtypes)) then
125 !--if iamtype has size 1 or 0 but should be allocated here,
126 ! deallocate so we can give it correct size
127 if (mixedtypes .and. size(iamtype(:,1)) < maxpart) deallocate(iamtype)
128 endif
129 endif
130
131 if (reallocate_step) then
132 allocate(npartoftypetemp(maxparttypes,maxstep),stat=ierr)
133 if (ierr /= 0) stop 'error allocating memory (npartoftypetemp)'
134 npartoftypetemp = npartoftype
135 deallocate(npartoftype)
136
137 allocate(masstypetemp(maxparttypes,maxstep),stat=ierr)
138 if (ierr /= 0) stop 'error allocating memory (npartoftypetemp)'
139 masstypetemp = masstype
140 deallocate(masstype)
141
142 allocate(timetemp(maxstep),gammatemp(maxstep),stat=ierr)
143 if (ierr /= 0) stop 'error allocating memory (timetemp,gammatemp)'
144 timetemp = time
145 gammatemp = gamma
146 deallocate(time,gamma)
147
148 allocate(headervalstemp(maxhdr,maxstep))
149 if (ierr /= 0) stop 'error allocating memory (hdrvalstemp)'
150 headervalstemp = headervals
151 deallocate(headervals)
152 endif
153
154 else
155 if (npartin > 1000000) print 10,'> allocating memory:',npartin,nstep,ncolumns
156 maxpart = npartin
157 maxstep = nstep
158 maxcol = ncolumns
159 endif
160
161
162 maxpart = npartin
163 maxstep = nstep
164 maxcol = ncolumns
165
166 !
167 !--main data array
168 !
169 allocate(dat(maxpart,maxcol,maxstep), stat=ierr)
170 if (ierr /= 0) then
171 print*,' parts = ',maxpart,' columns = ',maxcol,' steps = ',maxstep
172 stop 'error allocating memory for dat array'
173 endif
174
175 if (reallocate) then
176 dat(1:maxpartold,1:maxcolold,1:maxstepold) = dattemp(1:maxpartold,1:maxcolold,1:maxstepold)
177 deallocate(dattemp)
178 else
179 dat = 0.
180 endif
181 !
182 !--type array if necessary
183 !
184 if (present(mixedtypes)) then
185 if (mixedtypes .and. .not.allocated(iamtype)) then
186 allocate(iamtype(maxpart,maxstep), stat=ierr)
187 if (ierr /= 0) stop 'error allocating memory for type array'
188 iamtype = 1
189 !--copy contents if reallocating
190 if (reallocate_itype) then
191 iamtype(1:maxpartold,1:maxstepold) = iamtypetemp(1:maxpartold,1:maxstepold)
192 deallocate(iamtypetemp)
193 endif
194 elseif (.not.mixedtypes) then
195 !--if called with mixedtypes explictly false, deallocate itype array
196 if (allocated(iamtype)) deallocate(iamtype)
197 endif
198 elseif (reallocate_itype) then
199 !--if called without mixedtypes, preserve contents of itype array
200 allocate(iamtype(maxpart,maxstep), stat=ierr)
201 if (ierr /= 0) stop 'error allocating memory for type array'
202 iamtype = 1
203
204 iamtype(1:maxpartold,1:maxstepold) = iamtypetemp(1:maxpartold,1:maxstepold)
205 deallocate(iamtypetemp)
206 endif
207 !--make sure iamtype is always allocated for safety, just with size=1 if not used
208 if (.not.allocated(iamtype)) then
209 allocate(iamtype(1,maxstep),stat=ierr)
210 if (ierr /= 0) stop 'error allocating memory for type array (1)'
211 endif
212 !
213 !--particle arrays
214 !
215 if (.not.allocated(icolourme) .or. reallocate_part) then
216 allocate(icolourme(maxpart),stat=ierr)
217 if (ierr /= 0) stop 'error allocating memory for icolourme array'
218 icolourme = 1
219
220 if (reallocate_part) then
221 icolourme(1:maxpartold) = icolourmetemp(1:maxpartold)
222 deallocate(icolourmetemp)
223 endif
224 endif
225 !
226 !--other arrays
227 !
228 if (.not.allocated(npartoftype)) then
229 allocate(npartoftype(maxparttypes,maxstep),stat=ierr)
230 if (ierr /= 0) stop 'error allocating memory for header arrays'
231
232 allocate(masstype(maxparttypes,maxstep),stat=ierr)
233 if (ierr /= 0) stop 'error allocating memory for header arrays'
234
235 allocate(time(maxstep),gamma(maxstep),stat=ierr)
236 if (ierr /= 0) stop 'error allocating memory for header arrays'
237
238 allocate(headervals(maxhdr,maxstep),stat=ierr)
239 if (ierr /= 0) stop 'error allocating memory for header arrays'
240
241 npartoftype = 0
242 masstype = 0.
243 time = time_not_read_val ! initialise like this so we know if has not been read
244 gamma = 0.
245 headervals = 0.
246
247 if (reallocate_step) then
248 npartoftype(:,1:maxstepold) = npartoftypetemp(:,1:maxstepold)
249 masstype(:,1:maxstepold) = masstypetemp(:,1:maxstepold)
250 time(1:maxstepold) = timetemp(1:maxstepold)
251 gamma(1:maxstepold) = gammatemp(1:maxstepold)
252 headervals(:,1:maxstepold) = headervalstemp(:,1:maxstepold)
253 deallocate(npartoftypetemp,masstypetemp)
254 deallocate(timetemp,gammatemp)
255 deallocate(headervalstemp)
256 endif
257 endif
258
259 return
260 end subroutine alloc
261
262
263 !-----------------------------------------
264 !
265 ! deallocation of remaining memory
266 ! (for tidiness - not strictly necessary)
267 !
268 !-----------------------------------------
269 subroutine deallocate_all
270 use particle_data, only:dat,icolourme,iamtype,npartoftype,masstype,&
271 time,gamma,headervals
272
273 if (allocated(dat)) deallocate(dat)
274 if (allocated(icolourme)) deallocate(icolourme)
275 if (allocated(iamtype)) deallocate(iamtype)
276 if (allocated(npartoftype)) deallocate(npartoftype)
277 if (allocated(masstype)) deallocate(masstype)
278 if (allocated(time)) deallocate(time)
279 if (allocated(gamma)) deallocate(gamma)
280 if (allocated(headervals)) deallocate(headervals)
281
282 return
283 end subroutine deallocate_all
284
285 end module mem_allocation