-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdvectRH4.f90
375 lines (317 loc) · 10.8 KB
/
AdvectRH4.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
program rh4advection
use NumberKindsModule
use OutputWriterModule
use LoggerModule
use SphereMeshModule
use AdvectionModule
use ParticlesModule
use PanelsModule
use TracerSetupModule
use VTKOutputModule
use BVESetupModule
use SphereRemeshModule
implicit none
include 'mpif.h'
!
! mesh variables
!
type(SphereMesh) :: sphere
integer(kint) :: panelKind, initNest, AMR, nTracer
type(Particles), pointer :: sphereParticles
type(Panels), pointer :: spherePanels
!
! tracer variables
!
type(TracerSetup) :: nullScalar
integer(kint) :: tracerID
!
! vorticity placeholder
!
type(BVESetup) :: nullVort
!
! remeshing / refinement variables
!
type(RemeshSetup) :: remesh
integer(kint) :: remeshInterval, resetAlphaInterval, amrLimit, remeshCounter
real(kreal) :: tracerMassTol, tracerVarTol
type(ReferenceSphere), pointer :: reference
!
! time stepping variables
!
type(AdvRK4Data) :: timekeeper
real(kreal) :: t, tfinal, dt
integer(kint) :: timesteps, timeJ
!
! output variables
!
type(VTKSource) :: vtkOut, meshOut
character(len = MAX_STRING_LENGTH) :: vtkRoot, vtkFile, vtkMeshFile, outputDir, jobPrefix, dataFile, summaryFile
character(len = 56) :: amrString
integer(kint) :: frameCounter, frameOut, readWriteStat
type(OutputWriter) :: writer
!
! test case variables
!
real(kreal), allocatable :: totalTracer(:), tracerVar(:)
!
! logging
!
type(Logger) :: exeLog
character(len=28) :: logkey
character(len=MAX_STRING_LENGTH) :: logstring
!
! mpi / computing environment / general variables
!
integer(kint) :: errCode
real(kreal) :: wallclock
integer(kint) :: j
!
! namelists and user input
!
character(len=MAX_STRING_LENGTH) :: namelistFile = 'AdvectRH4.namelist'
namelist /meshDefine/ initNest, AMR, panelKind, amrLimit, tracerMassTol, tracerVarTol
namelist /timestepping/ tfinal, dt, remeshInterval, resetAlphaInterval
namelist /fileIO/ outputDir, jobPrefix, frameOut
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! INITIALIZE COMPUTER, MESH, TEST CASE
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call MPI_INIT(errCode)
call MPI_COMM_SIZE(MPI_COMM_WORLD, numProcs, errCode)
call MPI_COMM_RANK(MPI_COMM_WORLD, procRank, errCode)
call InitLogger(exeLog, procRank)
wallclock = MPI_WTIME()
nTracer = 2
tracerID = 1
!
! get user input
!
call ReadNamelistFile(procRank)
!
! build initial mesh
!
call New(sphere, panelKind, initNest, AMR, nTracer, ADVECTION_SOLVER)
call SetFlowMapLatitudeTracerOnMesh(sphere,1)
call SetFlowMapLatitudeTracerOnMesh(sphere,2)
!
! initialize remeshing and refinement
!
call ConvertFromRelativeTolerances(sphere, tracerMassTol, tracerVarTol, tracerID)
call LogMessage(exeLog, TRACE_LOGGING_LEVEL, 'tracerMassTol = ', tracerMassTol )
call LogMessage(exeLog, TRACE_LOGGING_LEVEL, 'tracerVarTol = ', tracerVarTol )
call New(remesh, tracerID, tracerMassTol, tracerVarTol, AMR)
nullify(reference)
if ( AMR > 0 ) then
!call InitialRefinement(sphere, remesh, SetGaussianHillsTracerOnMesh, gHills, NullVorticity, nullvort)
if ( panelKind == QUAD_PANEL ) &
write(amrstring,'(A,I1,A,I0.2,A)') 'quadAMR_', initNest, 'to', initNest+amrLimit, '_'
if ( panelKind == TRI_PANEL ) &
write(amrstring,'(A,I1,A,I0.2,A)') 'triAMR_', initNest, 'to', initNest+amrLimit, '_'
else
if ( panelKind == QUAD_PANEL ) &
write(amrstring,'(A,I1,A)') 'quadUnif_', initNest, '_'
if ( panelKind == TRI_PANEL ) &
write(amrstring,'(A,I1,A)') 'triUnif_', initNest, '_'
endif
sphereParticles => sphere%particles
spherePanels => sphere%panels
!
! initialize output
!
if ( procrank == 0 ) then
call LogStats( sphere, exeLog)
write(vtkRoot,'(A,A,A,A,A)') trim(outputDir), '/vtkOut/',trim(jobPrefix),trim(amrString),'_'
write(vtkFile,'(A,I0.4,A)') trim(vtkRoot),0,'.vtk'
write(vtkMeshFile,'(A,A,I0.4,A)') trim(vtkRoot),'_mesh_',0,'.vtk'
write(summaryFile,'(A,A,A,A)') trim(outputDir), trim(jobPrefix), trim(amrString), '_summary.txt'
write(datafile,'(A,A,A,A)') trim(outputDir), trim(jobPrefix), trim(amrstring), '_calculatedData.m'
call New(vtkOut, sphere, vtkFile, 'rh4 advection')
call VTKOutput(vtkOut, sphere)
call New(meshOut, sphere, vtkMeshFile, 'rh4 advection')
call VTKOutputMidpointRule(meshOUt,sphere)
endif
!
! initialize time stepping
!
call New(timekeeper, sphere, numProcs)
timesteps = floor(tfinal / dt)
t = 0.0_kreal
remeshCounter = 0
frameCounter = 1
allocate(totalTracer(0:timesteps))
totalTracer = 0.0_kreal
totalTracer(0) = TotalMass(sphere,1)
allocate(tracerVar(0:timesteps))
tracerVar = 0.0_kreal
tracerVar(0) = TracerVariance(sphere,1)
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! RUN THE PROBLEM
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
do timeJ = 0, timesteps-1
if ( mod(timeJ+1, remeshInterval) == 0 ) then
!
! remesh before timestep
!
remeshCounter = remeshCounter + 1
!
! choose appropriate remeshing procedure
!
if ( remeshCounter < resetAlphaInterval ) then
!
! remesh to t = 0
!
call LagrangianRemeshToInitialTime( sphere, remesh, NullVorticity, nullVort, nullTracer, nullScalar)
call SetFlowMapLatitudeTracerOnMesh(sphere, 1)
call SetFlowMapLatitudeTracerOnMesh(sphere, 2)
elseif ( remeshCounter == resetAlphaInterval ) then
!
! remesh to t = 0, then create reference mesh to current time
!
call LagrangianRemeshToInitialTime( sphere, remesh, NullVorticity, nullVort, nullTracer, nullScalar)
call SetFlowMapLatitudeTracerOnMesh(sphere, 1)
call SetFlowMapLatitudeTracerOnMesh(sphere, 2)
allocate(reference)
call New(reference, sphere)
call ResetLagrangianParameter(sphere)
elseif ( remeshCounter > resetAlphaInterval .AND. mod(remeshCounter, resetAlphaInterval) == 0 ) then
!
! remesh to existing reference time, then create new reference mesh to current time
!
call LagrangianRemeshToReference( sphere, reference, remesh )
call Delete(reference)
call New( reference, sphere )
call SetFlowMapLatitudeTracerOnMesh(sphere, 2)
call ResetLagrangianParameter(sphere)
else
!
! remesh to existing reference time
!
call LagrangianRemeshToReference(sphere, reference, remesh)
endif
!
! delete objects associated with old mesh, create new ones for new mesh
!
sphereParticles => sphere%particles
spherePanels => sphere%panels
call Delete(timekeeper)
call New( timekeeper, sphere, numProcs )
if ( procRank == 0 ) then
call Delete(vtkOut)
call Delete(meshOut)
call New(vtkOut, sphere, vtkFile, 'rh4 advection')
call New(meshOut, sphere, vtkMeshFile, 'rh4 advection')
endif
endif ! remesh
!
! advance time
!
call AdvectionRK4Timestep( timekeeper, sphere, dt, t, procRank, numProcs, rh4velocity)
totalTracer(timeJ+1) = TotalMass(sphere, 1)
tracerVar(timeJ+1) = TracerVariance(sphere, 1)
t = real( timeJ+1, kreal) * dt
if ( procRank == 0 .AND. mod(timeJ+1,frameOut) == 0 ) then
call LogMessage(exeLog, TRACE_LOGGING_LEVEL, 'day = ', t/ONE_DAY )
write(vtkFile, '(A,I0.4,A)') trim(vtkRoot), frameCounter, '.vtk'
write(vtkMeshFile, '(A,A,I0.4,A)') trim(vtkRoot), '_mesh_', frameCounter, '.vtk'
call UpdateFilename(vtkOut, vtkFile)
call UpdateFilename(meshOut, vtkMeshFile)
call VTKOutput(vtkOut, sphere)
call VTKOutputMidpointRule(meshOut, sphere)
frameCounter = frameCounter + 1
endif
enddo
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! OUTPUT FINAL DATA
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if ( procRank == 0 ) then
open( unit = WRITE_UNIT_1, file = datafile, status = 'REPLACE', action = 'WRITE', iostat = readwritestat)
if ( readwritestat /= 0 ) then
call LogMessage(exeLog, ERROR_LOGGING_LEVEL, 'data file ERROR : ', ' failed to open data file.')
else
write(WRITE_UNIT_1, *) 'totalTracer = [', totalTracer(0), ';...'
do j = 1, timesteps-1
write(WRITE_UNIT_1, *) totalTracer(j), ';...'
enddo
write(WRITE_UNIT_1, *) totalTracer(timesteps), '];'
write(WRITE_UNIT_1, *) 'tracerVar = [', tracerVar(0), ';...'
do j = 1, timesteps-1
write(WRITE_UNIT_1, *) tracerVar(j), ';...'
enddo
write(WRITE_UNIT_1,*) tracerVar(timesteps), '];'
endif
close(WRITE_UNIT_1)
write(logstring,'(A, F8.2,A)') 'elapsed time = ', (MPI_WTIME() - wallClock)/60.0, ' minutes.'
call LogMessage(exelog,TRACE_LOGGING_LEVEL,'PROGRAM COMPLETE : ',trim(logstring))
endif
if ( associated(reference) ) then
call Delete(reference)
deallocate(reference)
endif
deallocate(totalTracer)
deallocate(tracerVar)
call Delete(timekeeper)
call Delete(remesh)
if ( procRank == 0 ) then
call Delete(vtkOut)
call Delete(meshOut)
endif
call Delete(sphere)
call Delete(exeLog)
call MPI_Finalize(errCode)
contains
subroutine ConvertFromRelativeTolerances(aMesh, tracerMassTol, tracerVarTol, tracerID)
type(SphereMesh), intent(in) :: amesh
real(kreal), intent(inout) :: tracerMassTol, tracerVarTol
integer(kint), intent(in) :: tracerID
tracerMassTol = tracerMassTol * MaximumTracerMass(aMesh, tracerID)
tracerVarTol = tracerVarTol * MaximumTracerVariation(aMesh, tracerID)
end subroutine
subroutine ReadNamelistFile(rank)
integer(kint), intent(in) :: rank
integer(kint), parameter :: BCAST_INT_SIZE = 6, BCAST_REAL_SIZE= 4
integer(kint) :: broadcastIntegers(BCAST_INT_SIZE)
real(kreal) :: broadcastReals(BCAST_REAL_SIZE)
if ( rank == 0 ) then
open(unit=READ_UNIT, file=namelistfile, status='OLD', action='READ', iostat=readWriteStat)
if ( readWriteStat /= 0 ) stop 'cannot read namelist file.'
read(READ_UNIT, nml=meshDefine)
rewind(READ_UNIT)
read(READ_UNIT, nml=timestepping)
rewind(READ_UNIT)
read(READ_UNIT, nml=fileIO)
rewind(READ_UNIT)
close(READ_UNIT)
broadcastIntegers(1) = panelKind
broadcastIntegers(2) = initNest
broadcastIntegers(3) = AMR
broadcastIntegers(4) = amrLimit
broadcastIntegers(5) = remeshInterval
broadcastIntegers(6) = resetAlphaInterval
broadcastReals(1) = tracerMassTol
broadcastReals(2) = tracerVarTol
broadcastReals(3) = dt
broadcastReals(4) = tfinal
endif
call MPI_BCAST(broadcastIntegers, BCAST_INT_SIZE, MPI_INTEGER, 0, MPI_COMM_WORLD, errCode)
panelKind = broadcastIntegers(1)
initNest = broadcastIntegers(2)
AMR = broadcastIntegers(3)
amrLimit = broadcastIntegers(4)
remeshInterval = broadcastIntegers(5)
resetAlphaInterval = broadcastIntegers(6)
call MPI_BCAST(broadcastReals, BCAST_REAL_SIZE, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, errCode)
tracerMassTol = broadcastReals(1)
tracerVarTol = broadcastReals(2)
dt = broadcastReals(3) * ONE_DAY ! convert time to seconds
tfinal = broadcastReals(4) * ONE_DAY ! convert time to seconds
end subroutine
subroutine InitLogger(alog,rank)
type(Logger), intent(out) :: aLog
integer(kint), intent(in) :: rank
if ( rank == 0 ) then
call New(aLog,DEBUG_LOGGING_LEVEL)
else
call New(aLog,WARNING_LOGGING_LEVEL)
endif
write(logKey,'(A,I0.2,A)') 'EXE_LOG',rank,' : '
end subroutine
end program