Index: /LMDZ6/trunk/libf/phylmd/lmdz_wake.F90
===================================================================
--- /LMDZ6/trunk/libf/phylmd/lmdz_wake.F90	(revision 4907)
+++ /LMDZ6/trunk/libf/phylmd/lmdz_wake.F90	(revision 4908)
@@ -35,5 +35,5 @@
   USE lmdz_wake_ini , ONLY : sigmad, hwmin, wapecut, cstart, sigmaw_max, dens_rate, epsilon_loc
   USE lmdz_wake_ini , ONLY : iflag_wk_profile
-  USE lmdz_wake_ini , ONLY : smallestreal
+  USE lmdz_wake_ini , ONLY : smallestreal,wk_nsub
 
 
@@ -185,5 +185,4 @@
 
   REAL                                                  :: delta_t_min
-  INTEGER                                               :: nsub
   REAL                                                  :: dtimesub
   REAL                                                  :: wdens0
@@ -351,6 +350,5 @@
 !
 !   sub-time-stepping parameters
-  nsub = 10
-  dtimesub = dtime/nsub
+  dtimesub = dtime/wk_nsub
 !
 IF (first_call) THEN
@@ -685,4 +683,6 @@
                     dth, hw0, rho, delta_t_min, &
                     ktop, wk_adv, h_zzz, ptop1, ktop1)
+ 
+   !!print'("pkupper APPEL ",7i6)',0,int(ptop/100.),int(ptop1/100.),int(pupper/100.),ktop,ktop1,kupper
    
    IF (prt_level>=10) THEN
@@ -860,7 +860,7 @@
   ! -----------------
 
-!    nsub and dtimesub definitions moved to begining of routine.
-!!  nsub = 10
-!!  dtimesub = dtime/nsub
+!    wk_nsub and dtimesub definitions moved to begining of routine.
+!!  wk_nsub = 10
+!!  dtimesub = dtime/wk_nsub
 
   
@@ -869,5 +869,5 @@
   ! ------------------------------------------------------------------------
   !
-  DO isubstep = 1, nsub
+  DO isubstep = 1, wk_nsub
   !
   ! ------------------------------------------------------------------------
@@ -1315,5 +1315,5 @@
              (1.-alpha_up(i,k))*omgbdth(i,k)- &
              alpha_up(i,k+1)*omgbdth(i,k+1))*ppi(i, k)
-!           print*,'d_deltatw=', k, d_deltatw(i,k)
+!           print*,'d_d,k_ptop_provis(i)eltatw=', k, d_deltatw(i,k)
 
           d_deltaqw(i, k) = dtimesub/(ph(i,k)-ph(i,k+1))* &
@@ -1712,4 +1712,5 @@
                     dth, hw, rho, delta_t_min, &
                     ktop, wk_adv, h_zzz, ptop1, ktop1)
+   !! print'("pkupper APPEL ",7i6)',isubstep,int(ptop/100.),int(ptop1/100.),int(pupper/100.),ktop,ktop1,kupper
 
     ! 5/ Set deltatw & deltaqw to 0 above kupper
@@ -2422,4 +2423,5 @@
 USE lmdz_wake_ini , ONLY : hwmin
 USE lmdz_wake_ini , ONLY : iflag_wk_new_ptop, wk_delta_t_min, wk_frac_int_delta_t
+USE lmdz_wake_ini , ONLY : wk_int_delta_t_min
 
 IMPLICIT NONE
@@ -2444,4 +2446,5 @@
 INTEGER :: i,k
 
+LOGICAL,    DIMENSION (klon)       :: wk_active
 REAL                               :: delta_t_min
 REAL,     DIMENSION (klon)         :: dthmin
@@ -2451,4 +2454,5 @@
 
 INTEGER,     DIMENSION (klon)                     :: k_ptop_provis
+REAL,     DIMENSION (klon)                     :: zk_ptop_provis
 REAL,     DIMENSION (klon)                        :: omega        !!
 REAL,     DIMENSION (klon,klev+1)                 :: int_dth      !! 
@@ -2500,4 +2504,5 @@
       END DO
     END DO
+
 
 
@@ -2595,40 +2600,84 @@
     ! -----------------------------------------------------------------------
 !if (iflag_wk_new_ptop>0) then
+do i=1,klon
+   ptop1(i)=ph(i,1)
+   ktop1(i)=1
+   h_zzz(i)=0.
+enddo
     
 IF (iflag_wk_new_ptop/=0) THEN
-
     
-       DO i = 1, klon
-          IF (wk_adv(i)) THEN 
-             int_dth(i,1) = 0.
-         END IF
-       END DO
+    int_dth(1:klon,1:klev+1)=0.
+    DO i = 1, klon
+       IF (wk_adv(i)) THEN 
+          int_dth(i,1) = 0.
+      END IF
+    END DO
     
