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

Update TUVX constructor #213

Merged
merged 11 commits into from
Sep 3, 2024
Merged
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
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ RUN dnf -y update \
json-devel \
python \
valgrind \
tree \
&& dnf clean all

# Copy the musica code
Expand Down
62 changes: 47 additions & 15 deletions fortran/test/fetch_content_integration/test_tuvx_api.F90
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,66 @@ program combined_tuvx_tests

! Valid tuvx solver creation test
subroutine test_tuvx_api()
character(len=256) :: config_path
logical(c_bool) :: bool_value
type(tuvx_t), pointer :: tuvx
type(error_t) :: error

character(len=256) :: config_path
type(grid_map_t), pointer :: grids
type(profile_map_t), pointer :: profiles
type(radiator_map_t), pointer :: radiators
type(tuvx_t), pointer :: tuvx
type(error_t) :: error

config_path = "examples/ts1_tsmlt.json"

tuvx => tuvx_t(config_path, error)
grids => grid_map_t( error )
ASSERT( error%is_success() )
profiles => profile_map_t( error )
ASSERT( error%is_success() )
radiators => radiator_map_t( error )
ASSERT( error%is_success() )
tuvx => tuvx_t(config_path, grids, profiles, radiators, error)
ASSERT( error%is_success() )

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

end subroutine test_tuvx_api

! Invalid tuvx solver creation test
subroutine test_tuvx_api_invalid_config()
type(tuvx_t), pointer :: tuvx
type(error_t) :: error
character(len=256) :: config_path

character(len=256) :: config_path
type(grid_map_t), pointer :: grids
type(profile_map_t), pointer :: profiles
type(radiator_map_t), pointer :: radiators
type(tuvx_t), pointer :: tuvx
type(error_t) :: error
config_path = "invalid_config"

tuvx => tuvx_t(config_path, error)
grids => grid_map_t( error )
ASSERT( error%is_success() )
profiles => profile_map_t( error )
ASSERT( error%is_success() )
radiators => radiator_map_t( error )
ASSERT( error%is_success() )
tuvx => tuvx_t(config_path, grids, profiles, radiators, error)
ASSERT( .not. error%is_success() )

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

end subroutine test_tuvx_api_invalid_config

subroutine test_tuvx_solve()

type(tuvx_t), pointer :: tuvx
type(error_t) :: error
character(len=256) :: config_path
type(grid_map_t), pointer :: grids
type(grid_map_t), pointer :: grids, grids_from_host
type(grid_t), pointer :: grid, height_grid, wavelength_grid
type(profile_map_t), pointer :: profiles
type(profile_map_t), pointer :: profiles, profiles_from_host
type(profile_t), pointer :: profile, profile_copy
type(radiator_map_t), pointer :: radiators
type(radiator_map_t), pointer :: radiators, radiators_from_host
type(radiator_t), pointer :: radiator, radiator_copy
real*8, dimension(5), target :: edges, edge_values, temp_edge
real*8, dimension(4), target :: midpoints, midpoint_values, layer_densities, temp_midpoint
Expand All @@ -89,8 +111,15 @@ subroutine test_tuvx_solve()

config_path = "examples/ts1_tsmlt.json"

tuvx => tuvx_t( config_path, error )
grids_from_host => grid_map_t( error )
ASSERT( error%is_success() )
profiles_from_host => profile_map_t( error )
ASSERT( error%is_success() )
radiators_from_host => radiator_map_t( error )
ASSERT( error%is_success() )
tuvx => tuvx_t(config_path, grids_from_host, profiles_from_host, radiators_from_host, error)
ASSERT( error%is_success() )

grids => tuvx%get_grids( error )
ASSERT( error%is_success() )

Expand Down Expand Up @@ -467,6 +496,9 @@ subroutine test_tuvx_solve()
deallocate( radiators )
deallocate( height_grid )
deallocate( wavelength_grid )
deallocate( grids_from_host )
deallocate( profiles_from_host )
deallocate( radiators_from_host )
deallocate( tuvx )

end subroutine test_tuvx_solve
Expand Down
29 changes: 19 additions & 10 deletions fortran/tuvx/tuvx.F90
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ module musica_tuvx
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

interface
function create_tuvx_c(config_path, error) bind(C, name="CreateTuvx")
function create_tuvx_c(config_path, grids, profiles, radiators, error) &
bind(C, name="CreateTuvx")
use musica_util, only: error_t_c
use iso_c_binding, only: c_ptr, c_int, c_char
character(len=1, kind=c_char), intent(in) :: config_path(*)
type(error_t_c), intent(inout) :: error
type(c_ptr) :: create_tuvx_c
character(len=1, kind=c_char), intent(in) :: config_path(*)
type(c_ptr), value, intent(in) :: grids
type(c_ptr), value, intent(in) :: profiles
type(c_ptr), value, intent(in) :: radiators
type(error_t_c), intent(inout) :: error
type(c_ptr) :: create_tuvx_c
end function create_tuvx_c

subroutine delete_tuvx_c(tuvx, error) bind(C, name="DeleteTuvx")
Expand Down Expand Up @@ -87,18 +91,22 @@ end function get_radiator_map_c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!> Construct a tuvx instance
function constructor(config_path, error) result( this )
use iso_c_binding, only: c_char, c_null_char
function constructor(config_path, grids, profiles, radiators, error) &
result( this )
use iso_c_binding, only: c_char, c_null_char, c_loc
use musica_util, only: error_t_c, error_t

! Arguments
type(error_t), intent(inout) :: error
character(len=*), intent(in) :: config_path
character(len=*), intent(in) :: config_path
type(grid_map_t), target, intent(in) :: grids
type(profile_map_t), target, intent(in) :: profiles
type(radiator_map_t), target, intent(in) :: radiators
type(error_t), intent(inout) :: error

