Skip to content
Open
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
246 changes: 93 additions & 153 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# SPDX-FileCopyrightText: 2015 UCLA
# SPDX-FileCopyrightText: 2025 Bernhard Haas (GFZ)
# SPDX-FileCopyrightText: 2025 GFZ Helmholtz Centre for Geosciences
#
# SPDX-License-Identifier: BSD-3-Clause

# CMakeLists.txt
# Buildsystem for VERB4D - See README.md for build instructions
#
# Author: Anthony Miyaguchi (acmiyaguchi@gmail.com)
cmake_minimum_required(VERSION 3.14)
project(VERB4D LANGUAGES C CXX)

cmake_minimum_required(VERSION 3.9)
project(VERB4D C CXX)
# --- Project Settings ---
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# --- Options ---
option(DATA_ASSIMILATION "Enable data assimilation" OFF)
option(DATA_ASSIMILATION_DEBUG "Enable debug output for data assimilation" OFF)
option(USE_PPFV "Compile positive preserving finite volume method" OFF)
option(FAST_CONVECTION "Use larger time steps for convection (less accurate)" OFF)
option(PYTHON_BINDINGS "Build Python bindings using pybind11" OFF)
option(SAVE_PSD_LOST_CONV "Save lost PSD during convection step" OFF)
option(LU_CACHING "Cache LU decompositions during Lapack calls" OFF)

# --- Dependencies ---
find_package(OpenMP)
include(GetBlasType) # this defines BLAS_LIBRARIES_TO_LINK and BLAS_INCLUDE_DIRS

if(USE_PPFV)
message(STATUS "PPFV enabled: Finding or downloading Eigen3 and xtensor...")
include(FindOrDownloadEigen)
include(FindOrDownloadXTensor)
endif()

# Enable/disable data assimilation
set(DATA_ASSIMILATION FALSE CACHE BOOL "Enable data assimilation")

# Math Constants are not defined in Standard C++. To use them, we must first define _USE_MATH_DEFINES
add_compile_definitions(_USE_MATH_DEFINES)

