Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Feb 6, 2025
1 parent 7e6135e commit 3a4fc04
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 43 deletions.
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ FetchContent_Declare(
walberla
GIT_REPOSITORY https://i10git.cs.fau.de/walberla/walberla.git
GIT_TAG f36fa0a68bae59f0b516f6587ea8fa7c24a41141
PATCH_COMMAND patch -p0 < ${CMAKE_CURRENT_SOURCE_DIR}/cmake/walberla.patch
)
FetchContent_Declare(
stokesian_dynamics
Expand Down Expand Up @@ -769,11 +770,6 @@ if(ESPRESSO_BUILD_WITH_WALBERLA)
set(BUILD_SHARED_LIBS ${ESPRESSO_BUILD_SHARED_LIBS_DEFAULT})
set(CMAKE_SHARED_LIBRARY_PREFIX "${ESPRESSO_SHARED_LIBRARY_PREFIX}")
endif()
target_compile_options(
blockforest
PRIVATE
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,14>>:-Wno-alloc-size>
)
set(WALBERLA_LIBS
walberla::core walberla::domain_decomposition walberla::blockforest
walberla::boundary walberla::field walberla::lbm walberla::timeloop
Expand Down
11 changes: 11 additions & 0 deletions cmake/walberla.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
https://i10git.cs.fau.de/walberla/walberla/-/merge_requests/709
--- src/core/mpi/RecvBuffer.h
+++ src/core/mpi/RecvBuffer.h
@@ -230 +230 @@
- sb.begin_ = new T[0];
+ sb.begin_ = nullptr;
--- src/core/mpi/SendBuffer.h
+++ src/core/mpi/SendBuffer.h
@@ -700 +700 @@
- begin_ = new T[0];
+ begin_ = nullptr;
24 changes: 7 additions & 17 deletions src/core/cuda/init_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cuda_runtime.h>

#include <cstring>
#include <memory>
#include <string>

#if defined(OMPI_MPI_H) || defined(_MPI_H)
Expand Down Expand Up @@ -98,25 +99,14 @@ int cuda_get_device() {
}

bool cuda_test_device_access() {
int *d = nullptr;
auto const deleter = [](int *p) { cudaFree(reinterpret_cast<void *>(p)); };
int *ptr = nullptr;
int h = 42;
cudaError_t err;

err = cudaMalloc((void **)&d, sizeof(int));
if (err != cudaSuccess) {
throw cuda_runtime_error_cuda(err);
}
err = cudaMemcpy(d, &h, sizeof(int), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
cudaFree(d);
throw cuda_runtime_error_cuda(err);
}
CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&ptr), sizeof(int)));
std::unique_ptr<int, decltype(deleter)> d(ptr, deleter);
CUDA_CHECK(cudaMemcpy(d.get(), &h, sizeof(int), cudaMemcpyHostToDevice));
h = 0;
err = cudaMemcpy(&h, d, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(d);
if (err != cudaSuccess) {
throw cuda_runtime_error_cuda(err);
}
CUDA_CHECK(cudaMemcpy(&h, d.get(), sizeof(int), cudaMemcpyDeviceToHost));
return h != 42;
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/forces_inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,15 @@ inline void add_non_bonded_pair_force(

/** Compute the bonded interaction force between particle pairs.
*
* @param[in] iaparams Bonded parameters for the interaction.
* @param[in] p1 First particle.
* @param[in] p2 Second particle.
* @param[in] iaparams Bonded parameters for the interaction.
* @param[in] dx Vector between @p p1 and @p p2.
* @param[in] kernel Coulomb force kernel.
*/
inline std::optional<Utils::Vector3d> calc_bond_pair_force(
Particle const &p1, Particle const &p2,
Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &dx,
Bonded_IA_Parameters const &iaparams, Particle const &p1,
Particle const &p2, Utils::Vector3d const &dx,
Coulomb::ShortRangeForceKernel::kernel_type const *kernel) {
if (auto const *iap = boost::get<FeneBond>(&iaparams)) {
return iap->force(dx);
Expand Down Expand Up @@ -334,7 +334,7 @@ inline bool add_bonded_two_body_force(
return false;
}
} else {
auto result = calc_bond_pair_force(p1, p2, iaparams, dx, kernel);
auto result = calc_bond_pair_force(iaparams, p1, p2, dx, kernel);
if (result) {
p1.force() += result.value();
p2.force() -= result.value();
Expand Down
2 changes: 1 addition & 1 deletion src/core/pressure_inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ calc_bonded_virial_pressure_tensor(
Particle const &p2, BoxGeometry const &box_geo,
Coulomb::ShortRangeForceKernel::kernel_type const *kernel) {
auto const dx = box_geo.get_mi_vector(p1.pos(), p2.pos());
auto const result = calc_bond_pair_force(p1, p2, iaparams, dx, kernel);
auto const result = calc_bond_pair_force(iaparams, p1, p2, dx, kernel);
if (result) {
auto const &force = result.value();

Expand Down
18 changes: 9 additions & 9 deletions src/core/rattle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
#include <functional>
#include <span>

static void check_convergence(int cnt, char const *const name) {
static constexpr char const *const msg = " failed to converge after ";
if (cnt >= SHAKE_MAX_ITERATIONS) {
runtimeErrorMsg() << name << msg << cnt << " iterations";
}
}

/**
* @brief copy current position
*
Expand Down Expand Up @@ -168,10 +175,7 @@ void correct_position_shake(CellStructure &cs, BoxGeometry const &box_geo,
apply_positional_correction(particles);
cs.ghosts_update(Cells::DATA_PART_POSITION | Cells::DATA_PART_MOMENTUM);
}
if (cnt >= SHAKE_MAX_ITERATIONS) {
runtimeErrorMsg() << "RATTLE failed to converge after " << cnt
<< " iterations";
}
check_convergence(cnt, "RATTLE");

auto const resort_level =
cs.check_resort_required() ? Cells::RESORT_LOCAL : Cells::RESORT_NONE;
Expand Down Expand Up @@ -245,11 +249,7 @@ void correct_velocity_shake(CellStructure &cs, BoxGeometry const &box_geo,
apply_velocity_correction(particles);
cs.ghosts_update(Cells::DATA_PART_MOMENTUM);
}

if (cnt >= SHAKE_MAX_ITERATIONS) {
runtimeErrorMsg() << "VEL RATTLE failed to converge after " << cnt
<< " iterations";
}
check_convergence(cnt, "VEL RATTLE");
}

#endif
34 changes: 34 additions & 0 deletions src/core/unit_tests/EspressoSystemStandAlone_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace utf = boost::unit_test;
#include "electrostatics/coulomb.hpp"
#include "electrostatics/p3m.hpp"
#include "electrostatics/p3m.impl.hpp"
#include "energy_inline.hpp"
#include "forces_inline.hpp"
#include "galilei/Galilei.hpp"
#include "integrate.hpp"
#include "integrators/Propagation.hpp"
Expand Down Expand Up @@ -584,6 +586,38 @@ BOOST_FIXTURE_TEST_CASE(espresso_system_stand_alone, ParticleFactory) {
} else {
get_particle_node_parallel(12345);
}
std::vector<Particle> plist(5u);
std::vector<Particle *> plist_ptr{};
for (auto &p : plist) {
plist_ptr.emplace_back(&p);
}
auto const energy_kernel = [&](std::size_t n) {
auto const &box_geo = *system.box_geo;
auto const none = NoneBond{};
auto const beg = std::begin(plist_ptr);
calc_bonded_energy(none, plist[0], {beg, n}, box_geo, nullptr);
};
auto const force_kernel = [&](std::size_t n) {
auto const &box_geo = *system.box_geo;
auto const &pl = plist; // alias to improve code coverage
auto const none = NoneBond{};
if (n == 1u) {
calc_bond_pair_force(none, pl[0], pl[1], {}, nullptr);
} else if (n == 2u) {
calc_bonded_three_body_force(none, box_geo, pl[0], pl[1], pl[2]);
} else if (n == 3u) {
calc_bonded_four_body_force(none, box_geo, pl[0], pl[1], pl[2], pl[3]);
}
};
static_cast<void>(energy_kernel(0u));
BOOST_CHECK_THROW(energy_kernel(1u), BondUnknownTypeError);
BOOST_CHECK_THROW(energy_kernel(2u), BondUnknownTypeError);
BOOST_CHECK_THROW(energy_kernel(3u), BondUnknownTypeError);
BOOST_CHECK_THROW(energy_kernel(4u), BondInvalidSizeError);
static_cast<void>(force_kernel(0u));
BOOST_CHECK_THROW(force_kernel(1u), BondUnknownTypeError);
BOOST_CHECK_THROW(force_kernel(2u), BondUnknownTypeError);
BOOST_CHECK_THROW(force_kernel(3u), BondUnknownTypeError);
#ifdef CUDA
BOOST_CHECK_THROW(
invoke_skip_cuda_exceptions([]() { throw std::runtime_error(""); }),
Expand Down
8 changes: 1 addition & 7 deletions src/walberla_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,11 @@ function(espresso_configure_walberla_target)
LIBRARY DESTINATION ${ESPRESSO_INSTALL_PYTHON}/espressomd)
endfunction()

add_library(espresso_walberla_cpp_flags_overrides INTERFACE)
target_compile_options(
espresso_walberla_cpp_flags_overrides
INTERFACE
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,14>>:-Wno-alloc-size>
)
add_library(espresso_walberla_cpp_flags INTERFACE)
add_library(espresso::walberla::cpp_flags ALIAS espresso_walberla_cpp_flags)
target_link_libraries(
espresso_walberla_cpp_flags
INTERFACE espresso::cpp_flags espresso_walberla_cpp_flags_overrides
INTERFACE espresso::cpp_flags
$<$<BOOL:${ESPRESSO_BUILD_WITH_WALBERLA_AVX}>:espresso::avx_flags>)
add_library(espresso_walberla_cuda_flags INTERFACE)
add_library(espresso::walberla::cuda_flags ALIAS espresso_walberla_cuda_flags)
Expand Down

0 comments on commit 3a4fc04

Please sign in to comment.