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

Small Fixes and Tidy Up #45

Merged
merged 9 commits into from
Dec 17, 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cmake-build-*
build
polyhedral_gravity.egg-info
dist
docs/Doxyfile
docs/Doxyfile
src/polyhedralGravity/Info.h
55 changes: 34 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ project(polyhedralGravity)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Appends the the module path to contain additional CMake modules for this project
# and include everything necessary
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(CMakeDependentOption)

#####################################
Expand All @@ -15,52 +18,59 @@ set(POLYHEDRAL_GRAVITY_PARALLELIZATION "CPP" CACHE STRING "Host parallelization
set_property(CACHE POLYHEDRAL_GRAVITY_PARALLELIZATION PROPERTY STRINGS CPP, OMP, TBB)

# Set the Logging Level
set(LOGGING_LEVEL_LIST "TRACE" "DEBUG" "INFO" "WARN" "ERROR" "CRITICAL" "OFF")
set(LOGGING_LEVEL "INFO" CACHE STRING "Set the Logging level, default (INFO), available options: TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL, OFF")
set_property(CACHE LOGGING_LEVEL PROPERTY STRINGS ${LOGGING_LEVEL_LIST})
set(POLYHEDRAL_GRAVITY_LOGGING_LEVEL_LIST "TRACE" "DEBUG" "INFO" "WARN" "ERROR" "CRITICAL" "OFF")
set(POLYHEDRAL_GRAVITY_LOGGING_LEVEL "INFO" CACHE STRING "Set the Logging level, default (INFO), available options: TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL, OFF")
set_property(CACHE POLYHEDRAL_GRAVITY_LOGGING_LEVEL PROPERTY STRINGS ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL_LIST})
# Convert the logging level string to its corresponding number
list(FIND LOGGING_LEVEL_LIST ${LOGGING_LEVEL} LOGGING_LEVEL_INDEX)
list(FIND POLYHEDRAL_GRAVITY_LOGGING_LEVEL_LIST ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL} LOGGING_LEVEL_INDEX)
if (${LOGGING_LEVEL_INDEX} EQUAL -1)
message(FATAL_ERROR "Invalid logging level: ${LOGGING_LEVEL}")
message(FATAL_ERROR "Invalid logging level: ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL}")
endif ()
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=${LOGGING_LEVEL_INDEX})
message(STATUS "Logging level set to ${LOGGING_LEVEL} (=${LOGGING_LEVEL_INDEX})")

###################################
# What actually to build? - Options
###################################

