ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
walberla_bridge/include/walberla_bridge/electrokinetics/EKContainer.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
24
25#include <algorithm>
26#include <cassert>
27#include <cstddef>
28#include <memory>
29#include <stdexcept>
30#include <vector>
31
32template <class EKSpecies> class EKContainer {
33 using container_type = std::vector<std::shared_ptr<EKSpecies>>;
34
35public:
36 using value_type = container_type::value_type;
37 using iterator = container_type::iterator;
38 using const_iterator = container_type::const_iterator;
39
40private:
41 double m_tau;
42 std::shared_ptr<walberla::PoissonSolver> m_poisson_solver;
43 container_type m_ekcontainer;
44 bool m_is_gpu;
45
46 bool lattice_equal(LatticeWalberla const &lhs,
47 LatticeWalberla const &rhs) const {
48 return (lhs.get_ghost_layers() == rhs.get_ghost_layers()) and
49 (lhs.get_grid_dimensions() == rhs.get_grid_dimensions());
50 }
51
52 void sanity_checks(std::shared_ptr<EKSpecies> const &new_ek_species) const {
53 if (not lattice_equal(new_ek_species->get_lattice(),
54 m_poisson_solver->get_lattice())) {
55 throw std::runtime_error("EKSpecies lattice incompatible with existing "
56 "Poisson solver lattice");
57 }
58 if (new_ek_species->is_gpu() != m_poisson_solver->is_gpu()) {
59 throw std::runtime_error("The EK species and the EK solver need to all "
60 "be on either the CPU or GPU");
61 }
62 }
63
64 void sanity_checks(
65 std::shared_ptr<walberla::PoissonSolver> const &new_ek_solver) const {
66 if (not m_ekcontainer.empty()) {
67 auto const &old_ek_species = m_ekcontainer.front();
68 if (not lattice_equal(new_ek_solver->get_lattice(),
69 old_ek_species->get_lattice())) {
70 throw std::runtime_error("Poisson solver lattice incompatible with "
71 "existing EKSpecies lattice");
72 }
73 if (new_ek_solver->is_gpu() != old_ek_species->is_gpu()) {
74 throw std::runtime_error("The EK species and the EK solver need to all "
75 "be on either the CPU or GPU");
76 }
77 }
78 }
79
80public:
81 EKContainer(double tau, std::shared_ptr<walberla::PoissonSolver> solver)
82 : m_tau{tau}, m_poisson_solver{std::move(solver)}, m_ekcontainer{} {}
83
84 bool contains(std::shared_ptr<EKSpecies> const &ek_species) const noexcept {
85 return std::ranges::find(m_ekcontainer, ek_species) != m_ekcontainer.end();
86 }
87
88 void add(std::shared_ptr<EKSpecies> const &ek_species) {
90 sanity_checks(ek_species);
91 m_ekcontainer.emplace_back(ek_species);
92 }
93
94 void remove(std::shared_ptr<EKSpecies> const &ek_species) {
96 std::erase(m_ekcontainer, ek_species);
97 }
98
99 iterator begin() noexcept { return m_ekcontainer.begin(); }
100 iterator end() noexcept { return m_ekcontainer.end(); }
101 const_iterator begin() const noexcept { return m_ekcontainer.begin(); }
102 const_iterator end() const noexcept { return m_ekcontainer.end(); }
103 [[nodiscard]] bool empty() const noexcept { return m_ekcontainer.empty(); }
104
105 void
106 set_poisson_solver(std::shared_ptr<walberla::PoissonSolver> const &solver) {
107 assert(solver != nullptr);
108 sanity_checks(solver);
109 m_poisson_solver = solver;
110 }
111
113 return m_poisson_solver->is_gpu();
114 }
115
116 [[nodiscard]] double get_tau() const noexcept { return m_tau; }
117
118 void set_tau(double tau) noexcept { m_tau = tau; }
119
120 void reset_charge() const { m_poisson_solver->reset_charge_field(); }
121
122 void add_charge(std::size_t const id, double valency) const {
123 m_poisson_solver->add_charge_to_field(id, valency);
124 }
125
126 void solve_poisson() const { m_poisson_solver->solve(); }
127
128 [[nodiscard]] std::size_t get_potential_field_id() const {
129 return m_poisson_solver->get_potential_field_id();
130 }
131
133 return m_poisson_solver->get_lattice();
134 }
135};
void set_poisson_solver(std::shared_ptr< walberla::PoissonSolver > const &solver)
bool contains(std::shared_ptr< EKSpecies > const &ek_species) const noexcept
EKContainer(double tau, std::shared_ptr< walberla::PoissonSolver > solver)
void add_charge(std::size_t const id, double valency) const
void remove(std::shared_ptr< EKSpecies > const &ek_species)
void add(std::shared_ptr< EKSpecies > const &ek_species)
Class that runs and controls the BlockForest in waLBerla.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
STL namespace.