~skimura04/fluidity/fluidityDGSlope

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
!    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 arranty 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 gls
  use quadrature
  use elements
  use field_derivatives
  use fields
  use sparse_matrices_fields
  use state_module
  use spud
  use global_parameters, only:   OPTION_PATH_LEN
  use equation_of_state
  use state_fields_module
  use boundary_conditions
  use Coordinates
  use FLDebug
  use fefields

  implicit none

  private

  ! These variables are the parameters requried by GLS. 
  ! They are all private to prevent tampering
  ! and save'd so that we don't need to call init every time some GLS-y
  ! happens. These are *all* private
  real, save               :: gls_n, gls_m, gls_p
  real, save               :: sigma_psi, sigma_k, kappa
  integer, save            :: nNodes
  type(scalar_field), save :: tke_old, ll
  type(scalar_field), save :: local_tke ! our local copy of TKE. We amend this
  !to add the values of the Dirichlet BC onto the field for calculating the
  !diagnostic quantities and for output. See Warner et al 2005.
  type(scalar_field), save :: MM2, NN2, eps, Fwall, S_H, S_M
  type(scalar_field), save :: K_H, K_M, density, P, B
  real, save               :: eps_min = 1e-10, psi_min, k_min
  real, save               :: cm0, cde
  !  the a_i's for the ASM
  real, save               :: a1,a2,a3,a4,a5
  real, save               :: at1,at2,at3,at4,at5
  real, save               :: cc1
  real, save               :: ct1,ctt
  real, save               :: cc2,cc3,cc4,cc5,cc6
  real, save               :: ct2,ct3,ct4,ct5
  real, save               :: cPsi1,cPsi2,cPsi3,cPsi3_plus,cPsi3_minus
  real, save               :: relaxation


  ! these are the fields and variables for the surface values
  type(scalar_field), save             :: top_surface_values, bottom_surface_values ! these are used to populate the bcs
  type(scalar_field), save             :: top_surface_tke_values, bottom_surface_tke_values ! for the Psi BC
  type(scalar_field), save             :: top_surface_km_values, bottom_surface_km_values ! for the Psi BC
  integer, save                        :: NNodes_sur, NNodes_bot
  integer, dimension(:), pointer, save :: bottom_surface_nodes, top_surface_nodes
  integer, dimension(:), pointer, save :: top_surface_element_list, bottom_surface_element_list
  logical, save                        :: calculate_bcs, calc_fwall
  character(len=FIELD_NAME_LEN), save  :: gls_wall_option, gls_stability_option, gls_option

  ! Switch for on sphere simulations to rotate the required tensors
  logical, save :: on_sphere
  
  ! The following are the public subroutines
  public :: gls_init, gls_cleanup, gls_tke, gls_diffusivity, gls_psi, gls_adapt_mesh, gls_check_options

  ! General plan is:
  !  - Init in main/Fluids.F90
  !  - Populate_State and BoundaryConditionsFromOptions also contain some set up
  !  routines such as looking for the GLS fields and setting up the automatic
  !  boundary conditions
  !  - If solve is about to do TKE, call gls_tke (which calculates NN, MM and set source/absorption for solve)
  !  - If solve is about to do Psi, call gls_psi (which fixes TKE surfaces, set source/absorption for solve)
  !  - After Psi solve, call gls_diffusivity, which sets the diffusivity and viscosity, via the lengthscale
  !  - If we adapt, call gls_adapt, which deallocates and re-allocates the
  !  fields on the new mesh. This also sets up the diagnostic fields again,
  !  which aren't interpolated
  !  - When done, clean-up

contains

!----------
! gls_init does the following:
!    - check we have the right fields (if not abort)
!    - initialise GLS parameters based on options
!    - allocate space for optional fields, which are module level variables (to save passing them around)
!----------
subroutine gls_init(state)

    type(state_type), intent(inout) :: state

    real                           :: N,rad,rcm,cmsf
    integer                        :: stat
    type(scalar_field), pointer    :: psi, tke

    psi => extract_scalar_field(state, "GLSGenericSecondQuantity")
    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")

    ! Allocate the temporary, module-level variables
    call gls_allocate_temps(state)

    ! Check if we're on the sphere
    on_sphere = have_option('/geometry/spherical_earth/')
   
    ! populate some useful variables from options
    calculate_bcs = have_option("/material_phase[0]/subgridscale_parameterisations/GLS/calculate_boundaries/")
    calc_fwall = .false.
    call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                    &scalar_field::GLSTurbulentKineticEnergy/prognostic/minimum_value", k_min, stat)

    ! these lot are global
    call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/relax_diffusivity", relaxation, default=0.0)
    call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/stability_function", gls_stability_option)
    call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/option", gls_option)    
    ! there are lots of alternative formulae for this wall function, so let the
    ! user choose!
    if (have_option("/material_phase[0]/subgridscale_parameterisations/GLS/wall_function")) then
        call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/wall_function",gls_wall_option)
    else
        gls_wall_option = "none"
    end if

    ! Check the model used - we have four choices - then set the parameters appropriately
    select case (gls_option)
    case ("k-kl")  ! Mellor-Yamada 2.5
        gls_p = 0.0
        gls_m = 1.0
        gls_n = 1.0
        sigma_k = 2.44   ! turbinent Schmidt number
        sigma_psi = 2.44  ! turbulent Schmidt number
        cPsi1 = 0.9
        cPsi2 = 0.5
        cPsi3_plus = 1.0
        ! c3 depends on which stability function has been choosen
        select case (trim(gls_stability_option))
        case ("KanthaClayson-94")
            cPsi3_minus = 2.53
        case ("Canuto-01-A")
            cPsi3_minus = 2.681
        case ("Canuto-01-B")
            FLExit("GLS - Stability function combination not supported")
        case ("GibsonLaunder-78")
            FLExit("GLS - Stability function combination not supported")
        end select
        psi_min = 1.e-8
        calc_fwall = .true.
    case ("k-epsilon")
        gls_p = 3.0
        gls_m = 1.5
        gls_n = -1.0
        sigma_k = 1.3  ! turbulent Schmidt number
        sigma_psi = 1.0  ! turbulent Schmidt number
        cPsi1 = 1.44
        cPsi2 = 1.92
        cPsi3_plus = 1.0 
        select case (trim(gls_stability_option))
        case ("KanthaClayson-94")
            cPsi3_minus = -0.41
        case ("Canuto-01-A")
            cPsi3_minus = -0.63
        case ("Canuto-01-B")
            cPsi3_minus = -0.57
        case ("GibsonLaunder-78")
            cPsi3_minus = -0.3700
        end select
        psi_min = 1.e-12
    case ("k-omega")
        gls_p = -1.0
        gls_m = 0.5
        gls_n = -1.0
        sigma_k = 2.0  ! turbulent Schmidt number
        sigma_psi = 2.0  ! turbulent Schmidt number
        cPsi1 = 0.555
        cPsi2 = 0.833
        cPsi3_plus = 1.0 
        select case (trim(gls_stability_option))
        case ("KanthaClayson-94")
            cPsi3_minus = -0.58
        case ("Canuto-01-A")
            cPsi3_minus = -0.64
        case ("Canuto-01-B")
            cPsi3_minus = -0.61 
        case ("GibsonLaunder-78")
            cPsi3_minus = -0.4920
        end select
        psi_min = 1.e-12
    case ("gen")
        gls_p = 2.0
        gls_m = 1.0
        gls_n = -0.67
        sigma_k = 0.8  ! turbulent Schmidt number
        sigma_psi = 1.07  ! turbulent Schmidt number
        cPsi1 = 1.0
        cPsi2 = 1.22
        cPsi3_plus = 1.0 
        select case (trim(gls_stability_option))
        case ("KanthaClayson-94")
            cPsi3_minus = 0.1
        case ("Canuto-01-A")
            cPsi3_minus = 0.05
        case ("Canuto-01-B")
            cPsi3_minus = 0.08
        case ("GibsonLaunder-78")
            cPsi3_minus = 0.1704
        end select
        psi_min = 1.e-12
    case default
        FLAbort("Unknown gls_option")           
    end select

    select case (trim(gls_stability_option))
    case ("KanthaClayson-94")
         ! parameters for Kantha and Clayson (2004)
         cc1 =  6.0000
         cc2 =  0.3200
         cc3 =  0.0000
         cc4 =  0.0000
         cc5 =  0.0000
         cc6 =  0.0000
         ct1 =  3.7280
         ct2 =  0.7000
         ct3 =  0.7000
         ct4 =  0.0000
         ct5 =  0.2000
         ctt =  0.6102
    case("Canuto-01-B")
         cc1 =  5.0000
         cc2 =  0.6983
         cc3 =  1.9664
         cc4 =  1.0940
         cc5 =  0.0000
         cc6 =  0.4950
         ct1 =  5.6000
         ct2 =  0.6000
         ct3 =  1.0000
         ct4 =  0.0000
         ct5 =  0.3333
         ctt =  0.4770
    case("Canuto-01-A")
         cc1 =  5.0000
         cc2 =  0.8000
         cc3 =  1.9680
         cc4 =  1.1360
         cc5 =  0.0000
         cc6 =  0.4000
         ct1 =  5.9500
         ct2 =  0.6000
         ct3 =  1.0000
         ct4 =  0.0000
         ct5 =  0.3333
         ctt =  0.720     
     case("GibsonLaunder-78")
         cc1 =  3.6000
         cc2 =  0.8000
         cc3 =  1.2000
         cc4 =  1.2000
         cc5 =  0.0000
         cc6 =  0.5000
         ct1 =  3.0000
         ct2 =  0.3333
         ct3 =  0.3333
         ct4 =  0.0000
         ct5 =  0.3333
         ctt =  0.8000
    case default
        FLAbort("Unknown gls_stability_function") 
    end select

    ! compute the a_i's for the Algebraic Stress Model
    a1 =  2./3. - cc2/2.
    a2 =  1.    - cc3/2.
    a3 =  1.    - cc4/2.
    a4 =          cc5/2.
    a5 =  1./2. - cc6/2.

    at1 =          1. - ct2
    at2 =          1. - ct3
    at3 = 2. *   ( 1. - ct4)
    at4 = 2. *   ( 1. - ct5)
    at5 = 2.*ctt*( 1. - ct5)
    
    ! compute cm0
    N   =  cc1/2.
    cm0 =  ( (a2**2. - 3.*a3**2. + 3.*a1*N)/(3.* N**2.) )**0.25
    cmsf  =   a1/N/cm0**3
    rad=sigma_psi*(cPsi2-cPsi1)/(gls_n**2.)
    kappa = 0.41
    if (gls_option .ne. "k-kl") then    
        kappa=cm0*sqrt(rad)
    end if
    rcm  = cm0/cmsf
    cde = cm0**3.

    ewrite(1,*) "GLS Parameters"
    ewrite(1,*) "--------------------------------------------"
    ewrite(1,*) "cm0: ",cm0
    ewrite(1,*) "kappa: ",kappa
    ewrite(1,*) "p: ",gls_p
    ewrite(1,*) "m: ",gls_m
    ewrite(1,*) "n: ",gls_n
    ewrite(1,*) "sigma_k: ",sigma_k
    ewrite(1,*) "sigma_psi: ",sigma_psi
    ewrite(1,*) "Calculating BCs: ", calculate_bcs
    ewrite(1,*) "Using wall function: ", gls_wall_option
    ewrite(1,*) "Smoothing NN2: ", have_option('/material_phase[0]/subgridscale_parameterisations/GLS/smooth_buoyancy/')
    ewrite(1,*) "Smoothing MM2: ", have_option('/material_phase[0]/subgridscale_parameterisations/GLS/smooth_shear/')
    ewrite(1,*) "--------------------------------------------"

    ! intilise surface - only if we need to though
    if (calculate_bcs) then
        call gls_init_surfaces(state)
    end if

    call gls_init_diagnostics(state)
    
    call gls_calc_wall_function(state)

    ! we're all done!
