From e45c03a6b0c1903c8222246b9bdf1ce8c9da2339 Mon Sep 17 00:00:00 2001 From: Matt Dawson Date: Thu, 18 Jan 2024 12:17:51 -0800 Subject: [PATCH] add taylor series param test --- .../util/temperature_parameterization.F90 | 16 +-- ...erature_parameterization_taylor_series.F90 | 15 ++- .../cross_sections/util/taylor.config.json | 23 ++++ test/data/cross_sections/util/taylor.nc | Bin 0 -> 436 bytes test/unit/cross_section/CMakeLists.txt | 2 + test/unit/cross_section/util/CMakeLists.txt | 12 ++ ...erature_parameterization_taylor_series.F90 | 114 ++++++++++++++++++ 7 files changed, 170 insertions(+), 12 deletions(-) create mode 100644 test/data/cross_sections/util/taylor.config.json create mode 100644 test/data/cross_sections/util/taylor.nc create mode 100644 test/unit/cross_section/util/CMakeLists.txt create mode 100644 test/unit/cross_section/util/temperature_parameterization_taylor_series.F90 diff --git a/src/cross_sections/util/temperature_parameterization.F90 b/src/cross_sections/util/temperature_parameterization.F90 index baa34239..15c4273c 100644 --- a/src/cross_sections/util/temperature_parameterization.F90 +++ b/src/cross_sections/util/temperature_parameterization.F90 @@ -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 diff --git a/src/cross_sections/util/temperature_parameterization_taylor_series.F90 b/src/cross_sections/util/temperature_parameterization_taylor_series.F90 index cdb11e41..ef9b8f11 100644 --- a/src/cross_sections/util/temperature_parameterization_taylor_series.F90 +++ b/src/cross_sections/util/temperature_parameterization_taylor_series.F90 @@ -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 @@ -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 @@ -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 ) @@ -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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/test/data/cross_sections/util/taylor.config.json b/test/data/cross_sections/util/taylor.config.json new file mode 100644 index 00000000..54b0c7d3 --- /dev/null +++ b/test/data/cross_sections/util/taylor.config.json @@ -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 + } + ] +} \ No newline at end of file diff --git a/test/data/cross_sections/util/taylor.nc b/test/data/cross_sections/util/taylor.nc new file mode 100644 index 0000000000000000000000000000000000000000..1b73b9d55dd3af29bd7265d5c2ce86b40e39e180 GIT binary patch literal 436 zcmZ>EabskF04^ZK48%MosksHIMTsS)MXAM5IhLf%JP?}|h`9<9ixP8FOHzvIt23QJY2MF^3F<3s{Ei)%4EHS4v6)evNlCS>(W`hXU(!9(P zsO2E_OnJFrIgmOHpi&VaEr#UIcr-VG^#Ikt>;%zV1ndNvT>;c$<52Vf$Yfw}fI7gT zSOLm+fYJ#NT5Bc<%yQ5<0|I9q^ufXm4hB$h!_CGZ;9!~t1`cLE5OwBYVPgjiNr-++ Ts6GiOEd!+$ptKT{R)Np}mrP8* literal 0 HcmV?d00001 diff --git a/test/unit/cross_section/CMakeLists.txt b/test/unit/cross_section/CMakeLists.txt index 08b27742..4733957c 100644 --- a/test/unit/cross_section/CMakeLists.txt +++ b/test/unit/cross_section/CMakeLists.txt @@ -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) + ################################################################################ diff --git a/test/unit/cross_section/util/CMakeLists.txt b/test/unit/cross_section/util/CMakeLists.txt new file mode 100644 index 00000000..c33376f5 --- /dev/null +++ b/test/unit/cross_section/util/CMakeLists.txt @@ -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 ) + +################################################################################ \ No newline at end of file diff --git a/test/unit/cross_section/util/temperature_parameterization_taylor_series.F90 b/test/unit/cross_section/util/temperature_parameterization_taylor_series.F90 new file mode 100644 index 00000000..430a52a8 --- /dev/null +++ b/test/unit/cross_section/util/temperature_parameterization_taylor_series.F90 @@ -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 \ No newline at end of file