-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathAdvection_Diffusion_DG.F90
More file actions
3315 lines (2662 loc) · 125 KB
/
Advection_Diffusion_DG.F90
File metadata and controls
3315 lines (2662 loc) · 125 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Copyright (C) 2006 Imperial College London and others.
!
! Please see the AUTHORS file in the main source directory for a full list
! of copyright holders.
!
! Prof. C Pain
! Applied Modelling and Computation Group
! Department of Earth Science and Engineering
! Imperial College London
!
! amcgsoftware@imperial.ac.uk
!
! This library is free software; you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public
! License as published by the Free Software Foundation,
! version 2.1 of the License.
!
! This library is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
! Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public
! License along with this library; if not, write to the Free Software
! Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
! USA
#include "fdebug.h"
module advection_diffusion_DG
!!< This module contains the Discontinuous Galerkin form of the advection
!!< -diffusion equation for scalars.
use fldebug
use vector_tools
use global_parameters, only: OPTION_PATH_LEN, FIELD_NAME_LEN, COLOURING_DG2, &
COLOURING_DG0
use elements
use integer_set_module
use spud
#ifdef _OPENMP
use omp_lib
#endif
use parallel_tools
use sparse_tools
use shape_functions
use transform_elements
use fetools
use parallel_fields
use fields
use profiler
use state_module
use boundary_conditions
use sparsity_patterns
use dgtools
use vtk_interfaces
use field_options
use sparse_matrices_fields
use fefields
use field_derivatives
use coordinates
use sparsity_patterns_meshes
use petsc_solve_state_module
use boundary_conditions_from_options
use upwind_stabilisation
use slope_limiters_dg
use diagnostic_fields, only: calculate_diagnostic_variable
use colouring, only: get_mesh_colouring
implicit none
private
public solve_advection_diffusion_dg, construct_advection_diffusion_dg, &
advection_diffusion_dg_check_options
! Local private control parameters. These are module-global parameters
! because it would be expensive and/or inconvenient to re-evaluate them
! on a per-element or per-face basis
real :: dt, theta
! Whether the advection term is only integrated by parts once.
logical :: integrate_by_parts_once=.false.
! Whether the conservation term is integrated by parts or not
logical :: integrate_conservation_term_by_parts=.false.
! Weight between conservative and non-conservative forms of the advection
! equation.
! 1 is for conservative 0 is for non-conservative.
real :: beta
! Whether we are constructing equations in semidiscrete form
logical :: semi_discrete
! Whether to include various terms
logical :: include_advection, include_diffusion
! Whether we have a separate diffusion matrix
logical :: have_diffusion_m
! Discretisation to use for diffusion term.
integer :: diffusion_scheme
integer, parameter :: ARBITRARY_UPWIND=1
integer, parameter :: BASSI_REBAY=2
integer, parameter :: CDG=3
integer, parameter :: IP=4
integer, parameter :: MASSLUMPED_RT0=5
! Discretisation to use for advective flux.
integer :: flux_scheme
integer, parameter :: UPWIND_FLUX=1
integer, parameter :: LAX_FRIEDRICHS_FLUX=2
! Boundary condition types:
! (the numbers should match up with the order in the
! get_entire_boundary_condition call)
integer :: BCTYPE_WEAKDIRICHLET=1, BCTYPE_DIRICHLET=2, BCTYPE_NEUMANN=3
logical :: include_mass
! are we moving the mesh?
logical :: move_mesh
! Stabilisation schemes.
integer :: stabilisation_scheme
integer, parameter :: NONE=0
integer, parameter :: UPWIND=1
!IP penalty parameter
real :: Interior_Penalty_Parameter, edge_length_power
!special debugging options
logical :: debugging, remove_element_integral, remove_primal_fluxes, &
& remove_penalty_fluxes
real :: gradient_test_bound
! Method for getting h0 in IP
integer :: edge_length_option
integer, parameter :: USE_FACE_INTEGRALS=1
integer, parameter :: USE_ELEMENT_CENTRES=2
! CDG stuff
real, dimension(3) :: switch_g
logical :: remove_CDG_fluxes
logical :: CDG_penalty
! RT0 masslumping for diffusion
integer :: rt0_masslumping_scheme=0 ! choice from values below:
integer, parameter :: RT0_MASSLUMPING_ARBOGAST=1
integer, parameter :: RT0_MASSLUMPING_CIRCUMCENTRED=2
! Are we on a sphere?
logical :: on_sphere
! Vertical diffusion by mixing option
logical :: have_buoyancy_adjustment_by_vertical_diffusion
logical :: have_buoyancy_adjustment_diffusivity
contains
subroutine solve_advection_diffusion_dg(field_name, state, velocity_name)
!!< Construct and solve the advection-diffusion equation for the given
!!< field using discontinuous elements.
!! Name of the field to be solved for.
character(len=*), intent(in) :: field_name
!! Collection of fields defining system state.
type(state_type), intent(inout) :: state
character(len=*), optional, intent(in) :: velocity_name
!! Tracer to be solved for.
type(scalar_field), pointer :: T
!! Local velocity name.
character(len=FIELD_NAME_LEN) :: lvelocity_name, pmesh_name
!! Projected velocity field for them as needs it.
type(vector_field) :: pvelocity
!! Nonlinear velocity field.
type(vector_field), pointer :: U_nl, X
!! Mesh for projeced velocity.
type(mesh_type), pointer :: pmesh
ewrite(1,*) "In solve_advection_diffusion_dg"
ewrite(1,*) "Solving advection-diffusion equation for field " // &
trim(field_name) // " in state " // trim(state%name)
T=>extract_scalar_field(state, field_name)
! Set local velocity name:
if(present(velocity_name)) then
lvelocity_name=velocity_name
else
lvelocity_name="NonlinearVelocity"
end if
if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/advection_scheme"//&
&"/project_velocity_to_continuous")) then
if(.not.has_scalar_field(state, "Projected"//trim(lvelocity_name))) &
&then
call get_option(trim(T%option_path)&
//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/advection_scheme"//&
&"/project_velocity_to_continuous/mesh/name",pmesh_name)
U_nl=>extract_vector_field(state, lvelocity_name)
pmesh=>extract_mesh(state, pmesh_name)
X=>extract_vector_field(state, "Coordinate")
lvelocity_name="Projected"//trim(lvelocity_name)
call allocate(pvelocity, U_nl%dim, pmesh, lvelocity_name)
call project_field(U_nl, pvelocity, X)
call insert(state, pvelocity, lvelocity_name)
! Discard the additional reference.
call deallocate(pvelocity)
else
lvelocity_name="Projected"//trim(lvelocity_name)
pvelocity=extract_vector_field(state,lvelocity_name)
end if
end if
call get_option(trim(T%option_path)//"/prognostic/spatial_discretisation/"//&
&"conservative_advection", beta)
! by default we assume we're integrating by parts twice
integrate_by_parts_once = have_option(trim(T%option_path)//"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/advection_scheme/integrate_advection_by_parts/once")
integrate_conservation_term_by_parts = have_option(trim(T%option_path)//"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/advection_scheme/integrate_conservation_term_by_parts")
! Determine the scheme to use to discretise diffusivity.
if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/diffusion_scheme/bassi_rebay")) then
diffusion_scheme=BASSI_REBAY
else if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/diffusion_scheme/arbitrary_upwind")) then
diffusion_scheme=ARBITRARY_UPWIND
else if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin")) then
!=================Compact Discontinuous Galerkin
diffusion_scheme=CDG
!Set the switch vector
switch_g = 0.
switch_g(1) = exp(sin(3.0+exp(1.0)))
if(mesh_dim(T)>1) switch_g(2) = (cos(exp(3.0)/sin(2.0)))**2
if(mesh_dim(T)>2) switch_g(3) = sin(cos(sin(cos(3.0))))
switch_g = switch_g/sqrt(sum(switch_g**2))
!switch_g = 1.0/(sqrt(1.0*mesh_dim(T)))
remove_penalty_fluxes = .true.
interior_penalty_parameter = 0.0
if(have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/penalty_parameter")) then
remove_penalty_fluxes = .false.
edge_length_power = 0.0
call get_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/penalty_parameter"&
&,Interior_Penalty_Parameter)
end if
debugging = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation/"//&
&"discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug")
CDG_penalty = .true.
if(debugging) then
call get_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug/gradient_test_bound",&
&gradient_test_bound)
remove_element_integral = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug/remove_element_integral")
remove_primal_fluxes = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug/remove_primal_fluxes")
remove_cdg_fluxes = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug/remove_cdg_fluxes")
if (have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug"//&
&"/edge_length_power")) then
call get_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/compact_discontinuous_galerkin/debug"//&
&"/edge_length_power",edge_length_power)
cdg_penalty = .false.
end if
end if
edge_length_option = USE_FACE_INTEGRALS
else if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty")) then
remove_penalty_fluxes = .false.
diffusion_scheme=IP
CDG_penalty = .false.
call get_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/penalty_parameter",Interior_Penalty_Parameter)
call get_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/edge_length_power",edge_length_power)
edge_length_option = USE_FACE_INTEGRALS
if(have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/edge_length_option/use_element_centres")) then
edge_length_option = USE_ELEMENT_CENTRES
end if
debugging = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/debug")
remove_element_integral = .false.
remove_primal_fluxes = .false.
if(debugging) then
call get_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/debug/gradient_test_bound",gradient_test_bound)
remove_element_integral = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/debug/remove_element_integral")
remove_primal_fluxes = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/debug/remove_primal_fluxes")
remove_penalty_fluxes = have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/interior_penalty/debug/remove_penalty_fluxes")
end if
else if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/masslumped_rt0")) then
diffusion_scheme=MASSLUMPED_RT0
if (have_option(trim(T%option_path)//&
&"/prognostic/spatial_discretisation/discontinuous_galerkin/diffusion_scheme/masslumped_rt0/arbogast"&
&)) then
rt0_masslumping_scheme=RT0_MASSLUMPING_ARBOGAST
else if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/diffusion_scheme"//&
&"/masslumped_rt0/circumcentred")) then
rt0_masslumping_scheme=RT0_MASSLUMPING_CIRCUMCENTRED
else
FLAbort("Unknown rt0 masslumping for P0 diffusion.")
end if
else
FLAbort("Unknown diffusion scheme for DG Advection Diffusion")
end if
! Vertical mixing by diffusion
have_buoyancy_adjustment_by_vertical_diffusion=have_option(trim(T%option_path)//"/prognostic/buoyancy_adjustment/by_vertical_diffusion")
if (have_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/number_advection_subcycles")) then
call solve_advection_diffusion_dg_subcycle(field_name, state, lvelocity_name)
else if (have_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/maximum_courant_number_per_subcycle")) then
call solve_advection_diffusion_dg_subcycle(field_name, state, lvelocity_name)
else
call solve_advection_diffusion_dg_theta(field_name, state, lvelocity_name)
end if
end subroutine solve_advection_diffusion_dg
subroutine solve_advection_diffusion_dg_theta(field_name, state, velocity_name)
!!< Construct and solve the advection-diffusion equation for the given
!!< field unsing discontinuous elements.
!! Name of the field to be solved for.
character(len=*), intent(in) :: field_name
!! Collection of fields defining system state.
type(state_type), intent(inout) :: state
!! Name of advecting velocity field
character(len=*), intent(in) :: velocity_name
!! Tracer to be solved for.
type(scalar_field), pointer :: T, T_old
!! Change in T over one timestep.
type(scalar_field) :: delta_T
!! Sparsity of advection_diffusion matrix.
type(csr_sparsity), pointer :: sparsity
!! System matrix.
type(csr_matrix) :: matrix
!! Right hand side vector.
type(scalar_field) :: rhs
T=>extract_scalar_field(state, field_name)
T_old=>extract_scalar_field(state, "Old"//field_name)
! Reset T to value at the beginning of the timestep.
call set(T, T_old)
select case(diffusion_scheme)
case(CDG)
! This is bigger than we need for CDG
sparsity => get_csr_sparsity_compactdgdouble(state, T%mesh)
!sparsity => get_csr_sparsity_secondorder(state, T%mesh, T%mesh)
case(IP)
sparsity => get_csr_sparsity_compactdgdouble(state, T%mesh)
case default
sparsity => get_csr_sparsity_secondorder(state, T%mesh, T%mesh)
end select
call allocate(matrix, sparsity) ! Add data space to the sparsity
! pattern.
! Ensure delta_T inherits options from T.
call allocate(delta_T, T%mesh, trim(field_name)//"Change")
delta_T%option_path = T%option_path
call allocate(rhs, T%mesh, trim(field_name)//"RHS")
call construct_advection_diffusion_dg(matrix, rhs, field_name, state,&
velocity_name=velocity_name)
! Apply strong dirichlet boundary conditions.
! This is for big spring boundary conditions.
call apply_dirichlet_conditions(matrix, rhs, T, dt)
call zero(delta_T) ! Impose zero initial guess.
! Solve for the change in T.
call petsc_solve(delta_T, matrix, rhs, state)
! Add the change in T to T.
call addto(T, delta_T, dt)
call deallocate(delta_T)
call deallocate(matrix)
call deallocate(rhs)
end subroutine solve_advection_diffusion_dg_theta
subroutine solve_advection_diffusion_dg_subcycle(field_name, state, velocity_name)
!!< Construct and solve the advection-diffusion equation for the given
!!< field using discontinuous elements.
!! Name of the field to be solved for.
character(len=*), intent(in) :: field_name
!! Collection of fields defining system state.
type(state_type), intent(inout) :: state
!! Optional velocity name
character(len = *), intent(in) :: velocity_name
!! Tracer to be solved for.
type(scalar_field), pointer :: T, T_old, s_field
!! Coordinate field
type(vector_field), pointer :: X, U_nl
!! Change in T over one timestep.
type(scalar_field) :: delta_T
!! Sparsity of advection_diffusion matrix.
type(csr_sparsity), pointer :: sparsity
!! System matrix.
type(csr_matrix) :: matrix, matrix_diff, mass, inv_mass
!! Sparsity of mass matrix.
type(csr_sparsity) :: mass_sparsity
!! Right hand side vector.
type(scalar_field) :: rhs, rhs_diff
!! Whether to invoke the slope limiter
logical :: limit_slope
!! Which limiter to use
integer :: limiter
!! Number of advection subcycles.
integer :: subcycles
real :: max_courant_number
character(len=FIELD_NAME_LEN) :: limiter_name
integer :: i
!! Courant number field name used for temporal subcycling
character(len=FIELD_NAME_LEN) :: Courant_number_name
T=>extract_scalar_field(state, field_name)
T_old=>extract_scalar_field(state, "Old"//field_name)
X=>extract_vector_field(state, "Coordinate")
! Reset T to value at the beginning of the timestep.
call set(T, T_old)
sparsity => get_csr_sparsity_firstorder(state, T%mesh, T%mesh)
call allocate(matrix, sparsity) ! Add data space to the sparsity
! pattern.
select case(diffusion_scheme)
case(CDG)
! This is bigger than we need for CDG
sparsity => get_csr_sparsity_compactdgdouble(state, T%mesh)
!sparsity => get_csr_sparsity_secondorder(state, T%mesh, T%mesh)
case(IP)
sparsity => get_csr_sparsity_compactdgdouble(state, T%mesh)
case default
sparsity => get_csr_sparsity_secondorder(state, T%mesh, T%mesh)
end select
! Ditto for diffusion
call allocate(matrix_diff, sparsity)
mass_sparsity=make_sparsity_dg_mass(T%mesh)
call allocate(mass, mass_sparsity)
call allocate(inv_mass, mass_sparsity)
! Ensure delta_T inherits options from T.
call allocate(delta_T, T%mesh, "delta_T")
delta_T%option_path = T%option_path
call allocate(rhs, T%mesh, trim(field_name)//" RHS")
call allocate(rhs_diff, T%mesh, trim(field_name)//" Diffusion RHS")
call construct_advection_diffusion_dg(matrix, rhs, field_name, state, &
mass=mass, diffusion_m=matrix_diff, diffusion_rhs=rhs_diff, semidiscrete=.true., &
velocity_name=velocity_name)
! mass has only been assembled only for owned elements, so we can only compute
! its inverse for owned elements
call get_dg_inverse_mass_matrix(inv_mass, mass, only_owned_elements=.true.)
! Note that since theta and dt are module global, these lines have to
! come after construct_advection_diffusion_dg.
call get_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/theta", theta)
call get_option("/timestepping/timestep", dt)
if(have_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/number_advection_subcycles")) then
call get_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/number_advection_subcycles", subcycles)
else
call get_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/maximum_courant_number_per_subcycle", Max_Courant_number)
! Determine the courant field to use to find the max
call get_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/maximum_courant_number_per_subcycle/courant_number/name", &
&Courant_number_name, default="DG_CourantNumber")
s_field => extract_scalar_field(state, trim(Courant_number_name))
call calculate_diagnostic_variable(state, trim(Courant_number_name), &
& s_field, option_path=trim(T%option_path)//&
&"/prognostic/temporal_discretisation/discontinuous_galerkin"//&
&"/courant_number")
subcycles = ceiling( maxval(s_field%val)/Max_Courant_number)
call allmax(subcycles)
ewrite(2,*) 'Number of subcycles for tracer eqn: ', subcycles
end if
limit_slope=.false.
if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/slope_limiter")) then
limit_slope=.true.
! Note unsafe for mixed element meshes
if (element_degree(T,1)==0) then
FLExit("Slope limiters make no sense for degree 0 fields")
end if
call get_option(trim(T%option_path)//"/prognostic/spatial_discretisation"//&
&"/discontinuous_galerkin/slope_limiter/name",limiter_name)
select case(trim(limiter_name))
case("Cockburn_Shu")
limiter=LIMITER_COCKBURN
case("Hermite_Weno")
limiter=LIMITER_HERMITE_WENO
case("minimal")
limiter=LIMITER_MINIMAL
case("FPN")
limiter=LIMITER_FPN
case("Vertex_Based")
limiter=LIMITER_VB
case default
FLAbort('No such limiter')
end select
end if
U_nl=>extract_vector_field(state, velocity_name)
do i=1, subcycles
! dT = Advection * T
call mult(delta_T, matrix, T)
! dT = dT + RHS
call addto(delta_T, RHS, -1.0)
! dT = M^(-1) dT
call dg_apply_mass(inv_mass, delta_T)
! T = T + dt/s * dT
call addto(T, delta_T, scale=-dt/subcycles)
call halo_update(T)
if (limit_slope) then
! Filter wiggles from T
call limit_slope_dg(T, U_nl, X, state, limiter)
end if
end do
if (include_diffusion.or.have_buoyancy_adjustment_by_vertical_diffusion) then
! Form RHS of diffusion equation.
call mult(delta_T, matrix_diff, T)
call addto(RHS_diff, delta_T,-1.0)
call scale(matrix_diff, theta*dt)
call addto(matrix_diff,mass)
call zero(delta_T) ! Impose zero initial guess.
! Solve for the change in T.
call petsc_solve(delta_T, matrix_diff, RHS_diff, state)
! Add the change in T to T.
call addto(T, delta_T, dt)
end if
call deallocate(delta_T)
call deallocate(matrix)
call deallocate(matrix_diff)
call deallocate(mass)
call deallocate(inv_mass)
call deallocate(mass_sparsity)
call deallocate(rhs)
call deallocate(rhs_diff)
end subroutine solve_advection_diffusion_dg_subcycle
subroutine construct_advection_diffusion_dg(big_m, rhs, field_name,&
& state, mass, diffusion_m, diffusion_rhs, semidiscrete, velocity_name)
!!< Construct the advection_diffusion equation for discontinuous elements in
!!< acceleration form.
!!<
!!< If mass is provided then the mass matrix is not added into big_m or
!!< rhs. It is instead returned as mass. This may be useful for testing
!!< or for solving equations otherwise than in acceleration form.
!!<
!!< If diffusion_m and diffusion_rhs are provided then the diffustion
!!< terms are placed here instead of in big_m and rhs
!!<
!!< If semidiscrete is present and true then the semidiscrete matrices
!!< are formed. This is accomplished by locally setting theta to 1.0
!!< and only inserting boundary conditions in the right hand side.
!!< Setting semidiscrete to 1 probably only makes sense if a separate
!!< mass matrix is also provided.
!! Main advection_diffusion matrix.
type(csr_matrix), intent(inout) :: big_m
!! Right hand side vector.
type(scalar_field), intent(inout) :: rhs
!! Name of the field to be advected.
character(len=*), intent(in) :: field_name
!! Collection of fields defining system state.
type(state_type), intent(inout) :: state
!! Optional separate mass matrix.
type(csr_matrix), intent(inout), optional :: mass
!! Optional separate diffusion matrix
type(csr_matrix), intent(inout), optional :: diffusion_m
!! Corresponding right hand side vector
type(scalar_field), intent(inout), optional :: diffusion_rhs
!! Optional velocity name
character(len = *), intent(in), optional :: velocity_name
!! Flag for whether to construct semidiscrete form of the equation.
logical, intent(in), optional :: semidiscrete
!! Position, and velocity fields.
type(vector_field) :: X, U, U_nl
type(vector_field), pointer :: X_new, X_old, U_mesh
!! Tracer to be solved for.
type(scalar_field) :: T
!! Diffusivity
type(tensor_field) :: Diffusivity
type(tensor_field) :: LESDiffusivity
!! Source and absorption
type(scalar_field) :: Source, Absorption
!! Local velocity name
character(len = FIELD_NAME_LEN) :: lvelocity_name
!! Element index
integer :: ele
!! Status variable for field extraction.
integer :: stat
!! Gravitational sinking term
type(scalar_field) :: Sink
!! Direction of gravity
type(vector_field) :: gravity
!! Backup of U_nl for calculating sinking
type(vector_field) :: U_nl_backup
!! Buoyancy and gravity
type(scalar_field) :: buoyancy
type(scalar_field) :: buoyancy_from_state
real :: gravity_magnitude
real :: mixing_diffusion_amplitude
!! Mesh for auxiliary variable
type(mesh_type), save :: q_mesh
!! Local diffusion matrices and right hand side
type(csr_matrix) :: big_m_diff
type(scalar_field) :: rhs_diff
!! Field over the entire surface mesh containing bc values:
type(scalar_field) :: bc_value
!! Integer array of all surface elements indicating bc type
!! (see below call to get_entire_boundary_condition):
integer, dimension(:), allocatable :: bc_type
type(mesh_type), pointer :: mesh_cg
!! Add the Source directly to the right hand side?
logical :: add_src_directly_to_rhs
type(integer_set), dimension(:), pointer :: colours
integer :: len, clr, nnid
#ifdef _OPENMP
!! Is the transform_to_physical cache we prepopulated valid
logical :: cache_valid
integer :: num_threads
#endif
!! Diffusivity to add due to the buoyancy adjustment by vertical mixing scheme
type(scalar_field) :: buoyancy_adjustment_diffusivity
ewrite(1,*) "Writing advection-diffusion equation for "&
&//trim(field_name)
! These names are based on the CGNS SIDS.
T=extract_scalar_field(state, field_name)
X=extract_vector_field(state, "Coordinate")
semi_discrete=present_and_true(semidiscrete)
! If a separate diffusion matrix has been provided, put diffusion in
! there. Otherwise put it in the RHS.
have_diffusion_m = present(diffusion_m)
if (present(diffusion_m)) then
big_m_diff=diffusion_m
else
big_m_diff=big_m
end if
if (present(diffusion_rhs)) then
if(.not.have_diffusion_m) then
FLAbort("diffusion_m required")
end if
rhs_diff=diffusion_rhs
else
rhs_diff=rhs
end if
if(present(velocity_name)) then
lvelocity_name = velocity_name
else
lvelocity_name = "NonlinearVelocity"
end if
on_sphere = have_option('/geometry/spherical_earth/')
if (.not.have_option(trim(T%option_path)//"/prognostic"//&
&"/spatial_discretisation/discontinuous_galerkin"//&
&"/advection_scheme/none")) then
U_nl_backup=extract_vector_field(state, lvelocity_name)
call incref(U_nl_backup)
include_advection=.true.
else
! Forcing a zero NonlinearVelocity will disable advection.
U=extract_vector_field(state, "Velocity", stat)
if (stat/=0) then
FLExit("Oh dear, no velocity field. A velocity field is required for advection!")
end if
call allocate(U_nl_backup, U%dim, U%mesh, "BackupNonlinearVelocity", &
FIELD_TYPE_CONSTANT)
call zero(U_nl_backup)
include_advection=.false.
end if
flux_scheme=UPWIND_FLUX
if (have_option(trim(T%option_path)//"/prognostic"//&
&"/spatial_discretisation/discontinuous_galerkin"//&
&"/advection_scheme/lax_friedrichs")) then
flux_scheme=LAX_FRIEDRICHS_FLUX
end if
call allocate(U_nl, U_nl_backup%dim, U_nl_backup%mesh, "LocalNonlinearVelocity")
call set(U_nl, U_nl_backup)
Diffusivity=extract_tensor_field(state, trim(field_name)//"Diffusivity"&
&, stat=stat)
if (stat/=0) then
call allocate(Diffusivity, T%mesh, trim(field_name)//"Diffusivity",&
FIELD_TYPE_CONSTANT)
call zero(Diffusivity)
include_diffusion=.false.
else
if (have_option(trim(T%option_path)//"/prognostic"//&
&"/subgridscale_parameterisation::LES")) then
! this routine takes Diffusivity as its background diffusivity
! and returns the sum of this and the les diffusivity
call construct_les_dg(state,T, X, Diffusivity, LESDiffusivity)
! the sum is what we want to apply:
Diffusivity = LESDiffusivity
else
! Grab an extra reference to cause the deallocate below to be safe.
call incref(Diffusivity)
end if
include_diffusion=.true.
end if
Source=extract_scalar_field(state, trim(field_name)//"Source"&
&, stat=stat)
if (stat/=0) then
call allocate(Source, T%mesh, trim(field_name)//"Source",&
FIELD_TYPE_CONSTANT)
call zero(Source)
add_src_directly_to_rhs = .false.
else
! Grab an extra reference to cause the deallocate below to be safe.
call incref(Source)
add_src_directly_to_rhs = have_option(trim(Source%option_path)//'/diagnostic/add_directly_to_rhs')
if (add_src_directly_to_rhs) then
ewrite(2, *) "Adding Source field directly to the right hand side"
assert(node_count(Source) == node_count(T))
end if
end if
Absorption=extract_scalar_field(state, trim(field_name)//"Absorption"&
&, stat=stat)
if (stat/=0) then
call allocate(Absorption, T%mesh, trim(field_name)//"Absorption",&
FIELD_TYPE_CONSTANT)
call zero(Absorption)
else
! Grab an extra reference to cause the deallocate below to be safe.
call incref(Absorption)
end if
Sink=extract_scalar_field(state, trim(field_name)//"SinkingVelocity"&
&, stat=stat)
if (stat==0) then
gravity=extract_vector_field(state, "GravityDirection")
! this may perform a "remap" internally from CoordinateMesh to VelocityMesh
call addto(U_nl, gravity, scale=Sink)
! Gravitational sinking only makes sense if you include advection
! terms.
include_advection=.true.
end if
! Retrieve scalar options from the options dictionary.
if (.not.semi_discrete) then
call get_option(trim(T%option_path)//&
&"/prognostic/temporal_discretisation/theta", theta)
call get_option("/timestepping/timestep", dt)
else
! If we are assembling the semi-discrete forms of the equations then
! we don't need to scale by theta and dt in this routine.
theta=1.0
dt=1.0
end if
include_mass = .not. have_option(trim(T%option_path)//&
"/prognostic/spatial_discretisation/discontinuous_galerkin/mass_terms/exclude_mass_terms")
move_mesh = (have_option("/mesh_adaptivity/mesh_movement").and.include_mass)
if(move_mesh) then
ewrite(2,*) 'Moving mesh'
X_old => extract_vector_field(state, "OldCoordinate")
X_new => extract_vector_field(state, "IteratedCoordinate")
U_mesh=> extract_vector_field(state, "GridVelocity")
assert(U_mesh%dim == mesh_dim(t))
assert(ele_count(U_mesh) == ele_count(t))
else
ewrite(2,*) 'Not moving mesh'
end if
! Switch on upwind stabilisation if requested.
if (have_option(trim(T%option_path)//"/prognostic/spatial_discretisation"&
&"/discontinuous_galerkin/upwind_stabilisation")) then
stabilisation_scheme=UPWIND
if(move_mesh) then
FLExit("Haven't thought about how mesh movement works with stabilisation yet.")
end if
else
stabilisation_scheme=NONE
end if
q_mesh=diffusivity%mesh
assert(has_faces(X%mesh))
assert(has_faces(T%mesh))
! Enquire about boundary conditions we're interested in
! Returns an integer array bc_type over the surface elements
! that indicates the bc type (in the order we specified, i.e.
! BCTYPE_WEAKDIRICHLET=1)
allocate( bc_type(1:surface_element_count(T)) )
call get_entire_boundary_condition(T, &
& (/"weakdirichlet", &
& "dirichlet ", &
& "neumann "/), &
& bc_value, bc_type)
call zero(big_m)
call zero(RHS)
if (present(mass)) call zero(mass)
if (present(diffusion_m)) call zero(diffusion_m)
if (present(diffusion_RHS)) call zero(diffusion_RHS)
if (have_buoyancy_adjustment_by_vertical_diffusion) then
ewrite(3,*) "Buoyancy adjustment by vertical mixing: enabled"
if (have_option(trim(T%option_path)//"/prognostic/buoyancy_adjustment"//&
&"/by_vertical_diffusion/project_buoyancy_to_continuous_space")) then
buoyancy_from_state = extract_scalar_field(state, "VelocityBuoyancyDensity", stat)
if (stat/=0) FLAbort('Error extracting buoyancy field.')
mesh_cg=>extract_mesh(state, "CoordinateMesh")
call allocate(buoyancy, mesh_cg, "BuoyancyProjectedToContinuousSpace")
call zero(buoyancy)
! Grab an extra reference to cause the deallocate below to be safe.
! Check this is OK
call lumped_mass_galerkin_projection_scalar(state, buoyancy, buoyancy_from_state)
ewrite(3,*) "Buoyancy adjustment by vertical mixing: projecting to continuous space"
else
ewrite(3,*) "Buoyancy adjustment by vertical mixing: no projection"
buoyancy = extract_scalar_field(state, "VelocityBuoyancyDensity", stat)
if (stat/=0) FLAbort('Error extracting buoyancy field.')
call incref(buoyancy)
end if
gravity=extract_vector_field(state, "GravityDirection",stat)
if (stat/=0) FLAbort('Error extracting gravity field.')
call get_option("/physical_parameters/gravity/magnitude", gravity_magnitude)
if (have_option(trim(T%option_path)//&
&"/prognostic/buoyancy_adjustment/by_vertical_diffusion/amplitude")) then
call get_option(trim(T%option_path)//&
&"/prognostic/buoyancy_adjustment/by_vertical_diffusion/amplitude", &
&mixing_diffusion_amplitude)
else
mixing_diffusion_amplitude = 1.0
end if
! Set direction of mixing diffusion, default is in the y- and z-direction for 2- and 3-d spaces respectively
! TODO: Align this direction with gravity local to an element
! Check if the diagnostic associated with the buoyancy adjustment by vertical mixing scheme is required
buoyancy_adjustment_diffusivity = extract_scalar_field(state, "BuoyancyAdjustmentDiffusivity", stat)
if (stat==0) then
have_buoyancy_adjustment_diffusivity = .true.
ewrite(3,*) "Buoynacy adjustment by vertical mixing: Updating BuoyancyAdjustmentDiffusivity field."
else
have_buoyancy_adjustment_diffusivity = .false.
end if
end if
if (include_diffusion) then
call get_mesh_colouring(state, T%mesh, COLOURING_DG2, colours)
#ifdef _OPENMP
if(diffusion_scheme == MASSLUMPED_RT0) then
call omp_set_num_threads(1)
ewrite(1,*) "WARNING: hybrid assembly can't support The MASSLUMPED_RT0 scheme yet, &