source: codes/icosagcm/devel/src/dynamics/compute_caldyn_solver.F90 @ 928

Last change on this file since 928 was 928, checked in by dubos, 5 years ago

devel : separate modules for caldyn_vert and caldyn_vert_NH

File size: 8.0 KB
Line 
1MODULE compute_caldyn_solver_mod
2  USE grid_param, ONLY : llm
3  IMPLICIT NONE
4  PRIVATE
5
6#include "../unstructured/unstructured.h90"
7
8  PUBLIC :: compute_caldyn_solver
9
10CONTAINS
11
12#ifdef BEGIN_DYSL
13
14KERNEL(caldyn_solver)
15  !   
16  ! Compute pressure (pres) and Exner function (pk)
17  ! kappa = R/Cp
18  ! 1-kappa = Cv/Cp
19  ! Cp/Cv = 1/(1-kappa)
20  gamma    = 1./(1.-kappa)
21  vreff    = Rd*Treff/preff ! reference specific volume
22  Cvd      = 1./(cpp-Rd)
23  Rd_preff = kappa*cpp/preff
24  FORALL_CELLS_EXT()
25    SELECT CASE(caldyn_thermo)
26    CASE(thermo_theta)
27      ON_PRIMAL
28        rho_ij = 1./(geopot(UP(CELL))-geopot(CELL))
29        rho_ij = rho_ij*g*rhodz(CELL)
30        X_ij = Rd_preff*theta(CELL,1)*rho_ij
31        ! kappa.theta.rho = p/exner
32        ! => X = (p/p0)/(exner/Cp)
33        !      = (p/p0)^(1-kappa)
34        pres(CELL) = preff*(X_ij**gamma)  ! pressure
35        ! Compute Exner function (needed by compute_caldyn_fast)
36        ! other formulae possible if exponentiation is slow
37        pk(CELL)   = cpp*((pres(CELL)/preff)**kappa) ! Exner
38      END_BLOCK
39    CASE(thermo_entropy)
40      ON_PRIMAL
41        rho_ij = 1./(geopot(UP(CELL))-geopot(CELL))
42        rho_ij = rho_ij*g*rhodz(CELL)
43        T_ij = Treff*exp( (theta(CELL,1)+Rd*log(vreff*rho_ij))*Cvd )
44        pres(CELL) = rho_ij*Rd*T_ij
45        pk(CELL)   = T_ij
46      END_BLOCK
47    CASE DEFAULT
48      STOP
49    END SELECT
50  END_BLOCK
51
52  ! We need a barrier here because we compute pres above and do a vertical difference below
53  BARRIER
54
55  FORALL_CELLS_EXT('1', 'llm+1')
56    ON_PRIMAL
57      CST_IFTHEN(IS_BOTTOM_LEVEL)
58        ! Lower BC
59        dW(CELL)   = (1./g)*(pbot-rho_bot*(geopot(CELL)-PHI_BOT(HIDX(CELL)))-pres(CELL)) - m_il(CELL)
60      CST_ELSEIF(IS_TOP_INTERFACE)
61        ! Top BC
62        dW(CELL)   = (1./g)*(pres(DOWN(CELL))-ptop) - m_il(CELL)
63      CST_ELSE
64        dW(CELL)   = (1./g)*(pres(DOWN(CELL))-pres(CELL)) - m_il(CELL)
65      CST_ENDIF
66      W(CELL)    = W(CELL)+tau*dW(CELL) ! update W
67      dPhi(CELL) = g*g*W(CELL)/m_il(CELL)
68    END_BLOCK
69  END_BLOCK
70
71  ! We need a barrier here because we update W above and do a vertical average below
72  BARRIER
73
74  FORALL_CELLS_EXT()
75    ON_PRIMAL
76      ! compute du = -0.5*g^2.grad(w^2)
77      berni(CELL) = (-.25*g*g)*((W(CELL)/m_il(CELL))**2 + (W(UP(CELL))/m_il(UP(CELL)))**2 )
78    END_BLOCK
79  END_BLOCK
80  FORALL_CELLS_EXT()
81    ON_EDGES
82      du(EDGE) = SIGN*(berni(CELL1)-berni(CELL2))
83    END_BLOCK
84  END_BLOCK
85END_BLOCK
86
87#endif END_DYSL
88
89SUBROUTINE compute_caldyn_solver_unst(tau,rhodz,theta, berni,pres,m_il, pk,geopot,W,dPhi,dW,du)
90  USE ISO_C_BINDING, only : C_DOUBLE, C_FLOAT
91  USE earth_const
92  USE trace
93  USE grid_param, ONLY : nqdyn
94  USE disvert_mod, ONLY : ptop
95  USE data_unstructured_mod, ONLY : id_solver,primal_num,dual_num,edge_num,left, right,PHI_BOT, &
96    enter_trace, exit_trace
97  USE compute_NH_geopot_mod, ONLY : compute_NH_geopot_unst
98  NUM, INTENT(IN) :: tau 
99  FIELD_MASS   :: rhodz,pk,berni,pres    ! IN, OUT, BUF*2
100  FIELD_THETA  :: theta                  ! IN
101  FIELD_GEOPOT :: geopot,W,dPhi,dW, m_il ! INOUT,INOUT, OUT,OUT, BUF
102  FIELD_U      :: du                     ! OUT
103  DECLARE_INDICES
104  NUM :: X_ij, rho_ij, T_ij, gamma, Cvd, vreff, Rd_preff
105  REAL(rstd), PARAMETER :: pbot=1e5, rho_bot=1e6  ! FIXME
106#define PHI_BOT(ij) Phi_bot
107#include "../kernels_unst/caldyn_mil.k90"
108  IF(tau>0) THEN ! solve implicit problem for geopotential
109    CALL compute_NH_geopot_unst(tau, rhodz, m_il, theta, W, geopot)
110  END IF
111  START_TRACE(id_solver, 7,0,1)
112#include "../kernels_unst/caldyn_solver.k90"
113  STOP_TRACE
114#undef PHI_BOT
115END SUBROUTINE compute_caldyn_solver_unst
116
117  SUBROUTINE compute_caldyn_solver(tau,phis, rhodz,theta,pk, geopot,W, m_il,pres, dPhi,dW,du)
118    USE icosa
119    USE caldyn_vars_mod
120    USE trace
121    USE omp_para, ONLY : ll_begin, ll_end,ll_beginp1,ll_endp1
122    USE disvert_mod, ONLY : ptop
123    USE compute_NH_geopot_mod
124    REAL(rstd), PARAMETER :: pbot=1e5, rho_bot=1e6
125    REAL(rstd),INTENT(IN) :: tau ! "solve" Phi-tau*dPhi/dt = Phi_rhs
126    REAL(rstd),INTENT(IN)    :: phis(iim*jjm)
127    REAL(rstd),INTENT(IN)    :: rhodz(iim*jjm,llm)
128    REAL(rstd),INTENT(IN)    :: theta(iim*jjm,llm,nqdyn)
129    REAL(rstd),INTENT(OUT)   :: pk(iim*jjm,llm)
130    REAL(rstd),INTENT(INOUT) :: geopot(iim*jjm,llm+1)
131    REAL(rstd),INTENT(INOUT) :: W(iim*jjm,llm+1) ! OUT if tau>0
132    REAL(rstd),INTENT(OUT)   :: m_il(iim*jjm,llm+1)        ! rhodz averaged to interfaces
133    REAL(rstd),INTENT(OUT)   :: pres(iim*jjm,llm)          ! pressure
134    REAL(rstd),INTENT(OUT)   :: dW(iim*jjm,llm+1)
135    REAL(rstd),INTENT(OUT)   :: dPhi(iim*jjm,llm+1)
136    REAL(rstd),INTENT(OUT)   :: du(3*iim*jjm,llm)
137
138    REAL(rstd) :: berni(iim*jjm,llm)         ! (W/m_il)^2
139    REAL(rstd) :: berni1(iim*jjm)         ! (W/m_il)^2
140    !REAL(rstd) :: gamma, rho_ij, T_ij, X_ij, Y_ij, vreff, Rd, Cvd, Rd_preff
141    REAL(rstd) :: gamma, rho_ij, T_ij, X_ij, Y_ij, vreff, Cvd, Rd_preff
142    INTEGER    :: ij, l
143
144    CALL trace_start("compute_caldyn_solver")
145
146    !Rd=cpp*kappa
147
148    IF(dysl) THEN
149
150!$OMP BARRIER
151
152#include "../kernels_hex/caldyn_mil.k90"
153  IF(tau>0) THEN ! solve implicit problem for geopotential
154    CALL compute_NH_geopot(tau,phis, rhodz, m_il, theta, W, geopot)
155  END IF
156#define PHI_BOT(ij) phis(ij)
157#include "../kernels_hex/caldyn_solver.k90"
158#undef PHI_BOT
159!$OMP BARRIER
160
161    ELSE
162
163#define BERNI(ij) berni1(ij)
164    ! FIXME : vertical OpenMP parallelism will not work
165
166    ! average m_ik to interfaces => m_il
167    !DIR$ SIMD
168    DO ij=ij_begin_ext,ij_end_ext
169       m_il(ij,1) = .5*rhodz(ij,1)
170    ENDDO
171    DO l=2,llm
172       !DIR$ SIMD
173       DO ij=ij_begin_ext,ij_end_ext
174          m_il(ij,l) = .5*(rhodz(ij,l-1)+rhodz(ij,l))
175       ENDDO
176    ENDDO
177    !DIR$ SIMD
178    DO ij=ij_begin_ext,ij_end_ext
179       m_il(ij,llm+1) = .5*rhodz(ij,llm)
180    ENDDO
181
182    IF(tau>0) THEN ! solve implicit problem for geopotential
183       CALL compute_NH_geopot(tau, phis, rhodz, m_il, theta, W, geopot)
184    END IF
185
186    ! Compute pressure, stored temporarily in pk
187    ! kappa = R/Cp
188    ! 1-kappa = Cv/Cp
189    ! Cp/Cv = 1/(1-kappa)
190    gamma = 1./(1.-kappa)
191    DO l=1,llm
192       !DIR$ SIMD
193       DO ij=ij_begin_ext,ij_end_ext
194          rho_ij = (g*rhodz(ij,l))/(geopot(ij,l+1)-geopot(ij,l))
195          X_ij = (cpp/preff)*kappa*theta(ij,l,1)*rho_ij
196          ! kappa.theta.rho = p/exner
197          ! => X = (p/p0)/(exner/Cp)
198          !      = (p/p0)^(1-kappa)
199          pk(ij,l) = preff*(X_ij**gamma)
200       ENDDO
201    ENDDO
202
203    ! Update W, compute tendencies
204    DO l=2,llm
205       !DIR$ SIMD
206       DO ij=ij_begin_ext,ij_end_ext
207          dW(ij,l)   = (1./g)*(pk(ij,l-1)-pk(ij,l)) - m_il(ij,l)
208          W(ij,l)    = W(ij,l)+tau*dW(ij,l) ! update W
209          dPhi(ij,l) = g*g*W(ij,l)/m_il(ij,l)
210       ENDDO
211       !       PRINT *,'Max dPhi', l,ij_begin,ij_end, MAXVAL(abs(dPhi(ij_begin:ij_end,l)))
212       !       PRINT *,'Max dW', l,ij_begin,ij_end, MAXVAL(abs(dW(ij_begin:ij_end,l)))
213    ENDDO
214    ! Lower BC (FIXME : no orography yet !)
215    DO ij=ij_begin,ij_end         
216       dPhi(ij,1)=0
217       W(ij,1)=0
218       dW(ij,1)=0
219       dPhi(ij,llm+1)=0 ! rigid lid
220       W(ij,llm+1)=0
221       dW(ij,llm+1)=0
222    ENDDO
223    ! Upper BC p=ptop
224    !    DO ij=ij_omp_begin_ext,ij_omp_end_ext
225    !       dPhi(ij,llm+1) = W(ij,llm+1)/rhodz(ij,llm)
226    !       dW(ij,llm+1) = (1./g)*(pk(ij,llm)-ptop) - .5*rhodz(ij,llm)
227    !    ENDDO
228   
229    ! Compute Exner function (needed by compute_caldyn_fast) and du=-g^2.grad(w^2)
230    DO l=1,llm
231       !DIR$ SIMD
232       DO ij=ij_begin_ext,ij_end_ext
233          pk(ij,l) = cpp*((pk(ij,l)/preff)**kappa) ! other formulae possible if exponentiation is slow
234          BERNI(ij) = (-.25*g*g)*(              &
235                 (W(ij,l)/m_il(ij,l))**2       &
236               + (W(ij,l+1)/m_il(ij,l+1))**2 )
237       ENDDO
238       DO ij=ij_begin,ij_end         
239          du(ij+u_right,l) = ne_right*(BERNI(ij)-BERNI(ij+t_right))
240          du(ij+u_lup,l)   = ne_lup  *(BERNI(ij)-BERNI(ij+t_lup))
241          du(ij+u_ldown,l) = ne_ldown*(BERNI(ij)-BERNI(ij+t_ldown))
242       ENDDO
243    ENDDO
244#undef BERNI
245
246    END IF ! dysl
247
248    CALL trace_end("compute_caldyn_solver")
249   
250  END SUBROUTINE compute_caldyn_solver
251 
252END MODULE compute_caldyn_solver_mod
Note: See TracBrowser for help on using the repository browser.