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 = typename container_type::value_type;
37 using iterator = typename container_type::iterator;
38 using const_iterator = typename 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
45 bool lattice_equal(LatticeWalberla const &lhs,
46 LatticeWalberla const &rhs) const {
47 return (lhs.get_ghost_layers() == rhs.get_ghost_layers()) and
49 }
50
51 void sanity_checks(std::shared_ptr<EKSpecies> const &new_ek_species) const {
52 if (not lattice_equal(new_ek_species->get_lattice(),
53 m_poisson_solver->get_lattice())) {
54 throw std::runtime_error("EKSpecies lattice incompatible with existing "
55 "Poisson solver lattice");
56 }
57 }
58
59 void sanity_checks(
60 std::shared_ptr<walberla::PoissonSolver> const &new_ek_solver) const {
61 if (not m_ekcontainer.empty()) {
62 auto const &old_ek_species = m_ekcontainer.front();
63 if (not lattice_equal(new_ek_solver->get_lattice(),
64 old_ek_species->get_lattice())) {
65 throw std::runtime_error("Poisson solver lattice incompatible with "
66 "existing EKSpecies lattice");
67 }
68 }
69 }
70
71public:
72 EKContainer(double tau, std::shared_ptr<walberla::PoissonSolver> solver)
73 : m_tau{tau}, m_poisson_solver{std::move(solver)}, m_ekcontainer{} {}
74
75 bool contains(std::shared_ptr<EKSpecies> const &ek_species) const noexcept {
76 return std::ranges::find(m_ekcontainer, ek_species) != m_ekcontainer.end();
77 }
78
79 void add(std::shared_ptr<EKSpecies> const &ek_species) {
80 assert(not contains(ek_species));
81 sanity_checks(ek_species);
82 m_ekcontainer.emplace_back(ek_species);
83 }
84
85 void remove(std::shared_ptr<EKSpecies> const &ek_species) {
86 assert(contains(ek_species));
87 std::erase(m_ekcontainer, ek_species);
88 }
89
90 iterator begin() noexcept { return m_ekcontainer.begin(); }
91 iterator end() noexcept { return m_ekcontainer.end(); }
92 const_iterator begin() const noexcept { return m_ekcontainer.begin(); }
93 const_iterator end() const noexcept { return m_ekcontainer.end(); }
94 [[nodiscard]] bool empty() const noexcept { return m_ekcontainer.empty(); }
95
96 void
97 set_poisson_solver(std::shared_ptr<walberla::PoissonSolver> const &solver) {
98 assert(solver != nullptr);
99 sanity_checks(solver);
100 m_poisson_solver = solver;
101 }
102
103 [[nodiscard]] double get_tau() const noexcept { return m_tau; }
104
105 void set_tau(double tau) noexcept { m_tau = tau; }
106
107 void reset_charge() const { m_poisson_solver->reset_charge_field(); }
108
109 void add_charge(std::size_t const id, double valency,
110 bool is_double_precision) const {
111 m_poisson_solver->add_charge_to_field(id, valency, is_double_precision);
112 }
113
114 void solve_poisson() const { m_poisson_solver->solve(); }
115
116 [[nodiscard]] std::size_t get_potential_field_id() const {
117 return m_poisson_solver->get_potential_field_id();
118 }
119
120 LatticeWalberla const &get_lattice() const noexcept {
121 return m_poisson_solver->get_lattice();
122 }
123};
void set_poisson_solver(std::shared_ptr< walberla::PoissonSolver > const &solver)
bool contains(std::shared_ptr< EKSpecies > const &ek_species) const noexcept
void add_charge(std::size_t const id, double valency, bool is_double_precision) const
EKContainer(double tau, std::shared_ptr< walberla::PoissonSolver > solver)
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.