end subroutine gls_init

!----------
! Update TKE
!----------
subroutine gls_tke(state)

    type(state_type), intent(inout)  :: state 
    
    type(scalar_field), pointer      :: source, absorption, scalarField
    type(tensor_field), pointer      :: tke_diff, background_diff
    type(scalar_field), pointer      :: tke
    real                             :: prod, buoyan, diss
    integer                          :: i, stat
    character(len=FIELD_NAME_LEN)    :: bc_type
    type(scalar_field), pointer      :: scalar_surface
    type(vector_field), pointer      :: positions

    ! Temporary tensor to hold  rotated values if on the sphere (note: must be a 3x3 mat)
    real, dimension(3,3) :: K_M_sphere_node

    ewrite(1,*) "In gls_tke"

    ! Get N^2 and M^2 -> NN2 and MM2
    call gls_buoyancy(state)

    ! calculate stability function
    call gls_stability_function(state)

    source => extract_scalar_field(state, "GLSTurbulentKineticEnergySource")
    absorption  => extract_scalar_field(state, "GLSTurbulentKineticEnergyAbsorption")
    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")
    tke_diff => extract_tensor_field(state, "GLSTurbulentKineticEnergyDiffusivity")
    positions => extract_vector_field(state, "Coordinate")

    ! swap state tke for our local one - the state tke has had the upper and
    ! lower boundaries ammended with the Dirichlet condition as in Warner et al
    ! 2005. The local TKE is the non-amended version. This ensures a
    ! non-ill-posed problem.
    call set(tke,local_tke)

    do i=1,nNodes
        prod = node_val(K_M,i)*node_val(MM2,i) 
        buoyan = -1.*node_val(K_H,i)*node_val(NN2,i)
        diss = node_val(eps,i)
        if (prod+buoyan.gt.0) then
            call set(source,i, prod + buoyan)
            call set(absorption,i, diss / node_val(tke,i))
        else
            call set(source,i, prod)
            call set(absorption,i, (diss-buoyan)/node_val(tke,i))
        end if
        call set(P, i, prod)
        call set(B, i, buoyan)
    enddo

    ! set diffusivity for tke
    call zero(tke_diff)
    background_diff => extract_tensor_field(state, "GLSBackgroundDiffusivity")
    if (on_sphere) then
      do i=1,nNodes
        K_M_sphere_node=align_with_radial(node_val(positions,i),node_val(K_M,i))
        K_M_sphere_node=K_M_sphere_node*1./sigma_k
        call set(tke_diff,i,K_M_sphere_node)
      end do
    else
      call set(tke_diff,tke_diff%dim(1),tke_diff%dim(2),K_M,scale=1./sigma_k)
    end if
    call addto(tke_diff,background_diff) 

    ! boundary conditions
    if (calculate_bcs) then
        ewrite(1,*) "Calculating BCs"
        call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/calculate_boundaries/", bc_type)
        call gls_tke_bc(state,bc_type)
        ! above puts the BC boundary values in top_surface_values and bottom_surface_values module level variables
        ! map these onto the actual BCs in tke
        scalar_surface => extract_surface_field(tke, 'tke_bottom_boundary', "value")
        call remap_field(bottom_surface_values, scalar_surface)
        scalar_surface => extract_surface_field(tke, 'tke_top_boundary', "value")
        call remap_field(top_surface_values, scalar_surface)
    end if
    
    ! finally, we need a copy of the old TKE for Psi, so grab it before we solve
    call set(tke_old,tke)

    ! that's the TKE set up ready for the solve which is the next thing to happen (see Fluids.F90)
    ewrite_minmax(source)
    ewrite_minmax(absorption)
    ! set source and absorption terms in optional output fields
    scalarField => extract_scalar_field(state, "GLSSource1", stat)
    if(stat == 0) then
       call set(scalarField,source)  
    end if
    scalarField => extract_scalar_field(state, "GLSAbsorption1", stat)
    if(stat == 0) then
       call set(scalarField,absorption)  
    end if


end subroutine gls_tke


!----------
! Calculate the second quantity
!----------
subroutine gls_psi(state)

    type(state_type), intent(inout)  :: state
    
    type(scalar_field), pointer      :: source, absorption, tke,  psi, scalarField
    type(tensor_field), pointer      :: psi_diff, background_diff
    real                             :: prod, buoyan,diss,PsiOverTke
    character(len=FIELD_NAME_LEN)    :: bc_type
    integer                          :: i, stat
    type(scalar_field), pointer      :: scalar_surface
    type(vector_field), pointer      :: positions

    ! Temporary tensor to hold  rotated values (note: must be a 3x3 mat)
    real, dimension(3,3)             :: psi_sphere_node
    real                             :: src, absn

    ewrite(1,*) "In gls_psi"

    source => extract_scalar_field(state, "GLSGenericSecondQuantitySource")
    absorption  => extract_scalar_field(state, "GLSGenericSecondQuantityAbsorption")
    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")
    psi => extract_scalar_field(state, "GLSGenericSecondQuantity")
    psi_diff => extract_tensor_field(state, "GLSGenericSecondQuantityDiffusivity")
    positions => extract_vector_field(state, "Coordinate")

    ewrite(2,*) "In gls_psi: setting up"

    ! store the tke in an internal field and then
    ! add the dirichlet conditions to the upper and lower surfaces. This
    ! helps stabilise the diffusivity (e.g. rapid heating cooling of the surface
    ! can destabilise the run) but also is then done for output so we match
    ! other models which also play this trick, c.f. Warner et al 2005.
    call set(local_tke,tke)
    ! Call the bc code, but specify we want dirichlet
    call gls_tke_bc(state, 'dirichlet')
    ! copy the values onto the mesh using the global node id
    do i=1,NNodes_sur
        call set(tke,top_surface_nodes(i),node_val(top_surface_values,i))
        call set(tke_old,top_surface_nodes(i),node_val(top_surface_values,i))   
    end do   
    do i=1,NNodes_bot
        call set(tke,bottom_surface_nodes(i),node_val(bottom_surface_values,i))
        call set(tke_old,bottom_surface_nodes(i),node_val(bottom_surface_values,i))
    end do   

    ! clip at k_min
    do i=1,nNodes
        call set(tke,i, max(node_val(tke,i),k_min))
        call set(tke_old,i, max(node_val(tke_old,i),k_min))
        call set(local_tke,i, max(node_val(local_tke,i),k_min))
    end do

    ! re-construct psi at "old" timestep
    do i=1,nNodes
        call set(psi,i, cm0**gls_p * node_val(tke_old,i)**gls_m * node_val(ll,i)**gls_n)
        call set(psi,i,max(node_val(psi,i),psi_min))
    end do

    ewrite(2,*) "In gls_psi: computing RHS"
    ! compute RHS
    do i=1,nNodes

        ! compute production terms in psi-equation
        if (node_val(B,i).gt.0) then ! note that we have already set B when setting up the RHS for TKE
            cPsi3=cPsi3_plus  ! unstable strat
        else
            cPsi3=cPsi3_minus ! stable strat
        end if

        ! compute production terms in psi-equation
        PsiOverTke  = node_val(psi,i)/node_val(tke_old,i)
        prod        = cPsi1*PsiOverTke*node_val(P,i)
        buoyan      = cPsi3*PsiOverTke*node_val(B,i)
        diss        = cPsi2*PsiOverTke*node_val(eps,i)*node_val(Fwall,i)
        if (prod+buoyan.gt.0) then
            src = prod+buoyan
            absn = diss/node_val(psi,i)
            call set(source,i, src)
            call set(absorption,i, absn)
        else
            src = prod
            absn = (diss-buoyan)/node_val(psi,i)
            call set(source,i, src)
            call set(absorption,i, absn)
        end if
    end do

    ewrite(2,*) "In gls_psi: setting diffusivity"
    ! Set diffusivity for Psi
    call zero(psi_diff)
    background_diff => extract_tensor_field(state, "GLSBackgroundDiffusivity")
    if (on_sphere) then
      do i=1,nNodes
        psi_sphere_node=align_with_radial(node_val(positions,i),node_val(K_M,i))
        psi_sphere_node=psi_sphere_node*1./sigma_psi
        call set(psi_diff,i,psi_sphere_node)
      end do
    else
      call set(psi_diff,psi_diff%dim(1),psi_diff%dim(2),K_M,scale=1./sigma_psi)
    end if
    call addto(psi_diff,background_diff) 

    ewrite(2,*) "In gls_psi: setting BCs"
    ! boundary conditions
    if (calculate_bcs) then
        call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/calculate_boundaries/", bc_type)
        call gls_psi_bc(state,bc_type)
        ! above puts the BC boundary values in top_surface_values and bottom_surface_values module level variables
        ! map these onto the actual BCs in Psi
        scalar_surface => extract_surface_field(psi, 'psi_bottom_boundary', "value")
        call remap_field(bottom_surface_values, scalar_surface)
        scalar_surface => extract_surface_field(psi, 'psi_top_boundary', "value")
        call remap_field(top_surface_values, scalar_surface)
    end if


    ewrite(2,*) "In gls_psi: tearing down"
    ! Psi is now ready for solving (see Fluids.F90)
    ewrite_minmax(source)
    ewrite_minmax(absorption)
    ! set source and absorption terms in optional output fields
    scalarField => extract_scalar_field(state, "GLSSource2", stat)
    if(stat == 0) then
       call set(scalarField,source)  
    end if
    scalarField => extract_scalar_field(state, "GLSAbsorption2", stat)
    if(stat == 0) then
       call set(scalarField,absorption)  
    end if

