Index: codes/icosagcm/trunk/src/check_conserve.f90
===================================================================
--- codes/icosagcm/trunk/src/check_conserve.f90	(revision 364)
+++ codes/icosagcm/trunk/src/check_conserve.f90	(revision 365)
@@ -3,15 +3,20 @@
   IMPLICIT NONE 
 
-  PRIVATE 
-    TYPE(t_field),POINTER,SAVE :: f_pk(:), f_pks(:), f_p(:)
-    TYPE(t_field),POINTER,SAVE :: f_vort(:)
-    TYPE(t_field),POINTER,SAVE :: f_rhodz(:) 
-
-    INTEGER, PARAMETER :: check_basic=1, check_detailed=2
-    INTEGER, SAVE :: check_type
-  PUBLIC init_check_conserve, check_conserve, check_conserve_detailed
-    REAL(rstd),SAVE:: mtot0,ztot0,etot0,angtot0,stot0,rmsvtot0
-!$OMP THREADPRIVATE(check_type, mtot0,ztot0,etot0,angtot0,stot0,rmsvtot0)
-     
+  PRIVATE
+  
+  INTEGER, PARAMETER, PUBLIC :: check_basic=1, check_detailed=2, &
+       AAM_dissip=1, AAM_dyn=2, AAM_phys=3
+  INTEGER, SAVE :: check_type
+
+  TYPE(t_field),POINTER,SAVE :: f_pk(:), f_pks(:), f_p(:)
+  TYPE(t_field),POINTER,SAVE :: f_vort(:)
+  TYPE(t_field),POINTER,SAVE :: f_rhodz(:) 
+  
+  REAL(rstd),SAVE :: AAM_mass, AAM_mass_old, AAM_vel, AAM_vel_old, &
+       AAM_mass_source(3), AAM_vel_source(3) ! read/written only IF is_master
+  REAL(rstd),SAVE :: mtot0,ztot0,etot0,angtot0,stot0,rmsvtot0
+  !$OMP THREADPRIVATE(check_type, mtot0,ztot0,etot0,angtot0,stot0,rmsvtot0)
+  
+  PUBLIC :: init_check_conserve, check_conserve_detailed, check_conserve
 CONTAINS 
 
@@ -19,8 +24,6 @@
 
   SUBROUTINE init_check_conserve
-    USE icosa
     USE getin_mod
     USE omp_para, ONLY : is_master
-    IMPLICIT NONE
     CHARACTER(LEN=255) :: check_type_str
     CALL allocate_field(f_pk,field_t,type_real,llm)
@@ -47,12 +50,10 @@
 
   SUBROUTINE check_conserve(f_ps,f_dps,f_ue,f_theta_rhodz,f_phis,it) 
-  USE icosa 
   USE pression_mod
   USE vorticity_mod
   USE caldyn_gcm_mod
   USE exner_mod 
-  USE mpipara, ONLY : is_mpi_master, comm_icosa
-  USE omp_para
-  IMPLICIT NONE
+!  USE mpipara, ONLY : is_mpi_master, comm_icosa
+  USE omp_para, ONLY : is_master
     TYPE(t_field),POINTER :: f_ps(:)
     TYPE(t_field),POINTER :: f_dps(:)
@@ -63,7 +64,7 @@
 
     REAL(rstd),POINTER :: p(:,:),rhodz(:,:) 
-    INTEGER::ind,ierr
+    INTEGER :: ind,ierr
     REAL(rstd) :: mtot, angtot, rmsdpdt
-    REAL(rstd) :: etot, stot, ang_mass, ang_vit, rmsvtot, ztot
+    REAL(rstd) :: etot, stot, ang_mass, ang_vel, rmsvtot, ztot
 
     CALL transfert_request(f_ue,req_e1_vect)
@@ -83,8 +84,11 @@
     CALL check_PV(ztot) 
     CALL exner(f_ps,f_p,f_pks,f_pk)
-    CALL check_energy(f_ue,f_theta_rhodz,f_phis, etot, stot, ang_mass, ang_vit, rmsvtot) 
-    angtot  = ang_mass + ang_vit 
-
-    IF (is_master) THEN 
+    CALL check_energy(f_ue,f_theta_rhodz,f_phis, etot, stot, ang_mass, ang_vel, rmsvtot) 
+
+    IF (is_master) THEN ! is_master = is_omp_master && is_mpi_master
+       ! AAM_mass and AAM_vel must be read/written only IF is_master
+       AAM_mass = ang_mass
+       AAM_vel = ang_vel
+       angtot  = ang_mass + ang_vel
        IF ( it == itau0  ) THEN 
           ztot0 = ztot
