Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core_atmosphere/Registry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@
possible_values="`mpas_dmpar', `mpas_halo'"/>
</nml_record>

#ifdef MPAS_USE_MUSICA
<nml_record name="musica" in_defaults="true">
<nml_option name="config_micm_file" type="character" default_value=""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that we should let the default value be an empty string, should we also add logic in musica_init to not try to initialize MUSICA if config_micm_file is an empty string? Otherwise, would there be another way to not call MUSICA in a particular simulation when MPAS was compiled with MUSICA=true?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Added musica_is_initialized flag (line 32)
  2. Added early return when config file is empty, with log message (lines 69-75)
  3. Added logging of the config file path (line 80)
  4. Set musica_is_initialized = .true. after successful init (line 93)
  5. Added guard in musica_step (line 114)
  6. Added guard in musica_finalize (line 137)

units="-"
description="MICM configuration file name"
possible_values="Any valid filename"/>
</nml_record>
#endif

<!-- **************************************************************************************** -->
<!-- ************************************** Packages ************************************** -->
<!-- **************************************************************************************** -->
Expand Down
22 changes: 12 additions & 10 deletions src/core_atmosphere/chemistry/mpas_atm_chemistry.F
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,19 @@ subroutine chemistry_init(configs, dimensions)
use mpas_musica, only: musica_init
#endif
use mpas_log, only : mpas_log_write
use mpas_derived_types, only: mpas_pool_type
use mpas_derived_types, only: mpas_pool_type, MPAS_LOG_CRIT
use mpas_kind_types, only: StrKIND
use mpas_pool_routines, only: mpas_pool_get_config, mpas_pool_get_dimension

type (mpas_pool_type), intent(in) :: configs
type (mpas_pool_type), intent(in) :: dimensions

#ifdef MPAS_USE_MUSICA
integer :: error_code
character(len=:), allocatable :: error_message
integer :: nVertLevels
integer, pointer :: nVertLevels_ptr
! MUSICA will get the MICM JSON config from a namelist
! hardcode filepath for now
character(len=StrKIND) :: filepath = 'chapman.json'
character(len=StrKIND), pointer :: filepath_ptr
integer :: error_code
character(len=:), allocatable :: error_message
integer :: nVertLevels
integer, pointer :: nVertLevels_ptr
#endif

call mpas_log_write('Initializing chemistry packages...')
Expand All @@ -66,9 +64,13 @@ subroutine chemistry_init(configs, dimensions)
call mpas_pool_get_dimension(dimensions, 'nVertLevels', nVertLevels_ptr)
nVertLevels = nVertLevels_ptr

call musica_init(filepath, nVertLevels, error_code, error_message)
call mpas_pool_get_config(configs, 'config_micm_file', filepath_ptr)

! TODO check error_code and generate MPAS error log message
call musica_init(filepath_ptr, nVertLevels, error_code, error_message)

if (error_code /= 0) then
call mpas_log_write(error_message, messageType=MPAS_LOG_CRIT)
end if
#endif

end subroutine chemistry_init
Expand Down
22 changes: 21 additions & 1 deletion src/core_atmosphere/chemistry/musica/mpas_musica.F
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module mpas_musica

type(micm_t), pointer :: micm => null ( ) ! Pointer to the MICM ODE solver instance
type(state_t), pointer :: state => null ( ) ! Pointer to the state of the MICM solver
logical :: musica_is_initialized = .false. ! Flag to track if MUSICA was successfully initialized

contains

Expand Down Expand Up @@ -61,13 +62,22 @@ subroutine musica_init(filename_of_micm_configuration, &

type(error_t) :: error
type(string_t) :: micm_version

! TEMPORARY: Hard-coded options for the MICM solver
integer :: solver_type = RosenbrockStandardOrder
integer :: i_species

! Skip MUSICA initialization if no configuration file is provided
if (len_trim(filename_of_micm_configuration) == 0) then
call mpas_log_write('MUSICA chemistry disabled: no MICM configuration file specified')
error_code = 0
error_message = ''
return
end if

micm_version = get_micm_version()

call mpas_log_write('Initializing MUSICA chemistry package...')
call mpas_log_write('MICM configuration file: ' // trim(filename_of_micm_configuration))
call mpas_log_write('MICM version: ' // micm_version%value_)
call mpas_log_write('MICM number of grid cells: $i', intArgs=[number_of_grid_cells])

Expand All @@ -77,6 +87,12 @@ subroutine musica_init(filename_of_micm_configuration, &
state => micm%get_state(number_of_grid_cells, error)
if (has_error_occurred(error, error_message, error_code)) return

do i_species = 1, state%species_ordering%size()
call mpas_log_write('MICM species: ' // state%species_ordering%name(i_species))
end do

musica_is_initialized = .true.

end subroutine musica_init

!------------------------------------------------------------------------
Expand All @@ -96,6 +112,8 @@ subroutine musica_step()

use mpas_log, only : mpas_log_write

if (.not. musica_is_initialized) return

call mpas_log_write('Stepping MUSICA chemistry package...')

! Here we would typically call the TUV-x and MICM packages to perform
Expand All @@ -117,6 +135,8 @@ subroutine musica_finalize()

use mpas_log, only : mpas_log_write

if (.not. musica_is_initialized) return

call mpas_log_write('Finalizing MUSICA chemistry package...')

! Here we would typically clean up resources, but for now we do nothing.
Expand Down