Index: dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/inifis_mod.F90
===================================================================
--- dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/inifis_mod.F90	(revision 3990)
+++ dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/inifis_mod.F90	(revision 4028)
@@ -6,5 +6,5 @@
   SUBROUTINE inifis(prad, pg, pr, pcpp)
   ! Initialize some physical constants and settings
-  USE print_control_mod, ONLY: init_print_control
+  USE init_print_control_mod, ONLY: init_print_control
   USE comcstphy, ONLY: rradius, & ! planet radius (m)
                        rr, & ! recuced gas constant: R/molar mass of atm
Index: dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/iophy.F90
===================================================================
--- dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/iophy.F90	(revision 3990)
+++ dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/iophy.F90	(revision 4028)
@@ -11,5 +11,4 @@
   INTEGER, save :: npstn
   INTEGER, allocatable, dimension(:), save :: nptabij
-  
 
 #ifdef CPP_XIOS
@@ -45,5 +44,5 @@
                                 mpi_size, mpi_rank, klon_mpi, &
                                 is_sequential, is_south_pole_dyn
-  USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo
+  USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo, grid_type, unstructured
   USE print_control_mod, ONLY: lunout, prt_level
   USE mod_grid_phy_lmdz, ONLY: nbp_lon, nbp_lat
@@ -52,5 +51,5 @@
 #endif
 #ifdef CPP_XIOS
-  use wxios, only: wxios_domain_param
+  use wxios, only: wxios_domain_param, wxios_domain_param_unstructured, wxios_context_init
 #endif
   implicit none
@@ -71,85 +70,102 @@
     integer :: data_ibegin,data_iend
 
-    CALL gather(rlat,rlat_glo)
-    CALL bcast(rlat_glo)
-    CALL gather(rlon,rlon_glo)
-    CALL bcast(rlon_glo)
-    
-!$OMP MASTER  
-    ALLOCATE(io_lat(nbp_lat))
-    io_lat(1)=rlat_glo(1)
-    io_lat(nbp_lat)=rlat_glo(klon_glo)
-    IF ((nbp_lon*nbp_lat) > 1) then
-      DO i=2,nbp_lat-1
-        io_lat(i)=rlat_glo(2+(i-2)*nbp_lon)
-      ENDDO
+#ifdef CPP_XIOS
+      CALL wxios_context_init
+#endif
+    
+    IF (grid_type==unstructured) THEN
+    
+#ifdef CPP_XIOS
+      CALL wxios_domain_param_unstructured("dom_glo")
+#endif
+    
+    ELSE
+
+      CALL gather(rlat,rlat_glo) 
+      CALL bcast(rlat_glo)
+      CALL gather(rlon,rlon_glo)
+      CALL bcast(rlon_glo)
+    
+  !$OMP MASTER  
+      ALLOCATE(io_lat(nbp_lat))
+      io_lat(1)=rlat_glo(1)
+      io_lat(nbp_lat)=rlat_glo(klon_glo)
+      IF ((nbp_lon*nbp_lat) > 1) then
+        DO i=2,nbp_lat-1
+          io_lat(i)=rlat_glo(2+(i-2)*nbp_lon)
+        ENDDO
+      ENDIF
+
+      ALLOCATE(io_lon(nbp_lon))
+      IF (klon_glo == 1) THEN
+        io_lon(1)=rlon_glo(1)
+      ELSE 
+        io_lon(1:nbp_lon)=rlon_glo(2:nbp_lon+1)
+      ENDIF
+    
+  !! (I) dtnb   : total number of domains
+  !! (I) dnb    : domain number
+  !! (I) did(:) : distributed dimensions identifiers
+  !!              (up to 5 dimensions are supported)
+  !! (I) dsg(:) : total number of points for each dimension
+  !! (I) dsl(:) : local number of points for each dimension
+  !! (I) dpf(:) : position of first local point for each dimension
+  !! (I) dpl(:) : position of last local point for each dimension
+  !! (I) dhs(:) : start halo size for each dimension
+  !! (I) dhe(:) : end halo size for each dimension
+  !! (C) cdnm   : Model domain definition name.
+  !!              The names actually supported are :
+  !!              "BOX", "APPLE", "ORANGE".
+  !!              These names are case insensitive.
+
+      ddid=(/ 1,2 /)
+      dsg=(/ nbp_lon, nbp_lat /)
+      dsl=(/ nbp_lon, jj_nb /)
+      dpf=(/ 1,jj_begin /)
+      dpl=(/ nbp_lon, jj_end /)
+      dhs=(/ ii_begin-1,0 /)
+      IF (mpi_rank==mpi_size-1) THEN
+        dhe=(/0,0/)
+      ELSE
+        dhe=(/ nbp_lon-ii_end,0 /)  
+      ENDIF
+
+#ifndef CPP_IOIPSL_NO_OUTPUT    
+      CALL flio_dom_set(mpi_size,mpi_rank,ddid,dsg,dsl,dpf,dpl,dhs,dhe, &
+                        'APPLE',phys_domain_id)
+#endif
+#ifdef CPP_XIOS
+      ! Set values for the mask:
+      IF (mpi_rank == 0) THEN
+          data_ibegin = 0
+      ELSE 
+          data_ibegin = ii_begin - 1
+      END IF
+
+      IF (mpi_rank == mpi_size-1) THEN
+          data_iend = nbp_lon
+      ELSE
+          data_iend = ii_end + 1
+      END IF
+
+      if (prt_level>=10) then
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," iibegin=",ii_begin , " ii_end=",ii_end," jjbegin=",jj_begin," jj_nb=",jj_nb," jj_end=",jj_end
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," nbp_lon=",nbp_lon," nbp_lat=",nbp_lat
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," is_south_pole_dyn=",is_south_pole_dyn
+      endif
+#endif
+
+!$OMP END MASTER
+
+#ifdef CPP_XIOS
+      ! Initialize the XIOS domain coreesponding to this process:
+    
+        CALL wxios_domain_param("dom_glo")
+#endif
+
     ENDIF
