Skip to content

Commit

Permalink
Merge pull request #2896 from boutproject/sundials-v7-next-rebase
Browse files Browse the repository at this point in the history
Update SUNDIALS support (rebase onto next)
  • Loading branch information
ZedThree committed Apr 8, 2024
2 parents edcb570 + 2c0c60d commit 1d52b38
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 597 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Checks: 'clang-diagnostic-*,clang-analyzer-*,performance-*,readability-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-misc-non-private-member-variables-in-classes,-clang-analyzer-optin.mpi*,-bugprone-exception-escape,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-misc-no-recursion,-bugprone-easily-swappable-parameters'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle: file
CheckOptions:

Expand Down
12 changes: 2 additions & 10 deletions cmake/FindSUNDIALS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,8 @@ endforeach()

if (SUNDIALS_INCLUDE_DIR)
file(READ "${SUNDIALS_INCLUDE_DIR}/sundials_config.h" SUNDIALS_CONFIG_FILE)
string(FIND "${SUNDIALS_CONFIG_FILE}" "SUNDIALS_PACKAGE_VERSION" index)
if("${index}" LESS 0)
# Version >3
set(SUNDIALS_VERSION_REGEX_PATTERN
".*#define SUNDIALS_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\".*")
else()
# Version <3
set(SUNDIALS_VERSION_REGEX_PATTERN
".*#define SUNDIALS_PACKAGE_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\".*")
endif()
set(SUNDIALS_VERSION_REGEX_PATTERN
".*#define SUNDIALS_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\".*")
string(REGEX MATCH ${SUNDIALS_VERSION_REGEX_PATTERN} _ "${SUNDIALS_CONFIG_FILE}")
set(SUNDIALS_VERSION_MAJOR ${CMAKE_MATCH_1} CACHE STRING "")
set(SUNDIALS_VERSION_MINOR ${CMAKE_MATCH_2} CACHE STRING "")
Expand Down
6 changes: 3 additions & 3 deletions cmake/SetupBOUTThirdParty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ if (BOUT_USE_SUNDIALS)
include(FetchContent)
FetchContent_Declare(
sundials
GIT_REPOSITORY https://github.com/ZedThree/sundials
GIT_TAG cmake-export-fixes
GIT_REPOSITORY https://github.com/LLNL/sundials
GIT_TAG v7.0.0
)
# Note: These are settings for building SUNDIALS
set(EXAMPLES_ENABLE_C OFF CACHE BOOL "" FORCE)
Expand All @@ -297,7 +297,7 @@ if (BOUT_USE_SUNDIALS)
FetchContent_MakeAvailable(sundials)
message(STATUS "SUNDIALS done configuring")
else()
find_package(SUNDIALS REQUIRED)
find_package(SUNDIALS 4 REQUIRED)
endif()
target_link_libraries(bout++ PUBLIC SUNDIALS::nvecparallel)
target_link_libraries(bout++ PUBLIC SUNDIALS::cvode)
Expand Down
85 changes: 39 additions & 46 deletions include/bout/sundials_backports.hxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Backports for SUNDIALS compatibility between versions 3-6
// Backports for SUNDIALS compatibility between versions 4-7
//
// These are common backports shared between the CVode, ARKode, and IDA solvers
//
Expand All @@ -8,74 +8,67 @@
#ifndef BOUT_SUNDIALS_BACKPORTS_H
#define BOUT_SUNDIALS_BACKPORTS_H

#include "bout/bout_types.hxx"

#include <type_traits>

#include <nvector/nvector_parallel.h>
#include <sundials/sundials_config.h>
#include <sundials/sundials_iterative.h>
#include <sundials/sundials_nonlinearsolver.h>
#include <sundials/sundials_types.h>

#if SUNDIALS_VERSION_MAJOR >= 3
#include <sunlinsol/sunlinsol_spgmr.h>
#endif

#if SUNDIALS_VERSION_MAJOR >= 4
#include <sundials/sundials_nonlinearsolver.h>
#include <sunnonlinsol/sunnonlinsol_fixedpoint.h>
#include <sunnonlinsol/sunnonlinsol_newton.h>
#endif

#include "bout/unused.hxx"

#if SUNDIALS_VERSION_MAJOR < 3
using SUNLinearSolver = int*;
inline void SUNLinSolFree([[maybe_unused]] SUNLinearSolver solver) {}
using sunindextype = long int;
#if SUNDIALS_VERSION_MAJOR >= 6
#include <sundials/sundials_context.hpp>
#endif