! Local variables
character(len=1, kind=c_char) :: c_config_path(len_trim(config_path)+1)
integer :: n, i
type(error_t_c) :: error_c
integer :: n, i

! Return value
type(tuvx_t), pointer :: this
Expand All @@ -111,7 +119,8 @@ function constructor(config_path, error) result( this )
end do
c_config_path(n+1) = c_null_char

this%ptr_ = create_tuvx_c(c_config_path, error_c)
this%ptr_ = create_tuvx_c(c_config_path, grids%ptr_, profiles%ptr_, &
radiators%ptr_, error_c)

error = error_t(error_c)
if (.not. error%is_success()) then
Expand Down
7 changes: 5 additions & 2 deletions include/musica/tuvx/tuvx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ namespace musica

/// @brief Create an instance of tuvx from a configuration file
/// @param config_path Path to configuration file
/// @param grids Grid map from host application
/// @param profiles Profile map from host application
/// @param radiators Radiator map from host application
/// @param error Error struct to indicate success or failure
void Create(const char *config_path, Error *error);
void Create(const char *config_path, GridMap *grids, ProfileMap *profiles, RadiatorMap *radiators, Error *error);

/// @brief Create a grid map. For now, this calls the interal tuvx fortran api, but will allow the change to c++ later on
/// to be transparent to downstream projects
Expand Down Expand Up @@ -58,7 +61,7 @@ namespace musica

// The external C API for TUVX
// callable by wrappers in other languages
TUVX *CreateTuvx(const char *config_path, Error *error);
TUVX *CreateTuvx(const char *config_path, GridMap *grids, ProfileMap *profiles, RadiatorMap *radiators, Error *error);
void DeleteTuvx(const TUVX *tuvx, Error *error);
GridMap *GetGridMap(TUVX *tuvx, Error *error);
ProfileMap *GetProfileMap(TUVX *tuvx, Error *error);
Expand Down
29 changes: 26 additions & 3 deletions src/test/unit/tuvx/tuvx_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ class TuvxCApiTest : public ::testing::Test
{
protected:
TUVX* tuvx;
GridMap* grids_from_host;
ProfileMap* profiles_from_host;
RadiatorMap* radiators_from_host;

// the function that google test actually calls before each test
void SetUp() override
{
tuvx = nullptr;
grids_from_host = nullptr;
profiles_from_host = nullptr;
radiators_from_host = nullptr;
}

void SetUp(const char* config_path)
{
Error error;
tuvx = CreateTuvx(config_path, &error);
grids_from_host = CreateGridMap(&error);
profiles_from_host = CreateProfileMap(&error);
radiators_from_host = CreateRadiatorMap(&error);
tuvx = CreateTuvx(config_path, grids_from_host, profiles_from_host, radiators_from_host, &error);
if (!IsSuccess(error))
{
std::cerr << "Error creating TUVX instance: " << error.message_.value_ << std::endl;
Expand All @@ -37,8 +46,13 @@ class TuvxCApiTest : public ::testing::Test
Error error;
DeleteTuvx(tuvx, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteGridMap(grids_from_host, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteProfileMap(profiles_from_host, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteRadiatorMap(radiators_from_host, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteError(&error);
tuvx = nullptr;
}
};

Expand All @@ -60,8 +74,17 @@ TEST_F(TuvxCApiTest, DetectsNonexistentConfigFile)
{
const char* config_path = "nonexisting.yml";
Error error;
TUVX* tuvx = CreateTuvx(config_path, &error);
GridMap* grids_from_host = CreateGridMap(&error);
ProfileMap* profiles_from_host = CreateProfileMap(&error);
RadiatorMap* radiators_from_host = CreateRadiatorMap(&error);
TUVX* tuvx = CreateTuvx(config_path, grids_from_host, profiles_from_host, radiators_from_host, &error);
ASSERT_FALSE(IsSuccess(error));
DeleteGridMap(grids_from_host, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteProfileMap(profiles_from_host, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteRadiatorMap(radiators_from_host, &error);
ASSERT_TRUE(IsSuccess(error));
DeleteError(&error);
}

Expand Down
12 changes: 7 additions & 5 deletions src/tuvx/tuvx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace musica
{

// TUVX external C API functions

TUVX *CreateTuvx(const char *config_path, Error *error)
TUVX *CreateTuvx(const char *config_path, GridMap *grids, ProfileMap *profiles, RadiatorMap *radiators, Error *error)
{
DeleteError(error);
TUVX *tuvx = new TUVX();
tuvx->Create(config_path, error);

tuvx->Create(config_path, grids, profiles, radiators, error);
if (!IsSuccess(*error))
{
delete tuvx;
Expand Down Expand Up @@ -49,6 +49,7 @@ namespace musica
GridMap *GetGridMap(TUVX *tuvx, Error *error)
{
DeleteError(error);

return tuvx->CreateGridMap(error);
}

Expand All @@ -67,7 +68,7 @@ namespace musica
// TUVX class functions

TUVX::TUVX()
: tuvx_()
: tuvx_(nullptr)
{
}

Expand All @@ -79,7 +80,7 @@ namespace musica
tuvx_ = nullptr;
}

void TUVX::Create(const char *config_path, Error *error)
void TUVX::Create(const char *config_path, GridMap *grids, ProfileMap *profiles, RadiatorMap *radiators, Error *error)
{
int parsing_status = 0; // 0 on success, 1 on failure
try
Expand Down Expand Up @@ -115,6 +116,7 @@ namespace musica
{
*error = NoError();
int error_code = 0;

GridMap *grid_map = new GridMap(InternalGetGridMap(tuvx_, &error_code));
if (error_code != 0)
{
Expand Down
Loading