Skip to content

Commit

Permalink
Merge pull request #35 from NCAR/adding_solver
Browse files Browse the repository at this point in the history
Added creating solver interface
  • Loading branch information
boulderdaze authored Oct 17, 2023
2 parents 3e2fd13 + b81615d commit f786f3d
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 104 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN dnf -y update \
lcov \
make \
netcdf-fortran-devel \
json-devel \
valgrind \
&& dnf clean all

Expand All @@ -35,5 +36,5 @@ RUN cd musica \
-D ENABLE_TESTS=ON \
&& cd build \
&& make install -j 8

WORKDIR musica/build
5 changes: 5 additions & 0 deletions Dockerfile.fortran
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN apt update \
libcurl4-openssl-dev \
libhdf5-dev \
m4 \
nlohmann-json3-dev \
vim \
zlib1g-dev \
git \
Expand Down Expand Up @@ -44,6 +45,7 @@ RUN cd musica \
&& cmake -S . \
-B build \
-D ENABLE_TESTS=ON \
-D ENABLE_TUVX=OFF \
&& cd build \
&& make install -j 8

Expand All @@ -67,4 +69,7 @@ RUN cd musica/musica-fortran/test \
&& cmake .. \
&& make

RUN cd musica/musica-fortran/test \
&& cp -r micm_config ./build/micm_config

WORKDIR musica/musica-fortran/test/build
1 change: 1 addition & 0 deletions Dockerfile.mpi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RUN sudo dnf -y install \
make \
netcdf-fortran-devel \
openmpi-devel \
json-devel \
valgrind-openmpi \
&& dnf clean all

Expand Down
1 change: 1 addition & 0 deletions Dockerfile.mpi_openmp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN sudo dnf -y install \
lcov \
make \
netcdf-fortran-devel \
json-devel \
openmpi-devel \
valgrind-openmpi \
&& dnf clean all
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.openmp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN sudo dnf -y install \
lcov \
make \
netcdf-fortran-devel \
json-devel \
openmpi-devel \
valgrind-openmpi \
&& dnf clean all
Expand Down
2 changes: 1 addition & 1 deletion lib/tuv-x
2 changes: 1 addition & 1 deletion musica-fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(netcdff IMPORTED_TARGET REQUIRED netcdf-fortran)

# Find MUSICA package
find_package(musica 0.3.0 REQUIRED)
find_package(musica 0.4.0 REQUIRED)

