Index: trunk/LMDZ.TITAN/libf/muphytitan/config/mmp2m1d.titan.cfg
===================================================================
--- trunk/LMDZ.TITAN/libf/muphytitan/config/mmp2m1d.titan.cfg	(revision 1815)
+++ 	(revision )
@@ -1,56 +1,0 @@
-
-## include parameters from YAMMS configuration.
-## (all the parameters defined in this file will overriden values defined in mp2m.cfg)
-#include inputs/mp2m.cfg
-
-## INPUT FILES (profiles)
-# Path of the spherical mode transfert probability look-up tables file
-# (optional if 'transfert_probability' is True) 
-ps2s_file             = inputs/mmp_ps2s.nc
-# Path of the spherical mode transfert probability look-up tables file
-# (optional if 'transfert_probability' is True) 
-mq_file               = inputs/mmp_qmean.nc
-
-# Electric charging coagulation correction
-# If set to .false. then no correction is assumed
-electric_charging     = F 
-
-# Enable/disable spherical mode transfert probability
-transfert_probability = T
-
-# MP2M Configuration file (relative to executable path !)
-# not needed here.. we have included the file (at the top of this one).
-mp2m_cfg = ''
-
-#  alpha_X sections contain the parameters of the inter-moments relation function for
-#  the mode X: either spherical (s) or fractal (f)
-#  dndr_X sections contain the parameters of the size-distribution law of the mode X
-[alpha_s]
-a =  0.09887273981548324
-b =  0.0
-c =  0.0
-[dndr_s]
-rc = 1e-6
-a0 = 1.2339698E+15
-c  = 0.
-a  = 6.6133008E-06, 8.9867475E+00,   6.5136506E+00,   4.5832927E-02
-b  = 3.1369611E+01, 4.9958440E+00,  -6.7012835E+00,  -1.5499933E+01
-[alpha_f]
-a =  -2.5000000E-02,   2.1999082E-02,   1.5642034E-01,   2.5988014E-02,  -5.3718083E-02
-b =   1.6000000E+00,   1.7287786E-02,   4.4026716E-01,   2.7319185E-01,   2.2855094E-01
-c =  -3.5000000E+01,  -3.6736168E-02,  -2.3930789E+01,  -3.3881667E+00,  -6.0753628E+00
-[dndr_f]
-rc = 5.1230710E-07
-a0 = 1.2339698E+15
-c  = 0.
-a  = 6.6133008E-06, 8.9867475E+00,   6.5136506E+00,   4.5832927E-02
-b  = 3.1369611E+01, 4.9958440E+00,  -6.7012835E+00,  -1.5499933E+01
-# ================= #
-# b^T_k cofficients #
-# ================= #
-# This section gathers the values of all the btk coefficient used in the coagulation
-# equations for the free-molecular regime.
-[btks]
-bt0 = 0.72d0, 0.72d0, 0.80d0, 0.97d0, 0.00d0
-bt3 = 0.73d0, 0.73d0, 0.00d0, 0.97d0, 0.97d0
-
Index: trunk/LMDZ.TITAN/libf/muphytitan/config/mp2m.titan.cfg
===================================================================
--- trunk/LMDZ.TITAN/libf/muphytitan/config/mp2m.titan.cfg	(revision 1815)
+++ 	(revision )
@@ -1,74 +1,0 @@
-# YAMMS model parameters
-# ======================
-
-### Model global parameters 
-###------------------------
-# Model time step (s)
-timestep               = 5000
-# Enable/disable Haze production process 
-haze_production        = T
-# Enable/disable Haze coagulation process 
-haze_coagulation       = T
-# Coagulation interactions, a combination of:
-#    0 - no interactions (same as haze_coagulation == F)
-#    1 - SS interactions
-#    2 - SF interactions
-#    4 - FF interactions.
-# (for example: 5 = 4+1 --> SS and FF coagulation only)
-haze_coag_interactions = 7
-# Enable/disable Haze sedimentation process 
-haze_sedimentation     = T
-# Disable Fiadero correction for sedimentation process
-no_fiadero             = F
-# Fiadero correction minimum ratio threshold
-fiadero_min_ratio      = 0.1
-# Fiadero correction maximum ratio threshold
-fiadero_max_ratio      = 10.
-# Force settling velocity to M0
-wsed_m0 = F
-# Force settling velocity to M3
-wsed_m3 = F
-# Enable/disable clouds microphysics
-clouds_microphysics    = F 
-# Enable/disable clouds sedimentation process
-# (automatically set to F if clouds microphysics is not enabled)
-clouds_sedimentation   = T
-# Enable/disable clouds nucleation and condensation processes
-# (automatically set to F if clouds microphysics is not enabled)
-clouds_nuc_cond        = T
-# Condensible species configuration file
-# (not needed if clouds microphysics is not enabled)
-specie_cfg             = /home/burgalat/test_sedim/inputs/mp2m_species.cfg
-
-### Aerosols related parameters
-###----------------------------
-# Aerosols density (kg.m-3)
-rho_aer = 1000.
-# Monomer radius (m)
-rm       = 6.6e-8
-# Fractal dimension of the fractal mode 
-df       = 2.
-# Aerosols production pressure level (Pa)
-p_prod   = 1.0
-# Aerosols production rate (kg.m-2.s-1)
-tx_prod  = 3.5e-13 
-# Characteristic radius for production scheme
-rc_prod  = 20e-9
-# Aerosols electric charging (e-.m-1)
-# (not used, only set as a reminder !)
-ne       = -15d6
-###--------------------------
-
-### Planet related parameters (currently Venus for sedim, Titan for other params)
-###--------------------------
-# Planet radius (m)
-planet_radius = 2575000.
-# Air molecule radius (m)
-air_radius    = 1.75e-10
-# Air mean molar mass (kg.mol-1)
-air_molarmass = 28.e-3
-## Planet acceleration of gravity at the ground (m.s-2)
-g0            = 1.35
-###--------------------------
-
-
Index: trunk/LMDZ.TITAN/libf/muphytitan/config/template.cfg
===================================================================
--- trunk/LMDZ.TITAN/libf/muphytitan/config/template.cfg	(revision 1815)
+++ 	(revision )
@@ -1,87 +1,0 @@
-# YAMMS model parameters
-# ======================
-
-### Model global parameters 
-###------------------------
-# Enable/disable Haze production process 
-haze_production        = T
-# Enable/disable Haze coagulation process 
-haze_coagulation       = T
-# Coagulation interactions, a combination of:
-#    0 - no interactions (same as haze_coagulation == F)
-#    1 - SS interactions
-#    2 - SF interactions
-#    4 - FF interactions.
-# (for example: 5 = 4+1 --> SS and FF coagulation only)
-haze_coag_interactions = 7
-# Enable/disable Haze sedimentation process 
-haze_sedimentation     = T
-# Disable Fiadero correction for sedimentation process
-no_fiadero             = F
-# Fiadero correction minimum ratio threshold
-fiadero_min_ratio      = 0.1
-# Fiadero correction maximum ratio threshold
-fiadero_max_ratio      = 10.
-# Force settling velocity to M0
-wsed_m0 = T
-# Force settling velocity to M3
-wsed_m3 = F
-# Enable/disable clouds sedimentation process
-# (automatically set to F if clouds microphysics is not enabled)
-clouds_sedimentation   = T
-# Enable/disable clouds nucleation and condensation processes
-# (automatically set to F if clouds microphysics is not enabled)
-clouds_nuc_cond        = T
-# Condensible species configuration file
-# (not needed if clouds microphysics is not enabled)
-specie_cfg             = /home/burgalat/test_sedim/inputs/mp2m_species.cfg
-
-
-# Enable/disable spherical mode transfert probability
-transfert_probability = F
-# Path of the spherical mode transfert probability look-up tables file
-# (optional if 'transfert_probability' is False) 
-ps2s_file             = inputs/mmp_ps2s.nc
-
-# Electric charging coagulation correction
-# If set to .false. then no correction is assumed
-electric_charging     = F 
-# Path of the electric charging correction factor.
-# (optional if 'electric_charging' is False) 
-mq_file               = inputs/mmp_qmean.nc
-
-#  alpha_X sections contain the parameters of the inter-moments relation function for
-#  the mode X: either spherical (s) or fractal (f)
-#  dndr_X sections contain the parameters of the size-distribution law of the mode X
-[alpha_s]
-a =  -3.8450202E-04,   1.9300880E-01,  -1.5824302E+02,   3.1805033E-01,   1.1335227E-01,   3.0615066E-02
-b =  -3.9438738E-01,   3.3815486E+00,  -1.7000626E+02,   3.3353316E-01,   2.4314346E-01,  -1.6559447E-01
-c =  -7.9835178E-01,  -1.8853289E+01,  -2.0306468E+02,  -7.3818724E+00,  -3.4832076E+00,  -6.5662798E-01
-[dndr_s]
-rc = 4.58219580180634588E-007
-a0 = 86144.861255561875
-c  = 0d0
-a  = 2.48333861883769357E-040, 1.46076790655632173E-013, 1.71525517568997062E-009,
-    1.80855172875974993E-019, 1.48212594918347503E-047, 6.87247318898338451E-081
-b  = 59.518212357684796, 15.507500262021228, -5.4179933012448069,
-    -9.3500794017892854, -18.207927270524777, -27.248924688740562
-[alpha_f]
-a =  -3.8450202E-04,   1.9300880E-01,  -1.5824302E+02,   3.1805033E-01,   1.1335227E-01,   3.0615066E-02
-b =  -3.9438738E-01,   3.3815486E+00,  -1.7000626E+02,   3.3353316E-01,   2.4314346E-01,  -1.6559447E-01
-c =  -7.9835178E-01,  -1.8853289E+01,  -2.0306468E+02,  -7.3818724E+00,  -3.4832076E+00,  -6.5662798E-01
-[dndr_f]
-rc = 4.58219580180634588E-007
-a0 = 86144.861255561875
-c  = 0d0
-a  = 2.48333861883769357E-040, 1.46076790655632173E-013, 1.71525517568997062E-009,
-    1.80855172875974993E-019, 1.48212594918347503E-047, 6.87247318898338451E-081
-
-# ================= #
-# b^T_k cofficients #
-# ================= #
-# This section gathers the values of all the btk coefficient used in the coagulation
-# equations for the free-molecular regime.
-[btks]
-bt0 = 0.72d0, 0.72d0, 0.80d0, 0.97d0, 0.00d0
-bt3 = 0.73d0, 0.73d0, 0.00d0, 0.97d0, 0.97d0
-
Index: trunk/LMDZ.TITAN/libf/muphytitan/mm_haze.f90
===================================================================
--- trunk/LMDZ.TITAN/libf/muphytitan/mm_haze.f90	(revision 1815)
+++ trunk/LMDZ.TITAN/libf/muphytitan/mm_haze.f90	(revision 1819)
@@ -743,5 +743,7 @@
         IF ((ratio <= 0.9_mm_wp .OR. ratio >= 1.1_mm_wp) .AND. wth(i) /= 0._mm_wp) THEN
           wdt = wth(i)*dt
