Skip to content

Commit

Permalink
trying a different interface
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Mar 12, 2024
1 parent 27f402a commit a937230
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 108 deletions.
40 changes: 16 additions & 24 deletions fortran/micm_core.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,25 @@ module micm_core
private

interface
subroutine create_micm_c(micm, error_code) bind(C, name="create_micm")
import c_ptr, c_int
type(c_ptr), intent(out) :: micm
function create_micm_c(config_path, error_code) bind(C, name="create_micm")
import c_ptr, c_int, c_char
character(kind=c_char), intent(in) :: config_path(*)
integer(kind=c_int), intent(out) :: error_code
end subroutine create_micm_c
type(c_ptr) :: create_micm_c
end function create_micm_c

subroutine delete_micm_c(micm) bind(C, name="delete_micm")
import c_ptr
type(c_ptr), intent(inout) :: micm
type(c_ptr), intent(in) :: micm
end subroutine delete_micm_c

function micm_create_solver_c(micm, config_path) result(res) bind(C, name="micm_create_solver")
import c_ptr, c_char, c_int
type(c_ptr) :: micm
character(kind=c_char), intent(in) :: config_path(*)
integer(kind=c_int) :: res
end function micm_create_solver_c

subroutine micm_solve_c(micm, time_step, temperature, pressure, num_concentrations, concentrations) bind(C, name="micm_solve")
import c_ptr, c_double, c_int
type(c_ptr), intent(inout) :: micm
real(kind=c_double), value, intent(in) :: time_step
real(kind=c_double), value, intent(in) :: temperature
real(kind=c_double), value, intent(in) :: pressure
integer(kind=c_int), value, intent(in) :: num_concentrations
type(c_ptr), intent(in) :: micm
real(kind=c_double), intent(in) :: time_step
real(kind=c_double), intent(in) :: temperature
real(kind=c_double), intent(in) :: pressure
integer(kind=c_int), intent(in) :: num_concentrations
real(kind=c_double), intent(inout) :: concentrations(num_concentrations)
end subroutine micm_solve_c
end interface
Expand Down Expand Up @@ -61,19 +55,17 @@ function constructor(config_path, errcode) result( this )

allocate( this )

call create_micm_c(this%ptr, errcode)

if (errcode /= 0) then
return
end if

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

errcode = micm_create_solver_c(this%ptr, c_config_path)
this%ptr = create_micm_c(c_config_path, errcode)

if (errcode /= 0) then
return
end if
end function constructor

subroutine solve(this, time_step, temperature, pressure, num_concentrations, concentrations)
Expand Down
10 changes: 5 additions & 5 deletions include/musica/micm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#include <string>
#include <vector>

class MICM;

#ifdef __cplusplus
extern "C" {
#endif

void create_micm(void** micm, int error_code);
void delete_micm(void** micm);
int micm_create_solver(void** micm, const char* config_path);
void micm_solve(void** 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);

#ifdef __cplusplus
}
Expand Down
73 changes: 8 additions & 65 deletions src/micm/micm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,21 @@
#include <micm/solver/rosenbrock_solver_parameters.hpp>
#include <musica/micm.hpp>

/**
* @brief Creates a new instance of the MICM class.
*
* This function attempts to create a new instance of the MICM class and assigns it to the provided pointer.
* If the creation is successful, it sets the error code to 0. If a bad_alloc exception is thrown (indicating
* that the allocation failed), it sets the error code to 1.
*
* @param micm A pointer to a pointer where the new MICM instance will be stored.
* @param error_code A pointer to an integer where the error code will be stored.
*/
void create_micm(void** micm, int error_code)
{
try
{
*micm = new MICM();
error_code = 0; // 0 indicates success
}
catch (const std::bad_alloc&)
{
error_code = 1; // 1 indicates failure due to bad allocation
}
}

