Skip to content

Commit

Permalink
79 allow updates to custom rate constant parameters and mapping by na…
Browse files Browse the repository at this point in the history
…me (#80)

* converting tests into python unit tests, adding analytical test

* moving configs

* moving one more config

* adding python interfade, stubbing c stuff for fortran

* passing rate constants in solve with python

* trying to fix mac build

* fine I'll add the three

* trying a python setup action

* release builds in docker files

* am annoyed

* thing

* stuff

* using most recent micm version
  • Loading branch information
K20shores authored Apr 2, 2024
1 parent d2a2dcd commit 19590a7
Show file tree
Hide file tree
Showing 41 changed files with 368 additions and 214 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-python@v5
- run: pip install -r python/requirements.txt

- name: Run Cmake
run: cmake -S . -B build -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -D MUSICA_ENABLE_PYTHON_LIBRARY=ON

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-python@v5
- run: pip install -r python/requirements.txt

- name: Run Cmake
run: cmake -S . -B build -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -D MUSICA_ENABLE_PYTHON_LIBRARY=ON

Expand Down
20 changes: 18 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
with:
platform: ${{ matrix.architecture }}
version: 12.2.0 # https://github.com/egor-tensin/setup-mingw/issues/14

- uses: actions/setup-python@v5
- run: pip install -r python/requirements.txt

- name: Run Cmake
run: cmake -S . -B build -G "MinGW Makefiles" -D MUSICA_ENABLE_PYTHON_LIBRARY=ON
Expand All @@ -48,11 +51,12 @@ jobs:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
if: ${{ matrix.architecture == 'Win32' }}
with:
python-version: '3.x'
architecture: x86
- run: pip install -r python/requirements.txt

- name: Run CMake
run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -D MUSICA_ENABLE_PYTHON_LIBRARY=ON
Expand All @@ -74,12 +78,14 @@ jobs:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
if: ${{ matrix.architecture == 'Win32' }}
with:
python-version: '3.x'
architecture: x86

- run: pip install -r python/requirements.txt

- name: Run CMake
run: cmake -S . -B build -G "Visual Studio 17 2022" -A ${{ matrix.architecture }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -D MUSICA_ENABLE_PYTHON_LIBRARY=ON

Expand All @@ -97,12 +103,19 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Install Clang
run: curl -fsSL -o LLVM${{ matrix.version }}.exe https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.version }}.0.0/LLVM-${{ matrix.version }}.0.0-win64.exe ; 7z x LLVM${{ matrix.version }}.exe -y -o"C:/Program Files/LLVM"

- uses: actions/setup-python@v5
- run: pip install -r python/requirements.txt

- name: Run CMake
run: cmake -S . -B build -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe" -DCMAKE_C_COMPILER="C:/Program Files/LLVM/bin/clang.exe" -G"MinGW Makefiles" -D MUSICA_ENABLE_PYTHON_LIBRARY=ON

- name: Build
run: cmake --build build --parallel

- name: Test
run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure

Expand All @@ -122,6 +135,9 @@ jobs:
python-version: '3.x'
architecture: x86

- uses: actions/setup-python@v5
- run: pip install -r python/requirements.txt

- name: Run CMake
run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -T ClangCL -D MUSICA_ENABLE_PYTHON_LIBRARY=ON

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions configs/analytical/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-files": ["species.json", "reactions.json"]}
1 change: 1 addition & 0 deletions configs/analytical/reactions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-data": [{"type": "MECHANISM", "name": "music box interactive configuration", "reactions": [{"type": "ARRHENIUS", "A": 0.00012, "B": 7, "C" : 75, "D": 50, "E": 0.5, "reactants": {"B": {"qty": 1}}, "products": {"C": {"yield": 1}, "irr__089f1f45-4cd8-4278-83d5-d638e98e4315": {"yield": 1}}}, {"type": "ARRHENIUS", "A": 0.004, "C" : 50, "reactants": {"A": {"qty": 1}}, "products": {"B": {"yield": 1}, "irr__2a109b21-bb24-41ae-8f06-7485fd36f1a7": {"yield": 1}}}]}]}
1 change: 1 addition & 0 deletions configs/analytical/species.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-data": [{"name": "A", "type": "CHEM_SPEC"}, {"name": "B", "type": "CHEM_SPEC"}, {"name": "C", "type": "CHEM_SPEC"}, {"name": "irr__089f1f45-4cd8-4278-83d5-d638e98e4315", "type": "CHEM_SPEC"}, {"name": "irr__2a109b21-bb24-41ae-8f06-7485fd36f1a7", "type": "CHEM_SPEC"}]}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion configs/chapman/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"species.json",
"reactions.json"
]
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions docker/Dockerfile.fortran-intel
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ RUN cd musica \
-B build \
-D MUSICA_ENABLE_TESTS=ON \
-D MUSICA_ENABLE_TUVX=OFF \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_BUILD_TYPE=Release \
&& cd build \
&& make install

