-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallocateND.f90
488 lines (458 loc) · 18.1 KB
/
allocateND.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
!------------------------------------------------------------------------------!
! NDSPMHD: A Smoothed Particle (Magneto)Hydrodynamics code for (astrophysical) !
! fluid dynamics simulations in 1, 2 and 3 spatial dimensions. !
! !
! (c) 2002-2015 Daniel Price !
! !
! http://users.monash.edu.au/~dprice/ndspmhd !
! daniel.price@monash.edu -or- dprice@cantab.net (forwards to current address) !
! !
! NDSPMHD comes with ABSOLUTELY NO WARRANTY. !
! This is free software; and you are welcome to redistribute !
! it under the terms of the GNU General Public License !
! (see LICENSE file for details) and the provision that !
! this notice remains intact. If you modify this file, please !
! note section 2a) of the GPLv2 states that: !
! !
! a) You must cause the modified files to carry prominent notices !
! stating that you changed the files and the date of any change. !
! !
! ChangeLog: !
!------------------------------------------------------------------------------!
module mem_allocation
implicit none
public :: alloc
private
contains
!!----------------------------------------------------------------------
!! this subroutine allocates/reallocates memory for all arrays
!! given the new number of particles (should be initially called from setup).
!!----------------------------------------------------------------------
subroutine alloc(newsizein,sortlist)
!
!--include relevant global variables
!
use dimen_mhd
use debug
use loguns
use bound
use derivb
use eos
use fmagarray
use hterms
use linklist
use options
use part
use part_in
use rates
use xsph
use matrixcorr
!
!--define local variables
!
implicit none
integer, intent(in) :: newsizein
integer, dimension(newsizein), intent(in), optional :: sortlist
integer, dimension(newsizein) :: iorder
integer :: i,newsize,ioldsize,idumsize
real, dimension(newsizein) :: dumpmass,dumrhoin,dumenin,dumpsiin,dumrhoalt
real, dimension(newsizein) :: dumrho,dumdrhodt,dumuu,dumdudt,dumen,dumdendt
real, dimension(3,newsizein) :: dumalpha,dumalphain,dumdaldt
real, dimension(newsizein) :: dumpsi,dumrho0
real, dimension(newsizein) :: dumhh,dumgradh,dumgradhn,dumgradsoft,dumgradgradh,dumpr,dumspsound
real, dimension(newsizein) :: dumdivB,dumhhin,dumdhdt,dumdpsidt,dumpoten
real, dimension(ndim,newsizein) :: dumxin,dumx
real, dimension(ndimv,newsizein) :: dumvelin,dumvel,dumBevolin
real, dimension(ndimv,newsizein) :: dumforce,dumBevol,dumdBevoldt,dumfmag
real, dimension(ndimv,newsizein) :: dumBfield,dumcurlB,dumxsphterm,dumgradpsi
real, dimension(ndimv,ndimv,newsizein) :: dumgradB
!--gr terms
real, dimension(newsizein) :: dumsqrtg,dumdens
real, dimension(ndimv,newsizein) :: dumsourceterms,dumpmom,dumpmomin
integer, dimension(newsizein) :: idumireal,idumitype,idumnumneigh
real, dimension(ndxdx,newsizein) :: dumdxdx
real, dimension(ndim,newsizein) :: dumx0
!--dust
real, dimension(ndust,newsizein) :: dumdustfrac,dumdustevol,dumdustevolin,dumddustevoldt
real, dimension(ndimV,newsizein) :: dumdeltav,dumdeltavin,dumddeltavdt
real, dimension(ndust,newsizein) :: dumrhodust
real, dimension(newsizein) :: dumrhogas
!--quantum
real, dimension(ndim,ndim,newsizein) :: dumP_Q
logical :: reallocate, isortparts
!
!--work out whether reallocating memory and what the current array size is
!
reallocate = allocated(x)
if (allocated(rho)) then
ioldsize = size(rho) ! current array size
else
ioldsize = 0
endif
idumsize = 0
!
!--set default list order
!
do i=1,size(iorder)
iorder(i) = i
enddo
isortparts = .false.
!
!--check for errors
!
if (newsizein.le.0) then
write(iprint,*) 'error: size<=0 in call to allocate : ',newsizein
stop
endif
!
!--set new size (add 5% if using ghosts so don't have to do this too often)
!
if (any(ibound.ge.1) .and. newsizein.gt.ioldsize) then
newsize = int(1.1*newsizein)
elseif (newsizein.eq.ioldsize) then
if (present(sortlist)) then
iorder(:) = sortlist(:)
isortparts = .true.
reallocate = .false.
else
write(iprint,*) 'allocate: old size = new size: doing nothing'
return
endif
else
newsize = newsizein
endif
if (reallocate) then
write(iprint,*) 'reallocating memory for all arrays, old, new =',size(rho),newsize
elseif (isortparts) then
write(iprint,*) 'reshuffling all particles...'
else
write(iprint,*) 'allocating memory for all arrays, maxpart = ',newsize
endif
if (reallocate .or. isortparts) then
idumsize = min(newsize,ioldsize) ! dummy array size is minimum of old/new
!-----------------------------------------------------------------------------
! if reallocating, copy everything into dummy arrays using the smallest size
!-----------------------------------------------------------------------------
dumpmass(1:idumsize) = pmass(1:idumsize)
dumrhoin(1:idumsize) = rhoin(1:idumsize)
dumhhin(1:idumsize) = hhin(1:idumsize)
dumenin(1:idumsize) = enin(1:idumsize)
dumalphain(:,1:idumsize) = alphain(:,1:idumsize)
dumpsiin(1:idumsize) = psiin(1:idumsize)
dumxin(:,1:idumsize) = xin(:,1:idumsize)
dumvelin(:,1:idumsize) = velin(:,1:idumsize)
dumBevolin(:,1:idumsize) = Bevolin(:,1:idumsize)
dumx(:,1:idumsize) = x(:,1:idumsize)
dumvel(:,1:idumsize) = vel(:,1:idumsize)
dumforce(:,1:idumsize) = force(:,1:idumsize)
dumBevol(:,1:idumsize) = Bevol(:,1:idumsize)
dumBfield(:,1:idumsize) = Bfield(:,1:idumsize)
dumdBevoldt(:,1:idumsize) = dBevoldt(:,1:idumsize)
dumfmag(:,1:idumsize) = fmag(:,1:idumsize)
dumcurlB(:,1:idumsize) = curlB(:,1:idumsize)
dumgradB(:,:,1:idumsize) = gradB(:,:,1:idumsize)
dumgradpsi(:,1:idumsize) = gradpsi(:,1:idumsize)
dumxsphterm(:,1:idumsize) = xsphterm(:,1:idumsize)
dumrho(1:idumsize) = rho(1:idumsize)
dumrhoalt(1:idumsize) = rhoalt(1:idumsize)
dumdrhodt(1:idumsize) = drhodt(1:idumsize)
dumuu(1:idumsize) = uu(1:idumsize)
dumdudt(1:idumsize) = dudt(1:idumsize)
dumen(1:idumsize) = en(1:idumsize)
dumdendt(1:idumsize) = dendt(1:idumsize)
dumalpha(:,1:idumsize) = alpha(:,1:idumsize)
dumdaldt(:,1:idumsize) = daldt(:,1:idumsize)
dumpsi(1:idumsize) = psi(1:idumsize)
dumdpsidt(1:idumsize) = dpsidt(1:idumsize)
dumhh(1:idumsize) = hh(1:idumsize)
dumdhdt(1:idumsize) = dhdt(1:idumsize)
dumgradh(1:idumsize) = gradh(1:idumsize)
dumgradhn(1:idumsize) = gradhn(1:idumsize)
dumgradsoft(1:idumsize) = gradsoft(1:idumsize)
dumgradgradh(1:idumsize) = gradgradh(1:idumsize)
dumpr(1:idumsize) = pr(1:idumsize)
dumspsound(1:idumsize) = spsound(1:idumsize)
! dumll(1:idumsize) = ll(1:idumsize)
dumdivB(1:idumsize) = divB(1:idumsize)
idumireal(1:idumsize) = ireal(1:idumsize)
idumitype(1:idumsize) = itype(1:idumsize)
idumnumneigh(1:idumsize) = numneigh(1:idumsize)
dumsqrtg(1:idumsize) = sqrtg(1:idumsize)
dumdens(1:idumsize) = dens(1:idumsize)
if (allocated(poten)) dumpoten(1:idumsize) = poten(1:idumsize)
if (allocated(sourceterms)) dumsourceterms(:,1:idumsize) = sourceterms(:,1:idumsize)
if (allocated(pmom)) dumpmom(:,1:idumsize) = pmom(:,1:idumsize)
if (allocated(pmomin)) dumpmomin(:,1:idumsize) = pmomin(:,1:idumsize)
if (allocated(dustfrac)) dumdustfrac(:,1:idumsize) = dustfrac(:,1:idumsize)
if (allocated(dustevol)) dumdustevol(:,1:idumsize) = dustevol(:,1:idumsize)
if (allocated(ddustevoldt)) dumddustevoldt(:,1:idumsize) = ddustevoldt(:,1:idumsize)
if (allocated(deltav)) dumdeltav(:,1:idumsize) = deltav(:,1:idumsize)
if (allocated(ddeltavdt)) dumddeltavdt(:,1:idumsize) = ddeltavdt(:,1:idumsize)
if (allocated(dustevolin)) dumdustevolin(:,1:idumsize) = dustevolin(:,1:idumsize)
if (allocated(deltavin)) dumdeltavin(:,1:idumsize) = deltavin(:,1:idumsize)
if (allocated(rhodust)) dumrhodust(:,1:idumsize) = rhodust(:,1:idumsize)
if (allocated(rhogas)) dumrhogas(1:idumsize) = rhogas(1:idumsize)
dumdxdx(:,1:idumsize) = dxdx(:,1:idumsize)
dumx0(:,1:idumsize) = x0(:,1:idumsize)
if (allocated(rho0)) dumrho0(1:idumsize) = rho0(1:idumsize)
if (allocated(P_Q)) dumP_Q(:,:,1:idumsize) = P_Q(:,:,1:idumsize)
!-----------------------------------------------------------------------------
! deallocate the arrays
!-----------------------------------------------------------------------------
if (reallocate) then
!
!--initial particle properties
!
deallocate (pmass,xin,rhoin,hhin,enin,alphain,psiin)
deallocate (velin)
if (allocated(Bevolin)) deallocate (Bevolin)
!
!--particle properties and derivatives
!
deallocate(x,vel,force,rho,drhodt,uu,dudt,en,dendt)
deallocate(alpha,daldt,psi,dpsidt,hh,dhdt,gradh,gradhn,gradsoft,pr)
deallocate(gradgradh)
if (allocated(rhoalt)) deallocate(rhoalt)
if (allocated(zeta)) deallocate(zeta)
if (allocated(poten)) deallocate(poten)
if (allocated(Bfield)) deallocate(Bfield)
if (allocated(Bevol)) deallocate(Bevol)
if (allocated(dBevoldt)) deallocate(dBevoldt)
if (allocated(gradpsi)) deallocate(gradpsi)
!
!--equation of state
!
deallocate(spsound)
!
!--linklist and boundaries - note ifirstincell is reallocated in link
! (depends on max # of link list cells)
!
deallocate(ll,ireal,ifirstincell,iamincell)
!
!--itype is particle type (normal, fixed particle etc)
!
if (allocated(itype)) deallocate(itype)
if (allocated(numneigh)) deallocate(numneigh)
!
!--mhd quantities and derivatives
!
if (allocated(fmag)) deallocate(fmag)
if (allocated(divB)) deallocate(divB)
if (allocated(curlB)) deallocate(curlB)
if (allocated(gradB)) deallocate(gradB)
!
!--xsph
!
if (allocated(xsphterm)) deallocate(xsphterm)
!
!--gr
!
if (allocated(sqrtg)) deallocate(sqrtg)
if (allocated(sourceterms)) deallocate(sourceterms)
if (allocated(pmom)) deallocate(pmom)
if (allocated(pmomin)) deallocate(pmomin)
if (allocated(dens)) deallocate(dens)
if (allocated(dxdx)) deallocate(dxdx)
if (allocated(x0)) deallocate(x0)
if (allocated(rho0)) deallocate(rho0)
!
!--dust
!
if (allocated(dustfrac)) deallocate(dustfrac)
if (allocated(dustevol)) deallocate(dustevol)
if (allocated(deltav)) deallocate(deltav)
if (allocated(ddustevoldt)) deallocate(ddustevoldt)
if (allocated(ddeltavdt)) deallocate(ddeltavdt)
if (allocated(dustevolin)) deallocate(dustevolin)
if (allocated(deltavin)) deallocate(deltavin)
if (allocated(rhodust)) deallocate(rhodust)
if (allocated(rhogas)) deallocate(rhogas)
!
!--quantum
!
if (allocated(P_Q)) deallocate(P_Q)
!
!--physical viscosity
!
if (allocated(del2v)) deallocate(del2v)
if (allocated(graddivv)) deallocate(graddivv)
endif
endif
if (reallocate .or. .not.allocated(x)) then
!-----------------------------------------------------------------------------
! allocate all arrays (for both first time and reallocation)
!-----------------------------------------------------------------------------
idim = newsize
!
!--initial particle properties
!
allocate (pmass(newsize))
allocate (xin(ndim,newsize))
allocate (rhoin(newsize),hhin(newsize))
allocate (enin(newsize),psiin(newsize)) ! alpha done below
allocate (velin(ndimv,newsize))
allocate (Bevolin(ndimb,newsize))
!
!--particle properties and derivatives
!
allocate(x(ndim,newsize))
allocate(vel(ndimv,newsize),force(ndimv,newsize))
allocate(rho(newsize),drhodt(newsize))
allocate(rhoalt(newsize))
allocate(uu(newsize),dudt(newsize),en(newsize),dendt(newsize))
allocate(alpha(3,newsize),alphain(3,newsize),daldt(3,newsize))
allocate(psi(newsize),dpsidt(newsize))
allocate(hh(newsize))
allocate(dhdt(newsize),gradh(newsize),gradhn(newsize),gradsoft(newsize))
allocate(gradgradh(newsize))
if (imhd.lt.0) allocate(zeta(newsize))
allocate(pr(newsize))
allocate(Bevol(ndimb,newsize))
allocate(Bfield(ndimb,newsize),dBevoldt(ndimb,newsize)) ! mag field
allocate(gradpsi(ndimb,newsize))
if (igravity.ne.0) allocate(poten(newsize))
!
!--equation of state
!
allocate(spsound(newsize))
!
!--linklist and boundaries - note ifirstincell is reallocated in link
! (depends on max # of link list cells)
!
allocate(ll(newsize),iamincell(newsize),ifirstincell(0),ireal(newsize))
!
!--particle type
!
allocate(itype(newsize),numneigh(newsize))
itype(:) = 0 ! give type 0 by default
!
!--mhd quantities and derivatives
!
allocate(fmag(ndimb,newsize),divB(newsize),curlB(ndimb,newsize),gradB(ndimb,ndimb,newsize))
!
!--xsph
!
allocate(xsphterm(ndimv,newsize))
!
!--gr
!
allocate(sqrtg(newsize))
allocate(dens(newsize))
allocate(pmom(ndimv,newsize))
allocate(pmomin(ndimv,newsize))
if (geom(1:4).ne.'cart') then
allocate(sourceterms(ndimv,newsize))
endif
allocate(dxdx(ndxdx,newsize))
allocate(x0(ndim,newsize))
allocate(rho0(newsize))
!
!--dust
!
if (onef_dust) then
allocate(dustfrac(ndust,newsize),dustevol(ndust,newsize),dustevolin(ndust,newsize))
allocate(ddustevoldt(ndust,newsize))
allocate(deltav(ndimV,newsize),deltavin(ndimV,newsize))
allocate(ddeltavdt(ndimV,newsize))
allocate(rhogas(newsize),rhodust(ndust,newsize))
endif
!
!--quantum
!
if (iquantum /= 0) then
allocate(P_Q(ndim,ndim,newsize))
endif
!
!--physical viscosity
!
! if (ivisc.gt.0) then
allocate(del2v(ndimV,newsize))
allocate(graddivv(ndimV,newsize))
!endif
endif
if (reallocate .or. isortparts) then
!-----------------------------------------------------------------------------
! copy properties back from old arrays to new arrays
!-----------------------------------------------------------------------------
pmass(1:idumsize) = dumpmass(iorder(1:idumsize))
rhoin(1:idumsize) = dumrhoin(iorder(1:idumsize))
hhin(1:idumsize) = dumhhin(iorder(1:idumsize))
enin(1:idumsize) = dumenin(iorder(1:idumsize))
alphain(:,1:idumsize) = dumalphain(:,iorder(1:idumsize))
psiin(1:idumsize) = dumpsiin(iorder(1:idumsize))
xin(:,1:idumsize) = dumxin(:,iorder(1:idumsize))
velin(:,1:idumsize) = dumvelin(:,iorder(1:idumsize))
Bevolin(:,1:idumsize) = dumBevolin(:,iorder(1:idumsize))
x(:,1:idumsize) = dumx(:,iorder(1:idumsize))
vel(:,1:idumsize) = dumvel(:,iorder(1:idumsize))
force(:,1:idumsize) = dumforce(:,iorder(1:idumsize))
Bfield(:,1:idumsize) = dumBfield(:,iorder(1:idumsize))
Bevol(:,1:idumsize) = dumBevol(:,iorder(1:idumsize))
dBevoldt(:,1:idumsize) = dumdBevoldt(:,iorder(1:idumsize))
gradpsi(:,1:idumsize) = dumgradpsi(:,iorder(1:idumsize))
fmag(:,1:idumsize) = dumfmag(:,iorder(1:idumsize))
curlB(:,1:idumsize) = dumcurlB(:,iorder(1:idumsize))
gradB(:,:,1:idumsize) = dumgradB(:,:,iorder(1:idumsize))
xsphterm(:,1:idumsize) = dumxsphterm(:,iorder(1:idumsize))
rho(1:idumsize) = dumrho(iorder(1:idumsize))
rhoalt(1:idumsize) = dumrhoalt(iorder(1:idumsize))
drhodt(1:idumsize) = dumdrhodt(iorder(1:idumsize))
uu(1:idumsize) = dumuu(iorder(1:idumsize))
dudt(1:idumsize) = dumdudt(iorder(1:idumsize))
en(1:idumsize) = dumen(iorder(1:idumsize))
dendt(1:idumsize) = dumdendt(iorder(1:idumsize))
alpha(:,1:idumsize) = dumalpha(:,iorder(1:idumsize))
daldt(:,1:idumsize) = dumdaldt(:,iorder(1:idumsize))
psi(1:idumsize) = dumpsi(iorder(1:idumsize))
dpsidt(1:idumsize) = dumdpsidt(iorder(1:idumsize))
hh(1:idumsize) = dumhh(iorder(1:idumsize))
dhdt(1:idumsize) = dumdhdt(iorder(1:idumsize))
gradh(1:idumsize) = dumgradh(iorder(1:idumsize))
gradhn(1:idumsize) = dumgradhn(iorder(1:idumsize))
gradsoft(1:idumsize) = dumgradsoft(iorder(1:idumsize))
gradgradh(1:idumsize) = dumgradgradh(iorder(1:idumsize))
if (allocated(poten)) poten(1:idumsize) = dumpoten(iorder(1:idumsize))
pr(1:idumsize) = dumpr(iorder(1:idumsize))
spsound(1:idumsize) = dumspsound(iorder(1:idumsize))
! ll(1:idumsize) = dumll(1:idumsize)
divB(1:idumsize) = dumdivB(iorder(1:idumsize))
ireal(1:idumsize) = idumireal(iorder(1:idumsize))
itype(1:idumsize) = idumitype(iorder(1:idumsize))
numneigh(1:idumsize) = idumnumneigh(iorder(1:idumsize))
sqrtg(1:idumsize) = dumsqrtg(iorder(1:idumsize))
dens(1:idumsize) = dumdens(iorder(1:idumsize))
pmom(:,1:idumsize) = dumpmom(:,iorder(1:idumsize))
pmomin(:,1:idumsize) = dumpmomin(:,iorder(1:idumsize))
if (allocated(sourceterms)) then
sourceterms(:,1:idumsize) = dumsourceterms(:,iorder(1:idumsize))
endif
dxdx(:,1:idumsize) = dumdxdx(:,iorder(1:idumsize))
x0(:,1:idumsize) = dumx0(:,iorder(1:idumsize))
rho0(1:idumsize) = dumrho0(iorder(1:idumsize))
!
!--dust
!
if (onef_dust) then
dustfrac(:,1:idumsize) = dumdustfrac(:,iorder(1:idumsize))
dustevol(:,1:idumsize) = dumdustevol(:,iorder(1:idumsize))
ddustevoldt(:,1:idumsize) = dumddustevoldt(:,iorder(1:idumsize))
deltav(:,1:idumsize) = dumdeltav(:,iorder(1:idumsize))
ddeltavdt(:,1:idumsize) = dumddeltavdt(:,iorder(1:idumsize))
dustevolin(:,1:idumsize) = dumdustevolin(:,iorder(1:idumsize))
deltavin(:,1:idumsize) = dumdeltavin(:,iorder(1:idumsize))
rhogas(1:idumsize) = dumrhogas(iorder(1:idumsize))
rhodust(:,1:idumsize) = dumrhodust(:,iorder(1:idumsize))
endif
! no need to copy physical viscosity stuff
else
itype(:) = 0 ! on first memory allocation, set all parts = normal
numneigh(:) = 0
dxdx(:,:) = 1.
endif
!
!--debugging information
!
if (trace) write(iprint,*) ' memory allocated, size(x,v)=',size(x),size(vel)
return
end subroutine alloc
end module mem_allocation