source: codes/icosagcm/devel/src/dynamics/caldyn_kernels_base.F90 @ 557

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

devel : macro-generated kernels for caldyn_coriolis, compute_NH_geopot, caldyn_solver, caldyn_vert_NH

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