Skip to content

Commit ed50c4d

Browse files
committed
Refactor dyn_init for better readability
Put thermodynamics-related initialization code into separate subroutines so that it does not bloat and pollute the scope of dyn_init.
1 parent fb5bf45 commit ed50c4d

File tree

1 file changed

+76
-47
lines changed

1 file changed

+76
-47
lines changed

src/dynamics/mpas/dyn_comp_impl.F90

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,15 @@ end subroutine dyn_readnl
132132
! Called by `cam_init` in `src/control/cam_comp.F90`.
133133
module subroutine dyn_init(cam_runtime_opts, dyn_in, dyn_out)
134134
! Module(s) from CAM-SIMA.
135-
use air_composition, only: thermodynamic_active_species_num, &
136-
thermodynamic_active_species_liq_num, &
137-
thermodynamic_active_species_ice_num, &
138-
thermodynamic_active_species_idx, thermodynamic_active_species_idx_dycore, &
139-
thermodynamic_active_species_liq_idx, thermodynamic_active_species_liq_idx_dycore, &
140-
thermodynamic_active_species_ice_idx, thermodynamic_active_species_ice_idx_dycore
141135
use cam_abortutils, only: check_allocate
142136
use cam_constituents, only: const_name, const_is_water_species, num_advected, readtrace
143137
use cam_control_mod, only: initial_run
144138
use cam_initfiles, only: initial_file_get_id, topo_file_get_id
145139
use cam_logfile, only: debugout_debug, debugout_info
146140
use cam_pio_utils, only: clean_iodesc_list
147-
use cam_thermo_formula, only: energy_formula_dycore, energy_formula_dycore_mpas
148141
use dyn_coupling, only: dyn_exchange_constituent_states
149142
use inic_analytic, only: analytic_ic_active
150-
use physics_types, only: dycore_energy_consistency_adjust
151143
use runtime_obj, only: runtime_options
152-
use string_utils, only: stringify
153144
use time_manager, only: get_step_size
154145
! Module(s) from CCPP.
155146
use phys_vars_init_check, only: std_name_len
@@ -174,14 +165,6 @@ module subroutine dyn_init(cam_runtime_opts, dyn_in, dyn_out)
174165
nullify(pio_init_file)
175166
nullify(pio_topo_file)
176167

177-
! Set the energy formula of dynamical core to MPAS for use in `cam_thermo`.
178-
energy_formula_dycore = energy_formula_dycore_mpas
179-
180-
! The total energy of dynamical core, which uses "MPAS formula" as set above, is not consistent with
181-
! that of CAM physics, which uses "FV formula". Therefore, temperature and temperature tendency adjustments
182-
! are needed at the end of each physics time step.
183-
dycore_energy_consistency_adjust = .true.
184-
185168
allocate(constituent_name(num_advected), stat=ierr)
186169
call check_allocate(ierr, subname, 'constituent_name(num_advected)', 'dyn_comp', __LINE__)
187170

@@ -195,41 +178,14 @@ module subroutine dyn_init(cam_runtime_opts, dyn_in, dyn_out)
195178

196179
call dyn_debug_print(debugout_info, 'Defining MPAS scalars and scalar tendencies')
197180

198-
! Inform MPAS about constituent names and their corresponding waterness.
181+
! Inform MPAS about the constituent names and their corresponding waterness.
199182
call mpas_dynamical_core % define_scalar(constituent_name, is_water_species)
200183

201184
deallocate(constituent_name)
202185
deallocate(is_water_species)
203186

