PHYEX 0.9.5
PHYsique EXternalisée
Loading...
Searching...
No Matches
mode_budget_offline.F90
Go to the documentation of this file.
1!MNH_LIC Copyright 1994-2026 CNRS, Meteo-France and Universite Paul Sabatier
2!MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
3!MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4!MNH_LIC for details. version 1.
5!-----------------------------------------------------------------
7!
8!> @file
9!! MODE_BUDGET_OFFLINE - simple implementation of the budget data structure for offline use
10!!
11!! The contributions of the different processes are stored in a module array, as long as
12!! budgets are not read (and reset) with BUDGET_OFFLINE_GET. This array is indexed by
13!! entries, an entry being a (budget variable, process name) couple. Entries are registered
14!! the first time they are encountered.
15!!
16!! The intended use (python binding) is:
17!! - BUDGET_OFFLINE_CONF to set the budget configuration (TBUCONF)
18!! - call to a parametrisation with TBUDGETS objects of type TBUDGETDATA_OFFLINE
19!! - BUDGET_OFFLINE_GET_SIZE to get the number of entries
20!! - BUDGET_OFFLINE_GET_NAMES to get the budget variable number and the process name
21!! of each entry
22!! - BUDGET_OFFLINE_GET to get the contributions (with the units of PVARS of the
23!! INIT_PHY, END_PHY and ADD_PHY procedures) and to reset the budgets
24!!
25!! The INIT_PHY/END_PHY pairs contribute with the difference between the field given
26!! to END_PHY and the field given to INIT_PHY; ADD_PHY adds directly the field.
27!! In all cases, contributions are accumulated until the next call to BUDGET_OFFLINE_GET.
28!!
29!! AUTHOR
30!! ------
31!! S. Riette
32!!
33!! MODIFICATIONS
34!! -------------
35!! Original Nov 2026
36!
38!
39IMPLICIT NONE
40!
42 CONTAINS
43 PROCEDURE :: init_phy => budget_init
44 PROCEDURE :: end_phy => budget_end
45 PROCEDURE :: add_phy => budget_add
47!
48INTEGER, PARAMETER :: nbuoff_max=200 ! Maximum number of entries
49INTEGER, PARAMETER :: nbuoff_namelen=16 ! Length of process names
50!
51INTEGER :: nbuoff_n=0 ! Number of registered entries
52INTEGER, DIMENSION(NBUOFF_MAX) :: nbuoff_budget=0 ! Budget variable of each entry
53CHARACTER(LEN=NBUOFF_NAMELEN), DIMENSION(NBUOFF_MAX) :: cbuoff_name=' ' ! Process name of each entry
54REAL, DIMENSION(:,:,:), ALLOCATABLE :: xbuoff_tend ! Accumulated contributions
55REAL, DIMENSION(:,:,:), ALLOCATABLE :: xbuoff_init ! Fields given to INIT_PHY
56CHARACTER(LEN=NBUOFF_NAMELEN), DIMENSION(NBUDGET_RH) :: cbuoff_pending=' ' ! Process name of
57 ! the INIT_PHY waiting for its END_PHY
58!
59PRIVATE
62!
63CONTAINS
64!
65SUBROUTINE budget_init(SELF, D, HSOURCE, PVARS)
66 USE modd_dimphyex, ONLY: dimphyex_t
67 USE modd_io, ONLY: nverb_fatal
68 USE mode_msg, ONLY: print_msg
69 CLASS(tbudgetdata_offline), INTENT(INOUT) :: self
70 TYPE(dimphyex_t), INTENT(IN) :: d
71 CHARACTER(LEN=*), INTENT(IN) :: hsource
72 REAL, DIMENSION(D%NIJT,D%NKT), INTENT(IN) :: pvars
73 !
74 CHARACTER(LEN=200) :: clmsg
75 !
76 CALL check_alloc(d, self%NBUDGET, 'BUDGET_INIT')
77 IF (cbuoff_pending(self%NBUDGET) /= ' ') THEN
78 clmsg='INIT_PHY called for ' // trim(hsource) // &
79 &' whereas the INIT_PHY for ' // trim(cbuoff_pending(self%NBUDGET)) // &
80 &' is still waiting for its END_PHY'
81 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_INIT', clmsg)
82 ENDIF
83 cbuoff_pending(self%NBUDGET)=hsource
84 xbuoff_init(:, :, self%NBUDGET)=pvars(:, :)
85END SUBROUTINE budget_init
86!
87SUBROUTINE budget_end(SELF, D, HSOURCE, PVARS)
88 USE modd_dimphyex, ONLY: dimphyex_t
89 USE modd_io, ONLY: nverb_fatal
90 USE mode_msg, ONLY: print_msg
91 CLASS(tbudgetdata_offline), INTENT(INOUT) :: self
92 TYPE(dimphyex_t), INTENT(IN) :: d
93 CHARACTER(LEN=*), INTENT(IN) :: hsource
94 REAL, DIMENSION(D%NIJT,D%NKT), INTENT(IN) :: pvars
95 !
96 INTEGER :: ientry
97 CHARACTER(LEN=200) :: clmsg
98 !
99 CALL check_alloc(d, self%NBUDGET, 'BUDGET_END')
100 IF (cbuoff_pending(self%NBUDGET) /= hsource) THEN
101 clmsg='END_PHY called for ' // trim(hsource) // &
102 &' whereas the pending INIT_PHY is for "' // trim(cbuoff_pending(self%NBUDGET)) // '"'
103 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_END', clmsg)
104 ENDIF
105 cbuoff_pending(self%NBUDGET)=' '
106 ientry=get_entry(self%NBUDGET, hsource)
107 xbuoff_tend(:, :, ientry)=xbuoff_tend(:, :, ientry) + pvars(:, :) - xbuoff_init(:, :, self%NBUDGET)
108END SUBROUTINE budget_end
109!
110SUBROUTINE budget_add(SELF, D, HSOURCE, PVARS)
111 USE modd_dimphyex, ONLY: dimphyex_t
112 CLASS(tbudgetdata_offline), INTENT(INOUT) :: self
113 TYPE(dimphyex_t), INTENT(IN) :: d
114 CHARACTER(LEN=*), INTENT(IN) :: hsource
115 REAL, DIMENSION(D%NIJT,D%NKT), INTENT(IN) :: pvars
116 !
117 INTEGER :: ientry
118 !
119 CALL check_alloc(d, self%NBUDGET, 'BUDGET_ADD')
120 ientry=get_entry(self%NBUDGET, hsource)
121 xbuoff_tend(:, :, ientry)=xbuoff_tend(:, :, ientry) + pvars(:, :)
122END SUBROUTINE budget_add
123!
124SUBROUTINE check_alloc(D, KBUDGET, HSUB)
125 !Allocation of the module arrays, and consistency checks
126 USE modd_dimphyex, ONLY: dimphyex_t
127 USE modd_io, ONLY: nverb_fatal
128 USE mode_msg, ONLY: print_msg
129 TYPE(dimphyex_t), INTENT(IN) :: d
130 INTEGER, INTENT(IN) :: kbudget
131 CHARACTER(LEN=*), INTENT(IN) :: hsub
132 !
133 IF (kbudget < 1 .OR. kbudget > nbudget_rh) THEN
134 CALL print_msg(nverb_fatal, 'GEN', hsub, 'this budget variable is not handled')
135 ENDIF
136 IF (ALLOCATED(xbuoff_tend)) THEN
137 IF (SIZE(xbuoff_tend, 1) /= d%NIJT .OR. SIZE(xbuoff_tend, 2) /= d%NKT) THEN
138 IF (nbuoff_n == 0 .AND. all(cbuoff_pending(:) == ' ')) THEN
139 !Budgets have been read, dimensions can change
140 DEALLOCATE(xbuoff_tend, xbuoff_init)
141 ELSE
142 CALL print_msg(nverb_fatal, 'GEN', hsub, &
143 &'dimensions have changed whereas budgets have not been read')
144 ENDIF
145 ENDIF
146 ENDIF
147 IF (.NOT. ALLOCATED(xbuoff_tend)) THEN
148 ALLOCATE(xbuoff_tend(d%NIJT, d%NKT, nbuoff_max))
149 ALLOCATE(xbuoff_init(d%NIJT, d%NKT, nbudget_rh))
150 xbuoff_tend(:, :, :)=0.
151 ENDIF
152END SUBROUTINE check_alloc
153!
154FUNCTION get_entry(KBUDGET, HSOURCE) RESULT(KENTRY)
155 !Returns the entry corresponding to (KBUDGET, HSOURCE), a new entry is registered if needed
156 USE modd_io, ONLY: nverb_fatal
157 USE mode_msg, ONLY: print_msg
158 INTEGER, INTENT(IN) :: kbudget
159 CHARACTER(LEN=*), INTENT(IN) :: hsource
160 INTEGER :: kentry
161 !
162 INTEGER :: jentry
163 CHARACTER(LEN=200) :: clmsg
164 !
165 IF (len_trim(hsource) > nbuoff_namelen) THEN
166 clmsg='process name too long: ' // trim(hsource)
167 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE', clmsg)
168 ENDIF
169 kentry=0
170 DO jentry=1, nbuoff_n
171 IF (nbuoff_budget(jentry) == kbudget .AND. cbuoff_name(jentry) == hsource) THEN
172 kentry=jentry
173 EXIT
174 ENDIF
175 ENDDO
176 IF (kentry == 0) THEN
177 IF (nbuoff_n == nbuoff_max) THEN
178 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE', 'NBUOFF_MAX is too small')
179 ENDIF
181 kentry=nbuoff_n
182 nbuoff_budget(kentry)=kbudget
183 cbuoff_name(kentry)=hsource
184 xbuoff_tend(:, :, kentry)=0.
185 ENDIF
186END FUNCTION get_entry
187!
188SUBROUTINE budget_offline_conf(KBUDGETS, OBUDGET)
189 !Sets the budget configuration (TBUCONF of MODD_BUDGET)
190 !OBUDGET(JBU) must be .TRUE. to compute the budget of the variable whose number is JBU
191 !(NBUDGET_U, NBUDGET_V... of MODD_BUDGET)
195 USE modd_io, ONLY: nverb_fatal
196 USE mode_msg, ONLY: print_msg
197 INTEGER, INTENT(IN) :: kbudgets
198 LOGICAL, DIMENSION(KBUDGETS), INTENT(IN) :: obudget
199 !
200 LOGICAL, DIMENSION(NBUDGET_RH) :: gbudget
201 !
202 IF (kbudgets > nbudget_rh) THEN
203 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE_CONF', 'only budgets up to NBUDGET_RH are handled')
204 ENDIF
205 gbudget(:)=.false.
206 gbudget(1:kbudgets)=obudget(:)
208 tbuconf%LBU_ENABLE=any(gbudget)
209 tbuconf%LBUDGET_U=gbudget(nbudget_u)
210 tbuconf%LBUDGET_V=gbudget(nbudget_v)
211 tbuconf%LBUDGET_W=gbudget(nbudget_w)
212 tbuconf%LBUDGET_TH=gbudget(nbudget_th)
213 tbuconf%LBUDGET_TKE=gbudget(nbudget_tke)
214 tbuconf%LBUDGET_RV=gbudget(nbudget_rv)
215 tbuconf%LBUDGET_RC=gbudget(nbudget_rc)
216 tbuconf%LBUDGET_RR=gbudget(nbudget_rr)
217 tbuconf%LBUDGET_RI=gbudget(nbudget_ri)
218 tbuconf%LBUDGET_RS=gbudget(nbudget_rs)
219 tbuconf%LBUDGET_RG=gbudget(nbudget_rg)
220 tbuconf%LBUDGET_RH=gbudget(nbudget_rh)
221 tbuconf%LBUDGET_SV=.false.
222END SUBROUTINE budget_offline_conf
223!
224SUBROUTINE budget_offline_get_size(KNBUD)
225 !Returns the number of registered entries
226 INTEGER, INTENT(OUT) :: knbud
227 knbud=nbuoff_n
228END SUBROUTINE budget_offline_get_size
229!
230SUBROUTINE budget_offline_get_names(KNBUD, KBUDGET, HNAMES)
231 !Returns the budget variable number (NBUDGET_U, NBUDGET_V... of MODD_BUDGET)
232 !and the process name of each entry
233 USE modd_io, ONLY: nverb_fatal
234 USE mode_msg, ONLY: print_msg
235 INTEGER, INTENT(IN) :: knbud
236 INTEGER, DIMENSION(KNBUD), INTENT(OUT) :: kbudget
237 CHARACTER(LEN=16), DIMENSION(KNBUD), INTENT(OUT) :: hnames ! LEN=NBUOFF_NAMELEN
238 !
239 IF (knbud /= nbuoff_n) THEN
240 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE_GET_NAMES', 'wrong number of entries')
241 ENDIF
242 kbudget(:)=nbuoff_budget(1:knbud)
243 hnames(:)=cbuoff_name(1:knbud)
244END SUBROUTINE budget_offline_get_names
245!
246SUBROUTINE budget_offline_get(KDIMIJ, KDIMK, KNBUD, PTEND)
247 !Returns the accumulated contributions of all the entries, and resets the budgets
248 !(entries are deleted)
249 USE modd_io, ONLY: nverb_fatal
250 USE mode_msg, ONLY: print_msg
251 INTEGER, INTENT(IN) :: kdimij, kdimk ! horizontal and vertical dimensions
252 INTEGER, INTENT(IN) :: knbud ! number of entries
253 REAL, DIMENSION(KDIMIJ, KDIMK, KNBUD), INTENT(OUT) :: ptend
254 !
255 IF (knbud /= nbuoff_n) THEN
256 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE_GET', 'wrong number of entries')
257 ENDIF
258 IF (any(cbuoff_pending(:) /= ' ')) THEN
259 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE_GET', 'an INIT_PHY is waiting for its END_PHY')
260 ENDIF
261 IF (knbud > 0) THEN
262 IF (SIZE(xbuoff_tend, 1) /= kdimij .OR. SIZE(xbuoff_tend, 2) /= kdimk) THEN
263 CALL print_msg(nverb_fatal, 'GEN', 'BUDGET_OFFLINE_GET', 'wrong dimensions')
264 ENDIF
265 ptend(:, :, :)=xbuoff_tend(:, :, 1:knbud)
266 ENDIF
267 !Reset
268 nbuoff_n=0
269 nbuoff_budget(:)=0
270 cbuoff_name(:)=' '
271END SUBROUTINE budget_offline_get
272!
273END MODULE mode_budget_offline
integer, parameter nbudget_rv
integer, parameter nbudget_rh
integer, parameter nbudget_th
integer, parameter nbudget_rs
integer, parameter nbudget_ri
integer, parameter nbudget_rc
integer, parameter nbudget_v
integer, parameter nbudget_tke
integer, parameter nbudget_rr
type(tbudgetconf_t), target tbuconf
integer, parameter nbudget_w
integer, parameter nbudget_rg
subroutine tbuconf_associate()
integer, parameter nbudget_u
integer, parameter nverb_fatal
Definition modd_io.F90:6
character(len=nbuoff_namelen), dimension(nbudget_rh) cbuoff_pending
integer, dimension(nbuoff_max) nbuoff_budget
subroutine, public budget_offline_get(kdimij, kdimk, knbud, ptend)
subroutine, public budget_offline_get_size(knbud)
integer, parameter nbuoff_namelen
subroutine, public budget_offline_conf(kbudgets, obudget)
integer, parameter nbuoff_max
real, dimension(:,:,:), allocatable xbuoff_init
subroutine, public budget_offline_get_names(knbud, kbudget, hnames)
character(len=nbuoff_namelen), dimension(nbuoff_max) cbuoff_name
real, dimension(:,:,:), allocatable xbuoff_tend