-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMOM_particles.F90
1480 lines (1266 loc) · 54 KB
/
MOM_particles.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
module MOM_particles_mod
use constants_mod, only: radius, pi, omega, HLF
use MOM_grid, only : ocean_grid_type
use MOM_time_manager, only : time_type, get_date, operator(-)
use MOM_variables, only : thermo_var_ptrs
use fms_mod, only: field_exist, get_global_att_value
use fms_mod, only: stdlog, stderr, error_mesg, FATAL, WARNING
use fms_mod, only: write_version_number, read_data, write_data, file_exist
use mpp_mod, only: mpp_npes, mpp_pe, mpp_root_pe, mpp_sum, mpp_min, mpp_max, NULL_PE
use mpp_mod, only: mpp_send, mpp_recv, mpp_sync_self, mpp_chksum
use mpp_mod, only: mpp_clock_begin, mpp_clock_end, mpp_clock_id
use mpp_mod, only: CLOCK_COMPONENT, CLOCK_SUBCOMPONENT, CLOCK_LOOP
use mpp_mod, only: mpp_gather
use fms_mod, only: clock_flag_default
use fms_io_mod, only: get_instance_filename
use mpp_domains_mod, only: domain2D, mpp_update_domains, mpp_define_domains
use mpp_parameter_mod, only: CGRID_NE, BGRID_NE, CORNER, AGRID
use mpp_domains_mod, only: mpp_get_compute_domain, mpp_get_data_domain
use mpp_domains_mod, only: mpp_get_neighbor_pe, NORTH, SOUTH, EAST, WEST
use diag_manager_mod, only: send_data
use MOM_particles_framework, only: particles_framework_init
use MOM_particles_framework, only: particles_gridded, xyt, particle, particles, buffer
use MOM_particles_framework, only: verbose, really_debug,debug,use_roundoff_fix
use MOM_particles_framework, only: find_cell,find_cell_by_search,count_parts,is_point_in_cell,pos_within_cell
use MOM_particles_framework, only: bilin,yearday,count_parts,parts_chksum
use MOM_particles_framework, only: find_u, find_v
use MOM_particles_framework, only: checksum_gridded,add_new_part_to_list
use MOM_particles_framework, only: send_parts_to_other_pes,move_trajectory,move_all_trajectories
use MOM_particles_framework, only: record_posn,check_position,print_part,print_parts,print_fld
use MOM_particles_framework, only: add_new_part_to_list,delete_particle_from_list,destroy_particle
use MOM_particles_framework, only: grd_chksum2,grd_chksum3
use MOM_particles_framework, only: offset_part_dates
use MOM_particles_framework, only: count_parts_in_list,list_chksum
use MOM_particles_framework, only: monitor_a_part,move_part_between_cells, update_halo_particles
use MOM_particles_framework, only: is_point_within_xi_yj_bounds
use MOM_particles_framework, only: find_layer, find_depth
use MOM_particles_io, only: particles_io_init,write_restart,write_trajectory
use MOM_particles_io, only: read_restart_parts, particles_io_init
implicit none ; private
public particles_init !, particles_end, particles_run, particles_stock_pe, particles
public particles_end, particles_run, particles
public particles_save_restart
public particles_to_z_space, particles_to_k_space
real, parameter :: pi_180=pi/180. !< Converts degrees to radians
real, parameter :: r180_pi=180./pi !< Converts radians to degrees
real, parameter :: Rearth=6360000. !< Radius of earth (m)
#ifdef _FILE_VERSION
character(len=128) :: version = _FILE_VERSION
#else
character(len=128) :: version = 'unknown'
#endif
contains
! ##############################################################################
subroutine particles_init(parts, Grid, Time, dt, u, v, h)
type(particles), pointer, intent(out) :: parts
type(ocean_grid_type), target, intent(in) :: Grid !< Grid type from parent model
type(time_type), intent(in) :: Time !< Time type from parent model
real, intent(in) :: dt !< particle timestep in seconds
real, dimension(:,:,:),intent(in) :: u, v !< Horizontal velocity fields
real, dimension(:,:,:),intent(in) :: h !< Thickness of layers
integer :: io_layout(2)
integer :: stdlogunit, stderrunit
! Get the stderr and stdlog unit numbers
stderrunit=stderr()
stdlogunit=stdlog()
write(stdlogunit,*) "particles: "//trim(version)
call particles_framework_init(parts, Grid, Time, dt)
call particles_to_z_space(parts,h)
call mpp_clock_begin(parts%clock_ior)
call particles_io_init(parts,Grid%Domain%io_layout)
call read_restart_parts(parts,Time, u, v,h)
call parts_chksum(parts, 'read_restart_particles')
call mpp_clock_end(parts%clock_ior)
if (really_debug) call print_parts(stderrunit,parts,'particles_init, initial status')
end subroutine particles_init
subroutine interp_flds(grd, i, j, k, xi, yj, uo, vo, x ,y)
! Arguments
type(particles_gridded), pointer :: grd
integer, intent(in) :: i, j
real, intent(in) :: k
real, intent(in) :: xi, yj
real, intent(in) :: x, y
real, intent(out) :: uo, vo
! Local variables
real :: cos_rot, sin_rot
real :: hxm, hxp
integer :: kint
real :: xiu,yjv
real :: dx_dlon, dy_dlat
integer :: iu, jv
kint = ceiling(k)
call convert_from_grid_to_meters(y, grd%grid_is_latlon,grd%grid_is_regular, dx_dlon, dy_dlat)
cos_rot=bilin(grd, grd%cos, i, j, xi, yj) ! If true, uses the inverted bilin function
sin_rot=bilin(grd, grd%sin, i, j, xi, yj)
yjv=yj+0.5
if (yjv>1) then
yjv=yjv-1.
jv=j+1
else
jv=j
endif
!uo=linlinx(grd, grd%uo(:,:,kint), i+1, j, xi,yj)
uo=find_u(grd, grd%uo(grd%isd:grd%ied+1,grd%jsd:grd%jed+1,kint), x, y, i+1, j, xi, yj, dx_dlon, dy_dlat)
xiu = xi+0.5
if (xiu>1) then
xiu= xiu-1.
iu=i+1
else
iu=i
endif
vo=find_v(grd, grd%vo(grd%isd:grd%ied+1,grd%jsd:grd%jed+1,kint), x, y, i, j+1, xi, yj, dx_dlon, dy_dlat)
!vo=linliny(grd, grd%vo(:,:,kint), i, j+1, xi, yj)
! Rotate vectors from local grid to lat/lon coordinates
call rotate(uo, vo, cos_rot, sin_rot)
contains
subroutine rotate(u, v, cos_rot, sin_rot)
! Arguments
real, intent(inout) :: u, v
real, intent(in) :: cos_rot, sin_rot
! Local variables
real :: u_old, v_old
u_old=u
v_old=v
u=cos_rot*u_old+sin_rot*v_old
v=cos_rot*v_old-sin_rot*u_old
end subroutine rotate
end subroutine interp_flds
! ##############################################################################
!> The main driver the steps updates particles
subroutine particles_run(parts, time, uo, vo, ho, tv, dt_adv, use_uh)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
type(time_type), intent(in) :: time !< Model time
real, dimension(:,:,:),intent(in) :: uo !< Ocean zonal velocity (m/s)
real, dimension(:,:,:),intent(in) :: vo !< Ocean meridional velocity (m/s)
real, dimension(:,:,:),intent(in) :: ho !< Ocean layer thickness [H ~> m or kg m-2]
type(thermo_var_ptrs), intent(in) :: tv !< structure containing pointers to available thermodynamic fields
logical :: use_uh !<use uh rather than u
! Local variables
integer :: iyr, imon, iday, ihr, imin, isec, k
type(particles_gridded), pointer :: grd
logical :: lerr, sample_traj, write_traj, lverbose
real :: grdd_u_particle, grdd_v_particle
integer :: i, j, Iu, ju, iv, Jv, Iu_off, ju_off, iv_off, Jv_off
real :: mask
real :: dt_adv
real, dimension(:,:), allocatable :: uC_tmp, vC_tmp, uA_tmp, vA_tmp
real, dimension(:,:,:),allocatable :: h_upoints, h_vpoints
real, dimension(:,:), allocatable :: iCount
integer :: stderrunit
! Get the stderr unit number
stderrunit = stderr()
! For convenience
grd=>parts%grd
! grd%u_particle(:,:)=0.
! grd%v_particle(:,:)=0.
!Initializing _on_ocean_fields
! grd%Uvel_on_ocean(:,:,:)=0. ; grd%Vvel_on_ocean(:,:,:)=0.
! Manage time
call get_date(time, iyr, imon, iday, ihr, imin, isec)
parts%current_year=iyr
parts%current_yearday=yearday(iyr, imon, iday, ihr, imin, isec)
! Turn on sampling of trajectories, verbosity, budgets
sample_traj=.false.
if ( (parts%traj_sample_hrs>0) .and. (.not. parts%ignore_traj) ) then
if (mod(60*60*24*iday+ 60*60*ihr + 60*imin + isec ,60*60*parts%traj_sample_hrs).eq.0) &
sample_traj=.true.
endif
write_traj=.false.
if ((parts%traj_write_hrs>0) .and. (.not. parts%ignore_traj)) then
if (mod(60*60*24*(iday-1)+ 60*60*ihr + 60*imin + isec, 60*60*parts%traj_write_hrs).eq.0) &
write_traj=.true.
endif
lverbose=.false.
if (parts%verbose_hrs>0) then
if (mod(24*iday+ihr+(imin/60.),float(parts%verbose_hrs)).eq.0) lverbose=verbose
endif
if (mpp_pe()==mpp_root_pe().and.lverbose) write(*,'(a,3i5,a,3i5,a,i5,f8.3)') &
'diamonds: y,m,d=',iyr, imon, iday,' h,m,s=', ihr, imin, isec, &
' yr,yrdy=', parts%current_year, parts%current_yearday
! SPENCER: here is where we need to pass all ocean velocities
if (use_uh) then
h_upoints=0.5*(ho(grd%isd+1:grd%ied+1,grd%jsd:grd%jed,1:grd%ke)+ho(grd%isd:grd%ied,grd%jsd:grd%jed,1:grd%ke))
h_vpoints=0.5*(ho(grd%isd:grd%ied,grd%jsd+1:grd%jed+1,1:grd%ke)+ho(grd%isd:grd%ied,grd%jsd:grd%jed,1:grd%ke))
do k=1,grd%ke
grd%uo(:,:,k) = uo(grd%isd:grd%ied,grd%jsd:grd%jed,k) /h_upoints(grd%isd:grd%ied,grd%jsd:grd%jed,k) / dt_adv ! parts%dt
grd%vo(:,:,k) = vo(grd%isd:grd%ied,grd%jsd:grd%jed,k) /h_vpoints(grd%isd:grd%ied,grd%jsd:grd%jed,k) / dt_adv
enddo
!parts%dt = dt_adv
else
do k=1,grd%ke
grd%uo(:,:,k) = uo(:,:,k)*grd%dy(:,:)
grd%vo(:,:,k) = vo(:,:,k)*grd%dx(:,:)
enddo
endif
do k=2,grd%ke
grd%hdepth(grd%isd:grd%ied,grd%jsd:grd%jed,k) = grd%hdepth(grd%isd:grd%ied,grd%jsd:grd%jed,k-1)+ho(grd%isd:grd%ied,grd%jsd:grd%jed,k)
enddo
parts%dt = dt_adv
if (debug) call parts_chksum(parts, 'run parts (top)')
if (debug) call checksum_gridded(parts%grd, 'top of s/r run')
!Move to k-space if not already
call particles_to_k_space(parts,ho)
if (parts%initial_traj) then
call record_posn(parts, ho, tv%T)
parts%initial_traj=.False.
endif
call evolve_particles(parts)
if (parts%debug_particle_with_id>0) call monitor_a_part(parts, 'particles_run, after evolve() ')
call move_part_between_cells(parts) !Markpoint6
if (parts%debug_particle_with_id>0) call monitor_a_part(parts, 'particles_run, after move_lists() ')
if (debug) call parts_chksum(parts, 'run parts (evolved)',ignore_halo_violation=.true.)
if (debug) call checksum_gridded(parts%grd, 's/r run after evolve')
call send_parts_to_other_pes(parts)
if (parts%debug_particle_with_id>0) call monitor_a_part(parts, 'particles_run, after send_parts() ')
! For each part, record
! sample_traj = .true.
if (sample_traj) call record_posn(parts, ho, tv%T)
if (write_traj) then
call move_all_trajectories(parts)
call write_trajectory(parts%trajectories, parts%save_short_traj)
endif
! Dump particles to screen
if (really_debug) call print_parts(stderrunit,parts,'particles_run, status')
if (debug) call parts_chksum(parts, 'run parts (bot)')
end subroutine particles_run
! ##############################################################################
!> Checks whether all particles are in k-space and if not, moves them to k-space
subroutine particles_to_k_space(parts,h)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
real, dimension(:,:,:),intent(in) :: h !< Thickness of layers
!Local variables
type(particles_gridded), pointer :: grd
type(particle), pointer :: part
integer :: grdi, grdj
integer :: stdlogunit, stderrunit
! Get the stderr and stdlog unit numbers
stderrunit=stderr()
! For convenience
grd=>parts%grd
do grdj = grd%jsc,grd%jec ; do grdi = grd%isc,grd%iec
part=>parts%list(grdi,grdj)%first
do while (associated(part)) ! loop over all parts
call find_layer(grd, part%depth, h, part%k, part%ine,part%jne, part%xi,part%yj, part%k_space)
part=>part%next
enddo
enddo ; enddo
end subroutine particles_to_k_space
! ##############################################################################
!> Checks whether all particles are in k-space and if not, moves them to k-space
subroutine particles_to_z_space(parts,h)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
real, dimension(:,:,:),intent(in) :: h !< Thickness of layers
!Local variables
type(particles_gridded), pointer :: grd
type(particle), pointer :: part
integer :: grdi, grdj
integer :: stdlogunit, stderrunit
! Get the stderr and stdlog unit numbers
stderrunit=stderr()
! For convenience
grd=>parts%grd
do grdj = grd%jsc,grd%jec ; do grdi = grd%isc,grd%iec
part=>parts%list(grdi,grdj)%first
do while (associated(part)) ! loop over all parts
call find_depth(grd, part%k, h, part%depth, part%ine,part%jne, part%xi,part%yj, part%k_space)
part=>part%next
enddo
enddo ; enddo
end subroutine particles_to_z_space
! ##############################################################################
!> Evolves particles forward by updating velocity and position with a time-stepping scheme
subroutine evolve_particles(parts)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
! Local variables
type(particles_gridded), pointer :: grd
type(particle), pointer :: part
real :: uveln, vveln, lonn, latn
real :: xi, yj
integer :: i, j
integer :: grdi, grdj
integer :: stderrunit
logical :: bounced, xystagger
! Get the stderr unit number
stderrunit = stderr()
! For convenience
grd=>parts%grd
xystagger = parts%xystagger
do grdj = grd%jsc,grd%jec ; do grdi = grd%isc,grd%iec
part=>parts%list(grdi,grdj)%first
do while (associated(part)) ! loop over all parts
!Checking it everything is ok:
if (.not. is_point_in_cell(parts%grd, part%lon, part%lat, part%ine, part%jne) ) then
write(stderrunit,'(i4,a4,32i7)') mpp_pe(),'Lon',(i,i=grd%isd,grd%ied)
do j=grd%jed,grd%jsd,-1
write(stderrunit,'(2i4,32f7.1)') mpp_pe(),j,(grd%lon(i,j),i=grd%isd,grd%ied)
enddo
write(stderrunit,'(i4,a4,32i7)') mpp_pe(),'Lat',(i,i=grd%isd,grd%ied)
do j=grd%jed,grd%jsd,-1
write(stderrunit,'(2i4,32f7.1)') mpp_pe(),j,(grd%lat(i,j),i=grd%isd,grd%ied)
enddo
call print_part(stderrunit, part, 'evolve_particle, part is not in proper starting cell')
write(stderrunit,'(a,i3,2(i4,3f8.2))') 'evolve_particle: pe,lon/lat(i,j)=', mpp_pe(), &
part%ine,part%lon,grd%lon(part%ine-1,part%jne-1),grd%lon(part%ine,part%jne), &
part%jne,part%lat,grd%lat(part%ine-1,part%jne-1),grd%lat(part%ine,part%jne)
if (debug) call error_mesg('diamonds, evolve_particle','part is in wrong starting cell!',FATAL)
endif
if (debug) call check_position(grd, part, 'evolve_particle (top)')
! Interpolate gridded velocity fields to part and generate uvel and vvel
call interp_flds(grd,part%ine,part%jne,part%k,part%xi,part%yj,part%uvel, part%vvel, part%lon, part%lat)
!Time stepping schemes:
!call Runge_Kutta_stepping(parts,part, uveln, vveln,lonn, latn, i, j, xi, yj)
if (xystagger) then
call Runge_Kutta_xystagger(parts,part, uveln, vveln,lonn, latn, i, j, xi, yj)
else
call Runge_Kutta_stepping(parts,part, uveln, vveln,lonn, latn, i, j, xi, yj)
endif
part%uvel=uveln
part%vvel=vveln
part%lon=lonn ; part%lat=latn
part%ine=i ; part%jne=j
part%xi=xi ; part%yj=yj
!call interp_flds(grd, i, j, xi, yj, part%uo, part%vo, part%ui, part%vi, part%ua, part%va, part%ssh_x, part%ssh_y, part%sst)
!if (debug) call print_part(stderr(), part, 'evolve_particle, final posn.')
if (debug) call check_position(grd, part, 'evolve_particle (bot)')
!endif
part=>part%next
enddo ! loop over all parts
enddo ; enddo
end subroutine evolve_particles
!> Calculate explicit and implicit accelerations, new velocity, and new position, using the fourth order Runge-Kutta method
subroutine Runge_Kutta_stepping(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
type(particle), pointer, intent(inout) :: part !< particle
real, intent(out) :: uveln !< New zonal velocity (m/s)
real, intent(out) :: vveln !< New meridional velocity (m/s)
real, intent(out) :: lonn !< New longitude (degree E)
real, intent(out) :: latn !< New latitude (degree N)
integer, intent(out) :: i !< New i-index of containing cell
integer, intent(out) :: j !< New i-index of containing cell
real, intent(out) :: xi !< New non-dimensional x-position
real, intent(out) :: yj !< New non-dimensional y-position
! Local variables
type(particles_gridded), pointer :: grd
real :: uvel1, vvel1, lon1, lat1, u1, v1, dxdl1
real :: uvel2, vvel2, lon2, lat2, u2, v2, dxdl2
real :: uvel3, vvel3, lon3, lat3, u3, v3, dxdl3
real :: uvel4, vvel4, lon4, lat4, u4, v4, dxdl4
real :: x1, xdot1, xddot1, y1, ydot1, yddot1, xddot1n, yddot1n
real :: x2, xdot2, xddot2, y2, ydot2, yddot2, xddot2n, yddot2n
real :: x3, xdot3, xddot3, y3, ydot3, yddot3, xddot3n, yddot3n
real :: x4, xdot4, xddot4, y4, ydot4, yddot4, xddot4n, yddot4n
real :: xn, xdotn, xddotn, yn, ydotn, yddotn, xddotnn, yddotnn
real :: xdummy, ydummy, xdotdummy, ydotdummy, londummy, latdummy
real :: dt, dt_2, dt_6, dydl
real :: reg_dldx, udummy, vdummy
integer :: i1,j1,i2,j2,i3,j3,i4,j4
integer :: stderrunit
logical :: bounced, on_tangential_plane, error_flag
! 4th order Runge-Kutta to solve:
! d/dt X = V, d/dt V = A
! with I.C.'s:
! X=X1 and V=V1
!
! A1 = A(X1)
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
! X4 = X1+ dt*V3 ; V4 = V1+ dt*A3; A4=A(X4)
!
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Vn = V1+dt*(A1+2*A2+2*A3+A4)/6
! Get the stderr unit number
stderrunit = stderr()
! For convenience
grd=>parts%grd
! Common constants
dt=parts%dt
dt_2=0.5*dt
dt_6=dt/6.
i=part%ine
j=part%jne
xi=part%xi
yj=part%yj
bounced=.false.
on_tangential_plane=.false.
if ((part%lat>89.) .and. (parts%grd%grid_is_latlon)) on_tangential_plane=.true.
i1=i;j1=j
reg_dldx=min(abs(grd%lon(i+1,j)-grd%lon(i,j)),abs(grd%lon(i,j)-grd%lon(i-1,j)))/grd%dx(i,j)
lon1=part%lon; lat1=part%lat
if (on_tangential_plane) call rotpos_to_tang(lon1,lat1,x1,y1)
call convert_from_meters_to_grid(lat1,parts%grd%grid_is_latlon,parts%grd%grid_is_regular,dxdl1,dydl,reg_dldx)
uvel1=part%uvel; vvel1=part%vvel
if (on_tangential_plane) call rotvec_to_tang(lon1,uvel1,vvel1,xdot1,ydot1)
u1=uvel1*dxdl1; v1=vvel1*dydl
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
call Runge_Kutta_step(parts, part, dt_2, x1, y1, x2, y2, xdot1, ydot1, xdot2, ydot2, lon1, lat1, lon2, lat2, u1,v1, i1, j1, i, j, xi, yj, u2, v2, reg_dldx, on_tangential_plane)
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
call Runge_Kutta_step(parts, part, dt_2, x1, y1, x3, y3, xdot2, ydot2, xdot3, ydot3, lon1, lat1, lon3, lat3, u2,v2, i1, j1, i, j, xi, yj, u3, v3, reg_dldx, on_tangential_plane)
! X4 = X1+dt*V3 ; V4 = V1+dt*A3; A4=A(X4)
call Runge_Kutta_step(parts, part, dt, x1, y1, x4, y4, xdot3, ydot3, xdot4, ydot4, lon1, lat1, lon4, lat4, u3,v3, i1, j1, i, j, xi, yj, u4, v4, reg_dldx, on_tangential_plane)
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Vn = V1+dt*(A1+2*A2+2*A3+A4)/6
if (on_tangential_plane) then
xn=x1+dt_6*( (xdot1+xdot4)+2.*(xdot2+xdot3) )
yn=y1+dt_6*( (ydot1+ydot4)+2.*(ydot2+ydot3) )
xdotn=( (xdot1+xdot4)+2.*(xdot2+xdot3) )/6.
ydotn=( (ydot1+ydot4)+2.*(ydot2+ydot3) )/6.
call rotpos_from_tang(xn,yn,lonn,latn)
call rotvec_from_tang(lonn,xdotn,ydotn,uveln,vveln)
else
lonn=part%lon+dt_6*( (u1+u4)+2.*(u2+u3) )
latn=part%lat+dt_6*( (v1+v4)+2.*(v2+v3) )
uveln=part%uvel
vveln=part%vvel
endif
i=i1;j=j1;xi=part%xi;yj=part%yj
call adjust_index_and_ground(grd, lonn, latn, uveln, vveln, i, j, xi, yj, bounced, error_flag, part%id)
end subroutine Runge_Kutta_stepping
subroutine Runge_Kutta_xystagger(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
type(particle), pointer, intent(inout) :: part !< particle
real, intent(out) :: uveln !< New zonal velocity (m/s)
real, intent(out) :: vveln !< New meridional velocity (m/s)
real, intent(out) :: lonn !< New longitude (degree E)
real, intent(out) :: latn !< New latitude (degree N)
integer, intent(out) :: i !< New i-index of containing cell
integer, intent(out) :: j !< New i-index of containing cell
real, intent(out) :: xi !< New non-dimensional x-position
real, intent(out) :: yj !< New non-dimensional y-position
! Local variables
type(particles_gridded), pointer :: grd
real :: uvel1, vvel1, lon1, lat1, u1, v1, dxdl1
real :: uvel2, vvel2, lon2, lat2, u2, v2, dxdl2
real :: uvel3, vvel3, lon3, lat3, u3, v3, dxdl3
real :: uvel4, vvel4, lon4, lat4, u4, v4, dxdl4
real :: x1, xdot1, xddot1, y1, ydot1, yddot1, xddot1n, yddot1n
real :: x2, xdot2, xddot2, y2, ydot2, yddot2, xddot2n, yddot2n
real :: x3, xdot3, xddot3, y3, ydot3, yddot3, xddot3n, yddot3n
real :: x4, xdot4, xddot4, y4, ydot4, yddot4, xddot4n, yddot4n
real :: xn, xdotn, xddotn, yn, ydotn, yddotn, xddotnn, yddotnn
real :: xdummy, ydummy, xdotdummy, ydotdummy, londummy, latdummy
real :: dt, dt_2, dt_6, dydl
real :: reg_dldx, udummy, vdummy
integer :: i1,j1,i2,j2,i3,j3,i4,j4
integer :: stderrunit
logical :: bounced, on_tangential_plane, error_flag
! 4th order Runge-Kutta to solve:
! d/dt X = V, d/dt V = A
! with I.C.'s:
! X=X1 and V=V1
!
! A1 = A(X1)
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
! X4 = X1+ dt*V3 ; V4 = V1+ dt*A3; A4=A(X4)
!
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Vn = V1+dt*(A1+2*A2+2*A3+A4)/6
! Get the stderr unit number
stderrunit = stderr()
!If x-step is first
if (mod(parts%nstep,2).eq.0) then
call Runge_xtheny_step(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
part%lon=lonn ; part%lat=latn
part%ine=i ; part%jne=j
part%xi=xi ; part%yj=yj
call Runge_ythenx_step(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
else
call Runge_ythenx_step(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
part%lon=lonn ; part%lat=latn
part%ine=i ; part%jne=j
part%xi=xi ; part%yj=yj
call Runge_xtheny_step(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
endif
parts%nstep = parts%nstep + 1
end subroutine Runge_Kutta_xystagger
subroutine Runge_xtheny_step(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
type(particle), pointer, intent(inout) :: part !< particle
real, intent(out) :: uveln !< New zonal velocity (m/s)
real, intent(out) :: vveln !< New meridional velocity (m/s)
real, intent(out) :: lonn !< New longitude (degree E)
real, intent(out) :: latn !< New latitude (degree N)
integer, intent(out) :: i !< New i-index of containing cell
integer, intent(out) :: j !< New i-index of containing cell
real, intent(out) :: xi !< New non-dimensional x-position
real, intent(out) :: yj !< New non-dimensional y-position
! Local variables
type(particles_gridded), pointer :: grd
real :: uvel1, vvel1, lon1, lat1, u1, v1, dxdl1
real :: uvel2, vvel2, lon2, lat2, u2, v2, dxdl2
real :: uvel3, vvel3, lon3, lat3, u3, v3, dxdl3
real :: uvel4, vvel4, lon4, lat4, u4, v4, dxdl4
real :: x1, xdot1, xddot1, y1, ydot1, yddot1, xddot1n, yddot1n
real :: x2, xdot2, xddot2, y2, ydot2, yddot2, xddot2n, yddot2n
real :: x3, xdot3, xddot3, y3, ydot3, yddot3, xddot3n, yddot3n
real :: x4, xdot4, xddot4, y4, ydot4, yddot4, xddot4n, yddot4n
real :: xn, xdotn, xddotn, yn, ydotn, yddotn, xddotnn, yddotnn
real :: xdummy, ydummy, xdotdummy, ydotdummy, londummy, latdummy
real :: dt, dt_2, dt_6, dydl
real :: reg_dldx, udummy, vdummy
integer :: i1,j1,i2,j2,i3,j3,i4,j4
integer :: stderrunit
logical :: bounced, on_tangential_plane, error_flag
! 4th order Runge-Kutta to solve:
! d/dt X = V, d/dt V = A
! with I.C.'s:
! X=X1 and V=V1
!
! A1 = A(X1)
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
! X4 = X1+ dt*V3 ; V4 = V1+ dt*A3; A4=A(X4)
!
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Get the stderr unit number
stderrunit = stderr()
! For convenience
grd=>parts%grd
! Common constants
dt=parts%dt
dt_2=0.5*dt
dt_6=dt/6.
i=part%ine
j=part%jne
xi=part%xi
yj=part%yj
bounced=.false.
on_tangential_plane=.false.
if ((part%lat>89.) .and. (parts%grd%grid_is_latlon)) on_tangential_plane=.true.
i1=i;j1=j
reg_dldx=min(abs(grd%lon(i+1,j)-grd%lon(i,j)),abs(grd%lon(i,j)-grd%lon(i-1,j)))/grd%dx(i,j)
lon1=part%lon; lat1=part%lat
if (on_tangential_plane) call rotpos_to_tang(lon1,lat1,x1,y1)
call convert_from_meters_to_grid(lat1,parts%grd%grid_is_latlon,parts%grd%grid_is_regular,dxdl1,dydl,reg_dldx)
call interp_flds(grd, i, j, part%k, xi, yj, uvel1, vvel1, lon1, lat1)
if (on_tangential_plane) then
call rotvec_to_tang(lon1,uvel1,vvel1,xdot1,ydot1)
ydot1=0.0
else
vvel1 = 0.0
endif
u1=uvel1*dxdl1; v1=vvel1*dydl
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
call Runge_Kutta_step(parts, part, dt_2, x1, y1, x2, y2, xdot1, ydot1, xdot2, ydot2, lon1, lat1, lon2, lat2, u1,vvel1, i1, j1, i, j, xi, yj, u2, v2, reg_dldx, on_tangential_plane)
if (on_tangential_plane) then
ydot2=0.0
else
v2 = 0.0
endif
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
call Runge_Kutta_step(parts, part, dt_2, x1, y1, x3, y3, xdot2, ydot2, xdot3, ydot3, lon1, lat1, lon3, lat3, u2,v2, i1, j1, i, j, xi, yj, u3, v3, reg_dldx, on_tangential_plane)
if (on_tangential_plane) then
ydot3=0.0
else
v3 = 0.0
endif
! X4 = X1+dt*V3 ; V4 = V1+dt*A3; A4=A(X4)
call Runge_Kutta_step(parts, part, dt, x1, y1, x4, y4, xdot3, ydot3, xdot4, ydot4, lon1, lat1, lon4, lat4, u3,v3, i1, j1, i, j, xi, yj, u4, v4, reg_dldx, on_tangential_plane)
if (on_tangential_plane) then
ydot4=0.0
else
v4 = 0.0
endif
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Vn = V1+dt*(A1+2*A2+2*A3+A4)/6
if (on_tangential_plane) then
xn=x1+dt_6*( (xdot1+xdot4)+2.*(xdot2+xdot3) )
yn=y1!+dt_6*( (ydot1+ydot4)+2.*(ydot2+ydot3) )
xdotn=( (xdot1+xdot4)+2.*(xdot2+xdot3) )/6.
ydotn=0.0
call rotpos_from_tang(xn,yn,lonn,latn)
call rotvec_from_tang(lonn,xdotn,ydotn,uveln,vveln)
else
lonn=part%lon+dt_6*( (u1+u4)+2.*(u2+u3) )
latn=part%lat!+dt_6*( (v1+v4)+2.*(v2+v3) )
uveln=part%uvel
vveln=0.0
endif
i=i1;j=j1;xi=part%xi;yj=part%yj
call adjust_index_and_ground(grd, lonn, latn, uveln, vveln, i, j, xi, yj, bounced, error_flag, part%id)
end subroutine Runge_xtheny_step
subroutine Runge_ythenx_step(parts, part, uveln, vveln, lonn, latn, i, j, xi, yj)
! Arguments
type(particles), pointer :: parts !< Container for all types and memory
type(particle), pointer, intent(inout) :: part !< particle
real, intent(out) :: uveln !< New zonal velocity (m/s)
real, intent(out) :: vveln !< New meridional velocity (m/s)
real, intent(out) :: lonn !< New longitude (degree E)
real, intent(out) :: latn !< New latitude (degree N)
integer, intent(out) :: i !< New i-index of containing cell
integer, intent(out) :: j !< New i-index of containing cell
real, intent(out) :: xi !< New non-dimensional x-position
real, intent(out) :: yj !< New non-dimensional y-position
! Local variables
type(particles_gridded), pointer :: grd
real :: uvel1, vvel1, lon1, lat1, u1, v1, dxdl1
real :: uvel2, vvel2, lon2, lat2, u2, v2, dxdl2
real :: uvel3, vvel3, lon3, lat3, u3, v3, dxdl3
real :: uvel4, vvel4, lon4, lat4, u4, v4, dxdl4
real :: x1, xdot1, xddot1, y1, ydot1, yddot1, xddot1n, yddot1n
real :: x2, xdot2, xddot2, y2, ydot2, yddot2, xddot2n, yddot2n
real :: x3, xdot3, xddot3, y3, ydot3, yddot3, xddot3n, yddot3n
real :: x4, xdot4, xddot4, y4, ydot4, yddot4, xddot4n, yddot4n
real :: xn, xdotn, xddotn, yn, ydotn, yddotn, xddotnn, yddotnn
real :: xdummy, ydummy, xdotdummy, ydotdummy, londummy, latdummy
real :: dt, dt_2, dt_6, dydl
real :: reg_dldx, udummy, vdummy
integer :: i1,j1,i2,j2,i3,j3,i4,j4
integer :: stderrunit
logical :: bounced, on_tangential_plane, error_flag
! 4th order Runge-Kutta to solve:
! d/dt X = V, d/dt V = A
! with I.C.'s:
! X=X1 and V=V1
!
! A1 = A(X1)
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
! X4 = X1+ dt*V3 ; V4 = V1+ dt*A3; A4=A(X4)
!
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Get the stderr unit number
stderrunit = stderr()
! For convenience
grd=>parts%grd
! Common constants
dt=parts%dt
dt_2=0.5*dt
dt_6=dt/6.
i=part%ine
j=part%jne
xi=part%xi
yj=part%yj
bounced=.false.
on_tangential_plane=.false.
if ((part%lat>89.) .and. (parts%grd%grid_is_latlon)) on_tangential_plane=.true.
i1=i;j1=j
reg_dldx=min(abs(grd%lon(i+1,j)-grd%lon(i,j)),abs(grd%lon(i,j)-grd%lon(i-1,j)))/grd%dx(i,j)
lon1=part%lon; lat1=part%lat
if (on_tangential_plane) call rotpos_to_tang(lon1,lat1,x1,y1)
call convert_from_meters_to_grid(lat1,parts%grd%grid_is_latlon,parts%grd%grid_is_regular,dxdl1,dydl,reg_dldx)
call interp_flds(grd, i, j, part%k, xi, yj, uvel1, vvel1,lon1,lat1)
if (on_tangential_plane) then
call rotvec_to_tang(lon1,uvel1,vvel1,xdot1,ydot1)
xdot1=0.0
else
uvel1 = 0.0
endif
u1=uvel1*dxdl1; v1=vvel1*dydl
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
call Runge_Kutta_step(parts, part, dt_2, x1, y1, x2, y2, xdot1, ydot1, xdot2, ydot2, lon1, lat1, lon2, lat2, u1, v1, i1, j1, i, j, xi, yj, u2, v2, reg_dldx, on_tangential_plane)
if (on_tangential_plane) then
xdot2=0.0
else
u2 = 0.0
endif
! X3 = X1+dt/2*V2 ; V3 = V1+dt/2*A2; A3=A(X3)
call Runge_Kutta_step(parts, part, dt_2, x1, y1, x3, y3, xdot2, ydot2, xdot3, ydot3, lon1, lat1, lon3, lat3, u2, v2, i1, j1, i, j, xi, yj, u3, v3, reg_dldx, on_tangential_plane)
if (on_tangential_plane) then
xdot3=0.0
else
u3 = 0.0
endif
! X4 = X1+dt*V3 ; V4 = V1+dt*A3; A4=A(X4)
call Runge_Kutta_step(parts, part, dt, x1, y1, x4, y4, xdot3, ydot3, xdot4, ydot4, lon1, lat1, lon4, lat4, u3, v3, i1, j1, i, j, xi, yj, u4, v4, reg_dldx, on_tangential_plane)
if (on_tangential_plane) then
xdot4=0.0
else
u4 = 0.0
endif
! Xn = X1+dt*(V1+2*V2+2*V3+V4)/6
! Vn = V1+dt*(A1+2*A2+2*A3+A4)/6
if (on_tangential_plane) then
xn=x1!+dt_6*( (xdot1+xdot4)+2.*(xdot2+xdot3) )
yn=y1+dt_6*( (ydot1+ydot4)+2.*(ydot2+ydot3) )
xdotn=0.0
ydotn=( (ydot1+ydot4)+2.*(ydot2+ydot3) )/6.
call rotpos_from_tang(xn,yn,lonn,latn)
call rotvec_from_tang(lonn,xdotn,ydotn,uveln,vveln)
else
lonn=part%lon!+dt_6*( (u1+u4)+2.*(u2+u3) )
latn=part%lat+dt_6*( (v1+v4)+2.*(v2+v3) )
uveln=0.0
vveln=part%vvel
endif
i=i1;j=j1;xi=part%xi;yj=part%yj
call adjust_index_and_ground(grd, lonn, latn, uveln, vveln, i, j, xi, yj, bounced, error_flag, part%id)
end subroutine Runge_ythenx_step
! ###############################################################################
!> Perform an individual step in the Runge-Kutta algorithm
subroutine Runge_Kutta_step(parts, part, dt, xa, ya, xb, yb, xdota, ydota, xdotb, ydotb, lona, lata, lonb, latb, ua,va, ia, ja, i, j, xi, yj, ub, vb, reg_dldx, on_tangential_plane)
type(particles), pointer :: parts !< Container for all types and memory
type(particle), pointer, intent(inout) :: part !< particle
real :: dt !< timestep for this Runge-Kutta step
real :: xa
real :: ya
real :: xb
real :: yb
real :: xdota
real :: ydota
real :: xdotb
real :: ydotb
real :: lona
real :: lata
real :: lonb
real :: latb
real :: ua
real :: va
real :: ub
real :: vb
integer :: ia
integer :: ja
real :: reg_dldx
logical :: on_tangential_plane
! Local variables
type(particles_gridded), pointer :: grd
real :: dxdl1
real :: uvel2, vvel2, u2, v2, dxdl2
real :: xdot2, ydot2, yddot2
real :: xi, yj, dydl
integer :: i, j
integer :: stderrunit
logical :: bounced, error_flag
! Get the stderr unit number
stderrunit = stderr()
grd=>parts%grd
! X2 = X1+dt/2*V1 ; V2 = V1+dt/2*A1; A2=A(X2)
if (on_tangential_plane) then
xb=xa+dt*xdota; yb=ya+dt*ydota
call rotpos_from_tang(xb,yb,lonb,latb)
else
lonb=lona+dt*ua; latb=lata+dt*va
endif
bounced = .false.
i=ia;j=ja;xi=part%xi;yj=part%yj
call adjust_index_and_ground(grd, lonb, latb, ua, va, i, j, xi, yj, bounced, error_flag, part%id)
! if (.not.error_flag) then
! if (debug .and. .not. is_point_in_cell(parts%grd, lonb, latb, i, j)) error_flag=.true.
! endif
call convert_from_meters_to_grid(latb,parts%grd%grid_is_latlon,parts%grd%grid_is_regular,dxdl2,dydl,reg_dldx)
call interp_flds(grd, i, j, part%k, xi, yj, uvel2, vvel2,lonb,latb)
if (on_tangential_plane) call rotvec_to_tang(lonb,uvel2,vvel2,xdotb,ydotb)
ub=uvel2*dxdl2; vb=vvel2*dydl
end subroutine Runge_Kutta_step
! ###############################################################################
!> Calculate longitude-latitude from tangent plane coordinates
subroutine rotpos_to_tang(lon, lat, x, y, id_in)
! Arguments
real, intent(in) :: lon !< Longitude (degree E)
real, intent(in) :: lat !< Latitude (degree N)
real, intent(out) :: x !< x-coordinate in tangent plane
real, intent(out) :: y !< y-coordinate in tangent plane
integer(kind=8), intent(in), optional :: id_in !< part identifier
! Local variables
real :: r,colat,clon,slon
integer :: stderrunit, id
stderrunit = stderr()
id=0
if (present(id_in)) then
id=id_in
endif
if (lat>90.) then
write(stderrunit,*) 'drifters, rotpos_to_tang: lat>90 already!',lat, lon, id
call error_mesg('drifters, rotpos_to_tang','Something went very wrong!',FATAL)
endif
if (lat==90.) then
write(stderrunit,*) 'drifters, rotpos_to_tang: lat==90 already!',lat, lon
call error_mesg('drifters, rotpos_to_tang','Something went wrong!',FATAL)
endif
colat=90.-lat
r=Rearth*(colat*pi_180)
clon=cos(lon*pi_180)
slon=sin(lon*pi_180)
x=r*clon
y=r*slon
end subroutine rotpos_to_tang
! ###################################################################################
!> Calculate longitude-latitude from tangent plane coordinates
subroutine rotpos_from_tang(x, y, lon, lat)
! Arguments
real, intent(in) :: x !< x-coordinate in tangent plane
real, intent(in) :: y !< y-coordinate in tangent plane
real, intent(out) :: lon !< Longitude (degree E)
real, intent(out) :: lat !< Latitude (degree N)
! Local variables
real :: r
r=sqrt(x**2+y**2)
lat=90.-(r180_pi*r/Rearth)
lon=r180_pi*acos(x/r)*sign(1.,y)
end subroutine rotpos_from_tang
! ###################################################################################
!> Calculate velocity oriented in geographic coordinates from tangent plane velocity
subroutine rotvec_from_tang(lon, xdot, ydot, uvel, vvel)
! Arguments
real, intent(in) :: lon !< Longitude (degree E)
real, intent(in) :: xdot !< x-component of velocity in tangent plane (m/s)
real, intent(in) :: ydot !< y-component of velocity in tangent plane (m/s)
real, intent(out) :: uvel !< Zonal velocity (m/s)
real, intent(out) :: vvel !< Meridional velocity (m/s)
! Local variables
real :: clon,slon
clon=cos(lon*pi_180)
slon=sin(lon*pi_180)
uvel=-slon*xdot+clon*ydot
vvel=-clon*xdot-slon*ydot
end subroutine rotvec_from_tang
! ###################################################################################
!> Calculates tangent plane velocity from velocity in velocity oriented in geographic coordinates
subroutine rotvec_to_tang(lon, uvel, vvel, xdot, ydot)
! Arguments
real, intent(in) :: lon !< Longitude (degree E)
real, intent(in) :: uvel !< Zonal velocity (m/s)
real, intent(in) :: vvel !< Meridional velocity (m/s)
real, intent(out) :: xdot !< x-component of velocity in tangent plane (m/s)
real, intent(out) :: ydot !< y-component of velocity in tangent plane (m/s)
! Local variables
real :: clon,slon
clon=cos(lon*pi_180)
slon=sin(lon*pi_180)
xdot=-slon*uvel-clon*vvel
ydot=clon*uvel-slon*vvel
end subroutine rotvec_to_tang
! ####################################################################################
!> Returns metric converting distance in meters to grid distance
subroutine convert_from_meters_to_grid(lat_ref,grid_is_latlon,grid_is_regular,dlon_dx,dlat_dy,reg_dlon_dx)
! Arguments