24 implicit none ;
private
26 #include <MOM_memory.h>
28 public wave_structure, wave_structure_init
39 real,
allocatable,
dimension(:,:,:) :: w_strct
41 real,
allocatable,
dimension(:,:,:) :: u_strct
43 real,
allocatable,
dimension(:,:,:) :: w_profile
47 real,
allocatable,
dimension(:,:,:) :: uavg_profile
50 real,
allocatable,
dimension(:,:,:) :: z_depths
52 real,
allocatable,
dimension(:,:,:) :: n2
54 integer,
allocatable,
dimension(:,:):: num_intfaces
56 real :: int_tide_source_x
58 real :: int_tide_source_y
91 subroutine wave_structure(h, tv, G, GV, US, cn, ModeNum, freq, CS, En, full_halos)
95 real,
dimension(SZI_(G),SZJ_(G),SZK_(G)),
intent(in) :: h
98 real,
dimension(SZI_(G),SZJ_(G)),
intent(in) :: cn
100 integer,
intent(in) :: modenum
101 real,
intent(in) :: freq
104 real,
dimension(SZI_(G),SZJ_(G)), &
105 optional,
intent(in) :: en
106 logical,
optional,
intent(in) :: full_halos
109 real,
dimension(SZK_(G)+1) :: &
111 pres, t_int, s_int, &
113 real,
dimension(SZK_(G)) :: &
116 real,
dimension(SZK_(G),SZI_(G)) :: &
118 real,
dimension(SZK_(G)) :: &
121 real,
dimension(SZI_(G),SZJ_(G)) :: &
126 real,
dimension(SZI_(G)) :: &
128 h_here, hxt_here, hxs_here, hxr_here
130 real :: i_hnew, drxh_sum
131 real,
parameter :: tol1 = 0.0001, tol2 = 0.001
132 real,
pointer,
dimension(:,:,:) :: t => null(), s => null()
134 real :: rescale, i_rescale
135 integer :: kf(szi_(g))
136 integer,
parameter :: max_itt = 1
137 real,
parameter :: cg_subro = 1e-100
138 real,
parameter :: a_int = 0.5
144 real,
dimension(SZK_(G)+1) :: w_strct, u_strct, w_profile, uavg_profile, z_int, n2
147 real,
dimension(SZK_(G)+1) :: w_strct2, u_strct2
149 real,
dimension(SZK_(G)) :: dz
150 real,
dimension(SZK_(G)+1) :: dwdz_profile
152 real :: int_dwdz2, int_w2, int_n2w2, ke_term, pe_term, w0
155 real,
dimension(SZK_(G)-1) :: lam_z
157 real,
dimension(SZK_(G)-1) :: a_diag, b_diag, c_diag
160 real,
dimension(SZK_(G)-1) :: e_guess
161 real,
dimension(SZK_(G)-1) :: e_itt
164 integer :: i, j, k, k2, itt, is, ie, js, je, nz, nzm, row, ig, jg, ig_stop, jg_stop
166 is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
170 if (.not.
associated(cs))
call mom_error(fatal,
"MOM_wave_structure: "// &
171 "Module must be initialized before it is used.")
174 if (
present(full_halos))
then ;
if (full_halos)
then
175 is = g%isd ; ie = g%ied ; js = g%jsd ; je = g%jed
180 s => tv%S ; t => tv%T
181 g_rho0 = us%L_T_to_m_s**2 * gv%g_Earth /gv%Rho0
182 use_eos =
associated(tv%eqn_of_state)
184 h_to_pres = gv%Z_to_H*gv%H_to_Pa
185 rescale = 1024.0**4 ; i_rescale = 1.0/rescale
187 min_h_frac = tol1 / real(nz)
193 do i=is,ie ; htot(i,j) = 0.0 ;
enddo
194 do k=1,nz ;
do i=is,ie ; htot(i,j) = htot(i,j) + h(i,j,k)*gv%H_to_Z ;
enddo ;
enddo
197 hmin(i) = htot(i,j)*min_h_frac ; kf(i) = 1 ; h_here(i) = 0.0
198 hxt_here(i) = 0.0 ; hxs_here(i) = 0.0 ; hxr_here(i) = 0.0
201 do k=1,nz ;
do i=is,ie
202 if ((h_here(i) > hmin(i)) .and. (h(i,j,k)*gv%H_to_Z > hmin(i)))
then
203 hf(kf(i),i) = h_here(i)
204 tf(kf(i),i) = hxt_here(i) / h_here(i)
205 sf(kf(i),i) = hxs_here(i) / h_here(i)
209 h_here(i) = h(i,j,k)*gv%H_to_Z
210 hxt_here(i) = (h(i,j,k)*gv%H_to_Z)*t(i,j,k)
211 hxs_here(i) = (h(i,j,k)*gv%H_to_Z)*s(i,j,k)
213 h_here(i) = h_here(i) + h(i,j,k)*gv%H_to_Z
214 hxt_here(i) = hxt_here(i) + (h(i,j,k)*gv%H_to_Z)*t(i,j,k)
215 hxs_here(i) = hxs_here(i) + (h(i,j,k)*gv%H_to_Z)*s(i,j,k)
218 do i=is,ie ;
if (h_here(i) > 0.0)
then
219 hf(kf(i),i) = h_here(i)
220 tf(kf(i),i) = hxt_here(i) / h_here(i)
221 sf(kf(i),i) = hxs_here(i) / h_here(i)
224 do k=1,nz ;
do i=is,ie
225 if ((h_here(i) > hmin(i)) .and. (h(i,j,k)*gv%H_to_Z > hmin(i)))
then
226 hf(kf(i),i) = h_here(i) ; rf(kf(i),i) = hxr_here(i) / h_here(i)
230 h_here(i) = h(i,j,k)*gv%H_to_Z
231 hxr_here(i) = (h(i,j,k)*gv%H_to_Z)*gv%Rlay(k)
233 h_here(i) = h_here(i) + h(i,j,k)*gv%H_to_Z
234 hxr_here(i) = hxr_here(i) + (h(i,j,k)*gv%H_to_Z)*gv%Rlay(k)
237 do i=is,ie ;
if (h_here(i) > 0.0)
then
238 hf(kf(i),i) = h_here(i) ; rf(kf(i),i) = hxr_here(i) / h_here(i)
244 do i=is,ie ;
if (cn(i,j)>0.0)
then
246 ig = i + g%idg_offset ; jg = j + g%jdg_offset
249 if (g%mask2dT(i,j) > 0.5)
then
257 pres(k) = pres(k-1) + h_to_pres*hf(k-1,i)
258 t_int(k) = 0.5*(tf(k,i)+tf(k-1,i))
259 s_int(k) = 0.5*(sf(k,i)+sf(k-1,i))
262 kf(i)-1, tv%eqn_of_state)
268 drxh_sum = drxh_sum + 0.5*(hf(k-1,i)+hf(k,i)) * &
269 max(0.0,drho_dt(k)*(tf(k,i)-tf(k-1,i)) + &
270 drho_ds(k)*(sf(k,i)-sf(k-1,i)))
275 drxh_sum = drxh_sum + 0.5*(hf(k-1,i)+hf(k,i)) * &
276 max(0.0,rf(k,i)-rf(k-1,i))
282 if (drxh_sum >= 0.0)
then
287 hc(1) = hf(1,i) ; tc(1) = tf(1,i) ; sc(1) = sf(1,i)
289 if ((drho_dt(k)*(tf(k,i)-tc(kc)) + drho_ds(k)*(sf(k,i)-sc(kc))) * &
290 (hc(kc) + hf(k,i)) < 2.0 * tol2*drxh_sum)
then
292 i_hnew = 1.0 / (hc(kc) + hf(k,i))
293 tc(kc) = (hc(kc)*tc(kc) + hf(k,i)*tf(k,i)) * i_hnew
294 sc(kc) = (hc(kc)*sc(kc) + hf(k,i)*sf(k,i)) * i_hnew
295 hc(kc) = (hc(kc) + hf(k,i))
300 if ((drho_dt(k2)*(tc(k2)-tc(k2-1)) + drho_ds(k2)*(sc(k2)-sc(k2-1))) * &
301 (hc(k2) + hc(k2-1)) < tol2*drxh_sum)
then
303 i_hnew = 1.0 / (hc(kc) + hc(kc-1))
304 tc(kc-1) = (hc(kc)*tc(kc) + hc(kc-1)*tc(kc-1)) * i_hnew
305 sc(kc-1) = (hc(kc)*sc(kc) + hc(kc-1)*sc(kc-1)) * i_hnew
306 hc(kc-1) = (hc(kc) + hc(kc-1))
313 drho_ds(kc) = drho_ds(k) ; drho_dt(kc) = drho_dt(k)
314 tc(kc) = tf(k,i) ; sc(kc) = sf(k,i) ; hc(kc) = hf(k,i)
319 gprime(k) = g_rho0 * (drho_dt(k)*(tc(k)-tc(k-1)) + &
320 drho_ds(k)*(sc(k)-sc(k-1)))
325 hc(1) = hf(1,i) ; rc(1) = rf(1,i)
327 if ((rf(k,i) - rc(kc)) * (hc(kc) + hf(k,i)) < 2.0*tol2*drxh_sum)
then
329 rc(kc) = (hc(kc)*rc(kc) + hf(k,i)*rf(k,i)) / (hc(kc) + hf(k,i))
330 hc(kc) = (hc(kc) + hf(k,i))
335 if ((rc(k2)-rc(k2-1)) * (hc(k2)+hc(k2-1)) < tol2*drxh_sum)
then
337 rc(kc-1) = (hc(kc)*rc(kc) + hc(kc-1)*rc(kc-1)) / (hc(kc) + hc(kc-1))
338 hc(kc-1) = (hc(kc) + hc(kc-1))
345 rc(kc) = rf(k,i) ; hc(kc) = hf(k,i)
350 gprime(k) = g_rho0 * (rc(k)-rc(k-1))
362 if (kc >= modenum + 1)
then
368 igl(k) = 1.0/(gprime(k)*hc(k)) ; igu(k) = 1.0/(gprime(k)*hc(k-1))
369 z_int(k) = z_int(k-1) + hc(k-1)
370 n2(k) = us%m_to_Z**2*gprime(k)/(0.5*(hc(k)+hc(k-1)))
373 n2(1) = n2(2) ; n2(kc+1) = n2(kc)
375 z_int(kc+1) = z_int(kc)+hc(kc)
377 if (abs(z_int(kc+1)-htot(i,j)) > 1.e-14*htot(i,j))
then
378 call mom_error(fatal,
"wave_structure: mismatch in total depths")
391 gp_unscaled = us%m_to_Z*gprime(k)
392 lam_z(row) = lam*gp_unscaled
393 a_diag(row) = gp_unscaled*(-igu(k))
394 b_diag(row) = gp_unscaled*(igu(k)+igl(k)) - lam_z(row)
395 c_diag(row) = gp_unscaled*(-igl(k))
396 if (isnan(lam_z(row)))
then ; print *,
"Wave_structure: lam_z(row) is NAN" ;
endif
397 if (isnan(a_diag(row)))
then ; print *,
"Wave_structure: a(k) is NAN" ;
endif
398 if (isnan(b_diag(row)))
then ; print *,
"Wave_structure: b(k) is NAN" ;
endif
399 if (isnan(c_diag(row)))
then ; print *,
"Wave_structure: c(k) is NAN" ;
endif
403 gp_unscaled = us%m_to_Z*gprime(k)
404 lam_z(row) = lam*gp_unscaled
406 b_diag(row) = gp_unscaled*(igu(k)+igl(k)) - lam_z(row)
407 c_diag(row) = gp_unscaled*(-igl(k))
410 gp_unscaled = us%m_to_Z*gprime(k)
411 lam_z(row) = lam*gp_unscaled
412 a_diag(row) = gp_unscaled*(-igu(k))
413 b_diag(row) = gp_unscaled*(igu(k)+igl(k)) - lam_z(row)
417 e_guess(1:kc-1) = sin((z_int(2:kc)/htot(i,j)) *pi)
418 e_guess(1:kc-1) = e_guess(1:kc-1)/sqrt(sum(e_guess(1:kc-1)**2))
422 call tridiag_solver(a_diag(1:kc-1),b_diag(1:kc-1),c_diag(1:kc-1), &
423 -lam_z(1:kc-1),e_guess(1:kc-1),
"TDMA_H",e_itt)
424 e_guess(1:kc-1) = e_itt(1:kc-1)/sqrt(sum(e_itt(1:kc-1)**2))
426 w_strct(2:kc) = e_guess(1:kc-1)
431 ig_stop = 0 ; jg_stop = 0
432 if (isnan(sum(w_strct(1:kc+1))))
then
433 print *,
"Wave_structure: w_strct has a NAN at ig=", ig,
", jg=", jg
434 if (i<g%isc .or. i>g%iec .or. j<g%jsc .or. j>g%jec)
then
435 print *,
"This is occuring at a halo point."
437 ig_stop = ig ; jg_stop = jg
446 dz(k) = us%Z_to_m*hc(k)
447 w2avg = w2avg + 0.5*(w_strct(k)**2+w_strct(k+1)**2)*dz(k)
450 w2avg = w2avg / htot(i,j)
451 w_strct = w_strct / sqrt(htot(i,j)*w2avg*i_a_int)
455 u_strct(k) = 0.5*((w_strct(k-1) - w_strct(k) )/dz(k-1) + &
456 (w_strct(k) - w_strct(k+1))/dz(k))
458 u_strct(1) = (w_strct(1) - w_strct(2) )/dz(1)
459 u_strct(nzm) = (w_strct(nzm-1)- w_strct(nzm))/dz(nzm-1)
462 f2 = us%s_to_T**2 * g%CoriolisBu(i,j)**2
465 kmag2 = (freq**2 - f2) / (cn(i,j)**2 + cg_subro**2)
468 int_dwdz2 = 0.0 ; int_w2 = 0.0 ; int_n2w2 = 0.0
469 u_strct2 = u_strct(1:nzm)**2
470 w_strct2 = w_strct(1:nzm)**2
473 int_dwdz2 = int_dwdz2 + 0.5*(u_strct2(k)+u_strct2(k+1))*dz(k)
474 int_w2 = int_w2 + 0.5*(w_strct2(k)+w_strct2(k+1))*dz(k)
475 int_n2w2 = int_n2w2 + 0.5*(w_strct2(k)*n2(k)+w_strct2(k+1)*n2(k+1))*dz(k)
479 if (kmag2 > 0.0)
then
480 ke_term = 0.25*gv%Rho0*( (1+f2/freq**2)/kmag2*int_dwdz2 + int_w2 )
481 pe_term = 0.25*gv%Rho0*( int_n2w2/freq**2 )
482 if (en(i,j) >= 0.0)
then
483 w0 = sqrt( en(i,j)/(ke_term + pe_term) )
485 call mom_error(warning,
"wave_structure: En < 0.0; setting to W0 to 0.0")
486 print *,
"En(i,j)=", en(i,j),
" at ig=", ig,
", jg=", jg
490 w_profile = w0*w_strct
491 dwdz_profile = w0*u_strct
493 uavg_profile = abs(dwdz_profile) * sqrt((1+f2/freq**2)/(2.0*kmag2))
501 cs%w_strct(i,j,1:nzm) = w_strct(:)
502 cs%u_strct(i,j,1:nzm) = u_strct(:)
503 cs%W_profile(i,j,1:nzm) = w_profile(:)
504 cs%Uavg_profile(i,j,1:nzm)= uavg_profile(:)
505 cs%z_depths(i,j,1:nzm) = us%Z_to_m*z_int(:)
506 cs%N2(i,j,1:nzm) = n2(:)
507 cs%num_intfaces(i,j) = nzm
511 cs%w_strct(i,j,1:nzm) = 0.0
512 cs%u_strct(i,j,1:nzm) = 0.0
513 cs%W_profile(i,j,1:nzm) = 0.0
514 cs%Uavg_profile(i,j,1:nzm)= 0.0
515 cs%z_depths(i,j,1:nzm) = 0.0
516 cs%N2(i,j,1:nzm) = 0.0
517 cs%num_intfaces(i,j) = nzm
527 cs%w_strct(i,j,1:nzm) = 0.0
528 cs%u_strct(i,j,1:nzm) = 0.0
529 cs%W_profile(i,j,1:nzm) = 0.0
530 cs%Uavg_profile(i,j,1:nzm)= 0.0
531 cs%z_depths(i,j,1:nzm) = 0.0
532 cs%N2(i,j,1:nzm) = 0.0
533 cs%num_intfaces(i,j) = nzm
537 end subroutine wave_structure
542 subroutine tridiag_solver(a, b, c, h, y, method, x)
543 real,
dimension(:),
intent(in) :: a
544 real,
dimension(:),
intent(in) :: b
545 real,
dimension(:),
intent(in) :: c
546 real,
dimension(:),
intent(in) :: h
553 real,
dimension(:),
intent(in) :: y
554 character(len=*),
intent(in) :: method
555 real,
dimension(:),
intent(out) :: x
560 real,
allocatable,
dimension(:) :: c_prime, y_prime, q, alpha
562 real :: Q_prime, beta
567 allocate(c_prime(nrow))
568 allocate(y_prime(nrow))
570 allocate(alpha(nrow))
574 if (method ==
'TDMA_T')
then
577 c_prime(:) = 0.0 ; y_prime(:) = 0.0
578 c_prime(1) = c(1)/b(1) ; y_prime(1) = y(1)/b(1)
582 c_prime(k) = c(k)/(b(k)-a(k)*c_prime(k-1))
586 y_prime(k) = (y(k)-a(k)*y_prime(k-1))/(b(k)-a(k)*c_prime(k-1))
589 x(nrow) = y_prime(nrow)
593 x(k) = y_prime(k)-c_prime(k)*x(k+1)
612 elseif (method ==
'TDMA_H')
then
622 if (abs(a(k+1)-c(k)) > 1.e-10*(abs(a(k+1))+abs(c(k))))
then
623 call mom_error(warning,
"tridiag_solver: matrix not symmetric; need symmetry when invoking TDMA_H")
629 alpha(nrow) = b(nrow)-h(nrow)-alpha(nrow-1)
632 y_prime(:) = 0.0 ; q(:) = 0.0
633 y_prime(1) = beta*y(1) ; q(1) = beta*alpha(1)
638 beta = 1/(h(k)+alpha(k-1)*q_prime+alpha(k))
639 if (isnan(beta))
then ; print *,
"Tridiag_solver: beta is NAN" ;
endif
641 y_prime(k) = beta*(y(k)+alpha(k-1)*y_prime(k-1))
642 q_prime = beta*(h(k)+alpha(k-1)*q_prime)
644 if ((h(nrow)+alpha(nrow-1)*q_prime+alpha(nrow)) == 0.0)
then
645 call mom_error(fatal,
"Tridiag_solver: this system is not stable.")
648 beta = 1/(h(nrow)+alpha(nrow-1)*q_prime+alpha(nrow))
650 y_prime(nrow) = beta*(y(nrow)+alpha(nrow-1)*y_prime(nrow-1))
651 x(nrow) = y_prime(nrow)
654 x(k) = y_prime(k)+q(k)*x(k+1)
660 deallocate(c_prime,y_prime,q,alpha)
663 end subroutine tridiag_solver
666 subroutine wave_structure_init(Time, G, param_file, diag, CS)
667 type(time_type),
intent(in) :: time
671 type(
diag_ctrl),
target,
intent(in) :: diag
676 #include "version_variable.h"
677 character(len=40) :: mdl =
"MOM_wave_structure"
678 integer :: isd, ied, jsd, jed, nz
680 isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed ; nz = g%ke
682 if (
associated(cs))
then
683 call mom_error(warning,
"wave_structure_init called with an "// &
684 "associated control structure.")
686 else ;
allocate(cs) ;
endif
688 call get_param(param_file, mdl,
"INTERNAL_TIDE_SOURCE_X", cs%int_tide_source_x, &
689 "X Location of generation site for internal tide", default=1.)
690 call get_param(param_file, mdl,
"INTERNAL_TIDE_SOURCE_Y", cs%int_tide_source_y, &
691 "Y Location of generation site for internal tide", default=1.)
697 allocate(cs%w_strct(isd:ied,jsd:jed,nz+1))
698 allocate(cs%u_strct(isd:ied,jsd:jed,nz+1))
699 allocate(cs%W_profile(isd:ied,jsd:jed,nz+1))
700 allocate(cs%Uavg_profile(isd:ied,jsd:jed,nz+1))
701 allocate(cs%z_depths(isd:ied,jsd:jed,nz+1))
702 allocate(cs%N2(isd:ied,jsd:jed,nz+1))
703 allocate(cs%num_intfaces(isd:ied,jsd:jed))
708 end subroutine wave_structure_init