-          zcorf(i) = dzb/wdt * (exp(-wdt*log(ratio)/dzb)-1._mm_wp) / (1._mm_wp-ratio)
+          ! bugfix: max exponential arg to 30)
+          zcorf(i) = dzb/wdt * (exp(MIN(30._mm_wp,-wdt*log(ratio)/dzb))-1._mm_wp) / (1._mm_wp-ratio)
+          !zcorf(i) = dzb/wdt * (exp(-wdt*log(ratio)/dzb)-1._mm_wp) / (1._mm_wp-ratio)
         ENDIF
       ENDIF
Index: trunk/LMDZ.TITAN/libf/muphytitan/mm_microphysic.f90
===================================================================
--- trunk/LMDZ.TITAN/libf/muphytitan/mm_microphysic.f90	(revision 1815)
+++ trunk/LMDZ.TITAN/libf/muphytitan/mm_microphysic.f90	(revision 1819)
@@ -111,4 +111,8 @@
       ! add temporary aerosols tendencies (-> m-3)
       dm0a_f = dm0a_f + zdm0a_f  ; dm3a_f = dm3a_f + zdm3a_f
+      ! sanity check for clouds tendencies
+      WHERE (mm_m0ccn+dm0n*mm_dzlev < 0) ; dm0n = -mm_m0ccn/mm_dzlev ; END WHERE
+      WHERE (mm_m3ccn+dm3n*mm_dzlev < 0) ; dm3n = -mm_m3ccn/mm_dzlev ; END WHERE
+
       ! reverse directly clouds tendencies (-> m-2)
       dm0n   = dm0n(mm_nla:1:-1) * mm_dzlev(mm_nla:1:-1)
