source: codes/icosagcm/trunk/src/advect_tracer.f90 @ 370

Last change on this file since 370 was 364, checked in by dubos, 9 years ago

Bugfix : memory leak in transfert_mpi / New : detect send_message not paired with wait_message

File size: 9.8 KB
RevLine 
[17]1MODULE advect_tracer_mod
[19]2  USE icosa
[138]3  IMPLICIT NONE
[17]4  PRIVATE
[22]5
[186]6  TYPE(t_field),SAVE,POINTER :: f_normal(:)
7  TYPE(t_field),SAVE,POINTER :: f_tangent(:)
8  TYPE(t_field),SAVE,POINTER :: f_gradq3d(:)
9  TYPE(t_field),SAVE,POINTER :: f_cc(:)  ! starting point of backward-trajectory (Miura approach)
[252]10  TYPE(t_field),SAVE,POINTER :: f_sqrt_leng(:)
[151]11
[186]12  TYPE(t_message),SAVE :: req_u, req_cc, req_wfluxt, req_q, req_rhodz, req_gradq3d
[151]13
[136]14  REAL(rstd), PARAMETER :: pente_max=2.0 ! for vlz
15
[151]16! temporary shared variable for vlz
[186]17  TYPE(t_field),SAVE,POINTER :: f_dzqw(:)   ! vertical finite difference of q
18  TYPE(t_field),SAVE,POINTER :: f_adzqw(:)  ! abs(dzqw)
19  TYPE(t_field),SAVE,POINTER :: f_dzq(:)    ! limited slope of q
20  TYPE(t_field),SAVE,POINTER :: f_wq(:)     ! time-integrated flux of q
[151]21
[136]22  PUBLIC init_advect_tracer, advect_tracer
23
[17]24CONTAINS
[22]25
[98]26  SUBROUTINE init_advect_tracer
[22]27    USE advect_mod
[295]28    USE omp_para
[22]29    REAL(rstd),POINTER :: tangent(:,:)
30    REAL(rstd),POINTER :: normal(:,:)
[252]31    REAL(rstd),POINTER :: sqrt_leng(:)
[23]32    INTEGER :: ind
[22]33
[138]34    CALL allocate_field(f_normal,field_u,type_real,3, name='normal')
35    CALL allocate_field(f_tangent,field_u,type_real,3, name='tangent')
36    CALL allocate_field(f_gradq3d,field_t,type_real,llm,3, name='gradq3d')
37    CALL allocate_field(f_cc,field_u,type_real,llm,3, name='cc')
[252]38    CALL allocate_field(f_sqrt_leng,field_t,type_real, name='sqrt_leng')
[151]39    CALL allocate_field(f_dzqw, field_t, type_real, llm, name='dzqw')
40    CALL allocate_field(f_adzqw, field_t, type_real, llm, name='adzqw')
41    CALL allocate_field(f_dzq, field_t, type_real, llm, name='dzq')
42    CALL allocate_field(f_wq, field_t, type_real, llm+1, name='wq')
43   
[22]44    DO ind=1,ndomain
[186]45       IF (.NOT. assigned_domain(ind)) CYCLE
[22]46       CALL swap_dimensions(ind)
47       CALL swap_geometry(ind)
48       normal=f_normal(ind)
49       tangent=f_tangent(ind)
[252]50       sqrt_leng=f_sqrt_leng(ind)
[295]51       IF (is_omp_level_master) CALL init_advect(normal,tangent,sqrt_leng)
[22]52    END DO
53
[17]54  END SUBROUTINE init_advect_tracer
[22]55
[136]56  SUBROUTINE advect_tracer(f_hfluxt, f_wfluxt,f_u, f_q,f_rhodz)
[22]57    USE advect_mod
[136]58    USE mpipara
[145]59    USE trace
[347]60    USE write_field_mod
[22]61    IMPLICIT NONE
[145]62   
[136]63    TYPE(t_field),POINTER :: f_hfluxt(:)   ! time-integrated horizontal mass flux
64    TYPE(t_field),POINTER :: f_wfluxt(:)   ! time-integrated vertical mass flux
65    TYPE(t_field),POINTER :: f_u(:)        ! velocity (for back-trajectories)
66    TYPE(t_field),POINTER :: f_q(:)        ! tracer
67    TYPE(t_field),POINTER :: f_rhodz(:)    ! mass field at beginning of macro time step
[17]68
[252]69    REAL(rstd),POINTER :: q(:,:,:), normal(:,:), tangent(:,:), sqrt_leng(:), gradq3d(:,:,:), cc(:,:,:)
[136]70    REAL(rstd),POINTER :: hfluxt(:,:), wfluxt(:,:)
71    REAL(rstd),POINTER :: rhodz(:,:), u(:,:) 
[151]72! temporary shared variable for vlz
73    REAL(rstd),POINTER ::  dzqw(:,:)         ! vertical finite difference of q
74    REAL(rstd),POINTER ::  adzqw(:,:)        ! abs(dzqw)
75    REAL(rstd),POINTER ::  dzq(:,:)          ! limited slope of q
76    REAL(rstd),POINTER ::  wq(:,:)           ! time-integrated flux of q
77   
78     INTEGER :: ind,k
79    LOGICAL,SAVE :: first=.TRUE.
80!$OMP THREADPRIVATE(first)
[17]81
[151]82    IF (first) THEN
83      first=.FALSE.
[364]84      CALL init_message(f_u,req_e1_vect,req_u, 'req_u')
85      CALL init_message(f_cc,req_e1_scal,req_cc, 'req_cc')
86      CALL init_message(f_wfluxt,req_i1,req_wfluxt, 'req_wfluxt')
87      CALL init_message(f_q,req_i1,req_q, 'req_q')
88      CALL init_message(f_rhodz,req_i1,req_rhodz, 'req_rhodz')
89      CALL init_message(f_gradq3d,req_i1,req_gradq3d, 'req_gradq3d')
[151]90    ENDIF
91   
[186]92!!$OMP BARRIER
[151]93
[364]94    IF(nqtot<1) RETURN
[145]95    CALL trace_start("advect_tracer") 
96
[151]97    CALL send_message(f_u,req_u)
[327]98    CALL send_message(f_wfluxt,req_wfluxt)
99    CALL send_message(f_q,req_q)
100    CALL send_message(f_rhodz,req_rhodz)
101
[186]102    CALL wait_message(req_u)
103    CALL wait_message(req_wfluxt)
104    CALL wait_message(req_q)
[151]105    CALL wait_message(req_rhodz)
106   
[138]107    ! 1/2 vertical transport + back-trajectories
[22]108    DO ind=1,ndomain
[186]109       IF (.NOT. assigned_domain(ind)) CYCLE
[17]110       CALL swap_dimensions(ind)
111       CALL swap_geometry(ind)
[138]112       normal  = f_normal(ind)
113       tangent = f_tangent(ind)
114       cc      = f_cc(ind)
115       u       = f_u(ind)
[136]116       q       = f_q(ind)
117       rhodz   = f_rhodz(ind)
118       wfluxt  = f_wfluxt(ind) 
[151]119       dzqw    = f_dzqw(ind)
120       adzqw   = f_adzqw(ind)
121       dzq     = f_dzq(ind)
122       wq      = f_wq(ind) 
[148]123
[138]124       DO k = 1, nqtot
[151]125          CALL vlz(k==nqtot,0.5, wfluxt,rhodz,q(:,:,k),1,dzqw, adzqw, dzq, wq)
[138]126       END DO
[148]127
[138]128       CALL compute_backward_traj(tangent,normal,u,0.5*dt*itau_adv, cc) 
[151]129
[22]130    END DO
[17]131
[174]132    CALL send_message(f_cc,req_cc)
[17]133
[174]134
[138]135    ! horizontal transport - split in two to place transfer of gradq3d
[136]136    DO k = 1, nqtot
[138]137       DO ind=1,ndomain
[186]138          IF (.NOT. assigned_domain(ind)) CYCLE
[138]139          CALL swap_dimensions(ind)
140          CALL swap_geometry(ind)
141          q       = f_q(ind)
142          gradq3d = f_gradq3d(ind)
[252]143          sqrt_leng=f_sqrt_leng(ind)
144          CALL compute_gradq3d(q(:,:,k),sqrt_leng,gradq3d,xyz_i,xyz_v)
[327]145
[138]146       END DO
[17]147
[151]148       CALL send_message(f_gradq3d,req_gradq3d)
[327]149       CALL wait_message(req_cc)
[151]150       CALL wait_message(req_gradq3d)
[17]151
[148]152
[138]153       DO ind=1,ndomain
[186]154          IF (.NOT. assigned_domain(ind)) CYCLE
[138]155          CALL swap_dimensions(ind)
156          CALL swap_geometry(ind)
157          cc      = f_cc(ind)
158          q       = f_q(ind)
159          rhodz   = f_rhodz(ind)
160          hfluxt  = f_hfluxt(ind) 
161          gradq3d = f_gradq3d(ind)
162          CALL compute_advect_horiz(k==nqtot,hfluxt,cc,gradq3d, rhodz,q(:,:,k))
163       END DO
164    END DO 
[146]165   
[136]166    ! 1/2 vertical transport
[186]167!!$OMP BARRIER
[151]168
[138]169    DO ind=1,ndomain
[186]170       IF (.NOT. assigned_domain(ind)) CYCLE
[138]171       CALL swap_dimensions(ind)
172       CALL swap_geometry(ind)
173       q       = f_q(ind)
174       rhodz   = f_rhodz(ind)
175       wfluxt  = f_wfluxt(ind) 
[151]176       dzqw    = f_dzqw(ind)
177       adzqw   = f_adzqw(ind)
178       dzq     = f_dzq(ind)
179       wq      = f_wq(ind) 
180
[138]181       DO k = 1,nqtot
[151]182          CALL vlz(k==nqtot, 0.5,wfluxt,rhodz, q(:,:,k),0, dzqw, adzqw, dzq, wq)
[138]183       END DO
[151]184
[136]185    END DO
[138]186
[146]187    CALL trace_end("advect_tracer")
188
[186]189!!$OMP BARRIER
[151]190
[138]191  END SUBROUTINE advect_tracer
192
[151]193  SUBROUTINE vlz(update_mass, fac,wfluxt,mass, q, halo, dzqw, adzqw, dzq, wq)
[136]194    !
195    !     Auteurs:   P.Le Van, F.Hourdin, F.Forget, T. Dubos
196    !
197    !    ********************************************************************
198    !     Update tracers using vertical mass flux only
199    !     Van Leer scheme with minmod limiter
200    !     wfluxt >0 for upward transport
201    !    ********************************************************************
[148]202    USE trace
[151]203    USE omp_para
[22]204    IMPLICIT NONE
[136]205    LOGICAL, INTENT(IN)       :: update_mass
206    REAL(rstd), INTENT(IN)    :: fac, wfluxt(iim*jjm,llm+1) ! vertical mass flux
207    REAL(rstd), INTENT(INOUT) :: mass(iim*jjm,llm)
208    REAL(rstd), INTENT(INOUT) :: q(iim*jjm,llm)
[148]209    INTEGER, INTENT(IN) :: halo
[22]210
[151]211! temporary shared variable
212    REAL(rstd),INTENT(INOUT) :: dzqw(iim*jjm,llm),        & ! vertical finite difference of q
213                                adzqw(iim*jjm,llm),       & ! abs(dzqw)
214                                dzq(iim*jjm,llm),         & ! limited slope of q
215                                wq(iim*jjm,llm+1)           ! time-integrated flux of q
216
217
[136]218    REAL(rstd) :: dzqmax, newmass, sigw, qq, w
[174]219    INTEGER :: i,ij,l,j,ijb,ije
[22]220
[148]221    CALL trace_start("vlz")
[174]222     
223     ijb=((jj_begin-halo)-1)*iim+ii_begin-halo
224     ije = ((jj_end+halo)-1)*iim+ii_end+halo
[148]225
[136]226    ! finite difference of q
[151]227
228     DO l=ll_beginp1,ll_end
[174]229!$SIMD
230       DO ij=ijb,ije
231         dzqw(ij,l)=q(ij,l)-q(ij,l-1)
232         adzqw(ij,l)=abs(dzqw(ij,l))
[22]233       ENDDO
234    ENDDO
235
[151]236!--> flush dzqw, adzqw
[295]237!$OMP BARRIER
[151]238
[136]239    ! minmod-limited slope of q
240    ! dzq = slope*dz, i.e. the reconstructed q varies by dzq inside level l
[151]241
242     DO l=ll_beginp1,ll_endm1
[174]243!$SIMD
244       DO ij=ijb,ije 
245         IF(dzqw(ij,l)*dzqw(ij,l+1).gt.0.) THEN
246             dzq(ij,l) = 0.5*( dzqw(ij,l)+dzqw(ij,l+1) )
247             dzqmax    = pente_max * min( adzqw(ij,l),adzqw(ij,l+1) )
248             dzq(ij,l) = sign( min(abs(dzq(ij,l)),dzqmax) , dzq(ij,l) )  ! NB : sign(a,b)=a*sign(b)
249          ELSE
250             dzq(ij,l)=0.
251          ENDIF
[22]252       ENDDO
253    ENDDO
[17]254
[151]255
[136]256    ! 0 slope in top and bottom layers
[295]257    IF (is_omp_first_level) THEN
[174]258      DO ij=ijb,ije
[151]259           dzq(ij,1)=0.
260      ENDDO
261    ENDIF
262     
[295]263    IF (is_omp_last_level) THEN
[174]264      DO ij=ijb,ije
[136]265          dzq(ij,llm)=0.
[151]266      ENDDO
267    ENDIF
[17]268
[151]269!---> flush dzq
[295]270!$OMP BARRIER 
[151]271
[136]272    ! sigw = fraction of mass that leaves level l/l+1
273    ! then amount of q leaving level l/l+1 = wq = w * qq
[151]274     DO l=ll_beginp1,ll_end
[174]275!$SIMD
276       DO ij=ijb,ije
[151]277             w = fac*wfluxt(ij,l)
[138]278             IF(w>0.) THEN  ! upward transport, upwind side is at level l
[151]279                sigw       = w/mass(ij,l-1)
280                qq         = q(ij,l-1)+0.5*(1.-sigw)*dzq(ij,l-1) ! qq = q if sigw=1 , qq = q+dzq/2 if sigw=0
281             ELSE           ! downward transport, upwind side is at level l+1
[138]282                sigw       = w/mass(ij,l)
[151]283                qq         = q(ij,l)-0.5*(1.+sigw)*dzq(ij,l) ! qq = q if sigw=-1 , qq = q-dzq/2 if sigw=0               
[22]284             ENDIF
[151]285             wq(ij,l) = w*qq
[22]286       ENDDO
287    END DO
[136]288    ! wq = 0 at top and bottom
[295]289    IF (is_omp_first_level) THEN
[174]290       DO ij=ijb,ije
[151]291            wq(ij,1)=0.
292      END DO
293    ENDIF
294   
[295]295    IF (is_omp_last_level) THEN
[174]296      DO ij=ijb,ije
[151]297            wq(ij,llm+1)=0.
298      END DO
299    ENDIF
[17]300
[151]301! --> flush wq
[295]302!$OMP BARRIER
[151]303
304
[136]305    ! update q, mass is updated only after all q's have been updated
[151]306    DO l=ll_begin,ll_end
[174]307!$SIMD
308       DO ij=ijb,ije
[136]309             newmass = mass(ij,l) + fac*(wfluxt(ij,l)-wfluxt(ij,l+1))
310             q(ij,l) = ( q(ij,l)*mass(ij,l) + wq(ij,l)-wq(ij,l+1) ) / newmass
311             IF(update_mass) mass(ij,l)=newmass
[22]312       ENDDO
313    END DO
[136]314
[148]315    CALL trace_end("vlz")
316
[22]317  END SUBROUTINE vlz
[17]318
319END MODULE advect_tracer_mod
Note: See TracBrowser for help on using the repository browser.