Skip to content

Commit

Permalink
something different
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Mar 12, 2024
1 parent cb24aad commit 685f5e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
6 changes: 1 addition & 5 deletions src/micm/micm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <micm/configure/solver_config.hpp>
#include <micm/solver/rosenbrock_solver_parameters.hpp>
#include <musica/micm.hpp>
#include <musica/micm.hpp>

/**
* @brief Creates a new instance of the MICM class.
Expand All @@ -30,7 +29,7 @@ void create_micm(void** micm, int error_code)
{
try
{
*micm = new MICM(); // Attempt to create a new MICM instance
*micm = new MICM();
error_code = 0; // 0 indicates success
}
catch (const std::bad_alloc&)
Expand All @@ -49,12 +48,9 @@ void create_micm(void** micm, int error_code)
*/
void delete_micm(void **micm)
{
// Check if the pointer is not null
if (*micm)
{
// Delete the MICM instance
delete static_cast<MICM *>(*micm);
// Set the pointer to null
*micm = nullptr;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/connections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ include(test_util)
if (MUSICA_ENABLE_MICM)
create_standard_test_cxx(NAME connect_to_micm SOURCES micm.cpp)
create_standard_test_cxx(NAME micm_c_api SOURCES micm_c_api.cpp)

################################################################################
# Copy test data

add_custom_target(copy_unit_test_configs ALL ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/configs ${CMAKE_BINARY_DIR}/configs)
endif()
82 changes: 39 additions & 43 deletions src/test/connections/micm_c_api.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,53 @@
#include <musica/micm.hpp>

#include <stdio.h>


int main()
{
const char* config_path = "chapman";
void* micm = nullptr;
int error_code = 0;

create_micm(&micm, error_code);

if (error_code == 0)
{
printf("[test micm c api] Created MICM instance.\n");
#include <gtest/gtest.h>

// Test fixture for the MICM C API
class MicmCApiTest : public ::testing::Test {
protected:
void* micm;
int error_code;

void SetUp() override {
micm = nullptr;
error_code = 0;
create_micm(&micm, error_code);
}
else
{
printf("[test micm c api] Failed in creating MICM instance.\n");
return 1;

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

// Test case for creating the MICM instance
TEST_F(MicmCApiTest, CreateMicmInstance) {
ASSERT_EQ(error_code, 0);
ASSERT_NE(micm, nullptr);
}

printf("[test micm c api] Parsing configuration file: %s\n" , config_path);
// 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};

for (int i=0; i<num_concentrations; i++)
{
printf("[test micm c api] Initial concentrations:\t%e\n", concentrations[i]);
}

if (solver_creation_status == 0)
{
printf("[test micm c api] Created MICM solver. Solving starts...\n");

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

printf("[test micm c api] Finished solving.\n");

for (int i=0; i<num_concentrations; i++)
{
printf("[test micm c api] Solved concentrations:\t%e\n", concentrations[i]);
}
}
else
{
printf("[test micm c api] Failed in creating solver.\n");
}
micm_solve(&micm, time_step, temperature, pressure, num_concentrations, concentrations);

delete_micm(&micm);
// Add assertions to check the solved concentrations
ASSERT_EQ(concentrations[0], 0.75);
ASSERT_NE(concentrations[1], 0.4);
ASSERT_NE(concentrations[2], 0.8);
ASSERT_NE(concentrations[3], 0.01);
ASSERT_NE(concentrations[4], 0.02);
}

0 comments on commit 685f5e5

Please sign in to comment.