@@ -116,15 +120,23 @@
       DO i=1,mm_nesp
         dm3i(:,i)  = dm3i(mm_nla:1:-1,i)  * mm_dzlev(mm_nla:1:-1)
-        dgazs(:,i) = dgazs(mm_nla:1:-1,i) * mm_dzlev(mm_nla:1:-1)
+        WHERE (mm_m3ccn+dm3n*mm_dzlev < 0) ; dm3n = -mm_m3ccn/mm_dzlev ; END WHERE
+        dgazs(:,i) = dgazs(mm_nla:1:-1,i) 
+        ! no sanity check for gazs, let's prey.
       ENDDO
     ELSE
       dm0n = 0._mm_wp ; dm3n = 0._mm_wp ; dm3i = 0._mm_wp ; dgazs = 0._mm_wp
     ENDIF
-    ! multiply by altitude thickness and reverse vectors so they go
-    ! from ground to top :)
+    ! sanity check
+    WHERE (mm_m0aer_s+dm0a_s*mm_dzlev < 0) ; dm0a_s = -mm_m0aer_s/mm_dzlev ; END WHERE
+    WHERE (mm_m3aer_s+dm3a_f*mm_dzlev < 0) ; dm3a_s = -mm_m3aer_s/mm_dzlev ; END WHERE
+    WHERE (mm_m0aer_f+dm0a_f*mm_dzlev < 0) ; dm0a_f = -mm_m0aer_f/mm_dzlev ; END WHERE
+    WHERE (mm_m3aer_f+dm3a_f*mm_dzlev < 0) ; dm3a_f = -mm_m3aer_f/mm_dzlev ; END WHERE
+
+    ! multiply by altitude thickness and reverse vectors so they go from ground to top :)
     dm0a_s = dm0a_s(mm_nla:1:-1) * mm_dzlev(mm_nla:1:-1)
     dm3a_s = dm3a_s(mm_nla:1:-1) * mm_dzlev(mm_nla:1:-1)
     dm0a_f = dm0a_f(mm_nla:1:-1) * mm_dzlev(mm_nla:1:-1)
     dm3a_f = dm3a_f(mm_nla:1:-1) * mm_dzlev(mm_nla:1:-1)