end subroutine gls_psi


!----------
! gls_diffusivity fixes the top/bottom boundaries of Psi
! then calulates the lengthscale, and then uses those to calculate the 
! diffusivity and viscosity
! These are placed in the GLS fields ready for other tracer fields to use
! Viscosity is placed in the velocity viscosity
!----------
subroutine gls_diffusivity(state)

    type(state_type), intent(inout)  :: state
    
    type(scalar_field), pointer      :: tke_state, psi
    type(tensor_field), pointer      :: eddy_diff_KH,eddy_visc_KM,viscosity,background_diff,background_visc
    real                             :: exp1, exp2, exp3, x
    integer                          :: i, stat
    real                             :: psi_limit, tke_cur, limit, epslim
    real, parameter                  :: galp = 0.748331 ! sqrt(0.56)
    type(vector_field), pointer      :: positions, velocity
    type(scalar_field)               :: remaped_K_M, tke
    type(tensor_field)               :: remaped_background_visc


    ! Temporary tensors to hold  rotated values (note: must be a 3x3 mat)
    real, dimension(3,3) :: eddy_diff_KH_sphere_node, eddy_visc_KM_sphere_node, viscosity_sphere_node

    ewrite(1,*) "In gls_diffusivity"

    tke_state => extract_scalar_field(state, "GLSTurbulentKineticEnergy")
    psi => extract_scalar_field(state, "GLSGenericSecondQuantity")
    eddy_visc_KM  => extract_tensor_field(state, "GLSEddyViscosityKM",stat)
    eddy_diff_KH => extract_tensor_field(state, "GLSEddyDiffusivityKH",stat)
    viscosity => extract_tensor_field(state, "Viscosity",stat)
    positions => extract_vector_field(state, "Coordinate")
    velocity => extract_vector_field(state, "Velocity")

    call allocate(tke, tke_state%mesh, name="MyLocalTKE")
    if (gls_n > 0) then
        ! set the TKE to use below to the unaltered TKE
        ! with no chnages tothe upper/lower surfaces
        ! Applies to k-kl model only
        call set (tke, local_tke)
    else
        ! Use the altered TKE to get the surface diffusivity correct
        call set (tke, tke_state)
    end if

    ! call the bc code, but specify we want dirichlet
    if (gls_n < 0) then
        call gls_psi_bc(state, 'dirichlet')
        ! copy the values onto the mesh using the global node id
        do i=1,NNodes_sur
            call set(psi,top_surface_nodes(i),node_val(top_surface_values,i))
        end do
        do i=1,NNodes_bot
            call set(psi,bottom_surface_nodes(i),node_val(bottom_surface_values,i))
        end do
    end if

    exp1 = 3.0 + gls_p/gls_n
    exp2 = 1.5 + gls_m/gls_n
    exp3 =       - 1.0/gls_n

    if (gls_n > 0) then
        do i=1,nNodes
            tke_cur = node_val(tke,i)
            psi_limit = (sqrt(0.56) * tke_cur**(exp2) * (1./sqrt(max(node_val(NN2,i)+1e-10,0.))) &
                        & * cm0**(gls_p / gls_n))**(-gls_n)
            call set(psi,i,max(psi_min,min(node_val(psi,i),psi_limit)))
        end do
    end if
   
    do i=1,nNodes

        tke_cur = node_val(tke,i)

        ! recover dissipation rate from k and psi
        call set(eps,i, cm0**exp1 * tke_cur**exp2 * node_val(psi,i)**exp3)

        ! limit dissipation rate under stable stratification,
        ! see Galperin et al. (1988)
        if (node_val(NN2,i) > 0) then
            epslim = (cde*tke_cur*sqrt(node_val(NN2,i)))/galp
        else
            epslim = eps_min
        end if
        call set(eps,i, max(node_val(eps,i),max(eps_min,epslim)))

        ! compute dissipative scale
        call set(ll,i,cde*sqrt(tke_cur**3.)/node_val(eps,i))
        if (gls_n > 0) then
            if (node_val(NN2,i) > 0) then
                limit = sqrt(0.56 * tke_cur / node_val(NN2,i))
                call set(ll,i,min(limit,node_val(ll,i)))
            end if
        end if

    end do

    ! calc fwall
    ewrite(2,*) "Calculating the wall function for GLS"
    call gls_calc_wall_function(state)

    ! calculate diffusivities for next step and for use in other fields
    do i=1,nNodes
        x = sqrt(node_val(tke,i))*node_val(ll,i)
        ! momentum
        call set(K_M,i, relaxation*node_val(K_M,i) + (1-relaxation)*node_val(S_M,i)*x)
        ! tracer
        call set(K_H,i, relaxation*node_val(K_H,i) + (1-relaxation)*node_val(S_H,i)*x)
    end do

    ! put KM onto surface fields for Psi_bc
    if (calculate_bcs) then
        call remap_field_to_surface(K_M, top_surface_km_values, top_surface_element_list)
        call remap_field_to_surface(K_M, bottom_surface_km_values, bottom_surface_element_list)
    end if

    !set the eddy_diffusivity and viscosity tensors for use by other fields
    call zero(eddy_diff_KH) ! zero it first as we're using an addto below
    call zero(eddy_visc_KM)

    if (on_sphere) then
      do i=1,nNodes
        eddy_diff_KH_sphere_node=align_with_radial(node_val(positions,i),node_val(K_H,i))
        eddy_visc_KM_sphere_node=align_with_radial(node_val(positions,i),node_val(K_M,i))
        call set(eddy_diff_KH,i,eddy_diff_KH_sphere_node)
        call set(eddy_visc_KM,i,eddy_visc_KM_sphere_node)
      end do
    else
      call set(eddy_diff_KH,eddy_diff_KH%dim(1),eddy_diff_KH%dim(2),K_H)
      call set(eddy_visc_KM,eddy_visc_KM%dim(1),eddy_visc_KM%dim(2),K_M)
    end if

    background_diff => extract_tensor_field(state, "GLSBackgroundDiffusivity")
    call addto(eddy_diff_KH,background_diff)
    background_visc => extract_tensor_field(state, "GLSBackgroundViscosity")
    call addto(eddy_visc_KM,background_visc)

    ewrite_minmax(K_H)
    ewrite_minmax(K_M)
    ewrite_minmax(S_H)
    ewrite_minmax(S_M)
    ewrite_minmax(ll)
    ewrite_minmax(eps)
    ewrite_minmax(tke)
    ewrite_minmax(psi) 

    ! Set viscosity
    call allocate(remaped_K_M,velocity%mesh,name="remaped_Km")
    call allocate(remaped_background_visc,velocity%mesh,name="remaped_viscosity")
    if (K_M%mesh%continuity /= viscosity%mesh%continuity) then
        ! remap
        call remap_field(K_M,remaped_K_M)
        call remap_field(background_visc,remaped_background_visc)
    else
        ! copy
        call set(remaped_K_M,K_M)
        call set(remaped_background_visc,background_visc)
    end if
    call zero(viscosity)
    if (on_sphere) then
      do i=1,nNodes
        viscosity_sphere_node=align_with_radial(node_val(positions,i),node_val(remaped_K_M,i))
        call set(viscosity,i,viscosity_sphere_node)
      end do
    else
      call set(viscosity,viscosity%dim(1),viscosity%dim(2),remaped_K_M)
    end if
    call addto(viscosity,remaped_background_visc)
    
    ! Set output on optional fields - if the field exists, stick something in it
    ! We only need to do this to those fields that we haven't pulled from state, but
    ! allocated ourselves
    call gls_output_fields(state)

    call deallocate(remaped_background_visc)
    call deallocate(remaped_K_M) 
    call deallocate(tke)

