Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the creation of a TUV-x photolysis calculator to the MUSICA wrapper #108

Merged
merged 33 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
64d43ae
add tuvx ccpp wrapper
boulderdaze Aug 9, 2024
3b793e9
add tuvx to musica ccpp
boulderdaze Aug 9, 2024
a64505d
add tuvx wrapper
boulderdaze Aug 9, 2024
fc42fdb
remove mod
boulderdaze Aug 9, 2024
644ea3a
correct tuvx wrapper - in progress
boulderdaze Aug 9, 2024
9ecff58
fix bugs
boulderdaze Aug 14, 2024
4ef64e6
fix build bugs
boulderdaze Aug 14, 2024
917a12f
add reshaping array
boulderdaze Aug 16, 2024
5713d98
add tuvx data
boulderdaze Aug 16, 2024
084153b
cmake
boulderdaze Aug 16, 2024
017f645
fix config not found error
boulderdaze Aug 20, 2024
976fa29
fix cmake error
boulderdaze Aug 20, 2024
360b55e
update git tag
boulderdaze Aug 20, 2024
d7de417
code cleanup
boulderdaze Aug 20, 2024
85338ff
no third body
boulderdaze Aug 20, 2024
2ed7c7b
code clean up
boulderdaze Aug 20, 2024
509d62f
remove solve call
boulderdaze Aug 20, 2024
f666466
git stash
boulderdaze Aug 22, 2024
095f5aa
add reshape function
boulderdaze Aug 23, 2024
08c593a
fix bugs
boulderdaze Aug 23, 2024
46b33de
download data when test is on
boulderdaze Aug 23, 2024
dbfe69f
remove copy data
boulderdaze Aug 23, 2024
1301b9d
add test
boulderdaze Aug 24, 2024
92ac514
move conversion unit code outside of micm run
boulderdaze Aug 24, 2024
11c551d
add util unit test
boulderdaze Aug 24, 2024
8f4b4d5
update musica tag
boulderdaze Aug 24, 2024
a20ac81
code clean up
boulderdaze Aug 24, 2024
a5ca4ab
Merge branch 'development' into 75-tuvx-wrapper
boulderdaze Aug 24, 2024
f71e946
use ubuntu latest
boulderdaze Aug 24, 2024
93917df
ubuntu latest doesn't work
boulderdaze Aug 24, 2024
7408fa7
address review
boulderdaze Sep 3, 2024
170119d
address review
boulderdaze Sep 3, 2024
4fd3c44
fix bugs
boulderdaze Sep 3, 2024
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
61 changes: 38 additions & 23 deletions musica/micm/musica_ccpp_micm.F90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module musica_ccpp_micm
use iso_c_binding

! Note: "micm_core" is included in an external pre-built MICM library that the host
! Note: "micm_t" is included in an external pre-built MICM library that the host
! model is responsible for linking to during compilation
use musica_micm, only: micm_t
use musica_ccpp_util, only: has_error_occurred
Expand All @@ -20,21 +20,27 @@ module musica_ccpp_micm
!> Register MICM constituents with the CCPP
subroutine micm_register(constituents, errmsg, errcode)
use ccpp_constituent_prop_mod, only: ccpp_constituent_properties_t
use musica_util, only: error_t, mapping_t
type(ccpp_constituent_properties_t), allocatable, intent(out) :: constituents(:)
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode
use musica_micm, only: Rosenbrock, RosenbrockStandardOrder
use musica_util, only: error_t

type(ccpp_constituent_properties_t), allocatable, intent(out) :: constituents(:)
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode

type(error_t) :: error
type(mapping_t) :: mapping
! local variables
type(error_t) :: error
real(kind=kind_phys) :: molar_mass
logical :: is_advected
integer :: i
logical :: is_advected
integer :: solver_type
integer :: num_grid_cells
integer :: i

solver_type = Rosenbrock
num_grid_cells = 1
errcode = 0
errmsg = ''

micm => micm_t(filename_of_micm_configuration, error)
micm => micm_t(filename_of_micm_configuration, solver_type, num_grid_cells, error)
boulderdaze marked this conversation as resolved.
Show resolved Hide resolved
if (has_error_occurred(error, errmsg, errcode)) return