+    
     RETURN
   END FUNCTION muphys_all
@@ -155,4 +167,9 @@
     ! Calls haze microphysics
     call mm_haze_microphysics(dm0a_s,dm3a_s,dm0a_f,dm3a_f)
+    ! sanity check
+    WHERE (mm_m0aer_s+dm0a_s*mm_dzlev < 0) ; dm0a_s = -mm_m0aer_s/mm_dzlev ; END WHERE
+    WHERE (mm_m3aer_s+dm3a_f*mm_dzlev < 0) ; dm3a_s = -mm_m3aer_s/mm_dzlev ; END WHERE
+    WHERE (mm_m0aer_f+dm0a_f*mm_dzlev < 0) ; dm0a_f = -mm_m0aer_f/mm_dzlev ; END WHERE
+    WHERE (mm_m3aer_f+dm3a_f*mm_dzlev < 0) ; dm3a_f = -mm_m3aer_f/mm_dzlev ; END WHERE
     ! reverse vectors so they go from ground to top :)
     dm0a_s = dm0a_s(mm_nla:1:-1) * mm_dzlev(mm_nla:1:-1) 
Index: trunk/LMDZ.TITAN/libf/muphytitan/mmp_gcm.f90
===================================================================
--- trunk/LMDZ.TITAN/libf/muphytitan/mmp_gcm.f90	(revision 1815)
+++ trunk/LMDZ.TITAN/libf/muphytitan/mmp_gcm.f90	(revision 1819)
@@ -27,4 +27,29 @@
     REAL(kind=mm_wp), DIMENSION(:), ALLOCATABLE :: c
   END TYPE
