Skip to content

Commit

Permalink
Add yaml-cpp dependency and restructure codebase
Browse files Browse the repository at this point in the history
- Added yaml-cpp as a required package in CMakeLists and vcpkg.json.
- Refactored core sources to include "Config" and "Utils" directories.
- Updated `MalaSimCore` to link with yaml-cpp.
- Removed deprecated `Random` module from `Core`, including README and
implementation files.
- Relocated `Random` module to `Utils`, updated tests accordingly.
- Deleted obsolete `README` files for `Core/Config` and `Core/Random`.
  • Loading branch information
merlinvn committed Oct 1, 2024
1 parent 5ecd0c6 commit 159dc27
Show file tree
Hide file tree
Showing 21 changed files with 502 additions and 243 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 500
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ run r: build
./$(APP_EXECUTABLE)

clean:
rm -rf build $(DOCS_OUTPUT_DIR)
rm -rf build

setup-vcpkg:
if [ -n "$(VCPKG_ROOT)" ] && [ ! -x "$(VCPKG_EXEC)" ]; then \
Expand Down
514 changes: 284 additions & 230 deletions input/input.yml → sample_inputs/input.yml

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
find_package(fmt CONFIG REQUIRED)
find_package(GSL REQUIRED)
find_package(yaml-cpp CONFIG REQUIRED)

include_directories(${PROJECT_SOURCE_DIR}/src)

# craete source files
# Add source files for the core library
file(GLOB_RECURSE MALASIM_CORE_SOURCES
"*.cpp"
"Core/Random/*.cpp"
"Config/*.cpp"
"Utils/*.cpp"
)

