Index: trunk/LMDZ.MARS/libf/dynphy_lonlat/mod_interface_dyn_phys.F90
===================================================================
--- trunk/LMDZ.MARS/libf/dynphy_lonlat/mod_interface_dyn_phys.F90	(revision 1543)
+++ trunk/LMDZ.MARS/libf/dynphy_lonlat/mod_interface_dyn_phys.F90	(revision 1543)
@@ -0,0 +1,62 @@
+! 
+! $Id: mod_interface_dyn_phys.F90 2351 2015-08-25 15:14:59Z emillour $
+!
+MODULE mod_interface_dyn_phys
+  INTEGER,SAVE,dimension(:),allocatable :: index_i
+  INTEGER,SAVE,dimension(:),allocatable :: index_j
+  
+  
+CONTAINS
+  
+#ifdef CPP_PARA
+! Interface with parallel physics,
+  SUBROUTINE Init_interface_dyn_phys
+    USE mod_phys_lmdz_mpi_data
+    IMPLICIT NONE
+    include 'dimensions.h'    
+    
+    INTEGER :: i,j,k
+    
+    ALLOCATE(index_i(klon_mpi))
+    ALLOCATE(index_j(klon_mpi))
+    
+    k=1
+    IF (is_north_pole) THEN
+      index_i(k)=1
+      index_j(k)=1
+      k=2
+    ELSE
+      DO i=ii_begin,iim
+	index_i(k)=i
+	index_j(k)=jj_begin
+	k=k+1
+       ENDDO
+    ENDIF
+    
+    DO j=jj_begin+1,jj_end-1
+      DO i=1,iim
+	index_i(k)=i
+	index_j(k)=j
+	k=k+1
+      ENDDO
+    ENDDO
+    
+    IF (is_south_pole) THEN
+      index_i(k)=1
+      index_j(k)=jj_end
+    ELSE
+      DO i=1,ii_end
+	index_i(k)=i
+	index_j(k)=jj_end
+	k=k+1
+       ENDDO
+    ENDIF
+  
+  END SUBROUTINE Init_interface_dyn_phys 
+#else
+  SUBROUTINE Init_interface_dyn_phys
+  ! dummy routine for seq case
+  END SUBROUTINE Init_interface_dyn_phys 
+#endif
+! of #ifdef CPP_PARA
+END MODULE mod_interface_dyn_phys
Index: trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/iniphysiq_mod.F90
===================================================================
--- trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/iniphysiq_mod.F90	(revision 1541)
+++ trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/iniphysiq_mod.F90	(revision 1543)
@@ -3,30 +3,34 @@
 CONTAINS
 
