!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***s/r p_phystep_tl - Apply the TLM simplified physical processes: CMC/RPN package
*
#include "model_macros_f.h"
*

      subroutine p_phystep_tl (F_stepno) 1,11
      implicit none
*
      integer F_stepno
*
*author
*     Stephane Laroche        Janvier 2001
*
*revision
* v3_00 - Laroche S.       - initial MPI version
* v3_02 - Laroche S.       - allows physic variables outputs
* v3_11 - Laroche S.       - AIXport+Opti+OpenMP for TLM-ADJ
* v3_20 - Lee V.           - Introduce obussiz 
* v3_20 - Laroche S.       - Replace p_vmmphy by p_vmmphy_tl
* v3_30 - Tanguay M.       - adapt TL/AD to itf/new tendencies
*                          - Validation for LAM version
* v3_31 - Tanguay M.       - Introduce Ptopo_smtphy  
*
*object
*       Computes the physical processes and apply the resulting
*       tendencies to the dynamic variables of the model:
*       CMC/RPN package
*
*arguments
*  Name        I/O                 Description
*----------------------------------------------------------------
* F_stepno      O           step number
*----------------------------------------------------------------
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "mem.cdk"
#include "macro.cdk"
#include "ptopo.cdk"
#include "itf_phy_busind.cdk"
#include "dimout.cdk"
#include "itf_phy_buses.cdk"
#include "v4dg.cdk"
#include "p4d_simp.cdk"
*
*modules
      integer  open_db_file,rewind_db_file,close_db_file
      external open_db_file,rewind_db_file,close_db_file
      integer  omp_get_max_threads
      external omp_get_max_threads
**
      integer dim,err,i,j,k,obussiz
      real, dimension(:,:), allocatable :: obusval
      real, dimension(:), allocatable :: up,vp,wp,tp,qp,trp,
     $                                   um,vm,tm,trm,lnpsm,
     $                                   sig,kmm,ktm,
     $                                   upm,vpm,tpm,qpm,trpm,
     $                                   umm,vmm,tmm,    trmm
*
*     ---------------------------------------------------------------
*
      call iniobus(obussiz)
      allocate(obusval(p_ni*p_nj,obussiz))
      dim = (l_maxx-l_minx+1)*(l_maxy-l_miny+1)*l_nk
      allocate (up(dim),vp(dim),wp(dim),tp(dim),qp(dim),
     $          um(dim),vm(dim),tm(dim),lnpsm(dim),
     $          sig(dim),kmm(dim),ktm(dim),
     $          trp(dim*phyt_ntr),trm(dim*phyt_ntr),
     $          upm(dim),vpm(dim),tpm(dim),qpm(dim),
     $          umm(dim),vmm(dim),tmm(dim),
     $          trpm(dim),trmm(dim))
*
*C       2.	load all fields required by the physic in memory
*		------------------------------------------------
*
      call p_vmmphy_tl ( up, vp, wp, tp, qp, trp,
     $                   um, vm,     tm,     trm,
     $                   lnpsm, sig, LDIST_DIM,l_nk )
*
*
*       Read TRAJ for the simplified physics
*       -------------------------------------
        call v4d_rwtraj_sigma  (sig,LDIST_DIM,l_nk)
        call v4d_rwtraj_phystep(upm,vpm,tpm,qpm,trpm,
     $                          umm,vmm,tmm,    trmm,
     $                          LDIST_DIM,l_nk)
        if( P4d_pbl.eq.2) call v4d_rwtraj_kmkt(kmm,ktm,LDIST_DIM,l_nk)

*
*
*C       4.	compute physics tendencies
*		--------------------------
*	initialize the number of slices previously done
*
      Mem_pslic = 0
*
      if (.not.Mem_phyncore_L) then
         err = open_db_file   (Lun_waphy)
         err = rewind_db_file (Lun_waphy)
      endif
*
      call pe_rebind (Ptopo_smtphy,(Ptopo_myproc.eq.0).and.
     $                                 (F_stepno.eq.0))
!$omp parallel
!$omp do
      do j=1,omp_get_max_threads()
      call p_physlb_tl ( j, F_stepno,obusval,
     $                   up, vp, wp, tp, qp,  trp,
     $                   um, vm,     tm,      trm,
     $                   upm, vpm,  tpm, qpm, trpm,
     $                   umm, vmm,  tmm,      trmm,
     $                   lnpsm, sig, kmm, ktm, LDIST_DIM, l_nk)
      enddo
!$omp enddo
!$omp end parallel
      call pe_rebind (Ptopo_smtdyn,(Ptopo_myproc.eq.0).and.
     $                                 (F_stepno.eq.0))
*
      call out_phy(obusval,p_ni,p_nj,F_stepno)
      deallocate(obusval)
*
      if (.not.Mem_phyncore_L) err = close_db_file (Lun_waphy)
*
*C   6.	Apply physics tendencies to the appropriate dynamic fields
*C      and insure consistency with other dynamic variables
*       ----------------------------------------------------------
*
      call p_apply_tl ( um, vm, tm, trm, tp, qp,
     $                  tpm, trpm, qpm,
     $                  LDIST_DIM, l_nk)
*

      deallocate (up,vp,wp,tp,qp,trp,um,vm,tm,trm,lnpsm,sig,kmm,ktm,
     $            upm,vpm,tpm,qpm,trpm,umm,vmm,tmm,trmm)
*
*
*     ---------------------------------------------------------------
*
      return
      end