-
Notifications
You must be signed in to change notification settings - Fork 12
/
fvcom_driver.f90
1977 lines (1702 loc) · 58.9 KB
/
fvcom_driver.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
!=======================================================================
! Fiscm FVCOM Driver
!
! Description
! - Read and setup mesh
! - Advect
! - Diffuse
! - Locate Particles
!
! Comments:
! - Advection currently uses 1st Order Euler Step, update to a stiffer
! solver (for example 4-stage RK used by C. Chen)
! - Vertical diffusion uses Vissers modified random walk
! - Vertical diffusion improvement possibilities:
! a.) splines (previously used by J. Pringle, R. Ji, and M. Huret)
! b.) tensioned splines to avoid needing to smooth diffusivity
! before splining
! c.) binned random walk (Thygesen and Adlandsvik, MEPS 347,2007)
! - In theory fiscm could be driven by an alternate ocean model if
! these routines (initialize,advect,diffuse,find) are recoded
!
! !REVISION HISTORY:
! Original author(s): G. Cowles
!=======================================================================
Module Ocean_Model
use gparms
use mod_igroup
use forcing
implicit none
!mesh params
integer, parameter :: level_based = 1
integer, parameter :: layer_based = 2
!Runge-Kutta integration coefficients
integer, parameter :: nstage = 1
real(sp),parameter :: alpha(1) = 1.0
integer, parameter :: mstage = 4
real(sp),parameter :: A_RK(4) = (/0.0_sp,0.5_sp,0.5_sp,1.0_sp/)
real(sp), parameter:: B_RK(4) = (/1.0_sp/6.0_sp,1.0_sp/3.0_sp, 1.0_sp/3.0_sp,1.0_sp/6.0_sp/)
real(sp), parameter:: C_RK(4) = (/0.0_sp,0.5_sp,0.5_sp,1.0_sp/)
!Added by Xinyou Lin
integer, allocatable :: nbe(:,:) !!INDICES OF ELMNT NEIGHBORS^M
integer, allocatable :: isonb(:) !!NODE MARKER = 0,1,2 ^M
integer, allocatable :: isbce(:)
integer, allocatable :: nbvt(:,:)
!dimensions
integer :: N_lev
integer :: N_lay
integer :: N_verts
integer :: N_elems
integer :: Max_Elems
!mesh
integer :: iunit,ios
logical :: mesh_setup = .false.
real(sp), pointer :: xm(:),xm0(:)
real(sp), pointer :: ym(:),ym0(:)
real(sp), pointer :: xc(:),xc0(:)
real(sp), pointer :: yc(:),yc0(:)
real(sp), pointer :: hm(:)
real(sp), pointer :: aw0(:,:)
real(sp), pointer :: awx(:,:)
real(sp), pointer :: awy(:,:)
integer, pointer :: tri(:,:)
integer, pointer :: ntve(:)
integer, pointer :: nbve(:,:)
real(sp), pointer :: siglay(:,:)
real(sp), pointer :: siglev(:,:)
real(sp), pointer :: esiglay(:,:)
real(sp), pointer :: esiglev(:,:)
!added by Xinyou
real(sp), pointer :: art(:)
real(sp), pointer :: a1u(:,:)
real(sp), pointer :: a2u(:,:)
character(len=10) :: x_char,y_char,h_char,u_char,v_char, &
kh_char,viscofm_char,ua_char,va_char,nv_char,nbe_char,aw0_char,awx_char,awy_char,a1u_char, &
a2u_char,art_char,nele_char,node_char,zeta_char,omega_char, &
siglay_char,siglev_char,wu_char,wv_char
Namelist /NML_NCVAR/ &
x_char, &
y_char, &
h_char, &
nv_char, &
nele_char, &
node_char, &
nbe_char, &
aw0_char, &
awx_char, &
awy_char, &
a1u_char, &
a2u_char, &
art_char, &
siglay_char, &
siglev_char, &
ua_char, &
va_char, &
zeta_char, &
h_char, &
wu_char, &
wv_char, &
u_char, &
v_char, &
omega_char, &
kh_char, &
viscofm_char
!
logical :: grid_metrics
interface interp
module procedure interp_float2D
module procedure interp_float3D
end interface
interface interp_from_nodes
module procedure interp_flt_from_nodes
end interface
contains
!----------------------------------------------------
! Read the mesh and interpolation coefficients
! Add mesh to output files for viz
!----------------------------------------------------
subroutine ocean_model_init(ng,g,lsize,varlist)
use utilities, only : drawline,cfcheck
integer, intent(in) :: ng
type(igroup), intent(in) :: g(ng)
integer, intent(inout) :: lsize
character(len=*) :: varlist(max_state_vars)
!----------------------------------------------------
character(len=mstr) :: msg
integer :: dimid,varid,fid
character(len=fstr) :: dname
integer :: subset(3)
integer :: i,n,ierr,ofid,k
integer :: x_vid,y_vid,h_vid,nv_vid,nele_did,node_did,three_did,zeta_vid
!return if group spatial dimensions are all 0-d or 1-d
if(maxval(g%space_dim) < 2)return
!get the forcing file netcdf id
fid = get_ncfid()
!add required time dependent variables to the list
!--------------------------------------------------------
! open and read time dependent variables namelist: nml_ncvar
!--------------------------------------------------------
open(unit=iunit,file=trim(runcontrol),form='formatted')
read(unit=iunit,nml=nml_ncvar,iostat=ios)
if(ios /= 0)then
write(*,*)'fvcom:fatal error: could not read fiscm namelist from',trim(runcontrol)
stop
endif
do n=1,ng
if(g(n)%space_dim == 2)then
lsize = lsize + 1 ; varlist(lsize) = ua_char
lsize = lsize + 1 ; varlist(lsize) = va_char
lsize = lsize + 1 ; varlist(lsize) = zeta_char
lsize = lsize + 1 ; varlist(lsize) = h_char
if (wind_type == 1)then
lsize = lsize + 1 ; varlist(lsize) = wu_char
lsize = lsize + 1 ; varlist(lsize) = wv_char
endif
elseif(g(n)%space_dim ==3)then
lsize = lsize + 1 ; varlist(lsize) = u_char
lsize = lsize + 1 ; varlist(lsize) = v_char
lsize = lsize + 1 ; varlist(lsize) = zeta_char
lsize = lsize + 1 ; varlist(lsize) = omega_char
lsize = lsize + 1 ; varlist(lsize) = h_char
if (wind_type == 1)then
lsize = lsize + 1 ; varlist(lsize) = wu_char
lsize = lsize + 1 ; varlist(lsize) = wv_char
endif
lsize = lsize + 1 ; varlist(lsize) = kh_char
if (g(n)%hdiff_type ==2)then
lsize = lsize + 1 ; varlist(lsize) = viscofm_char
endif
endif
end do
!determine number of elements
msg = "dimension 'nele' not in the netcdf dataset"
call ncdchk(nf90_inq_dimid(fid, nele_char, dimid ),msg)
call ncdchk(nf90_inquire_dimension(fid, dimid, dname, N_elems ))
!determine number of nodes
msg = "dimension 'node' not in the netcdf dataset"
call ncdchk(nf90_inq_dimid(fid, node_char, dimid ),msg)
call ncdchk(nf90_inquire_dimension(fid, dimid, dname, N_verts ))
!determine number of layers
msg = "dimension 'siglay' not in the netcdf dataset"
call ncdchk(nf90_inq_dimid(fid, siglay_char, dimid ),msg)
call ncdchk(nf90_inquire_dimension(fid, dimid, dname, N_lay ))
N_lev = N_lay + 1
!allocate dataspace
allocate(xm(N_verts))
allocate(ym(N_verts))
allocate(xm0(N_verts))
allocate(ym0(N_verts))
allocate(xc0(N_elems))
allocate(yc0(N_elems))
allocate(xc(N_elems))
allocate(yc(N_elems))
allocate(hm(N_verts))
allocate(aw0(N_elems,3)) ; aw0 = a3rd
allocate(awx(N_elems,3)) ; awx = zero
allocate(awy(N_elems,3)) ; awy = zero
allocate(tri(N_elems,3))
allocate(nbe(N_elems,3))
allocate(siglay(N_verts,N_lay))
allocate(siglev(N_verts,N_lev))
allocate(esiglay(N_elems,N_lay))
allocate(esiglev(N_elems,N_lev))
allocate(a2u(N_elems,4)) ;a2u = zero
allocate(a1u(N_elems,4)) ;a1u = zero
allocate(art(N_verts)) ;art = zero
!----------------Node, Boundary Condition, and Control Volume-----------------------!
!ALLOCATE(NBE(0:N_elems,3)) ;NBE = 0 !!INDICES OF ELEMENT NEIGHBORS
ALLOCATE(NTVE(0:N_verts)) ;NTVE = 0
ALLOCATE(ISONB(0:N_verts)) ;ISONB = 0 !!NODE MARKER = 0,1,2
ALLOCATE(ISBCE(0:N_elems)) ;ISBCE = 0
!read in mesh
msg = "error reading x coordinate"
call ncdchk( nf90_inq_varid(fid,x_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, xm),msg)
msg = "error reading y coordinate"
call ncdchk( nf90_inq_varid(fid,y_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, ym),msg)
msg = "error reading h coordinate"
call ncdchk( nf90_inq_varid(fid,h_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, hm),msg)
msg = "error reading nv coordinate"
call ncdchk( nf90_inq_varid(fid,nv_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, tri),msg)
if(ncdscan( nf90_inq_varid(fid,nbe_char,varid),msg ) )then
msg = "error reading nbe"
call ncdchk( nf90_inq_varid(fid,nbe_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, nbe),msg)
else
write(*,*)'WARNING:::::: NBE is not in the forcing file'
write(*,*)'will try to compute internally'
endif
msg = "error reading aw0"
!read aw0 if they exist, otherwise use 1st order interpolation
if(ncdscan( nf90_inq_varid(fid,aw0_char,varid),msg ) )then
call ncdchk(nf90_get_var(fid, varid, aw0),msg)
msg = "error reading awx"
call ncdchk( nf90_inq_varid(fid,awx_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, awx),msg)
msg = "error reading awy"
call ncdchk( nf90_inq_varid(fid,awy_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, awy),msg)
msg = "error reading a1u"
call ncdchk( nf90_inq_varid(fid,a1u_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, a1u),msg)
msg = "error reading a2u"
call ncdchk( nf90_inq_varid(fid,a2u_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, a2u),msg)
else
write(*,*)'WARNING:::::: AW0 AWX AWY Do NOT exist in forcing file'
write(*,*)'Proceeding with: 1st Order interpolation'
write(*,*)'AW0 = 1/3; AWX=AWY = 0'
write(*,*)'In the future, select [grid metrics] in your NetCDF namelist'
endif
msg = "error reading art1"
if(ncdscan( nf90_inq_varid(fid,art_char,varid),msg ) )then
call ncdchk(nf90_get_var(fid, varid, art),msg)
msg = "error reading art1"
else
write(*,*)'Error reading art1 => needed for Okubo horizontal diffusivity'
write(*,*)'In the future, select [grid metrics] in your NetCDF namelist'
endif
msg = "error reading siglay"
call ncdchk( nf90_inq_varid(fid,siglay_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, siglay),msg)
msg = "error reading siglev"
call ncdchk( nf90_inq_varid(fid,siglev_char,varid),msg )
call ncdchk(nf90_get_var(fid, varid, siglev),msg)
!read secondary connectivity (nbve/ntve)
grid_metrics = .false.
msg = "dimension 'maxelem' not in the netcdf dataset"
if(ncdscan( nf90_inq_dimid(fid,'maxelem',dimid),msg ) )then
call ncdchk(nf90_inquire_dimension(fid, dimid, dname, Max_Elems))
allocate(ntve(N_verts))
allocate(nbve(N_verts,Max_Elems))
msg = "error reading ntve"
call ncdchk( nf90_inq_varid(fid,'ntve',varid),msg )
call ncdchk(nf90_get_var(fid, varid, ntve),msg)
msg = "error reading nbve"
call ncdchk( nf90_inq_varid(fid,'nbve',varid),msg )
call ncdchk(nf90_get_var(fid, varid, nbve),msg)
grid_metrics = .true.
else
! Revised by Xinyou Lin in Jan ,2009
! write(*,*)'WARNING:::::: NTVE/NBVE Do NOT exist in forcing file'
! write(*,*)'This will slow down the element search procedure'
! write(*,*)'Proceeding with: 1st Order interpolation'
! write(*,*)'In the future, select [grid metrics] in your NetCDF namelist'
endif
!calculate cell center coordinates
if(spherical == 1)then
do i=1,N_verts
xm0(i) = xm(i)
ym0(i) = ym(i)
if(xm0(i) >= 0.0_sp .and. xm0(i) <=180.0_sp)then
xm0(i) = xm0(i) + 180.0_sp
elseif( xm0(i) > 180.0_sp .and. xm0(i) <=360.0_sp) then
xm0(i) = xm0(i) - 180.0_sp
endif
enddo
endif
do i=1,N_elems
subset = tri(i,1:3)
xc(i) = a3rd*(sum(xm(subset)))
yc(i) = a3rd*(sum(ym(subset)))
if(spherical == 1)then
xc0(i) = a3rd*(sum(xm0(subset)))
yc0(i) = a3rd*(sum(ym0(subset)))
endif
end do
!calculate cell-center siglay/siglev
do i=1,N_elems
subset = tri(i,1:3)
do k=1,N_lay
esiglay(i,k) = a3rd*(sum(siglay(subset,k)))
esiglev(i,k) = a3rd*(sum(siglev(subset,k)))
end do
esiglev(i,N_lev) = a3rd*(sum(siglev(subset,N_lev)))
end do
! calculate secondary connectivity (nbve/ntve)
!determine nbve/ntve - secondary connectivity, used
!for searching element containing point
!mark boundary elements
!CALL TRIANGLE_GRID_EDGE
grid_metrics = .true.
!calculate node-based interpolation coefficients
call drawline('-')
write(*,*)'FVCOM mesh stats '
call drawline('-')
write(*,*)'Number of elements:: ',N_elems
write(*,*)'Number of nodes :: ',N_verts
write(*,*)'Number of sig levs:: ',N_lev
write(*,*)'xmin :: ',minval(xm)
write(*,*)'xmax :: ',maxval(xm)
write(*,*)'ymin :: ',minval(ym)
write(*,*)'ymax :: ',maxval(ym)
!flag that mesh is setup
mesh_setup = .true.
!------------------------------------------------------
!dump mesh to mesh.nc for viz
!------------------------------------------------------
call cfcheck( nf90_create("mesh.nc",nf90_clobber,ofid) )
!dimensions
call cfcheck(nf90_def_dim(ofid,"nele",N_elems, nele_did) )
call cfcheck(nf90_def_dim(ofid,"node",N_verts, node_did) )
call cfcheck(nf90_def_dim(ofid,"three",3, three_did) )
!x
call cfcheck( nf90_def_var(ofid,"x",nf90_float,(/node_did/), x_vid) )
call cfcheck( nf90_put_att(ofid, x_vid,"long_name","nodal x-coordinate") )
call cfcheck( nf90_put_att(ofid, x_vid,"units","meters") )
call cfcheck( nf90_put_att(ofid, x_vid,"grid","TWOD_MESH") )
!y
call cfcheck( nf90_def_var(ofid,"y",nf90_float,(/node_did/), y_vid) )
call cfcheck( nf90_put_att(ofid, y_vid,"long_name","nodal y-coordinate") )
call cfcheck( nf90_put_att(ofid, y_vid,"units","meters") )
call cfcheck( nf90_put_att(ofid, y_vid,"grid","TWOD_MESH") )
!h
call cfcheck( nf90_def_var(ofid,"h",nf90_float,(/node_did/), h_vid) )
call cfcheck( nf90_put_att(ofid, h_vid,"long_name","Bathymetry") )
call cfcheck( nf90_put_att(ofid, h_vid,"units","meters") )
call cfcheck( nf90_put_att(ofid, h_vid,"positive","down") )
call cfcheck( nf90_put_att(ofid, h_vid,"standard_name","depth") )
call cfcheck( nf90_put_att(ofid, h_vid,"grid","fvcom_grid") )
!el
call cfcheck( nf90_def_var(ofid,"zeta",nf90_float,(/node_did/), zeta_vid) )
call cfcheck( nf90_put_att(ofid, zeta_vid,"long_name","Water Surface Elevation") )
call cfcheck( nf90_put_att(ofid, zeta_vid,"units","meters") )
call cfcheck( nf90_put_att(ofid, zeta_vid,"positive","up") )
call cfcheck( nf90_put_att(ofid, zeta_vid,"standard_name","sea_surface_elevation") )
call cfcheck( nf90_put_att(ofid, zeta_vid,"type","data") )
!nv
call cfcheck( nf90_def_var(ofid,"nv",nf90_int,(/nele_did,three_did/), nv_vid) )
call cfcheck( nf90_put_att(ofid, nv_vid,"long_name","nodes surrounding element") )
!globals
call cfcheck( nf90_put_att(ofid,nf90_global,"source" ,"FVCOM") )
call cfcheck( nf90_put_att(ofid,nf90_global,"Conventions" ,"CF-1.0") )
call cfcheck( nf90_enddef(ofid) )
call cfcheck( nf90_put_var(ofid,x_vid,xm))
call cfcheck( nf90_put_var(ofid,y_vid,ym))
call cfcheck( nf90_put_var(ofid,h_vid,hm))
call cfcheck( nf90_put_var(ofid,zeta_vid,hm*0.0))
call cfcheck( nf90_put_var(ofid,nv_vid,tri,START=(/1,1/)))
!close the file
call cfcheck( nf90_close(ofid) )
end subroutine ocean_model_init
!----------------------------------------------------
! Random-Walk horizontal diffusion with constant
! turbulent eddy diffusivity
!
! m. huret use a Gauss dist. , this probably wont
! converge to the correct continuous form of diffusion
! here we will use a uniform random walk
!----------------------------------------------------
subroutine rw_hdiff_constant(g, dT)
use utilities, only : normal,unitrand
type(igroup), intent(inout) :: g
real(sp), intent(in) :: dT
real(sp), pointer :: x(:)
real(sp), pointer :: y(:)
integer, pointer :: istatus(:)
integer, pointer :: cell(:)
integer :: i,p,np
real(sp) :: tscale
real(sp), allocatable :: pdxt(:), pdyt(:)
!set pointers to x,y particle positions
call get_state('x',g,x)
call get_state('y',g,y)
call get_state('status',g,istatus)
call get_state('cell',g,cell)
!set dimensions for loops and time step
np = g%nind
allocate(pdxt(np)) ;
allocate(pdyt(np)) ;
!set diffusive time scale
tscale = sqrt(2.*dT*g%hdiff_const_val)
!horizontal random walk
if(spherical == 0 )then
do p=1,np
if(istatus(p)==ACTIVE)then
pdxt(p) = x(p) + normal()*tscale
pdyt(p) = y(p) + normal()*tscale
else
pdxt(p) = x(p)
pdyt(p) = y(p)
endif
end do
elseif (spherical == 1)then
do p=1,np
if(istatus(p)==ACTIVE)then
pdxt(p) = x(p) + normal()*tscale/(tpi*COS(y(p)) + 1.0E-6)
pdyt(p) = y(p) + normal()*tscale/tpi
else
pdxt(p) = x(p)
pdyt(p) = y(p)
endif
end do
where( pdxt < 0.0_SP)
pdxt = pdxt + 360.0_SP
end where
where( pdxt > 360.0_SP)
pdxt = pdxt - 360.0_SP
end where
where( pdyt > 90.0_SP)
pdyt = 180.0_SP - pdyt
end where
where( pdyt < -90.0_SP)
pdyt = - 180.0_SP - pdyt
end where
endif !spherical
call find_element(np,pdxt,pdyt,cell,istatus)
!!--Update Only Particle Still in Water
where(istatus==ACTIVE)
x = pdxt
y = pdyt
end where
!!--reset position of particles which are lost from domain to last known position
where(istatus==EXITED)
istatus=ACTIVE
end where
!nullify pointers
nullify(x)
nullify(y)
nullify(istatus)
nullify(cell)
deallocate(pdxt)
deallocate(pdyt)
end subroutine rw_hdiff_constant
!----------------------------------------------------
! Random-Walk horizontal diffusion with spatially
! variable turbulent eddy diffusivity
!
! => Eddy diffusivity from Okubo based on local
! grid scale in the model
! Okubo, 1971 Ocean Diffusion Diagrams,
! Deep Sea Research, V18, 789-802.
! (see Equation 4 - page 797)
!
! Use Visser's naive random walk to compute step
!----------------------------------------------------
subroutine rw_hdiff_okubo(g, dT)
use utilities, only : normal
type(igroup), intent(inout) :: g
real(sp), intent(in) :: dT
!----------------------------
integer, pointer :: istatus(:)
integer, pointer :: cell(:)
real(sp), pointer :: x(:)
real(sp), pointer :: y(:)
real(sp), allocatable :: pdxt(:), pdyt(:)
integer :: p,np,n1,n2,n3,icell
real(sp) :: l,tarea,tscale,Ah
!set problem size and time step
np = g%nind
!set pointers to particle positions and status
call get_state('status',g,istatus)
call get_state('cell',g,cell)
call get_state('x',g,x)
call get_state('y',g,y)
!allocate local data
allocate(pdxt(np)) ; pdxt = x
allocate(pdyt(np)) ; pdyt = y
! loop over particles and compute eddy diffusivity using Okubo, 1971
do p=1,np
icell = cell(p)
if(istatus(p) < 1 .or. icell == 0)cycle
n1 = tri(icell,1)
n2 = tri(icell,2)
n3 = tri(icell,3)
tarea = aw0(icell,1)*art(n1)+aw0(icell,2)*art(n2)+aw0(icell,3)*art(n3)
l = (tarea**.5)*100 !grid lengthscale in cm
Ah = 0.0103*(l**1.15)/(100.*100.) !Okubo horizontal diffusivity in m^2/s
tscale = sqrt(2.*dT*Ah)
!take random walk in x and y
pdxt(p) = x(p) + normal()*tscale
pdyt(p) = y(p) + normal()*tscale
end do
call find_element(np,pdxt,pdyt,cell,istatus)
!!--Update Only Particle Still in Water
where(istatus==ACTIVE)
x = pdxt
y = pdyt
end where
!!--reset position of particles which are lost from domain to last known position
where(istatus==EXITED)
istatus=ACTIVE
end where
!deallocate pointers
deallocate(pdxt)
deallocate(pdyt)
nullify(x)
nullify(y)
nullify(istatus)
nullify(cell)
end subroutine rw_hdiff_okubo
!----------------------------------------------------
! Random-Walk horizontal diffusion with spatially
! variable turbulent eddy diffusivity
!
! => Eddy diffusivity from the model (viscofm)
!
! Use Visser's naive random walk to compute step
!----------------------------------------------------
subroutine rw_hdiff_variable(g, dT)
use utilities, only : normal
type(igroup), intent(inout) :: g
real(sp), intent(in) :: dT
!----------------------------
integer, pointer :: istatus(:)
integer, pointer :: cell(:)
real(sp), pointer :: x(:)
real(sp), pointer :: y(:)
real(sp), pointer :: s(:)
real(sp), allocatable :: Ah(:), pdxt(:), pdyt(:)
real(sp), allocatable :: tscale(:)
integer :: i,np
!set problem size and time step
np = g%nind
!set pointers to particle positions and status
call get_state('status',g,istatus)
call get_state('cell',g,cell)
call get_state('x',g,x)
call get_state('y',g,y)
call get_state('s',g,s)
!allocate local data
allocate(tscale(np)) ; tscale = zero
allocate(pdxt(np)) ;
allocate(pdyt(np)) ;
allocate(Ah(np)) ; Ah = zero
call interp(np,x,y,s,cell,istatus,viscofm_char,Ah,3)
tscale = sqrt(2.*dT*Ah)
!update particle position using Visser modified random walk
!horizontal random walk
if(spherical == 0 )then
where(istatus == ACTIVE)
pdxt = x + normal()*tscale
pdyt = y + normal()*tscale
end where
elseif (spherical == 1)then
where(istatus == ACTIVE)
pdxt = x + normal()*tscale/(tpi*COS(y) + 1.0E-6)
pdyt = y + normal()*tscale/tpi
end where
where( pdxt < 0.0_SP)
pdxt = pdxt + 360.0_SP
end where
where( pdxt > 360.0_SP)
pdxt = pdxt - 360.0_SP
end where
where( pdyt > 90.0_SP)
pdyt = 180.0_SP - pdyt
end where
where( pdyt < -90.0_SP)
pdyt = - 180.0_SP - pdyt
end where
endif
call find_element(np,pdxt,pdyt,cell,istatus)
!!--Update Only Particle Still in Water
where(istatus==ACTIVE)
x = pdxt
y = pdyt
end where
!!--reset position of particles which are lost from domain to last known position
where(istatus==EXITED)
istatus=ACTIVE
end where
!deallocate workspace and nullify pointers
deallocate(Ah)
deallocate(tscale)
deallocate(pdxt)
deallocate(pdyt)
nullify(x)
nullify(y)
nullify(s)
nullify(istatus)
nullify(cell)
end subroutine rw_hdiff_variable
!----------------------------------------------------
! Random-Walk vertical diffusion
!
! - use eddy diffusivity from the model (kh)
! - use Vissers modified random walk to compute jump
!----------------------------------------------------
subroutine rw_vdiff(g, dT, nstep)
use utilities, only : normal,unitrand,ran1
type(igroup), intent(inout) :: g
real(sp), intent(in) :: dT
integer, intent(in) :: nstep
!----------------------------
integer, pointer :: istatus(:)
integer, pointer :: cell(:)
real(sp), pointer :: x(:)
real(sp), pointer :: y(:)
real(sp), pointer :: s(:)
real(sp), pointer :: z(:)
real(sp), pointer :: h(:)
real(sp), allocatable :: kh(:)
real(sp), allocatable :: kh2(:)
real(sp), allocatable :: ds(:)
real(sp), allocatable :: dkh_ds(:)
real(sp), allocatable :: zeta(:)
real(sp), allocatable :: s_shift(:)
real(sp), parameter :: delta_s = 0.001
real(sp) :: deltaT,fac,randy,dz,depth,dkh_dz,vsink
integer :: n,p,np
!set problem size and time step
vsink = g%vsink
np = g%nind
deltaT = dT/float(nstep)
!set pointers to particle positions and status
call get_state('status',g,istatus)
call get_state('cell',g,cell)
call get_state('x',g,x)
call get_state('y',g,y)
call get_state('s',g,s)
call get_state('z',g,z)
call get_state('h',g,h)
!allocate local data
allocate(s_shift(np)); s_shift = zero
allocate(zeta(np)); zeta = zero
allocate(kh(np)) ; kh = zero
allocate(kh2(np)) ; kh2 = zero
allocate(ds(np)) ; ds = zero
allocate(dkh_ds(np)) ; dkh_ds = zero
!set constants
fac = (2./rvar)*deltaT ![ 2*r^-1*deltaT], r = variance of uniform rw, set in gparms
call interp(np,x,y,cell,istatus,h_char,h,3)
call interp(np,x,y,cell,istatus,zeta_char,zeta,3)
! ==> loop over substeps
do n=1,nstep
!--------------------------------------------------
! calculate d(kh)/d(s) - brute force
!--------------------------------------------------
!set derivative step (backward near free surface)
ds = delta_s
where(s+delta_s > 0)ds = -delta_s
!evaluate kh at both locations
call interp(np,x,y,s,cell,istatus,kh_char,kh,3)
call interp(np,x,y,s+ds,cell,istatus,kh_char,kh2,3)
!form the derivative d(kh)/d(s)
dkh_ds = (kh2-kh)/ds
!function evaluation at [z + 0.5*dkh/dz*deltat] - Visser
s_shift = s + ahalf*dkh_ds*deltaT/((h+zeta)**2)
call interp(np,x,y,s_shift,cell,istatus,kh_char,kh,3)
! => main loop over particles
!gwc do p=1,1000
!gwc write(33,*)normal(),ran1()
!gwc end do
!gwc stop
do p=1,np
if(istatus(p) < 1)cycle
!update particle position using Visser modified random walk
depth = h(p)+zeta(p)
dkh_dz = dkh_ds(p)/depth
dz = dkh_dz*deltaT + normal(-deltaT*vsink*depth,1.0d0)*sqrt(fac*kh(p)) !Visser-modified
! dz = dkh_dz*deltaT + unitrand()*sqrt(fac*kh(p)) !Visser-modified
! dz = 2*unitrand()*sqrt(2.*3*kh(p)*deltaT) !naive unitrand
! dz = normal(-deltaT*.001*depth,1.d0)*sqrt(2.*kh(p)*deltaT) !naive normal
s(p) = s(p) + dz/depth
!set boundary conditions at free surface and bottom
s(p) = max(s(p),-(2.0+s(p))) !reflect off bottom
s(p) = min(s(p),0.0) !don't pierce free surface
end do
! <= end particle loop
end do
! <== end loop over substeps
!--Calculate Particle Location in Cartesian Vertical Coordinate----------------!
z = s*(h+zeta) + zeta
!deallocate workspace and nullify pointers
deallocate(ds)
deallocate(kh)
deallocate(kh2)
deallocate(dkh_ds)
deallocate(zeta)
deallocate(s_shift)
nullify(x)
nullify(y)
nullify(s)
nullify(h)
nullify(z)
nullify(istatus)
end subroutine rw_vdiff
!----------------------------------------------------
! Random-Walk vertical diffusion using splines
! - spline vertical diffusivity to smooth profile
! - adjust at ends
! - use vissers modified random walk to compute jump
!
! Follow M. Huret's Implementation for the spline
!----------------------------------------------------
subroutine rw_vdiff_splined(g, dT, nstep)
type(igroup), intent(inout) :: g
real(sp), intent(in) :: dT
integer, intent(in) :: nstep
real(sp) :: kh(N_lev)
integer, pointer :: istatus(:)
integer :: n,p,np
call get_state('status',g,istatus)
np = g%nind
! => main loop over particles
do p=1,np
if(istatus(p) < 1)cycle
!interpolate eddy diffusivity at model control points in column
!spline eddy diffusivity - assume constant during step
!loop over substeps
do n=1,nstep
!evaluate eddy diffusivity value and gradient at x,y,s
!calculate random step using Visser formulation
!use a random mixed layer, 1m from top and bottom
end do
end do
! <= end main particle loop
end subroutine rw_vdiff_splined
!----------------------------------------------------
! Random-Walk vertical diffusion using bins
!
! - Thygesen and Adlandsvik, MEPS, v347, 2007
!----------------------------------------------------
subroutine rw_vdiff_binned(g, dT, nstep)
type(igroup), intent(inout) :: g
real(sp), intent(in) :: dT
integer, intent(in) :: nstep
end subroutine rw_vdiff_binned
subroutine sz_trans(np,g)
integer, intent(in) :: np
type(igroup), intent(inout) :: g
real(sp), pointer :: x(:)
real(sp), pointer :: y(:)
real(sp), pointer :: s(:)
real(sp), pointer :: z(:)
real(sp), pointer :: h(:)
real(sp), pointer :: zeta(:)
integer , pointer :: cell(:)
integer , pointer :: istatus(:)
call get_state('x',g,x)
call get_state('y',g,y)
call get_state('s',g,s)
call get_state('z',g,z)
call get_state('h',g,h)
call get_state('zeta',g,zeta)
call get_state('status',g,istatus)
call get_state('cell',g,cell)
call interp(np,x,y,cell,istatus,zeta_char,zeta,3)
call interp(np,x,y,cell,istatus,h_char,h,3)
if(sz_cor == 1)then
! z = -z + zeta
s = (z - zeta)/(h + zeta)
elseif(sz_cor == 0)then
z = s*(h + zeta) + zeta
endif
nullify(x)
nullify(y)
nullify(s)
nullify(z)
nullify(h)
nullify(cell)
nullify(zeta)
nullify(istatus)
end subroutine sz_trans
!---------------------------------------------------
! 2-D Advection
!----------------------------------------------------
subroutine advect2D(g,deltaT,np)
integer, intent(in) :: np
integer :: k,i,ns
type(igroup), intent(inout) :: g
real(sp), intent(in) :: deltaT
real(sp), pointer :: x(:)
real(sp), pointer :: y(:)
real(sp), pointer :: h(:)
integer , pointer :: cell(:)
integer , pointer :: istatus(:)
real(sp), dimension(np) :: u,u1,u2,v,v1,v2
real(sp), dimension(np) :: pdx,pdy
real(sp), dimension(np) :: pdxt,pdyt
real(sp), dimension(np,0:mstage) :: chix,chiy
real(sp), parameter :: eps = 1.0E-5
!!!!!!!
!set dimensions for loops and time step
!np = g%nind
!set pointers to states
call get_state('x',g,x)
call get_state('y',g,y)
call get_state('h',g,h)
call get_state('cell',g,cell)
call get_state('status',g,istatus)
!--Initialize Stage Functional Evaluations
chix = 0.0_sp
chiy = 0.0_sp
!--Loop over RK Stages
do ns=1,mstage
!!Particle Position at Stage N (x,y,sigma)
pdx(:) = x(:) + a_rk(ns)*deltaT*chix(:,ns-1)
pdy(:) = y(:) + a_rk(ns)*deltaT*chiy(:,ns-1)
!!Calculate Velocity Field for Stage N Using C_RK Coefficients
!interpolate velocity field to particle position
call interp(np,pdx,pdy,cell,istatus,ua_char,u1,3)
call interp(np,pdx,pdy,cell,istatus,va_char,v1,3)
call interp(np,pdx,pdy,cell,istatus,ua_char,u2,4)
call interp(np,pdx,pdy,cell,istatus,va_char,v2,4)
u = (1.0_sp-c_rk(ns))*u1 + c_rk(ns)*u2
v = (1.0_sp-c_rk(ns))*v1 + c_rk(ns)*v2