# Add source files for the core library
Expand All @@ -20,6 +22,7 @@ add_library(MalaSimCore STATIC
target_link_libraries(MalaSimCore PUBLIC
fmt::fmt
GSL::gsl GSL::gslcblas
yaml-cpp::yaml-cpp
)

set_property(TARGET MalaSimCore PROPERTY CXX_STANDARD 20)
Expand Down
Empty file added src/Config/Config.cpp
Empty file.
79 changes: 79 additions & 0 deletions src/Config/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Config.h
#ifndef CONFIG_H
#define CONFIG_H

#include <yaml-cpp/yaml.h>

#include <functional>
#include <shared_mutex>
// #include <memory>
// #include <string>
// #include <vector>

// Forward declaration
class Model;

// Define configuration sections
struct SimulationConfig {
double timestep;
double duration;
};

struct PhysicsConfig {
double gravity;
double air_resistance;
};

struct OutputConfig {
std::string log_level;
std::string file_path;
};

// Aggregated Config Structure
struct ConfigData {
SimulationConfig simulation;
PhysicsConfig physics;
OutputConfig output;
};

// Observer Callback Type
using ConfigObserver = std::function<void(const ConfigData &)>;

class Config {
public:
// Constructor and Destructor
Config() = default;
~Config() = default;

// Load configuration from a YAML file
void Load(const std::string &filename);

// Reload configuration (useful for dynamic updates)
void Reload();

// Getters for configuration sections
SimulationConfig GetSimulationConfig() const;
PhysicsConfig GetPhysicsConfig() const;
OutputConfig GetOutputConfig() const;

// Register an observer for configuration changes
void RegisterObserver(ConfigObserver observer);

private:
// Internal Method to Notify Observers
void NotifyObservers();

// Configuration Data
ConfigData config_data_;

// Mutex for Thread-Safe Access
mutable std::shared_mutex mutex_;

// Observers
std::vector<ConfigObserver> observers_;

// Configuration File Path
std::string config_file_path_;
};

#endif // CONFIG_H
File renamed without changes.
54 changes: 54 additions & 0 deletions src/Simulation/Model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "Model.h"

#include <iostream>
#include <stdexcept>

#include "Config/Config.h" // Assuming Config is defined here

// Private constructor: creates the Config instance
Model::Model()
: config_(std::make_unique<Config>()),
config_file_path_("config.yml"),
is_initialized_(false) {
// Constructor does not load the configuration file
// Initialization is deferred to the Initialize() method
}

void Model::Initialize() {
if (!config_file_path_.empty()) {
// Load the configuration file
config_->Load(config_file_path_);

// Register an observer to handle dynamic configuration changes
config_->RegisterObserver([this](const ConfigData &new_config) {
// Handle configuration changes, e.g., adjust simulation parameters
std::cout << "Model received updated configuration.\n";
// Update internal state based on new_config if necessary
// For example:
// this->simulation_params = new_config.simulation;
});

std::cout << "Model initialized with configuration file: "
<< config_file_path_ << "\n";
is_initialized_ = true;
} else {
throw std::invalid_argument("Configuration file path must be provided.");
}
}

void Model::Run() {
if (!is_initialized_) {
throw std::runtime_error(
"Model is not initialized. Call Initialize() first.");
}
// Simulation run code
}

void Model::Finalize() {
if (!is_initialized_) {
throw std::runtime_error("Model is not initialized or already finalized.");
}
// Cleanup code
config_.reset(); // Automatically handled by unique_ptr, but explicitly
// showing intent
}
57 changes: 57 additions & 0 deletions src/Simulation/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef MODEL_H
#define MODEL_H

#include <memory>
#include <string>

// Forward declaration
class Config;

class Model {
public:
// Provides global access to the singleton instance
static Model &Instance() {
static Model instance;
return instance;
}

// Initialize the model
void Initialize();

// Run the simulation
void Run();

// Finalize and clean up resources
void Finalize();

// Access configuration in a controlled manner
const Config* GetConfig() const {
if (!config_) {
throw std::runtime_error(
"Model not initialized. Call Initialize() first.");
}
return config_.get();
}

// Prevent copying and moving
Model(const Model &) = delete;
Model(Model &&) = delete;
Model &operator=(const Model &) = delete;
Model &operator=(Model &&) = delete;

private:
// Private constructor and destructor
Model();
~Model() = default;

// Configuration managed by a smart pointer
std::unique_ptr<Config> config_;

// Configuration file path with default value
std::string config_file_path_;

bool is_initialized_;
};

#endif // MODEL_H

5 changes: 3 additions & 2 deletions src/Core/Random/Random.cpp → src/Utils/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_randist.h>

#include <cmath>
#include <cstddef>
#include <cstdint>
#include <random>
#include <stdexcept>
#include <vector>

using namespace Utils;

// Constructor
Random::Random(gsl_rng* rng, uint64_t seed) : seed_(seed) {
if (rng != nullptr) {
Expand All @@ -42,7 +43,7 @@ void Random::initialize(uint64_t initial_seed) {

// Use std::random_device to generate a random seed if seed is 0
std::random_device rd;
seed_ = (initial_seed == 0) ? rd() : initial_seed;
seed_ = (initial_seed == -1) ? rd() : initial_seed;

// Log the seed value
// LOG(INFO) << fmt::format("Random initializing with seed: {}", seed_);
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Random/Random.h → src/Utils/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <memory>
#include <vector>

namespace Utils {
/**
* @class Random
* @brief Encapsulates random number generation functionalities using GSL.
Expand Down Expand Up @@ -39,7 +40,7 @@ class Random {
*
* @param external_rng Pointer to an external GSL RNG. Defaults to nullptr.
*/
explicit Random(gsl_rng* external_rng = nullptr, uint64_t seed = 0);
explicit Random(gsl_rng* external_rng = nullptr, uint64_t seed = -1);

/**
* @brief Destructor.
Expand Down Expand Up @@ -385,6 +386,6 @@ class Random {
*/
void initialize(uint64_t initial_seed = 0);
};

} // namespace Utils
#endif // RANDOM_H

File renamed without changes.
5 changes: 2 additions & 3 deletions tests/Core/Random/RandomTestBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

#include <gtest/gtest.h>

#include "Core/Random/Random.h"
#include "helpers/test_helpers.h"
#include "Utils/Random.h"

// Test fixture for Random class
class RandomTest : public ::testing::Test {
protected:
Random rng;
Utils::Random rng;
void SetUp() override {
// Optional: Initialize any shared resources
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Core/Random/RandomTest_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#include "RandomTestBase.h"

using namespace Utils;
// Test default constructor initializes RNG
TEST_F(RandomTest, DefaultConstructorInitializesRNG) {
Random const rng;
EXPECT_NE(rng.get_seed(), 0);
EXPECT_NE(rng.get_seed(), -1);
}

// Test constructor with external RNG
Expand All @@ -24,7 +25,7 @@ TEST_F(RandomTest, ConstructorWithExternalRNG) {
// Test get_seed and set_seed
TEST_F(RandomTest, SeedManagement) {
uint64_t initial_seed = rng.get_seed();
EXPECT_NE(initial_seed, 0);
EXPECT_NE(initial_seed, -1);

rng.set_seed(67890);
EXPECT_EQ(rng.get_seed(), 67890);
Expand All @@ -36,6 +37,6 @@ TEST_F(RandomTest, SeedManagement) {
// Test constructor with nullptr (should initialize internally)
TEST_F(RandomTest, ConstructorWithNullptr) {
Random rng_instance(nullptr);
EXPECT_NE(rng_instance.get_seed(), 0);
EXPECT_NE(rng_instance.get_seed(), -1);
}

1 change: 1 addition & 0 deletions tests/Core/Random/RandomTest_random_beta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>

#include "RandomTestBase.h"
#include "helpers/test_helpers.h"

// Test that random_beta throws when alpha is non-positive
TEST_F(RandomTest, random_beta_ThrowsWhenAlphaNonPositive) {
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Random/RandomTest_random_binomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>

#include "RandomTestBase.h"
#include "helpers/test_helpers.h"

// Test that random_binomial throws when probability is less than 0.0
TEST_F(RandomTest, random_binomial_ThrowsWhenProbabilityLessThanZero) {
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Random/RandomTest_random_gamma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gtest/gtest.h>

#include "RandomTestBase.h"
#include "helpers/test_helpers.h"

// Test that random_gamma throws when shape is non-positive
TEST_F(RandomTest, random_gamma_ThrowsWhenShapeNonPositive) {
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Random/RandomTest_random_multinomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>

#include "RandomTestBase.h"
#include "helpers/test_helpers.h"

// Test that random_multinomial throws when categories is zero
TEST_F(RandomTest, random_multinomial_ThrowsWhenCategoriesZero) {
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Random/RandomTest_random_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>

#include "RandomTestBase.h"
#include "helpers/test_helpers.h"

// Exception Tests
TEST_F(RandomTest, random_normal_ThrowsWhenStdDevNonPositive_Integral) {
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Random/RandomTest_random_shuffle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>

#include "RandomTestBase.h"
#include "helpers/test_helpers.h"

// test that random_shuffle throws when the input vector is empty
TEST_F(RandomTest, random_shuffle_ThrowsWhenInputEmpty) {
Expand Down
3 changes: 2 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"dependencies": [
"fmt",
"gsl",
"gtest",
"gsl"
"yaml-cpp"
]
}

0 comments on commit 159dc27

Please sign in to comment.