-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart-densityfield_fluct.F90
350 lines (292 loc) · 11.2 KB
/
cart-densityfield_fluct.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
module problem
! Module for Capreole2D (F90)
! Author: Garrelt Mellema
! Date: 2015-08-11 (2004-05-11)
! This is the problem module. It contains the routines which define the
! problem being solved:
! Contents:
! init_problem - sets up the hydro variables according to the specified
! problem
! inflow - sets the inflow boundary conditions
! This version: specify an average density (n) and a field of
! fluctuations (delta). The density field used will be n*(1+delta).
! To avoid negative densities we impose a lower density limit eta
! so that the minimum density is eta*n.
! Temperature can be set isobarically (specify average temperature)
! or isothermal.
! Coordinates: cartesian coordinates.
use file_admin, only: stdinput
use precision, only: dp
use cgsconstants, only: m_p
use my_mpi
use sizes, only: mbc,neq,RHO,RHVX,RHVY,RHVZ,EN,nrofDim
use scaling, only: SCDENS, SCMOME, SCENER
use atomic, only: gamma1
use abundances, only: mu
use mesh, only: sx,ex,sy,ey,sz,ez,meshx,meshy,meshz
!use grid
use hydro, only: state,pressr,set_state_pointer,NEW,OLD,restart_state
use boundary, only: boundaries,REFLECTIVE,OUTFLOW,PROBLEM_DEF,X_IN,X_OUT,Y_IN, &
Y_OUT,Z_IN,Z_OUT
use ionic, only: init_ionic
use tped
implicit none
!> lowest density is minimum_fluctuation*average_density
real(kind=dp),parameter :: minimum_fluctuation=0.1
integer, dimension(nrofDim,2) :: domainboundaryconditions
contains
subroutine init_problem (restart,restartfile)
! This routine initializes all hydro variables
! This may be a fresh start or a restart of a saved run
logical,intent(in) :: restart ! tells you whether it's a new run or a restart
character(len=19),intent(in) :: restartfile
! Local variables
real(kind=dp) :: r_interface ! dummy needed for calling init_ionic
integer :: ierror
! Set domain boundary conditions
domainboundaryconditions(2:3,:)=REFLECTIVE
domainboundaryconditions(1,2)=OUTFLOW
domainboundaryconditions(1,1)=OUTFLOW
if (.not.restart) then ! Fresh start
call fresh_start_state( )
else
call restart_state(restartfile,ierror)
state(:,:,:,RHO)=state(:,:,:,RHO)/scdens
state(:,:,:,RHVX)=state(:,:,:,RHVX)/scmome
state(:,:,:,RHVY)=state(:,:,:,RHVY)/scmome
state(:,:,:,RHVZ)=state(:,:,:,RHVZ)/scmome
state(:,:,:,EN)=state(:,:,:,EN)/scener
call boundaries(OLD,domainboundaryconditions,problemboundary) ! Fill boundary conditions
endif
! Initialize the ionic concentrations
call init_ionic(restart,r_interface)
end subroutine init_problem
!==========================================================================
subroutine fresh_start_state ( )
! This routine initializes all hydro variables for a fresh start
! Case: density fields
! smoothing parameter for making soft-edged clump
real(kind=dp),parameter :: eta=0.1_dp
! e variables are environment
real(kind=dp) :: average_temperature
real(kind=dp) :: epressr
integer :: mx1,my1,mz1,temperature_setup
character(len=512) :: densityfluctuations_file
real,allocatable,dimension(:,:,:) :: tempdens
real(kind=dp) :: average_density_cm3
real(kind=dp) :: maxdens
integer :: i,j,k,nitt,ieq
#ifdef MPI
integer :: status(MPI_STATUS_SIZE)
integer :: request,nextproc
integer,parameter :: outputcircle=601
integer :: ierror
#endif
! Ask for the input if you are processor 0.
if (rank == 0) then
write (*,'(A,$)') '1) Average density (cm^-3): '
read (stdinput,*) average_density_cm3
write (*,'(A,$)') '1) File with density fluctuations: '
read (stdinput,*) densityfluctuations_file
write (*,'(A,$)') '2) Temperature setup: '
write (*,'(A,$)') ' 1: isobaric: '
write (*,'(A,$)') ' 2: isothermal: '
read (stdinput,*) temperature_setup
if (temperature_setup == 1) then
write (*,'(A,$)') '3) Average temperature: '
read (stdinput,*) average_temperature
else
write (*,'(A,$)') '3) Temperature: '
read (stdinput,*) average_temperature
endif
endif
! report input parameters
if (rank == 0) then
write(30,'(A)') &
'Problem: cart-densityfield_fluct (cartesian: density field with fluctuations)'
write (30,'(A,es10.3)') '1) Average density (cm^-3): ',average_density_cm3
write (30,'(2A)') '2) Density fluctuations file (cm^-3): ', &
densityfluctuations_file
write (30,'(A)') '3) Temperature setup:'
if (temperature_setup == 1) then
write (30,'(A)') 'isobaric'
write (30,'(A,es10.3)') '4) Average temperature: ',average_temperature
else
write (30,'(A)') ' isothermal'
write (30,'(A,es10.3)') '4) Temperature: ',average_temperature
endif
endif
#ifdef MPI
! Distribute the input parameters to the other nodes
! (densityfield_file is distributed in the MPI ring below)
call MPI_BCAST(average_density_cm3,1,MPI_DOUBLE_PRECISION,0, &
MPI_COMM_NEW,ierror)
call MPI_BCAST(temperature_setup,1,MPI_INTEGER,0, &
MPI_COMM_NEW,ierror)
call MPI_BCAST(average_temperature,1,MPI_DOUBLE_PRECISION,0, &
MPI_COMM_NEW,ierror)
#endif
! Read in density field (MPI ring)
if (rank.eq.0) then
open(unit=40,file=densityfluctuations_file,form='UNFORMATTED',status='OLD')
read(40) mx1,my1,mz1
write(30,*) mx1,my1,mz1
if (mx1 .ne. meshx .or. my1.ne.meshy .or. mz1.ne.meshz) then
write(30,*) 'Error: ', &
'density field does not match specified mesh size'
stop
endif
allocate(tempdens(1-mbc:mx1+mbc,1-mbc:my1+mbc,1-mbc:mz1+mbc))
read(40) tempdens(1:mx1,1:my1,1:mz1)
close(40)
#ifdef MPI
! Send filename to next processor
if (npr > 1) then
call MPI_ISSEND(densityfluctuations_file,512,MPI_CHARACTER,rank+1, &
outputcircle,MPI_COMM_NEW,request,ierror)
write(30,*) rank,'sent filename to ',rank+1
! Wait for the circle to complete
call MPI_RECV(densityfluctuations_file,512,MPI_CHARACTER,npr-1, &
outputcircle,MPI_COMM_NEW,status,ierror)
write(30,*) rank,'received filename from ',npr-1
flush(30)
endif
else
! Receive filename from previous processor
call MPI_RECV(densityfluctuations_file,512,MPI_CHARACTER,rank-1, &
outputcircle,MPI_COMM_NEW,status,ierror)
write(30,*) rank,'received filename from ',rank-1
flush(30)
if (ierror == 0) then ! if ok
open(unit=40,file=densityfluctuations_file,form='UNFORMATTED', &
status='OLD')
read(40) mx1,my1,mz1
write(30,*) mx1,my1,mz1
if (mx1 .ne. meshx .or. my1.ne.meshy .or. mz1.ne.meshz) then
write(30,*) 'Error: ', &
'density field does not match specified mesh size'
stop
endif
allocate(tempdens(1-mbc:mx1+mbc,1-mbc:my1+mbc,1-mbc:mz1+mbc))
read(40) tempdens(1:mx1,1:my1,1:mz1)
close(40)
! Determine next processor (npr -> 0)
nextproc=mod(rank+1,npr)
! Send filename along
call MPI_ISSEND(densityfluctuations_file,512,MPI_CHARACTER,nextproc, &
outputcircle,MPI_COMM_NEW,request,ierror)
write(30,*) rank,'sent filename to ',nextproc
else
write(30,*) 'MPI error in output routine'
ierror=ierror+1
endif
#endif
endif
flush(30)
! Assume outflow boundaries
do k=1-mbc,mz1+mbc
do j=1-mbc,my1+mbc
do i=1-mbc,0
tempdens(i,j,k)=tempdens(1,j,k)
enddo
enddo
enddo
do k=1-mbc,mz1+mbc
do j=1-mbc,0
do i=1-mbc,mx1+mbc
tempdens(i,j,k)=tempdens(i,1,k)
enddo
enddo
enddo
do k=1-mbc,0
do j=1-mbc,my1+mbc
do i=1-mbc,mx1+mbc
tempdens(i,j,k)=tempdens(i,j,1)
enddo
enddo
enddo
! Set lower fluctuation floor
do k=1-mbc,mz1+mbc
do j=1-mbc,my1+mbc
do i=1-mbc,mx1+mbc
if (1.0+tempdens(i,j,k) < minimum_fluctuation) &
tempdens(i,j,k)=minimum_fluctuation-1.0
enddo
enddo
enddo
! Find maximum density (for isobaric initial conditions)
write(30,*) maxval(tempdens),minval(tempdens),scdens
flush(30)
maxdens=maxval(tempdens)!*mu*m_p/scdens
! Set density in state array
! Scale to program scaling
do k=sz-mbc,ez+mbc
do j=sy-mbc,ey+mbc
do i=sx-mbc,ex+mbc
! densities are in cm^-3
state(i,j,k,RHO)=n2rho(average_density_cm3*(1.0+real(tempdens(i,j,k),dp)))/scdens
enddo
enddo
enddo
deallocate(tempdens)
! Set the initial momenta
do k=sz-mbc,ez+mbc
do j=sy-mbc,ey+mbc
do i=sx-mbc,ex+mbc
state(i,j,k,RHVX)=0.0d0
state(i,j,k,RHVY)=0.0d0
state(i,j,k,RHVZ)=0.0d0
enddo
enddo
enddo
write(30,*) 'setting pressure'
write(30,*) minval(state(:,:,:,RHO))
flush(30)
! Set the initial pressure and energy density
! (depends on the temperature setup, isobaric or isothermal)
if (temperature_setup == 1) then
! isobaric
epressr=temper2pressr(average_temperature,average_density_cm3,0.0_dp)/SCENER
do k=sz-mbc,ez+mbc
do j=sy-mbc,ey+mbc
do i=sx-mbc,ex+mbc
pressr(i,j,k)=epressr
state(i,j,k,EN)=pressr(i,j,k)/gamma1+ &
0.5d0*(state(i,j,k,RHVX)*state(i,j,k,RHVX)+ &
state(i,j,k,RHVY)*state(i,j,k,RHVY)+ &
state(i,j,k,RHVZ)*state(i,j,k,RHVZ))/state(i,j,k,RHO)
!state(i,j,k,TRACER1)=1.0d0
enddo
enddo
enddo
else
! isothermal
do k=sz-mbc,ez+mbc
do j=sy-mbc,ey+mbc
do i=sx-mbc,ex+mbc
pressr(i,j,k)=temper2pressr(average_temperature, &
rho2n(state(i,j,k,RHO)*scdens),0.0_dp)/SCENER
state(i,j,k,EN)=pressr(i,j,k)/gamma1+ &
0.5d0*(state(i,j,k,RHVX)*state(i,j,k,RHVX)+ &
state(i,j,k,RHVY)*state(i,j,k,RHVY)+ &
state(i,j,k,RHVZ)*state(i,j,k,RHVZ))/state(i,j,k,RHO)
!state(i,j,k,TRACER1)=1.0d0
enddo
enddo
enddo
endif
end subroutine fresh_start_state
!==========================================================================
subroutine problemboundary (boundary_id,newold)
! This routine resets the inner boundary to the inflow condition
! Version: dummy routine
integer,intent(in) :: boundary_id
integer,intent(in) :: newold
end subroutine problemboundary
!==========================================================================
subroutine apply_grav_force(dt,newold)
! Dummy routine
real(kind=dp),intent(in) :: dt
integer,intent(in) :: newold
end subroutine apply_grav_force
end module problem