-    
-    
-    DO K = 2, klev+1
-       Do i = 1, klon
-          IF (wk_adv(i)) THEN
-             if (k<=k_ptop_provis(i)) then
-                !  ddd=dth(i,k-1)*(ph(i,k-1) - min(ptop_provis(i),ph(i,k)))
-                  ddd=dth(i,k-1)*(ph(i,k-1) - ph(i,k))
-             else
-                  ddd=0.
-             endif
-             int_dth(i,k) = int_dth(i,k-1) + ddd
-          !ELSE
-          !   int_dth(i,k) = 0.
-          END IF
-       END DO
-    END DO
+    if (abs(iflag_wk_new_ptop) == 1 ) then
+        DO k = 2, klev+1
+           Do i = 1, klon
+              IF (wk_adv(i)) THEN
+                 if (k<=k_ptop_provis(i)) then
+                      ddd=dth(i,k-1)*(ph(i,k-1) - max(ptop_provis(i),ph(i,k)))
+                      !ddd=dth(i,k-1)*(ph(i,k-1) - ph(i,k))
+                 else
+                      ddd=0.
+                 endif             
+                 int_dth(i,k) = int_dth(i,k-1) + ddd
+              !ELSE
+              !   int_dth(i,k) = 0.
+              END IF
+           END DO
+        END DO
+    else
+        k_ptop_provis(:)=klev+1
+        dthmin(:)=dth(:,1)
+        ! calcul de l'int??grale de dT * dP jusqu'au dernier
+        ! niveau avec dT<0. (en s'assurant qu'on a bien un 
+        ! dT negatif plus bas)
+        DO k = 1, klev
+           DO i = 1, klon
+              dthmin(i)=min(dthmin(i),dth(i,k))
+              ddd=dth(i,k)*(ph(i,k)-ph(i,k+1))
+              if (dthmin(i)<0.) then
+                  if (k>=k_ptop_provis(i)) then
+                      ddd=0.
+                  else if (dth(i,k)>=0.) then
+                      ddd=0.
+                      k_ptop_provis(i)=k+1
+                  endif
+              endif
+              int_dth(i,k+1) = int_dth(i,k)+ ddd
+           ENDDO
+        ENDDO
+
+        DO i = 1, klon
+           if ( k_ptop_provis(i)==klev+1 .or. .not. wk_adv(i)) then
+                k_ptop_provis(i)=1
+           endif
+        ENDDO
+    endif ! (abs(iflag_wk_new_ptop) == 1 )
    ! print*, 'xxx, int_dth', (k,int_dth(1,k),k=1,klev)
    ! print*, 'xxx, k_ptop_provis', k_ptop_provis(1)
    
+
  
+    ! On se limite ?? des poches avec integrale dT * dp < -wk_int_delta_t_min
+    do i=1,klon
+          if (int_dth(i,k_ptop_provis(i)) > -wk_int_delta_t_min .or. k_ptop_provis(i)==1) then
+          !if (1==0) then
+             wk_active(i)=.false.
+             ptop(i)=ph(i,1)
+             ktop(i)=1
+             hw_(i)=0.
+          else
+             wk_active(i)=wk_adv(i)
+          endif
+    enddo
+
     DO i=1,klon
-       IF (wk_adv(i)) THEN
+       IF (wk_active(i)) THEN
         frac_int_dth(i)=wk_frac_int_delta_t*int_dth(i,k_ptop_provis(i))
-       !ELSE
-       ! frac_int_dth(i)=0.
        ENDIF
     ENDDO
@@ -2636,7 +2685,8 @@
        DO i =1, klon
 !         print*,ipas,'yyy ',k,int_dth(i,k),frac_int_dth(i)
-          IF (wk_adv(i)) THEN
+          IF (wk_active(i)) THEN
             IF (int_dth(i,k)>=frac_int_dth(i)) THEN
               ktop1(i) = min(k, k_ptop_provis(i))
+              !ktop1(i) = k
               !print*,ipas,'yyy ktop1= ',ktop1
             ENDIF
@@ -2647,5 +2697,5 @@
     
     DO i = 1, klon
-       IF (wk_adv(i)) THEN
+       IF (wk_active(i)) THEN
            !print*, ipas,'xxx1, int_dth(i,ktop1(i)), frac_int_dth(i), int_dth(i,ktop1(i)+1) ',ktop1
            ddd=int_dth(i,ktop1(i)+1)-int_dth(i,ktop1(i))
@@ -2655,11 +2705,11 @@
               omega(i) = (frac_int_dth(i) - int_dth(i,ktop1(i)))/ddd
            endif
-           print*,'OMEGA ',omega(i)
+           !! print*,'OMEGA ',omega(i)
        END IF
     END DO
     
-    print*, 'xxx'
-    DO i = 1, klon
-       IF (wk_adv(i)) THEN
+    !! print*, 'xxx'
+    DO i = 1, klon
+       IF (wk_active(i)) THEN
       ! print*, 'xxx, int_dth(i,ktop1(i)), frac_int_dth(i), int_dth(i,ktop1(i)+1) ', &
       !               int_dth(i,ktop1(i)), frac_int_dth(i), int_dth(i,ktop1(i)+1)
