Skip to content

Commit

Permalink
Merge pull request #172 from NCAR/55-vector-ordered-rosenbrock
Browse files Browse the repository at this point in the history
Vector-ordered Rosenbrock Solver
  • Loading branch information
boulderdaze authored Jul 15, 2024
2 parents b7d41cd + 830b426 commit 2fff1fb
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 131 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ option(MUSICA_BUILD_DOCS "Build the documentation" OFF)
option(MUSICA_ENABLE_MICM "Enable MICM" ON)
option(MUSICA_ENABLE_TUVX "Enable TUV-x" ON)

set(MUSICA_SET_MICM_VECTOR_MATRIX_SIZE "1" CACHE STRING "Set MICM vector-ordered matrix dimension")

cmake_dependent_option(
MUSICA_ENABLE_PYTHON_LIBRARY "Adds pybind11, a lightweight header-only library that exposes C++ types in Python and vice versa" OFF "MUSICA_BUILD_C_CXX_INTERFACE" OFF)

Expand Down
1 change: 1 addition & 0 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ if (MUSICA_ENABLE_MICM AND MUSICA_BUILD_C_CXX_INTERFACE)
)
set(MICM_ENABLE_TESTS OFF)
set(MICM_ENABLE_EXAMPLES OFF)
set(MICM_DEFAULT_VECTOR_MATRIX_SIZE ${MUSICA_SET_MICM_VECTOR_MATRIX_SIZE})

