MODULE frc_weak_mod ! !git $Id$ !svn $Id: frc_weak.F 1180 2023-07-13 02:42:10Z arango $ !================================================== Hernan G. Arango === ! Copyright (c) 2002-2023 The ROMS/TOMS Group Andrew M. Moore ! ! Licensed under a MIT/X style license ! ! See License_ROMS.md ! !======================================================================= ! ! ! These routines are used during the computation of the weak ! ! constraint forcing. ! ! ! !======================================================================= ! implicit none PRIVATE PUBLIC :: frc_ADgather PUBLIC :: frc_clear CONTAINS SUBROUTINE frc_ADgather (ng, tile) ! !======================================================================= ! ! ! This subroutine is the adjoint of the weak constraint forcing ! ! interpolation between snapshots used in the tangent linear and ! ! representer models. ! ! ! ! On Input: ! ! ! ! ng Nested grid number. ! ! tile Domain partition. ! ! ! !======================================================================= ! USE mod_param USE mod_forces USE mod_ocean ! ! Imported variable declarations. ! integer, intent(in) :: ng, tile ! ! Local variable declarations. ! character (len=*), parameter :: MyFile = & & "ROMS/Utility/frc_weak.F" ! integer :: IminS, ImaxS, JminS, JmaxS integer :: LBi, UBi, LBj, UBj, LBij, UBij ! ! Set horizontal starting and ending indices for automatic private ! storage arrays. ! IminS=BOUNDS(ng)%Istr(tile)-3 ImaxS=BOUNDS(ng)%Iend(tile)+3 JminS=BOUNDS(ng)%Jstr(tile)-3 JmaxS=BOUNDS(ng)%Jend(tile)+3 ! ! Determine array lower and upper bounds in the I- and J-directions. ! LBi=BOUNDS(ng)%LBi(tile) UBi=BOUNDS(ng)%UBi(tile) LBj=BOUNDS(ng)%LBj(tile) UBj=BOUNDS(ng)%UBj(tile) ! ! Set array lower and upper bounds for MIN(I,J) directions and ! MAX(I,J) directions. ! LBij=BOUNDS(ng)%LBij UBij=BOUNDS(ng)%UBij ! CALL wclock_on (ng, iADM, 7, 58, MyFile) CALL frc_ADgather_tile (ng, tile, & & LBi, UBi, LBj, UBj, & & IminS, ImaxS, JminS, JmaxS, & & OCEAN(ng) % ad_zeta, & & OCEAN(ng) % ad_zeta_sol, & & OCEAN(ng) % ad_u, & & OCEAN(ng) % ad_v, & & OCEAN(ng) % ad_t, & & OCEAN(ng) % f_zetaG, & & OCEAN(ng) % f_uG, & & OCEAN(ng) % f_vG, & & OCEAN(ng) % f_tG) CALL wclock_off (ng, iADM, 7, 85, MyFile) ! RETURN END SUBROUTINE frc_ADgather ! !*********************************************************************** SUBROUTINE frc_ADgather_tile (ng, tile, & & LBi, UBi, LBj, UBj, & & IminS, ImaxS, JminS, JmaxS, & & ad_zeta, ad_zeta_sol, & & ad_u, ad_v, ad_t, & & f_zetaG, & & f_uG, f_vG, f_tG) !*********************************************************************** ! USE mod_param USE mod_scalars USE mod_stepping USE mod_fourdvar ! ! Imported variable declarations. ! integer, intent(in) :: ng, tile integer, intent(in) :: LBi, UBi, LBj, UBj integer, intent(in) :: IminS, ImaxS, JminS, JmaxS ! real(r8), intent(in) :: ad_zeta(LBi:,LBj:,:) real(r8), intent(in) :: ad_zeta_sol(LBi:,LBj:) real(r8), intent(inout) :: f_zetaG(LBi:,LBj:,:) real(r8), intent(in) :: ad_u(LBi:,LBj:,:,:) real(r8), intent(in) :: ad_v(LBi:,LBj:,:,:) real(r8), intent(in) :: ad_t(LBi:,LBj:,:,:,:) real(r8), intent(inout) :: f_uG(LBi:,LBj:,:,:) real(r8), intent(inout) :: f_vG(LBi:,LBj:,:,:) real(r8), intent(inout) :: f_tG(LBi:,LBj:,:,:,:) ! ! Local variable declarations. ! integer :: i, it1, it2, j, k, kout integer :: itrc, nout real(r8) :: fac, fac1, fac2, time1, time2 ! !----------------------------------------------------------------------- ! Set lower and upper tile bounds and staggered variables bounds for ! this horizontal domain partition. Notice that if tile=-1, it will ! set the values for the global grid. !----------------------------------------------------------------------- ! integer :: Istr, IstrB, IstrP, IstrR, IstrT, IstrM, IstrU integer :: Iend, IendB, IendP, IendR, IendT integer :: Jstr, JstrB, JstrP, JstrR, JstrT, JstrM, JstrV integer :: Jend, JendB, JendP, JendR, JendT integer :: Istrm3, Istrm2, Istrm1, IstrUm2, IstrUm1 integer :: Iendp1, Iendp2, Iendp2i, Iendp3 integer :: Jstrm3, Jstrm2, Jstrm1, JstrVm2, JstrVm1 integer :: Jendp1, Jendp2, Jendp2i, Jendp3 ! Istr =BOUNDS(ng) % Istr (tile) IstrB =BOUNDS(ng) % IstrB (tile) IstrM =BOUNDS(ng) % IstrM (tile) IstrP =BOUNDS(ng) % IstrP (tile) IstrR =BOUNDS(ng) % IstrR (tile) IstrT =BOUNDS(ng) % IstrT (tile) IstrU =BOUNDS(ng) % IstrU (tile) Iend =BOUNDS(ng) % Iend (tile) IendB =BOUNDS(ng) % IendB (tile) IendP =BOUNDS(ng) % IendP (tile) IendR =BOUNDS(ng) % IendR (tile) IendT =BOUNDS(ng) % IendT (tile) Jstr =BOUNDS(ng) % Jstr (tile) JstrB =BOUNDS(ng) % JstrB (tile) JstrM =BOUNDS(ng) % JstrM (tile) JstrP =BOUNDS(ng) % JstrP (tile) JstrR =BOUNDS(ng) % JstrR (tile) JstrT =BOUNDS(ng) % JstrT (tile) JstrV =BOUNDS(ng) % JstrV (tile) Jend =BOUNDS(ng) % Jend (tile) JendB =BOUNDS(ng) % JendB (tile) JendP =BOUNDS(ng) % JendP (tile) JendR =BOUNDS(ng) % JendR (tile) JendT =BOUNDS(ng) % JendT (tile) ! Istrm3 =BOUNDS(ng) % Istrm3 (tile) ! Istr-3 Istrm2 =BOUNDS(ng) % Istrm2 (tile) ! Istr-2 Istrm1 =BOUNDS(ng) % Istrm1 (tile) ! Istr-1 IstrUm2=BOUNDS(ng) % IstrUm2(tile) ! IstrU-2 IstrUm1=BOUNDS(ng) % IstrUm1(tile) ! IstrU-1 Iendp1 =BOUNDS(ng) % Iendp1 (tile) ! Iend+1 Iendp2 =BOUNDS(ng) % Iendp2 (tile) ! Iend+2 Iendp2i=BOUNDS(ng) % Iendp2i(tile) ! Iend+2 interior Iendp3 =BOUNDS(ng) % Iendp3 (tile) ! Iend+3 Jstrm3 =BOUNDS(ng) % Jstrm3 (tile) ! Jstr-3 Jstrm2 =BOUNDS(ng) % Jstrm2 (tile) ! Jstr-2 Jstrm1 =BOUNDS(ng) % Jstrm1 (tile) ! Jstr-1 JstrVm2=BOUNDS(ng) % JstrVm2(tile) ! JstrV-2 JstrVm1=BOUNDS(ng) % JstrVm1(tile) ! JstrV-1 Jendp1 =BOUNDS(ng) % Jendp1 (tile) ! Jend+1 Jendp2 =BOUNDS(ng) % Jendp2 (tile) ! Jend+2 Jendp2i=BOUNDS(ng) % Jendp2i(tile) ! Jend+2 interior Jendp3 =BOUNDS(ng) % Jendp3 (tile) ! Jend+3 ! !----------------------------------------------------------------------- ! Compute weak-constraint forcing terms used by the tangent linear ! and representer models. Perform the adjoint of the interpolation ! between snapshots. !----------------------------------------------------------------------- ! ! Set time records and interpolation factor, if any. ! it1=MAX(0,(iic(ng)-1)/nADJ(ng))+1 it2=it1+1 time1=dstart*day2sec+(it1-1)*nADJ(ng)*dt(ng) time2=dstart*day2sec+(it2-1)*nADJ(ng)*dt(ng) fac1=time2-time(ng) fac2=time(ng)-time1 fac=1.0_r8/(fac1+fac2) fac1=fac*fac1 fac2=fac*fac2 ! ! Set weak-constraint force time. ! ForceTime(ng)=time2 ! ! Determine time index of adjoint variables to process. ! kout=kstp(ng) IF (iic(ng).ne.ntend(ng)) THEN nout=nnew(ng) ELSE nout=nstp(ng) END IF ! ! Clear forcing arrays on first call to this routine. ! IF (iic(ng).eq.ntstart(ng)-1) THEN DO j=JstrR,JendR DO i=IstrR,IendR f_zetaG(i,j,1)=0.0_r8 f_zetaG(i,j,2)=0.0_r8 END DO END DO DO k=1,N(ng) DO j=JstrR,JendR DO i=Istr,IendR f_uG(i,j,k,1)=0.0_r8 f_uG(i,j,k,2)=0.0_r8 END DO END DO END DO DO k=1,N(ng) DO j=Jstr,JendR DO i=IstrR,IendR f_vG(i,j,k,1)=0.0_r8 f_vG(i,j,k,2)=0.0_r8 END DO END DO END DO DO itrc=1,NT(ng) DO k=1,N(ng) DO j=JstrR,JendR DO i=IstrR,IendR f_tG(i,j,k,1,itrc)=0.0_r8 f_tG(i,j,k,2,itrc)=0.0_r8 END DO END DO END DO END DO END IF ! ! Gather free-surface weak-constraint forcing terms. ! IF (LwrtState2d(ng)) THEN DO j=JstrR,JendR DO i=IstrR,IendR f_zetaG(i,j,1)=f_zetaG(i,j,1)+fac1*ad_zeta(i,j,kout) f_zetaG(i,j,2)=f_zetaG(i,j,2)+fac2*ad_zeta(i,j,kout) END DO END DO ELSE DO j=JstrR,JendR DO i=IstrR,IendR f_zetaG(i,j,1)=f_zetaG(i,j,1)+fac1*ad_zeta_sol(i,j) f_zetaG(i,j,2)=f_zetaG(i,j,2)+fac2*ad_zeta_sol(i,j) END DO END DO END IF ! ! Gather 3D-momentum weak-constraint forcing terms. ! DO k=1,N(ng) DO j=JstrR,JendR DO i=Istr,IendR f_uG(i,j,k,1)=f_uG(i,j,k,1)+fac1*ad_u(i,j,k,nout) f_uG(i,j,k,2)=f_uG(i,j,k,2)+fac2*ad_u(i,j,k,nout) END DO END DO END DO DO k=1,N(ng) DO j=Jstr,JendR DO i=IstrR,IendR f_vG(i,j,k,1)=f_vG(i,j,k,1)+fac1*ad_v(i,j,k,nout) f_vG(i,j,k,2)=f_vG(i,j,k,2)+fac2*ad_v(i,j,k,nout) END DO END DO END DO ! ! Gather tracer weak-constraint forcing terms. ! DO itrc=1,NT(ng) DO k=1,N(ng) DO j=JstrR,JendR DO i=IstrR,IendR f_tG(i,j,k,1,itrc)=f_tG(i,j,k,1,itrc)+ & & fac1*ad_t(i,j,k,nout,itrc) f_tG(i,j,k,2,itrc)=f_tG(i,j,k,2,itrc)+ & & fac2*ad_t(i,j,k,nout,itrc) END DO END DO END DO END DO ! RETURN END SUBROUTINE frc_ADgather_tile ! SUBROUTINE frc_clear (ng, tile) ! !======================================================================= ! ! ! This routine copy weak-constraint arrays (f_***G storage arrays) ! ! index 1 into index 2 and then clear index 1. ! ! ! ! On Input: ! ! ! ! ng Nested grid number. ! ! tile Domain partition. ! ! ! !======================================================================= ! USE mod_param USE mod_forces USE mod_ocean ! ! Imported variable declarations. ! integer, intent(in) :: ng, tile ! ! Local variable declarations. ! character (len=*), parameter :: MyFile = & & "ROMS/Utility/frc_weak.F"//", frc_clear" ! integer :: IminS, ImaxS, JminS, JmaxS integer :: LBi, UBi, LBj, UBj, LBij, UBij ! ! Set horizontal starting and ending indices for automatic private ! storage arrays. ! IminS=BOUNDS(ng)%Istr(tile)-3 ImaxS=BOUNDS(ng)%Iend(tile)+3 JminS=BOUNDS(ng)%Jstr(tile)-3 JmaxS=BOUNDS(ng)%Jend(tile)+3 ! ! Determine array lower and upper bounds in the I- and J-directions. ! LBi=BOUNDS(ng)%LBi(tile) UBi=BOUNDS(ng)%UBi(tile) LBj=BOUNDS(ng)%LBj(tile) UBj=BOUNDS(ng)%UBj(tile) ! ! Set array lower and upper bounds for MIN(I,J) directions and ! MAX(I,J) directions. ! LBij=BOUNDS(ng)%LBij UBij=BOUNDS(ng)%UBij ! CALL wclock_on (ng, iADM, 7, 436, MyFile) CALL frc_clear_tile (ng, tile, & & LBi, UBi, LBj, UBj, & & IminS, ImaxS, JminS, JmaxS, & & OCEAN(ng) % f_zetaG, & & OCEAN(ng) % f_uG, & & OCEAN(ng) % f_vG, & & OCEAN(ng) % f_tG) CALL wclock_off (ng, iADM, 7, 451, MyFile) ! RETURN END SUBROUTINE frc_clear ! !*********************************************************************** SUBROUTINE frc_clear_tile (ng, tile, & & LBi, UBi, LBj, UBj, & & IminS, ImaxS, JminS, JmaxS, & & f_zetaG, & & f_uG, f_vG, f_tG) !*********************************************************************** ! USE mod_param USE mod_scalars USE mod_stepping USE mod_fourdvar ! ! Imported variable declarations. ! integer, intent(in) :: ng, tile integer, intent(in) :: LBi, UBi, LBj, UBj integer, intent(in) :: IminS, ImaxS, JminS, JmaxS ! real(r8), intent(inout) :: f_zetaG(LBi:,LBj:,:) real(r8), intent(inout) :: f_uG(LBi:,LBj:,:,:) real(r8), intent(inout) :: f_vG(LBi:,LBj:,:,:) real(r8), intent(inout) :: f_tG(LBi:,LBj:,:,:,:) ! ! Local variable declarations. ! integer :: i, it1, it2, j, k, kout, nout integer :: itrc real(r8) :: fac, fac1, fac2, time1, time2 ! !----------------------------------------------------------------------- ! Set lower and upper tile bounds and staggered variables bounds for ! this horizontal domain partition. Notice that if tile=-1, it will ! set the values for the global grid. !----------------------------------------------------------------------- ! integer :: Istr, IstrB, IstrP, IstrR, IstrT, IstrM, IstrU integer :: Iend, IendB, IendP, IendR, IendT integer :: Jstr, JstrB, JstrP, JstrR, JstrT, JstrM, JstrV integer :: Jend, JendB, JendP, JendR, JendT integer :: Istrm3, Istrm2, Istrm1, IstrUm2, IstrUm1 integer :: Iendp1, Iendp2, Iendp2i, Iendp3 integer :: Jstrm3, Jstrm2, Jstrm1, JstrVm2, JstrVm1 integer :: Jendp1, Jendp2, Jendp2i, Jendp3 ! Istr =BOUNDS(ng) % Istr (tile) IstrB =BOUNDS(ng) % IstrB (tile) IstrM =BOUNDS(ng) % IstrM (tile) IstrP =BOUNDS(ng) % IstrP (tile) IstrR =BOUNDS(ng) % IstrR (tile) IstrT =BOUNDS(ng) % IstrT (tile) IstrU =BOUNDS(ng) % IstrU (tile) Iend =BOUNDS(ng) % Iend (tile) IendB =BOUNDS(ng) % IendB (tile) IendP =BOUNDS(ng) % IendP (tile) IendR =BOUNDS(ng) % IendR (tile) IendT =BOUNDS(ng) % IendT (tile) Jstr =BOUNDS(ng) % Jstr (tile) JstrB =BOUNDS(ng) % JstrB (tile) JstrM =BOUNDS(ng) % JstrM (tile) JstrP =BOUNDS(ng) % JstrP (tile) JstrR =BOUNDS(ng) % JstrR (tile) JstrT =BOUNDS(ng) % JstrT (tile) JstrV =BOUNDS(ng) % JstrV (tile) Jend =BOUNDS(ng) % Jend (tile) JendB =BOUNDS(ng) % JendB (tile) JendP =BOUNDS(ng) % JendP (tile) JendR =BOUNDS(ng) % JendR (tile) JendT =BOUNDS(ng) % JendT (tile) ! Istrm3 =BOUNDS(ng) % Istrm3 (tile) ! Istr-3 Istrm2 =BOUNDS(ng) % Istrm2 (tile) ! Istr-2 Istrm1 =BOUNDS(ng) % Istrm1 (tile) ! Istr-1 IstrUm2=BOUNDS(ng) % IstrUm2(tile) ! IstrU-2 IstrUm1=BOUNDS(ng) % IstrUm1(tile) ! IstrU-1 Iendp1 =BOUNDS(ng) % Iendp1 (tile) ! Iend+1 Iendp2 =BOUNDS(ng) % Iendp2 (tile) ! Iend+2 Iendp2i=BOUNDS(ng) % Iendp2i(tile) ! Iend+2 interior Iendp3 =BOUNDS(ng) % Iendp3 (tile) ! Iend+3 Jstrm3 =BOUNDS(ng) % Jstrm3 (tile) ! Jstr-3 Jstrm2 =BOUNDS(ng) % Jstrm2 (tile) ! Jstr-2 Jstrm1 =BOUNDS(ng) % Jstrm1 (tile) ! Jstr-1 JstrVm2=BOUNDS(ng) % JstrVm2(tile) ! JstrV-2 JstrVm1=BOUNDS(ng) % JstrVm1(tile) ! JstrV-1 Jendp1 =BOUNDS(ng) % Jendp1 (tile) ! Jend+1 Jendp2 =BOUNDS(ng) % Jendp2 (tile) ! Jend+2 Jendp2i=BOUNDS(ng) % Jendp2i(tile) ! Jend+2 interior Jendp3 =BOUNDS(ng) % Jendp3 (tile) ! Jend+3 ! !----------------------------------------------------------------------- ! Copy weak-constraint forcing arrays index 1 into index 2, and ! clear index 1. !----------------------------------------------------------------------- ! ! Reset weak-constraing forcing time on last timestep. ! IF (iic(ng).eq.ntend(ng)) THEN ForceTime(ng)=dstart*day2sec END IF ! ! Update free-surface weak-constraint forcing terms. ! DO j=JstrR,JendR DO i=IstrR,IendR f_zetaG(i,j,2)=f_zetaG(i,j,1) f_zetaG(i,j,1)=0.0_r8 END DO END DO ! ! Update 3D-momentum weak-constraint forcing terms. ! DO k=1,N(ng) DO j=JstrR,JendR DO i=Istr,IendR f_uG(i,j,k,2)=f_uG(i,j,k,1) f_uG(i,j,k,1)=0.0_r8 END DO END DO DO j=Jstr,JendR DO i=IstrR,IendR f_vG(i,j,k,2)=f_vG(i,j,k,1) f_vG(i,j,k,1)=0.0_r8 END DO END DO END DO ! ! Update tracer weak-constraint forcing terms. ! DO itrc=1,NT(ng) DO k=1,N(ng) DO j=JstrR,JendR DO i=IstrR,IendR f_tG(i,j,k,2,itrc)=f_tG(i,j,k,1,itrc) f_tG(i,j,k,1,itrc)=0.0_r8 END DO END DO END DO END DO ! RETURN END SUBROUTINE frc_clear_tile END MODULE frc_weak_mod