Expand All @@ -45,7 +45,7 @@ ENV FC=ifx
RUN cd musica/fortran/test/fetch_content_integration \
&& mkdir build && cd build \
&& cmake .. \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_BUILD_TYPE=Release \
&& make

RUN cp -r /musica/configs/chapman musica/fortran/test/fetch_content_integration/build
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile.fortran-nvhpc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RUN cd musica \
-B build \
-D MUSICA_ENABLE_TESTS=ON \
-D MUSICA_ENABLE_TUVX=OFF \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_BUILD_TYPE=Release \
&& cd build \
&& make install

Expand All @@ -50,7 +50,7 @@ ENV FC=nvfortran
RUN cd musica/fortran/test/fetch_content_integration \
&& mkdir build && cd build \
&& cmake .. \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_BUILD_TYPE=Release \
&& make

RUN cp -r /musica/configs/chapman musica/fortran/test/fetch_content_integration/build
Expand Down
37 changes: 34 additions & 3 deletions fortran/micm_core.F90
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module micm_core

use iso_c_binding, only: c_ptr, c_char, c_int, c_double, c_null_char
use iso_c_binding, only: c_ptr, c_char, c_int, c_double, c_null_char, c_size_t, c_f_pointer
implicit none

public :: micm_t
private

type, bind(c) :: Mapping
character(kind=c_char, len=1) :: name(256)
integer(c_size_t) :: index
end type Mapping

