chombo-discharge
CD_GenericParticleImplem.H
Go to the documentation of this file.
1 /* chombo-discharge
2  * Copyright © 2022 SINTEF Energy Research.
3  * Please refer to Copyright.txt and LICENSE in the chombo-discharge root directory.
4  */
5 
12 #ifndef CD_GenericParticleImplem_H
13 #define CD_GenericParticleImplem_H
14 
15 // Our includes
16 #include <CD_GenericParticle.H>
17 #include <CD_NamespaceHeader.H>
18 
19 template <size_t M, size_t N>
21 {
22  m_position = RealVect::Zero;
23 
24  for (auto& s : m_scalars) {
25  s = 0.0;
26  }
27 
28  for (auto& v : m_vectors) {
29  v = RealVect::Zero;
30  }
31 }
32 
33 template <size_t M, size_t N>
35 {
36  this->m_position = a_other.m_position;
37  this->m_scalars = a_other.m_scalars;
38  this->m_vectors = a_other.m_vectors;
39 }
40 
41 template <size_t M, size_t N>
43 {}
44 
45 template <size_t M, size_t N>
46 inline RealVect&
48 {
49  return m_position;
50 }
51 
52 template <size_t M, size_t N>
53 inline const RealVect&
55 {
56  return m_position;
57 }
58 
59 template <size_t M, size_t N>
60 inline const std::array<Real, M>&
62 {
63  return m_scalars;
64 }
65 
66 template <size_t M, size_t N>
67 inline std::array<Real, M>&
69 {
70  return m_scalars;
71 }
72 
73 template <size_t M, size_t N>
74 inline const std::array<RealVect, N>&
76 {
77  return m_vectors;
78 }
79 
80 template <size_t M, size_t N>
81 inline std::array<RealVect, N>&
83 {
84  return m_vectors;
85 }
86 
87 template <size_t M, size_t N>
88 template <size_t K>
89 inline Real&
91 {
92  return std::get<K>(m_scalars);
93 }
94 
95 template <size_t M, size_t N>
96 template <size_t K>
97 inline const Real&
99 {
100  return std::get<K>(m_scalars);
101 }
102 
103 template <size_t M, size_t N>
104 template <size_t K>
105 inline RealVect&
107 {
108  return std::get<K>(m_vectors);
109 }
110 
111 template <size_t M, size_t N>
112 template <size_t K>
113 inline const RealVect&
115 {
116  return std::get<K>(m_vectors);
117 }
118 
119 template <size_t M, size_t N>
120 inline bool
122 {
123  return (this->m_position == a_p.m_position && this->m_scalars == a_p.m_scalars && this->m_vectors == a_p.m_vectors);
124 }
125 
126 template <size_t M, size_t N>
127 inline bool
129 {
130  return !(*this == a_p);
131 }
132 
133 template <size_t M, size_t N>
134 inline bool
135 GenericParticle<M, N>::operator<(const GenericParticle<M, N>& a_other) const noexcept
136 {
137  const RealVect& x = m_position;
138  const RealVect& y = a_other.m_position;
139 
140  if (x[0] < y[0]) {
141  return true;
142  }
143 #if CH_SPACEDIM > 1
144  if (x[0] > y[0]) {
145  return false;
146  }
147  if (x[1] < y[1]) {
148  return true;
149  }
150 #endif
151 #if CH_SPACEDIM > 2
152  if (x[1] > y[1]) {
153  return false;
154  }
155  if (x[2] < y[2]) {
156  return true;
157  }
158 #endif
159 
160  return false;
161 }
162 
163 template <size_t M, size_t N>
164 inline int
166 {
167  // Size is the position plus M Reals and N RealVects
168  return SpaceDim * sizeof(Real) + M * sizeof(Real) + N * SpaceDim * sizeof(Real);
169 }
170 
171 template <size_t M, size_t N>
172 inline void
174 {
175  Real* buffer = (Real*)buf;
176 
177  // Linearize m_position onto buffer.
178  *buffer++ = m_position[0];
179  *buffer++ = m_position[1];
180 #if CH_SPACEDIM == 3
181  *buffer++ = m_position[2];
182 #endif
183 
184  // Linearize m_scalars onto the buffer
185  for (size_t i = 0; i < M; i++) {
186  *buffer++ = m_scalars[i];
187  }
188 
189  // Linearize vectors onto the buffer
190  for (size_t i = 0; i < N; i++) {
191  const RealVect& v = m_vectors[i];
192 
193  *buffer++ = v[0];
194  *buffer++ = v[1];
195 #if CH_SPACEDIM == 3
196  *buffer++ = v[2];
197 #endif
198  }
199 }
200 
201 template <size_t M, size_t N>
202 inline void
204 {
205  Real* buffer = (Real*)buf;
206 
207  m_position[0] = *buffer++;
208  m_position[1] = *buffer++;
209 #if CH_SPACEDIM == 3
210  m_position[2] = *buffer++;
211 #endif
212 
213  // Linearize buffer onto scalars
214  for (size_t i = 0; i < M; i++) {
215  m_scalars[i] = *buffer++;
216  }
217 
218  // Linearize buffer onto vectors
219  for (size_t i = 0; i < N; i++) {
220  RealVect& v = m_vectors[i];
221 
222  v[0] = *buffer++;
223  v[1] = *buffer++;
224 #if CH_SPACEDIM == 3
225  v[2] = *buffer++;
226 #endif
227  }
228 }
229 
230 template <size_t M, size_t N>
231 inline std::ostream&
232 operator<<(std::ostream& ostr, const GenericParticle<M, N>& p)
233 {
234  ostr << "GenericParticle : \n";
235 
236  // Print position.
237  ostr << "\tPosition = " << p.position() << "\n";
238 
239  return ostr;
240 }
241 
242 #include <CD_NamespaceFooter.H>
243 
244 #endif
std::ostream & operator<<(std::ostream &ostr, const GenericParticle< M, N > &p)
Particle printing function.
Definition: CD_GenericParticleImplem.H:232
Declaration of a generic particle class.
A generic particle class, holding the position and a specified number of real and vector values.
Definition: CD_GenericParticle.H:33
virtual void linearOut(void *a_buffer) const
Write a linear binary representation of the internal data. Assumes that sufficient memory for the buf...
Definition: CD_GenericParticleImplem.H:173
virtual ~GenericParticle()
Destructor (deallocates runtime memory storage)
Definition: CD_GenericParticleImplem.H:42
virtual void linearIn(void *a_buffer)
Read a linear binary representation of the internal data. Assumes that the buffer has the correct dat...
Definition: CD_GenericParticleImplem.H:203
RealVect & position()
Get the particle position.
Definition: CD_GenericParticleImplem.H:47
Real & real()
Get one of the scalars.
Definition: CD_GenericParticleImplem.H:90
const std::array< Real, M > & getReals() const noexcept
Get the M scalars.
Definition: CD_GenericParticleImplem.H:61
virtual int size() const
Returns the size, in number of bytes, of a flat representation of the data in this object.
Definition: CD_GenericParticleImplem.H:165
RealVect & vect()
Get one of the RealVects.
Definition: CD_GenericParticleImplem.H:106
bool operator==(const GenericParticle< M, N > &a_other) const
Comparison operator with other particle.
Definition: CD_GenericParticleImplem.H:121
bool operator!=(const GenericParticle< M, N > &a_other) const
Comparison operator with other particle.
Definition: CD_GenericParticleImplem.H:128
bool operator<(const GenericParticle< M, N > &a_other) const noexcept
Particle comparison operator. Returns lexicographical ordering.
Definition: CD_GenericParticleImplem.H:135
RealVect m_position
Particle position.
Definition: CD_GenericParticle.H:180
GenericParticle()
Default constructor – initializes everything to zero.
Definition: CD_GenericParticleImplem.H:20
std::array< Real, M > m_scalars
Scalar storage array.
Definition: CD_GenericParticle.H:185
std::array< RealVect, N > m_vectors
vector storage array
Definition: CD_GenericParticle.H:190
const std::array< RealVect, N > & getVects() const noexcept
Get the N vectors.
Definition: CD_GenericParticleImplem.H:75