OPALX (Object Oriented Parallel Accelerator Library for Exascale) MINIorX
OPALX
BCHandler.hpp
Go to the documentation of this file.
1#ifndef OPALX_SRC_PARTBUNCH_BCHANDLER_HPP
2#define OPALX_SRC_PARTBUNCH_BCHANDLER_HPP
3
4#include <array>
5#include <ostream>
6
8
25template <unsigned Dim>
26class BCHandler {
27public:
37 enum BCType {
40 };
41
54 static BCType strToBCType(const std::string& str) {
55 if (str == "OPEN") {
56 return OPEN;
57 } else if (str == "PERIODIC") {
58 return PERIODIC;
59 } else {
60 throw OpalException("BCHandler::strToBCType",
61 "Unknown boundary condition type: " + str);
62 }
63 }
64
68 BCHandler(const BCHandler& other) = default;
69
81 template <typename... Bcs>
82 BCHandler(Bcs... bcs)
83 : bcs_m{{static_cast<BCType>(bcs)...}} {
84 if (sizeof...(bcs) != Dim) {
85 throw OpalException("BCHandler::BCHandler",
86 "Number of passed BCs does not match dimensionality!");
87 }
88 }
89
96 bool isAll(BCType bc_type) const {
97 for (unsigned int d = 0; d < Dim; ++d) {
98 if (bcs_m[d] != bc_type) return false;
99 }
100 return true;
101 }
102
112 bool isAllEqual() const {
113 bool base_case = bcs_m[0];
114 for (unsigned int d = 1; d < Dim; ++d) {
115 if (bcs_m[d] != base_case) return false;
116 }
117 return true;
118 }
119
126 friend std::ostream& operator<<(std::ostream& os, const BCHandler& h) {
127 os << "* BCHandler<" << Dim << ">: [";
128 for (unsigned int d = 0; d < Dim; ++d) {
129 switch (h.bcs_m[d]) {
130 case OPEN: os << "OPEN"; break;
131 case PERIODIC: os << "PERIODIC"; break;
132 default: os << "UNKNOWN"; break;
133 }
134 if (d + 1 < Dim) os << ", ";
135 }
136 os << "]";
137 return os;
138 }
139
140
141private:
143 std::array<BCType, Dim> bcs_m;
144};
145
146#endif // OPALX_SRC_PARTBUNCH_BCHANDLER_HPP
@ PERIODIC
Definition: ParticleBC.h:12
Handler for boundary conditions per spatial dimension.
Definition: BCHandler.hpp:26
bool isAllEqual() const
Check whether all stored BCs are equal (all the same value).
Definition: BCHandler.hpp:112
friend std::ostream & operator<<(std::ostream &os, const BCHandler &h)
Stream output helper for debugging and logging.
Definition: BCHandler.hpp:126
static BCType strToBCType(const std::string &str)
Convert a textual boundary-condition name to BCType enum.
Definition: BCHandler.hpp:54
BCHandler(const BCHandler &other)=default
Defaulted copy constructor.
BCHandler(Bcs... bcs)
Variadic constructor accepting exactly Dim BC values.
Definition: BCHandler.hpp:82
std::array< BCType, Dim > bcs_m
Internal storage of boundary conditions, one per dimension.
Definition: BCHandler.hpp:143
bool isAll(BCType bc_type) const
Return true if every stored BC equals bc_type.
Definition: BCHandler.hpp:96
BCType
Supported boundary condition types.
Definition: BCHandler.hpp:37
The base class for all OPAL exceptions.
Definition: OpalException.h:28
constexpr unsigned Dim