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-2023 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 }
59
60 void sanity_checks(
61 std::shared_ptr<walberla::PoissonSolver> const &new_ek_solver) const {
62 if (not m_ekcontainer.empty()) {
63 auto const &old_ek_species = m_ekcontainer.front();
64 if (not lattice_equal(new_ek_solver->get_lattice(),
65 old_ek_species->get_lattice())) {
66 throw std::runtime_error("Poisson solver lattice incompatible with "
67 "existing EKSpecies lattice");
68 }
69 }
70 }
71
72public:
73 EKContainer(double tau, std::shared_ptr<walberla::PoissonSolver> solver)
74 : m_tau{tau}, m_poisson_solver{std::move(solver)}, m_ekcontainer{} {}
75
76 bool contains(std::shared_ptr<EKSpecies> const &ek_species) const noexcept {
77 return std::ranges::find(m_ekcontainer, ek_species) != m_ekcontainer.end();
78 }
79
80 void add(std::shared_ptr<EKSpecies> const &ek_species) {
82 sanity_checks(ek_species);
83 if (!m_ekcontainer.empty()) {
84 if (ek_species->is_gpu() != m_is_gpu) {
85 throw std::runtime_error(
86 "All EK Species need to be on de same device.");
87 }
88 }
89 m_is_gpu = ek_species->is_gpu();
90 m_ekcontainer.emplace_back(ek_species);
91 }
92
93 void remove(std::shared_ptr<EKSpecies> const &ek_species) {
95 std::erase(m_ekcontainer, ek_species);
96 }
97
98 iterator begin() noexcept { return m_ekcontainer.begin(); }
99 iterator end() noexcept { return m_ekcontainer.end(); }
100 const_iterator begin() const noexcept { return m_ekcontainer.begin(); }
101 const_iterator end() const noexcept { return m_ekcontainer.end(); }
102 [[nodiscard]] bool empty() const noexcept { return m_ekcontainer.empty(); }
103
104 void
105 set_poisson_solver(std::shared_ptr<walberla::PoissonSolver> const &solver) {
106 assert(solver != nullptr);
107 sanity_checks(solver);
108 m_poisson_solver = solver;
109 }
110
111 [[nodiscard]] bool is_gpu() const noexcept { return m_is_gpu; }
112
113 [[nodiscard]] double get_tau() const noexcept { return m_tau; }
114
115 void set_tau(double tau) noexcept { m_tau = tau; }
116
117 void reset_charge() const { m_poisson_solver->reset_charge_field(); }
118
119 void add_charge(std::size_t const id, double valency) const {
120 m_poisson_solver->add_charge_to_field(id, valency);
121 }
122
123 void solve_poisson() const { m_poisson_solver->solve(); }
124
125 [[nodiscard]] std::size_t get_potential_field_id() const {
126 return m_poisson_solver->get_potential_field_id();
127 }
128
130 return m_poisson_solver->get_lattice();
131 }
132};
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.