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

Last change on this file since 2613 was 2584, checked in by romain.vande, 3 years ago

Second stage of implementation of Open_MP in the physic.
Run with callrad=.true.

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