Index: LMDZ5/trunk/libf/phylmd/cv3_routines.F90
===================================================================
--- LMDZ5/trunk/libf/phylmd/cv3_routines.F90	(revision 2507)
+++ LMDZ5/trunk/libf/phylmd/cv3_routines.F90	(revision 2508)
@@ -105,4 +105,6 @@
      ok_intermittent=.False.
      CALL getin_p('ok_intermittent',ok_intermittent)
+     ok_optim_yield=.False.
+     CALL getin_p('ok_optim_yield',ok_optim_yield)
      coef_peel=0.25
      CALL getin_p('coef_peel',coef_peel)
@@ -126,4 +128,5 @@
     WRITE (*, *) 'tau_stop=', tau_stop
     WRITE (*, *) 'ok_intermittent=', ok_intermittent
+    WRITE (*, *) 'ok_optim_yield =', ok_optim_yield
     WRITE (*, *) 'coef_peel=', coef_peel
 
@@ -2977,21 +2980,26 @@
 !
 !local variables:
-      INTEGER i, k, il, n, j, num1
-      REAL rat, delti
-      REAL ax, bx, cx, dx, ex
-      REAL cpinv, rdcp, dpinv
-      REAL awat(nloc)
-      REAL lvcp(nloc, na), lfcp(nloc, na)                  ! , mke(nloc, na) ! unused . jyg
-      REAL am(nloc), work(nloc), ad(nloc), amp1(nloc)
+      INTEGER                                            :: i, k, il, n, j, num1
+      REAL                                               :: rat, delti
+      REAL                                               :: ax, bx, cx, dx, ex
+      REAL                                               :: cpinv, rdcp, dpinv
+      REAL, DIMENSION (nloc)                             ::  awat
+      REAL, DIMENSION (nloc, nd)                         :: lvcp, lfcp              ! , mke ! unused . jyg
+      REAL, DIMENSION (nloc)                             :: am, work, ad, amp1
 !!      real up1(nloc), dn1(nloc)
-      REAL up1(nloc, nd, nd), dn1(nloc, nd, nd)
-      REAL asum(nloc), bsum(nloc), csum(nloc), dsum(nloc)
-      REAL esum(nloc), fsum(nloc), gsum(nloc), hsum(nloc)
-      REAL th_wake(nloc, nd)
-      REAL alpha_qpos(nloc), alpha_qpos1(nloc)
-      REAL qcond(nloc, nd), nqcond(nloc, nd), wa(nloc, nd) ! cld
-      REAL siga(nloc, nd), sax(nloc, nd), mac(nloc, nd) ! cld
-      REAL sument(nloc), sigment(nloc,nd), qtment(nloc,nd) ! cld
-      REAL qnk(nloc)
+      REAL, DIMENSION (nloc, nd, nd)                     :: up1, dn1
+!jyg<
+      REAL, DIMENSION (nloc, nd)                         :: up_to, up_from
+      REAL, DIMENSION (nloc, nd)                         :: dn_to, dn_from
+!>jyg
+      REAL, DIMENSION (nloc)                             :: asum, bsum, csum, dsum
+      REAL, DIMENSION (nloc)                             :: esum, fsum, gsum, hsum
+      REAL, DIMENSION (nloc, nd)                         :: th_wake
+      REAL, DIMENSION (nloc)                             :: alpha_qpos, alpha_qpos1
+      REAL, DIMENSION (nloc, nd)                         :: qcond, nqcond, wa           ! cld
+      REAL, DIMENSION (nloc, nd)                         :: siga, sax, mac              ! cld
+      REAL, DIMENSION (nloc)                             :: sument
+      REAL, DIMENSION (nloc, nd)                         :: sigment, qtment             ! cld
+      REAL, DIMENSION (nloc)                             :: qnk
       REAL sumdq !jyg
 ! 
@@ -3233,4 +3241,107 @@
 ! print*,'cv3_yield apres ft'
 