+
+  !> Size distribution parameters derived type.
+  !!
+  !! It stores the parameters of the size distribution law for Titan.
+  !! 
+  !! The size distribution law is represented by the minimization of a sum of
+  !! power law functions:
+  !!
+  !! $$ 
+  !! \displaystyle n\left(r\right) = \frac{A_{0}}{C+\sum_{i=1}^{n} A_{i}\times
+  !!                                    \left(\frac{r}{r_{c}}\right)^{-b_{i}}}
+  !! $$
+  TYPE, PUBLIC :: nprm
+    !> Scaling factor.
+    REAL(kind=mm_wp)                            :: a0
+    !> Characterisitic radius.
+    REAL(kind=mm_wp)                            :: rc
+    !> Additional constant to the sum of power law.
+    REAL(kind=mm_wp)                            :: c
+    !> Scaling factor of each power law.
+    REAL(kind=mm_wp), DIMENSION(:), ALLOCATABLE :: a
+    !> Power of each power law.
+    REAL(kind=mm_wp), DIMENSION(:), ALLOCATABLE :: b
+  END TYPE 
+
   !> Inter-moment relation set of parameters for the spherical mode.
   TYPE(aprm), PUBLIC, SAVE :: mmp_asp
@@ -32,15 +57,20 @@
   TYPE(aprm), PUBLIC, SAVE :: mmp_afp
 
+  !> Size-distribution law parameters of the spherical mode.
+  TYPE(nprm), PUBLIC, SAVE :: mmp_pns
+  !> Size-distribution law parameters of the fractal mode.
+  TYPE(nprm), PUBLIC, SAVE :: mmp_pnf
+
   !> Data set for @f$<Q>_{SF}^{M0}@f$.
   TYPE(dset2d), PUBLIC, SAVE, TARGET             :: mmp_qbsf0
-  !> Extended values of [[mmp_globals(module):mmp_qbsf0(variable)]] dataset.
+  !> Extended values of [[mmp_gcm(module):mmp_qbsf0(variable)]] dataset.
   REAL(kind=mm_wp), PUBLIC, SAVE, DIMENSION(2,2) :: mmp_qbsf0_e
   !> Data set for @f$<Q>_{SF}^{M3}@f$.
   TYPE(dset2d), PUBLIC, SAVE, TARGET             :: mmp_qbsf3
-  !> Extended values of [[mmp_globals(module):mmp_qbsf3(variable)]] dataset.
+  !> Extended values of [[mmp_gcm(module):mmp_qbsf3(variable)]] dataset.
   REAL(kind=mm_wp), PUBLIC, SAVE, DIMENSION(2,2) :: mmp_qbsf3_e
   !> Data set for @f$<Q>_{FF}^{M0}@f$.
   TYPE(dset2d), PUBLIC, SAVE, TARGET             :: mmp_qbff0
-  !> Extended values of [[mmp_globals(module):mmp_qbff0(variable)]] dataset.
+  !> Extended values of [[mmp_gcm(module):mmp_qbff0(variable)]] dataset.
   REAL(kind=mm_wp), PUBLIC, SAVE, DIMENSION(2,2) :: mmp_qbff0_e
   
