Skip to content

Commit

Permalink
add taylor series param test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattldawson committed Jan 18, 2024
1 parent d202bac commit e45c03a
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/cross_sections/util/temperature_parameterization.F90
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ module tuvx_temperature_parameterization
real(kind=dk), allocatable :: BB_(:)
real(kind=dk), allocatable :: lp_(:)
!> Base temperature [K] to use in calculations
real(kind=dk) :: base_temperature_
real(kind=dk) :: base_temperature_ = 0.0_dk
!> Base wavelength [nm] to use in calcuations
real(kind=dk) :: base_wavelength_
real(kind=dk) :: base_wavelength_ = 0.0_dk
!> Flag indicating whether cross section algorithm is base 10 (true)
!! or base e (false)
logical :: is_base_10_
logical :: is_base_10_ = .true.
!> Flad indicating whether to subtract base temperature from
!! actual temperature (false) or to subtract actual temperature
!! from base temperature (true)
logical :: is_temperature_inverted_
logical :: is_temperature_inverted_ = .false.
!> Minimum wavelength [nm] to calculate values for
real(kind=dk) :: min_wavelength_
real(kind=dk) :: min_wavelength_ = 0.0_dk
!> Maximum wavelength [nm] to calculate values for
real(kind=dk) :: max_wavelength_
real(kind=dk) :: max_wavelength_ = 0.0_dk
!> Index of minimum wavelength [nm] to calculate values for
integer :: min_wavelength_index_
integer :: min_wavelength_index_ = 0
!> Index of maximum wavelength to calculate values for
integer :: max_wavelength_index_
integer :: max_wavelength_index_ = 0
!> Temperature ranges used in parameterization
type(temperature_range_t), allocatable :: ranges_(:)
contains
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function constructor( config, wavelengths ) result ( this )

character(len=*), parameter :: my_name = &
"Taylor-series temperature parameterization constructor"
type(string_t) :: required_keys(2), optional_keys(3), file_path
type(config_t) :: temp_ranges, temp_range
type(string_t) :: required_keys(2), optional_keys(4), file_path
type(config_t) :: temp_ranges, temp_range, netcdf_file
class(iterator_t), pointer :: iter
type(netcdf_t) :: netcdf
integer :: i_range, i_param, n_param
Expand All @@ -86,12 +86,14 @@ function constructor( config, wavelengths ) result ( this )
optional_keys(1) = "minimum wavelength"
optional_keys(2) = "maximum wavelength"
optional_keys(3) = "temperature ranges"
optional_keys(4) = "type"
call assert_msg( 235183546, &
config%validate( required_keys, optional_keys ), &
"Bad configuration for temperature parameterization." )

! Load NetCDF file
call config%get( "netcdf file", file_path, my_name )
call config%get( "netcdf file", netcdf_file, my_name )
call netcdf_file%get( "file path", file_path, my_name )
call netcdf%read_netcdf_file( file_path = file_path%to_char( ), &
variable_name = "temperature_" )
n_param = size( netcdf%parameters, dim = 2 ) - 1
Expand All @@ -102,7 +104,7 @@ function constructor( config, wavelengths ) result ( this )
this%wavelengths_ = netcdf%wavelength
this%sigma_ = netcdf%parameters(:,1)
do i_param = 1, n_param
this%A_( i_param, : ) = netcdf%parameters( : , i_param )
this%A_( i_param, : ) = netcdf%parameters( : , i_param + 1 )
end do

call config%get( "base temperature", this%base_temperature_, my_name )
Expand Down Expand Up @@ -148,6 +150,11 @@ function constructor( config, wavelengths ) result ( this )
end do
deallocate( iter )

! initialize unused data members
allocate( this%AA_(0) )
allocate( this%BB_(0) )
allocate( this%lp_(0) )

end function constructor

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Expand Down
23 changes: 23 additions & 0 deletions test/data/cross_sections/util/taylor.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "TAYLOR_SERIES",
"netcdf file": {
"file path": "test/data/cross_sections/util/taylor.nc"
},
"base temperature": 295.2,
"minimum wavelength": 280.5,
"maximum wavelength": 540.2,
"temperature ranges": [
{
"maximum": 209.999999999999,
"fixed value": 210.0
},
{
"minimum": 210.0,
"maximum": 300.0
},
{
"minimum": 300.00000000001,
"fixed value": 300.0
}
]
}
Binary file added test/data/cross_sections/util/taylor.nc
Binary file not shown.
2 changes: 2 additions & 0 deletions test/unit/cross_section/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ create_standard_test(NAME cross_section_rono2 SOURCES rono2_test.F90 )
create_standard_test(NAME cross_section_t_butyl_nitrate SOURCES t_butyl_nitrate_test.F90 )
create_standard_test(NAME cross_section_tint SOURCES tint_test.F90 )

add_subdirectory(util)

################################################################################
12 changes: 12 additions & 0 deletions test/unit/cross_section/util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
################################################################################
# Test utilities

include(test_util)

################################################################################
# Cross section utility tests

create_standard_test( NAME temperature_parameterization_taylor_series
SOURCES temperature_parameterization_taylor_series.F90 )