+!jyg<
+!-----------------------------------------------------------
+           IF (ok_optim_yield) THEN                       !|
+!-----------------------------------------------------------
+!
+!***                                                      ***
+!***    Compute convective mass fluxes upwd and dnwd      ***
+
+upwd(:,:) = 0.
+up_to(:,:) = 0.
+up_from(:,:) = 0.
+dnwd(:,:) = 0.
+dn_to(:,:) = 0.
+dn_from(:,:) = 0.
+!
+! =================================================
+!              upward fluxes                      |
+! ------------------------------------------------
+DO i = 2, nl
+  DO il = 1, ncum
+    IF (i<=inb(il)) THEN
+      up_to(il,i) = m(il,i)
+    ENDIF
+  ENDDO
+  DO j = 1, i-1
+    DO il = 1, ncum
+      IF (i<=inb(il)) THEN
+        up_to(il,i) = up_to(il,i) + ment(il,j,i)
+      ENDIF
+    ENDDO
+  ENDDO
+ENDDO
+!
+DO i = 1, nl
+  DO il = 1, ncum
+    IF (i<=inb(il)) THEN
+      up_from(il,i) = cbmf(il)*wghti(il,i)
+    ENDIF
+  ENDDO
+ENDDO
+!!DO i = 2, nl
+!!  DO j = i+1, nl          !! Permuter les boucles i et j
+DO j = 3, nl
+  DO i = 2, j-1
+    DO il = 1, ncum
+      IF (j<=inb(il)) THEN
+        up_from(il,i) = up_from(il,i) + ment(il,i,j)
+      ENDIF
+    ENDDO
+  ENDDO
+ENDDO
+!
+! The difference between upwd(il,i) and upwd(il,i-1) is due to updrafts ending in layer 
+!(i-1) (theses drafts cross interface (i-1) but not interface(i)) and to updrafts starting 
+!from layer (i-1) (theses drafts cross interface (i) but not interface(i-1)): 
+! 
+DO i = 2, nlp
+  DO il = 1, ncum
+    upwd(il,i) = max(0., upwd(il,i-1) - up_to(il,i-1) + up_from(il,i-1))
+  ENDDO
+ENDDO
+!
+! =================================================
+!              downward fluxes                    |
+! ------------------------------------------------
+DO i = 1, nl
+  DO j = i+1, nl
+    DO il = 1, ncum
+      IF (j<=inb(il)) THEN
+        dn_to(il,i) = dn_to(il,i) + ment(il,j,i)
+      ENDIF
+    ENDDO
+  ENDDO
+ENDDO
+!
+!!DO i = 2, nl
+!!  DO j = 1, i-1          !! Permuter les boucles i et j 
+DO j = 1, nl
+  DO i = j+1, nl
+    DO il = 1, ncum
+      IF (i<=inb(il)) THEN
+        dn_from(il,i) = dn_from(il,i) + ment(il,i,j)
+      ENDIF
+    ENDDO
+  ENDDO
+ENDDO
+!
+! The difference between dnwd(il,i) and dnwd(il,i+1) is due to downdrafts ending in layer 
+!(i) (theses drafts cross interface (i+1) but not interface(i)) and to downdrafts 
+!starting from layer (i) (theses drafts cross interface (i) but not interface(i+1)): 
+!
+DO i = nl-1, 1, -1
+  DO il = 1, ncum
+    dnwd(il,i) = max(0., dnwd(il,i+1) - dn_to(il,i) + dn_from(il,i))
+  ENDDO
+ENDDO
+! =================================================
+!
+!-----------------------------------------------------------
+        ENDIF !(ok_optim_yield)                           !|
+!-----------------------------------------------------------
+!>jyg
+
 ! ***  calculate tendencies of potential temperature and mixing ratio  ***
 ! ***               at levels above the lowest level                   ***
@@ -3240,5 +3351,8 @@
 
 
-  DO i = 2, nl + 1 ! newvecto: mettre nl au lieu nl+1?
+!jyg<
+!!  DO i = 2, nl + 1 ! newvecto: mettre nl au lieu nl+1?
+  DO i = 2, nl
+!>jyg 
 
     num1 = 0