@@ -2671,5 +2721,5 @@
     
     DO i=1, klon
-       IF (wk_adv(i)) THEN 
+       IF (wk_active(i)) THEN 
            zzz(i, 1) = 0
        END IF
@@ -2677,5 +2727,5 @@
     DO k = 1, klev
        DO i = 1, klon 
-           IF (wk_adv(i)) THEN         
+           IF (wk_active(i)) THEN         
               dzz(i,k) = (ph(i,k) - ph(i,k+1))/(rho(i,k)*RG)
               zzz(i,k+1) = zzz(i,k) + dzz(i,k)
@@ -2685,10 +2735,11 @@
     
     DO i =1, klon
-       IF (wk_adv(i)) THEN
+       IF (wk_active(i)) THEN
            h_zzz(i) = max((1- omega(i))*zzz(i,ktop1(i)) + omega(i)*zzz(i,ktop1(i)+1), hwmin)
        END IF
     END DO
 
-ENDIF
+
+ENDIF ! (iflag_wk_new_ptop/=0)
 
 !if (iflag_wk_new_ptop==2) then
@@ -2720,8 +2771,8 @@
 
 ELSE
-  
   DO i=1, klon
      ! pupper(i) = wk_pupper*ptop(i)+(1.-wk_pupper)*ph(i, 1) 
-     pupper(i) = min( wk_pupper*ptop(i)+(1.-wk_pupper)*ph(i, 1) , ptop(i)-5000.) 
+     !  pupper(i) = min( wk_pupper*ptop(i)+(1.-wk_pupper)*ph(i, 1) , ptop(i)-50.) 
+      pupper(i) = min( wk_pupper*ptop(i)+(1.-wk_pupper)*ph(i, 1) , ptop(i)-5000.) 
   END DO
 END IF
@@ -2741,4 +2792,18 @@
   END DO 
   !---------- FIN nouveau calcul hw et ptop -------------------------------------
+
+IF (iflag_wk_new_ptop==999) THEN
+    DO i = 1, klon
+    hw_(i)=0.
+    ptop(i)=ph(i,1)
+    Ktop(i)=1
+    pupper(i)=ph(i,2)
+    kupper(i)=2
+    h_zzz(i)=0.
+    Ptop1(i)=ph(i,1)
+    ENDDO
+ENDIF
+
+zk_ptop_provis=k_ptop_provis
 
     RETURN
@@ -2798,5 +2863,4 @@
   
   REAL                                                  :: delta_t_min
-  INTEGER                                               :: nsub
   INTEGER                                               :: i, k
   REAL                                                  :: wdens0
Index: /LMDZ6/trunk/libf/phylmd/lmdz_wake_ini.F90
===================================================================
--- /LMDZ6/trunk/libf/phylmd/lmdz_wake_ini.F90	(revision 4907)
+++ /LMDZ6/trunk/libf/phylmd/lmdz_wake_ini.F90	(revision 4908)
@@ -55,4 +55,7 @@
   INTEGER, SAVE, PROTECTED                                    :: iflag_wk_profile
   !$OMP THREADPRIVATE(iflag_wk_profile)
+
+  INTEGER, SAVE, PROTECTED                                    :: wk_nsub
+  !$OMP THREADPRIVATE(wk_nsub)
 
   INTEGER, SAVE, PROTECTED                                    :: iflag_wk_new_ptop
@@ -75,4 +78,6 @@
   REAL, SAVE, PROTECTED                                        ::smallestreal
   !$OMP THREADPRIVATE(smallestreal)
+  REAL, SAVE, PROTECTED                                        :: wk_int_delta_t_min
+  !$OMP THREADPRIVATE(wk_int_delta_t_min)
 
 
@@ -226,4 +231,7 @@
   CALL getin_p('iflag_wk_new_ptop',iflag_wk_new_ptop)
 
+  wk_nsub = 10
+  CALL getin_p('wk_nsub',wk_nsub)
+
   tau_cv = 4000.
   CALL getin_p('tau_cv', tau_cv)
@@ -231,4 +239,7 @@
   wk_delta_t_min = 0.
   CALL getin_p('wk_delta_t_min', wk_delta_t_min)
+  
+  wk_int_delta_t_min = 10.
+  CALL getin_p('wk_int_delta_t_min', wk_int_delta_t_min)
   
   wk_frac_int_delta_t = 0.9
@@ -262,6 +273,8 @@
   
   WRITE(*,*) 'wk_delta_t_min=', wk_delta_t_min
+  WRITE(*,*) 'wk_int_delta_t_min=', wk_int_delta_t_min
   WRITE(*,*) 'wk_frac_int_delta_t=', wk_frac_int_delta_t
   WRITE(*,*) 'iflag_wk_new_ptop=', iflag_wk_new_ptop
+  WRITE(*,*) 'wk_nsub=', wk_nsub
 
  RETURN
