Skip to content

Commit

Permalink
Merge pull request SimVascular#45 from mrp089/refactor_patch
Browse files Browse the repository at this point in the history
Refactoring .hpp -> .h, .cpp and remove class templates
  • Loading branch information
ktbolt authored Sep 28, 2023
2 parents 06d92ed + 6b716fd commit df6a603
Show file tree
Hide file tree
Showing 75 changed files with 4,210 additions and 3,260 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codechecks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
- name: Run clang-format
run: |
cd src
find **/*.hpp **/*.cpp | xargs clang-format --dry-run --style=Google --Werror
find **/*.h **/*.cpp | xargs clang-format --dry-run --style=Google --Werror
127 changes: 121 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
# Copyright (c) Stanford University, The Regents of the University of
# California, and others.
#
# All Rights Reserved.
#
# See Copyright-SimVascular.txt for additional details.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 3.22)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

project(svZeroDSolver)

include(FetchContent)

# -----------------------------------------------------------------------------
# Enable code coverage
# -----------------------------------------------------------------------------
set(ENABLE_COVERAGE OFF CACHE BOOL "Enable code coverage")
Expand All @@ -26,30 +57,51 @@ if(ENABLE_COVERAGE)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

# -----------------------------------------------------------------------------
# Set the location to store the binaries and libraries created by this project.
# -----------------------------------------------------------------------------
#
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Need to add the -fPIC flag to build the interface shared library.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()

# -----------------------------------------------------------------------------
# Fetch Eigen
# -----------------------------------------------------------------------------
# Eigen is a header-only C++ template library for linear algebra.
#
FetchContent_Declare(
Eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG master
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE)

set(EIGEN_BUILD_DOC OFF)
set(BUILD_TESTING OFF)
set(EIGEN_BUILD_PKGCONFIG OFF)
set( OFF)

FetchContent_MakeAvailable(Eigen)

# -----------------------------------------------------------------------------
# Fetch pybind11
# -----------------------------------------------------------------------------
# pybind11 is a library used to create Python bindings of existing C++ code.
#
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG master
)

FetchContent_GetProperties(pybind11)

if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
Expand All @@ -58,38 +110,101 @@ endif()
# -----------------------------------------------------------------------------
# Fetch nlohmann/json
# -----------------------------------------------------------------------------
# This is a C++ header-only library for reading JSON files.
#
FetchContent_Declare(
json
URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz
)

FetchContent_MakeAvailable(json)

# -----------------------------------------------------------------------------
# Fetch pybind11_json
# -----------------------------------------------------------------------------
# pybind11_json is an nlohmann::json to pybind11 bridge that converts
# between nlohmann::json and py::object objects.
#
FetchContent_Declare(
pybind11_json
GIT_REPOSITORY https://github.com/pybind/pybind11_json.git
GIT_TAG master
)

FetchContent_MakeAvailable(pybind11_json)

# -----------------------------------------------------------------------------
# Set executable and python library
# Set executables for each application.
# -----------------------------------------------------------------------------
# There are two executables
# 1) svzerodsolver
# 2) svzerodcalibrator
#
add_executable(svzerodsolver applications/svzerodsolver.cpp
$<TARGET_OBJECTS:svzero_algebra_library>
$<TARGET_OBJECTS:svzero_model_library>
$<TARGET_OBJECTS:svzero_solve_library>
)

add_executable(svzerodcalibrator applications/svzerodcalibrator.cpp
$<TARGET_OBJECTS:svzero_optimize_library>
$<TARGET_OBJECTS:svzero_model_library>
)

# -----------------------------------------------------------------------------
# Setup building of the svzerodplus Python extension module.
# -----------------------------------------------------------------------------
include_directories(src)
add_executable(svzerodsolver applications/svzerodsolver.cpp)
add_executable(svzerodcalibrator applications/svzerodcalibrator.cpp)
pybind11_add_module(svzerodplus EXCLUDE_FROM_ALL applications/svzerodplus.cpp)

# -----------------------------------------------------------------------------
# Add source sub-directories.
# -----------------------------------------------------------------------------
add_subdirectory("src/algebra")
add_subdirectory("src/interface")
add_subdirectory("src/model")
add_subdirectory("src/optimize")
add_subdirectory("src/solve")

# -----------------------------------------------------------------------------
# Link libraries
# Set header file include directories.
# -----------------------------------------------------------------------------
#
target_include_directories(svzerodsolver PUBLIC
${CMAKE_SOURCE_DIR}/src/algebra
${CMAKE_SOURCE_DIR}/src/model
${CMAKE_SOURCE_DIR}/src/solve
)

target_include_directories(svzerodcalibrator PUBLIC
${CMAKE_SOURCE_DIR}/src/algebra
${CMAKE_SOURCE_DIR}/src/model
${CMAKE_SOURCE_DIR}/src/optimize
${CMAKE_SOURCE_DIR}/src/solve
)

target_include_directories(svzerodplus PUBLIC
${CMAKE_SOURCE_DIR}/src/algebra
${CMAKE_SOURCE_DIR}/src/model
${CMAKE_SOURCE_DIR}/src/optimize
${CMAKE_SOURCE_DIR}/src/solve
)