@@ -3248,12 +3362,21 @@
     IF (num1<=0) GO TO 500
 
+!
 !jyg<
-!!    CALL zilch(amp1, ncum)
-!!    CALL zilch(ad, ncum)
+!-----------------------------------------------------------
+           IF (ok_optim_yield) THEN                       !|
+!-----------------------------------------------------------
+DO il = 1, ncum
+   amp1(il) = upwd(il,i+1)
+   ad(il) = dnwd(il,i)
+ENDDO
+!-----------------------------------------------------------
+        ELSE !(ok_optim_yield)                            !|
+!-----------------------------------------------------------
+!>jyg
     DO il = 1,ncum
       amp1(il) = 0.
       ad(il) = 0.
     ENDDO
-!>jyg
 
     DO k = 1, nl + 1
@@ -3272,18 +3395,24 @@
     END DO
 
-    DO k = 1, i
-      DO j = i + 1, nl + 1
+    DO j = i + 1, nl + 1         
+       DO k = 1, i
+          !yor! reverted j and k loops 
+          DO il = 1, ncum
+!yor!        IF (i<=inb(il) .AND. j<=(inb(il)+1)) THEN ! the second condition implies the first !
+             IF (j<=(inb(il)+1)) THEN  
+                amp1(il) = amp1(il) + ment(il, k, j)
+             END IF
+          END DO
+       END DO
+    END DO
+
+    DO k = 1, i - 1
+!jyg<
+!!      DO j = i, nl + 1 ! newvecto: nl au lieu nl+1?
+      DO j = i, nl
+!>jyg
         DO il = 1, ncum
-          IF (i<=inb(il) .AND. j<=(inb(il)+1)) THEN
-            amp1(il) = amp1(il) + ment(il, k, j)
-          END IF
-        END DO
-      END DO
-    END DO
-
-    DO k = 1, i - 1
-      DO j = i, nl + 1 ! newvecto: nl au lieu nl+1?
-        DO il = 1, ncum
-          IF (i<=inb(il) .AND. j<=inb(il)) THEN
+!yor!        IF (i<=inb(il) .AND. j<=inb(il)) THEN ! the second condition implies the 1st !
+             IF (j<=inb(il)) THEN   
             ad(il) = ad(il) + ment(il, j, k)
           END IF
@@ -3291,4 +3420,10 @@
       END DO
     END DO
+!
+!-----------------------------------------------------------
+        ENDIF !(ok_optim_yield)                           !|
+!-----------------------------------------------------------
+!
+!!   print *,'yield, i, amp1, ad', i, amp1(1), ad(1)
 
     DO il = 1, ncum
@@ -3438,5 +3573,8 @@
 !AC!      enddo
 
-    DO k = i, nl + 1
+!jyg<
+!!    DO k = i, nl + 1
+    DO k = i, nl
+!>jyg
 
       IF (iflag_mix/=0) THEN
@@ -3741,6 +3879,22 @@
     END DO
   END DO
+!jyg<
+!-----------------------------------------------------------
+           IF (ok_optim_yield) THEN                       !|
+!-----------------------------------------------------------
   DO i = 1, nl
-    DO j = 1, nl
+    DO il = 1, ncum
+      IF (iflag(il)<=1) THEN
+        upwd(il, i) = upwd(il, i)/alpha_qpos(il)
+        dnwd(il, i) = dnwd(il, i)/alpha_qpos(il)
+      END IF
+    END DO
+  END DO
+!-----------------------------------------------------------
+        ENDIF !(ok_optim_yield)                           !|
+!-----------------------------------------------------------
+!>jyg
+  DO j = 1, nl !yor! inverted i and j loops
+     DO i = 1, nl
       DO il = 1, ncum
         IF (iflag(il)<=1) THEN
@@ -3776,11 +3930,4 @@
   DO i = 1, nl
     DO il = 1, ncum
-      upwd(il, i) = 0.0
-      dnwd(il, i) = 0.0
-    END DO
-  END DO
-
-  DO i = 1, nl
-    DO il = 1, ncum
       dnwd0(il, i) = -mp(il, i)
     END DO
