source: codes/icosagcm/devel/src/dynamics/caldyn_kernels_hevi.F90 @ 603

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

devel : output theta and momentum fluxes

File size: 37.0 KB
Line 
1MODULE caldyn_kernels_hevi_mod
2  USE icosa
3  USE trace
4  USE omp_para
5  USE disvert_mod
6  USE transfert_mod
7  USE caldyn_kernels_base_mod
8  IMPLICIT NONE
9  PRIVATE
10
11  REAL(rstd), PARAMETER :: pbot=1e5, rho_bot=1e6
12
13  LOGICAL, SAVE :: debug_hevi_solver = .FALSE.
14
15  PUBLIC :: compute_theta, compute_pvort_only, compute_caldyn_Coriolis, &
16       compute_caldyn_slow_hydro, compute_caldyn_slow_NH, &
17       compute_caldyn_solver, compute_caldyn_fast
18
19CONTAINS
20
21  SUBROUTINE compute_theta(ps,theta_rhodz, rhodz,theta)
22    REAL(rstd),INTENT(IN)    :: ps(iim*jjm)
23    REAL(rstd),INTENT(IN)    :: theta_rhodz(iim*jjm,llm,nqdyn)
24    REAL(rstd),INTENT(INOUT) :: rhodz(iim*jjm,llm)
25    REAL(rstd),INTENT(OUT)   :: theta(iim*jjm,llm,nqdyn)
26    INTEGER :: ij,l,iq
27    REAL(rstd) :: m
28    CALL trace_start("compute_theta")
29
30    IF(caldyn_eta==eta_mass) THEN ! Compute mass
31       DO l = ll_begin,ll_end
32          !DIR$ SIMD
33          DO ij=ij_begin_ext,ij_end_ext
34             m = mass_dak(l)+(ps(ij)*g+ptop)*mass_dbk(l) ! ps is actually Ms
35             rhodz(ij,l) = m/g
36          END DO
37       END DO
38    END IF
39
40    DO l = ll_begin,ll_end
41       DO iq=1,nqdyn
42          !DIR$ SIMD
43          DO ij=ij_begin_ext,ij_end_ext
44             theta(ij,l,iq) = theta_rhodz(ij,l,iq)/rhodz(ij,l)
45          END DO
46       END DO
47    END DO
48
49    CALL trace_end("compute_theta")
50  END SUBROUTINE compute_theta
51
52  SUBROUTINE compute_pvort_only(u,rhodz,qu,qv)
53    REAL(rstd),INTENT(IN)  :: u(iim*3*jjm,llm)
54    REAL(rstd),INTENT(INOUT) :: rhodz(iim*jjm,llm)
55    REAL(rstd),INTENT(OUT) :: qu(iim*3*jjm,llm)
56    REAL(rstd),INTENT(OUT) :: qv(iim*2*jjm,llm)
57
58    INTEGER :: ij,l
59    REAL(rstd) :: etav,hv,radius_m2
60
61    CALL trace_start("compute_pvort_only") 
62!!! Compute shallow-water potential vorticity
63    IF(dysl_pvort_only) THEN
64#include "../kernels/pvort_only.k90"
65    ELSE
66
67    radius_m2=radius**(-2)
68    DO l = ll_begin,ll_end
69       !DIR$ SIMD
70       DO ij=ij_begin_ext,ij_end_ext
71          etav= 1./Av(ij+z_up)*(  ne_rup  * u(ij+u_rup,l)   &
72                  + ne_left * u(ij+t_rup+u_left,l)          &
73                  - ne_lup  * u(ij+u_lup,l) )                               
74          hv =   Riv2(ij,vup)          * rhodz(ij,l)        &
75               + Riv2(ij+t_rup,vldown) * rhodz(ij+t_rup,l)  &
76               + Riv2(ij+t_lup,vrdown) * rhodz(ij+t_lup,l)
77          qv(ij+z_up,l) = ( etav+fv(ij+z_up) )/hv
78         
79          etav = 1./Av(ij+z_down)*(  ne_ldown * u(ij+u_ldown,l)   &
80               + ne_right * u(ij+t_ldown+u_right,l)               &
81               - ne_rdown * u(ij+u_rdown,l) )
82          hv =   Riv2(ij,vdown)        * rhodz(ij,l)              &
83               + Riv2(ij+t_ldown,vrup) * rhodz(ij+t_ldown,l)      &
84               + Riv2(ij+t_rdown,vlup) * rhodz(ij+t_rdown,l)
85          qv(ij+z_down,l) =( etav+fv(ij+z_down) )/hv
86       ENDDO
87
88       !DIR$ SIMD
89       DO ij=ij_begin,ij_end
90          qu(ij+u_right,l) = 0.5*(qv(ij+z_rdown,l)+qv(ij+z_rup,l)) 
91          qu(ij+u_lup,l) = 0.5*(qv(ij+z_up,l)+qv(ij+z_lup,l)) 
92          qu(ij+u_ldown,l) = 0.5*(qv(ij+z_ldown,l)+qv(ij+z_down,l)) 
93       END DO
94
95    ENDDO
96   
97    END IF ! dysl
98    CALL trace_end("compute_pvort_only")
99
100  END SUBROUTINE compute_pvort_only
101
102  SUBROUTINE compute_NH_geopot(tau, phis, m_ik, m_il, theta, W_il, Phi_il)
103    REAL(rstd),INTENT(IN)    :: tau ! solve Phi-tau*dPhi/dt = Phi_rhs
104    REAL(rstd),INTENT(IN)    :: phis(iim*jjm)
105    REAL(rstd),INTENT(IN)    :: m_ik(iim*jjm,llm)
106    REAL(rstd),INTENT(IN)    :: m_il(iim*jjm,llm+1)
107    REAL(rstd),INTENT(IN)    :: theta(iim*jjm,llm)
108    REAL(rstd),INTENT(IN)    :: W_il(iim*jjm,llm+1)   ! vertical momentum
109    REAL(rstd),INTENT(INOUT) :: Phi_il(iim*jjm,llm+1) ! geopotential
110
111    REAL(rstd) :: Phi_star_il(iim*jjm,llm+1) 
112    REAL(rstd) :: p_ik(iim*jjm,llm)      ! pressure
113    REAL(rstd) :: R_il(iim*jjm,llm+1)    ! rhs of tridiag problem
114    REAL(rstd) :: x_il(iim*jjm,llm+1)    ! solution of tridiag problem
115    REAL(rstd) :: A_ik(iim*jjm,llm)      ! off-diagonal coefficients of tridiag problem
116    REAL(rstd) :: B_il(iim*jjm,llm+1)    ! diagonal coefficients of tridiag problem
117    REAL(rstd) :: C_ik(iim*jjm,llm)      ! Thomas algorithm
118    REAL(rstd) :: D_il(iim*jjm,llm+1)    ! Thomas algorithm
119    REAL(rstd) :: gamma, rho_ij, X_ij, Y_ij
120    REAL(rstd) :: wil, tau2_g, g2, gm2, ml_g2, c2_mik
121
122    INTEGER    :: iter, ij, l, ij_omp_begin_ext, ij_omp_end_ext
123
124    CALL distrib_level(ij_begin_ext,ij_end_ext, ij_omp_begin_ext,ij_omp_end_ext)
125
126    IF(dysl) THEN
127#define PHI_BOT(ij) phis(ij)
128#include "../kernels/compute_NH_geopot.k90"
129#undef PHI_BOT
130    ELSE
131!    FIXME : vertical OpenMP parallelism will not work
132   
133    tau2_g=tau*tau/g
134    g2=g*g
135    gm2 = g**-2
136    gamma = 1./(1.-kappa)
137   
138    ! compute Phi_star
139    DO l=1,llm+1
140       !DIR$ SIMD
141       DO ij=ij_begin_ext,ij_end_ext
142          Phi_star_il(ij,l) = Phi_il(ij,l) + tau*g2*(W_il(ij,l)/m_il(ij,l)-tau)
143       ENDDO
144    ENDDO
145
146    ! Newton-Raphson iteration : Phi_il contains current guess value
147    DO iter=1,5 ! 2 iterations should be enough
148
149       ! Compute pressure, A_ik
150       DO l=1,llm
151          !DIR$ SIMD
152          DO ij=ij_begin_ext,ij_end_ext
153             rho_ij = (g*m_ik(ij,l))/(Phi_il(ij,l+1)-Phi_il(ij,l))
154             X_ij = (cpp/preff)*kappa*theta(ij,l)*rho_ij
155             p_ik(ij,l) = preff*(X_ij**gamma)
156             c2_mik = gamma*p_ik(ij,l)/(rho_ij*m_ik(ij,l)) ! c^2 = gamma*R*T = gamma*p/rho
157             A_ik(ij,l) = c2_mik*(tau/g*rho_ij)**2
158          ENDDO
159       ENDDO
160
161       ! Compute residual, B_il
162       ! bottom interface l=1
163       !DIR$ SIMD
164       DO ij=ij_begin_ext,ij_end_ext
165          ml_g2 = gm2*m_il(ij,1) 
166          B_il(ij,1) = A_ik(ij,1) + ml_g2 + tau2_g*rho_bot
167          R_il(ij,1) = ml_g2*( Phi_il(ij,1)-Phi_star_il(ij,1))  &
168               + tau2_g*( p_ik(ij,1)-pbot+rho_bot*(Phi_il(ij,1)-phis(ij)) )
169       ENDDO
170       ! inner interfaces
171       DO l=2,llm
172          !DIR$ SIMD
173          DO ij=ij_begin_ext,ij_end_ext
174             ml_g2 = gm2*m_il(ij,l) 
175             B_il(ij,l) = A_ik(ij,l)+A_ik(ij,l-1) + ml_g2
176             R_il(ij,l) = ml_g2*( Phi_il(ij,l)-Phi_star_il(ij,l))  &
177                     + tau2_g*(p_ik(ij,l)-p_ik(ij,l-1))
178             ! consistency check : if Wil=0 and initial state is in hydrostatic balance
179             ! then Phi_star_il(ij,l) = Phi_il(ij,l) - tau^2*g^2
180             ! and residual = tau^2*(ml+(1/g)dl_pi)=0
181          ENDDO
182       ENDDO
183       ! top interface l=llm+1
184       !DIR$ SIMD
185       DO ij=ij_begin_ext,ij_end_ext
186          ml_g2 = gm2*m_il(ij,llm+1) 
187          B_il(ij,llm+1) = A_ik(ij,llm) + ml_g2
188          R_il(ij,llm+1) = ml_g2*( Phi_il(ij,llm+1)-Phi_star_il(ij,llm+1))  &
189               + tau2_g*( ptop-p_ik(ij,llm) )
190       ENDDO
191
192       ! FIXME later
193       ! the lines below modify the tridiag problem
194       ! for flat, rigid boundary conditions at top and bottom :
195       ! zero out A(1), A(llm), R(1), R(llm+1)
196       ! => x(l)=0 at l=1,llm+1
197       DO ij=ij_begin_ext,ij_end_ext
198          A_ik(ij,1) = 0.
199          A_ik(ij,llm) = 0.
200          R_il(ij,1) = 0.
201          R_il(ij,llm+1) = 0.
202       ENDDO
203
204       IF(debug_hevi_solver) THEN ! print Linf(residual)
205          PRINT *, '[hevi_solver] R,p', iter, MAXVAL(ABS(R_il)), MAXVAL(p_ik)
206       END IF
207
208       ! Solve -A(l-1)x(l-1) + B(l)x(l) - A(l)x(l+1) = R(l) using Thomas algorithm
209       ! Forward sweep :
210       ! C(0)=0, C(l) = -A(l) / (B(l)+A(l-1)C(l-1)),
211       ! D(0)=0, D(l) = (R(l)+A(l-1)D(l-1)) / (B(l)+A(l-1)C(l-1))
212       ! bottom interface l=1
213       !DIR$ SIMD
214       DO ij=ij_begin_ext,ij_end_ext
215          X_ij = 1./B_il(ij,1)
216          C_ik(ij,1) = -A_ik(ij,1) * X_ij 
217          D_il(ij,1) =  R_il(ij,1) * X_ij
218       ENDDO
219       ! inner interfaces/layers
220       DO l=2,llm
221          !DIR$ SIMD
222          DO ij=ij_begin_ext,ij_end_ext
223             X_ij = 1./(B_il(ij,l) + A_ik(ij,l-1)*C_ik(ij,l-1))
224             C_ik(ij,l) = -A_ik(ij,l) * X_ij 
225             D_il(ij,l) =  (R_il(ij,l)+A_ik(ij,l-1)*D_il(ij,l-1)) * X_ij
226          ENDDO
227       ENDDO
228       ! top interface l=llm+1
229       !DIR$ SIMD
230       DO ij=ij_begin_ext,ij_end_ext
231          X_ij = 1./(B_il(ij,llm+1) + A_ik(ij,llm)*C_ik(ij,llm))
232          D_il(ij,llm+1) =  (R_il(ij,llm+1)+A_ik(ij,llm)*D_il(ij,llm)) * X_ij
233       ENDDO
234       
235       ! Back substitution :
236       ! x(i) = D(i)-C(i)x(i+1), x(N+1)=0
237       ! + Newton-Raphson update
238       x_il=0. ! FIXME
239       ! top interface l=llm+1
240       !DIR$ SIMD
241       DO ij=ij_begin_ext,ij_end_ext
242          x_il(ij,llm+1) = D_il(ij,llm+1)
243          Phi_il(ij,llm+1) = Phi_il(ij,llm+1) - x_il(ij,llm+1)
244       ENDDO
245       ! lower interfaces
246       DO l=llm,1,-1
247          !DIR$ SIMD
248          DO ij=ij_begin_ext,ij_end_ext
249             x_il(ij,l) = D_il(ij,l) - C_ik(ij,l)*x_il(ij,l+1)
250             Phi_il(ij,l) = Phi_il(ij,l) - x_il(ij,l)
251          ENDDO
252       ENDDO
253
254       IF(debug_hevi_solver) THEN
255          PRINT *, '[hevi_solver] A,B', iter, MAXVAL(ABS(A_ik)),MAXVAL(ABS(B_il))
256          PRINT *, '[hevi_solver] C,D', iter, MAXVAL(ABS(C_ik)),MAXVAL(ABS(D_il))
257          DO l=1,llm+1
258             WRITE(*,'(A,I2.1,I3.2,E9.2)'), '[hevi_solver] x', iter,l, MAXVAL(ABS(x_il(:,l)))
259          END DO
260       END IF
261
262    END DO ! Newton-Raphson
263
264    END IF ! dysl
265   
266  END SUBROUTINE compute_NH_geopot
267
268  SUBROUTINE compute_caldyn_solver(tau,phis, rhodz,theta,pk, geopot,W, m_il,pres, dPhi,dW,du)
269    REAL(rstd),INTENT(IN) :: tau ! "solve" Phi-tau*dPhi/dt = Phi_rhs
270    REAL(rstd),INTENT(IN)    :: phis(iim*jjm)
271    REAL(rstd),INTENT(IN)    :: rhodz(iim*jjm,llm)
272    REAL(rstd),INTENT(IN)    :: theta(iim*jjm,llm,nqdyn)
273    REAL(rstd),INTENT(OUT)   :: pk(iim*jjm,llm)
274    REAL(rstd),INTENT(INOUT) :: geopot(iim*jjm,llm+1)
275    REAL(rstd),INTENT(INOUT) :: W(iim*jjm,llm+1) ! OUT if tau>0
276    REAL(rstd),INTENT(OUT)   :: m_il(iim*jjm,llm+1)        ! rhodz averaged to interfaces
277    REAL(rstd),INTENT(OUT)   :: pres(iim*jjm,llm)          ! pressure
278    REAL(rstd),INTENT(OUT)   :: dW(iim*jjm,llm+1)
279    REAL(rstd),INTENT(OUT)   :: dPhi(iim*jjm,llm+1)
280    REAL(rstd),INTENT(OUT)   :: du(3*iim*jjm,llm)
281
282    REAL(rstd) :: berni(iim*jjm,llm)         ! (W/m_il)^2
283    REAL(rstd) :: berni1(iim*jjm)         ! (W/m_il)^2
284    REAL(rstd) :: gamma, rho_ij, T_ij, X_ij, Y_ij, vreff, Rd, Cvd
285    INTEGER    :: ij, l
286
287    CALL trace_start("compute_caldyn_solver")
288
289    Rd=cpp*kappa
290
291    IF(dysl) THEN
292
293!$OMP BARRIER
294#define PHI_BOT(ij) phis(ij)
295#define PHI_BOT_VAR phis
296#include "../kernels/caldyn_solver.k90"
297#undef PHI_BOT_VAR
298#undef PHI_BOT
299!$OMP BARRIER
300
301    ELSE
302
303#define BERNI(ij) berni1(ij)
304    ! FIXME : vertical OpenMP parallelism will not work
305
306    ! average m_ik to interfaces => m_il
307    !DIR$ SIMD
308    DO ij=ij_begin_ext,ij_end_ext
309       m_il(ij,1) = .5*rhodz(ij,1)
310    ENDDO
311    DO l=2,llm
312       !DIR$ SIMD
313       DO ij=ij_begin_ext,ij_end_ext
314          m_il(ij,l) = .5*(rhodz(ij,l-1)+rhodz(ij,l))
315       ENDDO
316    ENDDO
317    !DIR$ SIMD
318    DO ij=ij_begin_ext,ij_end_ext
319       m_il(ij,llm+1) = .5*rhodz(ij,llm)
320    ENDDO
321
322    IF(tau>0) THEN ! solve implicit problem for geopotential
323       CALL compute_NH_geopot(tau, phis, rhodz, m_il, theta, W, geopot)
324    END IF
325
326    ! Compute pressure, stored temporarily in pk
327    ! kappa = R/Cp
328    ! 1-kappa = Cv/Cp
329    ! Cp/Cv = 1/(1-kappa)
330    gamma = 1./(1.-kappa)
331    DO l=1,llm
332       !DIR$ SIMD
333       DO ij=ij_begin_ext,ij_end_ext
334          rho_ij = (g*rhodz(ij,l))/(geopot(ij,l+1)-geopot(ij,l))
335          X_ij = (cpp/preff)*kappa*theta(ij,l,1)*rho_ij
336          ! kappa.theta.rho = p/exner
337          ! => X = (p/p0)/(exner/Cp)
338          !      = (p/p0)^(1-kappa)
339          pk(ij,l) = preff*(X_ij**gamma)
340       ENDDO
341    ENDDO
342
343    ! Update W, compute tendencies
344    DO l=2,llm
345       !DIR$ SIMD
346       DO ij=ij_begin_ext,ij_end_ext
347          dW(ij,l)   = (1./g)*(pk(ij,l-1)-pk(ij,l)) - m_il(ij,l)
348          W(ij,l)    = W(ij,l)+tau*dW(ij,l) ! update W
349          dPhi(ij,l) = g*g*W(ij,l)/m_il(ij,l)
350       ENDDO
351       !       PRINT *,'Max dPhi', l,ij_begin,ij_end, MAXVAL(abs(dPhi(ij_begin:ij_end,l)))
352       !       PRINT *,'Max dW', l,ij_begin,ij_end, MAXVAL(abs(dW(ij_begin:ij_end,l)))
353    ENDDO
354    ! Lower BC (FIXME : no orography yet !)
355    DO ij=ij_begin,ij_end         
356       dPhi(ij,1)=0
357       W(ij,1)=0
358       dW(ij,1)=0
359       dPhi(ij,llm+1)=0 ! rigid lid
360       W(ij,llm+1)=0
361       dW(ij,llm+1)=0
362    ENDDO
363    ! Upper BC p=ptop
364    !    DO ij=ij_omp_begin_ext,ij_omp_end_ext
365    !       dPhi(ij,llm+1) = W(ij,llm+1)/rhodz(ij,llm)
366    !       dW(ij,llm+1) = (1./g)*(pk(ij,llm)-ptop) - .5*rhodz(ij,llm)
367    !    ENDDO
368   
369    ! Compute Exner function (needed by compute_caldyn_fast) and du=-g^2.grad(w^2)
370    DO l=1,llm
371       !DIR$ SIMD
372       DO ij=ij_begin_ext,ij_end_ext
373          pk(ij,l) = cpp*((pk(ij,l)/preff)**kappa) ! other formulae possible if exponentiation is slow
374          BERNI(ij) = (-.25*g*g)*(              &
375                 (W(ij,l)/m_il(ij,l))**2       &
376               + (W(ij,l+1)/m_il(ij,l+1))**2 )
377       ENDDO
378       DO ij=ij_begin,ij_end         
379          du(ij+u_right,l) = ne_right*(BERNI(ij)-BERNI(ij+t_right))
380          du(ij+u_lup,l)   = ne_lup  *(BERNI(ij)-BERNI(ij+t_lup))
381          du(ij+u_ldown,l) = ne_ldown*(BERNI(ij)-BERNI(ij+t_ldown))
382       ENDDO
383    ENDDO
384#undef BERNI
385
386    END IF ! dysl
387
388    CALL trace_end("compute_caldyn_solver")
389   
390  END SUBROUTINE compute_caldyn_solver
391 
392  SUBROUTINE compute_caldyn_fast(tau,u,rhodz,theta,pk,geopot,du)
393    REAL(rstd),INTENT(IN)    :: tau                ! "solve" u-tau*du/dt = rhs
394    REAL(rstd),INTENT(INOUT) :: u(iim*3*jjm,llm)   ! OUT if tau>0
395    REAL(rstd),INTENT(IN)    :: rhodz(iim*jjm,llm)
396    REAL(rstd),INTENT(IN)    :: theta(iim*jjm,llm,nqdyn)
397    REAL(rstd),INTENT(INOUT) :: pk(iim*jjm,llm)
398    REAL(rstd),INTENT(INOUT) :: geopot(iim*jjm,llm+1)
399    REAL(rstd),INTENT(INOUT)   :: du(iim*3*jjm,llm)
400    REAL(rstd) :: berni(iim*jjm,llm)  ! Bernoulli function
401    REAL(rstd) :: berniv(iim*jjm,llm)  ! moist Bernoulli function
402
403    INTEGER :: i,j,ij,l
404    REAL(rstd) :: Rd, qv, temp, chi, nu, due, due_right, due_lup, due_ldown
405
406    CALL trace_start("compute_caldyn_fast")
407
408    Rd=cpp*kappa
409
410    IF(dysl_caldyn_fast) THEN
411#include "../kernels/caldyn_fast.k90"
412    ELSE
413
414    ! Compute Bernoulli term
415    IF(boussinesq) THEN
416       DO l=ll_begin,ll_end
417          !DIR$ SIMD
418          DO ij=ij_begin,ij_end         
419             berni(ij,l) = pk(ij,l)
420             ! from now on pk contains the vertically-averaged geopotential
421             pk(ij,l) = .5*(geopot(ij,l)+geopot(ij,l+1))
422          END DO
423       END DO
424    ELSE ! compressible
425
426       DO l=ll_begin,ll_end
427          SELECT CASE(caldyn_thermo)
428          CASE(thermo_theta) ! vdp = theta.dpi => B = Phi
429             !DIR$ SIMD
430             DO ij=ij_begin,ij_end         
431                berni(ij,l) = .5*(geopot(ij,l)+geopot(ij,l+1))
432             END DO
433          CASE(thermo_entropy) ! vdp = dG + sdT => B = Phi + G, G=h-Ts=T*(cpp-s)
434             !DIR$ SIMD
435             DO ij=ij_begin,ij_end         
436                berni(ij,l) = .5*(geopot(ij,l)+geopot(ij,l+1)) &
437                     + pk(ij,l)*(cpp-theta(ij,l,1)) ! pk=temperature, theta=entropy
438             END DO
439          CASE(thermo_moist) 
440             !DIR$ SIMD
441             DO ij=ij_begin,ij_end         
442                ! du/dt = grad(Bd)+rv.grad(Bv)+s.grad(T)
443                ! Bd = Phi + gibbs_d
444                ! Bv = Phi + gibbs_v
445                ! pk=temperature, theta=entropy
446                qv = theta(ij,l,2)
447                temp = pk(ij,l)
448                chi = log(temp/Treff)
449                nu = (chi*(cpp+qv*cppv)-theta(ij,l,1))/(Rd+qv*Rv) ! log(p/preff)
450                berni(ij,l) = .5*(geopot(ij,l)+geopot(ij,l+1)) &
451                     + temp*(cpp*(1.-chi)+Rd*nu)
452                berniv(ij,l) = .5*(geopot(ij,l)+geopot(ij,l+1)) &
453                     + temp*(cppv*(1.-chi)+Rv*nu)
454            END DO
455          END SELECT
456       END DO
457
458    END IF ! Boussinesq/compressible
459
460!!! u:=u+tau*du, du = -grad(B)-theta.grad(pi)
461    DO l=ll_begin,ll_end
462       IF(caldyn_thermo == thermo_moist) THEN
463          !DIR$ SIMD
464          DO ij=ij_begin,ij_end         
465             due_right =  berni(ij+t_right,l)-berni(ij,l)       &
466                  + 0.5*(theta(ij,l,1)+theta(ij+t_right,l,1))   &
467                       *(pk(ij+t_right,l)-pk(ij,l))             &
468                  + 0.5*(theta(ij,l,2)+theta(ij+t_right,l,2))   &
469                       *(berniv(ij+t_right,l)-berniv(ij,l))
470             
471             due_lup = berni(ij+t_lup,l)-berni(ij,l)            &
472                  + 0.5*(theta(ij,l,1)+theta(ij+t_lup,l,1))     &
473                       *(pk(ij+t_lup,l)-pk(ij,l))               &
474                  + 0.5*(theta(ij,l,2)+theta(ij+t_lup,l,2))     &
475                       *(berniv(ij+t_lup,l)-berniv(ij,l))
476             
477             due_ldown = berni(ij+t_ldown,l)-berni(ij,l)        &
478                  + 0.5*(theta(ij,l,1)+theta(ij+t_ldown,l,1))   &
479                       *(pk(ij+t_ldown,l)-pk(ij,l))             &
480                  + 0.5*(theta(ij,l,2)+theta(ij+t_ldown,l,2))   &
481                       *(berniv(ij+t_ldown,l)-berniv(ij,l))
482             
483             du(ij+u_right,l) = du(ij+u_right,l) - ne_right*due_right
484             du(ij+u_lup,l)   = du(ij+u_lup,l)   - ne_lup*due_lup
485             du(ij+u_ldown,l) = du(ij+u_ldown,l) - ne_ldown*due_ldown
486             u(ij+u_right,l) = u(ij+u_right,l) + tau*du(ij+u_right,l)
487             u(ij+u_lup,l)   = u(ij+u_lup,l)   + tau*du(ij+u_lup,l)
488             u(ij+u_ldown,l) = u(ij+u_ldown,l) + tau*du(ij+u_ldown,l)
489          END DO
490       ELSE
491          !DIR$ SIMD
492          DO ij=ij_begin,ij_end         
493             due_right =  0.5*(theta(ij,l,1)+theta(ij+t_right,l,1))  &
494                  *(pk(ij+t_right,l)-pk(ij,l))        &
495                  +  berni(ij+t_right,l)-berni(ij,l)
496             due_lup = 0.5*(theta(ij,l,1)+theta(ij+t_lup,l,1))    &
497                  *(pk(ij+t_lup,l)-pk(ij,l))          &
498                  +  berni(ij+t_lup,l)-berni(ij,l)
499             due_ldown = 0.5*(theta(ij,l,1)+theta(ij+t_ldown,l,1)) &
500                  *(pk(ij+t_ldown,l)-pk(ij,l))      &
501                  +   berni(ij+t_ldown,l)-berni(ij,l)
502             du(ij+u_right,l) = du(ij+u_right,l) - ne_right*due_right
503             du(ij+u_lup,l)   = du(ij+u_lup,l)   - ne_lup*due_lup
504             du(ij+u_ldown,l) = du(ij+u_ldown,l) - ne_ldown*due_ldown
505             u(ij+u_right,l) = u(ij+u_right,l) + tau*du(ij+u_right,l)
506             u(ij+u_lup,l)   = u(ij+u_lup,l)   + tau*du(ij+u_lup,l)
507             u(ij+u_ldown,l) = u(ij+u_ldown,l) + tau*du(ij+u_ldown,l)
508          END DO
509       END IF
510    END DO
511
512    END IF ! dysl
513    CALL trace_end("compute_caldyn_fast")
514
515  END SUBROUTINE compute_caldyn_fast
516
517  SUBROUTINE compute_caldyn_Coriolis(hflux,theta,qu, convm,dtheta_rhodz,du)
518    REAL(rstd),INTENT(IN)  :: hflux(3*iim*jjm,llm)  ! hflux in kg/s
519    REAL(rstd),INTENT(IN)  :: theta(iim*jjm,llm,nqdyn) ! active scalars
520    REAL(rstd),INTENT(IN)  :: qu(3*iim*jjm,llm)
521    REAL(rstd),INTENT(OUT) :: convm(iim*jjm,llm)  ! mass flux convergence
522    REAL(rstd),INTENT(OUT) :: dtheta_rhodz(iim*jjm,llm,nqdyn)
523    REAL(rstd),INTENT(INOUT) :: du(3*iim*jjm,llm)
524   
525    REAL(rstd) :: Ftheta(3*iim*jjm,llm)  ! potential temperature flux
526    REAL(rstd) :: uu_right, uu_lup, uu_ldown, du_trisk, divF
527    INTEGER :: ij,iq,l,kdown
528
529    CALL trace_start("compute_caldyn_Coriolis")
530
531    IF(dysl_caldyn_coriolis) THEN
532
533#include "../kernels/coriolis.k90"
534
535    ELSE
536#define FTHETA(ij) Ftheta(ij,1)
537
538    DO l=ll_begin, ll_end
539       ! compute theta flux
540       DO iq=1,nqdyn
541       !DIR$ SIMD
542          DO ij=ij_begin_ext,ij_end_ext     
543             FTHETA(ij+u_right) = 0.5*(theta(ij,l,iq)+theta(ij+t_right,l,iq)) &
544                                  * hflux(ij+u_right,l)
545             FTHETA(ij+u_lup)   = 0.5*(theta(ij,l,iq)+theta(ij+t_lup,l,iq)) &
546                  * hflux(ij+u_lup,l)
547             FTHETA(ij+u_ldown) = 0.5*(theta(ij,l,iq)+theta(ij+t_ldown,l,iq)) &
548                  * hflux(ij+u_ldown,l)
549          END DO
550          ! horizontal divergence of fluxes
551       !DIR$ SIMD
552          DO ij=ij_begin,ij_end         
553             ! dtheta_rhodz =  -div(flux.theta)
554             dtheta_rhodz(ij,l,iq)= &
555                  -1./Ai(ij)*(ne_right*FTHETA(ij+u_right)    +  &
556                  ne_rup*FTHETA(ij+u_rup)        +  & 
557                  ne_lup*FTHETA(ij+u_lup)        +  & 
558                  ne_left*FTHETA(ij+u_left)      +  & 
559                  ne_ldown*FTHETA(ij+u_ldown)    +  &
560                  ne_rdown*FTHETA(ij+u_rdown) )
561          END DO
562       END DO
563
564       !DIR$ SIMD
565       DO ij=ij_begin,ij_end         
566          ! convm = -div(mass flux), sign convention as in Ringler et al. 2012, eq. 21
567          convm(ij,l)= -1./Ai(ij)*(ne_right*hflux(ij+u_right,l)   +  &
568               ne_rup*hflux(ij+u_rup,l)       +  & 
569               ne_lup*hflux(ij+u_lup,l)       +  & 
570               ne_left*hflux(ij+u_left,l)     +  & 
571               ne_ldown*hflux(ij+u_ldown,l)   +  & 
572               ne_rdown*hflux(ij+u_rdown,l))
573       END DO ! ij
574    END DO ! llm
575
576!!! Compute potential vorticity (Coriolis) contribution to du
577    SELECT CASE(caldyn_conserv)
578
579    CASE(energy) ! energy-conserving TRiSK
580
581       DO l=ll_begin,ll_end
582          !DIR$ SIMD
583          DO ij=ij_begin,ij_end         
584             uu_right = &
585                  wee(ij+u_right,1,1)*hflux(ij+u_rup,l)*(qu(ij+u_right,l)+qu(ij+u_rup,l))+                             &
586                  wee(ij+u_right,2,1)*hflux(ij+u_lup,l)*(qu(ij+u_right,l)+qu(ij+u_lup,l))+                             &
587                  wee(ij+u_right,3,1)*hflux(ij+u_left,l)*(qu(ij+u_right,l)+qu(ij+u_left,l))+                           &
588                  wee(ij+u_right,4,1)*hflux(ij+u_ldown,l)*(qu(ij+u_right,l)+qu(ij+u_ldown,l))+                         &
589                  wee(ij+u_right,5,1)*hflux(ij+u_rdown,l)*(qu(ij+u_right,l)+qu(ij+u_rdown,l))+                         & 
590                  wee(ij+u_right,1,2)*hflux(ij+t_right+u_ldown,l)*(qu(ij+u_right,l)+qu(ij+t_right+u_ldown,l))+         &
591                  wee(ij+u_right,2,2)*hflux(ij+t_right+u_rdown,l)*(qu(ij+u_right,l)+qu(ij+t_right+u_rdown,l))+         &
592                  wee(ij+u_right,3,2)*hflux(ij+t_right+u_right,l)*(qu(ij+u_right,l)+qu(ij+t_right+u_right,l))+         &
593                  wee(ij+u_right,4,2)*hflux(ij+t_right+u_rup,l)*(qu(ij+u_right,l)+qu(ij+t_right+u_rup,l))+             &
594                  wee(ij+u_right,5,2)*hflux(ij+t_right+u_lup,l)*(qu(ij+u_right,l)+qu(ij+t_right+u_lup,l))
595             uu_lup = &
596                  wee(ij+u_lup,1,1)*hflux(ij+u_left,l)*(qu(ij+u_lup,l)+qu(ij+u_left,l)) +                        &
597                  wee(ij+u_lup,2,1)*hflux(ij+u_ldown,l)*(qu(ij+u_lup,l)+qu(ij+u_ldown,l)) +                      &
598                  wee(ij+u_lup,3,1)*hflux(ij+u_rdown,l)*(qu(ij+u_lup,l)+qu(ij+u_rdown,l)) +                      &
599                  wee(ij+u_lup,4,1)*hflux(ij+u_right,l)*(qu(ij+u_lup,l)+qu(ij+u_right,l)) +                      &
600                  wee(ij+u_lup,5,1)*hflux(ij+u_rup,l)*(qu(ij+u_lup,l)+qu(ij+u_rup,l)) +                          & 
601                  wee(ij+u_lup,1,2)*hflux(ij+t_lup+u_right,l)*(qu(ij+u_lup,l)+qu(ij+t_lup+u_right,l)) +          &
602                  wee(ij+u_lup,2,2)*hflux(ij+t_lup+u_rup,l)*(qu(ij+u_lup,l)+qu(ij+t_lup+u_rup,l)) +              &
603                  wee(ij+u_lup,3,2)*hflux(ij+t_lup+u_lup,l)*(qu(ij+u_lup,l)+qu(ij+t_lup+u_lup,l)) +              &
604                  wee(ij+u_lup,4,2)*hflux(ij+t_lup+u_left,l)*(qu(ij+u_lup,l)+qu(ij+t_lup+u_left,l)) +            &
605                  wee(ij+u_lup,5,2)*hflux(ij+t_lup+u_ldown,l)*(qu(ij+u_lup,l)+qu(ij+t_lup+u_ldown,l))
606             uu_ldown = &
607                  wee(ij+u_ldown,1,1)*hflux(ij+u_rdown,l)*(qu(ij+u_ldown,l)+qu(ij+u_rdown,l)) +                      &
608                  wee(ij+u_ldown,2,1)*hflux(ij+u_right,l)*(qu(ij+u_ldown,l)+qu(ij+u_right,l)) +                      &
609                  wee(ij+u_ldown,3,1)*hflux(ij+u_rup,l)*(qu(ij+u_ldown,l)+qu(ij+u_rup,l)) +                          &
610                  wee(ij+u_ldown,4,1)*hflux(ij+u_lup,l)*(qu(ij+u_ldown,l)+qu(ij+u_lup,l)) +                          &
611                  wee(ij+u_ldown,5,1)*hflux(ij+u_left,l)*(qu(ij+u_ldown,l)+qu(ij+u_left,l)) +                        & 
612                  wee(ij+u_ldown,1,2)*hflux(ij+t_ldown+u_lup,l)*(qu(ij+u_ldown,l)+qu(ij+t_ldown+u_lup,l)) +          &
613                  wee(ij+u_ldown,2,2)*hflux(ij+t_ldown+u_left,l)*(qu(ij+u_ldown,l)+qu(ij+t_ldown+u_left,l)) +        &
614                  wee(ij+u_ldown,3,2)*hflux(ij+t_ldown+u_ldown,l)*(qu(ij+u_ldown,l)+qu(ij+t_ldown+u_ldown,l)) +      &
615                  wee(ij+u_ldown,4,2)*hflux(ij+t_ldown+u_rdown,l)*(qu(ij+u_ldown,l)+qu(ij+t_ldown+u_rdown,l)) +      &
616                  wee(ij+u_ldown,5,2)*hflux(ij+t_ldown+u_right,l)*(qu(ij+u_ldown,l)+qu(ij+t_ldown+u_right,l))
617             du(ij+u_right,l) = du(ij+u_right,l) + .5*uu_right
618             du(ij+u_lup,l)   = du(ij+u_lup,l)   + .5*uu_lup
619             du(ij+u_ldown,l) = du(ij+u_ldown,l)   + .5*uu_ldown
620          ENDDO
621       ENDDO
622
623    CASE(enstrophy) ! enstrophy-conserving TRiSK
624
625       DO l=ll_begin,ll_end
626          !DIR$ SIMD
627          DO ij=ij_begin,ij_end         
628             uu_right = &
629                  wee(ij+u_right,1,1)*hflux(ij+u_rup,l)+                           &
630                  wee(ij+u_right,2,1)*hflux(ij+u_lup,l)+                           &
631                  wee(ij+u_right,3,1)*hflux(ij+u_left,l)+                          &
632                  wee(ij+u_right,4,1)*hflux(ij+u_ldown,l)+                         &
633                  wee(ij+u_right,5,1)*hflux(ij+u_rdown,l)+                         & 
634                  wee(ij+u_right,1,2)*hflux(ij+t_right+u_ldown,l)+                 &
635                  wee(ij+u_right,2,2)*hflux(ij+t_right+u_rdown,l)+                 &
636                  wee(ij+u_right,3,2)*hflux(ij+t_right+u_right,l)+                 &
637                  wee(ij+u_right,4,2)*hflux(ij+t_right+u_rup,l)+                   &
638                  wee(ij+u_right,5,2)*hflux(ij+t_right+u_lup,l)
639             uu_lup = &
640                  wee(ij+u_lup,1,1)*hflux(ij+u_left,l)+                        &
641                  wee(ij+u_lup,2,1)*hflux(ij+u_ldown,l)+                       &
642                  wee(ij+u_lup,3,1)*hflux(ij+u_rdown,l)+                       &
643                  wee(ij+u_lup,4,1)*hflux(ij+u_right,l)+                       &
644                  wee(ij+u_lup,5,1)*hflux(ij+u_rup,l)+                         & 
645                  wee(ij+u_lup,1,2)*hflux(ij+t_lup+u_right,l)+                 &
646                  wee(ij+u_lup,2,2)*hflux(ij+t_lup+u_rup,l)+                   &
647                  wee(ij+u_lup,3,2)*hflux(ij+t_lup+u_lup,l)+                   &
648                  wee(ij+u_lup,4,2)*hflux(ij+t_lup+u_left,l)+                  &
649                  wee(ij+u_lup,5,2)*hflux(ij+t_lup+u_ldown,l)
650             uu_ldown = &
651                  wee(ij+u_ldown,1,1)*hflux(ij+u_rdown,l)+                      &
652                  wee(ij+u_ldown,2,1)*hflux(ij+u_right,l)+                      &
653                  wee(ij+u_ldown,3,1)*hflux(ij+u_rup,l)+                        &
654                  wee(ij+u_ldown,4,1)*hflux(ij+u_lup,l)+                        &
655                  wee(ij+u_ldown,5,1)*hflux(ij+u_left,l)+                       & 
656                  wee(ij+u_ldown,1,2)*hflux(ij+t_ldown+u_lup,l)+                &
657                  wee(ij+u_ldown,2,2)*hflux(ij+t_ldown+u_left,l)+               &
658                  wee(ij+u_ldown,3,2)*hflux(ij+t_ldown+u_ldown,l)+              &
659                  wee(ij+u_ldown,4,2)*hflux(ij+t_ldown+u_rdown,l)+              &
660                  wee(ij+u_ldown,5,2)*hflux(ij+t_ldown+u_right,l)
661
662             du(ij+u_right,l) = du(ij+u_right,l) + .5*uu_right
663             du(ij+u_lup,l)   = du(ij+u_lup,l)   + .5*uu_lup
664             du(ij+u_ldown,l) = du(ij+u_ldown,l)   + .5*uu_ldown
665          END DO
666       END DO
667    CASE DEFAULT
668       STOP
669    END SELECT
670#undef FTHETA
671
672    END IF ! dysl
673
674    CALL trace_end("compute_caldyn_Coriolis")
675
676  END SUBROUTINE compute_caldyn_Coriolis
677
678  SUBROUTINE compute_caldyn_slow_hydro(u,rhodz,hflux,du, zero)
679    LOGICAL, INTENT(IN) :: zero
680    REAL(rstd),INTENT(IN)  :: u(3*iim*jjm,llm)    ! prognostic "velocity"
681    REAL(rstd),INTENT(IN)  :: rhodz(iim*jjm,llm)
682    REAL(rstd),INTENT(OUT) :: hflux(3*iim*jjm,llm) ! hflux in kg/s
683    REAL(rstd),INTENT(INOUT) :: du(3*iim*jjm,llm)
684   
685    REAL(rstd) :: berni(iim*jjm,llm)  ! Bernoulli function
686    REAL(rstd) :: berni1(iim*jjm)  ! Bernoulli function
687    REAL(rstd) :: uu_right, uu_lup, uu_ldown, ke, uu
688    INTEGER :: ij,l
689
690    CALL trace_start("compute_caldyn_slow_hydro")
691
692    IF(dysl_slow_hydro) THEN
693
694#define BERNI(ij,l) berni(ij,l)
695#include "../kernels/caldyn_slow_hydro.k90"
696#undef BERNI
697
698     ELSE
699
700#define BERNI(ij) berni1(ij)
701
702    DO l = ll_begin, ll_end
703       !  Compute mass fluxes
704       IF (caldyn_conserv==energy) CALL test_message(req_qu) 
705       !DIR$ SIMD
706       DO ij=ij_begin_ext,ij_end_ext
707          uu_right=0.5*(rhodz(ij,l)+rhodz(ij+t_right,l))*u(ij+u_right,l)
708          uu_lup=0.5*(rhodz(ij,l)+rhodz(ij+t_lup,l))*u(ij+u_lup,l)
709          uu_ldown=0.5*(rhodz(ij,l)+rhodz(ij+t_ldown,l))*u(ij+u_ldown,l)
710          uu_right= uu_right*le_de(ij+u_right)
711          uu_lup  = uu_lup  *le_de(ij+u_lup)
712          uu_ldown= uu_ldown*le_de(ij+u_ldown)
713          hflux(ij+u_right,l)=uu_right
714          hflux(ij+u_lup,l)  =uu_lup
715          hflux(ij+u_ldown,l)=uu_ldown
716       ENDDO
717       ! Compute Bernoulli=kinetic energy
718       !DIR$ SIMD
719       DO ij=ij_begin,ij_end         
720          BERNI(ij) = &
721               1/(4*Ai(ij))*(le_de(ij+u_right)*u(ij+u_right,l)**2 +    &
722               le_de(ij+u_rup)*u(ij+u_rup,l)**2 +          &
723               le_de(ij+u_lup)*u(ij+u_lup,l)**2 +          &
724               le_de(ij+u_left)*u(ij+u_left,l)**2 +       &
725               le_de(ij+u_ldown)*u(ij+u_ldown,l)**2 +    &
726               le_de(ij+u_rdown)*u(ij+u_rdown,l)**2 ) 
727       ENDDO
728       ! Compute du=-grad(Bernoulli)
729       IF(zero) THEN
730          !DIR$ SIMD
731          DO ij=ij_begin,ij_end
732             du(ij+u_right,l) = ne_right*(BERNI(ij)-BERNI(ij+t_right))
733             du(ij+u_lup,l)   = ne_lup*(BERNI(ij)-BERNI(ij+t_lup))
734             du(ij+u_ldown,l) = ne_ldown*(BERNI(ij)-BERNI(ij+t_ldown))
735          END DO
736       ELSE
737          !DIR$ SIMD
738          DO ij=ij_begin,ij_end
739             du(ij+u_right,l) = du(ij+u_right,l) + &
740                  ne_right*(BERNI(ij)-BERNI(ij+t_right))
741             du(ij+u_lup,l)   = du(ij+u_lup,l) + &
742                  ne_lup*(BERNI(ij)-BERNI(ij+t_lup))
743             du(ij+u_ldown,l) = du(ij+u_ldown,l) + &
744                  ne_ldown*(BERNI(ij)-BERNI(ij+t_ldown))
745          END DO
746       END IF
747    END DO
748
749#undef BERNI
750
751    END IF ! dysl
752    CALL trace_end("compute_caldyn_slow_hydro")   
753  END SUBROUTINE compute_caldyn_slow_hydro
754
755  SUBROUTINE compute_caldyn_slow_NH(u,rhodz,Phi,W, F_el,gradPhi2,w_il, hflux,du,dPhi,dW)
756    REAL(rstd),INTENT(IN)  :: u(3*iim*jjm,llm)    ! prognostic "velocity"
757    REAL(rstd),INTENT(IN)  :: rhodz(iim*jjm,llm)  ! rho*dz
758    REAL(rstd),INTENT(IN)  :: Phi(iim*jjm,llm+1)  ! prognostic geopotential
759    REAL(rstd),INTENT(IN)  :: W(iim*jjm,llm+1)    ! prognostic vertical momentum
760
761    REAL(rstd),INTENT(OUT) :: hflux(3*iim*jjm,llm) ! hflux in kg/s
762    REAL(rstd),INTENT(OUT) :: du(3*iim*jjm,llm)
763    REAL(rstd),INTENT(OUT) :: dW(iim*jjm,llm+1)
764    REAL(rstd),INTENT(OUT) :: dPhi(iim*jjm,llm+1)
765
766    REAL(rstd) :: w_il(iim*jjm,llm+1) ! Wil/mil
767    REAL(rstd) :: F_el(3*iim*jjm,llm+1) ! NH mass flux
768    REAL(rstd) :: gradPhi2(iim*jjm,llm+1) ! grad_Phi**2
769    REAL(rstd) :: DePhil(3*iim*jjm,llm+1) ! grad(Phi)
770   
771    INTEGER :: ij,l,kdown,kup
772    REAL(rstd) :: W_el, W2_el, uu_right, uu_lup, uu_ldown, gPhi2, dP, divG, u2, uu
773
774    REAL(rstd) :: berni(iim*jjm,llm)  ! Bernoulli function
775    REAL(rstd) :: G_el(3*iim*jjm,llm+1) ! horizontal flux of W
776    REAL(rstd) :: v_el(3*iim*jjm,llm+1)
777
778    REAL(rstd) :: berni1(iim*jjm)  ! Bernoulli function
779    REAL(rstd) :: G_el1(3*iim*jjm) ! horizontal flux of W
780    REAL(rstd) :: v_el1(3*iim*jjm)
781
782    CALL trace_start("compute_caldyn_slow_NH")
783
784    IF(dysl) THEN
785
786!$OMP BARRIER
787#include "../kernels/caldyn_slow_NH.k90"
788!$OMP BARRIER
789     
790     ELSE
791
792#define BERNI(ij) berni1(ij)
793#define G_EL(ij) G_el1(ij)
794#define V_EL(ij) v_el1(ij)
795
796    DO l=ll_begin, ll_endp1 ! compute on l levels (interfaces)
797       IF(l==1) THEN
798          kdown=1
799       ELSE
800          kdown=l-1
801       END IF
802       IF(l==llm+1) THEN
803          kup=llm
804       ELSE
805          kup=l
806       END IF
807       ! below : "checked" means "formula also valid when kup=kdown (top/bottom)"
808       ! compute mil, wil=Wil/mil
809       DO ij=ij_begin_ext, ij_end_ext
810          w_il(ij,l) = 2.*W(ij,l)/(rhodz(ij,kdown)+rhodz(ij,kup)) ! checked
811       END DO
812       ! compute DePhi, v_el, G_el, F_el
813       ! v_el, W2_el and therefore G_el incorporate metric factor le_de
814       ! while DePhil, W_el and F_el don't
815       DO ij=ij_begin_ext, ij_end_ext
816          ! Compute on edge 'right'
817          W_el  = .5*( W(ij,l)+W(ij+t_right,l) )
818          DePhil(ij+u_right,l) = ne_right*(Phi(ij+t_right,l)-Phi(ij,l))
819          F_el(ij+u_right,l)   = DePhil(ij+u_right,l)*W_el
820          W2_el = .5*le_de(ij+u_right) * &
821               ( W(ij,l)*w_il(ij,l) + W(ij+t_right,l)*w_il(ij+t_right,l) )
822          V_EL(ij+u_right) = .5*le_de(ij+u_right)*(u(ij+u_right,kup)+u(ij+u_right,kdown)) ! checked
823          G_EL(ij+u_right) = V_EL(ij+u_right)*W_el - DePhil(ij+u_right,l)*W2_el
824          ! Compute on edge 'lup'
825          W_el  = .5*( W(ij,l)+W(ij+t_lup,l) )
826          DePhil(ij+u_lup,l) = ne_lup*(Phi(ij+t_lup,l)-Phi(ij,l))
827          F_el(ij+u_lup,l)   = DePhil(ij+u_lup,l)*W_el
828          W2_el = .5*le_de(ij+u_lup) * &
829               ( W(ij,l)*w_il(ij,l) + W(ij+t_lup,l)*w_il(ij+t_lup,l) )
830          V_EL(ij+u_lup) = .5*le_de(ij+u_lup)*( u(ij+u_lup,kup) + u(ij+u_lup,kdown)) ! checked
831          G_EL(ij+u_lup) = V_EL(ij+u_lup)*W_el - DePhil(ij+u_lup,l)*W2_el
832          ! Compute on edge 'ldown'
833          W_el  = .5*( W(ij,l)+W(ij+t_ldown,l) )
834          DePhil(ij+u_ldown,l) = ne_ldown*(Phi(ij+t_ldown,l)-Phi(ij,l))
835          F_el(ij+u_ldown,l) = DePhil(ij+u_ldown,l)*W_el
836          W2_el = .5*le_de(ij+u_ldown) * &
837               ( W(ij,l)*w_il(ij,l) + W(ij+t_ldown,l)*w_il(ij+t_ldown,l) )
838          V_EL(ij+u_ldown) = .5*le_de(ij+u_ldown)*( u(ij+u_ldown,kup) + u(ij+u_ldown,kdown)) ! checked
839          G_EL(ij+u_ldown) = V_EL(ij+u_ldown)*W_el - DePhil(ij+u_ldown,l)*W2_el
840       END DO
841       ! compute GradPhi2, dPhi, dW
842       DO ij=ij_begin_ext, ij_end_ext
843          gradPhi2(ij,l) = &
844               1/(2*Ai(ij))*(le_de(ij+u_right)*DePhil(ij+u_right,l)**2 + &
845               le_de(ij+u_rup)*DePhil(ij+u_rup,l)**2 +      &
846               le_de(ij+u_lup)*DePhil(ij+u_lup,l)**2 +      &
847               le_de(ij+u_left)*DePhil(ij+u_left,l)**2 +    &
848               le_de(ij+u_ldown)*DePhil(ij+u_ldown,l)**2 +  &
849               le_de(ij+u_rdown)*DePhil(ij+u_rdown,l)**2 )
850
851          dPhi(ij,l) = gradPhi2(ij,l)*w_il(ij,l) -1/(2*Ai(ij))* & 
852               ( DePhil(ij+u_right,l)*V_EL(ij+u_right) + & ! -v.gradPhi,
853                 DePhil(ij+u_rup,l)*V_EL(ij+u_rup) +     & ! v_el already has le_de
854                 DePhil(ij+u_lup,l)*V_EL(ij+u_lup) +     &
855                 DePhil(ij+u_left,l)*V_EL(ij+u_left) +   &
856                 DePhil(ij+u_ldown,l)*V_EL(ij+u_ldown) + &
857                 DePhil(ij+u_rdown,l)*V_EL(ij+u_rdown) )
858
859          dW(ij,l) = -1./Ai(ij)*(           & ! -div(G_el),
860               ne_right*G_EL(ij+u_right) +  & ! G_el already has le_de
861               ne_rup*G_EL(ij+u_rup) +      &
862               ne_lup*G_EL(ij+u_lup) +      & 
863               ne_left*G_EL(ij+u_left) +    &
864               ne_ldown*G_EL(ij+u_ldown) +  &
865               ne_rdown*G_EL(ij+u_rdown))
866       END DO
867    END DO
868
869    DO l=ll_begin, ll_end ! compute on k levels (layers)
870       ! Compute berni at scalar points
871       DO ij=ij_begin_ext, ij_end_ext
872          BERNI(ij) = &
873               1/(4*Ai(ij))*( &
874                    le_de(ij+u_right)*u(ij+u_right,l)**2 +    &
875                    le_de(ij+u_rup)*u(ij+u_rup,l)**2 +          &
876                    le_de(ij+u_lup)*u(ij+u_lup,l)**2 +          &
877                    le_de(ij+u_left)*u(ij+u_left,l)**2 +       &
878                    le_de(ij+u_ldown)*u(ij+u_ldown,l)**2 +    &
879                    le_de(ij+u_rdown)*u(ij+u_rdown,l)**2 ) &
880               - .25*( gradPhi2(ij,l)  *w_il(ij,l)**2 +   &
881                      gradPhi2(ij,l+1)*w_il(ij,l+1)**2 )
882       END DO
883       ! Compute mass flux and grad(berni) at edges
884       DO ij=ij_begin_ext, ij_end_ext
885          ! Compute on edge 'right'
886          uu_right = 0.5*(rhodz(ij,l)+rhodz(ij+t_right,l))*u(ij+u_right,l) &
887                    -0.5*(F_el(ij+u_right,l)+F_el(ij+u_right,l+1))
888          hflux(ij+u_right,l) = uu_right*le_de(ij+u_right)
889          du(ij+u_right,l) = ne_right*(BERNI(ij)-BERNI(ij+t_right))
890          ! Compute on edge 'lup'
891          uu_lup = 0.5*(rhodz(ij,l)+rhodz(ij+t_lup,l))*u(ij+u_lup,l) &
892                  -0.5*(F_el(ij+u_lup,l)+F_el(ij+u_lup,l+1))
893          hflux(ij+u_lup,l) = uu_lup*le_de(ij+u_lup)
894          du(ij+u_lup,l) = ne_lup*(BERNI(ij)-BERNI(ij+t_lup))
895          ! Compute on edge 'ldown'
896          uu_ldown = 0.5*(rhodz(ij,l)+rhodz(ij+t_ldown,l))*u(ij+u_ldown,l) &
897                    -0.5*(F_el(ij+u_ldown,l)+F_el(ij+u_ldown,l+1))
898          hflux(ij+u_ldown,l) = uu_ldown*le_de(ij+u_ldown)
899          du(ij+u_ldown,l) = ne_ldown*(BERNI(ij)-BERNI(ij+t_ldown))
900       END DO
901    END DO
902
903#undef V_EL
904#undef G_EL
905#undef BERNI
906
907    END IF ! dysl
908
909    CALL trace_end("compute_caldyn_slow_NH")
910
911  END SUBROUTINE compute_caldyn_slow_NH
912
913END MODULE caldyn_kernels_hevi_mod
Note: See TracBrowser for help on using the repository browser.