end subroutine gls_diffusivity

!----------
! gls_cleanup does...have a guess...go on.
!----------
subroutine gls_cleanup()

    ewrite(1,*) "Cleaning up GLS variables"
    ! deallocate all our variables
    if (calculate_bcs) then
        ewrite(1,*) "Cleaning up GLS surface variables"
        call deallocate(bottom_surface_values)
        call deallocate(bottom_surface_tke_values) 
        call deallocate(bottom_surface_km_values)
        call deallocate(top_surface_values)
        call deallocate(top_surface_tke_values)
        call deallocate(top_surface_km_values)
    end if
    call deallocate(NN2)
    call deallocate(MM2)  
    call deallocate(B)
    call deallocate(P)  
    call deallocate(S_H)    
    call deallocate(S_M)    
    call deallocate(K_H)    
    call deallocate(K_M)        
    call deallocate(eps) 
    call deallocate(Fwall) 
    call deallocate(density)
    call deallocate(tke_old)
    call deallocate(local_tke)
    call deallocate(ll)
    ewrite(1,*) "Finished gls_cleanup"

end subroutine gls_cleanup

!---------
! Needs to be called after an adapt to reset the fields
! and arrays within the module
! Note that clean_up has already been called in the pre-adapt hook
!----------
subroutine gls_adapt_mesh(state)

    type(state_type), intent(inout) :: state

    ewrite(1,*) "In gls_adapt_mesh"
    call gls_allocate_temps(state) ! reallocate everything
    if (calculate_bcs) then
        call gls_init_surfaces(state) ! re-do the boundaries
    end if
    call gls_init_diagnostics(state)

end subroutine gls_adapt_mesh