interface
function create_micm_c(config_path, error_code) bind(C, name="create_micm")
import c_ptr, c_int, c_char
Expand All @@ -28,11 +33,26 @@ subroutine micm_solve_c(micm, time_step, temperature, pressure, num_concentratio
integer(kind=c_int), value, intent(in) :: num_concentrations
real(kind=c_double), intent(inout) :: concentrations(num_concentrations)
end subroutine micm_solve_c

function get_species_ordering(micm, array_size) bind(c, name="get_species_ordering")
import :: c_ptr, c_size_t
type(c_ptr), value :: micm
integer(kind=c_size_t), intent(out) :: array_size
type(c_ptr) :: get_species_ordering
end function get_species_ordering

type(c_ptr) function get_user_defined_reaction_rates_ordering(micm, array_size) &
bind(c, name="get_user_defined_reaction_rates_ordering")
import :: c_ptr, c_size_t
type(c_ptr), value :: micm
integer(kind=c_size_t), intent(out) :: array_size
end function get_user_defined_reaction_rates_ordering
end interface

type :: micm_t
private
type(c_ptr) :: ptr
type(Mapping), pointer :: species_ordering(:), reaction_rates_ordering(:)
integer(kind=c_size_t) :: species_ordering_length, reaction_rates_ordering_length
type(c_ptr), private :: ptr
contains
! Solve the chemical system
procedure :: solve
Expand All @@ -52,9 +72,11 @@ function constructor(config_path, errcode) result( this )
integer, intent(out) :: errcode
character(len=1, kind=c_char) :: c_config_path(len_trim(config_path)+1)
integer :: n, i
type(c_ptr) :: mappings_ptr

allocate( this )

print *, "Config Path:", config_path
n = len_trim(config_path)
do i = 1, n
c_config_path(i) = config_path(i:i)
Expand All @@ -66,6 +88,15 @@ function constructor(config_path, errcode) result( this )
if (errcode /= 0) then
return
end if

mappings_ptr = get_species_ordering(this%ptr, this%species_ordering_length)
call c_f_pointer(mappings_ptr, this%species_ordering, [this%species_ordering_length])

! this%reaction_rates_ordering = get_user_defined_reaction_rates_ordering(this%ptr, this%reaction_rates_ordering_length)

! do i = 1, this%reaction_rates_ordering_length
! print *, "Reaction Rate Name:", this%reaction_rates_ordering(i)%name, ", Index:", this%reaction_rates_ordering(i)%index
! end do
end function constructor

subroutine solve(this, time_step, temperature, pressure, num_concentrations, concentrations)
Expand Down
2 changes: 1 addition & 1 deletion fortran/test/fetch_content_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include(FetchContent)

FetchContent_Declare(musica-fortran
GIT_REPOSITORY https://github.com/NCAR/musica.git
GIT_TAG main
GIT_TAG 79-allow-updates-to-custom-rate-constant-parameters-and-mapping-by-name
)

set(MUSICA_BUILD_C_CXX_INTERFACE OFF)
Expand Down
2 changes: 2 additions & 0 deletions fortran/test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include(test_util)

create_standard_test_fortran(NAME musica_fortran_interface SOURCES test_musica_fortran_interface.F90)

if (MUSICA_ENABLE_TUVX)
create_standard_test_fortran(NAME connect_to_tuvx SOURCES tuvx.F90)
if (MUSICA_ENABLE_OPENMP)
Expand Down
40 changes: 40 additions & 0 deletions fortran/test/unit/test_musica_fortran_interface.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
program test_micm_fort_api
use iso_c_binding
use micm_core, only: micm_t

implicit none

type(micm_t), pointer :: micm
real(c_double) :: time_step
real(c_double) :: temperature
real(c_double) :: pressure
integer(c_int) :: num_concentrations
real(c_double), dimension(5) :: concentrations
integer :: errcode
character(len=7) :: config_path

time_step = 200
temperature = 272.5
pressure = 101253.3
num_concentrations = 5
concentrations = (/ 0.75, 0.4, 0.8, 0.01, 0.02 /)
config_path = "configs/chapman"

write(*,*) "[test micm fort api] Creating MICM solver..."
micm => micm_t(config_path, errcode)

if (errcode /= 0) then
write(*,*) "[test micm fort api] Failed in creating solver."
stop 3
endif

write(*,*) "[test micm fort api] Initial concentrations", concentrations

write(*,*) "[test micm fort api] Solving starts..."
call micm%solve(time_step, temperature, pressure, num_concentrations, concentrations)

write(*,*) "[test micm fort api] After solving, concentrations", concentrations

write(*,*) "[test micm fort api] Finished."

end program
28 changes: 20 additions & 8 deletions include/musica/micm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file contains the defintion of the MICM class, which represents a multi-component
* reactive transport model. It also includes functions for creating and deleting MICM instances with c binding
* Copyright (C) 2023-2024 National Center for Atmospheric Research,
*
*
* SPDX-License-Identifier: Apache-2.0* creating solvers, and solving the model.
*/
#pragma once
Expand All @@ -15,13 +15,22 @@

class MICM;

struct Mapping
{
char *name;
size_t index;
};

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif

MICM* create_micm(const char* config_path, int* error_code);
void delete_micm(const MICM* micm);
void micm_solve(MICM* micm, double time_step, double temperature, double pressure, int num_concentrations, double* concentrations);
MICM *create_micm(const char *config_path, int *error_code);
void delete_micm(const MICM *micm);
void micm_solve(MICM *micm, double time_step, double temperature, double pressure, int num_concentrations, double *concentrations, int num_custom_rate_parameters, double *custom_rate_parameters);
Mapping *get_species_ordering(MICM *micm, size_t *array_size);
Mapping *get_user_defined_reaction_rates_ordering(MICM *micm, size_t *array_size);

#ifdef __cplusplus
}
Expand All @@ -39,16 +48,19 @@ class MICM
/// @brief Create a solver by reading and parsing configuration file
/// @param config_path Path to configuration file or directory containing configuration file
/// @return 0 on success, 1 on failure in parsing configuration file
int create_solver(const std::string& config_path);
int create_solver(const std::string &config_path);

/// @brief Solve the system
/// @param time_step Time [s] to advance the state by
/// @para/ @briefm temperature Temperature [K]
/// @param pressure Pressure/ [P@brief C@param num_concentrations The number oconfiguration file
/// @param config_path Path to configuration file or directory containing configuration file
/// @return 0 on success, 1 on failure in parsing species' concentrations
/// @param concentrations Species's concentrations
void solve(double time_step, double temperature, double pressure, int num_concentrations, double* concentrations);
void solve(double time_step, double temperature, double pressure, int num_concentrations, double *concentrations, int num_custom_rate_parameters, double *custom_rate_parameters);

