source: codes/icosagcm/trunk/src/dynamics/caldyn_kernels_base.F90 @ 561

Last change on this file since 561 was 561, checked in by dubos, 7 years ago

trunk : backported commits 555-557 from devel

File size: 13.1 KB
Line 
1MODULE caldyn_kernels_base_mod
2  USE icosa
3  USE transfert_mod
4  USE disvert_mod
5  USE omp_para
6  USE trace
7  IMPLICIT NONE
8  PRIVATE
9
10  INTEGER, PARAMETER,PUBLIC :: energy=1, enstrophy=2
11  TYPE(t_field),POINTER,PUBLIC :: f_out_u(:), f_qu(:), f_qv(:)
12  REAL(rstd),SAVE,POINTER :: out_u(:,:), p(:,:), qu(:,:)
13  !$OMP THREADPRIVATE(out_u, p, qu)
14
15  ! temporary shared variables for caldyn
16  TYPE(t_field),POINTER,PUBLIC :: f_pk(:),f_wwuu(:),f_planetvel(:), &
17                                  f_Fel(:), f_gradPhi2(:), f_wil(:), f_Wetadot(:)
18
19  INTEGER, PUBLIC :: caldyn_conserv
20  !$OMP THREADPRIVATE(caldyn_conserv)
21
22  TYPE(t_message),PUBLIC :: req_ps, req_mass, req_theta_rhodz, req_u, req_qu, req_geopot, req_w
23
24  PUBLIC :: compute_geopot, compute_caldyn_vert, compute_caldyn_vert_nh
25
26CONTAINS
27
28  !**************************** Geopotential *****************************
29
30  SUBROUTINE compute_geopot(rhodz,theta, ps,pk,geopot) 
31    REAL(rstd),INTENT(IN)    :: rhodz(iim*jjm,llm)
32    REAL(rstd),INTENT(IN)    :: theta(iim*jjm,llm,nqdyn) ! active scalars : theta/entropy, moisture, ...
33    REAL(rstd),INTENT(INOUT) :: ps(iim*jjm)
34    REAL(rstd),INTENT(OUT)   :: pk(iim*jjm,llm)       ! Exner function (compressible) /Lagrange multiplier (Boussinesq)
35    REAL(rstd),INTENT(INOUT) :: geopot(iim*jjm,llm+1) ! geopotential
36
37    INTEGER :: i,j,ij,l
38    REAL(rstd) :: Rd, p_ik, exner_ik, temp_ik, qv, chi, Rmix, gv
39    INTEGER    :: ij_omp_begin_ext, ij_omp_end_ext
40
41    CALL trace_start("compute_geopot")
42
43!$OMP BARRIER
44
45    CALL distrib_level(ij_end_ext-ij_begin_ext+1,ij_omp_begin_ext,ij_omp_end_ext)
46    ij_omp_begin_ext=ij_omp_begin_ext+ij_begin_ext-1
47    ij_omp_end_ext=ij_omp_end_ext+ij_begin_ext-1
48
49    Rd = kappa*cpp
50
51#ifdef CPP_DYSL
52!#if 0
53#include "../kernels/compute_geopot.k90"
54#else
55
56    ! Pressure is computed first top-down (temporarily stored in pk)
57    ! Then Exner pressure and geopotential are computed bottom-up
58    ! Works also when caldyn_eta=eta_mass         
59
60    IF(boussinesq) THEN ! compute geopotential and pk=Lagrange multiplier
61       ! specific volume 1 = dphi/g/rhodz
62       !         IF (is_omp_level_master) THEN ! no openMP on vertical due to dependency
63       DO l = 1,llm
64          !DIR$ SIMD
65          DO ij=ij_omp_begin_ext,ij_omp_end_ext         
66             geopot(ij,l+1) = geopot(ij,l) + g*rhodz(ij,l)
67          ENDDO
68       ENDDO
69       ! use hydrostatic balance with theta*rhodz to find pk (Lagrange multiplier=pressure)
70       ! uppermost layer
71       !DIR$ SIMD
72       DO ij=ij_begin_ext,ij_end_ext         
73          pk(ij,llm) = ptop + (.5*g)*theta(ij,llm,1)*rhodz(ij,llm)
74       END DO
75       ! other layers
76       DO l = llm-1, 1, -1
77          !          !$OMP DO SCHEDULE(STATIC)
78          !DIR$ SIMD
79          DO ij=ij_begin_ext,ij_end_ext         
80             pk(ij,l) = pk(ij,l+1) + (.5*g)*(theta(ij,l,1)*rhodz(ij,l)+theta(ij,l+1,1)*rhodz(ij,l+1))
81          END DO
82       END DO
83       ! now pk contains the Lagrange multiplier (pressure)
84    ELSE ! non-Boussinesq, compute pressure, Exner pressure or temperature, then geopotential
85       ! uppermost layer
86       
87       SELECT CASE(caldyn_thermo)
88          CASE(thermo_theta, thermo_entropy)
89             !DIR$ SIMD
90             DO ij=ij_omp_begin_ext,ij_omp_end_ext
91                pk(ij,llm) = ptop + (.5*g)*rhodz(ij,llm)
92             END DO
93             ! other layers
94             DO l = llm-1, 1, -1
95                !DIR$ SIMD
96                DO ij=ij_omp_begin_ext,ij_omp_end_ext         
97                   pk(ij,l) = pk(ij,l+1) + (.5*g)*(rhodz(ij,l)+rhodz(ij,l+1))
98                END DO
99             END DO
100             ! surface pressure (for diagnostics)
101             IF(caldyn_eta==eta_lag) THEN
102                DO ij=ij_omp_begin_ext,ij_omp_end_ext         
103                   ps(ij) = pk(ij,1) + (.5*g)*rhodz(ij,1)
104                END DO
105             END IF
106          CASE(thermo_moist) ! theta(ij,l,2) = qv = mv/md
107             !DIR$ SIMD
108             DO ij=ij_omp_begin_ext,ij_omp_end_ext
109                pk(ij,llm) = ptop + (.5*g)*rhodz(ij,llm)*(1.+theta(ij,l,2))
110             END DO
111             ! other layers
112             DO l = llm-1, 1, -1
113                !DIR$ SIMD
114                DO ij=ij_omp_begin_ext,ij_omp_end_ext         
115                   pk(ij,l) = pk(ij,l+1) + (.5*g)*(          &
116                        rhodz(ij,l)  *(1.+theta(ij,l,2)) +   &
117                        rhodz(ij,l+1)*(1.+theta(ij,l+1,2)) )
118                END DO
119             END DO
120             ! surface pressure (for diagnostics)
121             IF(caldyn_eta==eta_lag) THEN
122                DO ij=ij_omp_begin_ext,ij_omp_end_ext         
123                   ps(ij) = pk(ij,1) + (.5*g)*rhodz(ij,1)*(1.+theta(ij,l,2))
124                END DO
125             END IF
126          END SELECT
127
128       DO l = 1,llm
129          SELECT CASE(caldyn_thermo)
130          CASE(thermo_theta)
131             !DIR$ SIMD
132             DO ij=ij_omp_begin_ext,ij_omp_end_ext
133                p_ik = pk(ij,l)
134                exner_ik = cpp * (p_ik/preff) ** kappa
135                pk(ij,l) = exner_ik
136                ! specific volume v = kappa*theta*pi/p = dphi/g/rhodz
137                geopot(ij,l+1) = geopot(ij,l) + (g*kappa)*rhodz(ij,l)*theta(ij,l,1)*exner_ik/p_ik
138             ENDDO
139          CASE(thermo_entropy) ! theta is in fact entropy = cpp*log(theta/Treff) = cpp*log(T/Treff) - Rd*log(p/preff)
140             !DIR$ SIMD
141             DO ij=ij_omp_begin_ext,ij_omp_end_ext
142                p_ik = pk(ij,l)
143                temp_ik = Treff*exp((theta(ij,l,1) + Rd*log(p_ik/preff))/cpp)
144                pk(ij,l) = temp_ik
145                ! specific volume v = Rd*T/p = dphi/g/rhodz
146                geopot(ij,l+1) = geopot(ij,l) + (g*Rd)*rhodz(ij,l)*temp_ik/p_ik
147             ENDDO
148          CASE(thermo_moist) ! theta is moist pseudo-entropy per dry air mass
149             DO ij=ij_omp_begin_ext,ij_omp_end_ext
150                p_ik = pk(ij,l)
151                qv = theta(ij,l,2) ! water vaper mixing ratio = mv/md
152                Rmix = Rd+qv*Rv
153                chi = ( theta(ij,l,1) + Rmix*log(p_ik/preff) ) / (cpp + qv*cppv) ! log(T/Treff)
154                temp_ik = Treff*exp(chi)
155                pk(ij,l) = temp_ik
156                ! specific volume v = R*T/p = dphi/g/rhodz
157                ! R = (Rd + qv.Rv)/(1+qv)
158                geopot(ij,l+1) = geopot(ij,l) + g*Rmix*rhodz(ij,l)*temp_ik/(p_ik*(1+qv))
159             ENDDO
160          CASE DEFAULT
161             STOP
162          END SELECT
163       ENDDO
164    END IF
165
166#endif
167
168    !ym flush geopot
169    !$OMP BARRIER
170
171    CALL trace_end("compute_geopot")
172
173  END SUBROUTINE compute_geopot
174
175  SUBROUTINE compute_caldyn_vert(u,theta,rhodz,convm, wflux,wwuu, dps,dtheta_rhodz,du)
176    REAL(rstd),INTENT(IN)  :: u(iim*3*jjm,llm)
177    REAL(rstd),INTENT(IN)  :: theta(iim*jjm,llm,nqdyn)
178    REAL(rstd),INTENT(IN)  :: rhodz(iim*jjm,llm)
179    REAL(rstd),INTENT(INOUT)  :: convm(iim*jjm,llm)  ! mass flux convergence
180
181    REAL(rstd),INTENT(INOUT) :: wflux(iim*jjm,llm+1) ! vertical mass flux (kg/m2/s)
182    REAL(rstd),INTENT(INOUT) :: wwuu(iim*3*jjm,llm+1)
183    REAL(rstd),INTENT(INOUT) :: du(iim*3*jjm,llm)
184    REAL(rstd),INTENT(INOUT) :: dtheta_rhodz(iim*jjm,llm,nqdyn)
185    REAL(rstd),INTENT(OUT) :: dps(iim*jjm)
186
187    ! temporary variable   
188    INTEGER :: i,j,ij,l,iq
189    REAL(rstd) :: p_ik, exner_ik
190    INTEGER    :: ij_omp_begin, ij_omp_end
191
192    CALL trace_start("compute_caldyn_vert")
193
194    CALL distrib_level(ij_end-ij_begin+1,ij_omp_begin,ij_omp_end)
195    ij_omp_begin=ij_omp_begin+ij_begin-1
196    ij_omp_end=ij_omp_end+ij_begin-1
197
198    !    REAL(rstd) :: wwuu(iim*3*jjm,llm+1) ! tmp var, don't know why but gain 30% on the whole code in opemp
199    ! need to be understood
200
201    !    wwuu=wwuu_out
202    CALL trace_start("compute_caldyn_vert")
203
204    !$OMP BARRIER   
205!!! cumulate mass flux convergence from top to bottom
206    !  IF (is_omp_level_master) THEN
207    DO  l = llm-1, 1, -1
208       !    IF (caldyn_conserv==energy) CALL test_message(req_qu)
209
210!!$OMP DO SCHEDULE(STATIC)
211       !DIR$ SIMD
212       DO ij=ij_omp_begin,ij_omp_end         
213          convm(ij,l) = convm(ij,l) + convm(ij,l+1)
214       ENDDO
215    ENDDO
216    !  ENDIF
217
218    !$OMP BARRIER
219    ! FLUSH on convm
220!!!!!!!!!!!!!!!!!!!!!!!!!
221
222    ! compute dps
223    IF (is_omp_first_level) THEN
224       !DIR$ SIMD
225       DO ij=ij_begin,ij_end         
226          ! dps/dt = -int(div flux)dz
227          dps(ij) = convm(ij,1)
228       ENDDO
229    ENDIF
230
231!!! Compute vertical mass flux (l=1,llm+1 done by caldyn_BC)
232    DO l=ll_beginp1,ll_end
233       !    IF (caldyn_conserv==energy) CALL test_message(req_qu)
234       !DIR$ SIMD
235       DO ij=ij_begin,ij_end         
236          ! w = int(z,ztop,div(flux)dz) + B(eta)dps/dt
237          ! => w>0 for upward transport
238          wflux( ij, l ) = bp(l) * convm( ij, 1 ) - convm( ij, l ) 
239       ENDDO
240    ENDDO
241
242    !--> flush wflux 
243    !$OMP BARRIER
244
245    DO iq=1,nqdyn
246       DO l=ll_begin,ll_endm1
247       !DIR$ SIMD
248          DO ij=ij_begin,ij_end         
249             dtheta_rhodz(ij, l, iq) = dtheta_rhodz(ij, l, iq)  -   0.5 * &
250                  ( wflux(ij,l+1) * (theta(ij,l,iq) + theta(ij,l+1,iq)))
251          END DO
252       END DO
253       DO l=ll_beginp1,ll_end
254          !DIR$ SIMD
255          DO ij=ij_begin,ij_end         
256             dtheta_rhodz(ij, l, iq) = dtheta_rhodz(ij, l, iq)  +   0.5 * &
257                  ( wflux(ij,l) * (theta(ij,l-1,iq) + theta(ij,l,iq) ) )
258          END DO
259       END DO
260    END DO
261
262    ! Compute vertical transport
263    DO l=ll_beginp1,ll_end
264       !DIR$ SIMD
265       DO ij=ij_begin,ij_end         
266          wwuu(ij+u_right,l) = 0.5*( wflux(ij,l) + wflux(ij+t_right,l)) * (u(ij+u_right,l) - u(ij+u_right,l-1))
267          wwuu(ij+u_lup,l) = 0.5* ( wflux(ij,l) + wflux(ij+t_lup,l)) * (u(ij+u_lup,l) - u(ij+u_lup,l-1))
268          wwuu(ij+u_ldown,l) = 0.5*( wflux(ij,l) + wflux(ij+t_ldown,l)) * (u(ij+u_ldown,l) - u(ij+u_ldown,l-1))
269       ENDDO
270    ENDDO
271
272    !--> flush wwuu 
273    !$OMP BARRIER
274
275    ! Add vertical transport to du
276    DO l=ll_begin,ll_end
277       !DIR$ SIMD
278       DO ij=ij_begin,ij_end         
279          du(ij+u_right, l )   = du(ij+u_right,l)  - (wwuu(ij+u_right,l+1)+ wwuu(ij+u_right,l)) / (rhodz(ij,l)+rhodz(ij+t_right,l))
280          du(ij+u_lup, l )     = du(ij+u_lup,l)    - (wwuu(ij+u_lup,l+1)  + wwuu(ij+u_lup,l))   / (rhodz(ij,l)+rhodz(ij+t_lup,l))
281          du(ij+u_ldown, l )   = du(ij+u_ldown,l)  - (wwuu(ij+u_ldown,l+1)+ wwuu(ij+u_ldown,l)) / (rhodz(ij,l)+rhodz(ij+t_ldown,l))
282       ENDDO
283    ENDDO
284
285    !  DO l=ll_beginp1,ll_end
286    !!DIR$ SIMD
287    !      DO ij=ij_begin,ij_end         
288    !        wwuu_out(ij+u_right,l) = wwuu(ij+u_right,l)
289    !        wwuu_out(ij+u_lup,l)   = wwuu(ij+u_lup,l)
290    !        wwuu_out(ij+u_ldown,l) = wwuu(ij+u_ldown,l)
291    !     ENDDO
292    !   ENDDO
293
294    CALL trace_end("compute_caldyn_vert")
295
296  END SUBROUTINE compute_caldyn_vert
297
298  SUBROUTINE compute_caldyn_vert_NH(mass,geopot,W,wflux, W_etadot, du,dPhi,dW)
299    REAL(rstd),INTENT(IN) :: mass(iim*jjm,llm)
300    REAL(rstd),INTENT(IN) :: geopot(iim*jjm,llm+1)
301    REAL(rstd),INTENT(IN) :: W(iim*jjm,llm+1)
302    REAL(rstd),INTENT(IN) :: wflux(iim*jjm,llm+1)
303    REAL(rstd),INTENT(INOUT) :: du(iim*3*jjm,llm)
304    REAL(rstd),INTENT(INOUT) :: dPhi(iim*jjm,llm+1)
305    REAL(rstd),INTENT(INOUT) :: dW(iim*jjm,llm+1)
306    REAL(rstd) :: W_etadot(iim*jjm,llm) ! vertical flux of vertical momentum
307    ! local arrays
308    REAL(rstd) :: eta_dot(iim*jjm, llm) ! eta_dot in full layers
309    REAL(rstd) :: wcov(iim*jjm,llm) ! covariant vertical momentum in full layers
310    ! indices and temporary values
311    INTEGER    :: ij, l
312    REAL(rstd) :: wflux_ij, w_ij
313
314    CALL trace_start("compute_caldyn_vert_nh")
315
316#ifdef CPP_DYSL
317!$OMP BARRIER
318#include "../kernels/caldyn_vert_NH.k90"
319!$OMP BARRIER
320#else
321#define ETA_DOT(ij) eta_dot(ij,1)
322#define WCOV(ij) wcov(ij,1)
323
324    DO l=ll_begin,ll_end
325       ! compute the local arrays
326       !DIR$ SIMD
327       DO ij=ij_begin_ext,ij_end_ext
328          wflux_ij = .5*(wflux(ij,l)+wflux(ij,l+1))
329          w_ij = .5*(W(ij,l)+W(ij,l+1))/mass(ij,l)
330          W_etadot(ij,l) = wflux_ij*w_ij
331          ETA_DOT(ij) = wflux_ij / mass(ij,l)
332          WCOV(ij) = w_ij*(geopot(ij,l+1)-geopot(ij,l))
333       ENDDO
334       ! add NH term to du
335      !DIR$ SIMD
336      DO ij=ij_begin,ij_end
337          du(ij+u_right,l) = du(ij+u_right,l) &
338               - .5*(WCOV(ij+t_right)+WCOV(ij)) &
339               *ne_right*(ETA_DOT(ij+t_right)-ETA_DOT(ij))
340          du(ij+u_lup,l) = du(ij+u_lup,l) &
341               - .5*(WCOV(ij+t_lup)+WCOV(ij)) &
342               *ne_lup*(ETA_DOT(ij+t_lup)-ETA_DOT(ij))
343          du(ij+u_ldown,l) = du(ij+u_ldown,l) &
344               - .5*(WCOV(ij+t_ldown)+WCOV(ij)) &
345               *ne_ldown*(ETA_DOT(ij+t_ldown)-ETA_DOT(ij))
346       END DO
347    ENDDO
348    ! add NH terms to dW, dPhi
349    ! FIXME : TODO top and bottom
350    DO l=ll_beginp1,ll_end ! inner interfaces only
351       !DIR$ SIMD
352       DO ij=ij_begin,ij_end
353          dPhi(ij,l) = dPhi(ij,l) - wflux(ij,l) &
354               * (geopot(ij,l+1)-geopot(ij,l-1))/(mass(ij,l-1)+mass(ij,l))
355       END DO
356    END DO
357    DO l=ll_begin,ll_end
358       !DIR$ SIMD
359       DO ij=ij_begin,ij_end
360          dW(ij,l+1) = dW(ij,l+1) + W_etadot(ij,l) ! update inner+top interfaces
361          dW(ij,l)   = dW(ij,l)   - W_etadot(ij,l) ! update bottom+inner interfaces
362       END DO
363    END DO
364
365#undef ETA_DOT
366#undef WCOV
367#endif
368
369    CALL trace_end("compute_caldyn_vert_nh")
370
371  END SUBROUTINE compute_caldyn_vert_NH
372END MODULE caldyn_kernels_base_mod
Note: See TracBrowser for help on using the repository browser.