target_link_libraries(musica-fortran
PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion musica-fortran/src/micm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(musica-fortran
PUBLIC
interface.F90
micm_mod.F90
)
22 changes: 0 additions & 22 deletions musica-fortran/src/micm/interface.F90

This file was deleted.

32 changes: 32 additions & 0 deletions musica-fortran/src/micm/micm_c_def.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
interface
function create_micm_c(config_path) bind(C, name="create_micm")
use iso_c_binding
implicit none
type(c_ptr) :: create_micm_c
character(len=1, kind=C_CHAR), intent(in) :: config_path(*)
end function

subroutine delete_micm_c(micm_t) bind(C, name="delete_micm")
use iso_c_binding
implicit none
type(c_ptr), value :: micm_t
end subroutine

function micm_create_solver_c(micm_t) bind(C, name="micm_create_solver")
use iso_c_binding
implicit none
integer(c_int) :: micm_create_solver_c ! TODO(jiwon) return value?
type(c_ptr), intent(in), value :: micm_t
end function

subroutine micm_solve_c(micm_t, temperature, pressure, time_step, concentrations, num_concentrations) bind(C, name="micm_solve")
use iso_c_binding
implicit none
type(c_ptr), intent(in), value :: micm_t
real(c_double), value :: temperature
real(c_double), value :: pressure
real(c_double), value :: time_step
real(c_double), dimension(*), intent(inout) :: concentrations
integer(c_size_t), value, intent(in) :: num_concentrations
end subroutine
end interface
60 changes: 60 additions & 0 deletions musica-fortran/src/micm/micm_mod.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module micm
use iso_c_binding
implicit none

private
public :: micm_t

include "micm_c_def.F90"

type micm_t
private
type(c_ptr) :: ptr
contains
procedure :: create_solver => micm_create_solver
procedure :: solve => micm_solve
final :: delete_micm
end type

interface micm_t
procedure create_micm
end interface

contains
function create_micm(config_path)
type(micm_t) :: create_micm
character(len=*), intent(in) :: config_path
character(len=1, kind=C_CHAR) :: c_config_path(len_trim(config_path) + 1)
integer :: N, i

! Converting Fortran string to C string
N = len_trim(config_path)
do i = 1, N
c_config_path(i) = config_path(i:i)
end do
c_config_path(N + 1) = C_NULL_CHAR

create_micm%ptr = create_micm_c(c_config_path)
end function

subroutine delete_micm(this)
type(micm_t) :: this
call delete_micm_c(this%ptr)
end subroutine

integer function micm_create_solver(this)
class(micm_t), intent(in) :: this
micm_create_solver = micm_create_solver_c(this%ptr)
end function

subroutine micm_solve(this, temperature, pressure, time_step, concentrations, num_concentrations)
class(micm_t), intent(in) :: this
real(c_double), intent(in) :: temperature
real(c_double), intent(in) :: pressure
real(c_double), intent(in) :: time_step
real(c_double), dimension(*), intent(inout) :: concentrations
integer(c_size_t), intent(in) :: num_concentrations
call micm_solve_c(this%ptr, temperature, pressure, time_step, concentrations, num_concentrations)
end subroutine

end module
3 changes: 2 additions & 1 deletion musica-fortran/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ include(FetchContent)

set(USE_MUSICA OFF)
set(USE_MUSICA_FORTRAN ON)
set(ENABLE_TUVX OFF)

FetchContent_Declare(musica
GIT_REPOSITORY https://github.com/NCAR/musica.git
GIT_TAG bf1b77b ## todo: jiwon update this
GIT_TAG bad9756 # TODO(jiwon) - update the tag
)

FetchContent_MakeAvailable(musica)
Expand Down
1 change: 1 addition & 0 deletions musica-fortran/test/micm_config/mechanism.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-data": [{"name": "Chapman", "type": "MECHANISM", "reactions": [{"type": "ARRHENIUS", "reactants": {"spec_0": {}, "spec_1": {}, "spec_2": {}}, "products": {"spec_3": {"yield": 0.7}, "spec_4": {"yield": 0.6}, "spec_5": {"yield": 0.4}, "spec_6": {"yield": 0.1}}, "MUSICA name": "reaction_0", "A": 0.06307742014373313, "C": -4.580502916806157}, {"type": "ARRHENIUS", "reactants": {"spec_7": {}, "spec_8": {}}, "products": {"spec_9": {"yield": 1.4}}, "MUSICA name": "reaction_1", "A": 0.04349975747486285, "C": 0.9227077653687363}, {"type": "ARRHENIUS", "reactants": {"spec_3": {}, "spec_6": {}, "spec_8": {}}, "products": {"spec_5": {"yield": 1.2}, "spec_0": {"yield": 0.5}, "spec_7": {"yield": 0.6}, "spec_8": {"yield": 0.3}}, "MUSICA name": "reaction_2", "A": 0.06852791722073094, "C": 3.2487346324460002}, {"type": "ARRHENIUS", "reactants": {"spec_8": {}, "spec_4": {}}, "products": {"spec_9": {"yield": 0.5}, "spec_1": {"yield": 0.7}, "spec_6": {"yield": 1.1}, "spec_8": {"yield": 1.3}, "spec_4": {"yield": 1.2}, "spec_3": {"yield": 0.4}}, "MUSICA name": "reaction_3", "A": 0.25712668426775886, "C": -1.6984663902168808}, {"type": "ARRHENIUS", "reactants": {"spec_9": {}, "spec_1": {}}, "products": {"spec_5": {"yield": 0.6}}, "MUSICA name": "reaction_4", "A": 0.09981506051562195, "C": 4.434345327828195}, {"type": "ARRHENIUS", "reactants": {"spec_8": {}, "spec_1": {}, "spec_3": {}}, "products": {"spec_0": {"yield": 0.7}}, "MUSICA name": "reaction_5", "A": 0.04095822066544459, "C": -0.9768070373515956}, {"type": "ARRHENIUS", "reactants": {"spec_1": {}, "spec_7": {}}, "products": {"spec_8": {"yield": 1.1}, "spec_1": {"yield": 1.5}}, "MUSICA name": "reaction_6", "A": 0.05421274033990674, "C": -2.8281780970323145}, {"type": "ARRHENIUS", "reactants": {"spec_3": {}}, "products": {"spec_5": {"yield": 0.8}, "spec_8": {"yield": 0.6}}, "MUSICA name": "reaction_7", "A": 0.48935553510912033, "C": -4.956655119700008}, {"type": "ARRHENIUS", "reactants": {"spec_3": {}}, "products": {"spec_7": {"yield": 0.9}, "spec_3": {"yield": 1.0}, "spec_2": {"yield": 1.3}}, "MUSICA name": "reaction_8", "A": 0.18950947076801095, "C": 0.04442551245927007}, {"type": "ARRHENIUS", "reactants": {"spec_6": {}}, "products": {"spec_4": {"yield": 0.7}, "spec_9": {"yield": 0.5}, "spec_3": {"yield": 0.8}, "spec_1": {"yield": 0.6}, "spec_5": {"yield": 0.2}}, "MUSICA name": "reaction_9", "A": 0.021971337763203702, "C": -2.81627531289537}]}]}
1 change: 1 addition & 0 deletions musica-fortran/test/micm_config/species.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-data": [{"name": "spec_0", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_1", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_2", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_3", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_4", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_5", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_6", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_7", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_8", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "spec_9", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}]}
44 changes: 36 additions & 8 deletions musica-fortran/test/test_musica_api.F90
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
program test_musica_api
use iso_c_binding
use micm
program test
use iso_c_binding
use micm
implicit none
type(micm_t) :: m

character(len=5, kind=c_char) :: c_filepath
type(c_funptr) :: csolver_func_pointer
c_filepath = 'asdf'
csolver_func_pointer = get_solver(c_filepath)
real(c_double) :: temperature
real(c_double) :: pressure
real(c_double) :: time_step
real(c_double), dimension(10) :: concentrations
integer(c_size_t) :: num_concentrations

end program test_musica_api
temperature = 10d0
pressure = 20d0
time_step = 1d0
concentrations = (/ 1d0, 2d0, 3d0, 4d0, 5d0, 6d0, 7d0, 8d0, 9d0, 10d0 /)
num_concentrations = 10

write(*,*) " * [Fortran] Creating MICM"
m = micm_t("micm_config")

write(*,*) " * [Fortran] Creating solver"
write(*,*) " * [Fortran] Solver creating status indicates ", m%create_solver(), " (1 is success, else failure) "

write(*,*) " * [Fortran] Initial temp", temperature
write(*,*) " * [Fortran] Initial pressure", pressure
write(*,*) " * [Fortran] Initial time_step", time_step
write(*,*) " * [Fortran] Initial concentrations", concentrations
write(*,*) " * [Fortran] Initial number of concentrations", num_concentrations

write(*,*) " * [Fortran] Starting to solve"
call m%solve(temperature, pressure, time_step, concentrations, num_concentrations)
write(*,*) " * [Fortran] After solving, concentrations", concentrations

write(*,*) " * [Fortran] Calling destructor for MICM"
call m%delete

end program
6 changes: 3 additions & 3 deletions musica/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ target_sources(musica

add_subdirectory(${PROJECT_SOURCE_DIR}/lib/musica-core/src ${MUSICA_LIB_DIR}/musica-core/src)

# ####################
####################
# TUV-x
if (ENABLE_TUVX)
add_definitions(-DMUSICA_USE_TUVX)
Expand Down Expand Up @@ -122,7 +122,7 @@ if (ENABLE_TUVX)
)
endif()

# ####################
####################
# MICM
if (ENABLE_MICM)
add_definitions(-DMUSICA_USE_MICM)
Expand All @@ -136,7 +136,7 @@ if (ENABLE_MICM)

install(
DIRECTORY
${PROJECT_SOURCE_DIR}/lib/micm/include/micm
${PROJECT_SOURCE_DIR}/lib/micm/include
DESTINATION
${INSTALL_MOD_DIR}
)
Expand Down
24 changes: 0 additions & 24 deletions musica/include/micm/interface.hpp

This file was deleted.

38 changes: 38 additions & 0 deletions musica/include/micm/micm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <string>
#include <vector>
#include <cstddef>

#include <micm/solver/rosenbrock.hpp>
#include <micm/util/sparse_matrix_vector_ordering.hpp>
#include <micm/util/vector_matrix.hpp>


class MICM
{
public:
MICM(const std::string& config_path);
~MICM();

// TODO(jiwon): can return type indicate error?
int create_solver();
void solve(double temperature, double pressure, double time_step, double*& concentrations, size_t num_concentrations);

private:
static constexpr size_t NUM_GRID_CELLS = 1; // TODO(jiwon)

std::string config_path_;

// TODO(jiwon) - currently hard coded
std::vector<double> v_concentrations_;

// TODO(jiwon) - currently hard coded
template <class T = double>
using Vector1MatrixParam = micm::VectorMatrix<T, 1>;
template <class T = double>
using Vector1SparseMatrixParam = micm::SparseMatrix<T, micm::SparseMatrixVectorOrdering<1>>;
typedef micm::RosenbrockSolver<Vector1MatrixParam, Vector1SparseMatrixParam> VectorRosenbrockSolver;
VectorRosenbrockSolver* solver_;
};

18 changes: 18 additions & 0 deletions musica/include/micm/micm_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <cstddef>

#ifdef __cplusplus
extern "C" {
class MICM;
typedef MICM Micm;
#endif

Micm* create_micm(const char* config_path);
void delete_micm(const Micm* micm);
int micm_create_solver(Micm* micm);
void micm_solve(Micm* micm, double temperature, double pressure, double time_step,
double* concentrations, size_t num_concentrations);

#ifdef __cplusplus
}
#endif

3 changes: 2 additions & 1 deletion musica/src/micm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target_sources(musica
PRIVATE
interface.cpp
micm_c_api.cpp
micm.cpp
)
Loading

0 comments on commit f786f3d

Please sign in to comment.