!-------------------------------------- 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 pre - add metric corrections to r.h.s. of momentum equations
*            compute advective contributions on geopotential grid
*            interpolate adv. cont. from geopotential grid to wind grids
*            update r.h.s with advective contributions
*            add contribution of topography to rhs of momentum equations
*            compute rhs of divergence equation
*            compute rhs of combined horizontal equations
*            compute the linear rhs of Helmholtz equation
*            ( memory management and call to prep )
*
#include "model_macros_f.h"
*

      subroutine pre 1,1
*
      implicit none
*
*author
*     Alain Patoine - Gabriel Lemay
*
*revision
* v2_00 - Desgagne M.       - initial MPI version
* v2_31 - Desgagne M.       - remove stkmemw
* v3_00 - Desgagne & Lee    - Lam configuration
*
*object
*******************************************************************************
*                                                                             *
* First, add metric corrections to the RHS of momentum equations              *
*                                                                             *
*  R\' =   C   R   +  C   R                                                   *
*    U      xx  U      xy  V                                                  *
*                                                                             *
*                                                                             *
*  R\' =   C   R   +  C   R                                                   *
*    V      yx  U      yy  V                                                  *
*                                                                             *
* where:                                                                      *
*                          _   _                              _   _           *
*                 /        r . A     \               /        r . A     \     *
*         _   _   | _           x  _ |           _   | _           x  _ |     *
*  C   =  k . r ^ | A  -  -------- c |     C   = k . | A  -  -------- c |     *
*   xx            |  x     _   _     |      yx       |  x     _   _     |     *
*                 \        r . c     /               \        r . c     /     *
*                                                                             *
*                          _   _                              _   _           *
*                 /        r . A     \               /        r . A     \     *
*         _   _   | _           y  _ |           _   | _           y  _ |     *
*  C   =  k . r ^ | A  -  -------- c |     C   = k . | A  -  -------- c |     *
*   xy            |  y     _   _     |      yy       |  y     _   _     |     *
*                 \        r . c     /               \        r . c     /     *
*                                                                             *
*  _      1             _         _                                           *
*  A  = ----- ( - sin x i + cos x j )                                         *
*   x   cos y                                                                 *
*                                                                             *
*  _      1                   _               _         _                     *
*  A  = ----- ( - cos x sin y i - sin x sin y j + cos y k )                   *
*   y   cos y                                                                 *
*                                                                             *
*  _     _          b1  _                                                     *
*  c  =  r(t0)  +  ---- r(t1)                                                 *
*                   b0                                                        *
*                                                                             *
*******************************************************************************
*                                                                             *
* Then calculate the advective contributions #Ru & #Rv on G-grid,             *
* and interpolate them from G- to U- & V-grid.                                *
*                                                                             *
* Consequently, we get the final form of RHS of horizontal momentum equation  *
* by adding the advective and topographic contributions to that downstream:   *
*                                                                             *
*                      1   dphi                                               *
*                              s                                              *
* R" =  R  +  #Ru  -  --- ------                                         (ru) *
*  U     U              2                                                     *
*                      a    dx                                                *
*                                                                             *
*                                                                             *
*                      C   dphi                                               *
*                              s                                              *
* R" =  R  +  #Rv  -  --- ------                                         (rv) *
*  V     V              2                                                     *
*                      a    dy                                                *
*                                                                             *
*                                                                             *
*                      /  dR"       dR"  \                                    *
*                   1  |    U         V  |                                    *
* then compute R  = -- | ----- + C ----- |                               (rd) *
*               D    2 |                 |                                    *
*                   C  \  dx        dy   /                                    *
*                                                                             *
*******************************************************************************
*            1                                                                *
* R  = R  - --- R                                                        (r1) *
*  1    D   tau  cn                                                           *
*                                                                             *
*******************************************************************************
*                                                                             *
*                      &    /                \       _____________________    *
*         1             H   |        1       |      |                     |   *
* R  =  ----- R   +  ------ | R   - --- phi  |      | NONHYDROSTATIC ONLY |   *
*  3    g tau  w      2   2 |  vv   tau    s |      |_____________________|   *
*                    g tau  \                /                                *
*                                                                             *
*******************************************************************************
*                                                    _____________________    *
* We are now ready to calculate the                 |                     |   *
* linear RHS of Helmholtz equation:                 | NONHYDROSTATIC ONLY |   *
*                                                   |_____________________|   *
*                                      |                                      *
*                                      |                                      *
*       / _z \+     gamma   / ___z  \- |   gamma   / ___z \-   gamma / __z \+ *
* R  =  | R  |  + --------- | z R   |  + --------- | z R  |  + ----- | R'  |  *
*  h    \  1 /    cappa tau \    th /  | cappa tau \    3 /     tau  \  3  /  *
*                                      |                                      *
*                                      |   where                              *
*                                      |                       R  T*          *
*                                      |                        d             *
* The involved operators are defined   |   R'  =  R   -  &  ------------ R    *
* as in Cote et al., 1990, M.W.R. 118, |    3      3      H        2   2  th  *
* 2707-2717:                           |                    cappa g tau       *
*                                                                             *
*                                                                             *
* / _z \+     P(k-1) + P(k) dz(k-1)     P(k) + P(k+1) dz(k)                   *
* | P  |   =  ------------- -------  +  ------------- -----                   *
* \    /k           2          2              2         2                     *
*                                                                             *
* / _z \-     M(k-1) + M(k)     M(k) + M(k+1)     M(k-1) - M(k+1)             *
* | M  |   =  -------------  -  -------------  =  ---------------             *
* \    /k           2                 2                  2                    *
*                                                                             *
*                                                                             *
*******************************************************************************
*
*arguments
*        None
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "geomg.cdk"
#include "rhsc.cdk"
#include "vt1.cdk"
#include "p_geof.cdk"
#include "schm.cdk"
#include "lctl.cdk"