std::map<std::string, size_t> get_species_ordering();
std::map<std::string, size_t> get_user_defined_reaction_rates_ordering();

static constexpr size_t NUM_GRID_CELLS = 1;

Expand Down
2 changes: 1 addition & 1 deletion lib/micm
Submodule micm updated 47 files
+2 −1 .github/workflows/gh_pages.yml
+3 −3 .github/workflows/mac.yml
+2 −2 .github/workflows/ubuntu.yml
+2 −28 .github/workflows/windows.yml
+1 −1 CITATION.cff
+1 −1 CMakeLists.txt
+7 −2 docs/source/_static/switcher.json
+1 −1 docs/source/conf.py
+6 −4 include/micm/process/cuda_process_set.cuh
+10 −17 include/micm/process/cuda_process_set.hpp
+9 −9 include/micm/process/jit_process_set.hpp
+11 −9 include/micm/process/process_set.hpp
+35 −4 include/micm/solver/cuda_rosenbrock.cuh
+60 −11 include/micm/solver/cuda_rosenbrock.hpp
+0 −2 include/micm/solver/jit_rosenbrock.hpp
+7 −5 include/micm/solver/rosenbrock.hpp
+5 −9 include/micm/solver/rosenbrock.inl
+6 −1 include/micm/solver/state.hpp
+17 −1 include/micm/solver/state.inl
+20 −2 include/micm/util/cuda_param.hpp
+96 −0 include/micm/util/cuda_sparse_matrix.hpp
+13 −2 include/micm/util/cuda_vector_matrix.hpp
+3 −1 include/micm/util/sparse_matrix.hpp
+3 −3 include/micm/version.hpp
+47 −80 src/process/process_set.cu
+280 −37 src/solver/rosenbrock.cu
+6 −6 src/util/cuda_vector_matrix.cu
+3 −3 test/integration/analytical_policy.hpp
+7 −3 test/integration/e5.hpp
+7 −3 test/integration/hires.hpp
+7 −3 test/integration/oregonator.hpp
+1 −1 test/regression/RosenbrockChapman/regression_test_dforce_dy_policy.hpp
+35 −19 test/unit/process/test_cuda_process_set.cpp
+26 −26 test/unit/process/test_process_set_policy.hpp
+1 −1 test/unit/solver/CMakeLists.txt
+243 −0 test/unit/solver/test_cuda_rosenbrock.cpp
+15 −15 test/unit/solver/test_jit_rosenbrock.cpp
+7 −2 test/unit/solver/test_rosenbrock.cpp
+73 −0 test/unit/solver/test_state.cpp
+1 −0 test/unit/util/CMakeLists.txt
+16 −2 test/unit/util/cuda_matrix_utils.cu
+2 −1 test/unit/util/cuda_matrix_utils.cuh
+278 −0 test/unit/util/test_cuda_sparse_matrix_vector_ordering.cpp
+60 −0 test/unit/util/test_cuda_vector_matrix.cpp
+28 −26 test/unit/util/test_sparse_matrix_policy.hpp
+6 −6 test/unit/util/test_sparse_matrix_standard_ordering.cpp
+6 −6 test/unit/util/test_sparse_matrix_vector_ordering.cpp
7 changes: 2 additions & 5 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ target_link_libraries(musica_python PUBLIC musica::musica)


if(WIN32)

# makefiles on windows don't need the config directory
if (${CMAKE_GENERATOR} MATCHES "MinGW Makefiles")
set(PYTHON_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}")
Expand All @@ -24,7 +23,5 @@ else()
set(PYTHON_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}")
endif()

add_test(NAME musica_python_test
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHON_MODULE_PATH}
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/micm_test.py)

add_subdirectory(test)
23 changes: 0 additions & 23 deletions python/micm_test.py

This file was deleted.

1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy
9 changes: 9 additions & 0 deletions python/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_test(NAME musica_python_chapman_test
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHON_MODULE_PATH}
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/chapman.py)

add_test(NAME musica_python_analytical_test
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHON_MODULE_PATH}
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/analytical.py)
Loading

0 comments on commit 19590a7

Please sign in to comment.