#if SUNDIALS_VERSION_MAJOR < 4
using SUNNonlinearSolver = int*;
inline void SUNNonlinSolFree([[maybe_unused]] SUNNonlinearSolver solver) {}
#if SUNDIALS_VERSION_MAJOR < 6
using sundials_real_type = realtype;
#else
using sundials_real_type = sunrealtype;
#endif

#if SUNDIALS_VERSION_MAJOR < 6
namespace sundials {
struct Context {
Context(void* comm [[maybe_unused]]) {}
};
} // namespace sundials
static_assert(std::is_same_v<BoutReal, sundials_real_type>,
"BOUT++ and SUNDIALS real types do not match");

using SUNContext = sundials::Context;
#define SUNDIALS_CONTROLLER_SUPPORT \
(SUNDIALS_VERSION_MAJOR > 6 \
|| SUNDIALS_VERSION_MAJOR == 6 && SUNDIALS_VERSION_MINOR >= 7)
#define SUNDIALS_TABLE_BY_NAME_SUPPORT \
(SUNDIALS_VERSION_MAJOR > 6 \
|| SUNDIALS_VERSION_MAJOR == 6 && SUNDIALS_VERSION_MINOR >= 4)

#if SUNDIALS_VERSION_MAJOR < 6
constexpr auto SUN_PREC_RIGHT = PREC_RIGHT;
constexpr auto SUN_PREC_LEFT = PREC_LEFT;
constexpr auto SUN_PREC_NONE = PREC_NONE;

inline N_Vector N_VNew_Parallel(MPI_Comm comm, sunindextype local_length,
sunindextype global_length,
[[maybe_unused]] SUNContext sunctx) {
return N_VNew_Parallel(comm, local_length, global_length);
}
namespace sundials {
using Context = std::nullptr_t;
} // namespace sundials
#endif

#if SUNDIALS_VERSION_MAJOR >= 3
inline SUNLinearSolver SUNLinSol_SPGMR(N_Vector y, int pretype, int maxl,
[[maybe_unused]] SUNContext sunctx) {
#if SUNDIALS_VERSION_MAJOR == 3
return SUNSPGMR(y, pretype, maxl);
inline sundials::Context createSUNContext([[maybe_unused]] MPI_Comm& comm) {
#if SUNDIALS_VERSION_MAJOR < 6
return nullptr;
#elif SUNDIALS_VERSION_MAJOR < 7
return sundials::Context(static_cast<void*>(&comm));
#else
return SUNLinSol_SPGMR(y, pretype, maxl);
return sundials::Context(comm);
#endif
}
#if SUNDIALS_VERSION_MAJOR >= 4
inline SUNNonlinearSolver SUNNonlinSol_FixedPoint(N_Vector y, int m,
[[maybe_unused]] SUNContext sunctx) {
return SUNNonlinSol_FixedPoint(y, m);
}

inline SUNNonlinearSolver SUNNonlinSol_Newton(N_Vector y,
[[maybe_unused]] SUNContext sunctx) {
return SUNNonlinSol_Newton(y);
template <typename Func, typename... Args>
inline decltype(auto) callWithSUNContext(Func f, [[maybe_unused]] sundials::Context& ctx,
Args&&... args) {
#if SUNDIALS_VERSION_MAJOR < 6
return f(std::forward<Args>(args)...);
#else
return f(std::forward<Args>(args)..., ctx);
#endif
}
#endif // SUNDIALS_VERSION_MAJOR >= 4
#endif // SUNDIALS_VERSION_MAJOR >= 3
#endif // SUNDIALS_VERSION_MAJOR < 6

#endif // BOUT_SUNDIALS_BACKPORTS_H
7 changes: 4 additions & 3 deletions manual/sphinx/user_docs/advanced_install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ solver. Currently, BOUT++ also supports the SUNDIALS solvers CVODE, IDA
and ARKODE which are available from
https://computation.llnl.gov/casc/sundials/main.html.

.. note:: BOUT++ currently supports SUNDIALS > 2.6, up to 5.4.0 as of
September 2020. It is advisable to use the highest possible
version
.. note:: BOUT++ currently supports SUNDIALS > 2.6, up to 6.7.0 as of
January 2024. It is advisable to use the highest possible
version. Support for SUNDIALS versions < 4 will be removed
in the next release.

The full installation guide is found in the downloaded ``.tar.gz``,
but we will provide a step-by-step guide to install it and make it
Expand Down

0 comments on commit 1d52b38

Please sign in to comment.