-
-    ALLOCATE(io_lon(nbp_lon))
-    IF ((nbp_lon*nbp_lat) > 1) THEN
-      io_lon(:)=rlon_glo(2:nbp_lon+1)
-    ELSE
-      io_lon(1)=rlon_glo(1)
-    ENDIF
-!! (I) dtnb   : total number of domains
-!! (I) dnb    : domain number
-!! (I) did(:) : distributed dimensions identifiers
-!!              (up to 5 dimensions are supported)
-!! (I) dsg(:) : total number of points for each dimension
-!! (I) dsl(:) : local number of points for each dimension
-!! (I) dpf(:) : position of first local point for each dimension
-!! (I) dpl(:) : position of last local point for each dimension
-!! (I) dhs(:) : start halo size for each dimension
-!! (I) dhe(:) : end halo size for each dimension
-!! (C) cdnm   : Model domain definition name.
-!!              The names actually supported are :
-!!              "BOX", "APPLE", "ORANGE".
-!!              These names are case insensitive.
-    ddid=(/ 1,2 /)
-    dsg=(/ nbp_lon, nbp_lat /)
-    dsl=(/ nbp_lon, jj_nb /)
-    dpf=(/ 1,jj_begin /)
-    dpl=(/ nbp_lon, jj_end /)
-    dhs=(/ ii_begin-1,0 /)
-    if (mpi_rank==mpi_size-1) then
-      dhe=(/0,0/)
-    else
-      dhe=(/ nbp_lon-ii_end,0 /)  
-    endif
-    
-#ifndef CPP_IOIPSL_NO_OUTPUT
-    call flio_dom_set(mpi_size,mpi_rank,ddid,dsg,dsl,dpf,dpl,dhs,dhe, &
-                      'APPLE',phys_domain_id)
-#endif
-#ifdef CPP_XIOS
-    ! Set values for the mask:
-    IF (mpi_rank == 0) THEN
-        data_ibegin = 0
-    ELSE 
-        data_ibegin = ii_begin - 1
-    END IF
-
-    IF (mpi_rank == mpi_size-1) THEN
-        data_iend = nbp_lon
-    ELSE
-        data_iend = ii_end + 1
-    END IF
-
-    if (prt_level>=10) then
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," iibegin=",ii_begin , " ii_end=",ii_end," jjbegin=",jj_begin," jj_nb=",jj_nb," jj_end=",jj_end
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," nbp_lon=",nbp_lon," nbp_lat=",nbp_lat
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," is_south_pole=",is_south_pole_dyn
-    endif
-
-    ! Initialize the XIOS domain coreesponding to this process:
-    CALL wxios_domain_param("dom_glo", is_sequential, nbp_lon, jj_nb, nbp_lon, nbp_lat, &
-                            1, nbp_lon, ii_begin, ii_end, jj_begin, jj_end,             &
-                            klon_mpi+2*(nbp_lon-1), data_ibegin, data_iend,             &
-                            io_lat, io_lon,is_south_pole_dyn,mpi_rank)
-#endif
-!$OMP END MASTER
-      
+            
   END SUBROUTINE init_iophy_new
   
