source: trunk/LMDZ.MARS/libf/phymars/topmons_mod.F90 @ 2316

Last change on this file since 2316 was 2255, checked in by abierjon, 5 years ago

Mars GCM:
Bug fix in topmons_mod : the array rhobarz(ig,l) (density at interlayer levels) had only nlayer allocated instead of nlayer+1 (caused issues in calculating the topdust detrainment at the top of the model)
AB

File size: 47.2 KB
Line 
1      MODULE topmons_mod
2
3      IMPLICIT NONE
4
5!     sub-grid scale mountain mesh fraction
6      REAL, SAVE, ALLOCATABLE :: alpha_hmons(:)
7
8      CONTAINS
9
10!=======================================================================
11! ENTRAINMENT OF DUST ABOVE THE SUB-GRID SCALE TOPOGRAPHY
12!=======================================================================
13! calculation of the vertical wind velocity of topdust tracers
14! transport scheme of topdust tracers
15! detrainement of topdust into background dust
16! -----------------------------------------------------------------------
17! Authors: M. Vals; C. Wang; T. Bertrand; F. Forget
18! Institution: Laboratoire de Meteorologie Dynamique (LMD) Paris, France
19! -----------------------------------------------------------------------
20
21      SUBROUTINE topmons(ngrid,nlayer,nq,ptime,ptimestep,              &
22                                 pq,pdq,pt,pdt,pplev,pplay,pzlev,      &
23                                 pzlay,pdtsw,pdtlw,                    &
24!             input for radiative transfer
25                                 icount,zday,zls,tsurf,igout,aerosol,  &
26                                 tauscaling,                           &
27!             input sub-grid scale rocket dust storm
28                                 totstormfract,clearatm,               &
29!             input sub-grid scale cloud
30                                 clearsky,totcloudfrac,                &
31!             input sub-grid scale mountain
32                                 nohmons,hsummit,                      &
33!             output
34                                 pdqtop,wfin,dsodust,dsords,dsotop,    &
35                                 tauref)
36
37      USE tracer_mod, only: igcm_topdust_mass,igcm_topdust_number &
38                            ,igcm_dust_mass,igcm_dust_number      &
39                            ,rho_dust
40      USE comcstfi_h, only: r,g,cpp,rcp
41      USE dimradmars_mod, only: albedo,naerkind
42      USE comsaison_h, only: dist_sol,mu0,fract
43      USE surfdat_h, only: emis,co2ice,hmons,summit
44      USE callradite_mod, only: callradite
45
46      IMPLICIT NONE
47
48!--------------------------------------------------------
49! Input Variables
50!--------------------------------------------------------
51
52      INTEGER, INTENT(IN) :: ngrid ! number of horizontal grid points
53      INTEGER, INTENT(IN) :: nlayer ! number of vertical grid points
54      INTEGER, INTENT(IN) :: nq ! number of tracer species
55      REAL, INTENT(IN) :: ptime
56      REAL, INTENT(IN) :: ptimestep
57
58      REAL, INTENT(IN) :: pq(ngrid,nlayer,nq) ! advected field nq
59      REAL, INTENT(IN) :: pdq(ngrid,nlayer,nq)! tendancy field pq
60      REAL, INTENT(IN) :: pt(ngrid,nlayer)    ! temperature at mid-layer (K)
61      REAL, INTENT(IN) :: pdt(ngrid,nlayer)   ! tendancy temperature at mid-layer (K)
62
63      REAL, INTENT(IN) :: pplay(ngrid,nlayer)     ! pressure at middle of the layers
64      REAL, INTENT(IN) :: pplev(ngrid,nlayer+1)   ! pressure at intermediate levels
65      REAL, INTENT(IN) :: pzlay(ngrid,nlayer)     ! altitude at the middle of the layers
66      REAL, INTENT(IN) :: pzlev(ngrid,nlayer+1)   ! altitude at layer boundaries
67
68      REAL, INTENT(IN) :: pdtsw(ngrid,nlayer)     ! (K/s) env
69      REAL, INTENT(IN) :: pdtlw(ngrid,nlayer)     ! (K/s) env
70
71!     input for second radiative transfer
72      INTEGER, INTENT(INOUT) :: icount
73      REAL, INTENT(IN) :: zday
74      REAL, INTENT(IN) :: zls
75      REAL, INTENT(IN) :: tsurf(ngrid)
76      INTEGER, INTENT(IN) :: igout
77      REAL, INTENT(INOUT) :: tauscaling(ngrid)
78!     input sub-grid scale rocket dust storm
79      LOGICAL, INTENT(IN) :: clearatm
80      REAL, INTENT(IN) :: totstormfract(ngrid)
81!     input sub-grid scale water ice clouds
82      LOGICAL, INTENT(IN) :: clearsky
83      REAL, INTENT(IN) :: totcloudfrac(ngrid)
84!     input sub-grid scale mountain
85      LOGICAL, INTENT(IN) :: nohmons
86      REAL, INTENT(IN) :: hsummit(ngrid)
87
88!--------------------------------------------------------
89! Output Variables
90!--------------------------------------------------------
91
92      REAL, INTENT(OUT) :: pdqtop(ngrid,nlayer,nq)  ! tendancy field for dust
93      REAL, INTENT(OUT) :: wfin(ngrid,nlayer+1)     ! final vertical wind velocity: combination of both wup and wrad
94                                                    ! wfin < 0 means upward wind (in pressure levels/s)
95
96!     output for second radiative transfer
97      REAL, INTENT(OUT) :: aerosol(ngrid,nlayer,naerkind)
98      REAL, INTENT(INOUT) :: dsodust(ngrid,nlayer)
99      REAL, INTENT(INOUT) :: dsords(ngrid,nlayer)
100      REAL, INTENT(INOUT) :: dsotop(ngrid,nlayer)
101      REAL, INTENT(OUT) :: tauref(ngrid)
102
103!--------------------------------------------------------
104! Local variables
105!--------------------------------------------------------
106      LOGICAL,SAVE :: firstcall=.true.
107      INTEGER l,ig,tsub,iq,ll
108      REAL zq0(ngrid,nlayer,nq)     ! initial tracers
109      REAL zq(ngrid,nlayer,nq)      ! updated tracers
110      REAL,PARAMETER:: mu0lim=0.001 ! solar zenith angle limit to determine the daytime
111
112!     Variables for radiative transfer
113      REAL  fluxsurf_lw1(ngrid)
114      REAL  fluxsurf_sw1(ngrid,2)
115      REAL  fluxtop_lw1(ngrid)
116      REAL  fluxtop_sw1(ngrid,2)
117      REAL  tau(ngrid,naerkind)
118      REAL  taucloudtes(ngrid)
119      REAL  rdust(ngrid,nlayer)
120      REAL  rstormdust(ngrid,nlayer)
121      REAL  rtopdust(ngrid,nlayer)
122      REAL  rice(ngrid,nlayer)
123      REAL  nuice(ngrid,nlayer)
124
125!     Temperature profile
126      REAL zt(ngrid,nlayer)                                  ! actual temperature at mid-layer (K)
127      REAL ztlev(ngrid,nlayer)                               ! temperature at intermediate levels l+1/2 without last level
128      REAL zdtlw1(ngrid,nlayer)                              ! (K/s) topdust
129      REAL zdtsw1(ngrid,nlayer)                              ! (K/s) topdust
130      REAL zdtlw1_lev(ngrid,nlayer),zdtsw1_lev(ngrid,nlayer) ! rad. heating rate at intermediate levels l+1/2
131      REAL zdtlw_lev(ngrid,nlayer),zdtsw_lev(ngrid,nlayer)   ! rad. heating rate at intermediate levels l+1/2
132      REAL zdtvert(ngrid,nlayer)                             ! dT/dz , lapse rate
133      REAL deltahr(ngrid,nlayer+1)                           ! extra heating between the concentrated and non-concentrated dust
134
135      REAL newzt(ngrid,nlayer)    ! recalculated temperature with extra radiative heating
136      REAL t_top(ngrid,nlayer)    ! estimated temperature above the mountain
137      REAL t_top_lev(ngrid,nlayer)! estimated temperature above the mountain at the levels
138      REAL dt_top(ngrid)          ! temperature difference between the summit and the environment
139      INTEGER lmons(ngrid)        ! layer of the mountain's slope
140      INTEGER lsummit(ngrid)      ! layer of the mountain's summit
141      REAL t_env(ngrid,nlayer)    ! recalculated temperature of the environment air "next" to the mountain
142
143!     Vertical velocity profile
144      REAL wrad(ngrid,nlayer+1)  ! vertical wind velocity resulting from radiative heating
145      REAL wup(ngrid,nlayer+1)   ! vertical wind velocity calculated above the sub-grid scale mountain
146      REAL wup2(ngrid,nlayer+1)  ! square of the vertical velocity calculated above the sub-grid scale mountain
147      REAL w0(ngrid)             ! vertical wind velocity convergence at the top of the sub-grid scale mountain
148      INTEGER lwmax(ngrid)       ! level of the maximum vertical velocity
149
150      REAL,PARAMETER:: wmax=10.  ! maximum vertical velocity resulting from radiative heating (m/s)
151      REAL,PARAMETER:: secu=3.   ! coefficient on wfin to avoid dust crossing many layers during subtimestep
152      REAL,PARAMETER:: k0=0.25   !max 10m/s: k0=1.      !max 3m/s: k0=0.25
153      REAL,PARAMETER:: k1=5.e-4  !max 10m/s: k1=5.e-4   !max 3m/s: k1=5.e-4
154      REAL,PARAMETER:: k2=5.e-3  !max 10m/s: k2=30.e-3  !max 3m/s: k2=5.e-3
155
156!     Entrainment
157      REAL entr(ngrid)                                             ! entrainment flux
158      REAL rho(ngrid,nlayer),rhobarz(ngrid,nlayer+1)                 ! density, density at the levels
159      REAL masse(ngrid,nlayer)                                     ! air mass
160      REAL masse_pbl(ngrid)                                        ! total air mass within the PBL
161      REAL masse_pbl_dust_mass(ngrid),masse_pbl_dust_number(ngrid) ! total air mass + dust mass/number within the PBL (kg/m2)
162      REAL dqm_mass_pbl(ngrid),dqm_number_pbl(ngrid)               ! total mass of the entrained dust mass/number from the PBL (kg/m2)
163
164!     Van Leer
165      REAL zq_dust_mass(ngrid,nlayer),zq_dust_number(ngrid,nlayer)       ! mixing ratio background dust (kg/kg)
166      REAL zq_topdust_mass(ngrid,nlayer),zq_topdust_number(ngrid,nlayer) ! mixing ratio topdust (kg/kg)
167      REAL mr_dust_mass(ngrid,nlayer),mr_dust_number(ngrid,nlayer)       ! mixing ratio background dust (kg/kg)
168      REAL mr_topdust_mass(ngrid,nlayer),mr_topdust_number(ngrid,nlayer) ! mixing ratio background dust + concentrated topdust (kg/kg)
169
170      REAL w(ngrid,nlayer)          ! vertical flux above the sub-grid scale mountain
171      REAL wqmass(ngrid,nlayer+1)   ! flux mass
172      REAL wqnumber(ngrid,nlayer+1) ! flux number
173      REAL qbar_mass_pbl(ngrid)     ! total dust mass mixing ratio within the PBL for Van Leer
174      REAL qbar_number_pbl(ngrid)   ! total dust number mixing ratio within the PBL for Van Leer
175      REAL masse_col(nlayer)        ! masse of the atmosphere in one column
176
177      REAL dqvl_dust_mass(ngrid,nlayer)       ! tendancy pdq dust mass after vertical transport
178      REAL dqvl_dust_number(ngrid,nlayer)     ! tendancy pdq dust number after vertical transport
179      REAL dqvl_topdust_mass(ngrid,nlayer)    ! tendancy pdq topdust mass after vertical transport
180      REAL dqvl_topdust_number(ngrid,nlayer)  ! tendancy pdq topdust number after vertical transport
181
182      INTEGER nsubtimestep(ngrid)    ! number of subtimestep when calling Van Leer scheme
183      REAL subtimestep(ngrid)        ! ptimestep/nsubtimestep
184      REAL dtmax                     ! considered minimum time interval needed for the dust to cross one vertical layer
185
186!     Detrainment     
187      REAL coefdetrain(ngrid,nlayer)          ! coefficient for detrainment : % of stormdust detrained
188      REAL dqdet_topdust_mass(ngrid,nlayer)   ! tendancy pdq topdust mass after detrainment only
189      REAL dqdet_topdust_number(ngrid,nlayer) ! tendancy pdq topdust number after detrainment only
190
191!     Diagnostics
192      REAL    zlaywmax(ngrid)
193      REAL    detrain_rate(ngrid,nlayer)
194
195      ! **********************************************************************
196      ! **********************************************************************
197      ! Parametrization of the entrainment by slope wind above the sub-grid
198      ! scale topography
199      ! **********************************************************************
200      ! **********************************************************************     
201      !!    1. Radiative transfer in topdust
202      !!    2. Compute vertical velocity for topdust
203      !!    3. Vertical transport
204      !!    4. Detrainment
205      ! **********************************************************************
206      ! **********************************************************************
207     
208      ! **********************************************************************
209      ! 0. Initializations
210      ! **********************************************************************
211      aerosol(:,:,:)=0.
212      pdqtop(:,:,:) = 0.
213      dqvl_topdust_mass(:,:)=0.
214      dqvl_topdust_number(:,:)=0.
215      dqvl_dust_mass(:,:)=0.
216      dqvl_dust_number(:,:)=0.
217      dqdet_topdust_mass(:,:)=0.
218      dqdet_topdust_number(:,:)=0.
219      wup(:,:)=0.
220      wup2(:,:)=0.
221      wrad(:,:)=0.
222      w0(:)=0.
223      wfin(:,:)=0.     
224      w(:,:)=0.
225      zq(:,:,:) = 0.
226      zq0(:,:,:) = 0.
227      entr(:) = 0.
228      mr_dust_mass(:,:) = 0.
229      mr_dust_number(:,:) = 0.
230      mr_topdust_mass(:,:) = 0.
231      mr_topdust_number(:,:) = 0.
232      t_top(:,:) = 0.
233      dt_top(:) = 0.
234      newzt(:,:) = 0.
235      lmons(:) = 1
236      lsummit(:) =1
237      lwmax(:) = 1
238      deltahr(:,:) = 0.
239      zdtvert(:,:) = 0.
240      wqmass(:,:) = 0.
241      wqnumber(:,:) = 0.       
242      coefdetrain(:,:) = 1.
243      dqm_mass_pbl(:)=0.
244      dqm_number_pbl(:)=0.
245      nsubtimestep(:)=1
246      masse_pbl(:)=0.
247      detrain_rate(:,:) = 0.
248
249      !! Update the initial temperature
250      zt(1:ngrid,1:nlayer)=pt(1:ngrid,1:nlayer)+pdt(1:ngrid,1:nlayer)*ptimestep
251      newzt(1:ngrid,1:nlayer)=zt(1:ngrid,1:nlayer)
252      t_env(1:ngrid,1:nlayer)=zt(1:ngrid,1:nlayer)
253      t_top(1:ngrid,1:nlayer)=zt(1:ngrid,1:nlayer)
254
255      !! Update the initial mixing ratios
256      zq0(1:ngrid,1:nlayer,1:nq)=pq(1:ngrid,1:nlayer,1:nq)+pdq(1:ngrid,1:nlayer,1:nq)*ptimestep ! update of the background dust after rocket dust storm scheme
257      zq(1:ngrid,1:nlayer,1:nq)=zq0(1:ngrid,1:nlayer,1:nq)
258      zq_dust_mass(1:ngrid,1:nlayer)=zq(1:ngrid,1:nlayer,igcm_dust_mass)
259      zq_dust_number(1:ngrid,1:nlayer)=zq(1:ngrid,1:nlayer,igcm_dust_number)
260      zq_topdust_mass(1:ngrid,1:nlayer)=zq(1:ngrid,1:nlayer,igcm_topdust_mass)
261      zq_topdust_number(1:ngrid,1:nlayer)=zq(1:ngrid,1:nlayer,igcm_topdust_number)
262         
263! ----------------------------------------------------------------------
264! 1. Radiative heating
265! ----------------------------------------------------------------------
266
267       ! *********************************************************************
268       ! 1.1. Call the second radiative transfer for topdust, obtain the extra heating
269       ! *********************************************************************
270        CALL callradite(icount,ngrid,nlayer,nq,zday,zls,zq,albedo,     &
271                 emis,mu0,pplev,pplay,pt,tsurf,fract,dist_sol,igout,   &
272                 zdtlw1,zdtsw1,fluxsurf_lw1,fluxsurf_sw1,fluxtop_lw1,  &
273                 fluxtop_sw1,tauref,tau,aerosol,dsodust,tauscaling,    &
274                 taucloudtes,rdust,rice,nuice,co2ice,rstormdust,rtopdust, &
275                 totstormfract,clearatm,dsords,dsotop,alpha_hmons,nohmons,&
276                 clearsky,totcloudfrac)
277       ! **********************************************************************
278       ! 1.2. Compute radiative vertical velocity for entrained topdust
279       ! **********************************************************************
280        DO ig=1,ngrid
281          IF ( (mu0(ig) .gt. mu0lim) .and. (alpha_hmons(ig) .gt. 0.) ) THEN
282            !! **********************************************************************
283            !! Temperature profile above the mountain and in the close environment
284            call t_topmons(nlayer,summit(ig),hsummit(ig),hmons(ig),zt(ig,:),pzlay(ig,:), &
285                               t_top(ig,:),dt_top(ig),t_env(ig,:),lsummit(ig),lmons(ig))
286            !! **********************************************************************
287            !! Calculation of the extra heating : computing heating rates
288            !! gradient at boundaries of each layer
289            zdtlw1_lev(ig,1)=0.
290            zdtsw1_lev(ig,1)=0.
291            zdtlw_lev(ig,1)=0.
292            zdtsw_lev(ig,1)=0.
293            ztlev(ig,1)=zt(ig,1)
294            t_top_lev(ig,1)=tsurf(ig)
295           
296            DO l=1,nlayer-1
297              !! Calculation for the background dust AND the concentrated topdust
298              zdtlw1_lev(ig,l+1)=(zdtlw1(ig,l)*(pzlay(ig,l+1)-pzlev(ig,l+1))+ &
299                           zdtlw1(ig,l+1)*(pzlev(ig,l+1)-pzlay(ig,l)))  /  &
300                              (pzlay(ig,l+1)-pzlay(ig,l))
301           
302              zdtsw1_lev(ig,l+1)=(zdtsw1(ig,l)*(pzlay(ig,l+1)-pzlev(ig,l+1))+ &
303                           zdtsw1(ig,l+1)*(pzlev(ig,l+1)-pzlay(ig,l)))  /  &
304                              (pzlay(ig,l+1)-pzlay(ig,l))
305              !! Calculation for the background dust only
306              zdtlw_lev(ig,l+1)=(pdtlw(ig,l)*(pzlay(ig,l+1)-pzlev(ig,l+1))+   &
307                           pdtlw(ig,l+1)*(pzlev(ig,l+1)-pzlay(ig,l)))  /   &
308                              (pzlay(ig,l+1)-pzlay(ig,l))
309           
310              zdtsw_lev(ig,l+1)=(pdtsw(ig,l)*(pzlay(ig,l+1)-pzlev(ig,l+1))+   &
311                           pdtsw(ig,l+1)*(pzlev(ig,l+1)-pzlay(ig,l)))  /   &
312                              (pzlay(ig,l+1)-pzlay(ig,l))
313              !! Temperature at the levels
314              ztlev(ig,l+1)=(zt(ig,l)*(pzlay(ig,l+1)-pzlev(ig,l+1))+          &
315                           zt(ig,l+1)*(pzlev(ig,l+1)-pzlay(ig,l)))  /      &
316                              (pzlay(ig,l+1)-pzlay(ig,l))
317              !! Temperature above the mountain at the levels
318              t_top_lev(ig,l+1)=(t_top(ig,l)*(pzlay(ig,l+1)-pzlev(ig,l+1))+          &
319                           t_top(ig,l+1)*(pzlev(ig,l+1)-pzlay(ig,l)))  /      &
320                              (pzlay(ig,l+1)-pzlay(ig,l))
321            ENDDO ! DO l=1,nlayer-1
322            !! **********************************************************************
323            !! Vertical velocity profile for extra heating balanced by adiabatic cooling
324            !! Gradient at boundaries of each layer, start from surface
325            zdtvert(ig,1)=0.
326            DO l=1,nlayer-1
327              zdtvert(ig,l+1)=(t_top_lev(ig,l+1)-t_top_lev(ig,l))/(pzlay(ig,l+1)-pzlay(ig,l))
328            ENDDO
329            !! Extra heating balanced by adiabatic cooling
330            DO l=1,nlayer
331              deltahr(ig,l)=(zdtlw1_lev(ig,l)+zdtsw1_lev(ig,l))  &
332                                          -(zdtlw_lev(ig,l)+zdtsw_lev(ig,l))
333              wrad(ig,l)=-deltahr(ig,l)/(g/cpp+   &
334                                         max(zdtvert(ig,l),-0.99*g/cpp))
335              !! Limit of the vertical wind velocity in case of lapse rate close to adiabatic
336              wrad(ig,l)=max(wrad(ig,l),-wmax)
337              wrad(ig,l)=min(wrad(ig,l),wmax)
338              !! ATTENTION !! to simplify here, no downward velocity calculated
339              if (wrad(ig,l).gt.0.) then
340                wrad(ig,l)=0. 
341              endif
342            ENDDO
343          ENDIF ! IF ((mu0(ig) .gt. mu0lim) .and. alpha_hmons(ig) .gt. 0.)
344        ENDDO ! DO ig=1,ngrid
345
346! ----------------------------------------------------------------------
347! 2. Vertical velocity profile
348! ----------------------------------------------------------------------
349
350       ! **********************************************************************
351       ! 2.1 Compute total vertical velocity for entrained topdust: buoyancy + radiative
352       ! **********************************************************************
353       DO ig=1,ngrid
354         IF ( (mu0(ig) .gt. mu0lim) .and. (alpha_hmons(ig) .gt. 0.) ) THEN
355           !! Positive buoyancy: negative vertical velocity entrains UP
356           IF (dt_top(ig) .gt. 0.) THEN
357             !! Convergence of vertical winds at the top of the mountain
358             w0(ig)=-k0*g*sqrt((dt_top(ig)/t_env(ig,lsummit(ig))))
359             !! Case the vertical velocity at the top of the mountain > wrad
360             IF (w0(ig).lt.wrad(ig,lsummit(ig)+1)) then
361               wup(ig,lsummit(ig)+1)=w0(ig)
362               wfin(ig,lsummit(ig)+1)=w0(ig)
363               newzt(ig,lsummit(ig))= t_top(ig,lsummit(ig))
364               !! Temperature profile from the top of the summit to the top of the atmosphere
365               DO l=lsummit(ig)+1,nlayer-1
366                 !ATTENTION: test without wrad, T is only decreasing because of the adiabatic gradient
367                 !newzt(ig,l)=newzt(ig,l-1)-(g/cpp)*        &
368                 !               (pzlay(ig,l)-pzlay(ig,l-1))
369                 !! Case of superadiabatic layer
370                 IF (zdtvert(ig,l) .lt. -g/cpp) then
371                    newzt(ig,l)=t_top(ig,l)
372                 !! New temperature due to radiative heating balanced by adiabatic cooling
373                 ELSE
374                    newzt(ig,l)=newzt(ig,l-1)+ &
375                               ( (deltahr(ig,l)/(-wfin(ig,l)))-g/cpp )*  &
376                               ( pzlay(ig,l)-pzlay(ig,l-1) )
377                 ENDIF ! (zdtvert(ig,l) .lt. -g/cpp)
378                 !! Vertical velocity profile due to the presence of the mountain with the new temperature
379                 !! Square of the vertical velocity profile with the new temperature
380                 wup2(ig,l+1)=(1.-2*k1*(pzlev(ig,l+1)-pzlev(ig,l)))*&
381                              (wup(ig,l)**2)+2.*k2*g*(pzlev(ig,l+1) &
382                             -pzlev(ig,l))*((newzt(ig,l)       &
383                             -t_env(ig,l))/t_env(ig,l))
384                 !! Vertical velocity profile if W2 > 0
385                 IF (wup2(ig,l+1) .gt. 0.) THEN
386                   wup(ig,l+1)=-sqrt(wup2(ig,l+1))
387                   wfin(ig,l+1)=wup(ig,l+1)
388                   IF (wup(ig,l+1) .gt. wrad(ig,l+1)) then
389                     DO ll=l,nlayer-1
390                       newzt(ig,ll)=zt(ig,ll)
391                       wfin(ig,ll+1)=wrad(ig,ll+1)
392                     ENDDO
393                     EXIT
394                   ENDIF
395                 !! Vertical velocity profile if W2 < 0
396                 ELSE
397                   DO ll=l,nlayer-1
398                     newzt(ig,ll)=zt(ig,ll)
399                     wfin(ig,ll+1)=wrad(ig,ll+1)
400                   ENDDO
401                     EXIT
402                 ENDIF ! IF (wup2(ig,l+1) .gt. 0.)   
403               ENDDO ! DO l=lsummit(ig)+1,nlayer-1
404             !! Case the vertical velocity at the top of the mountain < wrad
405             ELSE
406               DO ll=lsummit(ig),nlayer-1
407                  newzt(ig,ll)=zt(ig,ll)
408                  wfin(ig,ll+1)=wrad(ig,ll+1)
409               ENDDO
410             ENDIF !(w0(ig).lt.wrad(ig,lsummit(ig)+1))
411           !! Positive buoyancy: positive vertical velocity entrains DOWN
412           ELSE
413             DO l=lsummit(ig)+1,nlayer
414                wfin(ig,l)=wrad(ig,l)
415             ENDDO
416           ENDIF ! IF (dt_top(ig) .gt. 0.)           
417       ! **********************************************************************
418       ! 2.2 Find the level lwmax of the maximum vertical velocity caused by the
419       ! mountain presence (wup), where to entrain the dust from the PBL
420       ! **********************************************************************
421           IF (minloc(wup(ig,:),dim=1).lt.nlayer) THEN
422             lwmax(ig)=minloc(wup(ig,:),dim=1)
423           ENDIF
424           zlaywmax(ig)=pzlay(ig,lwmax(ig)) ! wup(lwmax) acts on level lwmax, going to layer lwmax
425       ! **********************************************************************
426       ! 2.3 Compute the subtimestep to conserve the mass in the Van Leer transport
427       ! **********************************************************************
428           !! Calculation of the subtimesteps
429           dtmax=ptimestep
430           DO l=lwmax(ig),nlayer
431             IF (wfin(ig,l).lt.0.) THEN
432               dtmax=min(dtmax,(pzlev(ig,l)-pzlev(ig,l-1))/  &
433                                 (secu*abs(wfin(ig,l))))
434             ELSE IF (wfin(ig,l).gt.0.) then
435               dtmax=min(dtmax,(pzlev(ig,l+1)-pzlev(ig,l))/  &
436                                 (secu*abs(wfin(ig,l))))
437             ENDIF
438           ENDDO
439           nsubtimestep(ig)= int(ptimestep/dtmax)
440           subtimestep(ig)=ptimestep/float(nsubtimestep(ig))
441           !! Mass flux generated by wup in kg/m2
442           DO l=1,nlayer!+1
443             w(ig,l)=wfin(ig,l)*pplev(ig,l)/(r*ztlev(ig,l)) &
444                      *subtimestep(ig)
445           ENDDO ! l=1,nlayer
446                       
447          ENDIF ! ((mu0(ig) .gt. mu0lim)...
448        ENDDO ! DO ig=1,ngrid
449
450! ----------------------------------------------------------------------
451! 3. Dust entrainment by slope winds above the mountain
452! ----------------------------------------------------------------------
453        !! rho on the layer
454        rho(:,:)=pplay(:,:)/(r*pt(:,:))
455        !! rhobarz(l) = rho(l+1/2): rho on the levels
456        rhobarz(:,1)=rho(:,1)
457        DO l=2,nlayer
458          rhobarz(:,l)=pplev(:,l)/(r*0.5*(pt(:,l)+pt(:,l-1)))
459        ENDDO
460        rhobarz(:,nlayer+1)=0. !top of the model
461        !! Mass computation
462        DO l=1,nlayer
463          masse(:,l)=(pplev(:,l)-pplev(:,l+1))/g
464        ENDDO
465
466        DO ig=1,ngrid
467          IF ( (mu0(ig) .gt. mu0lim) .and. (alpha_hmons(ig) .gt. 0.) ) THEN
468            !! Total air mass within the PBL before entrainment (=> by PBL we mean between the surface and the layer where the vertical wind is maximum)
469            masse_pbl(ig)=0.
470            DO l=1,lwmax(ig)-1
471              masse_pbl(ig)=masse_pbl(ig)+(pplev(ig,l)-pplev(ig,l+1))/g
472            ENDDO
473          ENDIF ! ((mu0(ig) .gt. mu0lim)...
474        ENDDO ! ig=1,ngrid
475       ! **********************************************************************
476       ! 3.1. Entrainment
477       ! **********************************************************************
478        DO ig=1,ngrid
479          IF ( (mu0(ig) .gt. mu0lim) .and. (alpha_hmons(ig) .gt. 0.) .and. (masse_pbl(ig) .gt. 0.) ) THEN
480            !! Transport of background dust + concentrated topdust above lwmax
481            DO l=lwmax(ig),nlayer
482              mr_dust_mass(ig,l) = zq_dust_mass(ig,l)
483              mr_dust_number(ig,l) = zq_dust_number(ig,l)
484              mr_topdust_mass(ig,l) = zq_dust_mass(ig,l) &
485                                     +zq_topdust_mass(ig,l)/alpha_hmons(ig)
486              mr_topdust_number(ig,l) = zq_dust_number(ig,l) &
487                                     +zq_topdust_number(ig,l)/alpha_hmons(ig)
488            ENDDO ! l=lwmax(ig),nlayer
489            !! Entrainment flux from the PBL
490            entr(ig) = alpha_hmons(ig)*wup(ig,lwmax(ig))*rhobarz(ig,lwmax(ig))/masse_pbl(ig)
491            !! If entrains more than available masse_pbl (abs(entr) x mass_pbl x ptimestep > masse_pbl)
492            if (abs(entr(ig)) .gt. 1./ptimestep) then
493              entr(ig) = -(1./ptimestep)
494            end if
495       ! **********************************************************************
496       ! 3.2. Vertical transport: Van Leer
497       ! **********************************************************************
498            !! Loop over the subtimesteps
499            DO tsub=1,nsubtimestep(ig)
500              !! qbar_pbl: total mixing ratio within the PBL
501              masse_pbl_dust_mass(ig)=0.
502              masse_pbl_dust_number(ig)=0.
503              DO l=1,lwmax(ig)-1
504                masse_pbl_dust_mass(ig)=masse_pbl_dust_mass(ig)+zq_dust_mass(ig,l)*(pplev(ig,l)-pplev(ig,l+1))/g
505                masse_pbl_dust_number(ig)=masse_pbl_dust_number(ig)+zq_dust_number(ig,l)*(pplev(ig,l)-pplev(ig,l+1))/g
506              ENDDO
507              qbar_mass_pbl(ig)=masse_pbl_dust_mass(ig)/masse_pbl(ig)
508              qbar_number_pbl(ig)=masse_pbl_dust_mass(ig)/masse_pbl(ig)
509              !! integration of the equation dq/dt = -(entr)q, with entr constant --> w0 < 0 when up so the equation is dq/dt = (entr)q
510              dqm_mass_pbl(:)=0.
511              dqm_number_pbl(:)=0.
512              DO l=1,lwmax(ig)-1 ! this time we take the dust from the surface up to the level, where the velocity is maximum
513                !! Total dust entrained from the PBL:
514                dqm_mass_pbl(ig)=dqm_mass_pbl(ig)+(1.-exp(entr(ig)*subtimestep(ig)))*zq_dust_mass(ig,l)*(pplev(ig,l)-pplev(ig,l+1))/g
515                dqm_number_pbl(ig)=dqm_number_pbl(ig)+(1.-exp(entr(ig)*subtimestep(ig)))*zq_dust_number(ig,l)*(pplev(ig,l)-pplev(ig,l+1))/g
516                !! Background dust after entrainment (explicit: qdust(ig,l)=qdust(ig,l)-entr(ig)*qdust(ig,l)*ptimestep)
517                zq_dust_mass(ig,l)=zq_dust_mass(ig,l)-(1.-exp(entr(ig)*subtimestep(ig)))*zq_dust_mass(ig,l)
518                zq_dust_number(ig,l)=zq_dust_number(ig,l)-(1.-exp(entr(ig)*subtimestep(ig)))*zq_dust_number(ig,l)
519              ENDDO
520              !! Van Leer scheme (from lwmax to nlayer)
521              wqmass(ig,:)=0.
522              wqnumber(ig,:)=0.
523              CALL van_leer(nlayer,mr_topdust_mass(ig,:),2.,lwmax(ig),  &
524                  masse(ig,:),w(ig,:),wqmass(ig,:),masse_pbl(ig),dqm_mass_pbl(ig),alpha_hmons(ig),qbar_mass_pbl(ig))
525              CALL van_leer(nlayer,mr_topdust_number(ig,:),2.,lwmax(ig),  &
526                  masse(ig,:),w(ig,:),wqnumber(ig,:),masse_pbl(ig),dqm_number_pbl(ig),alpha_hmons(ig),qbar_number_pbl(ig))
527            ENDDO !tsub=...
528       ! **********************************************************************
529       ! 3.3. Re-calculation of the mixing ratios after vertical transport
530       ! **********************************************************************
531            !! Redistribution from lwmax to nlayer
532            DO l=lwmax(ig),nlayer
533              !! General and "healthy" case
534              IF (mr_topdust_mass(ig,l).ge.mr_dust_mass(ig,l)) THEN
535                zq_dust_mass(ig,l) = mr_dust_mass(ig,l)
536                zq_dust_number(ig,l) = mr_dust_number(ig,l)
537                zq_topdust_mass(ig,l) = alpha_hmons(ig)*(mr_topdust_mass(ig,l)-mr_dust_mass(ig,l))
538                zq_topdust_number(ig,l) = alpha_hmons(ig)*(mr_topdust_number(ig,l)-mr_dust_number(ig,l))
539              !! Particular case: there is less than initial dust mixing ratio after the vertical transport
540              ELSE
541                zq_dust_mass(ig,l) = (1.-alpha_hmons(ig))*mr_dust_mass(ig,l)+alpha_hmons(ig)*mr_topdust_mass(ig,l)
542                zq_dust_number(ig,l) = (1.-alpha_hmons(ig))*mr_dust_number(ig,l)+alpha_hmons(ig)*mr_topdust_number(ig,l)
543                zq_topdust_mass(ig,l) = 0.
544                zq_topdust_number(ig,l) = 0.
545              ENDIF
546            ENDDO ! DO l=lwmax(ig),nlayer
547       ! **********************************************************************
548       ! 3.4. Calculation of the tendencies of the vertical transport from the surface up to the top of the atmosphere
549       ! **********************************************************************
550            DO l=1,nlayer
551              dqvl_topdust_mass(ig,l) = (zq_topdust_mass(ig,l)- &
552                                    zq0(ig,l,igcm_topdust_mass)) /ptimestep
553              dqvl_topdust_number(ig,l) = (zq_topdust_number(ig,l)- &
554                                      zq0(ig,l,igcm_topdust_number)) /ptimestep
555              dqvl_dust_mass(ig,l) = (zq_dust_mass(ig,l)-zq0(ig,l,igcm_dust_mass)) /ptimestep
556              dqvl_dust_number(ig,l) = (zq_dust_number(ig,l)-zq0(ig,l,igcm_dust_number)) /ptimestep
557           ENDDO
558
559          ENDIF ! ((mu0(ig) .gt. mu0lim)...
560        ENDDO ! ig=1,ngrid
561
562! ----------------------------------------------------------------------
563! 4. Detrainment: topdust is converted to background dust
564! ----------------------------------------------------------------------
565
566        !! **********************************************************************
567        !! 4.1 Compute the coefficient of detrainment
568        !! **********************************************************************
569        DO ig=1,ngrid
570          !DO l=lwmax(ig),nlayer-1
571          DO l=1,nlayer!-1
572            !! Detrainment during the day
573            IF ( (mu0(ig) .gt. mu0lim) .and. (zq_topdust_mass(ig,l) .gt. zq_dust_mass(ig,l)*0.01)) THEN
574               coefdetrain(ig,l)=1.*( rhobarz(ig,l+1)*abs(wfin(ig,l+1)) - rhobarz(ig,l)*abs(wfin(ig,l)) ) / masse(ig,l)
575               !! Detrainment when abs(w(l)) > abs(w(l+1)), i.e. coefdetrain < 0
576               if ( coefdetrain(ig,l).lt.0.) then
577                dqdet_topdust_mass(ig,l)=-(1.-exp(coefdetrain(ig,l)*ptimestep))*zq_topdust_mass(ig,l)/ptimestep
578                dqdet_topdust_number(ig,l)=-(1.-exp(coefdetrain(ig,l)*ptimestep))*zq_topdust_number(ig,l)/ptimestep
579                ! for diagnostics
580                detrain_rate(ig,l)=(1.-exp(coefdetrain(ig,l)*ptimestep))
581                !! One cannot detrain more than available topdust
582                if ( (abs(dqdet_topdust_mass(ig,l)).gt.zq_topdust_mass(ig,l)/ptimestep) .or. (abs(dqdet_topdust_number(ig,l)).gt.zq_topdust_number(ig,l)/ptimestep) ) then
583                  dqdet_topdust_mass(ig,l)=-zq_topdust_mass(ig,l)/ptimestep
584                  dqdet_topdust_number(ig,l)=-zq_topdust_number(ig,l)/ptimestep
585                  detrain_rate(ig,l)=1.
586                endif
587               !else!entrainment
588               !  dqdet_topdust_mass(ig,l)=-(1.-exp(coefdetrain(ig,l)*ptimestep))*zq_dust_mass(ig,l)/ptimestep
589               !  dqdet_topdust_number(ig,l)=-(1.-exp(coefdetrain(ig,l)*ptimestep))*zq_dust_number(ig,l)/ptimestep
590               endif
591            !! Full detrainment during the night imposed
592            ELSE
593               dqdet_topdust_mass(ig,l)=-zq_topdust_mass(ig,l)/ptimestep
594               dqdet_topdust_number(ig,l)=-zq_topdust_number(ig,l)/ptimestep
595               detrain_rate(ig,l)=1.
596            ENDIF
597          ENDDO ! DO l=1,nlayer
598        ENDDO ! DO ig=1,ngrid
599
600! ----------------------------------------------------------------------
601! 5. Final tendencies: vertical transport + detrainment
602! ----------------------------------------------------------------------     
603
604        DO ig=1,ngrid
605          DO l=1,nlayer     
606          pdqtop(ig,l,igcm_topdust_mass)=dqdet_topdust_mass(ig,l) &
607                                                 +dqvl_topdust_mass(ig,l)
608          pdqtop(ig,l,igcm_topdust_number)=dqdet_topdust_number(ig,l) &
609                                                 +dqvl_topdust_number(ig,l)
610          pdqtop(ig,l,igcm_dust_mass)= -dqdet_topdust_mass(ig,l) &
611                                       +dqvl_dust_mass(ig,l)
612          pdqtop(ig,l,igcm_dust_number)= -dqdet_topdust_number(ig,l) &
613                                       +dqvl_dust_number(ig,l)
614          ENDDO ! DO l=1,nlayer
615        ENDDO ! DO ig=1,ngrid
616
617
618! **********************************************************************
619! WRITEDIAGFI
620! **********************************************************************
621        IF (ngrid.eq.1) THEN
622               CALL WRITEDIAGFI(ngrid,'wup_top', &
623                'wup_top','',1,wup(:,:))
624               CALL WRITEDIAGFI(ngrid,'wrad_top', &
625                'wrad_top','',1,wrad(:,:))
626               CALL WRITEDIAGFI(ngrid,'wfin_top', &
627                'wfin_top','',1,wfin(:,:))
628               CALL WRITEDIAGFI(ngrid,'wlwmax_top', &
629                'wlwmax_top','',0,wup(:,lwmax(1)))
630               CALL WRITEDIAGFI(ngrid,'w0_top', &
631                'w0_top','',0,wup(:,lsummit(1)+1))
632               CALL WRITEDIAGFI(ngrid,'alpha_hmons', &
633                'alpha_hmons','',0,alpha_hmons)
634               CALL WRITEDIAGFI(ngrid,'zt_top', &
635                'zt_top','',1,t_top(:,:))
636               CALL WRITEDIAGFI(ngrid,'dt_top', &
637                'dt_top','',0,dt_top(:))
638               CALL WRITEDIAGFI(ngrid,'envtemp', &
639                'envtemp','',1,zt(:,:))
640               CALL WRITEDIAGFI(ngrid,'t_env', &
641                't_env','',1,t_env(:,:))
642               CALL WRITEDIAGFI(ngrid,'newzt_top', &
643                'newzt_top','',1,newzt(:,:))
644               CALL WRITEDIAGFI(ngrid,'deltahr_top', &
645                'deltahr_top','',1,deltahr(:,:))
646               CALL WRITEDIAGFI(ngrid,'rholev', &
647                'rholev','',1,rho(:,:))
648               CALL WRITEDIAGFI(ngrid,'rhobarz', &
649                'rhobarz','',1,rhobarz(:,:))
650               CALL WRITEDIAGFI(ngrid,'zlsummit', &
651                'zlsummit','',0,pzlay(:,lsummit(1)))
652               CALL WRITEDIAGFI(ngrid,'zlwmax', &
653                'zlwmax','',0,pzlay(:,lwmax(1)))
654               CALL WRITEDIAGFI(ngrid,'pzlev_top', &
655                'pzlev_top','',1,pzlev(:,:))
656               CALL WRITEDIAGFI(ngrid,'coefdetrain', &
657                'coefdetrain','',1,coefdetrain(:,:))
658               CALL WRITEDIAGFI(ngrid,'zdtvert', &
659                'zdtvert','',1,zdtvert(:,:))
660               CALL WRITEDIAGFI(ngrid,'entr', &
661                'entr','',0,entr(:))
662        ELSE
663               CALL WRITEDIAGFI(ngrid,'wup_top', &
664                'wup_top','',3,wup(:,:))
665               CALL WRITEDIAGFI(ngrid,'wup2_top', &
666                'wup2_top','',3,wup2(:,:))
667               CALL WRITEDIAGFI(ngrid,'wrad_top', &
668                'wrad_top','',3,wrad(:,:))
669               CALL WRITEDIAGFI(ngrid,'wfin_top', &
670                'wfin_top','',3,wfin(:,:))
671               CALL WRITEDIAGFI(ngrid,'alpha_hmons', &
672                'alpha_hmons','',2,alpha_hmons)
673               CALL WRITEDIAGFI(ngrid,'zt_top', &
674                'zt_top','',3,t_top(:,:))
675               CALL WRITEDIAGFI(ngrid,'ztemp', &
676                'envtemp','',3,zt(:,:))
677               CALL WRITEDIAGFI(ngrid,'zt_env', &
678                't_env','',3,t_env(:,:))
679               CALL WRITEDIAGFI(ngrid,'zdtvert_top', &
680                'zdtvert_top','',3,zdtvert(:,:))
681               CALL WRITEDIAGFI(ngrid,'newzt_top', &
682                'newzt_top','',3,newzt(:,:))
683               CALL WRITEDIAGFI(ngrid,'deltahr_top', &
684                'deltahr_top','',3,deltahr(:,:))
685               CALL WRITEDIAGFI(ngrid,'rhobarz', &
686                'rhobarz','',3,rhobarz(:,:))
687               CALL WRITEDIAGFI(ngrid,'zlwmax', &
688                'zlwmax','',2,zlaywmax(:))
689               CALL WRITEDIAGFI(ngrid,'coefdetrain', &
690                'coefdetrain','',3,coefdetrain(:,:))
691               CALL WRITEDIAGFI(ngrid,'detrain_rate', &
692                              'detrain_rate', &
693                              '',3,detrain_rate(:,:))
694
695        ENDIF
696       
697        END SUBROUTINE topmons
698
699!=======================================================================     
700
701! **********************************************************************
702! Subroutine to determine both temperature profiles above and near
703! a sub-grid scale mountain
704!***********************************************************************
705       SUBROUTINE t_topmons(nlayer,summit,hsummit,hmons,zt,zlay,t_top,dt_top,t_env,lsummit,lmons)
706
707       USE comcstfi_h, only: g,cpp
708
709       IMPLICIT NONE
710
711!--------------------------------------------------------
712! Input Variables
713!--------------------------------------------------------
714       integer,intent(in) :: nlayer
715       real,intent(in) :: summit          ! Height of the mountain summit
716       real,intent(in) :: hmons           ! Height of the mountain slope
717       real,intent(in) :: hsummit         ! Height of the mountain summit from the GCM surface
718       real,intent(in) :: zt(nlayer)      ! large scale temperature profile
719       real,intent(in) :: zlay(nlayer)    ! height at the middle of each layer
720!--------------------------------------------------------
721! Output Variables
722!--------------------------------------------------------
723       real,intent(out) :: t_top(nlayer) ! temperature  above the mountain
724       real,intent(out) :: dt_top        ! temperature difference between the slope and
725                                         ! the environment
726       real,intent(out) :: t_env(nlayer)   ! temperature of the environment next to the mountain
727       integer,intent(out) :: lsummit      ! layer reached by the summit height in the GCM
728       integer,intent(out) :: lmons        ! layer of the real temperature to be considered near to the mountain top
729
730!--------------------------------------------------------
731! Local Variables
732!--------------------------------------------------------
733       integer l,ll
734       real zlmons
735
736! **********************************************************************
737       t_env(:)=zt(:)
738       t_top(:)=zt(:)
739       dt_top=0.
740       lsummit=1
741       lmons=1
742
743       !! Layer where the dust is injected
744       do while (hsummit .ge. zlay(lsummit))
745           !! highest layer reached by the mountain (we don't interpolate and define zlay(lsummit) as the top of the mountain)
746           lsummit=lsummit+1
747       enddo
748       !! temperature above the mountain is set to the surface temperature
749       t_top(lsummit)=zt(1)
750       do l=lsummit,nlayer-1
751          !! temperature above the mountain follows the adiabatic
752          t_top(l+1)=t_top(l)-(zlay(l+1)-zlay(l))*g/cpp
753       enddo
754
755       !! Layer where to take the temperature above the mountain
756       do while (hmons .ge. zlay(lmons))
757           lmons=lmons+1
758       enddo
759       !! Real temperature of the environment near to the mountain is t_env(lsummit)=zt(lmons)
760       !! (More simple and makes no real difference is to impose t_env(:)=zt(:))
761       if (lmons.gt.lsummit) then
762         t_env(lsummit)=zt(lmons)
763         zlmons=zlay(lmons)
764         do l=0,nlayer-lsummit-1!2
765           zlmons=zlmons+(zlay(lsummit+l+1)-zlay(lsummit+l))
766           ll=lmons
767           do while (zlmons .ge. zlay(ll) .and. ll.lt.nlayer )
768             ll=ll+1
769           enddo
770           ll=ll-1
771           !! Interpolation above lsummit
772           t_env(lsummit+l+1)= zt(ll) + ((zlmons-zlay(ll))*(zt(ll+1)-zt(ll))/(zlay(ll+1)-zlay(ll)))
773         enddo
774         t_env(nlayer)=zt(nlayer)
775       endif ! (lmons.gt.lsummit)
776       do l=1,nlayer
777          !! temperature above the mountain follows the adiabatic up to reach the environment temperature
778          if (t_top(l).le.t_env(l)) then
779            t_top(l)=t_env(l)
780          endif
781       enddo
782       !!  Temperature delta created at the top of the mountain
783       dt_top = t_top(lsummit)-t_env(lsummit)
784
785       END SUBROUTINE t_topmons
786
787!======================================================================= 
788! **********************************************************************
789! Subroutine to determine the vertical transport with
790! a Van Leer advection scheme (copied from the sedimentation scheme --> see vlz_fi.F)
791!***********************************************************************
792      SUBROUTINE van_leer(nlay,q,pente_max,lwmax,masse,w,wq,masse_pbl,dqm_pbl,alpha_hmons,qbar_pbl)
793
794      IMPLICIT NONE
795
796!--------------------------------------------------------
797! Input/Output Variables
798!--------------------------------------------------------
799      INTEGER,INTENT(IN) :: nlay       ! number of atmospheric layers
800      REAL,INTENT(IN) :: masse(nlay)   ! mass of the layer Dp/g
801      REAL,INTENT(IN) :: pente_max     != 2 conseillee
802      INTEGER,INTENT(IN) :: lwmax      ! layer of dust injection above the mountain, where the vertical velocity is maximum
803      REAL,INTENT(INOUT) :: w(nlay)    ! atmospheric mass "transferred" at each timestep (kg.m-2)
804      REAL,INTENT(INOUT) :: wq(nlay+1)
805      REAL,INTENT(INOUT) :: q(nlay)    ! mixing ratio (kg/kg)
806      REAL,INTENT(IN) :: masse_pbl
807      REAL,INTENT(IN) :: dqm_pbl
808      REAL,INTENT(IN) :: alpha_hmons
809      REAL,INTENT(IN) :: qbar_pbl
810
811!--------------------------------------------------------
812! Local Variables
813!--------------------------------------------------------
814
815      INTEGER i,l,j,ii
816      REAL dzq(nlay),dzqw(nlay),adzqw(nlay),dzqmax
817      REAL sigw, Mtot, MQtot
818      INTEGER m
819
820! **********************************************************************
821!  Mixing ratio vertical gradient at the levels
822! **********************************************************************
823      do l=lwmax+1,nlay !l=2,nlay
824            dzqw(l)=q(l-1)-q(l)
825            adzqw(l)=abs(dzqw(l))
826      enddo
827
828      do l=lwmax+1,nlay-1 !l=2,nlay-1
829            if(dzqw(l)*dzqw(l+1).gt.0.) then
830                dzq(l)=0.5*(dzqw(l)+dzqw(l+1))
831            else
832                dzq(l)=0.
833            endif
834            dzqmax=pente_max*min(adzqw(l),adzqw(l+1))
835            dzq(l)=sign(min(abs(dzq(l)),dzqmax),dzq(l))
836      enddo
837
838      dzq(lwmax)=0. !! dzq(1)=0. => Attention: in the case of van leer only above the mountain it is very important to initialize the transport from lwmax !!
839      dzq(nlay)=0.
840! **********************************************************************
841!  Vertical advection
842! **********************************************************************
843
844       !! No flux at the model top:
845       wq(nlay+1)=0.
846
847       !! Mass flux injected at lwmax
848       wq(lwmax) = -dqm_pbl/alpha_hmons ! dqm_pbl = alpha_hmons x wup(wmax) x rho(lwmax) x ptimestep x qbar_pbl
849                                        ! /alpha_hmons because the variable considered is mr_topdust
850       do l = lwmax,nlay-1
851
852!      1) Compute wq where w < 0 (up) (UPWARD TRANSPORT)     
853!      ===============================
854
855         if (w(l+1).le.0) then
856!         Regular scheme (transfered mass < 1 layer)
857!         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
858          if(-w(l+1).le.masse(l))then
859            sigw=w(l+1)/masse(l)
860            wq(l+1)=w(l+1)*(q(l)-0.5*(1.+sigw)*dzq(l))
861!!-------------------------------------------------------
862!          The following part should not be needed in the
863!          case of an integration over subtimesteps
864!!-------------------------------------------------------
865!!         Extended scheme (transfered mass > 1 layer)
866!!         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
867!!          else
868!!             m = l-1
869!!             Mtot = masse(m+1)
870!!             MQtot = masse(m+1)*q(m+1)
871!!             if (m.lt.lwmax)goto 77!if (m.le.0)goto 77
872!!             do while(-w(l+1).gt.(Mtot+masse(m)))
873!!                m=m-1
874!!                Mtot = Mtot + masse(m+1)
875!!                MQtot = MQtot + masse(m+1)*q(m+1)
876!!                if (m.lt.lwmax)goto 77!if (m.le.0)goto 77
877!!             end do
878!! 77          continue
879!!
880!!              if (m.gt.lwmax) then!if (m.gt.0) then
881!!                sigw=(w(l+1)+Mtot)/masse(m)
882!!                wq(l+1)= -(MQtot + (-w(l+1)-Mtot)*         &
883!!                (q(m)-0.5*(1.+sigw)*dzq(m)))
884!!              else if ((-w(l+1).gt.(Mtot))) then
885!!                Mtot=Mtot+masse_pbl!*alpha_hmons
886!!                MQtot=MQtot+masse_pbl*qbar_pbl!*alpha_hmons
887!!                sigw=(w(l+1)+Mtot)/masse(m)
888!!                wq(l+1)= -(MQtot + (-w(l+1)-Mtot)*qbar_pbl)
889!!              else
890!!                w(l+1) = -Mtot
891!!                wq(l+1) = -MQtot
892!!              end if
893!!
894          endif
895
896!      2) Compute wq where w > 0 (down) (DOWNWARD TRANSPORT)     
897!      ===============================
898
899         else if (w(l).gt.0.) then
900!         Regular scheme (transfered mass < 1 layer)
901!         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
902          if(w(l).le.masse(l))then
903            sigw=w(l)/masse(l)
904            wq(l)=w(l)*(q(l)+0.5*(1.-sigw)*dzq(l))
905!!-------------------------------------------------------
906!          The following part should not be needed in the
907!          case of an integration over subtimesteps
908!!-------------------------------------------------------
909!!         Extended scheme (transfered mass > 1 layer)
910!!         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
911!!          else
912!!            m=l
913!!            Mtot = masse(m)
914!!            MQtot = masse(m)*q(m)
915!!            if(m.ge.nlay)goto 88
916!!            do while(w(l).gt.(Mtot+masse(m+1)))
917!!               m=m+1
918!!                Mtot = Mtot + masse(m)
919!!                MQtot = MQtot + masse(m)*q(m)
920!!                if(m.ge.nlay)goto 88
921!!            end do
922!! 88         continue
923!!            if (m.lt.nlay) then
924!!                sigw=(w(l)-Mtot)/masse(m+1)
925!!                wq(l)=(MQtot + (w(l)-Mtot)* &
926!!                          (q(m+1)+0.5*(1.-sigw)*dzq(m+1)) )
927!!            else
928!!                w(l) = Mtot
929!!                wq(l) = MQtot
930!!            end if
931          end if ! (w(l).le.masse(l))
932
933         end if ! (w(l+1).le.0)
934
935       enddo ! l = lwmax+1,nlay-1
936
937! **********************************************************************
938!  Mixing ratio update after the vertical transport
939! **********************************************************************
940       do l = lwmax,nlay-1 !l = 1,nlay-1  ! loop different than when w>0
941
942         if ( (wq(l+1)-wq(l)) .lt. -(masse(l)*q(l)) .and. (abs(wq(l+1)).gt. 0.) .and.  (abs(wq(l)).gt. 0.) ) then
943           wq(l+1) = wq(l)-masse(l)*q(l)
944         end if
945
946         q(l)=q(l) +  (wq(l+1)-wq(l))/masse(l)
947
948       enddo
949
950
951      end subroutine van_leer
952
953!********************************************************************************
954       ! initialization module variables
955       subroutine ini_topmons_mod(ngrid,nlayer)
956       
957       implicit none
958       
959       integer, intent(in) :: ngrid
960       integer, intent(in) :: nlayer
961       
962       allocate(alpha_hmons(ngrid))
963
964       end subroutine ini_topmons_mod
965       
966       subroutine end_topmons_mod
967       
968       implicit none
969       
970       if (allocated(alpha_hmons)) deallocate(alpha_hmons)
971
972       end subroutine end_topmons_mod       
973     
974      END MODULE topmons_mod
Note: See TracBrowser for help on using the repository browser.