@@ -93,6 +97,9 @@
           angtot0  = angtot
           stot0 = stot 
+          AAM_mass_old=AAM_mass
+          AAM_vel_old=AAM_vel
+          AAM_mass_source=0.
+          AAM_vel_source=0.
        END IF
-
        rmsvtot=SQRT(rmsvtot/mtot) 
        ztot=ztot/ztot0-1. ; mtot=mtot/mtot0-1. 
@@ -107,22 +114,36 @@
        
 4000   FORMAT(10x,'masse',5x,'rmsdpdt',5x,'energie',5x,'enstrophie'  &
-            ,5x,'entropie',5x,'rmsv',5x,'mt.ang',/,'GLOB  '                &
+            ,5x,'entropie',5x,'rmsv',5x,'mt.ang',/,'GLOB  '          &
             ,e10.3,e13.6,5e13.3/)     
-       close(134)
-      
+       CLOSE(134)
+
+       IF(check_type == check_detailed) THEN
+          !ang_mass = AAM_mass-AAM_mass_old 
+          !WRITE(*,'(A,3E12.4)') 'AAM_mass sanity check', SUM(AAM_mass_source), ang_mass, SUM(AAM_mass_source)-ang_mass
+          !ang_vel = AAM_vel-AAM_vel_old 
+          !WRITE(*,'(A,3E12.4)') 'AAM_vel  sanity check', SUM(AAM_vel_source), ang_vel, SUM(AAM_vel_source)-ang_vel
+          WRITE(*,'(A,6E12.4)') 'AAM_mass : time,old,new,dissip,dyn,phys', dt*it, AAM_mass_old, AAM_mass, &
+               AAM_mass_source(AAM_dissip), AAM_mass_source(AAM_dyn), AAM_mass_source(AAM_phys)
+          WRITE(*,'(A,6E12.4)') 'AAM_vel  : time,old,new,dissip,dyn,phys', dt*it, AAM_vel_old, AAM_vel, &
+               AAM_vel_source(AAM_dissip), AAM_vel_source(AAM_dyn), AAM_vel_source(AAM_phys)
+          AAM_mass_old=AAM_mass
+          AAM_vel_old=AAM_vel
+          AAM_mass_source=0.
+          AAM_vel_source=0.
+       END IF
     END IF
+
   END SUBROUTINE check_conserve
 
 !------------------------ Detailed check (Lauritzen et al., 2014) -----------------------------
 
-  SUBROUTINE check_conserve_detailed(tag, f_ps,f_dps,f_ue,f_theta_rhodz,f_phis,it) 
-    USE icosa 
+  SUBROUTINE check_conserve_detailed(it,tag, f_ps,f_dps,f_ue,f_theta_rhodz,f_phis) 
     USE pression_mod
     USE vorticity_mod
     USE caldyn_gcm_mod
     USE exner_mod 
-    USE mpipara, ONLY : is_mpi_root, comm_icosa
-    IMPLICIT NONE
-    CHARACTER(LEN=*), INTENT(IN) :: tag
+!    USE mpipara, ONLY : is_mpi_root, comm_icosa
+    USE omp_para, ONLY : is_master
+    INTEGER, INTENT(IN) :: tag
     TYPE(t_field),POINTER :: f_ps(:)
     TYPE(t_field),POINTER :: f_dps(:)
@@ -134,10 +155,7 @@
     REAL(rstd),POINTER :: p(:,:),rhodz(:,:) 
     INTEGER::ind,ierr
-    REAL(rstd) :: mtot, ztot, rmsdpdt, etot,stot,rmsv, AAM_mass, AAM_vel
+    REAL(rstd) :: mtot, ztot, rmsdpdt, etot,stot,rmsv, ang_mass, ang_vel
     
     IF(check_type == check_detailed) THEN
-       etot=0.0; stot=0.0; rmsv=0.0 
-       AAM_vel=0. ; AAM_vel=0.
-       ztot = 0.0 
 
        CALL transfert_request(f_ue,req_e1_vect)
@@ -152,18 +170,17 @@
        END DO
        CALL exner(f_ps,f_p,f_pks,f_pk)