@@ -223,5 +239,5 @@
                                 jj_nb, klon_mpi
   USE ioipsl, only: histwrite
-  USE mod_grid_phy_lmdz, ONLY: nbp_lon
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon, grid_type, regular_lonlat, unstructured
   implicit none
     
@@ -242,5 +258,5 @@
     CALL Gather_omp(field,buffer_omp)    
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
+    if (grid_type==regular_lonlat) CALL grid1Dto2D_mpi(buffer_omp,Field2d)
     if(.NOT.lpoint) THEN
      ALLOCATE(index2d(nbp_lon*jj_nb))
@@ -282,5 +298,5 @@
                                 jj_nb, klon_mpi
   USE ioipsl, only: histwrite
-  USE mod_grid_phy_lmdz, ONLY: nbp_lon
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon, grid_type, regular_lonlat, unstructured
   implicit none
     
@@ -301,5 +317,5 @@
     CALL Gather_omp(field,buffer_omp)
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,field3d)
+    if (grid_type==regular_lonlat) CALL grid1Dto2D_mpi(buffer_omp,field3d)
     if(.NOT.lpoint) THEN
      ALLOCATE(index3d(nbp_lon*jj_nb*nlev))
@@ -346,5 +362,5 @@
   USE xios, only: xios_send_field
   USE print_control_mod, ONLY: prt_level, lunout
-  USE mod_grid_phy_lmdz, ONLY: nbp_lon
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon, grid_type, unstructured
   IMPLICIT NONE
 
@@ -360,8 +376,12 @@
     
     CALL Gather_omp(field,buffer_omp)    
+
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
-    
-    CALL xios_send_field(field_name, Field2d)
+    IF (grid_type==unstructured) THEN
+      CALL xios_send_field(field_name, buffer_omp)
+    ELSE
+      CALL grid1Dto2D_mpi(buffer_omp,Field2d)    
+      CALL xios_send_field(field_name, Field2d)
+    ENDIF !of IF (grid_type==unstructured)
 !$OMP END MASTER   
 
@@ -380,5 +400,5 @@
   USE xios, only: xios_send_field
   USE print_control_mod, ONLY: prt_level,lunout
-  USE mod_grid_phy_lmdz, ONLY: nbp_lon
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon, grid_type, unstructured
 
   IMPLICIT NONE
@@ -399,8 +419,12 @@
 
     CALL Gather_omp(field,buffer_omp)
+
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,field3d)
-
-    CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
+    IF (grid_type==unstructured) THEN
+      CALL xios_send_field(field_name, buffer_omp(:,1:nlev))
+    ELSE   
+      CALL grid1Dto2D_mpi(buffer_omp,field3d)
+      CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
+    ENDIF !of IF (grid_type==unstructured)
 !$OMP END MASTER   
 
Index: dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/physiq_mod.F90
===================================================================
--- dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/physiq_mod.F90	(revision 3990)
+++ dynamico_lmdz/aquaplanet/LMDZ5/libf/phydev/physiq_mod.F90	(revision 4028)
@@ -22,7 +22,9 @@
       USE phys_state_var_mod, only : phys_state_var_init
       USE mod_grid_phy_lmdz, ONLY: nbp_lon,nbp_lat
-
-#ifdef CPP_XIOS
-      USE xios, ONLY: xios_update_calendar
+      USE mod_phys_lmdz_omp_data, ONLY: is_omp_master
+      USE write_field_phy, ONLY: writefield_phy
+
+#ifdef CPP_XIOS
+      USE xios, ONLY: xios_update_calendar, xios_context_finalize
       USE wxios, only: wxios_add_vaxis, wxios_set_cal, wxios_closedef
       USE iophy, ONLY: histwrite_phy
@@ -195,4 +197,7 @@
 if (lafin) then
   call phyredem("restartphy.nc")
+#ifdef CPP_XIOS
+  IF (is_omp_master) CALL xios_context_finalize
+#endif
 endif
 
