Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add main program and added access methods for matrix types #13

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
endif()

add_subdirectory(submod)
add_subdirectory(submod)
add_subdirectory(app)
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file(GLOB_RECURSE APP_SOURCES "src/*.cpp")
add_executable(plasma_emulator ${APP_SOURCES})
target_link_libraries(plasma_emulator core chempars)
18 changes: 18 additions & 0 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <Eigen/Dense>
#include <fstream>

using namespace std;
using namespace Eigen;

int main() {
// 线性方程求解 Ax =B;
Matrix4d A;
A << 2, -1, -1, 1,
1, 1, -2, 1,
4, -6, 2, -2,
3, 6, -9, 7;
fstream f;
f.open("A.csv", ios::out);
f << A;
f.close();
}
2 changes: 2 additions & 0 deletions submod/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
find_package(units CONFIG REQUIRED)
find_package(Eigen3 CONFIG REQUIRED)
find_path(RAPIDCSV_INCLUDE_DIRS "rapidcsv.h")
file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.c")
add_library(core STATIC ${SOURCES})
target_include_directories(
core
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
PUBLIC ${RAPIDCSV_INCLUDE_DIRS}
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
INTERFACE $<INSTALL_INTERFACE:include>
)
Expand Down
8 changes: 6 additions & 2 deletions submod/core/include/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ using ESM = Eigen::SparseMatrix<double>;
using stack_matrix = Eigen::Matrix<double, x_cells, y_cells, Eigen::ColMajor>;

template <typename T>
concept EigenMatrix = std::is_same_v<T, Matrix> || std::is_same_v<T, ESM> || std::is_same_v<T, stack_matrix> ||
std::is_same_v<T, Eigen::VectorXd>;
concept EigenMatrixLike = std::is_base_of_v<Eigen::DenseBase<T>, T>;

template <EigenMatrixLike T>
bool static operator==(const T& a, const T& b) {
return (a - b).norm() < 1e-5;
}

}; // namespace gds::core
11 changes: 8 additions & 3 deletions submod/core/include/matrix_helper.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <fstream>

#include "matrix.hpp"
#include "common/type_limits.hpp"
#include "matrix.hpp"

namespace gds::core {
class MatrixHelper {
Expand All @@ -12,8 +12,8 @@ class MatrixHelper {
/// @param matrix specifies the matrix to be written to the CSV file
/// @param file_name the name of the CSV file to be written to
/// @return true if the matrix was successfully written to the CSV file, false otherwise
template <gds::core::EigenMatrix T>
bool write_matrix_to_CSV(const T& matrix, const std::string& file_name) {
template <gds::core::EigenMatrixLike T>
static bool write_matrix_to_CSV(const T& matrix, const std::string& file_name) {
std::ofstream file;
file.open(file_name, std::ios::out | std::ios::binary);
if (!file.is_open()) {
Expand All @@ -24,6 +24,11 @@ class MatrixHelper {
return true;
}

/// @brief reads a matrix from a CSV file
/// @param file_name the name of the CSV file to be read from
/// @return the matrix read from the CSV file
static Matrix load_matrix_from_CSV(const std::string& file_name);

/// @brief initializes a tridiagonal matrix with the given diagonal, subdiagonal, and superdiagonal elements
/// @param n size of the matrix
/// @param elements diagonal, subdiagonal, and superdiagonal elements of the matrix
Expand Down
25 changes: 23 additions & 2 deletions submod/core/src/matrix_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
#include "matrix_helper.hpp"

#include <iostream>
#include <rapidcsv.h>

#include "common/type_limits.hpp"
#include <algorithm>
#include <cstdint>
#include <string>

#include "matrix.hpp"

namespace gds::core {

Matrix MatrixHelper::load_matrix_from_CSV(const std::string &file_name) {
rapidcsv::Document doc(file_name, rapidcsv::LabelParams(-1, -1), rapidcsv::SeparatorParams(' ', 0));
auto rows = doc.GetRowCount();
auto row0 = doc.GetRow<std::string>(0);
row0.erase(std::remove(row0.begin(), row0.end(), ""), row0.end());
auto cols = row0.size();
Matrix matrix(rows, cols);
for (uint32_t i = 0; i < rows; i++) {
auto tr = doc.GetRow<std::string>(i);
tr.erase(std::remove(tr.begin(), tr.end(), ""), tr.end());
for (int j = 0; j < cols; j++) {
matrix(i, j) = std::stod(tr[j]);
}
}
return matrix;
}

} // namespace gds::core
13 changes: 13 additions & 0 deletions submod/core/test/test_matrix_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <gtest/gtest.h>

#include <iostream>
#include <string>

#include "core/matrix.hpp"
#include "core/matrix_helper.hpp"

TEST(MatrixHelperTest, Test_init_tridiagonal_matrix) {
Expand All @@ -12,4 +16,13 @@ TEST(MatrixHelperTest, Test_init_fiveiagonal_matrix) {
std::array<double, 5> diag = {3, 1, 2, 3, 4};
auto matrix = gds::core::MatrixHelper::init_fivediagonal_matrices<25>(diag);
std::cout << matrix << std::endl;
}

TEST(MatrixHelperTest, Test_write_load_matrix_to_file) {
gds::core::Matrix m = gds::core::Matrix::Random(10, 10);
auto r = gds::core::MatrixHelper::write_matrix_to_CSV(m, "test.csv");
EXPECT_TRUE(r);
auto lr = gds::core::MatrixHelper::load_matrix_from_CSV("test.csv");
using gds::core::operator==;
EXPECT_TRUE(m == lr);
}
3 changes: 2 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"gtest",
"fmt",
"units",
"eigen3"
"eigen3",
"rapidcsv"
]
}
Loading