subroutine gls_check_options
    
    character(len=FIELD_NAME_LEN) :: buffer
    integer                       :: stat
    real                          :: min_tke, relax, nbcs
    integer                       :: dimension

    ! Don't do GLS if it's not included in the model!
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/")) return

    ! one dimensional problems not supported
    call get_option("/geometry/dimension/", dimension) 
    if (dimension .eq. 1 .and. have_option("/material_phase[0]/subgridscale_parameterisations/GLS/")) then
        FLExit("GLS modelling is only supported for dimension > 1")
    end if

    call get_option("/problem_type", buffer)
    if (.not. (buffer .eq. "oceans" .or. buffer .eq. "large_scale_ocean_options")) then
        FLExit("GLS modelling is only supported for problem type oceans or large_scale_oceans.")
    end if

    if (.not.have_option("/physical_parameters/gravity")) then
        ewrite(-1, *) "GLS modelling requires gravity" 
        FLExit("(otherwise buoyancy is a bit meaningless)")
    end if

    ! checking for required fields
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy")) then
        FLExit("You need GLSTurbulentKineticEnergy field for GLS")
    end if
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity")) then
        FLExit("You need GLSGenericSecondQuantity field for GLS")
    end if

    ! check that the diffusivity is on for the two turbulent fields and is
    ! diagnostic
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy/prognostic/&
                          &tensor_field::Diffusivity")) then
        FLExit("You need GLSTurbulentKineticEnergy Diffusivity field for GLS")
    end if    
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy/prognostic/&
                          &tensor_field::Diffusivity/diagnostic/algorithm::Internal")) then
        FLExit("You need GLSTurbulentKineticEnergy Diffusivity field set to diagnostic/internal")
    end if
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity/prognostic/&
                          &tensor_field::Diffusivity")) then
        FLExit("You need GLSGenericSecondQuantity Diffusivity field for GLS")
    end if
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity/prognostic/&
                          &tensor_field::Diffusivity/diagnostic/algorithm::Internal")) then
        FLExit("You need GLSGenericSecondQuantity Diffusivity field set to diagnostic/internal")
    end if


    ! source and absorption terms...
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy/prognostic/&
                          &scalar_field::Source")) then
        FLExit("You need GLSTurbulentKineticEnergy Source field for GLS")
    end if 
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy/prognostic/&
                          &scalar_field::Source/diagnostic/algorithm::Internal")) then
        FLExit("You need GLSTurbulentKineticEnergy Source field set to diagnostic/internal")
    end if 

    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity/prognostic/&
                          &scalar_field::Source")) then
        FLExit("You need GLSGenericSecondQuantity Source field for GLS")
    end if      
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity/prognostic/&
                          &scalar_field::Source/diagnostic/algorithm::Internal")) then
        FLExit("You need GLSGenericSecondQuantity Source field set to diagnostic/internal")
    end if   

    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy/prognostic/&
                          &scalar_field::Absorption")) then
        FLExit("You need GLSTurbulentKineticEnergy Absorption field for GLS")
    end if    
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSTurbulentKineticEnergy/prognostic/&
                          &scalar_field::Absorption/diagnostic/algorithm::Internal")) then
        FLExit("You need GLSTurbulentKineticEnergy Source field set to diagnostic/internal")
    end if 
    
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity/prognostic/&
                          &scalar_field::Absorption")) then
        FLExit("You need GLSGenericSecondQuantity Absorption field for GLS")
    end if
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &scalar_field::GLSGenericSecondQuantity/prognostic/&
                          &scalar_field::Absorption/diagnostic/algorithm::Internal")) then
        FLExit("You need GLSGenericSecondQuantity Source field set to diagnostic/internal")
    end if 


    ! background diffusivities are also needed
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &tensor_field::GLSBackgroundDiffusivity/prescribed")) then
        FLExit("You need GLSBackgroundDiffusivity tensor field for GLS")
    end if
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &tensor_field::GLSBackgroundViscosity/prescribed")) then
        FLExit("You need GLSBackgroundViscosity tensor field for GLS")
    end if

    ! check for some purturbation density and velocity
    if (.not.have_option("/material_phase[0]/scalar_field::PerturbationDensity")) then
        FLExit("You need PerturbationDensity field for GLS")
    end if
    if (.not.have_option("/material_phase[0]/vector_field::Velocity")) then
        FLExit("You need Velocity field for GLS")
    end if

    ! these two fields allow the new diffusivities/viscosities to be used in
    ! other calculations - we need them!
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &tensor_field::GLSEddyViscosityKM")) then
        FLExit("You need GLSEddyViscosityKM field for GLS")
    end if
    if (.not.have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                          &tensor_field::GLSEddyViscosityKM")) then
        FLExit("You need GLSEddyViscosityKM field for GLS")
    end if

    
    ! check there's a viscosity somewhere
    if (.not.have_option("/material_phase[0]/vector_field::Velocity/prognostic/&
                          &tensor_field::Viscosity/")) then
        FLExit("Need viscosity switched on under the Velcotiy field for GLS.") 
    end if
    ! check that the user has switch Velocity/viscosity to diagnostic
    if (.not.have_option("/material_phase[0]/vector_field::Velocity/prognostic/&
                          &tensor_field::Viscosity/diagnostic/")) then
        FLExit("You need to switch the viscosity field under Velocity to diagnostic/internal")
    end if

  
    ! check a minimum value of TKE has been set
    call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                    &scalar_field::GLSTurbulentKineticEnergy/prognostic/minimum_value", min_tke, stat)
    if (stat/=0) then
        FLExit("You need to set a minimum TKE value - recommend a value of around 1e-6")
    end if

    ! check if priorities have been set - if so warn the user this might screw
    ! things up
    if (have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                     &scalar_field::GLSTurbulentKineticEnergy/prognostic/priority")) then
        ewrite(-1,*)("WARNING: Priorities for the GLS fields are set internally. Setting them in the FLML might mess things up")
    end if
    if (have_option("/material_phase[0]/subgridscale_parameterisations/GLS/&
                     &scalar_field::GLSGenericSecondQuantity/prognostic/priority")) then
        ewrite(-1,*)("WARNING: Priorities for the GLS fields are set internally. Setting them in the FLML might mess things up")
    end if

    ! check the relax option is valid
    if (have_option("/material_phase[0]/subgridscale_parameterisations/GLS/relax_diffusivity")) then
        call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/relax_diffusivity", relax)
        if (relax < 0 .or. relax >= 1.0) then
            FLExit("The GLS diffusivity relaxation value should be greater than or equal to zero, but less than 1.0")
        end if
        if (.not. have_option("/material_phase[0]/subgridscale_parameterisations/GLS/scalar_field::GLSVerticalViscosity/")) then
            FLExit("You will need to switch on the GLSVerticalViscosity field when using relaxation")
        end if
        if (.not. have_option("/material_phase[0]/subgridscale_parameterisations/GLS/scalar_field::GLSVerticalDiffusivity/")) then
            FLExit("You will need to switch on the GLSVerticalDiffusivity field when using relaxation")
        end if
    end if
   
    ! Check that the we don't have auto boundaries and user-defined boundaries
    if (have_option("/material_phase[0]/subgridscale_parameterisations/GLS/calculate_boundaries")) then
        nbcs=option_count(trim("/material_phase[0]/subgridscale_parameterisations/GLS/scalar_field::GLSTurbulentKineticEnergy/prognostic/boundary_conditions"))
        if (nbcs > 0) then
            FLExit("You have automatic boundary conditions on, but some boundary conditions on the GLS TKE field. Not allowed")
        end if
        nbcs=option_count(trim("/material_phase[0]/subgridscale_parameterisations/GLS/scalar_field::GLSGenericSecondQuantity/prognostic/boundary_conditions"))
        if (nbcs > 0) then
            FLExit("You have automatic boundary conditions on, but some boundary conditions on the GLS Psi field. Not allowed")
        end if
    end if

    ! If the user has selected k-kl we need the ocean surface and bottom fields
    ! on in ocean_boundaries
    call get_option("/material_phase[0]/subgridscale_parameterisations/GLS/option", buffer)
    if (trim(buffer) .eq. "k-kl") then
        if (.not. have_option("/geometry/ocean_boundaries")) then 
            FLExit("If you use the k-kl option under GLS, you need to switch on ocean_boundaries under /geometry/ocean_boundaries")
       end if
    end if

  end subroutine gls_check_options

!------------------------------------------------------------------!
!------------------------------------------------------------------!
!                                                                  !
!                       Private subroutines                        !
!                                                                  !
!------------------------------------------------------------------!
!------------------------------------------------------------------!

!---------
! Initilise the surface meshes used for the BCS
! Called at startup and after an adapt
!----------
subroutine gls_init_surfaces(state)
    type(state_type), intent(in)     :: state  

    type(scalar_field), pointer      :: tke
    type(vector_field), pointer      :: position
    type(mesh_type), pointer         :: ocean_mesh
    type(mesh_type)                  :: meshy
  
    ewrite(1,*) "Initialising the GLS surfaces required for BCs"

    ! grab hold of some essential field
    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")
    position => extract_vector_field(state, "Coordinate")

    ! create a surface mesh to place values onto. This is for the top surface
    call get_boundary_condition(tke, 'tke_top_boundary', surface_mesh=ocean_mesh, &
        surface_element_list=top_surface_element_list)
    NNodes_sur = node_count(ocean_mesh) 
    call allocate(top_surface_values, ocean_mesh, name="top_surface")
    call allocate(top_surface_tke_values,ocean_mesh, name="surface_tke")
    call allocate(top_surface_km_values,ocean_mesh, name="surface_km")
    ! Creating a surface mesh gives a mapping between to global node number
    call create_surface_mesh(meshy, top_surface_nodes, tke%mesh, &
                                     top_surface_element_list, 'OceanTop')
    call deallocate(meshy)

    ! bottom
    call get_boundary_condition(tke, 'tke_bottom_boundary', surface_mesh=ocean_mesh, &
        surface_element_list=bottom_surface_element_list)
    NNodes_bot = node_count(ocean_mesh) 
    call allocate(bottom_surface_values, ocean_mesh, name="bottom_surface")
    call allocate(bottom_surface_tke_values,ocean_mesh, name="bottom_tke")
    call allocate(bottom_surface_km_values,ocean_mesh, name="bottom_km")
    call create_surface_mesh(meshy, bottom_surface_nodes, tke%mesh, &
                                     bottom_surface_element_list, 'OceanBottom')
    call deallocate(meshy)

end subroutine gls_init_surfaces

!----------------------
! Initialise the diagnostic fields, such as diffusivity, length
! scale, etc. This is called during initialisation and after an
! adapt
!----------------------
subroutine gls_init_diagnostics(state)
    type(state_type), intent(inout)     :: state

    type(scalar_field), pointer :: tke

    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")

    ! put tke onto surface fields if we need to
    if (calculate_bcs) then
        call remap_field_to_surface(tke, top_surface_tke_values, top_surface_element_list)
        call remap_field_to_surface(tke, bottom_surface_tke_values, bottom_surface_element_list)
    end if

    call set(tke_old,tke)
    call set(FWall,1.0)

    ! bit complicated here - we need to repopulate the fields internal to this
    ! module, post adapt or at initialisation. We need the diffusivity for the first iteration to
    ! calculate the TKE src/abs terms, but for diffusivity, we need stability
    ! functions, for those we need epsilon, which is calculated in the
    ! diffusivity subroutine, but first we need the buoyancy freq.
    ! So, working backwards...
    call gls_buoyancy(state) ! buoyancy for epsilon calculation
    call gls_diffusivity(state) ! gets us epsilon, but K_H and K_M are wrong
    call gls_stability_function(state) ! requires espilon, but sets S_H and S_M
    call gls_diffusivity(state) ! sets K_H, K_M to correct values
    ! and this one sets up the diagnostic fields for output
    call gls_output_fields(state)

end subroutine gls_init_diagnostics

!----------
! Calculate the buoyancy frequency and shear velocities
!----------
subroutine gls_buoyancy(state)

    type(state_type), intent(inout)       :: state

    type(scalar_field), pointer           :: pert_rho
    type(vector_field), pointer           :: positions, gravity
    type(vector_field), pointer           :: velocity
    type(scalar_field)                    :: NU, NV, MM2_av, NN2_av, inverse_lumpedmass
    type(scalar_field), pointer           :: lumpedmass
    real                                  :: g
    logical                               :: on_sphere, smooth_buoyancy, smooth_shear
    integer                               :: ele, i, dim
    type(csr_matrix), pointer             :: mass

    ! grab variables required from state - already checked in init, so no need to check here
    positions => extract_vector_field(state, "Coordinate")
    velocity => extract_vector_field(state, "Velocity")
    pert_rho => extract_scalar_field(state, "PerturbationDensity")
    gravity => extract_vector_field(state, "GravityDirection")

    ! now allocate our temp fields
    call allocate(NU, velocity%mesh, "NU")    
    call allocate(NV, velocity%mesh, "NV")
    call set(NU, extract_scalar_field(velocity, 1))
    call set(NV, extract_scalar_field(velocity, 2))

    call get_option("/physical_parameters/gravity/magnitude", g)
    on_sphere = have_option('/geometry/spherical_earth/')
    smooth_buoyancy = have_option('/material_phase[0]/subgridscale_parameterisations/GLS/smooth_buoyancy/')
    smooth_shear = have_option('/material_phase[0]/subgridscale_parameterisations/GLS/smooth_shear/')
    dim = mesh_dim(NN2)
    
    call zero(NN2)
    call zero(MM2)
    element_loop: do ele=1, element_count(velocity)
        call assemble_elements(ele,MM2,NN2,velocity,pert_rho,NU,NV,on_sphere,dim)
    end do element_loop
  
    ! Solve
    lumpedmass => get_lumped_mass(state, NN2%mesh)
    NN2%val = NN2%val / lumpedmass%val
    lumpedmass => get_lumped_mass(state, MM2%mesh)
    MM2%val = MM2%val / lumpedmass%val

    if (smooth_shear) then
        call allocate(MM2_av, MM2%mesh, "MM2_averaged")
        call allocate(inverse_lumpedmass, MM2%mesh, "InverseLumpedMass")
        mass => get_mass_matrix(state, MM2%mesh)
        lumpedmass => get_lumped_mass(state, MM2%mesh)
        call invert(lumpedmass, inverse_lumpedmass)
        call mult( MM2_av, mass, MM2) 
        call scale(MM2_av, inverse_lumpedmass) ! so the averaging operator is [inv(ML)*M*]
        call set(MM2, MM2_av)
        call deallocate(inverse_lumpedmass)
        call deallocate(MM2_av)
    end if

    if (smooth_buoyancy) then
        call allocate(NN2_av, NN2%mesh, "NN2_averaged")
        call allocate(inverse_lumpedmass, NN2%mesh, "InverseLumpedMass")
        mass => get_mass_matrix(state, NN2%mesh)
        lumpedmass => get_lumped_mass(state, NN2%mesh)
        call invert(lumpedmass, inverse_lumpedmass)
        call mult( NN2_av, mass, NN2) 
        call scale(NN2_av, inverse_lumpedmass) ! so the averaging operator is [inv(ML)*M*]
        call set(NN2, NN2_av)
        call deallocate(NN2_av)
        call deallocate(inverse_lumpedmass)
    end if

    call deallocate(NU)
    call deallocate(NV)

    contains 
        subroutine assemble_elements(ele,MM2,NN2,velocity,rho,NU,NV,on_sphere,dim)

            type(vector_field), intent(in), pointer    :: velocity
            type(scalar_field), intent(in)             :: rho
            type(scalar_field), intent(inout)          :: NN2, MM2
            type(scalar_field), intent(in)             :: NU, NV
            logical, intent(in)                        :: on_sphere
            integer, intent(in)                        :: ele, dim

            type(element_type), pointer                :: NN2_shape, MM2_shape
            real, dimension(ele_ngi(velocity,ele))     :: detwei, shear, drho_dz
            real, dimension(dim, ele_ngi(velocity,ele)) :: grad_theta_gi, du_dz
            real, dimension(dim,ele_ngi(velocity,ele)) :: grav_at_quads
            type(element_type), pointer                :: theta_shape, velocity_shape
            integer, dimension(:), pointer             :: element_nodes
            real, dimension(ele_loc(velocity,ele),ele_ngi(velocity,ele),dim)  :: dn_t
            real, dimension(ele_loc(rho,ele),ele_ngi(rho,ele),dim)            :: dtheta_t
            real, dimension(ele_loc(velocity, ele),ele_ngi(velocity, ele),dim):: du_t
    
            NN2_shape => ele_shape(NN2, ele)
            MM2_shape => ele_shape(MM2, ele)
            velocity_shape => ele_shape(velocity, ele)
            theta_shape => ele_shape(rho, ele)

            call transform_to_physical(positions, ele, NN2_shape, &
              & dshape = dn_t, detwei = detwei)
            if(NN2_shape == velocity_shape) then
              du_t = dn_t
            else
              call transform_to_physical(positions, ele, velocity_shape, dshape = du_t)
            end if
            if(theta_shape == velocity_shape) then
              dtheta_t = dn_t
            else
              call transform_to_physical(positions, ele, theta_shape, dshape = dtheta_t)
            end if
          
            if (on_sphere) then
                grav_at_quads=sphere_inward_normal_at_quad_ele(positions, ele)
            else
                grav_at_quads=ele_val_at_quad(gravity, ele)
            end if
            grad_theta_gi=ele_grad_at_quad(rho, ele, dtheta_t)
            do i=1,ele_ngi(velocity,ele)
                drho_dz(i)=dot_product(grad_theta_gi(:,i),grav_at_quads(:,i)) ! Divide this by rho_0 for non-Boussinesq?
            end do
            grad_theta_gi=ele_grad_at_quad(NU, ele, dtheta_t)
            do i=1,ele_ngi(velocity,ele)
                du_dz(1,i)=dot_product(grad_theta_gi(:,i),grav_at_quads(:,i)) ! Divide this by rho_0 for non-Boussinesq?
            end do        
            grad_theta_gi=ele_grad_at_quad(NV, ele, dtheta_t)
            do i=1,ele_ngi(velocity,ele)
                du_dz(2,i)=dot_product(grad_theta_gi(:,i),grav_at_quads(:,i)) ! Divide this by rho_0 for non-Boussinesq?
            end do
            shear = 0.0
            do i = 1, dim - 1
              shear = shear + du_dz(i,:) ** 2
            end do
              
            element_nodes => ele_nodes(NN2, ele)
            
            call addto(NN2, element_nodes, &
              ! already in the right direction due to multipling by grav_at_quads
              & shape_rhs(NN2_shape, detwei * g * drho_dz) &
              & )

            call addto(MM2, element_nodes, &
              & shape_rhs(MM2_shape,detwei * shear) &
              & )

        end subroutine assemble_elements
end subroutine gls_buoyancy

!----------
! Stability function based on Caunto et al 2001
!----------
subroutine gls_stability_function(state)

    type(state_type), intent(in)     :: state   
 
    integer                          :: i
    real                             :: N,Nt,an,anMin,anMinNum,anMinDen
    real, parameter                  :: anLimitFact = 0.5
    real                             :: d0,d1,d2,d3,d4,d5
    real                             :: n0,n1,n2,nt0,nt1,nt2
    real                             :: dCm,nCm,nCmp,cm3_inv
    real                             :: tmp0,tmp1,tmp2,tau2,as
    type(scalar_field), pointer      :: KK

    ewrite(1,*) "Calculating GLS stability functions"

    ! grab stuff from state
    KK => extract_scalar_field(state, 'GLSTurbulentKineticEnergy')
    
    ! This is written out verbatim as in GOTM v4.3.1 (also GNU GPL)
    N    =   0.5*cc1
    Nt   =   ct1
    d0   =   36.* N**3. * Nt**2.
    d1   =   84.*a5*at3 * N**2. * Nt  + 36.*at5 * N**3. * Nt
    d2   =   9.*(at2**2.-at1**2.) * N**3. - 12.*(a2**2.-3.*a3**2.) * N * Nt**2.
    d3   =   12.*a5*at3*(a2*at1-3.*a3*at2) * N + 12.*a5*at3*(a3**2.-a2**2.) * Nt       &
           + 12.*at5*(3.*a3**2.-a2**2.) * N * Nt
    d4   =   48.*a5**2.*at3**2. * N + 36.*a5*at3*at5 * N**2.
    d5   =   3.*(a2**2.-3.*a3**2.)*(at1**2.-at2**2.) * N
    n0   =   36.*a1 * N**2. * Nt**2.
    n1   = - 12.*a5*at3*(at1+at2) * N**2. + 8.*a5*at3*(6.*a1-a2-3.*a3) * N * Nt        &
           + 36.*a1*at5 * N**2. * Nt
    n2   =   9.*a1*(at2**2.-at1**2.) * N**2.
    nt0  =   12.*at3 * N**3. * Nt
    nt1  =   12.*a5*at3**2.  * N**2.
    nt2  =   9.*a1*at3*(at1-at2) * N**2. + (  6.*a1*(a2-3.*a3)                         &
           - 4.*(a2**2.-3.*a3**2.) )*at3 * N * Nt
    cm3_inv = 1./cm0**3


    ! mininum value of "an" to insure that "as" > 0 in equilibrium
    anMinNum  = -(d1 + nt0) + sqrt((d1+nt0)**2. - 4.*d0*(d4+nt1))
    anMinDen  = 2.*(d4+nt1)
    anMin     = anMinNum / anMinDen

    if (abs(n2-d5) .lt. 1e-7) then
        ! (special treatment to  avoid a singularity)
        do i=1,nNodes
            tau2   = node_val(KK,i)*node_val(KK,i) / ( node_val(eps,i)*node_val(eps,i) )
            an = tau2 * node_val(NN2,i)
            ! clip an at minimum value
            an = max(an,anLimitFact*anMin)
            ! compute the equilibrium value of as
            tmp0  = -d0 - (d1 + nt0)*an - (d4 + nt1)*an*an
            tmp1  = -d2 + n0 +  (n1-d3-nt2)*an

            as = -tmp0 / tmp1

            ! compute stability function
            dCm  = d0  +  d1*an +  d2*as + d3*an*as + d4*an*an + d5*as*as
            nCm  = n0  +  n1*an +  n2*as
            nCmp = nt0 + nt1*an + nt2*as
            call set(S_M,i, cm3_inv*nCm /dCm)
            call set(S_H,i, cm3_inv*nCmp/dCm)

        end do

     else
        do i=1,nNodes

            tau2 = node_val(KK,i)*node_val(KK,i) / ( node_val(eps,i)*node_val(eps,i) )
            an = tau2 * node_val(NN2,i)
            ! clip an at minimum value
            an = max(an,anLimitFact*anMin)

            ! compute the equilibrium value of as
            tmp0  = -d0 - (d1 + nt0)*an - (d4 + nt1)*an*an
            tmp1  = -d2 + n0 + (n1-d3-nt2)*an
            tmp2  =  n2-d5
            as =  (-tmp1 + sqrt(tmp1*tmp1-4.*tmp0*tmp2) ) / (2.*tmp2)

            ! compute stability function
            dCm  = d0  +  d1*an +  d2*as + d3*an*as + d4*an*an + d5*as*as
            nCm  = n0  +  n1*an +  n2*as
            nCmp = nt0 + nt1*an + nt2*as
            call set(S_M,i, cm3_inv*nCm /dCm)
            call set(S_H,i, cm3_inv*nCmp/dCm)
        end do

    endif

end subroutine gls_stability_function


!----------
! gls_tke_bc calculates the boundary conditions on the TKE (tke) field
! Boundary can be either Dirichlet or Neumann.
!----------
subroutine gls_tke_bc(state, bc_type)

    type(state_type), intent(in)     :: state  
    character(len=*), intent(in)     :: bc_type

    type(vector_field), pointer      :: positions 
    real                             :: gravity_magnitude
    integer                          :: i
    real, allocatable, dimension(:)  :: z0s, z0b, u_taus_squared, u_taub_squared
 
    ! grab hold of some essential field
    call get_option("/physical_parameters/gravity/magnitude", gravity_magnitude)
    positions => extract_vector_field(state, "Coordinate") 

    ! Top boundary condition
    select case(bc_type)
    case("neumann")
        ! Top TKE flux BC
        call set(top_surface_values,0.0)
        call set(bottom_surface_values,0.0)
    case("dirichlet") 
        allocate(z0s(NNodes_sur))
        allocate(z0b(NNodes_bot))
        allocate(u_taus_squared(NNodes_sur))
        allocate(u_taub_squared(NNodes_bot))
        call gls_friction(state,z0s,z0b,gravity_magnitude,u_taus_squared,u_taub_squared)

        ! Top TKE value set
        do i=1,NNodes_sur
            call set(top_surface_values,i,max(u_taus_squared(i)/(cm0**2),k_min))
        end do 
        do i=1,NNodes_bot
            call set(bottom_surface_values,i,max(u_taub_squared(i)/(cm0**2),k_min))
        end do   
        deallocate(z0s)
        deallocate(z0b)
        deallocate(u_taus_squared)
        deallocate(u_taub_squared)
    case default
        FLAbort('Unknown BC for TKE')
    end select 

end subroutine gls_tke_bc

!----------
! gls_psi_bc calculates the boundary conditions on the Psi (psi) field
! Boundary can be either Dirichlet or Neumann.
!----------
subroutine gls_psi_bc(state, bc_type)

    type(state_type), intent(in)     :: state  
    character(len=*), intent(in)     :: bc_type

    type(vector_field), pointer      :: positions 
    real                             :: gravity_magnitude
    integer                          :: i
    real, allocatable, dimension(:)  :: z0s, z0b, u_taus_squared, u_taub_squared
    type(scalar_field), pointer      :: tke, psi
    real                             :: value


    ewrite(2,*) "In gls_psi_bc: setting up"
    ! grab hold of some essential fields
    call get_option("/physical_parameters/gravity/magnitude", gravity_magnitude)
    positions => extract_vector_field(state, "Coordinate") 
    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")    
    psi => extract_scalar_field(state, "GLSGenericSecondQuantity")

    allocate(z0s(NNodes_sur))
    allocate(z0b(NNodes_bot))
    allocate(u_taus_squared(NNodes_sur))
    allocate(u_taub_squared(NNodes_bot))

    ewrite(2,*) "In gls_psi_bc: friction"
    ! get friction
    call gls_friction(state,z0s,z0b,gravity_magnitude,u_taus_squared,u_taub_squared)
    
    ! put tke onto surface fields
    call remap_field_to_surface(tke, top_surface_tke_values, top_surface_element_list)
    call remap_field_to_surface(tke, bottom_surface_tke_values, bottom_surface_element_list)

    ewrite(2,*) "In gls_psi_bc: setting values"
    select case(bc_type)
    case("neumann")
        do i=1,NNodes_sur
            ! GOTM Boundary
            !value = -(gls_n*(cm0**(gls_p+1.))*(kappa**(gls_n+1.)))/sigma_psi     &
            !         *node_val(top_surface_values,i)**(gls_m+0.5)*(z0s(i))**gls_n  
            ! Warner 2005
            value = -gls_n*(cm0**(gls_p))*(node_val(top_surface_tke_values,i)**gls_m)* &
                     (kappa**gls_n)*(z0s(i)**(gls_n-1))*((node_val(top_surface_km_values,i)/sigma_psi))
            call set(top_surface_values,i,value)
        end do
        do i=1,NNodes_bot
            ! GOTM Boundary
            !value = - gls_n*cm0**(gls_p+1.)*(kappa**(gls_n+1.)/sigma_psi)      &
            !           *node_val(bottom_surface_tke_values,i)**(gls_m+0.5)*(z0b(i))**gls_n
            ! Warner 2005
            value = gls_n*cm0**(gls_p)*node_val(bottom_surface_tke_values,i)**(gls_m)* &
                     kappa**gls_n*(z0b(i)**(gls_n-1))*(node_val(bottom_surface_km_values,i)/sigma_psi)
            call set(bottom_surface_values,i,value)
        end do
    case("dirichlet")
        do i=1,NNodes_sur
            value = max(cm0**(gls_p-2.*gls_m)*kappa**gls_n*u_taus_squared(i)**gls_m * &
                    (z0s(i))**gls_n,psi_min)
            call set(top_surface_values,i,value)
        end do
        do i=1,NNodes_bot
            value = max(cm0**(gls_p-2.*gls_m)*kappa**gls_n*u_taub_squared(i)**gls_m * &
                    (z0b(i))**gls_n,psi_min)
            call set(bottom_surface_values,i,value)
        end do    
    case default
        FLAbort('Unknown boundary type for Psi')
    end select
  
    
    deallocate(z0s)
    deallocate(z0b)
    deallocate(u_taus_squared)
    deallocate(u_taub_squared)

end subroutine gls_psi_bc

!----------
! gls_frction works out the depth of the friction layer
! either due to bottom topography roughness or the shear stress
! on the surface
!---------
subroutine gls_friction(state,z0s,z0b,gravity_magnitude,u_taus_squared,u_taub_squared)

    type(state_type), intent(in)         :: state
    real, intent(in)                     :: gravity_magnitude
    real, dimension(:), intent(inout)    :: z0s,z0b,u_taus_squared,u_taub_squared

    integer                              :: nobcs
    integer                              :: i,ii, MaxIter
    real                                 :: rr
    real                                 :: charnock_val=1400.
    character(len=OPTION_PATH_LEN)       :: bctype
    type(vector_field), pointer          :: wind_surface_field, positions, velocity
    type(scalar_field), pointer          :: tke
    type(vector_field)                   :: bottom_velocity, surface_forcing, cont_vel, surface_pos
    type(mesh_type)                      :: ocean_mesh
    real                                 :: u_taub, z0s_min
    real, dimension(1)                   :: temp_vector_1D ! Obviously, not really a vector, but lets keep the names consistant
    real, dimension(2)                   :: temp_vector_2D
    real, dimension(3)                   :: temp_vector_3D
    logical                              :: surface_allocated
    integer                              :: stat

    MaxIter = 10
    z0s_min = 0.003
    surface_allocated = .false.
  
    ! get meshes
    velocity => extract_vector_field(state, "Velocity")
    positions => extract_vector_field(state, "Coordinate")
    tke => extract_scalar_field(state, "GLSTurbulentKineticEnergy")
    wind_surface_field => null()
   
    ! grab stresses from velocity field - Surface
    nobcs = get_boundary_condition_count(velocity)
    do i=1, nobcs
        call get_boundary_condition(velocity, i, type=bctype)
        if (bctype=='wind_forcing') then
            wind_surface_field => extract_surface_field(velocity, i, "WindSurfaceField")
            call create_surface_mesh(ocean_mesh, top_surface_nodes, tke%mesh, &
                                     top_surface_element_list, 'OceanTop')
            call allocate(surface_forcing, wind_surface_field%dim, ocean_mesh, name="surface_velocity")
            surface_pos = get_coordinates_remapped_to_surface(positions, ocean_mesh, top_surface_element_list) 
            call deallocate(ocean_mesh)

            if (tke%mesh%continuity == velocity%mesh%continuity) then
                call set(surface_forcing,wind_surface_field)
            else
                ! remap onto same mesh as TKE
                call project_field(wind_surface_field, surface_forcing, surface_pos)
            end if

            surface_allocated = .true.
            call deallocate(surface_pos)
            exit

        end if
    end do

    ! sort out bottom surface velocity
    call create_surface_mesh(ocean_mesh, bottom_surface_nodes, tke%mesh, &
                             bottom_surface_element_list, 'OceanBottom')
    call allocate(bottom_velocity, velocity%dim, ocean_mesh, name="bottom_velocity")
    call allocate(cont_vel, velocity%dim, tke%mesh, name="ContVel")
    call deallocate(ocean_mesh)
    ! Do we need to project or copy?
    if (velocity%mesh%continuity == tke%mesh%continuity) then
        call set(cont_vel,velocity)
    else            
        ! remap onto same mesh as TKE
        call project_field(velocity, cont_vel, positions)
    end if

    call remap_field_to_surface(cont_vel, bottom_velocity, &
                                bottom_surface_element_list)
    call deallocate(cont_vel)
    ! we now have a bottom velocity surface and a top surface
    ! with the wind stress on (note easier to to zero the output array
    ! below than set wind_forcing to zero and work through all the calcs

            
    ! work out the friction in either 3 or 2 dimensions.
    if (positions%dim .eq. 3) then

        if (surface_allocated) then
            do i=1,NNodes_sur
                temp_vector_2D = node_val(surface_forcing,i)
                ! big hack! Assumes that the wind stress forcing has ALREADY been divded by ocean density
                ! Note that u_taus = sqrt(wind_stress/rho0)
                ! we assume here that the wind stress in diamond is already
                ! wind_stress/rho0, hence here:
                ! u_taus = sqrt(wind_stress)
                u_taus_squared(i) = max(1e-12,sqrt(((temp_vector_2D(1))**2+(temp_vector_2D(2))**2)))
                !  use the Charnock formula to compute the surface roughness
                z0s(i)=charnock_val*u_taus_squared(i)/gravity_magnitude
                if (z0s(i).lt.z0s_min) z0s(i)=z0s_min
            end do
        else
            z0s = z0s_min
            u_taus_squared = 0.0
        end if

        do i=1,NNodes_bot
            temp_vector_3D = node_val(bottom_velocity,i)
            u_taub = max(1e-12,sqrt(temp_vector_3D(1)**2+temp_vector_3D(2)**2+temp_vector_3D(3)**2))


            !  iterate bottom roughness length MaxIter times
            do ii=1,MaxIter
                z0b(i)=1e-7/max(1e-6,u_taub)+0.03*0.1

                ! compute the factor r
                rr=kappa/log(z0b(i))

                ! compute the friction velocity at the bottom
                u_taub = rr*sqrt(temp_vector_3D(1)**2+temp_vector_3D(2)**2+temp_vector_3D(3)**2)

            end do

            u_taub_squared(i) = u_taub**2
        end do

    else if (positions%dim .eq. 2) then
        if (surface_allocated) then
            do i=1,NNodes_sur
                temp_vector_1D = node_val(surface_forcing,i)
                u_taus_squared(i) = max(1e-12,temp_vector_1D(1))
                !  use the Charnock formula to compute the surface roughness
                z0s(i)=charnock_val*u_taus_squared(i)/gravity_magnitude
                if (z0s(i).lt.z0s_min) z0s(i)=z0s_min

            end do
        else
            z0s = z0s_min
            u_taus_squared = 0.0
        end if

        do i=1,NNodes_bot
            temp_vector_2D = node_val(bottom_velocity,i)
            u_taub = max(1e-12,sqrt(temp_vector_2D(1)**2+temp_vector_2D(2)**2))

            !  iterate bottom roughness length MaxIter times
            do ii=1,MaxIter
                z0b(i)=1e-7/(max(1e-6,u_taub)+0.03*0.1)
                rr=kappa/log(z0b(i))

                ! compute the friction velocity at the bottom
                u_taub = rr*sqrt((temp_vector_2D(1)**2+temp_vector_2D(2)**2))

            end do

            u_taub_squared(i) = u_taub**2
        end do

    else
        FLAbort("Unsupported dimension in GLS friction")
    end if


    call deallocate(bottom_velocity)
    if (surface_allocated) then
        call deallocate(surface_forcing)
    end if
        
    return

end subroutine gls_friction


!---------
! Output the optional fields if they exist in state
!---------
subroutine gls_output_fields(state)

    type(state_type), intent(in)     :: state

    type(scalar_field), pointer      :: scalarField
    type(tensor_field), pointer      :: tensorField
    integer                          :: stat

    scalarField => extract_scalar_field(state, "GLSLengthScale", stat)
    if(stat == 0) then
        call set(scalarField,ll) 
    end if

    scalarField => extract_scalar_field(state,"GLSTurbulentKineticEnergyOriginal", stat)
    if(stat == 0) then
        call set(scalarField,local_tke) 
    end if  

    scalarField => extract_scalar_field(state, "GLSBuoyancyFrequency", stat)
    if(stat == 0) then
        call set(scalarField,NN2) 
    end if
    
    scalarField => extract_scalar_field(state, "GLSVelocityShear", stat)
    if(stat == 0) then
        call set(scalarField,MM2) 
    end if
    
    scalarField => extract_scalar_field(state, "GLSShearProduction", stat)
    if(stat == 0) then
        call set(scalarField,P) 
    end if
    
    scalarField => extract_scalar_field(state, "GLSBuoyancyProduction", stat)
    if(stat == 0) then
        call set(scalarField,B) 
    end if
    
    scalarField => extract_scalar_field(state, "GLSDissipationEpsilon", stat)
    if(stat == 0) then
        call set(scalarField,eps) 
    end if

    scalarField => extract_scalar_field(state, "GLSStabilityFunctionSH", stat)
    if(stat == 0) then
        call set(scalarField,S_H) 
    end if    

    scalarField => extract_scalar_field(state, "GLSStabilityFunctionSM", stat)
    if(stat == 0) then
        call set(scalarField,S_M) 
    end if           
    
    scalarField => extract_scalar_field(state, "GLSWallFunction", stat)
    if(stat == 0) then
        call set(scalarField,Fwall) 
    end if    

    scalarField => extract_scalar_field(state, "GLSVerticalViscosity", stat)
    if(stat == 0) then
        ! add vertical background
        tensorField => extract_tensor_field(state, "GLSBackgroundDiffusivity")
       call set(scalarField,K_M)  
       call addto(scalarField, extract_scalar_field(tensorField, tensorField%dim(1), tensorField%dim(2)))
    end if     
      
    scalarField => extract_scalar_field(state, "GLSVerticalDiffusivity", stat)
    if(stat == 0) then
        ! add vertical background
        tensorField => extract_tensor_field(state, "GLSBackgroundDiffusivity")
        call set(scalarField,K_H)
        call addto(scalarField, extract_scalar_field(tensorField,tensorField%dim(1), tensorField%dim(2)))
    end if  


end subroutine gls_output_fields


!---------
! Calculate the wall function as set by the user
! Each wall function has been designed with a 
! particular problem in mind, so best to have a choice here
!---------
subroutine gls_calc_wall_function(state)

    type(state_type), intent(in)     :: state

    type(scalar_field), pointer    :: distanceToBottom, distanceToTop, tke
    real                           :: LLL, distTop, distBot
    type(scalar_field)             :: top, bottom
    real, parameter                :: E2 = 1.33, E4 = 0.25
    integer                        :: i, stat

 
    ! FWall is initialised in gls_init to 1, so no need to do anything
    if (gls_wall_option .eq. "none") return

    tke => extract_scalar_field(state,"GLSTurbulentKineticEnergy")
    distanceToTop => extract_scalar_field(state, "DistanceToTop")
    distanceToBottom => extract_scalar_field(state, "DistanceToBottom") 
    call allocate(top,tke%mesh,"TopOnTKEMesh")
    call allocate(bottom,tke%mesh,"BottomOnTKEMesh")
    call remap_field(distanceToTop,top,stat)
    call remap_field(distanceToBottom,bottom,stat)
    select case (gls_wall_option)
    case ("MellorYamda")
        do i=1,nNodes
            distTop = max(1.0,node_val(top,i))
            distBot = max(1.0,node_val(bottom,i))
            LLL = (distBot +  distTop) / (distTop * distBot)
            call set( Fwall, i, 1.0 + E2*( ((node_val(ll,i)/kappa)*( LLL ))**2 ))       
        end do
    case ("Burchard98")
        do i=1,nNodes
            distTop = max(1.0,node_val(top,i))
            distBot = max(1.0,node_val(bottom,i))
            LLL = 1.0 / min(distTop,distBot)
            call set( Fwall, i, 1.0 + E2*( ((node_val(ll,i)/kappa)*( LLL ))**2 ))       
        end do
    case ("Burchard01")
        do i=1,nNodes
            distTop = max(1.0,node_val(top,i))
            distBot = max(1.0,node_val(bottom,i))
            LLL = 1.0 / distTop
            call set( Fwall, i, 1.0 + E2*( ((node_val(ll,i)/kappa)*( LLL ))**2 ))       
        end do
    case ("Blumberg")   
        do i=1,nNodes
            distTop = max(0.1,node_val(top,i))
            distBot = max(0.1,node_val(bottom,i))
            LLL = E2 * (node_val(ll,i) / (kappa *  distBot)) ** 2
            LLL = LLL + E4 * (node_val(ll,i) / (kappa *  distTop)) ** 2
            call set( Fwall, i, 1.0 + LLL)      
        end do
    case default
        FLAbort("Unknown wall function") 
    end select
    call deallocate(top)
    call deallocate(bottom)


end subroutine gls_calc_wall_function

subroutine gls_allocate_temps(state)

    type(state_type), intent(inout) :: state
    type(scalar_field), pointer    :: tkeField

    tkeField => extract_scalar_field(state,"GLSTurbulentKineticEnergy")

    ! allocate some space for the fields we need for calculations, but are optional in the model
    ! we're going to allocate these on the velocity mesh as we need one of these...
    call allocate(ll,         tkeField%mesh, "LengthScale")    
    call allocate(NN2,        tkeField%mesh, "BuoyancyFrequency")
    call allocate(MM2,        tkeField%mesh, "VelocityShear")  
    call allocate(B,          tkeField%mesh, "BuoyancyFrequency")
    call allocate(P,          tkeField%mesh, "ShearProduction")  
    call allocate(S_H,        tkeField%mesh, "StabilityH")    
    call allocate(S_M,        tkeField%mesh, "StabilityM")    
    call allocate(K_H,        tkeField%mesh, "EddyDiff")    
    call allocate(K_M,        tkeField%mesh, "EddyVisc")        
    call allocate(eps,        tkeField%mesh, "GLS_TKE_Dissipation") 
    call allocate(Fwall,      tkeField%mesh, "GLS_WallFunction") 
    call allocate(density,    tkeField%mesh, "Density")
    call allocate(tke_old,    tkeField%mesh, "Old_TKE")
    call allocate(local_tke,  tkeField%mesh, "Local_TKE")

    call set(ll,0.)
    call set(NN2,0.)
    call set(MM2,0.)
    call set(B,0.)
    call set(P,0.)
    call set(S_H,0.)
    call set(S_M,0.)
    call set(K_H,0.)
    call set(K_M,0.)
    call set(eps,0.)
    call set(density,0.)
    call set(tke_old,0.)
    call set(local_tke,tkeField)

    nNodes = node_count(tkeField)

end subroutine gls_allocate_temps

!---------
! Align the diff/visc tensors with gravity when on the sphere
!---------
function align_with_radial(position, scalar) result(rotated_tensor)
    ! Function to align viscosities/diffusivities in the radial direction when on
    ! the sphere
    real, dimension(:), intent(in) :: position
    real, intent(in) :: scalar
    real, dimension(size(position),size(position)) :: rotated_tensor
    real :: rad, phi, theta

    assert(size(position)==3)

    rad=sqrt(sum(position(:)**2))
    phi=atan2(position(2),position(1))
    theta=acos(position(3)/rad)

    rotated_tensor(1,1)=scalar*sin(theta)**2*cos(phi)**2
    rotated_tensor(1,2)=scalar*sin(theta)**2*sin(phi)*cos(phi)
    rotated_tensor(1,3)=scalar*sin(theta)*cos(theta)*cos(phi)
    rotated_tensor(2,1)=rotated_tensor(1,2)
    rotated_tensor(2,2)=scalar*sin(theta)**2*sin(phi)**2
    rotated_tensor(2,3)=scalar*sin(theta)*cos(theta)*sin(phi)
    rotated_tensor(3,1)=rotated_tensor(1,3)
    rotated_tensor(3,2)=rotated_tensor(2,3)
    rotated_tensor(3,3)=scalar*cos(theta)**2

end function align_with_radial

end module gls