@@ -3795,12 +3942,23 @@
 
 
+!jyg<
+!-----------------------------------------------------------
+           IF (.NOT.ok_optim_yield) THEN                  !|
+!-----------------------------------------------------------
   DO i = 1, nl
     DO il = 1, ncum
-      IF (i>=icb(il) .AND. i<=inb(il)) THEN
-        upwd(il, i) = 0.0
-        dnwd(il, i) = 0.0
-      END IF
-    END DO
-  END DO
+      upwd(il, i) = 0.0
+      dnwd(il, i) = 0.0
+    END DO
+  END DO
+
+!!  DO i = 1, nl                                           ! useless; jyg
+!!    DO il = 1, ncum                                      ! useless; jyg
+!!      IF (i>=icb(il) .AND. i<=inb(il)) THEN              ! useless; jyg
+!!        upwd(il, i) = 0.0                                ! useless; jyg
+!!        dnwd(il, i) = 0.0                                ! useless; jyg
+!!      END IF                                             ! useless; jyg
+!!    END DO                                               ! useless; jyg
+!!  END DO                                                 ! useless; jyg
 
   DO i = 1, nl
@@ -3813,11 +3971,24 @@
   END DO
 
+!yor! commented original
+!  DO i = 1, nl
+!    DO k = i, nl
+!      DO n = 1, i - 1
+!        DO il = 1, ncum
+!          IF (i>=icb(il) .AND. i<=inb(il) .AND. k<=inb(il)) THEN
+!            up1(il, k, i) = up1(il, k, i) + ment(il, n, k)
+!            dn1(il, k, i) = dn1(il, k, i) - ment(il, k, n)
+!          END IF
+!        END DO
+!      END DO
+!    END DO
+!  END DO
+!yor! replaced with
   DO i = 1, nl
     DO k = i, nl
       DO n = 1, i - 1
         DO il = 1, ncum
-          IF (i>=icb(il) .AND. i<=inb(il) .AND. k<=inb(il)) THEN
-            up1(il, k, i) = up1(il, k, i) + ment(il, n, k)
-            dn1(il, k, i) = dn1(il, k, i) - ment(il, k, n)
+          IF (i>=icb(il) .AND. k<=inb(il)) THEN ! yor ! as i always <= k
+             up1(il, k, i) = up1(il, k, i) + ment(il, n, k)
           END IF
         END DO
@@ -3825,4 +3996,16 @@
     END DO
   END DO
+  DO i = 1, nl
+    DO n = 1, i - 1
+      DO k = i, nl
+        DO il = 1, ncum
+          IF (i>=icb(il) .AND. k<=inb(il)) THEN ! yor !  i always <= k
+             dn1(il, k, i) = dn1(il, k, i) - ment(il, k, n)
+          END IF
+        END DO
+      END DO
+    END DO
+  END DO
+!yor! end replace 
 
   DO i = 1, nl
@@ -3875,4 +4058,8 @@
 !!!!
 !!!!      ENDDO
+!-----------------------------------------------------------
+        ENDIF !(.NOT.ok_optim_yield)                      !|
+!-----------------------------------------------------------
+!>jyg
 
 ! ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
Index: LMDZ5/trunk/libf/phylmd/cv3param.h
===================================================================
--- LMDZ5/trunk/libf/phylmd/cv3param.h	(revision 2507)
+++ LMDZ5/trunk/libf/phylmd/cv3param.h	(revision 2508)
@@ -7,4 +7,5 @@
 !------------------------------------------------------------
 
+      logical ok_optim_yield
       logical ok_convstop
       logical ok_intermittent
@@ -36,5 +37,6 @@
                       ,flag_wb &
                       ,noff, minorig, nl, nlp, nlm  &
-                      ,ok_convstop, ok_intermittent
+                      ,ok_convstop, ok_intermittent &
+                      ,ok_optim_yield
 !$OMP THREADPRIVATE(/cv3param/)
 