# Recursively search for header files to include for project files. This isn't
# necessary to compile, but its nice to have in Visual Studio solutions.
file(GLOB_RECURSE VERB4D_HEADERS *.h)
# --- Source Files ---
file(GLOB_RECURSE VERB4D_HEADERS src/*.h)

# Add new source files here and re-run cmake
set(VERB4D_SOURCES
src/VERB4D_Solver.cpp
src/Convection_1D_ULTIMATE_QUICKEST6.cpp
Expand All @@ -42,165 +52,95 @@ set(VERB4D_SOURCES
src/MonotCubicInterpolator.cpp
src/Parameters.cpp
src/ReadInitialData.cpp
src/UpdatableMatrix.cpp)
src/UpdatableMatrix.cpp
)

# Add data assimilation source files and macros
if(DATA_ASSIMILATION)
add_definitions(-DDATA_ASSIMILATION)

set(VERB4D_SOURCES
${VERB4D_SOURCES}
message(STATUS "Data Assimilation enabled")
list(APPEND VERB4D_SOURCES
src/MatrixOperations.cpp
src/CustomDate.cpp
src/DataAssimilationHelper.cpp
src/DataAssimilation.cpp)

if(MATLAB_FOUND)
set(VERB4D_SOURCES
${VERB4D_SOURCES}
src/PMF.cpp)
endif(MATLAB_FOUND)

if(DATA_ASSIMILATION_DEBUG)
message("Saving DA debug output!")
add_definitions(-DDATA_ASSIMILATION_DEBUG)
endif(DATA_ASSIMILATION_DEBUG)
src/DataAssimilation.cpp
)
endif()

endif(DATA_ASSIMILATION)
if(USE_PPFV)
list(APPEND VERB4D_SOURCES src/Diffusion_2D_PPFV.cpp)
endif()

# Build and link
# --- Main Executable Target ---
add_executable(VERB4D_Solver ${VERB4D_SOURCES} ${VERB4D_HEADERS})

# Apply different compiler options based on the compiler
# --- Compiler Definitions and Options ---
target_compile_definitions(VERB4D_Solver PRIVATE
_USE_MATH_DEFINES
$<$<BOOL:${DATA_ASSIMILATION}>:DATA_ASSIMILATION>
$<$<BOOL:${DATA_ASSIMILATION_DEBUG}>:DATA_ASSIMILATION_DEBUG>
$<$<BOOL:${USE_PPFV}>:USE_PPFV>
$<$<BOOL:${FAST_CONVECTION}>:FAST_CONVECTION>
$<$<BOOL:${SAVE_PSD_LOST_CONV}>:SAVE_PSD_LOST_CONV>
$<$<BOOL:${LU_CACHING}>:LU_CACHING>
$<$<CONFIG:Release>:RELEASE>
)

if(MSVC)
# For MSVC, use /W4 for warnings and /WX to treat warnings as errors.
# /wd4804 - T should not be bool in some cases. TODO: solve later
# /wd4996 'fpclassify': ambiguous call to overloaded function [C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/corecrt_math.h"]
# /wd4849 OpenMP 'collapse' clause ignored in 'parallel for' directive
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX /permissive /wd4804 /wd4996 /wd4849")
target_compile_options(VERB4D_Solver PRIVATE /W4)
else()
# For GCC/Clang, use -Wall -Wextra -Wpedantic (-Werror removed)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
target_compile_options(VERB4D_Solver PRIVATE -Wall -Wextra -Wpedantic)
endif()

# Add directories for include files
target_include_directories(VERB4D_Solver PUBLIC ${PROJECT_SOURCE_DIR}/src)


# Check if interprocedural optimization (link time optimization) is supported by the compiler
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)

if(NOT DEFINED BLAS_TYPE)
message("No BLAS_TYPE specified, choosing LAPACK")
set(BLAS_TYPE LAPACK)
endif()
string(TOUPPER ${BLAS_TYPE} BLAS_TYPE)
if(${BLAS_TYPE} STREQUAL "LAPACK")
if(WIN32)
# The libraries for lapack and BLAS for windows are in the lib folder
set(LAPACK_LIBRARIES ${CMAKE_SOURCE_DIR}/lib/clapack.lib)
set(BLAS_LIBRARIES ${CMAKE_SOURCE_DIR}/lib/blas.lib)
set(F2C_LIBRARIES ${CMAKE_SOURCE_DIR}/lib/libf2c.lib)
list(APPEND DEPLIBS ${F2C_LIBRARIES})
else(WIN32)
# Installed by distribution's package manager
find_package(LAPACK REQUIRED)
endif(WIN32)
list(APPEND DEPLIBS ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
elseif(${BLAS_TYPE} STREQUAL "OPENBLAS")
add_library(openblas SHARED IMPORTED)
if(WIN32)
if(NOT DEFINED OPENBLAS_LIBPATH)
set(OPENBLAS_LIBPATH $ENV{HOMEPATH}/AppData/Local/OpenBLAS/openblas.lib)
message("No OPENBLAS_LIBPATH specified, choosing ${OPENBLAS_LIBPATH}")
endif()
set_target_properties(openblas PROPERTIES IMPORTED_IMPLIB ${OPENBLAS_LIBPATH})
else()
if(NOT DEFINED OPENBLAS_LIBPATH)
set(OPENBLAS_LIBPATH $ENV{HOME}/.local/lib/libopenblas.so)
message("No OPENBLAS_LIBPATH specified, choosing ${OPENBLAS_LIBPATH}")
endif()
set_target_properties(openblas PROPERTIES IMPORTED_LOCATION ${OPENBLAS_LIBPATH})
endif()
list(APPEND DEPLIBS openblas)
elseif(${BLAS_TYPE} STREQUAL "MKL")
set(MKL_THREADING sequential)
set(MKL_INTERFACE lp64)
find_package(MKL CONFIG REQUIRED)
list(APPEND DEPLIBS ${MKL_IMPORTED_TARGETS})
target_include_directories(VERB4D_Solver PUBLIC ${MKL_INCLUDE})
add_definitions(-DMKL_FOUND)
else()
message( FATAL_ERROR "Unknown BLAS_TYPE. Choose either LAPACK, OPENBLAS or MKL" )
# --- Include Directories ---
target_include_directories(VERB4D_Solver PUBLIC src)
if(BLAS_INCLUDE_DIRS)
target_include_directories(VERB4D_Solver PRIVATE ${BLAS_INCLUDE_DIRS})
endif()

# Find OpenMP
find_package(OpenMP REQUIRED)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)

# Find Matlab
find_package(Matlab COMPONENTS ENG_LIBRARY MAT_LIBRARY)

if(Matlab_FOUND)
include_directories(${Matlab_INCLUDE_DIRS})
list(APPEND DEPLIBS
${Matlab_MAT_LIBRARY}
${Matlab_MX_LIBRARY}
${Matlab_ENG_LIBRARY})
# Set the definition for MATLAB_CAPABLE
add_definitions(-DMATLAB_CAPABLE=true)
endif()
# --- Link Libraries ---
target_link_libraries(VERB4D_Solver PRIVATE ${BLAS_LIBRARIES_TO_LINK})

# use interprocidural optimization if supported
if( ipo_supported )
message(STATUS "IPO enabled")
set_property(TARGET VERB4D_Solver PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
if(OpenMP_CXX_FOUND)
message(STATUS "OpenMP found! Parallelization enabled.")
target_link_libraries(VERB4D_Solver PRIVATE OpenMP::OpenMP_CXX)
else()
message(STATUS "IPO not supported: <${ipo_error}>")
message(WARNING "OpenMP not found. VERB4D will be compiled in SERIAL mode.")
endif()

target_link_libraries(VERB4D_Solver ${DEPLIBS})

# Flag compile type feedback
if(DEFINED CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE STREQUAL "")
message(WARNING "\nCMAKE_BUILD_TPYE not specified. Run cmake with -DCMAKE_BUILD_TYPE=Release or -DCMAKE_BUILD_TYPE=Debug")
else(CMAKE_BUILD_TYPE STREQUAL "")
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_upper)
if(CMAKE_BUILD_TYPE_upper STREQUAL "RELEASE")
add_definitions(-DRELEASE)
endif()
endif()
# --- PPFV ---
if(USE_PPFV)
set(PPFV_SOURCES
src/ppfv/Mesh.cpp
src/ppfv/Solver.cpp
src/ppfv/Cases/VERB_ppfv2d.cpp
)
add_library(ppfv SHARED ${PPFV_SOURCES} ${VERB4D_HEADERS})

target_link_libraries(ppfv PUBLIC Eigen3::Eigen)
target_link_libraries(ppfv PUBLIC xtensor)

# Link the internal library to the main solver
target_link_libraries(VERB4D_Solver PRIVATE ppfv)
endif()

# Flag to decide convection step size in Convection_2D.cpp
if(${FAST_CONVECTION})
add_definitions(-DFAST_CONVECTION)
# --- Optimization (IPO/LTO) ---
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)
if(ipo_supported)
set_property(TARGET VERB4D_Solver PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
message(STATUS "IPO/LTO enabled for VERB4D_Solver")
endif()
unset(FAST_CONVECTION CACHE)

# Python bindings
if (${PYTHON_BINDINGS})
include_directories(${PROJECT_SOURCE_DIR}/extern/pybind11/include ${PROJECT_SOURCE_DIR}/src/python_bindings ${PROJECT_SOURCE_DIR}/src)
# --- Python Bindings ---
if(PYTHON_BINDINGS)
message(STATUS "Building Python bindings with pybind11...")
add_subdirectory(extern/pybind11)
pybind11_add_module(verb4d_solver src/python_bindings/Define_python_modules.cpp ${VERB4D_SOURCES})
target_link_libraries(verb4d_solver PUBLIC ${DEPLIBS})
endif()
unset(PYTHON_BINDINGS CACHE)

# save lost PSD during Convection_2D
if(${SAVE_PSD_LOST_CONV})
add_definitions(-DSAVE_PSD_LOST_CONV)
endif()
unset(SAVE_PSD_LOST CACHE)
pybind11_add_module(verb4d_solver src/python_bindings/Define_python_modules.cpp ${VERB4D_SOURCES})
target_link_libraries(verb4d_solver PUBLIC OpenMP::OpenMP_CXX ${BLAS_LIBRARIES_TO_LINK})
include_directories(${PROJECT_SOURCE_DIR}/src/python_bindings ${PROJECT_SOURCE_DIR}/src)

# cache LU decomposition during Lapack call in Diffusion_2D.cpp
if(${LU_CACHING})
add_definitions(-DLU_CACHING)
endif()
unset(SAVE_PSD_LOST CACHE)
if(USE_PPFV)
target_compile_definitions(verb4d_solver PRIVATE USE_PPFV)
target_link_libraries(verb4d_solver PUBLIC ppfv)
endif()
endif()
Loading