-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_anyrho1Dran.f90
262 lines (233 loc) · 5.96 KB
/
setup_anyrho1Dran.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
!----------------------------------------------------------------
! Set up an arbitrary density distribution in 1D
! as a test for higher-dimensional grid->SPH data conversion
!----------------------------------------------------------------
subroutine setup
!
!--include relevant global variables
!
use dimen_mhd
use debug
use loguns
use bound
use options
use part
use setup_params
use mem_allocation, only:alloc
use uniform_distributions
use random, only:ran1
!
!--define local variables
!
implicit none
integer :: i,iseed,ipart,itry
integer, parameter :: npts = 100
real :: massp,totmass,rhofunc
real :: myran,rhomax,xi,dx,rhoi,rhoprev,dxmin
real, dimension(npts) :: rhogrid
real, external :: rhointerp,rhoexclude
logical, external :: iallowed
!
!--allow for tracing flow
!
if (trace) write(iprint,*) ' entering subroutine setup(anyrho)'
!
!--set boundaries
!
ibound = 0 ! boundaries
nbpts = 0 ! use ghosts not fixed
xmin(:) = 0. ! set position of boundaries
xmax(:) = 1.
print*,' enter ntotal '
read*,ntotal
call alloc(int(1.1*ntotal))
totmass = 0.
!--integrate to get total mass
dx = (xmax(1)-xmin(1))/real(npts)
rhomax = 0.
rhoprev = 0.
!--setup grid based data
do i=1,npts
xi = (i-0.5)*dx
rhogrid(i) = rhofunc(xi)
print*,'xgrid = ',xi
enddo
!--integrate to get total mass
rhoprev = rhointerp(0.,rhogrid,dx,npts)
do i=1,npts
xi = i*dx
rhoi = rhointerp(xi,rhogrid,dx,npts)
totmass = totmass + 0.5*(rhoi + rhoprev)*dx
rhomax = max(rhomax,rhoi)
rhoprev = rhoi
enddo
print*,'totmass = ',totmass,' rhomax = ',rhomax
!--set particle mass based on total mass
massp = totmass/ntotal
ipart = 0
iseed = -233
!--initialise sobol sequence
call sobseq(-123,xi)
itry = 0
dxmin = (massp/rhomax)**dndim
do while (ipart < ntotal)
itry = itry + 1
xi = 0.001*(itry-1)*dxmin
if (xi.gt.xmax(1)) then
ntotal = ipart
endif
! call sobseq(1,xi)
!xi = ran1(iseed)
rhoi = rhointerp(xi,rhogrid,dx,npts)
if (iallowed(xi,rhoi,ipart,massp,x(1,1:ipart),hh(1:ipart))) then
ipart = ipart + 1
x(1,ipart) = xi
print*,ipart,xi,rhoi
hh(ipart) = (massp/rhointerp(xi,rhogrid,dx,npts))
endif
!rhoi = rhoexclude(xi,rhogrid,dx,npts,ipart,massp,x(1,1:ipart),hh(1:ipart))
!myran = ran1(iseed)
!if (myran < rhoi/rhomax) then
! ipart = ipart + 1
! x(1,ipart) = xi
! print*,ipart,xi,rhoi
! hh(ipart) = hfact*(massp/rhointerp(xi,rhogrid,dx,npts))
!endif
enddo
npart = ipart
ntotal = npart
print*,'npart =',npart
!
!--now assign particle properties
!
do i=1,ntotal
vel(:,i) = 0.
!!!vel(1,i) = x(1,i)
dens(i) = rhointerp(x(1,i),rhogrid,dx,npts)
pmass(i) = massp
uu(i) = 1.0 ! isothermal
bfield(:,i) = 0.
enddo
!
!--allow for tracing flow
!
if (trace) write(iprint,*) ' exiting subroutine setup'
return
end
!
!--get position of particle assuming a linear
! density profile between two adjacent points
!
subroutine getx(fracm,rho1,rho2,dx,xfrac)
implicit none
real, intent(in) :: fracm,rho1,rho2,dx
real, intent(out) :: xfrac
real :: AA, BB, CC, drho
drho = (rho2 - rho1)
if (dx.gt.epsilon(dx)) then
AA = 0.5*drho/dx
else
xfrac = 0.
return
endif
BB = rho1
if (BB.lt.0.) stop 'rho -ve on input to getx'
CC = -fracm
if (abs(AA).lt.epsilon(AA)) then ! linear equation
xfrac = -CC/BB
else
xfrac = 0.5/AA*(-BB + sqrt(BB**2 - 4.*AA*CC))
endif
return
end subroutine getx
real function rhofunc(xi)
use setup_params, only:pi
implicit none
real, intent(in) :: xi
rhofunc = 2. - xi**2
end function rhofunc
real function rhointerp(xi,rhogrid,dxgrid,nx)
implicit none
integer, intent(in) :: nx
real, intent(in), dimension(nx) :: rhogrid
real, intent(in) :: xi,dxgrid
integer :: i,ip1
real :: xgrid,dxfrac
! xi = (i-0.5)*dx
i = int(xi/dxgrid + 0.500001)
xgrid = (i-0.5)*dxgrid
dxfrac = (xi - xgrid)/dxgrid
!print*,'> x = ',xi,xgrid,'cell=',i,xi/dxgrid + 0.5,'frac=',dxfrac,'<'
ip1 = i + 1
if (ip1.gt.nx) ip1 = ip1 - nx
if (i.lt.1) i = i+nx
rhointerp = (1.-dxfrac)*rhogrid(i) + dxfrac*rhogrid(ip1)
end function rhointerp
!
!--function to get probability EXCLUDING particles
! we have already set up
!
real function rhoexclude(xi,rhogrid,dxgrid,nx,np,massp,xp,hp)
implicit none
integer, intent(in) :: nx
real, intent(in), dimension(nx) :: rhogrid
real, intent(in) :: xi,dxgrid
integer, intent(in) :: np
real, intent(in) :: massp
real, intent(in), dimension(np) :: xp,hp
integer :: i
real :: rhoin,rhotemp,rhointerp,wabi,h1i,h2i,r2,q2,q,hmin
real, parameter :: cnormk = 2./3.
rhoin = rhointerp(xi,rhogrid,dxgrid,nx)
hmin = 0.5*(massp/rhoin)
rhotemp = rhoin
!--subtract off density of particles already set
overi: do i=1,np
r2 = (xi - xp(i))**2
!--exclude if closer than minimum allowed separation
if (r2.lt.hmin*hmin) then
rhotemp = 0.
exit overi
else
h1i = 1./hp(i)
h2i = h1i*h1i
q2 = r2*h2i
if (q2 < 4.) then
q = sqrt(q2)
if (q2.lt.1.0) then
wabi = 1. - 1.5*q2 + 0.75*q2*q
else
wabi = 0.25*(2.-q)**3
endif
rhotemp = max(rhotemp - massp*cnormk*wabi*h1i,0.)
endif
endif
enddo overi
rhoexclude = rhotemp
!if (rhoexclude.lt.rhoin) print*,xi,np,' rho = ',rhoin,rhoexclude
end function rhoexclude
logical function iallowed(xi,rhoi,np,massp,xp,hp)
implicit none
integer, intent(in) :: np
real, intent(in) :: xi,rhoi,massp
real, intent(in), dimension(np) :: xp,hp
integer :: i
real :: hmin,r2
hmin = (massp/rhoi)
iallowed = .true.
!--exclude if any particle is closer than the allowed minimum separation
overi: do i=1,np
r2 = (xi - xp(i))**2
!--exclude if closer than minimum allowed separation
if (r2.lt.hmin*hmin) then
iallowed = .false.
exit overi
endif
enddo overi
end function iallowed
!
! use this routine to modify the dump upon code restart
!
subroutine modify_dump()
implicit none
end subroutine modify_dump