################################################################################
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
! Copyright (C) 2024 National Center for Atmospheric Research
! SPDX-License-Identifier: Apache-2.0
!
!> \file
!> Tests for the temperature_parameterization_taylor_series_t type
program test_temperature_parameterization_taylor_series

use musica_mpi, only : musica_mpi_init, &
musica_mpi_finalize
use tuvx_temperature_parameterization_taylor_series

implicit none

call musica_mpi_init( )
call test_taylor_series_t( )
call musica_mpi_finalize( )

contains

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

subroutine test_taylor_series_t( )

use musica_assert, only : assert
use musica_constants, only : dk => musica_dk
use musica_config, only : config_t
use musica_mpi
use musica_string, only : string_t
use tuvx_grid, only : grid_t
use tuvx_test_utils, only : check_values

type(temperature_parameterization_taylor_series_t) :: taylor_param
type(config_t) :: config
type(grid_t) :: wavelengths
character, allocatable :: buffer(:)
integer :: pack_size, pos
integer, parameter :: comm = MPI_COMM_WORLD

wavelengths%handle_ = "wavelengths"
wavelengths%units_ = "nm"
wavelengths%ncells_ = 5
wavelengths%mid_ = (/ 250.0_dk, 350.0_dk, 450.0_dk, 550.0_dk, 650.0_dk /)
wavelengths%edge_ = (/ 200.0_dk, 300.0_dk, 400.0_dk, 500.0_dk, 600.0_dk, &
700.0_dk /)
wavelengths%delta_ = (/ 100.0_dk, 100.0_dk, 100.0_dk, 100.0_dk, 100.0_dk /)

call config%from_file( "test/data/cross_sections/util/taylor.config.json" )

if( musica_mpi_rank( comm ) == 0 ) then
taylor_param = &
temperature_parameterization_taylor_series_t( config, wavelengths )
pack_size = taylor_param%pack_size( comm )
allocate( buffer( pack_size ) )
pos = 0
call taylor_param%mpi_pack( buffer, pos, comm )
call assert( 857895829, pos <= pack_size )
end if

call musica_mpi_bcast( pack_size, comm )
if( musica_mpi_rank( comm ) .ne. 0 ) allocate( buffer( pack_size ) )
call musica_mpi_bcast( buffer, comm )

if( musica_mpi_rank( comm ) .ne. 0 ) then
pos = 0
call taylor_param%mpi_unpack( buffer, pos, comm )
call assert( 960137855, pos <= pack_size )
end if
deallocate( buffer )

! Check temperature parameterization data members
call check_values( 405965907, taylor_param%wavelengths_, &
(/ 302.0_dk, 304.0_dk, 306.0_dk, 308.0_dk, 310.0_dk /),&
1.0e-6_dk )
call check_values( 973109062, taylor_param%sigma_, &
(/ 13.3_dk, 14.4_dk, 15.5_dk, 16.6_dk, 17.7_dk /), &
1.0e-6_dk )
call assert( 614095855, size( taylor_param%A_, dim = 1) == 2 )
call check_values( 178709862, taylor_param%A_(1,:), &
(/ 21.4_dk, 22.3_dk, 23.2_dk, 24.1_dk, 25.0_dk /), &
1.0e-6_dk )
call check_values( 842091318, taylor_param%A_(2,:), &
(/ 6.0_dk, 7.0_dk, 8.0_dk, 9.0_dk, 10.0_dk /), &
1.0e-6_dk )
call assert( 161915997, taylor_param%base_temperature_ == 295.2_dk )
call assert( 940974571, taylor_param%min_wavelength_ == 280.5_dk )
call assert( 992095584, taylor_param%max_wavelength_ == 540.2_dk )
call assert( 483530406, size( taylor_param%ranges_ ) == 3 )
call assert( 815221134, taylor_param%ranges_(1)%min_temperature_ == &
0.0_dk )
call assert( 182355758, taylor_param%ranges_(1)%max_temperature_ == &
209.999999999999_dk )
call assert( 977207253, taylor_param%ranges_(1)%is_fixed_ .eqv. .true. )
call assert( 242099851, taylor_param%ranges_(1)%fixed_temperature_ == &
210.0_dk )
call assert( 689467697, taylor_param%ranges_(2)%min_temperature_ == &
210.0_dk )
call assert( 301843944, taylor_param%ranges_(2)%max_temperature_ == &
300.0_dk )
call assert( 466736541, taylor_param%ranges_(2)%is_fixed_ .eqv. .false. )
call assert( 914104387, taylor_param%ranges_(2)%fixed_temperature_ == &
0.0_dk )
call assert( 178996985, taylor_param%ranges_(3)%min_temperature_ == &
300.00000000001_dk )
call assert( 691373231, taylor_param%ranges_(3)%max_temperature_ == &
huge(1.0_dk) )
call assert( 856265828, taylor_param%ranges_(3)%is_fixed_ .eqv. .true. )
call assert( 403633675, taylor_param%ranges_(3)%fixed_temperature_ == &
300.0_dk )

end subroutine test_taylor_series_t

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

end program test_temperature_parameterization_taylor_series

0 comments on commit e45c03a

Please sign in to comment.