-       CALL check_energy(f_ue,f_theta_rhodz,f_phis, etot, stot, AAM_mass, AAM_vel, rmsv) 
+       CALL check_energy(f_ue,f_theta_rhodz,f_phis, etot, stot, ang_mass, ang_vel, rmsv) 
        
-       IF (is_mpi_root) THEN 
-          !$OMP MASTER       
-          IF ( it == itau0  ) Then 
-             ztot0 = ztot
-             mtot0 = mtot
-             etot0 = etot 
-             angtot0  = AAM_mass + AAM_vel
-             stot0 = stot 
-          END IF
-          
-          PRINT *, TRIM(tag), AAM_mass, AAM_vel
-          !$OMP END MASTER 
+       IF (is_master) THEN
+          ! XXX=mass,vel
+          ! AAM_XXX = value of total AAM_XXX at last check
+          ! ang_XXX = value of total AAM_XXX now
+          ! AAM_XXX_source(tag) = source of AAA_XXX due to tag=dissip,dyn,phys
+          !WRITE(*,'(A,3E12.2)') 'AAM_mass', tag, ang_mass, AAM_mass, ang_mass-AAM_mass ! FIXME
+          !WRITE(*,'(A,3E12.2)') 'AAM_vel ', tag, ang_vel, AAM_vel, ang_vel-AAM_vel ! FIXME
+          AAM_mass_source(tag) = AAM_mass_source(tag) + ang_mass - AAM_mass
+          AAM_vel_source(tag) = AAM_vel_source(tag) + ang_vel - AAM_vel
+          AAM_mass = ang_mass
+          AAM_vel = ang_vel
        END IF
     END IF
@@ -176,5 +193,4 @@
     USE mpipara
     USE transfert_omp_mod
-    IMPLICIT NONE
     REAL(rstd), INTENT(IN) :: loc_sum
     REAL(rstd), INTENT(OUT) :: glob_sum
@@ -188,5 +204,5 @@
     ELSE
       glob_sum=loc_sum
-    ENDIF 
+    ENDIF
   END SUBROUTINE global_sum
 
@@ -195,5 +211,4 @@
   USE mpipara
   USE transfert_omp_mod
-  USE icosa
   USE omp_para
   IMPLICIT NONE
@@ -233,8 +248,6 @@
   SUBROUTINE check_energy(f_ue,f_theta_rhodz,f_phis, etot, &
                       stot, AAM_mass_tot, AAM_vel_tot, rmsvtot)  
-  USE icosa
   USE pression_mod
   USE vorticity_mod
-  IMPLICIT NONE
     TYPE(t_field), POINTER :: f_ue(:)
     TYPE(t_field), POINTER :: f_theta_rhodz(:)
@@ -274,9 +287,7 @@
 
   SUBROUTINE compute_energy(ind,u,p,rhodz,theta_rhodz,pk,phis, e, s, AAM_mass, AAM_vel, rmsv)
-    USE icosa
     USE disvert_mod
     USE wind_mod
     USE omp_para
-    IMPLICIT NONE
     INTEGER,INTENT(IN)::ind 
     REAL(rstd),INTENT(IN) :: u(3*iim*jjm,llm)
@@ -324,22 +335,17 @@
     END DO
     
-END SUBROUTINE compute_energy
+  END SUBROUTINE compute_energy
 
 !---------------------------------------------------------------------
-
+  
   SUBROUTINE check_PV(ztot)
-  USE icosa
-  USE mpi_mod
-  USE mpipara
-  USE transfert_omp_mod
-  IMPLICIT NONE
-     REAL(rstd),INTENT(OUT) :: ztot
-     REAL(rstd), POINTER :: vort(:,:)
-     REAL(rstd), POINTER :: rhodz(:,:) 
-     INTEGER :: ind
-     REAL(rstd) :: z,z_mpi
-
-     z=0
-     DO ind=1,ndomain 
+    REAL(rstd),INTENT(OUT) :: ztot
+    REAL(rstd), POINTER :: vort(:,:)
+    REAL(rstd), POINTER :: rhodz(:,:) 
+    INTEGER :: ind
+    REAL(rstd) :: z,z_mpi
+    
+    z=0
+    DO ind=1,ndomain 
        IF (.NOT. assigned_domain(ind)) CYCLE
        CALL swap_dimensions(ind)
@@ -348,22 +354,11 @@
        rhodz=f_rhodz(ind) 
        CALL compute_PV(vort,rhodz,z) 