#########################################################
# What actually to build? - Options, Versions and Output
#########################################################
# Build docs
option(BUILD_POLYHEDRAL_GRAVITY_DOCS "Builds the documentation (Default: OFF)" OFF)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_DOCS = ${BUILD_POLYHEDRAL_GRAVITY_DOCS}")
# Build C++ executable
option(BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE "Builds the C++ executable (Default: ON)" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE = ${BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE}")
# Build library (default ON), if the executable or tests are built this forced to ON
cmake_dependent_option(BUILD_POLYHEDRAL_GRAVITY_LIBRARY "Builds the library (Default: ON)" ON
"NOT BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE AND NOT BUILD_POLYHEDRAL_GRAVITY_TESTS" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_LIBRARY = ${BUILD_POLYHEDRAL_GRAVITY_LIBRARY}")
# Option to build the python interface
option(BUILD_POLYHEDRAL_PYTHON_INTERFACE "Set this to on if the python interface should be built (Default: ON)" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE = ${BUILD_POLYHEDRAL_PYTHON_INTERFACE}")
option(BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE "Set this to on if the python interface should be built (Default: ON)" ON)
# Option to build tests or not
option(BUILD_POLYHEDRAL_GRAVITY_TESTS "Set to on if the tests should be built (Default: ON)" ON)
message(STATUS "BUILD_POLYHEDRAL_GRAVITY_TESTS = ${BUILD_POLYHEDRAL_GRAVITY_TESTS}")


IF(_LIBCPP_DISABLE_AVAILABILITY)
message(STATUS "Disabling availability macros for libc++")
add_definitions(-D_LIBCPP_DISABLE_AVAILABILITY)
endif ()

# Resolves missing fmt symbols when working with spdlog (bundled via brew/ conda on Arm architecture)
# Refer to https://github.com/gabime/spdlog/issues/660
add_compile_definitions(FMT_HEADER_ONLY)

include(git)
include(version.cmake)

message(STATUS "#################################################################")
message(STATUS "Polyhedral Gravity Version ${POLYHEDRAL_GRAVITY_VERSION}")
message(STATUS "Polyhedral Gravity Commit Hash ${POLYHEDRAL_GRAVITY_COMMIT_HASH}")
message(STATUS "Polyhedral Parallelization Backend ${POLYHEDRAL_GRAVITY_PARALLELIZATION}")
message(STATUS "Polyhedral Gravity Logging Level ${POLYHEDRAL_GRAVITY_LOGGING_LEVEL}")
message(STATUS "#################################################################")
message(STATUS "Polyhedral Gravity Documentation ${BUILD_POLYHEDRAL_GRAVITY_DOCS}")
message(STATUS "Polyhedral Gravity Library ${BUILD_POLYHEDRAL_GRAVITY_LIBRARY}")
message(STATUS "Polyhedral Gravity C++ Executable ${BUILD_POLYHEDRAL_GRAVITY_EXECUTABLE}")
message(STATUS "Polyhedral Gravity Python Interface ${BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE}")
message(STATUS "Polyhedral Gravity Tests ${BUILD_POLYHEDRAL_GRAVITY_TESTS}")
message(STATUS "#################################################################")
#######################################################
# Including dependencies needed across multiple targets
#######################################################
# Appends the the module path to contain additional CMake modules for this project
# and include everything necessary
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# Include dependecies
include(thrust)
include(spdlog)
include(tetgen)
Expand All @@ -76,6 +86,9 @@ include(clang_format)
if (${POLYHEDRAL_GRAVITY_PARALLELIZATION} STREQUAL "TBB")
include(tbb)
thrust_set_TBB_target(TBB::tbb)
add_compile_definitions(POLYHEDRAL_GRAVITY_TBB)
elseif (${POLYHEDRAL_GRAVITY_PARALLELIZATION} STREQUAL "OMP")
add_compile_definitions(POLYHEDRAL_GRAVITY_OMP)
endif ()

# Thrust set-up i.e. the parallelization library, create targets according to the users specification
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ cmake --build .

The following options are available:

| Name (Default) | Options |
|-------------------------------------------:|:--------------------------------------------------------------------------------------------|
| POLYHEDRAL_GRAVITY_PARALLELIZATION (`CPP`) | `CPP` = Serial Execution / `OMP` or `TBB` = Parallel Execution with OpenMP or Intel\'s TBB |
| LOGGING_LEVEL (`INFO`) | `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `OFF` |
| BUILD_POLYHEDRAL_GRAVITY_DOCS (`OFF`) | Build this documentation |
| BUILD_POLYHEDRAL_GRAVITY_TESTS (`ON`) | Build the Tests |
| BUILD_POLYHEDRAL_PYTHON_INTERFACE (`ON`) | Build the Python interface |
| Name (Default) | Options |
|-------------------------------------------------------------:|:--------------------------------------------------------------------------------------------|
| POLYHEDRAL_GRAVITY_PARALLELIZATION (`CPP`) | `CPP` = Serial Execution / `OMP` or `TBB` = Parallel Execution with OpenMP or Intel\'s TBB |
| POLYHEDRAL_GRAVITY_LOGGING_LEVEL (`INFO`) | `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `OFF` |
| BUILD_POLYHEDRAL_GRAVITY_DOCS (`OFF`) | Build this documentation |
| BUILD_POLYHEDRAL_GRAVITY_TESTS (`ON`) | Build the Tests |
| BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE (`ON`) | Build the Python interface |

During testing POLYHEDRAL_GRAVITY_PARALLELIZATION=`TBB` has been the most performant.
It is further not recommend to change the LOGGING_LEVEL to something else than `INFO=2`.
It is further not recommend to change the POLYHEDRAL_GRAVITY_LOGGING_LEVEL to something else than `INFO=2`.

The recommended CMake settings using the `TBB` backend would look like this:

Expand Down
46 changes: 46 additions & 0 deletions cmake/git.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
find_package(Git QUIET REQUIRED)

function(get_git_commit_hash OUTPUT_VAR)
# Run a Git command to get the first 8 characters of the current commit hash
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short=8 HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE GIT_ERROR
ERROR_STRIP_TRAILING_WHITESPACE
)

# Check if the Git command was successful
if (NOT GIT_COMMIT_HASH OR GIT_ERROR)
message(WARNING "Failed to retrieve Git commit hash: ${GIT_ERROR}")
set(${OUTPUT_VAR} "UNKNOWN" PARENT_SCOPE)
else()
# Pass the short hash back to the calling scope
set(${OUTPUT_VAR} "${GIT_COMMIT_HASH}" PARENT_SCOPE)
endif()
endfunction()

function(is_git_working_tree_clean OUTPUT_VAR)
# Run a Git command to check if the working tree is clean
execute_process(
COMMAND ${GIT_EXECUTABLE} diff-index --quiet HEAD --
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE GIT_STATUS_RESULT
ERROR_VARIABLE GIT_ERROR
ERROR_STRIP_TRAILING_WHITESPACE
)

# Check the result of the Git command
if (NOT GIT_ERROR AND GIT_STATUS_RESULT EQUAL 0)
# Working tree is clean
set(${OUTPUT_VAR} TRUE PARENT_SCOPE)
else()
# Working tree has uncommitted changes or an error occurred
set(${OUTPUT_VAR} FALSE PARENT_SCOPE)

if (GIT_ERROR)
message(WARNING "Error while checking Git working tree: ${GIT_ERROR}")
endif()
endif()
endfunction()
1 change: 1 addition & 0 deletions cmake/spdlog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(SPDLOG_VERSION 1.14.1)
# If you install [email protected] via homebrew on ARM macOS, CMake will find spdlog
# However, there is a version mismatch between the `fmt` library installed as dependency, and the one actually
# being required leading to a linking error (i.e. missing symbols) while compiling!
# Update 29.11.2024: We fixed this by using spdlog has header library (--> top-level CMake file)
find_package(spdlog ${SPDLOG_VERSION} QUIET)

if(${spdlog_FOUND})
Expand Down
59 changes: 28 additions & 31 deletions cmake/tetgen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,44 @@ include(FetchContent)
message(STATUS "Setting up tetgen")

# IMPORTANT NOTE
# We do ot use findPackage here, as we modify the one source file slightly to suppress output via stdout!!!
# We do not use find_package here, as we modify the one source file slightly to suppress output via stdout!!!

#Fetches the version 1.6 for tetgen
# Fetches the version 1.6 for tetgen
FetchContent_Declare(tetgen
GIT_REPOSITORY https://github.com/libigl/tetgen.git
GIT_TAG 4f3bfba3997f20aa1f96cfaff604313a8c2c85b6 # release 1.6
)
)

FetchContent_GetProperties(tetgen)
if(NOT tetgen_POPULATED)
FetchContent_Populate(tetgen)
FetchContent_MakeAvailable(tetgen)

# This is ugly, but functional
# We modify one source file in order to prevent console output from the printf function
# without polluting the global include path (which would cause to more uglier issues)
if(NOT EXISTS ${tetgen_SOURCE_DIR}/tetgen_mod.cxx)
message(STATUS "Creating modified tetgen.cxx in order to prevent console output from library")
file(READ ${tetgen_SOURCE_DIR}/tetgen.cxx TETGEN_CXX)
# Modify the tetgen library to suppress console output from the printf function
if(NOT EXISTS ${tetgen_SOURCE_DIR}/tetgen_mod.cxx)
message(STATUS "Creating modified tetgen.cxx in order to prevent console output from library")
file(READ ${tetgen_SOURCE_DIR}/tetgen.cxx TETGEN_CXX)

string(REPLACE
"#include \"tetgen.h\""
"#include \"tetgen.h\"\n#define printf(fmt, ...) (0)\n"
TETGEN_CXX "${TETGEN_CXX}")
string(REPLACE
"#include \"tetgen.h\""
"#include \"tetgen.h\"\n#define printf(fmt, ...) (0)\n"
TETGEN_CXX "${TETGEN_CXX}")

file(WRITE ${tetgen_SOURCE_DIR}/tetgen_mod.cxx
"${TETGEN_CXX}"
)
else()
message(STATUS "A modified tetgen.cxx already exists! It is assumed that is the correct one disabling output")
endif()

add_library(tetgen STATIC
${tetgen_SOURCE_DIR}/tetgen_mod.cxx
${tetgen_SOURCE_DIR}/predicates.cxx
)
file(WRITE ${tetgen_SOURCE_DIR}/tetgen_mod.cxx
"${TETGEN_CXX}"
)
else()
message(STATUS "A modified tetgen.cxx already exists! It is assumed that it is the correct one disabling output")
endif()

target_compile_definitions(tetgen PRIVATE -DTETLIBRARY)
# Add the modified version of the tetgen library
add_library(tetgen_lib STATIC
${tetgen_SOURCE_DIR}/tetgen_mod.cxx
${tetgen_SOURCE_DIR}/predicates.cxx
)

target_include_directories(tetgen INTERFACE "${tetgen_SOURCE_DIR}")
# Define the TETLIBRARY macro for usage
target_compile_definitions(tetgen_lib PRIVATE -DTETLIBRARY)

endif()
# Include the tetgen source directory for the library
target_include_directories(tetgen_lib INTERFACE "${tetgen_SOURCE_DIR}")

# Disable warnings from the library target
target_compile_options(tetgen PRIVATE -w)
target_compile_options(tetgen_lib PRIVATE -w)
19 changes: 9 additions & 10 deletions docs/quickstart/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ Build Options

The available options are the following:

================================================ ===================================================================================================================================
Name (Default) Options
================================================ ===================================================================================================================================
POLYHEDRAL_GRAVITY_PARALLELIZATION (:code:`CPP`) :code:`CPP` = Serial Execution / :code:`OMP` or :code:`TBB` = Parallel Execution with OpenMP or Intel's TBB
LOGGING_LEVEL (:code:`INFO`) :code:`TRACE`, :code:`DEBUG`, :code:`INFO`, :code:`WARN`, :code:`ERROR`, :code:`CRITICAL`, :code:`OFF`
BUILD_POLYHEDRAL_GRAVITY_DOCS (:code:`OFF`) Build this documentation
BUILD_POLYHEDRAL_GRAVITY_TESTS (:code:`ON`) Build the Tests
BUILD_POLYHEDRAL_PYTHON_INTERFACE (:code:`ON`) Build the Python interface
================================================ ===================================================================================================================================

====================================================== ============================================================================================================
Name (Default) Options
====================================================== ============================================================================================================
POLYHEDRAL_GRAVITY_PARALLELIZATION (:code:`CPP`) :code:`CPP` = Serial Execution / :code:`OMP` or :code:`TBB` = Parallel Execution with OpenMP or Intel's TBB
POLYHEDRAL_GRAVITY_LOGGING_LEVEL (:code:`INFO`) :code:`TRACE`, :code:`DEBUG`, :code:`INFO`, :code:`WARN`, :code:`ERROR`, :code:`CRITICAL`, :code:`OFF`
BUILD_POLYHEDRAL_GRAVITY_DOCS (:code:`OFF`) Build this documentation
BUILD_POLYHEDRAL_GRAVITY_TESTS (:code:`ON`) Build the Tests
BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE (:code:`ON`) Build the Python interface
====================================================== ============================================================================================================

Dependencies (automatically set-up)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
25 changes: 23 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Modify to change the parallelization (Default value: TBB)
"POLYHEDRAL_GRAVITY_PARALLELIZATION": "TBB",
# Default value (INFO=2)
"LOGGING_LEVEL": "INFO",
"POLYHEDRAL_GRAVITY_LOGGING_LEVEL": "INFO",
# Not required for the python interface (--> OFF)
"BUILD_POLYHEDRAL_GRAVITY_DOCS": "OFF",
# Not required for the python interface (--> OFF)
Expand Down Expand Up @@ -53,6 +53,27 @@ def get_cmake_generator():
else:
return None

def get_version():
"""Returns the version of the polyhedral gravity package by reading the CMake file."""
# Path to the CMake file
cmake_file = os.path.join(os.path.dirname(__file__), "version.cmake" )

# Check if the CMake file exists
if not os.path.exists(cmake_file):
raise FileNotFoundError(f"CMake file not found: {cmake_file}")

# Open and read the file
with open(cmake_file, "r") as file:
content = file.read()

# Use regex to extract the PROJECT_VERSION
version_match = re.search(r'set\(PROJECT_VERSION\s+([^\s)]+)\)', content)
if version_match:
return version_match.group(1)
else:
raise ValueError("Version string not found in CMakeLists.txt")


# -----------------------------------------------------------------------------------------
# The following is adapted from https://github.com/pybind/cmake_example/blob/master/setup.py
# -----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -173,7 +194,7 @@ def build_extension(self, ext):
# --------------------------------------------------------------------------------
setup(
name="polyhedral_gravity",
version="3.2.1",
version=get_version(),
author="Jonas Schuhmacher",
author_email="[email protected]",
description="Package to compute full gravity tensor of a given constant density polyhedron for arbitrary points "
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (BUILD_POLYHEDRAL_GRAVITY_LIBRARY)
target_link_libraries(${PROJECT_NAME}_lib
spdlog::spdlog
yaml-cpp::yaml-cpp
tetgen
tetgen_lib
xsimd
Thrust
)
Expand Down Expand Up @@ -60,7 +60,7 @@ if(BUILD_POLYHEDRAL_GRAVITY_PYTHON_INTERFACE)

target_link_libraries(polyhedral_gravity PUBLIC
spdlog::spdlog
tetgen
tetgen_lib
xsimd
Thrust
)
Expand Down
Loading
Loading