204-
! Provide mapping information between MPAS scalars and constituent names to CAM-SIMA.
205-
do i = 1, thermodynamic_active_species_num
206-
thermodynamic_active_species_idx_dycore(i) = &
207-
mpas_dynamical_core % map_mpas_scalar_index(thermodynamic_active_species_idx(i))
208-
end do
209-
210-
do i = 1, thermodynamic_active_species_liq_num
211-
thermodynamic_active_species_liq_idx_dycore(i) = &
212-
mpas_dynamical_core % map_mpas_scalar_index(thermodynamic_active_species_liq_idx(i))
213-
end do
214-
215-
do i = 1, thermodynamic_active_species_ice_num
216-
thermodynamic_active_species_ice_idx_dycore(i) = &
217-
mpas_dynamical_core % map_mpas_scalar_index(thermodynamic_active_species_ice_idx(i))
218-
end do
219-
220-
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_num = ' // &
221-
stringify([thermodynamic_active_species_num]))
222-
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_liq_num = ' // &
223-
stringify([thermodynamic_active_species_liq_num]))
224-
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_ice_num = ' // &
225-
stringify([thermodynamic_active_species_ice_num]))
226-
227-
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_idx_dycore = [' // &
228-
stringify(thermodynamic_active_species_idx_dycore) // ']')
229-
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_liq_idx_dycore = [' // &
230-
stringify(thermodynamic_active_species_liq_idx_dycore) // ']')
231-
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_ice_idx_dycore = [' // &
232-
stringify(thermodynamic_active_species_ice_idx_dycore) // ']')
187+
call set_thermodynamic_active_species_mapping()
188+
call set_thermodynamic_energy_formula()
233189

234190
pio_init_file => initial_file_get_id()
235191
pio_topo_file => topo_file_get_id()
@@ -290,6 +246,79 @@ module subroutine dyn_init(cam_runtime_opts, dyn_in, dyn_out)
290246
call dyn_debug_print(debugout_debug, subname // ' completed')
291247
end subroutine dyn_init
292248

249+
!> Inform CAM-SIMA about the index mapping between MPAS scalars and CAM-SIMA constituents.
250+
!> (KCW, 2025-07-17)
251+
subroutine set_thermodynamic_active_species_mapping()
252+
! Module(s) from CAM-SIMA.
253+
use air_composition, only: thermodynamic_active_species_num, &
254+
thermodynamic_active_species_liq_num, &
255+
thermodynamic_active_species_ice_num, &
256+
thermodynamic_active_species_idx, thermodynamic_active_species_idx_dycore, &
257+
thermodynamic_active_species_liq_idx, thermodynamic_active_species_liq_idx_dycore, &
258+
thermodynamic_active_species_ice_idx, thermodynamic_active_species_ice_idx_dycore
259+
use cam_logfile, only: debugout_debug
260+
use string_utils, only: stringify
261+
262+
character(*), parameter :: subname = 'dyn_comp::set_thermodynamic_active_species_mapping'
263+
integer :: i
264+
265+
call dyn_debug_print(debugout_debug, subname // ' entered')
266+
267+
do i = 1, thermodynamic_active_species_num
268+
thermodynamic_active_species_idx_dycore(i) = &
269+
mpas_dynamical_core % map_mpas_scalar_index(thermodynamic_active_species_idx(i))
270+
end do
271+
272+
do i = 1, thermodynamic_active_species_liq_num
273+
thermodynamic_active_species_liq_idx_dycore(i) = &
274+
mpas_dynamical_core % map_mpas_scalar_index(thermodynamic_active_species_liq_idx(i))
275+
end do
276+
277+
do i = 1, thermodynamic_active_species_ice_num
278+
thermodynamic_active_species_ice_idx_dycore(i) = &
279+
mpas_dynamical_core % map_mpas_scalar_index(thermodynamic_active_species_ice_idx(i))
280+
end do
281+
282+
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_num = ' // &
283+
stringify([thermodynamic_active_species_num]))
284+
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_liq_num = ' // &
285+
stringify([thermodynamic_active_species_liq_num]))
286+
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_ice_num = ' // &
287+
stringify([thermodynamic_active_species_ice_num]))
288+
289+
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_idx_dycore = [' // &
290+
stringify(thermodynamic_active_species_idx_dycore) // ']')
291+
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_liq_idx_dycore = [' // &
292+
stringify(thermodynamic_active_species_liq_idx_dycore) // ']')
293+
call dyn_debug_print(debugout_debug, 'thermodynamic_active_species_ice_idx_dycore = [' // &
294+
stringify(thermodynamic_active_species_ice_idx_dycore) // ']')
295+
296+
call dyn_debug_print(debugout_debug, subname // ' completed')
297+
end subroutine set_thermodynamic_active_species_mapping
298+
299+
!> Set the thermodynamic energy formula of dynamical core to MPAS.
300+
!> (KCW, 2025-07-17)
301+
subroutine set_thermodynamic_energy_formula()
302+
! Module(s) from CAM-SIMA.
303+
use cam_logfile, only: debugout_debug
304+
use cam_thermo_formula, only: energy_formula_dycore, energy_formula_dycore_mpas
305+
use physics_types, only: dycore_energy_consistency_adjust
306+
307+
character(*), parameter :: subname = 'dyn_comp::set_thermodynamic_energy_formula'
308+
309+
call dyn_debug_print(debugout_debug, subname // ' entered')
310+
311+
! Set the thermodynamic energy formula of dynamical core to MPAS for use in `cam_thermo`.
312+
energy_formula_dycore = energy_formula_dycore_mpas
313+
314+
! The total energy of dynamical core, which uses "MPAS formula" as set above, is not consistent with
315+
! that of CAM physics, which uses "FV formula". Therefore, temperature and temperature tendency adjustments
316+
! are needed at the end of each physics time step.
317+
dycore_energy_consistency_adjust = .true.
318+
319+
call dyn_debug_print(debugout_debug, subname // ' completed')
320+
end subroutine set_thermodynamic_energy_formula
321+
293322
!> Check for consistency in topography data. The presence of topography file is inferred from the `pio_file` pointer.
294323
!> If topography file is used, check that the "PHIS" variable, which denotes surface geopotential,
295324
!> is consistent with the surface geometric height in MPAS.

0 commit comments

Comments
 (0)