*
*modules
      integer  vmmlod, vmmget, vmmuld
      external vmmlod, vmmget, vmmuld
*
      integer pnerr, pnlod, pnlkey1(19), i, j
      real wijk1(LDIST_SHAPE,l_nk),wijk2(LDIST_SHAPE,l_nk)
**
*     __________________________________________________________________
*
      if (Lun_debug_L) write (Lun_out,1000)
*
      pnlkey1(1)  = VMM_KEY(ru)
      pnlkey1(2)  = VMM_KEY(rv)
      pnlkey1(3)  = VMM_KEY(ruw1)
      pnlkey1(4)  = VMM_KEY(ruw2)
      pnlkey1(5)  = VMM_KEY(rvw1)
      pnlkey1(6)  = VMM_KEY(rvw2)
      pnlkey1(7)  = VMM_KEY(xct1)
      pnlkey1(8)  = VMM_KEY(yct1)
      pnlkey1(9)  = VMM_KEY(zct1)
      pnlkey1(10) = VMM_KEY(topo)
      pnlkey1(11) = VMM_KEY(rd)
      pnlkey1(12) = VMM_KEY(rcn)
      pnlkey1(13) = VMM_KEY(r1)
      pnlkey1(14) = VMM_KEY(rth)
      pnlkey1(15) = VMM_KEY(rhell)
      pnlod = 15
      if (.not. Schm_hydro_L) then
         pnlkey1(16) = VMM_KEY(rw)
         pnlkey1(17) = VMM_KEY(rvv)
         pnlkey1(18) = VMM_KEY(r3)
         pnlkey1(19) = VMM_KEY(r3p)
         pnlod = 19
      endif
*     - - - - - - - - - - - - - - -
      pnerr = vmmlod(pnlkey1,pnlod)
*     - - - - - - - - - - - - - - -
      pnerr = VMM_GET_VAR(ru)
      pnerr = VMM_GET_VAR(rv)
      pnerr = VMM_GET_VAR(ruw1)
      pnerr = VMM_GET_VAR(ruw2)
      pnerr = VMM_GET_VAR(rvw1)
      pnerr = VMM_GET_VAR(rvw2)
      pnerr = VMM_GET_VAR(xct1)
      pnerr = VMM_GET_VAR(yct1)
      pnerr = VMM_GET_VAR(zct1)
      pnerr = VMM_GET_VAR(topo)
      pnerr = VMM_GET_VAR(rd)
      pnerr = VMM_GET_VAR(rcn)
      pnerr = VMM_GET_VAR(r1)
      pnerr = VMM_GET_VAR(rth)
      pnerr = VMM_GET_VAR(rhell)
      if (.not. Schm_hydro_L) then
         pnerr = VMM_GET_VAR(rw)
         pnerr = VMM_GET_VAR(rvv)
         pnerr = VMM_GET_VAR(r3)
         pnerr = VMM_GET_VAR(r3p)
      else
         rw_  = 0
         rvv_ = 0
         r3_  = 0
         r3p_ = 0
      endif
*
c     if (Acid_test_L)then
c     if (Lun_out.gt.0) write(Lun_out,*) 'Before prep_2'
c     call glbstat (xct1,'Xct1',1,l_ni,1,l_nj,G_nk,6+acid_i0,G_ni-5-acid_in,
c    %                                             6+acid_j0,G_nj-5-acid_jn,1,1)
c     call glbstat (topo,'FIS',LDIST_DIM,1,1+acid_i0,G_ni-acid_in,
c    %                                     1+acid_j0,G_nj-acid_jn,1,1)
c     call glbstat (ru,'RU0',LDIST_DIM,G_nk,7+acid_i0,G_ni-7-acid_in,
c    %                                      7+acid_j0,G_nj-7-acid_jn,1,G_nk)
c     call glbstat (ruw1,'RUW1',LDIST_DIM,G_nk,6+acid_i0,G_ni-5-acid_in,
c    %                                         5+acid_j0,G_nj-4-acid_jn,1,G_nk)
c     call glbstat (ruw2,'RUW2',LDIST_DIM,G_nk,6+acid_i0,G_ni-5-acid_in,
c    %                                         5+acid_j0,G_nj-4-acid_jn,1,G_nk)
c     call glbstat (rcn,'RCN',LDIST_DIM,G_nk,8+acid_i0,G_ni-7-acid_in,
c    %                                       8+acid_j0,G_nj-7-acid_jn,1,G_nk)
c     endif
*
      call prep_2 ( ru,rv, ruw1,ruw2,rvw1,rvw2,xct1,yct1,zct1,topo, 
     %              rd,rcn,r1,rth, rw,rvv,r3,r3p, rhell,
     %              wijk1,wijk2,LDIST_DIM,l_ni,l_nj,l_nk )
*
* The following 5 glbstats will not match piloting run because they
* contain the future Ru,Rv inserted in RHS, Ru,Rv,Rd,R1,Rhell
* But glbstats should match for Rcn,Rth
c     if (Acid_test_L)then
c     if (Lun_out.gt.0) then
c         write(Lun_out,*) 'After prep_2'
c         write(Lun_out,*) 'STATS will not match, contains future RU,RV'
c     endif
c     call glbstat (ru,'RU',LDIST_DIM,G_nk,8+acid_i0,G_ni-7-acid_in,
c    %                                     8+acid_j0,G_nj-7-acid_jn,1,G_nk)
*
      pnerr = vmmuld(-1,0)
*
1000  format(3X,'PRE-COMPUTATION FOR THE HELMHOLTZ PROBLEM: (S/R PRE)')
*     __________________________________________________________________
*
      return
      end