@@ -247,4 +277,13 @@
     err = read_aprm(cparser,'alpha_f',mmp_afp)
     IF (err /= 0) call abort_program(error("alpha_s: "//TRIM(err%msg),-1))
+
+    ! get size-distribution laws parameters
+    IF (.NOT.cfg_has_section(cparser,'dndr_s')) call abort_program(error("Cannot find [dndr_s] section",-2))
+    err = read_nprm(cparser,'dndr_s',mmp_pns)
+    IF (err /= 0) call abort_program(error("dndr_s: "//TRIM(err%msg),-2))
+    IF (.NOT.cfg_has_section(cparser,'dndr_f')) call abort_program(error("Cannot find [dndr_f] section",-2))
+    err = read_nprm(cparser,'dndr_f',mmp_pnf)
+    IF (err /= 0) call abort_program(error("dndr_f: "//TRIM(err%msg),-2))
+
     ! btk coefficients
     IF (.NOT.cfg_has_section(cparser,'btks')) call abort_program(error("Cannot find [btks] section",-1))
@@ -265,8 +304,8 @@
 
   FUNCTION read_aprm(parser,sec,pp) RESULT(err)
-    !! Read and store [[mmp_globals(module):aprm(type)]] parameters. 
+    !! Read and store [[mmp_gcm(module):aprm(type)]] parameters. 
     TYPE(cfgparser), INTENT(in)  :: parser !! Configuration parser 
     CHARACTER(len=*), INTENT(in) :: sec    !! Name of the section that contains the parameters.
-    TYPE(aprm), INTENT(out)      :: pp     !! [[mmp_globals(module):aprm(type)]] object that stores the parameters values.
+    TYPE(aprm), INTENT(out)      :: pp     !! [[mmp_gcm(module):aprm(type)]] object that stores the parameters values.
     TYPE(error) :: err                     !! Error status of the function.
     err = cfg_get_value(parser,TRIM(sec)//'/a',pp%a) ; IF (err /= 0) RETURN
@@ -278,4 +317,20 @@
   END FUNCTION read_aprm
 
+  FUNCTION read_nprm(parser,sec,pp) RESULT(err)
+    !! Read and store [[mmp_gcm(module):nprm(type)]] parameters.
+    TYPE(cfgparser), INTENT(in)  :: parser !! Configuration parser 
+    CHARACTER(len=*), INTENT(in) :: sec    !! Name of the section that contains the parameters.
+    TYPE(nprm), INTENT(out)      :: pp     !! [[mmp_gcm(module):nprm(type)]] object that stores the parameters values.
+    TYPE(error) :: err                     !! Error status of the function.
+    err = cfg_get_value(parser,TRIM(sec)//'/rc',pp%rc) ; IF (err /= 0) RETURN
+    err = cfg_get_value(parser,TRIM(sec)//'/a0',pp%a0) ; IF (err /= 0) RETURN
+    err = cfg_get_value(parser,TRIM(sec)//'/c',pp%c)   ; IF (err /= 0) RETURN
+    err = cfg_get_value(parser,TRIM(sec)//'/a',pp%a)   ; IF (err /= 0) RETURN
+    err = cfg_get_value(parser,TRIM(sec)//'/b',pp%b)   ; IF (err /= 0) RETURN
+    IF (SIZE(pp%a) /= SIZE(pp%b)) &
+      err = error("Inconsistent number of coefficients (a and b must have the same size)",3)
+    RETURN
+  END FUNCTION read_nprm
+
 END MODULE MMP_GCM
 
Index: trunk/LMDZ.TITAN/libf/phytitan/calmufi.F90
===================================================================
--- trunk/LMDZ.TITAN/libf/phytitan/calmufi.F90	(revision 1815)
+++ trunk/LMDZ.TITAN/libf/phytitan/calmufi.F90	(revision 1819)
@@ -89,5 +89,5 @@
     ! Convert tracers to extensive ( except for gazs where we work with molar mass ratio )
     ! We suppose a given order of tracers !
-    int2ext(:) = ( plev(ilon,2:nlay+1) - plev(ilon,1:nlay) ) / g
+    int2ext(:) = ( plev(ilon,1:nlay) - plev(ilon,2:nlay+1) ) / g
 
     m0as(:) = pq(ilon,:,1) * int2ext(:)