allocate(constituents(size(micm%species_ordering)), stat=errcode)
Expand Down Expand Up @@ -85,7 +91,8 @@ end subroutine micm_init
subroutine micm_run(time_step, temperature, pressure, dry_air_density, constituent_props, &
constituents, errmsg, errcode)
use ccpp_constituent_prop_mod, only: ccpp_constituent_prop_ptr_t
use musica_util, only: error_t
use musica_micm, only: solver_stats_t
use musica_util, only: string_t, error_t

real(kind_phys), intent(in) :: time_step ! s
real(kind_phys), intent(in) :: temperature(:,:) ! K
Expand All @@ -98,22 +105,23 @@ subroutine micm_run(time_step, temperature, pressure, dry_air_density, constitue

! local variables
real(c_double) :: c_time_step
real(c_double), dimension(size(temperature, dim=1), &
real(c_double), dimension(size(temperature, dim=1), &
size(temperature, dim=2)) :: c_temperature
real(c_double), dimension(size(pressure, dim=1), &
real(c_double), dimension(size(pressure, dim=1), &
size(pressure, dim=2)) :: c_pressure
real(c_double), dimension(size(constituents, dim=1), &
size(constituents, dim=2), &
size(constituents, dim=3)) :: c_constituents
real(c_double), dimension(size(constituents, dim=1), &
size(constituents, dim=2), &
0) :: c_rate_params

real(kind_phys), dimension(size(constituents, dim=3)) :: molar_mass_arr ! kg mol-1
type(error_t) :: error

integer :: num_columns, num_layers, num_constituents
integer :: i_column, i_layer, i_elem
type(string_t) :: solver_state
type(solver_stats_t) :: solver_stats
type(error_t) :: error
integer :: num_columns, num_layers, num_constituents
integer :: i_column, i_layer, i_elem

num_columns = size(constituents, dim=1)
num_layers = size(constituents, dim=2)
Expand Down Expand Up @@ -149,15 +157,22 @@ subroutine micm_run(time_step, temperature, pressure, dry_air_density, constitue
c_temperature = real(temperature, c_double)
c_pressure = real(pressure, c_double)
c_constituents = real(constituents, c_double)

do i_column = 1, num_columns
! c_constitnudents_()
! do i_column = 1, num_columns
do i_layer = 1, num_layers
call micm%solve(c_temperature(i_column, i_layer), c_pressure(i_column, i_layer), &
c_time_step, num_constituents, c_constituents(i_column, i_layer, :), &
0, c_rate_params(i_column, i_layer, :), error)

call micm%solve(c_time_step, &
c_temperature(:, i_layer), &
c_pressure(:, i_layer), &
dry_air_density(:, i_layer), &
c_constituents(1, i_layer, :), & !TODO(jiwon)
c_rate_params(1, i_layer, :), &
solver_state, &
solver_stats, &
error)
if (has_error_occurred(error, errmsg, errcode)) return
end do
end do
! end do

constituents = real(c_constituents, kind_phys)

Expand Down
25 changes: 15 additions & 10 deletions musica/musica_ccpp.F90
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
!> Top-level wrapper for MUSICA chemistry components
module musica_ccpp
use musica_ccpp_micm, only : micm_register, micm_init, micm_run, micm_final
use musica_ccpp_tuvx, only : tuvx_init, tuvx_run, tuvx_final

implicit none
private
Expand All @@ -12,8 +13,8 @@ module musica_ccpp
subroutine musica_ccpp_register(constituents, errmsg, errcode)
use ccpp_constituent_prop_mod, only : ccpp_constituent_properties_t
type(ccpp_constituent_properties_t), allocatable, intent(out) :: constituents(:)
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode

call micm_register(constituents, errmsg, errcode)
end subroutine musica_ccpp_register
Expand All @@ -22,27 +23,30 @@ end subroutine musica_ccpp_register
!! \htmlinclude musica_ccpp_init.html
subroutine musica_ccpp_init(errmsg, errcode)
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode
integer, intent(out) :: errcode

call tuvx_init(errmsg, errcode)
call micm_init(errmsg, errcode)
end subroutine musica_ccpp_init

!> \section arg_table_musica_ccpp_run Argument Table
!! \htmlinclude musica_ccpp_run.html
subroutine musica_ccpp_run(time_step, temperature, pressure, dry_air_density, constituent_props, &
constituents, errmsg, errcode)
constituents, height, errmsg, errcode)
use ccpp_kinds, only: kind_phys
use ccpp_constituent_prop_mod, only: ccpp_constituent_prop_ptr_t

real(kind_phys), intent(in) :: time_step ! s
real(kind_phys), intent(in) :: temperature(:,:) ! K
real(kind_phys), intent(in) :: pressure(:,:) ! Pa
real(kind_phys), intent(in) :: dry_air_density(:,:) ! kg m-3
real(kind_phys), intent(in) :: time_step ! s
real(kind_phys), intent(in) :: temperature(:,:) ! K
real(kind_phys), intent(in) :: pressure(:,:) ! Pa
real(kind_phys), intent(in) :: dry_air_density(:,:) ! kg m-3
type(ccpp_constituent_prop_ptr_t), intent(in) :: constituent_props(:)
real(kind_phys), intent(inout) :: constituents(:,:,:) ! kg kg-1
real(kind_phys), intent(inout) :: constituents(:,:,:) ! kg kg-1
real(kind_phys), intent(in) :: height(:,:) ! km
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode

call tuvx_run(height, temperature, dry_air_density, errmsg, errcode)
call micm_run(time_step, temperature, pressure, dry_air_density, constituent_props, &
constituents, errmsg, errcode)

Expand All @@ -51,9 +55,10 @@ end subroutine musica_ccpp_run
!> \section arg_table_musica_ccpp_final Argument Table
!! \htmlinclude musica_ccpp_final.html
subroutine musica_ccpp_final(errmsg, errcode)
integer, intent(out) :: errcode
integer, intent(out) :: errcode
character(len=512), intent(out) :: errmsg

call tuvx_final(errmsg, errcode)
call micm_final(errmsg, errcode)
end subroutine musica_ccpp_final

Expand Down
2 changes: 1 addition & 1 deletion musica/musica_ccpp.meta
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[ccpp-table-properties]
name = musica_ccpp
type = scheme
dependencies = micm/musica_ccpp_micm.F90,musica_ccpp_util.F90
dependencies = micm/musica_ccpp_micm.F90,micm/musica_ccpp_tuvx.F90,musica_ccpp_util.F90
dynamic_constituent_routine = musica_ccpp_register

[ccpp-arg-table]
Expand Down
13 changes: 13 additions & 0 deletions musica/musica_ccpp_namelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,18 @@
<value>UNSET_PATH</value>
</values>
</entry>
<entry id="filename_of_tuvx_configuration">
<type>char*512</type>
<category>musica_ccpp</category>
<group>musica_ccpp</group>
<standard_name>filename_of_tuvx_configuration</standard_name>
<units>none</units>
<desc>
A configuration file for the TUVX photolysis rate calculator
</desc>
<values>
<value>UNSET_PATH</value>
</values>
</entry>

</entry_id_pg>
85 changes: 85 additions & 0 deletions musica/tuvx/musica_ccpp_tuvx.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module musica_ccpp_tuvx
use iso_c_binding

! Note: "tuvx_t" is included in an external pre-built tuvx library that the host
! model is responsible for linking to during compilation
use musica_tuvx, only: tuvx_t
use musica_ccpp_util, only: has_error_occurred
use ccpp_kinds, only: kind_phys
use musica_ccpp_namelist, only: filename_of_tuvx_configuration

implicit none
private

public :: tuvx_init, tuvx_run, tuvx_final

type(tuvx_t), pointer :: tuvx => null( )

contains

!> Intitialize TUVX
subroutine tuvx_init(errmsg, errcode)
use musica_tuvx, only: grid_map_t, profile_map_t, radiator_map_t
use musica_util, only: error_t, mapping_t

character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode

! local variables
type(grid_map_t), pointer :: grids
type(profile_map_t), pointer :: profiles
type(radiator_map_t), pointer :: radiators
type(error_t) :: error

errcode = 0
errmsg = ''

grids => grid_map_t( error )
if (has_error_occurred( error, errmsg, errcode )) return

profiles => profile_map_t( error )
if (has_error_occurred( error, errmsg, errcode )) return

radiators =>radiator_map_t( error )
if (has_error_occurred( error, errmsg, errcode )) return

tuvx => tuvx_t( filename_of_tuvx_configuration, error )
! TODO(jiwon) - MUSICA TUVX constuctor needs update
! tuvx => tuvx_t( filename_of_tuvx_configuration, grids, profiles, radiators, error )
if (has_error_occurred( error, errmsg, errcode )) return

deallocate( grids )
deallocate( profiles )
deallocate( radiators )

end subroutine tuvx_init

!> Calculates photolysis rate constants for the current model conditions
subroutine tuvx_run( height, temperature, dry_air_density, errmsg, errcode )
use musica_util, only: error_t

real(kind_phys), intent(in) :: height(:,:) ! [km] (layer, column)
real(kind_phys), intent(in) :: temperature(:,:) ! [K] (layer, column)
real(kind_phys), intent(in) :: dry_air_density(:,:) ! [molecule cm-3] (layer, column)
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode

! local variables
type(error_t) :: error

errcode = 0
errmsg = ''

end subroutine tuvx_run

!> Finalize tuvx
subroutine tuvx_final(errmsg, errcode)
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errcode

errcode = 0
errmsg = ''

end subroutine tuvx_final

end module musica_ccpp_tuvx
9 changes: 6 additions & 3 deletions test/docker/Dockerfile.musica
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM ubuntu:22.04

ARG MUSICA_GIT_TAG=a030011202ff3f8b753981578fead22274b4d64c

RUN apt update \
&& apt install -y sudo \
&& adduser test_user \
Expand Down Expand Up @@ -45,7 +47,7 @@ ENV FFLAGS="-I/usr/include/"
RUN git clone https://github.com/NCAR/musica.git
RUN cd musica \
&& git fetch \
&& git checkout 2a5eeaac982a3eb80b96d1e2087b91b301d1e748 \
&& git checkout ${MUSICA_GIT_TAG} \
&& cmake \
-S . \
-B build \
Expand Down Expand Up @@ -74,6 +76,7 @@ RUN cd atmospheric_physics/test \
&& cmake --build ./build

RUN cd atmospheric_physics \
&& cp -r test/musica/micm/configs/chapman ./test/build/chapman
&& cp -r test/musica/micm/configs/chapman ./test/build/chapman \
&& cp -r test/musica/tuvx/configs/ts1_tsmlt.json ./test/build/ts1_tsmlt.json

WORKDIR /home/test_user/atmospheric_physics/test/build
WORKDIR /home/test_user/atmospheric_physics/test/build
10 changes: 6 additions & 4 deletions test/musica/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
include(FetchContent)
include(TestUtils)

set(MUSICA_GIT_TAG a030011202ff3f8b753981578fead22274b4d64c CACHE STRING "Git tag for the musica repository")

FetchContent_Declare(musica
GIT_REPOSITORY https://github.com/NCAR/musica.git
# GIT_TAG v0.6.1
GIT_TAG 2a5eeaac982a3eb80b96d1e2087b91b301d1e748
GIT_TAG ${MUSICA_GIT_TAG}
)

set(MUSICA_BUILD_C_CXX_INTERFACE OFF)
Expand All @@ -13,7 +14,7 @@ set(MUSICA_ENABLE_TESTS OFF)
set(MUSICA_ENABLE_INSTALL OFF)

FetchContent_MakeAvailable(musica)

# ---------------------------------------------------------
# Create a test for MUSICA CCPP wrapper
# ---------------------------------------------------------
Expand All @@ -23,6 +24,7 @@ add_executable(test_musica_api test_musica_api.F90 musica_ccpp_namelist.F90)
target_sources(test_musica_api
PUBLIC
${MUSICA_SRC_PATH}/micm/musica_ccpp_micm.F90
${MUSICA_SRC_PATH}/tuvx/musica_ccpp_tuvx.F90
${MUSICA_SRC_PATH}/musica_ccpp.F90
${MUSICA_SRC_PATH}/musica_ccpp_util.F90
${CCPP_SRC_PATH}/ccpp_constituent_prop_mod.F90
Expand Down Expand Up @@ -50,4 +52,4 @@ add_test(
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

add_memory_check_test(test_musica_api $<TARGET_FILE:test_musica_api> "" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
add_memory_check_test(test_musica_api $<TARGET_FILE:test_musica_api> "" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
5 changes: 3 additions & 2 deletions test/musica/musica_ccpp_namelist.F90
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module musica_ccpp_namelist

implicit none

public :: filename_of_micm_configuration, filename_of_tuvx_configuration
private
public :: filename_of_micm_configuration


character(len=*), parameter :: filename_of_micm_configuration = 'chapman'
character(len=*), parameter :: filename_of_tuvx_configuration = 'ts1_tsmlt.json'

end module musica_ccpp_namelist
Loading
Loading