-     ENDDO
-
-    IF (using_mpi) THEN
-      CALL reduce_sum_omp(z, z_mpi)
-!$OMP MASTER    
-      CALL MPI_REDUCE(z_mpi, ztot, 1, MPI_REAL8, MPI_SUM, 0, comm_icosa, ierr)
-!$OMP END MASTER
-    ELSE
-      ztot=z
-    ENDIF 
- 
+    ENDDO
+    CALL global_sum(z, ztot)
   END SUBROUTINE check_PV 
 
   SUBROUTINE compute_PV(vort,rhodz,z) 
-  USE icosa
   USE disvert_mod
   USE omp_para
-  IMPLICIT NONE 
     REAL(rstd),INTENT(IN) :: vort(2*iim*jjm,llm)
     REAL(rstd),INTENT(IN) :: rhodz(iim*jjm,llm) 
@@ -402,6 +397,4 @@
 
    SUBROUTINE compute_rhodz(p,rhodz) 
-   USE icosa 
-   IMPLICIT NONE 
      REAL(rstd),INTENT(IN) :: p(iim*jjm,llm+1)
      REAL(rstd),INTENT(OUT):: rhodz(iim*jjm,llm)
Index: codes/icosagcm/trunk/src/timeloop_gcm.f90
===================================================================
--- codes/icosagcm/trunk/src/timeloop_gcm.f90	(revision 364)
+++ codes/icosagcm/trunk/src/timeloop_gcm.f90	(revision 365)
@@ -225,9 +225,7 @@
 
        IF (mod(it,itau_out)==0 ) THEN
+          CALL transfert_request(f_u,req_e1_vect)
           CALL write_output_fields_basic(f_ps, f_u, f_q)
        ENDIF
-
-       CALL check_conserve_detailed('detailed_budget 0', &
-            f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it)
 
        CALL guided(it*dt,f_ps,f_theta_rhodz,f_u,f_q)
@@ -239,7 +237,4 @@
           CALL HEVI_scheme(it, fluxt_zero)
        END SELECT
-       
-       CALL check_conserve_detailed('detailed_budget 1', &
-            f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it)
        
        IF (MOD(it,itau_dissip)==0) THEN
@@ -257,4 +252,7 @@
           ENDIF
           
+          CALL check_conserve_detailed(it, AAM_dyn, &
+               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
+       
           CALL dissip(f_u,f_du,f_mass,f_phis, f_theta_rhodz,f_dtheta_rhodz)
           
@@ -264,8 +262,8 @@
              CALL euler_scheme(.FALSE.)  ! update only u, theta
           END IF
-       END IF
-       
-       CALL check_conserve_detailed('detailed_budget 2', &
-            f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it)
+
+          CALL check_conserve_detailed(it, AAM_dissip, &
+               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
+       END IF
        
        IF(MOD(it,itau_adv)==0) THEN
@@ -284,10 +282,10 @@
        END IF
        
-       IF (MOD(it,itau_check_conserv)==0) THEN
-          CALL check_conserve(f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it) 
-       ENDIF
-       
        IF (MOD(it,itau_physics)==0) THEN
+          CALL check_conserve_detailed(it, AAM_dyn, &
+            f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
           CALL physics(it,f_phis, f_ps, f_theta_rhodz, f_u, f_wflux, f_q)        
+          CALL check_conserve_detailed(it, AAM_phys, &
+               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
           !$OMP MASTER
           IF (first_physic) CALL SYSTEM_CLOCK(start_clock)
@@ -295,9 +293,16 @@
           first_physic=.FALSE.
        END IF
-       
+
+       IF (MOD(it,itau_check_conserv)==0) THEN
+          CALL check_conserve_detailed(it, AAM_dyn, &
+               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
+          CALL check_conserve(f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it) 
+       ENDIF       
     END DO
     
     CALL write_etat0(itau0+itaumax,f_ps, f_phis,f_theta_rhodz,f_u,f_q) 
     
+    CALL check_conserve_detailed(it, AAM_dyn, &
+         f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
     CALL check_conserve(f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it)  
     
@@ -316,5 +321,5 @@
     INTEGER :: it, itau0, itaumax, start_clock, stop_clock, rate_clock, throughput
     REAL :: per_step,total, elapsed
-    WRITE(*,'(A,I7,A,F8.1)') "It No :",it,"   t :",dt*it
+    WRITE(*,'(A,I7,A,F14.1)') "It No :",it,"   t :",dt*it
     IF(MOD(it,10)==0) THEN
        CALL SYSTEM_CLOCK(stop_clock)