# -----------------------------------------------------------------------------
# Set the libraries each application needs to link to.
# -----------------------------------------------------------------------------
# These are needed only for the header-only applications.
#
target_link_libraries(svzerodsolver PRIVATE Eigen3::Eigen)
target_link_libraries(svzerodsolver PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(svzerodcalibrator PRIVATE Eigen3::Eigen)
target_link_libraries(svzerodcalibrator PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(svzerodplus PRIVATE Eigen3::Eigen)
target_link_libraries(svzerodplus PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(svzerodplus PRIVATE pybind11_json)
target_link_libraries(svzerodplus PRIVATE svzero_algebra_library)
target_link_libraries(svzerodplus PRIVATE svzero_model_library)
target_link_libraries(svzerodplus PRIVATE svzero_optimize_library)
target_link_libraries(svzerodplus PRIVATE svzero_solve_library)

5 changes: 2 additions & 3 deletions applications/svzerodcalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
* @file svzerodcalibrator.cpp
* @brief Main routine for svZeroDCalibrator
*/
#include "optimize/calibrate.hpp"
#include "calibrate.h"

// Setting scalar type to double
typedef double T;

int main(int argc, char* argv[]) {
DEBUG_MSG("Starting svZeroDCalibrator");
Expand All @@ -56,7 +55,7 @@ int main(int argc, char* argv[]) {
std::ifstream ifs(input_file);
const auto& config = nlohmann::json::parse(ifs);

auto output_config = OPT::calibrate<T>(config);
auto output_config = optimize::calibrate(config);

// Write optimized simulation config
std::ofstream o(output_file);
Expand Down
12 changes: 6 additions & 6 deletions applications/svzerodplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "optimize/calibrate.hpp"
#include "Solver.h"
#include "calibrate.h"
#include "pybind11_json/pybind11_json.hpp"
#include "solve/solver.hpp"

namespace py = pybind11;

PYBIND11_MODULE(svzerodplus, m) {
using Solver = SOLVE::Solver<double>;
using Solver = solve::Solver;
py::class_<Solver>(m, "Solver")
.def(py::init([](py::dict& config) {
const nlohmann::json& config_json = config;
Expand Down Expand Up @@ -83,7 +83,7 @@ PYBIND11_MODULE(svzerodplus, m) {
});
m.def("calibrate", [](py::dict& config) {
const nlohmann::json& config_json = config;
return OPT::calibrate<double>(config);
return optimize::calibrate(config);
});
m.def("run_simulation_cli", []() {
py::module_ sys = py::module_::import("sys");
Expand All @@ -96,7 +96,7 @@ PYBIND11_MODULE(svzerodplus, m) {
}
std::ifstream ifs(argv[1]);
const auto& config = nlohmann::json::parse(ifs);
auto solver = SOLVE::Solver<double>(config);
auto solver = solve::Solver(config);
solver.run();
solver.write_result_to_csv(argv[2]);
});
Expand All @@ -111,7 +111,7 @@ PYBIND11_MODULE(svzerodplus, m) {
}
std::ifstream ifs(argv[1]);
const auto& config = nlohmann::json::parse(ifs);
auto output_config = OPT::calibrate<double>(config);
auto output_config = optimize::calibrate(config);
std::ofstream o(argv[2]);
o << std::setw(4) << output_config << std::endl;
});
Expand Down
11 changes: 9 additions & 2 deletions applications/svzerodsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
* @file svzerodsolver.cpp
* @brief Main routine of svZeroDSolver
*/
#include "solve/solver.hpp"
#include <fstream>

#include "Solver.h"

/**
*
Expand Down Expand Up @@ -59,26 +61,31 @@ int main(int argc, char* argv[]) {
"Usage: svzerodsolver path/to/config.json "
"(optional:path/to/output.csv)");
}

std::string input_file = argv[1];
std::string output_file;

if (argc == 3) {
output_file = argv[2];

} else {
// If output file is not provided, default is <path to .json>+"output.csv"

std::size_t end_of_path = input_file.rfind("/");
if (end_of_path == std::string::npos) {
end_of_path = input_file.rfind("\\"); // For Windows paths (?)
if (end_of_path == std::string::npos) {
std::runtime_error("Could not find path to .json file.");
}
}

output_file = input_file.substr(0, end_of_path) + "output.csv";
}

std::ifstream ifs(input_file);
const auto& config = nlohmann::json::parse(ifs);

auto solver = SOLVE::Solver<double>(config);
auto solver = solve::Solver(config);
solver.run();
solver.write_result_to_csv(output_file);

Expand Down
5 changes: 2 additions & 3 deletions docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ INPUT = "src" \
"docs/pages"
INPUT_ENCODING = UTF-8
FILE_PATTERNS = *.cpp \
*.hpp \
*.h \
*.inc \
*.markdown \
*.md \
Expand All @@ -122,7 +122,7 @@ EXCLUDE_SYMBOLS =
EXAMPLE_PATH =
EXAMPLE_PATTERNS = *
EXAMPLE_RECURSIVE = NO
IMAGE_PATH = images
IMAGE_PATH = "docs"
INPUT_FILTER =
FILTER_PATTERNS =
FILTER_SOURCE_FILES = NO
Expand Down Expand Up @@ -159,7 +159,6 @@ HTML_EXTRA_FILES =
HTML_COLORSTYLE_HUE = 220
HTML_COLORSTYLE_SAT = 100
HTML_COLORSTYLE_GAMMA = 80
HTML_TIMESTAMP = YES
HTML_DYNAMIC_SECTIONS = NO
HTML_INDEX_NUM_ENTRIES = 100
GENERATE_DOCSET = NO
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/developer_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ comment to format all your files:

```bash
cd src
find **/*.hpp **/*.cpp | xargs clang-format -i --style=Google
find **/*.h **/*.cpp | xargs clang-format -i --style=Google
```

You can also just check **if** a file would be formatted without actually formatting
it with:

```bash
cd src
find **/*.hpp **/*.cpp | xargs clang-format --dry-run --style=Google --Werror
find **/*.h **/*.cpp | xargs clang-format --dry-run --style=Google --Werror
```

The latter check is also performed in the GitHub CI/CD (a.k.a. Actions) and
Expand Down
Loading

0 comments on commit df6a603

Please sign in to comment.