FetchContent_MakeAvailable(micm)
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/test_util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function(create_standard_test_cxx)
add_executable(test_${TEST_NAME} ${TEST_SOURCES})
target_link_libraries(test_${TEST_NAME} PUBLIC musica::musica GTest::gtest_main)
if(MUSICA_ENABLE_OPENMP)
target_link_libraries(test_${TEST_NAME} PUBLIC OpenMP::OpenMP_CXX OpenMP::OpenMP_Fortran)
target_link_libraries(test_${TEST_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()
if(NOT DEFINED TEST_WORKING_DIRECTORY)
set(TEST_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.fortran-gcc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM fedora:35

ARG BUILD_TYPE=release
ARG BUILD_TYPE=Release

RUN dnf -y update \
&& dnf -y install \
Expand Down
6 changes: 2 additions & 4 deletions docker/Dockerfile.fortran-intel
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ COPY . musica
RUN cd musica \
&& cmake -S . \
-B build \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
&& cd build \
&& make install -j

Expand All @@ -69,6 +69,4 @@ RUN cd musica/fortran/test/fetch_content_integration \
-D MUSICA_ENABLE_MEMCHECK=ON \
&& make -j

WORKDIR musica/fortran/test/fetch_content_integration/build
RUN cp -r /musica/build/_deps/tuvx-src/examples/ .
RUN cp -r /musica/build/_deps/tuvx-src/data/ .
WORKDIR musica/fortran/test/fetch_content_integration/build
4 changes: 3 additions & 1 deletion docker/Dockerfile.memcheck
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM fedora:35

ARG BUILD_TYPE=Debug

RUN dnf -y update \
&& dnf -y install \
cmake \
Expand All @@ -22,7 +24,7 @@ COPY . musica
RUN cd musica \
&& cmake -S . \
-B build \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D MUSICA_ENABLE_MEMCHECK=ON \
&& cd build \
&& make install -j 8
Expand Down
8 changes: 5 additions & 3 deletions docker/Dockerfile.mpi
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM fedora:35

ARG BUILD_TYPE=Debug

RUN dnf -y update \
&& dnf install -y sudo \
&& adduser test_user \
Expand Down Expand Up @@ -38,9 +40,9 @@ RUN sudo chown -R test_user.test_user musica
RUN cd musica \
&& cmake -S . \
-B build \
-D CMAKE_BUILD_TYPE=debug \
-D ENABLE_TESTS=ON \
-D ENABLE_MPI=ON \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D MUSICA_ENABLE_TESTS=ON \
-D MUSICA_ENABLE_MPI=ON \
-D CMAKE_Fortran_COMPILER=/usr/lib64/openmpi/bin/mpif90 \
-D CMAKE_C_COMPILER=/usr/lib64/openmpi/bin/mpicc \
-D CMAKE_CXX_COMPILER=/usr/lib64/openmpi/bin/mpicxx \
Expand Down
10 changes: 6 additions & 4 deletions docker/Dockerfile.mpi_openmp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM fedora:35

ARG BUILD_TYPE=Debug

RUN dnf -y update \
&& dnf install -y sudo \
&& adduser test_user \
Expand Down Expand Up @@ -38,10 +40,10 @@ RUN sudo chown -R test_user.test_user musica
RUN cd musica \
&& cmake -S . \
-B build \
-D CMAKE_BUILD_TYPE=debug \
-D ENABLE_MPI=ON \
-D ENABLE_OPENMP=ON \
-D ENABLE_TESTS=ON \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D MUSICA_ENABLE_MPI=ON \
-D MUSICA_ENABLE_OPENMP=ON \
-D MUSICA_ENABLE_TESTS=ON \
-D CMAKE_Fortran_COMPILER=/usr/lib64/openmpi/bin/mpif90 \
-D CMAKE_C_COMPILER=/usr/lib64/openmpi/bin/mpicc \
-D CMAKE_CXX_COMPILER=/usr/lib64/openmpi/bin/mpicxx \
Expand Down
8 changes: 5 additions & 3 deletions docker/Dockerfile.openmp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM fedora:35

ARG BUILD_TYPE=Debug

RUN dnf -y update \
&& dnf install -y sudo \
&& adduser test_user \
Expand Down Expand Up @@ -37,9 +39,9 @@ RUN sudo chown -R test_user.test_user musica
RUN cd musica \
&& cmake -S . \
-B build \
-D CMAKE_BUILD_TYPE=debug \
-D ENABLE_OPENMP:BOOL=TRUE \
-D ENABLE_TESTS=ON \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D MUSICA_ENABLE_OPENMP=ON \
-D MUSICA_ENABLE_TESTS=ON \
-D CMAKE_Fortran_COMPILER=/usr/lib64/openmpi/bin/mpif90 \
-D CMAKE_C_COMPILER=/usr/lib64/openmpi/bin/mpicc \
-D CMAKE_CXX_COMPILER=/usr/lib64/openmpi/bin/mpic++ \
Expand Down
4 changes: 3 additions & 1 deletion docker/Dockerfile.python
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM fedora:latest

ARG BUILD_TYPE=Release

RUN dnf -y update \
&& dnf -y install \
cmake \
Expand Down Expand Up @@ -45,7 +47,7 @@ RUN pip install -r requirements.txt
RUN cd musica \
&& cmake -S . \
-B build \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D MUSICA_ENABLE_PYTHON_LIBRARY=ON \
-D MUSICA_ENABLE_TUVX=OFF \
&& cd build \
Expand Down
24 changes: 18 additions & 6 deletions fortran/micm.F90
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module musica_micm
implicit none

public :: micm_t, solver_stats_t, get_micm_version
public :: Rosenbrock, RosenbrockStandardOrder
private

!> Wrapper for c solver stats
Expand All @@ -25,13 +26,22 @@ module musica_micm
real(c_double) :: final_time_ = 0._c_double
end type solver_stats_t_c

! We could use Fortran 2023 enum type feature if Fortran 2023 is supported
! https://fortran-lang.discourse.group/t/enumerator-type-in-bind-c-derived-type-best-practice/5947/2
enum, bind(c)
enumerator :: Rosenbrock = 1
enumerator :: RosenbrockStandardOrder = 2
end enum

interface
function create_micm_c(config_path, error) bind(C, name="CreateMicm")
function create_micm_c(config_path, solver_type, num_grid_cells, error) bind(C, name="CreateMicm")
use musica_util, only: error_t_c
import c_ptr, c_int, c_char
character(kind=c_char), intent(in) :: config_path(*)
type(error_t_c), intent(inout) :: error
type(c_ptr) :: create_micm_c
character(kind=c_char), intent(in) :: config_path(*)
integer(kind=c_int), value, intent(in) :: solver_type
integer(kind=c_int), value, intent(in) :: num_grid_cells
type(error_t_c), intent(inout) :: error
type(c_ptr) :: create_micm_c
end function create_micm_c

subroutine delete_micm_c(micm, error) bind(C, name="DeleteMicm")
Expand Down Expand Up @@ -182,10 +192,12 @@ function get_micm_version() result(value)
value = string_t(string_c)
end function get_micm_version

function constructor(config_path, error) result( this )
function constructor(config_path, solver_type, num_grid_cells, error) result( this )
use musica_util, only: error_t_c, error_t, copy_mappings
type(micm_t), pointer :: this
character(len=*), intent(in) :: config_path
integer(c_int), intent(in) :: solver_type
integer(c_int), intent(in) :: num_grid_cells
type(error_t), intent(inout) :: error
character(len=1, kind=c_char) :: c_config_path(len_trim(config_path)+1)
integer :: n, i
Expand All @@ -201,7 +213,7 @@ function constructor(config_path, error) result( this )
end do
c_config_path(n+1) = c_null_char

this%ptr = create_micm_c(c_config_path, error_c)
this%ptr = create_micm_c(c_config_path, solver_type, num_grid_cells, error_c)
error = error_t(error_c)
if (.not. error%is_success()) then
deallocate(this)
Expand Down
11 changes: 8 additions & 3 deletions fortran/test/fetch_content_integration/test_micm_api.F90
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ program test_micm_api
use, intrinsic :: iso_c_binding
use, intrinsic :: ieee_arithmetic
use musica_micm, only: micm_t, solver_stats_t, get_micm_version
use musica_micm, only: Rosenbrock, RosenbrockStandardOrder
use musica_util, only: assert, error_t, mapping_t, string_t

#include "micm/util/error.hpp"
Expand All @@ -32,6 +33,8 @@ subroutine test_api()
real(c_double), dimension(5) :: concentrations
real(c_double), dimension(3) :: user_defined_reaction_rates
character(len=256) :: config_path
integer(c_int) :: solver_type
integer(c_int) :: num_grid_cells
character(len=:), allocatable :: string_value
real(c_double) :: double_value
integer(c_int) :: int_value
Expand All @@ -42,21 +45,23 @@ subroutine test_api()
real(c_double), parameter :: GAS_CONSTANT = 8.31446261815324_c_double ! J mol-1 K-1
integer :: i

config_path = "configs/chapman"
solver_type = Rosenbrock
num_grid_cells = 1
time_step = 200
temperature = 272.5
pressure = 101253.4
air_density = pressure / ( GAS_CONSTANT * temperature )
num_concentrations = 5
concentrations = (/ 0.75, 0.4, 0.8, 0.01, 0.02 /)
config_path = "configs/chapman"
num_user_defined_reaction_rates = 3
user_defined_reaction_rates = (/ 0.1, 0.2, 0.3 /)

micm_version = get_micm_version()
print *, "[test micm fort api] MICM version ", micm_version%get_char_array()

write(*,*) "[test micm fort api] Creating MICM solver..."
micm => micm_t(config_path, error)
micm => micm_t(config_path, solver_type, num_grid_cells, error)
ASSERT( error%is_success() )

do i = 1, size( micm%species_ordering )
Expand Down Expand Up @@ -116,7 +121,7 @@ subroutine test_api()
ASSERT( error%is_error( MICM_ERROR_CATEGORY_SPECIES, \
MICM_SPECIES_ERROR_CODE_PROPERTY_NOT_FOUND ) )
deallocate( micm )
micm => micm_t( "configs/invalid", error )
micm => micm_t( "configs/invalid", solver_type, num_grid_cells, error )
ASSERT( error%is_error( MICM_ERROR_CATEGORY_CONFIGURATION, \
MICM_CONFIGURATION_ERROR_CODE_INVALID_FILE_PATH ) )
ASSERT( .not. associated( micm ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ program test_micm_box_model

use musica_util, only: error_t, string_t, mapping_t
use musica_micm, only: micm_t, solver_stats_t
use musica_micm, only: Rosenbrock, RosenbrockStandardOrder

implicit none

Expand All @@ -15,6 +16,8 @@ program test_micm_box_model
subroutine box_model()

character(len=256) :: config_path
integer(c_int) :: solver_type
integer(c_int) :: num_grid_cells

real(c_double), parameter :: GAS_CONSTANT = 8.31446261815324_c_double ! J mol-1 K-1

Expand All @@ -38,6 +41,8 @@ subroutine box_model()
integer :: i

config_path = "configs/analytical"
solver_type = RosenbrockStandardOrder
num_grid_cells = 1

time_step = 200
temperature = 273.0
Expand All @@ -47,7 +52,7 @@ subroutine box_model()
concentrations = (/ 1.0, 1.0, 1.0 /)

write(*,*) "Creating MICM solver..."
micm => micm_t(config_path, error)
micm => micm_t(config_path, solver_type, num_grid_cells, error)

do i = 1, size( micm%species_ordering )
associate(the_mapping => micm%species_ordering(i))
Expand Down
Loading

0 comments on commit 2fff1fb

Please sign in to comment.