/**
* @brief Deletes an instance of the MICM class.
*
* This function attempts to delete an instance of the MICM class pointed to by the provided pointer.
* If the pointer is not null, it deletes the MICM instance and sets the pointer to null.
*
* @param micm A pointer to a pointer to the MICM instance to be deleted.
*/
void delete_micm(void **micm)
MICM* create_micm(const char* config_path, int* error_code)
{
if (*micm)
{
delete static_cast<MICM *>(*micm);
*micm = nullptr;
}
MICM* micm = new MICM();
*error_code = micm->create_solver(std::string(config_path));
return micm;
}


/**
* @brief Creates a solver for the MICM instance.
*
* This function attempts to create a solver for the MICM instance pointed to by the provided pointer.
* It uses the provided configuration path to configure the solver.
*
* @param micm A pointer to a pointer to the MICM instance for which the solver will be created.
* @param config_path A string representing the path to the configuration file for the solver.
* @return An integer representing the status of the solver creation (0 on success, 1 on failure).
*/
int micm_create_solver(void **micm, const char *config_path)
void delete_micm(const MICM* micm)
{
return static_cast<MICM *>(*micm)->create_solver(std::string(config_path));
delete micm;
}

/**
* @brief Solves the MICM model.
*
* This function attempts to solve the MICM model for the instance pointed to by the provided pointer.
* It uses the provided time step, temperature, pressure, number of concentrations, and concentrations.
*
* @param micm A pointer to a pointer to the MICM instance to be solved.
* @param time_step A double representing the time step for the solver.
* @param temperature A double representing the temperature for the solver.
* @param pressure A double representing the pressure for the solver.
* @param num_concentrations An integer representing the number of concentrations for the solver.
* @param concentrations A pointer to a double representing the concentrations for the solver.
*/
void micm_solve(void **micm, double time_step, double temperature, double pressure, int num_concentrations, double *concentrations)
void micm_solve(MICM* micm, double time_step, double temperature, double pressure, int num_concentrations, double* concentrations)
{
static_cast<MICM *>(*micm)->solve(time_step, temperature, pressure, num_concentrations, concentrations);
micm->solve(time_step, temperature, pressure, num_concentrations, concentrations);
}

MICM::MICM() : solver_(nullptr) {}
Expand Down
19 changes: 5 additions & 14 deletions src/test/connections/micm_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
// Test fixture for the MICM C API
class MicmCApiTest : public ::testing::Test {
protected:
void* micm;
MICM* micm;
int error_code;
const char* config_path = "configs/chapman";

void SetUp() override {
micm = nullptr;
error_code = 0;
create_micm(&micm, error_code);
micm = create_micm(config_path, &error_code);
}

void TearDown() override {
delete_micm(&micm);
delete_micm(micm);
}
};

Expand All @@ -24,25 +25,15 @@ TEST_F(MicmCApiTest, CreateMicmInstance) {
ASSERT_NE(micm, nullptr);
}

// Test case for creating the MICM solver
TEST_F(MicmCApiTest, CreateMicmSolver) {
const char* config_path = "configs/chapman";
int solver_creation_status = micm_create_solver(&micm, config_path);
ASSERT_EQ(solver_creation_status, 0);
}

// Test case for solving the MICM instance
TEST_F(MicmCApiTest, SolveMicmInstance) {
const char* config_path = "configs/chapman";
micm_create_solver(&micm, config_path);

double time_step = 200.0;
double temperature = 272.5;
double pressure = 101253.3;
int num_concentrations = 5;
double concentrations[] = {0.75, 0.4, 0.8, 0.01, 0.02};

micm_solve(&micm, time_step, temperature, pressure, num_concentrations, concentrations);
micm_solve(micm, time_step, temperature, pressure, num_concentrations, concentrations);

// Add assertions to check the solved concentrations
ASSERT_EQ(concentrations[0], 0.75);
Expand Down

0 comments on commit a937230

Please sign in to comment.