-subroutine iniphysiq(ii,jj,nlayer,punjours, pdayref,ptimestep,           &
-                     rlatu,rlatv,rlonu,rlonv,aire,cu,cv,                 &
+subroutine iniphysiq(ii,jj,nlayer, &
+                     nbp, communicator, &
+                     punjours, pdayref,ptimestep, &
+                     rlatu,rlatv,rlonu,rlonv,aire,cu,cv, &
                      prad,pg,pr,pcpp,iflag_phys)
 
-use dimphy, only : klev ! number of atmospheric levels
-use mod_grid_phy_lmdz, only : klon_glo ! number of atmospheric columns
-                                       ! (on full grid)
+use dimphy, only : init_dimphy
+use mod_grid_phy_lmdz, only : klon_glo, & ! number of atmospheric columns (on full grid)
+                              regular_lonlat  ! regular longitude-latitude grid type
 use mod_phys_lmdz_para, only : klon_omp, & ! number of columns (on local omp grid)
                                klon_omp_begin, & ! start index of local omp subgrid
                                klon_omp_end, & ! end index of local omp subgrid
                                klon_mpi_begin ! start indes of columns (on local mpi grid)
-
-use comgeomphy, only : initcomgeomphy, &
-                       cell_area, & ! physics grid area (m2)
-                       dx, & ! cu coeff. (u_covariant = cu * u)
-                       dy, & ! cv coeff. (v_covariant = cv * v)
-                       longitude, & ! longitudes (rad)
-                       latitude ! latitudes (rad)
+use geometry_mod, only: init_geometry
+!use comgeomphy, only : initcomgeomphy, &
+!                       cell_area, & ! physics grid area (m2)
+!                       dx, & ! cu coeff. (u_covariant = cu * u)
+!                       dy, & ! cv coeff. (v_covariant = cv * v)
+!                       longitude, & ! longitudes (rad)
+!                       latitude ! latitudes (rad)
 use infotrac, only : nqtot ! number of advected tracers
 use comgeomfi_h, only: ini_fillgeom
 use temps_mod, only: day_ini, hour_ini
 use phys_state_var_init_mod, only: phys_state_var_init
+use physics_distribution_mod, only: init_physics_distribution
 use regular_lonlat_mod, only: init_regular_lonlat, &
                               east, west, north, south, &
                               north_east, north_west, &
                               south_west, south_east
+use mod_interface_dyn_phys, only: init_interface_dyn_phys
 
 implicit none
@@ -43,4 +47,6 @@
 integer,intent(in) :: ii ! number of atmospheric coulumns along longitudes
 integer,intent(in) :: jj  ! number of atompsheric columns along latitudes
+integer,intent(in) :: nbp ! number of physics columns for this MPI process
+integer,intent(in) :: communicator ! MPI communicator
 real,intent(in) :: rlatu(jj+1) ! latitudes of the physics grid
 real,intent(in) :: rlatv(jj) ! latitude boundaries of the physics grid
@@ -55,5 +61,5 @@
 
 integer :: ibegin,iend,offset
-integer :: i,j
+integer :: i,j,k
 character(len=20) :: modname='iniphysiq'
 character(len=80) :: abort_message
@@ -66,29 +72,29 @@
 
 ! global array, on full physics grid:
-real,allocatable :: latfi(:)
-real,allocatable :: lonfi(:)
-real,allocatable :: cufi(:)
-real,allocatable :: cvfi(:)
-real,allocatable :: airefi(:)
+real,allocatable :: latfi_glo(:)
+real,allocatable :: lonfi_glo(:)
+real,allocatable :: cufi_glo(:)
+real,allocatable :: cvfi_glo(:)
+real,allocatable :: airefi_glo(:)
+real,allocatable :: boundslonfi_glo(:,:)
+real,allocatable :: boundslatfi_glo(:,:)
+
+! local arrays, on given MPI/OpenMP domain:
+real,allocatable,save :: latfi(:)
+real,allocatable,save :: lonfi(:)
+real,allocatable,save :: cufi(:)
+real,allocatable,save :: cvfi(:)
+real,allocatable,save :: airefi(:)
+real,allocatable,save :: boundslonfi(:,:)
+real,allocatable,save :: boundslatfi(:,:)
+!$OMP THREADPRIVATE (latfi,lonfi,cufi,cvfi,airefi,boundslonfi,boundslatfi)
+
 
 pi=2.*asin(1.0)
 
-IF (nlayer.NE.klev) THEN
-  write(*,*) 'STOP in ',trim(modname)
-  write(*,*) 'Problem with dimensions :'
-  write(*,*) 'nlayer     = ',nlayer
-  write(*,*) 'klev   = ',klev
-  abort_message = ''
-  CALL abort_gcm (modname,abort_message,1)
-ENDIF
-
-!IF (ngrid.NE.klon_glo) THEN
-!  write(*,*) 'STOP in ',trim(modname)
-!  write(*,*) 'Problem with dimensions :'
-!  write(*,*) 'ngrid     = ',ngrid
-!  write(*,*) 'klon   = ',klon_glo
-!  abort_message = ''
-!  CALL abort_gcm (modname,abort_message,1)
-!ENDIF
+! Initialize Physics distibution and parameters and interface with dynamics
+CALL init_physics_distribution(regular_lonlat,4, &
+                                 nbp,ii,jj+1,nlayer,communicator)
+CALL init_interface_dyn_phys
 
 ! init regular global longitude-latitude grid points and boundaries
@@ -115,32 +121,63 @@
 
 ! Generate global arrays on full physics grid
-allocate(latfi(klon_glo),lonfi(klon_glo),cufi(klon_glo),cvfi(klon_glo))
-latfi(1)=rlatu(1)
-lonfi(1)=0.
-cufi(1) = cu(1)
-cvfi(1) = cv(1)
+allocate(latfi_glo(klon_glo),lonfi_glo(klon_glo))
+allocate(cufi_glo(klon_glo),cvfi_glo(klon_glo))
+allocate(airefi_glo(klon_glo))
+allocate(boundslonfi_glo(klon_glo,4))
+allocate(boundslatfi_glo(klon_glo,4))
+
+! North pole
+latfi_glo(1)=rlatu(1)
+lonfi_glo(1)=0.
+cufi_glo(1) = cu(1)
+cvfi_glo(1) = cv(1)
+boundslonfi_glo(1,north_east)=0
+boundslatfi_glo(1,north_east)=PI/2
+boundslonfi_glo(1,north_west)=2*PI
+boundslatfi_glo(1,north_west)=PI/2
+boundslonfi_glo(1,south_west)=2*PI
+boundslatfi_glo(1,south_west)=rlatv(1)
+boundslonfi_glo(1,south_east)=0
+boundslatfi_glo(1,south_east)=rlatv(1)
 DO j=2,jj
   DO i=1,ii
-    latfi((j-2)*ii+1+i)= rlatu(j)
-    lonfi((j-2)*ii+1+i)= rlonv(i)
-    cufi((j-2)*ii+1+i) = cu((j-1)*(ii+1)+i)
-    cvfi((j-2)*ii+1+i) = cv((j-1)*(ii+1)+i)
+    k=(j-2)*ii+1+i
+    latfi_glo((j-2)*ii+1+i)= rlatu(j)
+    lonfi_glo((j-2)*ii+1+i)= rlonv(i)
+    cufi_glo((j-2)*ii+1+i) = cu((j-1)*(ii+1)+i)
+    cvfi_glo((j-2)*ii+1+i) = cv((j-1)*(ii+1)+i)
+    boundslonfi_glo(k,north_east)=rlonu(i)
+    boundslatfi_glo(k,north_east)=rlatv(j-1)
+    boundslonfi_glo(k,north_west)=rlonu(i+1)
+    boundslatfi_glo(k,north_west)=rlatv(j-1)
+    boundslonfi_glo(k,south_west)=rlonu(i+1)
+    boundslatfi_glo(k,south_west)=rlatv(j)
+    boundslonfi_glo(k,south_east)=rlonu(i)
+    boundslatfi_glo(k,south_east)=rlatv(j)
   ENDDO
 ENDDO
-latfi(klon_glo)= rlatu(jj+1)
-lonfi(klon_glo)= 0.
-cufi(klon_glo) = cu((ii+1)*jj+1)
-cvfi(klon_glo) = cv((ii+1)*jj-ii)
+! South pole
+latfi_glo(klon_glo)= rlatu(jj+1)
+lonfi_glo(klon_glo)= 0.
+cufi_glo(klon_glo) = cu((ii+1)*jj+1)
+cvfi_glo(klon_glo) = cv((ii+1)*jj-ii)
+boundslonfi_glo(klon_glo,north_east)= 0
+boundslatfi_glo(klon_glo,north_east)= rlatv(jj)
+boundslonfi_glo(klon_glo,north_west)= 2*PI
+boundslatfi_glo(klon_glo,north_west)= rlatv(jj)
+boundslonfi_glo(klon_glo,south_west)= 2*PI
+boundslatfi_glo(klon_glo,south_west)= -PI/2
+boundslonfi_glo(klon_glo,south_east)= 0
+boundslatfi_glo(klon_glo,south_east)= -Pi/2
 
 ! build airefi(), mesh area on physics grid
-allocate(airefi(klon_glo))
-CALL gr_dyn_fi(1,ii+1,jj+1,klon_glo,aire,airefi)
+CALL gr_dyn_fi(1,ii+1,jj+1,klon_glo,aire,airefi_glo)
 ! Poles are single points on physics grid
-airefi(1)=sum(aire(1:ii,1))
-airefi(klon_glo)=sum(aire(1:ii,jj+1))
+airefi_glo(1)=sum(aire(1:ii,1))
+airefi_glo(klon_glo)=sum(aire(1:ii,jj+1))
 
 ! Sanity check: do total planet area match between physics and dynamics?
 total_area_dyn=sum(aire(1:ii,1:jj+1))
-total_area_phy=sum(airefi(1:klon_glo))
+total_area_phy=sum(airefi_glo(1:klon_glo))
 IF (total_area_dyn/=total_area_phy) THEN
   WRITE (lunout, *) 'iniphysiq: planet total surface discrepancy !!!'
@@ -158,22 +195,31 @@
 !$OMP PARALLEL 
 ! Now generate local lon/lat/cu/cv/area arrays
-call initcomgeomphy
-
-!!!!$OMP PARALLEL PRIVATE(ibegin,iend) 
-!!!$OMP+         SHARED(parea,pcu,pcv,plon,plat)
+allocate(latfi(klon_omp),lonfi(klon_omp),cufi(klon_omp),cvfi(klon_omp))
+allocate(airefi(klon_omp))
+allocate(boundslonfi(klon_omp,4))
+allocate(boundslatfi(klon_omp,4))
+!call initcomgeomphy
       
 offset=klon_mpi_begin-1
-cell_area(1:klon_omp)=airefi(offset+klon_omp_begin:offset+klon_omp_end)
-dx(1:klon_omp)=cufi(offset+klon_omp_begin:offset+klon_omp_end)
-dy(1:klon_omp)=cvfi(offset+klon_omp_begin:offset+klon_omp_end)
-longitude(1:klon_omp)=lonfi(offset+klon_omp_begin:offset+klon_omp_end)
-latitude(1:klon_omp)=latfi(offset+klon_omp_begin:offset+klon_omp_end)
+airefi(1:klon_omp)=airefi_glo(offset+klon_omp_begin:offset+klon_omp_end)
+cufi(1:klon_omp)=cufi_glo(offset+klon_omp_begin:offset+klon_omp_end)
+cvfi(1:klon_omp)=cvfi_glo(offset+klon_omp_begin:offset+klon_omp_end)
+lonfi(1:klon_omp)=lonfi_glo(offset+klon_omp_begin:offset+klon_omp_end)
+latfi(1:klon_omp)=latfi_glo(offset+klon_omp_begin:offset+klon_omp_end)
+boundslonfi(1:klon_omp,:)=boundslonfi_glo(offset+klon_omp_begin:offset+klon_omp_end,:)
+boundslatfi(1:klon_omp,:)=boundslatfi_glo(offset+klon_omp_begin:offset+klon_omp_end,:)
+
+! copy over local grid longitudes and latitudes
+CALL init_geometry(klon_omp,lonfi,latfi,boundslonfi,boundslatfi, &
+                   airefi,cufi,cvfi)
 
 ! copy some fundamental parameters to physics 
 ! and do some initializations 
+
+call init_dimphy(klon_omp,nlayer) ! Initialize dimphy module
 call phys_state_var_init(klon_omp,nlayer,nqtot, &
                          day_ini,hour_ini,punjours,ptimestep, &
                          prad,pg,pr,pcpp)
-call ini_fillgeom(klon_omp,latitude,longitude,cell_area)
+call ini_fillgeom(klon_omp,latfi,lonfi,airefi)
 call conf_phys(klon_omp,nlayer,nqtot)
 
Index: trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/newstart.F
===================================================================
--- trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/newstart.F	(revision 1541)
+++ trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/newstart.F	(revision 1543)
@@ -15,5 +15,8 @@
 c=======================================================================
 
-      use ioipsl_getincom, only: getin 
+      use ioipsl_getincom, only: getin
+      use mod_phys_lmdz_para, only: is_parallel, is_sequential,
+     &                              is_mpi_root, is_omp_root,
+     &                              is_master
       use infotrac, only: infotrac_init, nqtot, tname
       use tracer_mod, only: noms, mmol,
@@ -29,10 +32,8 @@
       use phyredem, only: physdem0, physdem1
       use iostart, only: open_startphy
-      use comgeomphy, only: initcomgeomphy
-!      use planete_h
       use dimradmars_mod, only: tauscaling
       use turb_mod, only: q2
-      use comgeomfi_h, only: ini_fillgeom
       use filtreg_mod, only: inifilr
+      USE mod_const_mpi, ONLY: COMM_LMDZ
       USE comvert_mod, ONLY: ap,bp,pa,preff
       USE comconst_mod, ONLY: lllm,daysec,dtphys,dtvr,
@@ -41,16 +42,16 @@
       USE temps_mod, ONLY: day_ini,hour_ini
       USE ener_mod, ONLY: etot0,ptot0,ztot0,stot0,ang0
-      USE phys_state_var_init_mod, ONLY: phys_state_var_init
+      USE iniphysiq_mod, ONLY: iniphysiq
 
       implicit none
 
-#include "dimensions.h"
+      include "dimensions.h"
       integer, parameter :: ngridmx = (2+(jjm-1)*iim - 1/jjm) 
-#include "paramet.h"
-#include "comgeom2.h"
-#include "comdissnew.h"
-#include "clesph0.h"
-#include "netcdf.inc"
-#include "datafile.h"
+      include "paramet.h"
+      include "comgeom2.h"
+      include "comdissnew.h"
+      include "clesph0.h"
+      include "netcdf.inc"
+      include "datafile.h"
 c=======================================================================
 c   Declarations
@@ -181,10 +182,13 @@
       planet_type="mars"
 
-! initialize "serial/parallel" related stuff
-      CALL init_phys_lmdz(iim,jjp1,llm,1,(/(jjm-1)*iim+2/))
-      call initcomgeomphy
-
+! initialize "serial/parallel" related stuff:
+! (required because we call tabfi() below, before calling iniphysiq)
+      is_sequential=.true.
+      is_parallel=.false.
+      is_mpi_root=.true.
+      is_omp_root=.true.
+      is_master=.true.
+      
 ! Load tracer number and names:
-!      call iniadvtrac(nqtot,numvanle)
       call infotrac_init
 ! allocate arrays
@@ -335,5 +339,4 @@
       mugaz = p_mugaz
       daysec = p_daysec
-!      write(*,*) 'aire',aire
 
 
@@ -349,33 +352,11 @@
       idum=0
 
-c Initialisation coordonnees /aires
-c -------------------------------
-! Note: rlatu(:) and rlonv(:) are commons defined in "comgeom.h"
-!       rlatu() and rlonv() are given in radians
-      latfi(1)=rlatu(1)
-      lonfi(1)=0.
-      DO j=2,jjm
-         DO i=1,iim
-            latfi((j-2)*iim+1+i)=rlatu(j)
-            lonfi((j-2)*iim+1+i)=rlonv(i)
-         ENDDO
-      ENDDO
-      latfi(ngridmx)=rlatu(jjp1)
-      lonfi(ngridmx)=0.
-      
-      ! build airefi(), mesh area on physics grid
-      CALL gr_dyn_fi(1,iip1,jjp1,ngridmx,aire,airefi)
-      ! Poles are single points on physics grid
-      airefi(1)=sum(aire(1:iim,1))
-      airefi(ngridmx)=sum(aire(1:iim,jjm+1))
-
-! also initialize various physics flags/settings which might be needed
-!    (for instance initracer needs to know about some flags, and/or
-!      'datafile' path may be changed by user)
-      call phys_state_var_init(ngridmx,llm,nqtot,
-     &                         day_ini,hour_ini,daysec,dtphys,
-     &                         rad,g,r,cpp)
-      call ini_fillgeom(ngridmx,latfi,lonfi,airefi)
-      call conf_phys(ngridmx,llm,nqtot)
+! Initialize the physics
+         CALL iniphysiq(iim,jjm,llm,
+     &                  (jjm-1)*iim+2,comm_lmdz,
+     &                  daysec,day_ini,dtphys,
+     &                  rlatu,rlatv,rlonu,rlonv,
+     &                  aire,cu,cv,rad,g,r,cpp,
+     &                  1)
 
 c=======================================================================
Index: trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/start2archive.F
===================================================================
--- trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/start2archive.F	(revision 1541)
+++ trunk/LMDZ.MARS/libf/dynphy_lonlat/phymars/start2archive.F	(revision 1543)
@@ -23,19 +23,21 @@
       use surfdat_h, only: ini_surfdat_h, qsurf
       use comsoil_h, only: ini_comsoil_h
-      use comgeomphy, only: initcomgeomphy
+!      use comgeomphy, only: initcomgeomphy
       use filtreg_mod, only: inifilr
+      USE mod_const_mpi, ONLY: COMM_LMDZ
       use control_mod, only: planet_type
       USE comvert_mod, ONLY: ap,bp
-      USE comconst_mod, ONLY: g,cpp
+      USE comconst_mod, ONLY: daysec,dtphys,rad,g,r,cpp
       USE logic_mod, ONLY: grireg
       USE temps_mod, ONLY: day_ini,hour_ini
+      USE iniphysiq_mod, ONLY: iniphysiq
       implicit none
 
-#include "dimensions.h"
+      include "dimensions.h"
       integer, parameter :: ngridmx = (2+(jjm-1)*iim - 1/jjm) 
-#include "paramet.h"
-#include "comdissip.h"
-#include "comgeom.h"
-#include "netcdf.inc"
+      include "paramet.h"
+      include "comdissip.h"
+      include "comgeom.h"
+      include "netcdf.inc"
 
 c-----------------------------------------------------------------------
@@ -59,7 +61,7 @@
 c Variable Physiques (grille physique)
 c ------------------------------------
-      REAL tsurf(ngridmx)	! Surface temperature
+      REAL tsurf(ngridmx)        ! Surface temperature
       REAL tsoil(ngridmx,nsoilmx) ! Soil temperature
-      REAL co2ice(ngridmx)	! CO2 ice layer
+      REAL co2ice(ngridmx)        ! CO2 ice layer
       REAL tauscaling(ngridmx) ! dust conversion factor
       REAL q2(ngridmx,llm+1)
@@ -116,7 +118,5 @@
       CALL defrun_new(99, .TRUE. )
       grireg   = .TRUE.
-! initialize "serial/parallel" related stuff
-      CALL init_phys_lmdz(iim,jjp1,llm,1,(/(jjm-1)*iim+2/))
-      call initcomgeomphy
+
       planet_type='mars'
 
@@ -125,5 +125,4 @@
 c=======================================================================
 ! Load tracer number and names:
-!      call iniadvtrac(nqtot,numvanle)
       call infotrac_init
 
@@ -131,6 +130,4 @@
       allocate(q(ip1jmp1,llm,nqtot))
       allocate(qsurfS(ip1jmp1,nqtot))
-      call ini_surfdat_h(ngridmx,nqtot)
-      call ini_comsoil_h(ngridmx)
       
 
@@ -139,4 +136,20 @@
      .       ps,phis,timedyn)
 
+c-----------------------------------------------------------------------
+c   Initialisations 
+c-----------------------------------------------------------------------
+
+      CALL defrun_new(99, .FALSE. )
+      call iniconst
+      call inigeom
+      call inifilr
+
+! Initialize the physics
+         CALL iniphysiq(iim,jjm,llm,
+     &                  (jjm-1)*iim+2,comm_lmdz,
+     &                  daysec,day_ini,dtphys,
+     &                  rlatu,rlatv,rlonu,rlonv,
+     &                  aire,cu,cv,rad,g,r,cpp,
+     &                  1)
 
       fichnom = 'startfi.nc'
@@ -185,12 +198,4 @@
 c *****************************************************************
 
-c-----------------------------------------------------------------------
-c   Initialisations 
-c-----------------------------------------------------------------------
-
-      CALL defrun_new(99, .FALSE. )
-      call iniconst
-      call inigeom
-      call inifilr
       CALL pression(ip1jmp1, ap, bp, ps, p3d)
       call exner_hyb(ip1jmp1, ps, p3d, beta, pks, pk, pkf)
