-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReferenceSphere.f90
471 lines (431 loc) · 14.8 KB
/
ReferenceSphere.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
module ReferenceSphereModule
!******************************************************************************
! Peter A. Bosler
! Department of Mathematics
! University of Michigan
! pbosler@umich.edu
!
!******************************************************************************
!
! Defines output data structures and methods for plotting Lagrangian meshes of the sphere.
!
! Bosler, P.A., "Particle Methods for Geophysical Flow on the Sphere," PhD Thesis; the University of Michigan, 2013.
!
!----------------
use NumberKindsModule
use SphereGeomModule
use IntegerListModule
use LoggerModule
use ParticlesModule
use EdgesModule
use PanelsModule
use SphereMeshModule
use STRIPACKInterfaceModule
use SSRFPACKInterfaceModule
use RefineRemeshModule
implicit none
private
public ReferenceSphere
public New, Delete
public LagrangianRemesh
type ReferenceSphere
type(STRIPACKData) :: delTri
type(SSRFPACKData), pointer :: absVortSource => null(), &
tracerSource => null()
integer(kint) :: startSearch = 1
end type
!
!----------------
! Interfaces
!----------------
!
interface New
module procedure NewPrivate
end interface
interface Delete
module procedure DeletePrivate
end interface
interface LagrangianRemesh
module procedure LagrangianRemeshPrivate
end interface
!
!----------------
! Logging
!----------------
!
logical(klog), save :: logInit = .FALSE.
type(Logger) :: log
character(len=28), save :: logKey = 'ReferenceSphere'
integer(kint), parameter :: logLevel = DEBUG_LOGGING_LEVEL
character(len=28) :: formatString
character(len=128) :: logString
contains
!
!----------------
! Standard methods : Constructor / Destructor, Copy
!----------------
!
subroutine NewPrivate(self, oldSphere)
type(ReferenceSphere), intent(out) :: self
type(SphereMesh), intent(in) :: oldSphere
!
integer(kint) :: i, j, k, errCode
type(Panels), pointer :: oldPanels
type(Particles), pointer :: oldParticles
real(kreal) :: dSig
logical(klog) :: false
if ( .NOT. logInit) call InitLogger(log,procRank)
call LogMessage(log,DEBUG_LOGGING_LEVEL,logKey,' creating new reference sphere.')
call New(self%delTri,oldSphere)
call LogMessage(log,DEBUG_LOGGING_LEVEL,logKey,' reference sphere delaunay graph ready.')
oldParticles => oldSphere%particles
oldPanels => oldSphere%panels
if ( oldSphere%problemKind == BVE_SOLVER) then
false = .FALSE.
allocate(self%absVortSource)
call New(self%absVortSource, self%delTri, false)
call SetSourceAbsVort(self%absVortSource, self%delTri)
endif
if ( oldSphere%nTracer > 0 ) then
allocate(self%tracerSource)
call New(self%tracerSource, self%delTri, oldSphere%nTracer)
call SetSourceTracer(self%tracerSource, self%delTri)
endif
call LogMessage(log,DEBUG_LOGGING_LEVEL,logKey,' reference sphere ready.')
end subroutine
subroutine DeletePrivate(self)
type(ReferenceSphere), intent(inout) :: self
call Delete(self%delTri)
if ( associated(self%absVortSource) ) then
call Delete(self%absVortSource)
deallocate(self%absVortSource)
endif
if ( associated(self%tracerSource) ) then
call Delete(self%tracerSource)
deallocate(self%tracerSource)
endif
end subroutine
!
!----------------
! Public functions
!----------------
!
subroutine LagrangianRemeshPrivate(aMesh, reference, vortRefine, tracerRefine, flowMapRefine)
type(SphereMesh), intent(inout) :: aMesh
type(ReferenceSphere), intent(inout) :: reference
type(RefinementSetup), intent(in) :: vortRefine
type(RefinementSetup), intent(in) :: tracerRefine
type(RefinementSetup), intent(in) :: flowMapRefine
!
type(STRIPACKData) :: delTri
type(SSRFPACKData) :: lagSource
logical(klog) :: vectorInterp
type(SphereMesh) :: newMesh
type(Particles), pointer :: newParticles
type(Edges), pointer :: newEdges
type(Panels), pointer :: newPanels
integer(kint) :: j, k, amrLoopCounter, counter1, counter2
logical(klog), allocatable :: refineFlag(:)
logical(klog) :: keepGoing, refineTracer, refineVort, refineFlowMap
integer(kint) :: startIndex, nOldPanels, nOldParticles, refineCount, spaceLeft, limit
nullify(newParticles)
nullify(newEdges)
nullify(newPanels)
vectorInterp = .TRUE.
refineFlowMap = .FALSE.
refineTracer = .FALSE.
refineVort = .FALSE.
call LogMessage(log,DEBUG_LOGGING_LEVEL,logkey,' entering Lagrangian remesh.')
!
! determine what types of AMR to use
!
if ( tracerRefine%type == TRACER_REFINE .AND. &
GetNTracer(aMesh%panels) <= tracerRefine%tracerID ) refineTracer = .TRUE.
if ( vortRefine%type == RELVORT_REFINE ) refineVort = .TRUE.
if ( flowMapRefine%type == FLOWMAP_REFINE) refineFlowMap = .TRUE.
!
! set existing mesh as data source for interpolation
!
call New(delTri,aMesh)
call DelaunayTriangulation(delTri)
call New(lagSource,delTri,vectorInterp)
!if ( present(interpSmoothTol) ) call SetSigmaTol(lagSource,interpSmoothTol)
call SetSourceLagrangianParameter(lagSource,delTri)
call LogMessage(log,DEBUG_LOGGING_LEVEL,logkey,' remesh source data ready.')
!
! Build a new mesh
!
call New(newMesh,aMesh%panelKind,aMesh%initNest,aMesh%AMR,aMesh%nTracer,aMesh%problemKind)
newParticles => newMesh%particles
newEdges => newMesh%edges
newPanels => newMesh%panels
!
! interpolate lagrangian parameter from old mesh to new mesh
!
do j=1,newParticles%N
newParticles%x0(:,j) = InterpolateVector(newParticles%x(:,j),lagSource,delTri)
! renormalize to spherical surface
newParticles%x0(:,j) = newParticles%x0(:,j) / &
sqrt(sum(newParticles%x0(:,j)*newParticles%x0(:,j)))*EARTH_RADIUS
enddo
do j=1,newPanels%N
newPanels%x0(:,j) = InterpolateVector(newPanels%x(:,j),lagSource,delTri)
! renormalize
newPanels%x0(:,j) = newPanels%x0(:,j) / &
sqrt(sum(newPanels%x0(:,j)*newPanels%x0(:,j)))*EARTH_RADIUS
enddo
!
! set tracer values on new mesh
!
if ( aMesh%nTracer > 0 ) then
do j=1,newParticles%N
do k=1,aMesh%nTracer
newParticles%tracer(j,k) = GetTracer(reference,newParticles%x0(:,j),k)
enddo
enddo
do j=1,newPanels%N
if (.NOT. newPanels%hasChildren(j) ) then
do k=1,aMesh%nTracer
newPanels%tracer(j,k) = GetTracer(reference,newPanels%x0(:,j),k)
enddo
else
newPanels%tracer(j,:) = 0.0_kreal
endif
enddo
endif
!
! set vorticity values on new mesh
!
if ( aMesh%problemKind == BVE_SOLVER ) then
do j=1,newParticles%N
newParticles%absVort(j) = GetAbsVort(reference,newParticles%x0(:,j))
newParticles%relVort(j) = newParticles%absVort(j) - 2.0_kreal*OMEGA*newParticles%x(3,j)/EARTH_RADIUS
enddo
do j=1,newPanels%N
newPanels%absVort(j) = GetAbsVort(reference,newPanels%x0(:,j))
newPanels%relVort(j) = newPanels%absVort(j) - 2.0_kreal*OMEGA*newPanels%x(3,j)/EARTH_RADIUS
enddo
endif
!
! AMR
!
if ( aMesh%AMR > 0 ) then
allocate(refineFlag(newPanels%N_Max))
refineFlag = .FALSE.
startIndex = 1
keepGoing = .FALSE.
limit = 0
!
! Apply refinement criteria
!
if ( refineTracer ) then
limit = max(limit,tracerRefine%limit)
call FlagPanelsForTracerMaxRefinement(refineFlag,newMesh,tracerRefine,startIndex)
counter1 = count(refineFlag)
call FlagPanelsForTracerVariationRefinement(refineFlag,newMesh,tracerRefine,startIndex)
counter2 = count(refineFlag) - counter1
write(formatString,'(A)') '(A,I8,A)'
write(logString,formatString) 'LagRemesh AMR : tracerMax criterion triggered ', counter1, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
write(logString,formatString) 'LagRemesh AMR : tracerVar criterion triggered ', counter2, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
endif
if ( refineVort) then
limit = max(limit,vortRefine%limit)
counter1 = count(refineFlag)
call FlagPanelsForCirculationRefinement(refineFlag,newMesh,vortRefine,startIndex)
counter1 = count(refineFlag) - counter1
call FlagPanelsForRelVortVariationRefinement(refineFlag,newMesh,vortRefine,startIndex)
counter2 = count(refineFlag) - counter1
write(formatString,'(A)') '(A,I8,A)'
write(logString,formatString) 'LagRemesh AMR : circMax criterion triggered ', counter1, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
write(logString,formatString) 'LagRemesh AMR : relVortVar criterion triggered ', counter2, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
endif
if ( refineFlowMap ) then
limit = max(limit,flowMapRefine%limit)
counter1 = count(refineFlag)
call FlagPanelsForFlowMapRefinement(refineFlag,newMesh,flowMapRefine,startIndex)
counter1 = count(refineFlag) - counter1
write(formatString,'(A)') '(A,I8,A)'
write(logString,formatString) 'LagRemesh AMR : flowMap variation criterion triggered ', counter1, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
endif
refineCount = count(refineFlag)
spaceLeft = newPanels%N_Max - newPanels%N
!
! exit if refinement is not needed, or insufficient memory
!
if ( refineCount == 0 ) then
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,'LagRemesh : no refinement necessary.')
keepGoing = .FALSE.
elseif ( spaceLeft/4 < refineCount ) then
call LogMessage(log,WARNING_LOGGING_LEVEL,logkey,'LagRemesh : insufficient memory for AMR.')
keepGoing = .FALSE.
else
keepGoing = .TRUE.
endif
amrLoopCounter = 0
do while (keepGoing)
amrLoopCounter = amrLoopCounter + 1
write(logString,'(A,I3,A,I8,A)') 'AMR loop ',amrLoopCounter,' : refining ',refineCount,' panels.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
!
! divide flagged panels
!
nOldPanels = newPanels%N
nOldParticles = newParticles%N
do j=startIndex,newPanels%N
if ( refineFlag(j) ) then
call DividePanel(newMesh,j)
refineFlag(j) = .FALSE.
endif
enddo
!
! ensure adjacent panels differ by no more than one level
!
call FlagPanelsAtRefinementBoundaries(refineFlag,newMesh)
do j=1,newPanels%N
if ( refineFlag(j) ) then
call DividePanel(newMesh,j)
refineFlag(j) = .FALSE.
endif
enddo
!
! set problem data on mesh
!
do j=nOldParticles+1,newParticles%N
newParticles%x0(:,j) = InterpolateVector(newParticles%x(:,j),lagSource,delTri)
newParticles%x0(:,j) = newParticles%x0(:,j) / &
sqrt(sum(newParticles%x0(:,j)*newParticles%x0(:,j)))*EARTH_RADIUS
enddo
do j=nOldPanels+1,newPanels%N
newPanels%x0(:,j) = InterpolateVector(newPanels%x(:,j),lagSource,delTri)
newPanels%x0(:,j) = newPanels%x0(:,j) / &
sqrt(sum(newPanels%x0(:,j)*newPanels%x0(:,j)))*EARTH_RADIUS
enddo
if ( aMesh%nTracer > 0 ) then
do j=nOldParticles+1,newParticles%N
do k=1,aMesh%nTracer
newParticles%tracer(j,k) = GetTracer(reference,newParticles%x0(:,j),k)
enddo
enddo
do j=nOldPanels+1,newPanels%N
do k=1,aMesh%nTracer
newPanels%tracer(j,k) = GetTracer(reference,newPanels%x0(:,j),k)
enddo
enddo
endif
if ( aMesh%problemKind == BVE_SOLVER) then
do j=nOldParticles+1,newParticles%N
newParticles%absVort(j) = GetAbsVort(reference,newParticles%x0(:,j))
newParticles%relVort(j) = newParticles%absVort(j) - 2.0_kreal*OMEGA*newParticles%x(3,j)/EARTH_RADIUS
enddo
do j=nOldPanels+1,newPanels%N
newPanels%absVort(j) = GetAbsVort(reference,newPanels%x0(:,j))
newPanels%relVort(j) = newPanels%absVort(j) - 2.0_kreal*OMEGA*newPanels%x(3,j)/EARTH_RADIUS
enddo
endif
!
! prevent too much refinement
!
if ( amrLoopCounter >= limit ) then
keepGoing = .FALSE.
call LogMessage(log,WARNING_LOGGING_LEVEL,logkey,'LagRemesh WARNING : refinement limit reached.')
endif
!
! apply refinement criteria
!
startIndex = nOldPanels + 1
nOldPanels = newPanels%N
if ( refineTracer ) then
limit = max(limit,tracerRefine%limit)
call FlagPanelsForTracerMaxRefinement(refineFlag,newMesh,tracerRefine,startIndex)
counter1 = count(refineFlag)
call FlagPanelsForTracerVariationRefinement(refineFlag,newMesh,tracerRefine,startIndex)
counter2 = count(refineFlag) - counter1
write(formatString,'(A)') '(A,I8,A)'
write(logString,formatString) 'LagRemesh AMR : tracerMax criterion triggered ', counter1, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
write(logString,formatString) 'LagRemesh AMR : tracerVar criterion triggered ', counter2, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
endif
if ( refineVort) then
limit = max(limit,vortRefine%limit)
counter1 = count(refineFlag)
call FlagPanelsForCirculationRefinement(refineFlag,newMesh,vortRefine,startIndex)
counter1 = count(refineFlag) - counter1
call FlagPanelsForRelVortVariationRefinement(refineFlag,newMesh,vortRefine,startIndex)
counter2 = count(refineFlag) - counter1
write(formatString,'(A)') '(A,I8,A)'
write(logString,formatString) 'LagRemesh AMR : circMax criterion triggered ', counter1, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
write(logString,formatString) 'LagRemesh AMR : relVortVar criterion triggered ', counter2, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
endif
if ( refineFlowMap ) then
limit = max(limit,flowMapRefine%limit)
counter1 = count(refineFlag)
call FlagPanelsForFlowMapRefinement(refineFlag,newMesh,flowMapRefine,startIndex)
counter1 = count(refineFlag) - counter1
write(formatString,'(A)') '(A,I8,A)'
write(logString,formatString) 'LagRemesh AMR : flowMap variation criterion triggered ', counter1, ' times.'
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,logString)
endif
refineCount = count(refineFlag)
spaceLeft = newPanels%N_Max - newPanels%N
!
! exit if refinement is not needed, or insufficient memory
!
if ( refineCount == 0 ) then
call LogMessage(log,TRACE_LOGGING_LEVEL,logkey,'LagRemesh : refinement comverged.')
keepGoing = .FALSE.
elseif ( spaceLeft/4 < refineCount ) then
call LogMessage(log,WARNING_LOGGING_LEVEL,logkey,'LagRemesh : WARNING insufficient memory to continue AMR.')
keepGoing = .FALSE.
else
keepGoing = .TRUE.
endif
enddo ! while keepgoing
deallocate(refineFlag)
endif ! AMR
!
! replace old mesh with new mesh
!
call Copy(aMesh,newMesh)
!
! clean up
!
call Delete(newMesh)
call Delete(delTri)
call Delete(lagSource)
end subroutine
!
!----------------
! Module methods : type-specific functions
!----------------
!
function GetAbsVort(self, alphaIn)
real(kreal) :: GetAbsVort
type(ReferenceSphere), intent(inout) :: self
real(kreal), intent(in) :: alphaIn(3)
!
GetAbsVort = InterpolateScalar(alphaIn, self%absVortSource, self%delTri)
end function
function GetTracer(self, alphaIn, tracerID)
real(kreal) :: GetTracer
type(ReferenceSphere), intent(inout) :: self
real(kreal), intent(in) :: alphaIn(3)
integer(kint), intent(in) :: tracerID
!
GetTracer = InterpolateTracer(alphaIn, self%tracerSource, self%delTri, tracerID)
end function
subroutine InitLogger(aLog,rank)
type(Logger), intent(inout) :: aLog
integer(kint), intent(in) :: rank
write(logKey,'(A,A,I0.2,A)') trim(logKey),'_',rank,' : '
call New(aLog,logLevel)
logInit = .TRUE.
end subroutine
end module