Skip to content

Commit

Permalink
Make waLBerla dependency private
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Feb 21, 2024
1 parent d0c9154 commit 220aa61
Show file tree
Hide file tree
Showing 28 changed files with 312 additions and 327 deletions.
1 change: 1 addition & 0 deletions src/core/lb/particle_coupling.cpp
Expand Up @@ -86,6 +86,7 @@ Utils::Vector3d lb_drag_force(LB::Solver const &lb, double lb_gamma,
/**
* @brief Check if a position is within the local box + halo.
*
* @param local_box Local geometry
* @param pos Position to check
* @param halo Halo
*
Expand Down
4 changes: 2 additions & 2 deletions src/core/unit_tests/ek_interface_test.cpp
Expand Up @@ -85,7 +85,7 @@ static auto make_ek_actor() {
ek_lattice = std::make_shared<LatticeWalberla>(
params.grid_dimensions, ::communicator.node_grid, n_ghost_layers);
ek_container = std::make_shared<EK::EKWalberla::ek_container_type>(
params.tau, new_ek_poisson_none(ek_lattice, single_precision));
params.tau, walberla::new_ek_poisson_none(ek_lattice, single_precision));
ek_reactions = std::make_shared<EK::EKWalberla::ek_reactions_type>();
ek_instance = std::make_shared<EK::EKWalberla>(ek_container, ek_reactions);
#endif
Expand Down Expand Up @@ -146,7 +146,7 @@ BOOST_AUTO_TEST_CASE(ek_interface_walberla) {
auto constexpr single_precision = true;
auto constexpr stoich = 1.;
auto constexpr order = 2.;
auto ek_species = new_ek_walberla(
auto ek_species = walberla::new_ek_walberla(
espresso::ek_lattice, params.diffusion, params.kT, params.valency,
params.ext_efield, params.density, false, false, single_precision);
auto ek_reactant = std::make_shared<EKReactant>(ek_species, stoich, order);
Expand Down
4 changes: 2 additions & 2 deletions src/script_interface/walberla/EKFFT.hpp
Expand Up @@ -55,8 +55,8 @@ class EKFFT : public EKPoissonSolver {
auto const permittivity =
get_value<double>(args, "permittivity") * m_conv_permittivity;

m_instance = new_ek_poisson_fft(m_lattice->lattice(), permittivity,
m_single_precision);
m_instance = ::walberla::new_ek_poisson_fft(
m_lattice->lattice(), permittivity, m_single_precision);

add_parameters({
{"permittivity",
Expand Down
3 changes: 2 additions & 1 deletion src/script_interface/walberla/EKNone.hpp
Expand Up @@ -45,7 +45,8 @@ class EKNone : public EKPoissonSolver {
m_single_precision = get_value_or<bool>(args, "single_precision", false);
m_lattice = get_value<std::shared_ptr<LatticeWalberla>>(args, "lattice");

m_instance = new_ek_poisson_none(m_lattice->lattice(), m_single_precision);
m_instance = ::walberla::new_ek_poisson_none(m_lattice->lattice(),
m_single_precision);

add_parameters({
{"single_precision", AutoParameter::read_only,
Expand Down
26 changes: 12 additions & 14 deletions src/script_interface/walberla/EKReaction.hpp
Expand Up @@ -27,9 +27,9 @@
#include "LatticeIndices.hpp"
#include "LatticeWalberla.hpp"

#include <walberla_bridge/electrokinetics/ek_walberla_init.hpp>
#include <walberla_bridge/electrokinetics/reactions/EKReactionBase.hpp>
#include <walberla_bridge/src/electrokinetics/reactions/EKReactionImplBulk.hpp>
#include <walberla_bridge/src/electrokinetics/reactions/EKReactionImplIndexed.hpp>
#include <walberla_bridge/electrokinetics/reactions/EKReactionBaseIndexed.hpp>

#include <script_interface/ScriptInterface.hpp>
#include <script_interface/auto_parameters/AutoParameters.hpp>
Expand Down Expand Up @@ -80,22 +80,21 @@ class EKReaction : public AutoParameters<EKReaction, LatticeIndices> {
return tau / std::pow(Utils::int_pow<3>(agrid), sum_alphas - 1.);
}

template <typename T>
std::shared_ptr<T> make_instance(VariantMap const &args) const {
template <typename F>
auto make_instance(VariantMap const &args, F &allocator) const {
auto lattice = get_value<std::shared_ptr<LatticeWalberla>>(args, "lattice");
auto reactant = get_value<std::vector<Variant>>(args, "reactants");
auto output =
std::vector<std::shared_ptr<::walberla::EKReactant>>(reactant.size());
auto reactants = get_value<std::vector<Variant>>(args, "reactants");
auto output = ::walberla::EKReactionBase::reactants_type(reactants.size());
auto get_instance = [](Variant const &v) {
return get_value<std::shared_ptr<EKReactant>>(v)->get_instance();
};
std::transform(reactant.begin(), reactant.end(), output.begin(),
std::transform(reactants.begin(), reactants.end(), output.begin(),
get_instance);

auto const coefficient =
get_value<double>(args, "coefficient") * get_conversion_coefficient();

return std::make_shared<T>(lattice->lattice(), output, coefficient);
return allocator(lattice->lattice(), output, coefficient);
}

std::shared_ptr<::walberla::EKReactionBase> m_ekreaction;
Expand All @@ -118,7 +117,7 @@ class EKBulkReaction : public EKReaction {

void do_construct(VariantMap const &args) override {
m_conv_coefficient = calculate_bulk_conversion_factor(args);
m_ekreaction = make_instance<::walberla::EKReactionImplBulk>(args);
m_ekreaction = make_instance(args, ::walberla::new_ek_reaction_bulk);
}
};

Expand All @@ -143,10 +142,9 @@ class EKIndexedReaction : public EKReaction {
void do_construct(VariantMap const &args) override {
auto const agrid = get_agrid(args);
m_conv_coefficient = calculate_bulk_conversion_factor(args) / agrid;
m_ekreaction = make_instance<::walberla::EKReactionImplIndexed>(args);
m_ekreaction_impl =
std::dynamic_pointer_cast<::walberla::EKReactionImplIndexed>(
get_instance());
make_instance(args, ::walberla::new_ek_reaction_indexed);
m_ekreaction = m_ekreaction_impl;
}

[[nodiscard]] Variant do_call_method(std::string const &method,
Expand All @@ -170,7 +168,7 @@ class EKIndexedReaction : public EKReaction {
}

private:
std::shared_ptr<::walberla::EKReactionImplIndexed> m_ekreaction_impl;
std::shared_ptr<::walberla::EKReactionBaseIndexed> m_ekreaction_impl;
};

} // namespace ScriptInterface::walberla
Expand Down
3 changes: 1 addition & 2 deletions src/script_interface/walberla/EKSpecies.cpp
Expand Up @@ -24,7 +24,6 @@
#include "EKWalberlaNodeState.hpp"
#include "WalberlaCheckpoint.hpp"

#include <walberla_bridge/LatticeWalberla.hpp>
#include <walberla_bridge/electrokinetics/ek_walberla_init.hpp>

#include <boost/mpi.hpp>
Expand Down Expand Up @@ -119,7 +118,7 @@ void EKSpecies::do_construct(VariantMap const &args) {
auto const ek_ext_efield = ext_efield * m_conv_ext_efield;
auto const ek_density = m_density = density * m_conv_density;
auto const ek_kT = kT * m_conv_energy;
m_instance = new_ek_walberla(
m_instance = ::walberla::new_ek_walberla(
m_lattice->lattice(), ek_diffusion, ek_kT,
get_value<double>(args, "valency"), ek_ext_efield, ek_density,
get_value<bool>(args, "advection"),
Expand Down
9 changes: 4 additions & 5 deletions src/walberla_bridge/CMakeLists.txt
Expand Up @@ -42,8 +42,8 @@ if(ESPRESSO_BUILD_WITH_CUDA AND WALBERLA_BUILD_WITH_CUDA)
PRIVATE ${WALBERLA_LIBS})
target_include_directories(espresso_walberla_cuda PUBLIC include)
target_include_directories(
espresso_walberla_cuda PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${WALBERLA_INCLUDE_DIRS} ${walberla_BINARY_DIR}/src)
espresso_walberla_cuda PRIVATE ${WALBERLA_INCLUDE_DIRS}
${walberla_BINARY_DIR}/src)
install(TARGETS espresso_walberla_cuda
LIBRARY DESTINATION ${ESPRESSO_INSTALL_PYTHON}/espressomd)
target_link_libraries(espresso_walberla PUBLIC espresso::walberla_cuda)
Expand All @@ -52,9 +52,8 @@ endif()
target_link_libraries(
espresso_walberla PUBLIC MPI::MPI_CXX espresso::utils
PRIVATE espresso::cpp_flags espresso::walberla::cpp_flags ${WALBERLA_LIBS})
target_include_directories(
espresso_walberla PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${WALBERLA_INCLUDE_DIRS} ${walberla_BINARY_DIR}/src)
target_include_directories(espresso_walberla PRIVATE ${WALBERLA_INCLUDE_DIRS}
${walberla_BINARY_DIR}/src)

add_subdirectory(src)

Expand Down
Expand Up @@ -20,11 +20,14 @@
#pragma once

#include <walberla_bridge/LatticeWalberla.hpp>

#include "PoissonSolver/PoissonSolver.hpp"
#include <walberla_bridge/electrokinetics/PoissonSolver/PoissonSolver.hpp>

#include <memory>

namespace walberla {

std::shared_ptr<walberla::PoissonSolver>
new_ek_poisson_fft(std::shared_ptr<LatticeWalberla> const &lattice,
double permittivity, bool single_precision);

} // namespace walberla
Expand Up @@ -20,11 +20,14 @@
#pragma once

#include <walberla_bridge/LatticeWalberla.hpp>

#include "PoissonSolver/PoissonSolver.hpp"
#include <walberla_bridge/electrokinetics/PoissonSolver/PoissonSolver.hpp>

#include <memory>

namespace walberla {

std::shared_ptr<walberla::PoissonSolver>
new_ek_poisson_none(std::shared_ptr<LatticeWalberla> const &lattice,
bool single_precision);

} // namespace walberla
Expand Up @@ -22,13 +22,29 @@
#include "EKinWalberlaBase.hpp"

#include <walberla_bridge/LatticeWalberla.hpp>
#include <walberla_bridge/electrokinetics/reactions/EKReactionBase.hpp>
#include <walberla_bridge/electrokinetics/reactions/EKReactionBaseIndexed.hpp>

#include <utils/Vector.hpp>

#include <memory>

namespace walberla {

std::shared_ptr<EKinWalberlaBase>
new_ek_walberla(std::shared_ptr<LatticeWalberla> const &lattice,
double diffusion, double kT, double valency,
Utils::Vector3d ext_efield, double density, bool advection,
bool friction_coupling, bool single_precision);

std::shared_ptr<EKReactionBase>
new_ek_reaction_bulk(std::shared_ptr<LatticeWalberla> const &lattice,
typename EKReactionBase::reactants_type const &reactants,
double coefficient);

std::shared_ptr<EKReactionBaseIndexed> new_ek_reaction_indexed(
std::shared_ptr<LatticeWalberla> const &lattice,
typename EKReactionBase::reactants_type const &reactants,
double coefficient);

} // namespace walberla
Expand Up @@ -29,18 +29,19 @@
namespace walberla {

class EKReactionBase {
private:
std::vector<std::shared_ptr<EKReactant>> m_reactants;
double m_coefficient;
public:
using reactants_type = std::vector<std::shared_ptr<EKReactant>>;

private:
std::shared_ptr<LatticeWalberla> m_lattice;
reactants_type m_reactants;
double m_coefficient;

public:
EKReactionBase(std::shared_ptr<LatticeWalberla> lattice,
std::vector<std::shared_ptr<EKReactant>> reactants,
double coefficient)
: m_reactants(std::move(reactants)), m_coefficient(coefficient),
m_lattice(std::move(lattice)) {}
reactants_type reactants, double coefficient)
: m_lattice(std::move(lattice)), m_reactants(std::move(reactants)),
m_coefficient(coefficient) {}

virtual ~EKReactionBase() = default;

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 The ESPResSo project
* Copyright (C) 2024 The ESPResSo project
*
* This file is part of ESPResSo.
*
Expand All @@ -17,26 +17,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "EKReactionImplBulk.hpp"
#pragma once

#include "generated_kernels/ReactionKernelBulk_all.h"
#include "EKReactionBase.hpp"

#include <blockforest/StructuredBlockForest.h>
#include <utils/Vector.hpp>

namespace walberla {
#include <optional>

void EKReactionImplBulk::perform_reaction() {
// TODO: if my understanding is correct:
// the kernels need to either run in the ghost layers and do the
// synchronization before or not run and do a synchronization afterwards.
// The better solution is probably the latter one. Not sure why it fails
// atm.
namespace walberla {

auto kernel = detail::ReactionKernelBulkSelector::get_kernel(
get_reactants(), get_coefficient());
class EKReactionBaseIndexed : public EKReactionBase {
public:
using EKReactionBase::EKReactionBase;
~EKReactionBaseIndexed() override = default;
virtual void set_node_is_boundary(Utils::Vector3i const &node,
bool is_boundary) = 0;
virtual std::optional<bool>
get_node_is_boundary(Utils::Vector3i const &node) = 0;
};

for (auto &block : *get_lattice()->get_blocks()) {
kernel(&block);
}
}
} // namespace walberla
16 changes: 9 additions & 7 deletions src/walberla_bridge/src/BoundaryHandling.hpp
Expand Up @@ -20,7 +20,8 @@
#pragma once

#include <walberla_bridge/BlockAndCell.hpp>
#include <walberla_bridge/utils/walberla_utils.hpp>

#include "utils/types_conversion.hpp"

#include <blockforest/StructuredBlockForest.h>
#include <domain_decomposition/BlockDataID.h>
Expand All @@ -38,12 +39,13 @@

namespace walberla {

/// Flag for domain cells, i.e. all cells
FlagUID const Domain_flag("domain");
/// Flag for boundary cells
FlagUID const Boundary_flag("boundary");

template <typename T, typename BoundaryClass> class BoundaryHandling {
private:
/** Flag for domain cells, i.e. all cells. */
FlagUID const Domain_flag{"domain"};
/** Flag for boundary cells. */
FlagUID const Boundary_flag{"boundary"};

/** Container for the map between cells and values. */
class DynamicValueCallback {
public:
Expand Down Expand Up @@ -172,7 +174,7 @@ template <typename T, typename BoundaryClass> class BoundaryHandling {
std::shared_ptr<BoundaryClass> m_boundary;
bool m_pending_changes;

/** Register flags and set all cells to @ref Domain_flag. */
/** Register flags and reset all cells. */
void flag_reset_kernel(IBlock *const block) {
auto flag_field = block->template getData<FlagField>(m_flag_field_id);
// register flags
Expand Down
5 changes: 5 additions & 0 deletions src/walberla_bridge/src/BoundaryPackInfo.hpp
Expand Up @@ -23,6 +23,7 @@
#include <core/mpi/RecvBuffer.h>
#include <core/mpi/SendBuffer.h>
#include <domain_decomposition/IBlock.h>
#include <field/FlagUID.h>
#include <field/communication/PackInfo.h>
#include <stencil/Directions.h>

Expand All @@ -38,6 +39,10 @@ template <typename GhostLayerField_T, typename Boundary_T>
class BoundaryPackInfo : public PackInfo<GhostLayerField_T> {
protected:
using PackInfo<GhostLayerField_T>::bdId_;
/** Flag for domain cells, i.e. all cells. */
FlagUID const Domain_flag{"domain"};
/** Flag for boundary cells. */
FlagUID const Boundary_flag{"boundary"};

public:
using PackInfo<GhostLayerField_T>::PackInfo;
Expand Down
3 changes: 2 additions & 1 deletion src/walberla_bridge/src/LatticeWalberla.cpp
Expand Up @@ -19,7 +19,8 @@

#include <walberla_bridge/BlockAndCell.hpp>
#include <walberla_bridge/LatticeWalberla.hpp>
#include <walberla_bridge/utils/walberla_utils.hpp>

#include "utils/types_conversion.hpp"

#include <blockforest/Initialization.h>
#include <blockforest/StructuredBlockForest.h>
Expand Down

0 comments on commit 220aa61

Please sign in to comment.