-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathio_ascii.F90
More file actions
executable file
·1301 lines (1046 loc) · 59.6 KB
/
io_ascii.F90
File metadata and controls
executable file
·1301 lines (1046 loc) · 59.6 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
! This file is part of Bottom RedOx Model (BROM, v.1.1).
! BROM is free software: you can redistribute it and/or modify it under
! the terms of the GNU General Public License as published by the Free
! Software Foundation (https://www.gnu.org/licenses/gpl.html).
! It 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. A copy of the license is provided in
! the COPYING file at the root of the BROM distribution.
!-----------------------------------------------------------------------
! Original author(s): Evgeniy Yakushev, Shamil Yakubov,
! Elizaveta Protsenko, Phil Wallhead,
! Anfisa Berezina, Matvey Novikov, Beatriz Arellano-Nava
!-----------------------------------------------------------------------
module io_ascii
use fabm_omp, only: type_fabm_model => type_fabm_omp_model
use fabm_driver
use yaml_types
use yaml,yaml_parse=>parse,yaml_error_length=>error_length
use fabm_types, only: attribute_length, rk
implicit none
private
type brom_par ! Type for sparse matrix
real(rk) :: realvalue
character(len=64) :: initname
character(len=64) :: outname
type(brom_par), pointer :: next
end type brom_par
type brom_name
character(len=64) :: outname
character(len=64) :: initname
type(brom_name), pointer :: next
end type brom_name
type (brom_par), pointer :: first
type (brom_name), pointer :: first_name
public find_index, porting_initial_state_variables, init_common, saving_state_variables, saving_state_variables_diag,&
get_brom_par, get_brom_name, svan, make_vert_grid, input_primitive_physics, input_ascii_physics, &
make_physics_bbl_sed, find_closest_index
contains
!=======================================================================================================================
subroutine init_common()
class (type_node), pointer :: node
character(len=yaml_error_length) :: yaml_error
integer :: unit_eff
character(len=256) :: path_eff
unit_eff = 1
path_eff = 'brom.yaml'
! Parse YAML file.
node => yaml_parse(trim(path_eff),unit_eff,yaml_error)
if (yaml_error/='') call fatal_error('fabm_create_model_from_yaml_file',trim(yaml_error))
if (.not.associated(node)) call fatal_error('fabm_create_model_from_yaml_file', &
'No configuration information found in '//trim(path_eff)//'.')
select type (node)
class is (type_dictionary)
call tree(node)
class is (type_node)
call fatal_error('brom', trim(path_eff)//' must contain a dictionary &
& at the root (non-indented) level, not a single value. Are you missing a trailing colon?')
end select
end subroutine init_common
!=======================================================================================================================
!=======================================================================================================================
subroutine tree(mapping)
class (type_dictionary), intent(in) :: mapping
class (type_node), pointer :: node
type (type_key_value_pair), pointer :: pair
character(len=64) :: instancename
node => mapping%get('instances')
if (.not.associated(node)) &
call fatal_error('create_model_tree_from_dictionary', 'No "instances" dictionary found at root level.')
select type (node)
class is (type_dictionary)
pair => node%first
class is (type_node)
nullify(pair)
call fatal_error('create_model_tree_from_dictionary',trim(node%path)// &
' must be a dictionary with (model name : information) pairs, not a single value.')
end select
! only brom acceptance, iterate throw other models with no action
do while (associated(pair))
instancename = trim(pair%key)
if (instancename == "brom") then
select type (dict=>pair%value)
class is (type_dictionary)
call from_tree(instancename, dict)
class is (type_null)
call fatal_error('create_model_tree_from_dictionary','Configuration information for model "'// &
trim(instancename)//'" must be a dictionary, not a single value or nothing')
class is (type_node)
call fatal_error('create_model_tree_from_dictionary','Configuration information for model "'// &
trim(instancename)//'" must be a dictionary, not a single value.')
end select
end if
pair => pair%next
end do
end subroutine tree
!=======================================================================================================================
!=======================================================================================================================
subroutine from_tree(instancename, node)
character(len=*), intent(in) :: instancename
class (type_dictionary),intent(in) :: node
class (type_dictionary),pointer :: childmap
type (type_error),pointer :: config_error
childmap => node%get_dictionary('initialization',required=.false.,error=config_error)
if (associated(config_error)) call fatal_error('create_model_from_dictionary',config_error%message)
if (associated(childmap)) call parse_initialization(childmap)
end subroutine from_tree
!=======================================================================================================================
!=======================================================================================================================
subroutine parse_initialization(node)
class (type_dictionary),intent(in) :: node
type (type_key_value_pair), pointer :: pair
type (brom_par), pointer :: current
type (brom_name), pointer :: current_name
logical :: success
real(rk) :: realvalue
character(len=64) :: initname
character(len=64) :: outname
! Transfer user-specified initial state to the model.
nullify(first)
pair => node%first
do while (associated(pair))
select type (value=>pair%value)
class is (type_scalar)
initname = trim(pair%key)
realvalue = value%to_real(default=real(0,real_kind),success=success)
if (.not.success) then !Assume the value is a string
realvalue = huge(1.0_rk)
outname = value%string
else
outname = repeat('z',64)
end if
class is (type_null)
call fatal_error('parse_initialization',trim(value%path)//' must be set to a real number, not to null.')
class is (type_dictionary)
call fatal_error('parse_initialization',trim(value%path)//' must be set to a real number, not to a dictionary.')
end select
allocate (current)
current = brom_par(realvalue, initname, outname, first)
first => current
pair => pair%next
end do
end subroutine parse_initialization
!=======================================================================================================================
!=======================================================================================================================
function get_brom_par(initname,defvalue) result(realvalue)
real(rk) :: realvalue
character(len=*), intent(in) :: initname
real(rk), optional, intent(in) :: defvalue
type (brom_par), pointer :: current
current => first
do
if (.not.associated(current)) exit
if (initname == current%initname) then
realvalue = current%realvalue
return
end if
current => current%next
end do
if (present(defvalue)) then
realvalue = defvalue
else
print *, "Check brom.yaml or name of the input variable for ", initname
stop
end if
end function get_brom_par
!=======================================================================================================================
!=======================================================================================================================
function get_brom_name(initname) result(outname)
character(len=64) :: outname
character(len=*), intent(in) :: initname
type (brom_par), pointer :: current
current => first
do
if (.not.associated(current)) exit
if (trim(initname) == trim(current%initname)) then
outname = trim(current%outname)
return
end if
current => current%next
end do
print *, "Check brom.yaml or name of the input variable for ", initname
stop
end function get_brom_name
!=======================================================================================================================
!=======================================================================================================================
function find_index(names, name) result(index)
character(len=*), intent(in) :: names(:)
character(len=*), intent(in) :: name
integer :: index
do index = 1, size(names)
if (names(index) == name) return
end do
index = -1
end function
!=======================================================================================================================
!=======================================================================================================================
function find_closest_index(array, value) result(index)
!> This function finds the index of the closest value in an array to a given value, considering a tolerance.
!!
!! @param array The input array of real numbers.
!! @param value The value to which the closest array element is to be found.
!! @return index The index of the element in the array that is closest to the given value.
implicit none
! Arguments
real(rk), intent(in) :: array(:) !< Array with values (e.g. depths in the vertical grid)
real(rk), intent(in) :: value !< The value to find the closest match for.
! Local variables
integer :: index !< The index of the closest value.
real(rk), allocatable :: diff(:) !< Array to store the absolute differences.
! Compute the absolute differences
allocate(diff(size(array)))
diff = abs(array - value)
! Find the index of the minimum difference
index = minloc(diff, dim=1)
! Deallocate the temporary array
deallocate(diff)
end function find_closest_index
!=======================================================================================================================
!=======================================================================================================================
subroutine porting_initial_state_variables(icfile_name, model_year, julianday, i_max, k_max, par_max, &
par_name, cc, vv)
!Reads in initial conditions from an ascii file <icfilename>
!Note: format should match that used in subroutine saving_state_variables
!Input variables
character(len=*), intent(in) :: icfile_name
integer, intent(in) :: model_year, julianday, i_max, k_max, par_max
character(len=*), intent(in) :: par_name(:)
!Output variables
! real(rk), dimension(:,:,:), pointer, intent(out) :: cc, vv
real(rk), dimension(:,:,:), intent(inout) :: cc, vv
!Local variables
integer :: d_year_start, julianday_start, k_max_start, par_max_start
integer, allocatable :: column2state(:)
character(20000) :: labels
character(200) :: comments
integer :: i, j, k, ip, istart, istop, foo, m
real(rk) :: value
open(9,file=icfile_name)
read(9,'( 1x,i4,1x,i5,1x,i5,1x,i5 )') d_year_start, julianday_start, k_max_start, par_max_start
if (k_max_start /= k_max) stop 'number of levels in start-up file does not match number of levels in model'
allocate(column2state(par_max))
read(9, '( 1x, a )') labels
istart = 1
do ip = 1, par_max_start
istop = istart - 1 + index(labels(istart:), ' ')
column2state(ip) = find_index(par_name, labels(istart:istop-1))
istart = istop + 1
end do
read(9, *) comments
do i=1,i_max
do k=1,k_max
read(9, '( i5 )', advance = 'no') foo
read(9, '( i5 )', advance = 'no') foo
read(9, '( 1x,f10.4 )', advance = 'no') value !we don't read again z()
do m=1,4
read(9, '( 1x,f15.9 )', advance = 'no') value !we don't read again t,s,Kz,hz
end do
read(9, '( 1x,f15.9 )', advance = 'no') vv(i,k,1)
do ip=1,par_max
read(9, '( 1x, f17.9 )', advance = 'no') value
if (ip /= -1) then
if (value /= 0.) then
cc(i,k,ip) = value
else
cc(i,k,ip) = 0.000
end if
end if
end do
read(9, *)
end do
end do
close(9)
end subroutine porting_initial_state_variables
!=======================================================================================================================
!=======================================================================================================================
subroutine saving_state_variables_diag(outfile_name, model_year, julianday, i_max, k_max, par_max, par_name, &
z, hz, cc, vv, t, s, kz, model, extend_out)
!Saves final conditions in an ascii file <outfilename>
!Input variables
character (len = *), intent(in) :: outfile_name
integer, intent(in) :: model_year, julianday, i_max, k_max, par_max
character(len=*), intent(in) :: par_name(:)
real(rk), dimension(:), intent(in) :: z, hz
real(rk), dimension(:,:,:), intent(in) :: cc
real(rk), dimension(:,:,:), intent(in) :: vv
real(rk), dimension (:,:,:), intent(in) :: t, s, kz
class (type_fabm_model), pointer :: model
logical, intent(in) :: extend_out
!Local variables
integer :: ip, k, i, ilast
real :: temp_matrix(i_max,k_max)
open(10,file=outfile_name)
! First line with year, julian day, number of levels, number of state variables
write(10,'( i4,1x,i5,1x,i5,1x,i5 )') model_year, julianday, k_max, par_max
do ip=1, par_max
write(10,'(1x,a)',advance='NO') trim(par_name(ip))
end do
write(10,*)
write(10,'(3h i ,3h k ,6hDepth ,4h t ,4h s ,4h Kz ,4h hz ,4hVol )',advance='NO')
do ip=1,par_max
write(10,'(1x,a)',advance='NO') trim(par_name(ip)(15:))
end do
if (extend_out .eqv. .true.) then
do ip = 1, size(model%interior_diagnostic_variables)
if (model%interior_diagnostic_variables(ip)%save) then
ilast = index(model%interior_diagnostic_variables(ip)%path,'/',.true.)
write(10,'(1x,a)',advance='NO') trim(model%interior_diagnostic_variables(ip)%path(ilast+1:))
end if
end do
end if
write(10,*)
!Subsequent lines: one for each depth level
do i=1,i_max
do k=1,k_max
write(10,'(i5)',advance='NO') i
write(10,'(i5)',advance='NO') k
write(10,'(1x,f10.4)',advance='NO') z(k)
write(10,'(1x,f15.9)',advance='NO') t(i,k,julianday)
write(10,'(1x,f15.9)',advance='NO') s(i,k,julianday)
write(10,'(1x,f15.9)',advance='NO') kz(i,k,julianday)
write(10,'(1x,f15.9)',advance='NO') hz(k)
write(10,'(1x,f15.9)',advance='NO') vv(i,k,1)
do ip=1,par_max
write(10,'(1x,f15.9)',advance='NO') cc(i,k,ip)
end do
if (extend_out .eqv. .true.) then
do ip = 1, size(model%interior_diagnostic_variables)
if (model%interior_diagnostic_variables(ip)%save) then
!temp_matrix = fabm_get_bulk_diagnostic_data(model, ip)
write(10,'( 1x,f15.9 )',advance='NO') model%get_interior_diagnostic_data(ip)
end if
end do
end if
write(10,*)
end do
end do
close(10)
end subroutine saving_state_variables_diag
!=======================================================================================================================
!=======================================================================================================================
subroutine saving_state_variables(outfile_name, model_year, julianday, i_max, k_max, par_max, par_name, &
z, hz, cc, vv, t, s, kz)
!Saves final conditions in an ascii file <outfilename>
!Input variables
character (len = *), intent(in) :: outfile_name
integer, intent(in) :: model_year, julianday, i_max, k_max, par_max
character(len=*), intent(in) :: par_name(:)
real(rk), dimension(:), intent(in) :: z, hz
real(rk), dimension(:,:,:), intent(in) :: cc
real(rk), dimension(:,:,:), intent(in) :: vv
real(rk), dimension (:,:,:), intent(in) :: t, s, kz
!Local variables
integer :: ip, k, i
open(10,file=outfile_name)
! First line with year, julian day, number of levels, number of state variables
write(10,'( i4,1x,i5,1x,i5,1x,i5 )') model_year, julianday, k_max, par_max
do ip=1, par_max
write(10,'(1x,a)',advance='NO') trim(par_name(ip))
end do
write(10,*)
write(10,'(3h i ,3h k ,6hDepth ,4h t ,4h s ,4h Kz ,4h hz ,4hVol )',advance='NO')
do ip=1,par_max
write(10,'(1x,a)',advance='NO') trim(par_name(ip)(15:))
end do
write(10,*)
!Subsequent lines: one for each depth level
do i=1,i_max
do k=1,k_max
write(10,'(i5)',advance='NO') i
write(10,'(i5)',advance='NO') k
write(10,'(1x,f10.4)',advance='NO') z(k)
write(10,'(1x,f15.9)',advance='NO') t(i,k,julianday)
write(10,'(1x,f15.9)',advance='NO') s(i,k,julianday)
write(10,'(1x,f15.9)',advance='NO') kz(i,k,julianday)
write(10,'(1x,f15.9)',advance='NO') hz(k)
write(10,'(1x,f15.9)',advance='NO') vv(i,k,1)
do ip=1,par_max
write(10,'(1x,f17.9)',advance='NO') cc(i,k,ip)
end do
write(10,*)
end do
end do
close(10)
end subroutine saving_state_variables
!=======================================================================================================================
!=======================================================================================================================
subroutine make_vert_grid(z, dz, hz, z_w, dz_w, hz_w, k_wat_bbl, k_max, k_bbl_sed)
!Constructs full vertical grid (water + sediments) given water column parameters and parameters from brom.yaml
!Input variables
real(rk), dimension(:), intent(in) :: z_w, dz_w, hz_w !Water column layer midpoints (z_w), increments between them (dz_w), and layer thicknesses (hz_w)
integer, intent(in) :: k_wat_bbl !Number of layers above the water-BBL interface
integer, intent(in) :: k_max !Total number of layers (set in init_brom_transport using k_wat_bbl and k_points_below_water from brom.yaml)
!Output variables
real(rk), dimension(:), intent(out) :: z, dz, hz !Full column layer midpoints (z), increments between them (dz), and layer thicknesses (hz)
integer, intent(out) :: k_bbl_sed !Number of layers above the BBL-sediment interface (SWI)
!Local variables
real(rk) :: bbl_thickness !Input BBL thickness [m]
real(rk) :: hz_bbl_min !Minimum hz in the BBL [m]
real(rk) :: hz_sed_min !Minimum hz in the sediments [m]
real(rk) :: hz_sed_max !Maximum hz in the sediments [m]
real(rk) :: mult_bbl !Common ratio for the geometric progression of BBL layer thicknesses (hz(n) = hz_bbl_min*(mult_bbl^n), mult_bbl > 1, default = 2)
real(rk) :: scale_fac !Scale factor to ensure that total BBL thickness = bbl_thickness
real(rk) :: new_hz !Temporal variable to calculate the new thickness of a BBL layer
integer :: extra_layer !If the deepest layer is too thin (1.5 times the BBL), it is converted into the BBL
integer :: k
! The grid structure is:
!
!======================= (air-sea interface)
! o z(1), hz(1)
!-----------------------
! o z(2)=z(1)+dz(1), hz(2)
! : :
! : :
! o z(k_wat_bbl), hz(k_wat_bbl) NOTE: k_wat_bbl is set by brom.yaml or (with priority) by netcdf input
!======================= (water-bbl interface)
! o z(k_wat_bbl+1)=z(k_wat_bbl)+dz(k_wat_bbl), hz(k_wat_bbl+1)
!-----------------------
! o z(k_wat_bbl+2), hz(k_wat_bbl+2)
! : :
! : :
! o z(k_bbl_sed), hz(k_bbl_sed) NOTE: k_bbl_sed is set by this routine
!======================= (bbl-sediment interface)
! o z(k_bbl_sed+1)=z(k_bbl_sed)+dz(k_bbl_sed), hz(k_bbl_sed+1)
!-----------------------
! o z(k_bbl_sed+2), hz(k_bbl_sed+2)
! : :
! : :
! o z(k_max)=z(k_max-1)+dz(k_max-1), hz(k_max) NOTE: k_max is set by this routine using k_bbl_sed and input k_sed
!======================= (bottom boundary)
!Getting parameters from brom.yaml
bbl_thickness = get_brom_par("bbl_thickness")
hz_bbl_min = get_brom_par("hz_bbl_min")
hz_sed_min = get_brom_par("hz_sed_min")
hz_sed_max = get_brom_par("hz_sed_max")
!Defining layer midpoint depths [m] and their thicknesses/increments in the water column
z(1:k_wat_bbl) = z_w(1:k_wat_bbl)
hz(1:k_wat_bbl) = hz_w(1:k_wat_bbl)
dz(1:k_wat_bbl-1) = dz_w(1:k_wat_bbl-1)
!Adjusting the input grid to allow high-resolution BBL to be inserted (if not too thick)
if (bbl_thickness.gt.hz(k_wat_bbl)) then
write(*,*) "Requested BBL thickness exceeds input thickness of bottom pelagic layer: cannot insert BBL"
stop
end if
mult_bbl=2.0_rk ! mult_bbl should be larger than 1 to ensure successive layers are thicker
if ((hz(k_wat_bbl) - bbl_thickness) <= bbl_thickness / 2.0) then ! if the deepest layer is thinner than 1.5 times the BBL thickness
extra_layer = 1 ! The deepest layer becomes the BBL
bbl_thickness = hz(k_wat_bbl)
else
hz(k_wat_bbl) = hz_w(k_wat_bbl) - bbl_thickness ! Otherwise adjusts the thickness of the bottom layer
extra_layer = 0
end if
!Defining the grid in the BBL
hz(k_wat_bbl+1-extra_layer) = hz_bbl_min ! Introducing the thinnest layer in the BBL
do k=k_wat_bbl+1-extra_layer,k_max
new_hz = hz_bbl_min * (mult_bbl**(k-k_wat_bbl+extra_layer)) ! hz(n) = hz(1)*(mult_bbl^n)
! Calculating cumulative sum and check if it exceeds bbl_thickness
if (sum(hz(k_wat_bbl+1-extra_layer:k)) + new_hz <= (bbl_thickness - hz_sed_min)) then
hz(k_wat_bbl+2-extra_layer:k+2) = hz(k_wat_bbl+1-extra_layer:k) ! Moving the layers downwards
hz(k_wat_bbl+1-extra_layer) = new_hz ! Insterting the new layer
else
k_bbl_sed = k + 1 ! Defining the BBL-sediment interface layer
hz(k_bbl_sed) = hz_sed_min
exit ! Exit loop
end if
end do
! Rescaling layers to adjust the thickness to bbl_thickness
scale_fac = (bbl_thickness - hz_sed_min) / sum(hz(k_wat_bbl+1-extra_layer:k_bbl_sed))
hz(k_wat_bbl+1-extra_layer:k_bbl_sed-1) = scale_fac * hz(k_wat_bbl+1-extra_layer:k_bbl_sed-1)
hz(k_wat_bbl+1-extra_layer) = hz(k_wat_bbl+1-extra_layer) - (sum(hz(k_wat_bbl+1-extra_layer:k_bbl_sed)) - bbl_thickness) ! Adjusting the first boundary layer
! Grid in the sediments
do k = k_bbl_sed + 1, k_max
hz(k) = min(hz_sed_min * (2.0**(k - k_bbl_sed-1)), hz_sed_max)
end do
! Calculating depths and dz from the rescaled hz values in the BBL and sediment layers
z(k_wat_bbl) = z(k_wat_bbl-1) +(hz(k_wat_bbl-1) + hz(k_wat_bbl))* 0.5_rk ! Adjust depth (z) at the deepest layer of the water column
dz(k_wat_bbl-1) = z(k_wat_bbl) - z(k_wat_bbl-1) ! Updating the distance (dz) to the previous layer
! Estimating depths and distances between layers in BBL and sediments
do k = k_wat_bbl + 1, k_max
dz(k-1) = 0.5 * (hz(k-1) + hz(k))
z(k) = z(k-1) + dz(k-1)
end do
!Record the vertical grid in an ascii output file
open(10,FILE = 'Vertical_grid.dat')
write(10,'(5h k ,6hz[k] ,6hdz[k] ,7hhz[k] )')
do k=1,k_max
if (k == k_wat_bbl) then
write(10,'(1x,i4, 1x,f9.4, 5h _o_ , 1x,f9.4, " <- k_wat_bbl")') k, z(k), hz(k)
else if (k == k_bbl_sed) then
write(10,'(1x,i4, 1x,f9.4, 5h _o_ , 1x,f9.4, " <- k_bbl_sed")') k, z(k), hz(k)
else
write(10,'(1x,i4, 1x,f9.4, 5h _o_ , 1x,f9.4)') k, z(k), hz(k)
end if
write(10,'(2x,i4, 6h ==== , 1x,f8.4,6h ==== )') k, dz(k)
end do
close(10)
open(10,FILE = 'Vertical_grid_flat.dat', status='unknown', action='write')
write(10, '(A)') 'k,z,dz,hz,Layer Type' ! Header
do k = 1, k_max
if (k < k_wat_bbl) then
write(10, '(I4, ",", F13.5, ",", F13.5, ",", F13.5, ",", A12)') k, z(k), dz(k), hz(k), 'water'
else if (k == k_wat_bbl) then
write(10, '(I4, ",", F13.5, ",", F13.5, ",", F13.5, ",", A12)') k, z(k), dz(k), hz(k), 'water-BBL'
else if (k > k_wat_bbl .and. k < k_bbl_sed) then
write(10, '(I4, ",", F13.5, ",", F13.5, ",", F13.5, ",", A12)') k, z(k), dz(k), hz(k), 'BBL'
else if (k == k_bbl_sed) then
write(10, '(I4, ",", F13.5, ",", F13.5, ",", F13.5, ",", A12)') k, z(k), dz(k), hz(k), 'BBL-sediment'
else
write(10, '(I4, ",", F13.5, ",", F13.5, ",", F13.5, ",", A12)') k, z(k), dz(k), hz(k), 'sediment'
end if
end do
close(10)
end subroutine make_vert_grid
!=======================================================================================================================
!=======================================================================================================================
subroutine input_primitive_physics(z_w, dz_w, hz_w, k_wat_bbl, water_layer_thickness, t_w, s_w, kz_w, i_max, days_in_yr)
!Constructs (temperature, salinity, diffusivity) forcings for the water column assuming a simple hydrophysical scenario
!with hypothetical sinusoidal variations
!Input variables
integer, intent(in) :: k_wat_bbl, i_max, days_in_yr
real(rk), intent(in) :: water_layer_thickness
!Output variables
real(rk), allocatable, dimension (:,:,:), intent(out) :: t_w, s_w, kz_w
real(rk), allocatable, dimension (:), intent(out) :: z_w, dz_w, hz_w
!Local variables
real(rk), allocatable, dimension(:) :: sal_sum, sal_win, tem_sum, tem_win, dens
integer :: i, k, iday
!Allocate local variables
allocate(sal_sum(k_wat_bbl))
allocate(sal_win(k_wat_bbl))
allocate(tem_sum(k_wat_bbl))
allocate(tem_win(k_wat_bbl))
allocate(dens(k_wat_bbl))
allocate(z_w(k_wat_bbl))
allocate(dz_w(k_wat_bbl))
allocate(t_w(i_max,k_wat_bbl,days_in_yr))
allocate(s_w(i_max,k_wat_bbl,days_in_yr))
allocate(kz_w(i_max,k_wat_bbl,days_in_yr))
!!! HERE THE USER MUST ADAPT FOR HIS/HER DESIRED SCENARIO --------------------------------------------------------------
!
!Make grid parameters assuming uniform grid defined by water_layer_thickness and k_wat_bbl in brom.yaml
hz_w = water_layer_thickness/k_wat_bbl
dz_w = hz_w
z_w(1) = 0.5_rk*hz_w(1)
do k=2,k_wat_bbl
z_w(k) = z_w(k-1) + dz_w(k-1)
end do
!Set summer and winter vertical distributions
do k=1,k_wat_bbl
sal_sum(k) = 35.0_rk + 0.01_rk*k
sal_win(k) = 35.0_rk + 0.001_rk*k
tem_sum(k) = 25.0_rk - 0.1_rk*k
tem_win(k) = 10.0_rk - 0.001_rk*k
enddo
!Seasonal variability of T and S
do iday=1,days_in_yr
do k=1,k_wat_bbl
s_w(i_max,k,iday) = sal_win(k)+0.5*(1.+sin(2*3.14*(iday+240.)/365.))*(sal_sum(k)-sal_win(k))
t_w(i_max,k,iday) = tem_win(k)+0.5*(1.+sin(2*3.14*(iday+240.)/365.))*(tem_sum(k)-tem_win(k))
enddo
enddo
!Calculate vertical turbulent coefficient Kz from density (Gargett)
do iday=1,days_in_yr
do k=1, k_wat_bbl
call svan(s_w(i_max,k,iday),t_w(i_max,k,iday),z_w(k),dens(k)) !calculate density as f(p,t,s)
end do
do k=1, k_wat_bbl-1
Kz_w(i_max,k,iday)=0.5E-6 /&
((9.81/(1000.+(dens(k)+dens(k+1))/2.)&
*(abs(dens(k+1)-dens(k))/dz_w(k)) &
)**0.5)
end do
end do
!!!---------------------------------------------------------------------------------------------------------------------
!Write to output ascii file
open(12,file = 'Hydrophysics.dat')
do iday=1,days_in_yr
do k=1,k_wat_bbl
write(12,'(2(1x,i4))',advance='NO') iday, k
do i=1,i_max
write (12,'(1x,i4, 2(1x,f8.4),1x,f15.11,2f7.3)',advance='NO') i, t_w(i_max,k,iday), s_w(i_max,k, iday), kz_w(i_max,k,iday), dz_w(k), z_w(k)
end do
write(12,*)
end do
end do
close(12)
end subroutine input_primitive_physics
!=======================================================================================================================
!=======================================================================================================================
subroutine input_ascii_physics(z_w, dz_w, hz_w, k_wat_bbl, water_layer_thickness, t_w, s_w, kz_w, i_max, days_in_yr)
!Constructs (temperature, salinity, diffusivity) forcings for water column using input physics in ascii format
!Input variables
integer, intent(in) :: k_wat_bbl, i_max, days_in_yr
real(rk), intent(in) :: water_layer_thickness
!Output variables
real(rk), allocatable, dimension (:,:,:), intent(out) :: t_w, s_w, kz_w
real(rk), allocatable, dimension (:), intent(out) :: z_w, dz_w, hz_w
!Local variables
real(rk), allocatable, dimension(:) :: sal_sum, sal_win, tem_sum, tem_win, dens
integer :: i, j, k, iday, k_max_TEL
! real(rk) :: tem_TEL(55,365), sal_TEL(55,365), kz_TEL(55,365) ! this is for TELEMARK model outputs for Berre
real(rk) :: tem_TEL(100,365), sal_TEL(100,365), kz_TEL(100,365) ! this is for TELEMARK model outputs for Berre
!Allocate local variables
allocate(sal_sum(k_wat_bbl))
allocate(sal_win(k_wat_bbl))
allocate(tem_sum(k_wat_bbl))
allocate(tem_win(k_wat_bbl))
allocate(dens(k_wat_bbl))
allocate(z_w(k_wat_bbl))
allocate(dz_w(k_wat_bbl))
allocate(hz_w(k_wat_bbl))
allocate(t_w(i_max,k_wat_bbl,days_in_yr))
allocate(s_w(i_max,k_wat_bbl,days_in_yr))
allocate(kz_w(i_max,k_wat_bbl,days_in_yr))
!!! HERE THE USER MUST ADAPT FOR HIS/HER PARTICULAR APPLICATION --------------------------------------------------------
goto 2
!
1 continue ! CASE BERRE:
!
! The code below is provided as an example, reading (T,S) data from an ascii file of interpolated output from a model
! (TELEMARC) and using the resulting seawater density to compute Kz for a particular application (Berre)
!____________________________________________________________________
! TELEMARK produced file
!____________________________________________________________________
!Make grid parameters assuming uniform grid defined by water_layer_thickness and k_wat_bbl in brom.yaml
hz_w = water_layer_thickness/k_wat_bbl
dz_w = hz_w
z_w(1) = 0.5_rk*hz_w(1)
do k=2,k_wat_bbl
z_w(k) = z_w(k-1) + dz_w(k-1)
end do
!Read (T,S) data for Berre
k_max_TEL= 35
open(19, file='tem2_new.dat')
do i=1,days_in_yr
do j=1,k_max_TEL
read(19, *) tem_TEL(j,i) ! measured and interpolated data from TELEMARC model
end do
end do
close(19)
open(20, file='sal2_new.dat')
do i=1,days_in_yr
do j=1,k_max_TEL
read(20, *) sal_TEL(j,i) ! measured and interpolated data from TELEMARC model
end do
end do
close(20)
do iday=1,days_in_yr
do k=1,k_wat_bbl
s_w(i_max,k,iday) = sal_TEL(k,iday)
t_w(i_max,k,iday) = tem_TEL(k,iday)
enddo
enddo
!Calculate vertical turbulent coefficient Kz from density (Gargett)
do iday=1,days_in_yr
do k=1, k_wat_bbl
call svan(s_w(i_max,k,iday),t_w(i_max,k,iday),z_w(k),dens(k)) !calculate density as f(p,t,s)
end do
do k=1, k_wat_bbl-1
Kz_w(i_max,k,iday)=0.5E-6 /&
((9.81/(1000.+(dens(k)+dens(k+1))/2.)&
*(abs(dens(k+1)-dens(k))/dz_w(k)) &
)**0.5)
end do
Kz_w(i_max,k_wat_bbl,iday)=Kz_w(i_max,k_wat_bbl-1,iday)
end do
!---end CASE BERRE-----------------------------------------------------------------------------------------------------
2 continue ! CASE SPANGEREID:
!
! The code below is provided as an example, reading (T,S) data from an ascii file of interpolated output from NODC
! from the North Sea and using the resulting seawater density to compute Kz for a particular application (Spangereid)
!____________________________________________________________________
!Make grid parameters assuming uniform grid defined by water_layer_thickness and k_wat_bbl in brom.yaml
hz_w = water_layer_thickness/k_wat_bbl
dz_w = hz_w
z_w(1) = 0.5_rk*hz_w(1)
do k=2,k_wat_bbl
z_w(k) = z_w(k-1) + dz_w(k-1)
end do
!Read (T,S) data for Berre
k_max_TEL= 100
open(19, file='spa_t.dat')
do j=1,k_max_TEL
do i=1,days_in_yr
read(19, *) k,k,tem_TEL(j,i) ! measured and interpolated data from TELEMARC model
end do
end do
close(19)
open(20, file='spa_s.dat')
do j=1,k_max_TEL
do i=1,days_in_yr
read(20, *) k,k,sal_TEL(j,i) ! measured and interpolated data from TELEMARC model
end do
end do
close(20)
do iday=1,days_in_yr
do k=1,k_wat_bbl
s_w(i_max,k,iday) = sal_TEL(k,iday)
t_w(i_max,k,iday) = tem_TEL(k,iday)
enddo
enddo
!Calculate vertical turbulent coefficient Kz from density (Gargett)
do iday=1,days_in_yr
do k=1, k_wat_bbl
call svan(s_w(i_max,k,iday),t_w(i_max,k,iday),z_w(k),dens(k)) !calculate density as f(p,t,s)
end do
do k=1, k_wat_bbl-1
Kz_w(i_max,k,iday)=1.0E-5 /& !0.5E-4
((9.81/(1000.+(dens(k)+dens(k+1))/2.)&
*(abs(dens(k+1)-dens(k))/dz_w(k)) &
)**0.4)
end do
Kz_w(i_max,k_wat_bbl,iday)=Kz_w(i_max,k_wat_bbl-1,iday)
end do
!---end CASE SPANGEREID--------------------------------------------------------------------------------------------------
end subroutine input_ascii_physics
!=======================================================================================================================
!=======================================================================================================================
subroutine make_physics_bbl_sed(t, s, kz, hmix_rate, cc_hmix, u_x, u_x_w, t_w, s_w, kz_w, cc_hmix_w, kz_mol, kz_bio, &
z, dz, hz, i_min, i_max, k_wat_bbl, k_bbl_sed, k_max, par_max, steps_in_yr, alpha, is_solid, phi, phi1, phi_inv, &
pF1, pF2, mu0_musw, tortuosity, w_b, u_b, rho, dt, freq_turb, par_name, diff_method, bioturb_across_SWI, &
ip_sol, ip_par, dphidz_SWI, wat_content, pWC, input_step)
!Constructs profiles for (temperature, salinity, vertical diffusivity etc.) for the full column (water + sediments)
!given water column variability and parameters from brom.yaml
!Input variables
integer, intent(in) :: i_min, i_max, k_wat_bbl, k_bbl_sed, k_max, par_max, steps_in_yr, freq_turb
integer, intent(in) :: diff_method, bioturb_across_SWI, ip_sol, ip_par,input_step
integer, dimension(:), intent(in) :: is_solid
real(rk), intent(in) :: dt
real(rk), dimension(:), intent(in) :: z, dz, hz
real(rk), dimension(:,:,:), intent(in) :: t_w, s_w, kz_w, u_x_w
real(rk), dimension(:,:,:,:), intent(in) :: cc_hmix_w
character(len=attribute_length), dimension(:), intent(in) :: par_name
!Output variables
real(rk) :: mu0_musw, dphidz_SWI
real(rk), dimension(:), intent(out) :: rho
real(rk), dimension(:,:), intent(out) :: kz_bio, alpha, phi, phi_inv, tortuosity, w_b, u_b, wat_content
real(rk), dimension(:,:,:), intent(out) :: t, s, kz, u_x, hmix_rate, kz_mol, pF1, pF2, pWC
real(rk), dimension(:,:,:,:), intent(out) :: cc_hmix
!Local variables
integer :: i, k, ip, sel, iday, istep
integer :: k_sed(k_max-k_bbl_sed), k_sed1(k_max+1-k_bbl_sed), k_bbl1(k_bbl_sed-k_wat_bbl)
real(rk) :: z_wat_bbl, z_bbl_sed, kz_gr, z1(k_max+1), z_s1(k_max+1), phi1(i_max,k_max+1)
integer :: kz_bbl_type, dynamic_kz_bbl
real(rk) :: kz_bbl_max, dbl_thickness, kz_mol0
real(rk) :: a1_bioirr, a2_bioirr
real(rk) :: kz_bioturb_max, z_const_bioturb, z_decay_bioturb
real(rk) :: phi_0, phi_inf, z_decay_phi, w_binf, rho_def, wat_con_0, wat_con_inf
real(rk) :: kzCFL(k_bbl_sed-1,steps_in_yr), kz_molCFL(k_max-1,par_max)
!!Get diffusivity parameters from brom.yaml
!Turbulence in the benthic boundary layer
kz_bbl_type = get_brom_par("kz_bbl_type")
kz_bbl_max = get_brom_par("kz_bbl_max")
dbl_thickness = get_brom_par("dbl_thickness")
dynamic_kz_bbl = get_brom_par("dynamic_kz_bbl")
!Molecular diffusivity of solutes (single constant value, infinite dilution)
kz_mol0 = get_brom_par("kz_mol0")
mu0_musw = get_brom_par("mu0_musw")
!Bioturbation
kz_bioturb_max = get_brom_par("kz_bioturb_max")
z_const_bioturb = get_brom_par("z_const_bioturb")
z_decay_bioturb = get_brom_par("z_decay_bioturb")
!Bioirrigation
a1_bioirr = get_brom_par("a1_bioirr")
a2_bioirr = get_brom_par("a2_bioirr")
!Porosity
phi_0 = get_brom_par("phi_0")
phi_inf = get_brom_par("phi_inf")
z_decay_phi = get_brom_par("z_decay_phi")
wat_con_0 = get_brom_par("wat_con_0")
wat_con_inf = get_brom_par("wat_con_inf")
!Vertical advection in the sediments
w_binf = get_brom_par("w_binf")
!Density of particles
rho_def = get_brom_par("rho_def")
rho = 0.0_rk
do ip=1,par_max
if (is_solid(ip).eq.1) then
rho(ip) = get_brom_par('rho_' // trim(par_name(ip)),rho_def)
write(*,*) "Assumed density of ", trim(par_name(ip)), " = ", rho(ip)
end if
end do
!!Set useful parameters for calculations