AutoPas  3.0.0
Loading...
Searching...
No Matches
SPHParticle.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstring>
10#include <vector>
11
13
14namespace sphLib {
19 public:
25 : autopas::ParticleBaseFP64(),
26 _density(0.),
27 _pressure(0.),
28 _mass(0.),
29 _smth(0.),
30 _snds(0.),
31 // temporaries / helpers
32 _v_sig_max(0.),
33 _acc{0., 0., 0.},
34 _energy_dot(0.),
35 _energy(0.),
36 _dt(0.),
37 _vel_half{0., 0., 0.},
38 _eng_half(0.) {}
45 SPHParticle(const std::array<double, 3> &r, const std::array<double, 3> &v, unsigned long id)
46 : autopas::ParticleBaseFP64(r, v, id),
47 _density(0.),
48 _pressure(0.),
49 _mass(0.),
50 _smth(0.),
51 _snds(0.),
52 // temporaries / helpers
53 _v_sig_max(0.),
54 _acc{0., 0., 0.},
55 _energy_dot(0.),
56 _energy(0.),
57 _dt(0.),
58 _vel_half{0., 0., 0.},
59 _eng_half(0.) {}
60
70 SPHParticle(const std::array<double, 3> &r, const std::array<double, 3> &v, unsigned long id, double mass,
71 double smth, double snds)
72 : autopas::ParticleBaseFP64(r, v, id),
73 _density(0.),
74 _pressure(0.),
75 _mass(mass),
76 _smth(smth),
77 _snds(snds),
78 // temporaries / helpers
79 _v_sig_max(0.),
80 _acc{0., 0., 0.},
81 _energy_dot(0.),
82 _energy(0.),
83 _dt(0.),
84 _vel_half{0., 0., 0.},
85 _eng_half(0.) {}
86
90 ~SPHParticle() override = default;
91
96 double getDensity() const { return _density; }
97
102 void addDensity(double density) { _density += density; }
103
108 void setDensity(double density) { _density = density; }
109
114 double getPressure() const { return _pressure; }
115
120 void calcPressure();
121
126 void setPressure(double pressure) { _pressure = pressure; }
127
132 double getMass() const { return _mass; }
133
138 void setMass(double mass) { _mass = mass; }
139
144 double getSmoothingLength() const { return _smth; }
145
150 void setSmoothingLength(double smth) { _smth = smth; }
151
156 double getSoundSpeed() const { return _snds; }
157
162 void setSoundSpeed(double snds) { _snds = snds; }
163
168 double getVSigMax() const { return _v_sig_max; }
169
175 void checkAndSetVSigMax(double v_sig) { _v_sig_max = std::max(v_sig, _v_sig_max); }
176
181 void setVSigMax(double v_sig_max) { _v_sig_max = v_sig_max; }
182
187 const std::array<double, 3> &getAcceleration() const { return _acc; }
188
194 void addAcceleration(const std::array<double, 3> &acc);
195
201 void subAcceleration(const std::array<double, 3> &acc);
202
207 void setAcceleration(const std::array<double, 3> &acc) { _acc = acc; }
208
213 double getEngDot() const { return _energy_dot; }
214
220 void addEngDot(double eng_dot) { _energy_dot += eng_dot; }
221
226 void setEngDot(double eng_dot) { _energy_dot = eng_dot; }
227
232 double getEnergy() const { return _energy; }
233
238 void setEnergy(double energy) { _energy = energy; }
239
244 void addEnergy(double energy) { _energy += energy; }
245
250 double getDt() const { return _dt; }
251
256 void setDt(double dt) { _dt = dt; }
257
262 void calcDt() {
263 const double C_CFL = 0.3;
264 _dt = C_CFL * 2.0 * _smth / _v_sig_max;
265 }
266
271 const std::array<double, 3> &getVel_half() const { return _vel_half; }
272
277 void setVel_half(const std::array<double, 3> &vel_half) { SPHParticle::_vel_half = vel_half; }
278
283 double getEng_half() const { return _eng_half; }
284
289 void setEng_half(double eng_half) { SPHParticle::_eng_half = eng_half; }
290
295 std::vector<double> serialize() const {
296 std::vector<double> stream;
297 for (int i = 0; i < 3; i++) {
298 stream.push_back(this->getR()[i]);
299 }
300 for (int i = 0; i < 3; i++) {
301 stream.push_back(this->getV()[i]);
302 }
303
304 for (int i = 0; i < 3; i++) {
305 // stream.push_back(this->getF()[i]); // not actually needed
306 }
307 auto id = this->getID();
308 double id_dbl;
309 memcpy(&id_dbl, &id, sizeof(double));
310 static_assert(sizeof(id) == sizeof(double), "sizes should be the same, otherwise the above will not work");
311
312 stream.push_back(id_dbl);
313 stream.push_back(_density);
314 stream.push_back(_pressure);
315 stream.push_back(_mass);
316 stream.push_back(_smth);
317 stream.push_back(_snds);
318
319 for (int i = 0; i < 3; i++) {
320 stream.push_back(this->getAcceleration()[i]);
321 }
322 stream.push_back(_energy_dot);
323 stream.push_back(_energy);
324 // stream.push_back(_dt); // not needed
325 for (int i = 0; i < 3; i++) {
326 stream.push_back(this->getVel_half()[i]);
327 }
328 stream.push_back(_eng_half);
329 return stream;
330 }
331
339 static SPHParticle deserialize(const double *stream, size_t &index) {
340 std::array<double, 3> r = {stream[index], stream[index + 1], stream[index + 2]};
341 index += 3;
342 std::array<double, 3> v = {stream[index], stream[index + 1], stream[index + 2]};
343 index += 3;
344 // std::array<double,3> F = {stream[index], stream[index+1],
345 // stream[index+2]}; // not needed
346 // index+=3 // not needed
347 double id_dbl = stream[index++];
348 unsigned long id;
349 memcpy(&id, &id_dbl, sizeof(double));
350 static_assert(sizeof(id) == sizeof(double), "sizes should be the same, otherwise the above will not work");
351
352 double density = stream[index++];
353 double pressure = stream[index++];
354
355 double mass = stream[index++];
356 double smth = stream[index++];
357 double snds = stream[index++];
358
359 std::array<double, 3> ac = {stream[index], stream[index + 1], stream[index + 2]};
360 index += 3;
361 double energy_dot = stream[index++];
362 double energy = stream[index++];
363 // double dt = stream[index++]; // not needed
364 std::array<double, 3> vel_half = {stream[index], stream[index + 1], stream[index + 2]};
365 index += 3;
366 double eng_half = stream[index++];
367
368 SPHParticle p = SPHParticle(r, v, id, mass, smth, snds);
369 p.setDensity(density);
370 p.setPressure(pressure);
371 p.setAcceleration(ac);
372 p.setEngDot(energy_dot);
373 p.setEnergy(energy);
374 p.setVel_half(vel_half);
375 p.setEng_half(eng_half);
376 return p;
377 }
378
382 enum AttributeNames : int {
383 ptr,
384 mass,
385 posX,
386 posY,
387 posZ,
388 smth,
389 density,
390 velX,
391 velY,
392 velZ,
393 soundSpeed,
394 pressure,
395 vsigmax,
396 accX,
397 accY,
398 accZ,
399 engDot,
400 ownershipState
401 };
402
407 double, // mass
408 double, // posX
409 double, // posY
410 double, // posZ
411 double, // smth
412 double, // density
413 double, // velX
414 double, // velY
415 double, // velZ
416 double, // soundSpeed
417 double, // pressure
418 double, // vsigmax
419 double, // accX
420 double, // accY
421 double, // accZ
422 double, // engDot
424
430 template <AttributeNames attribute, std::enable_if_t<attribute == AttributeNames::ptr, bool> = true>
431 constexpr typename std::tuple_element<attribute, SoAArraysType>::type::value_type get() {
432 return this;
433 }
434
440 template <AttributeNames attribute, std::enable_if_t<attribute != AttributeNames::ptr, bool> = true>
441 constexpr typename std::tuple_element<attribute, SoAArraysType>::type::value_type get() const {
442 if constexpr (attribute == AttributeNames::mass) {
443 return getMass();
444 } else if constexpr (attribute == AttributeNames::posX) {
445 return getR()[0];
446 } else if constexpr (attribute == AttributeNames::posY) {
447 return getR()[1];
448 } else if constexpr (attribute == AttributeNames::posZ) {
449 return getR()[2];
450 } else if constexpr (attribute == AttributeNames::smth) {
451 return getSmoothingLength();
452 } else if constexpr (attribute == AttributeNames::density) {
453 return getDensity();
454 } else if constexpr (attribute == AttributeNames::velX) {
455 return getV()[0];
456 } else if constexpr (attribute == AttributeNames::velY) {
457 return getV()[1];
458 } else if constexpr (attribute == AttributeNames::velZ) {
459 return getV()[2];
460 } else if constexpr (attribute == AttributeNames::soundSpeed) {
461 return getSoundSpeed();
462 } else if constexpr (attribute == AttributeNames::pressure) {
463 return getPressure();
464 } else if constexpr (attribute == AttributeNames::vsigmax) {
465 return getVSigMax();
466 } else if constexpr (attribute == AttributeNames::accX) {
467 return getAcceleration()[0];
468 } else if constexpr (attribute == AttributeNames::accY) {
469 return getAcceleration()[1];
470 } else if constexpr (attribute == AttributeNames::accZ) {
471 return getAcceleration()[2];
472 } else if constexpr (attribute == AttributeNames::engDot) {
473 return getEngDot();
474 } else if constexpr (attribute == AttributeNames::ownershipState) {
475 return this->_ownershipState;
476 } else {
477 autopas::utils::ExceptionHandler::exception("SPHParticle::get: unknown attribute");
478 }
479 }
480
486 template <AttributeNames attribute>
487 constexpr void set(typename std::tuple_element<attribute, SoAArraysType>::type::value_type value) {
488 if constexpr (attribute == AttributeNames::mass) {
489 setMass(value);
490 } else if constexpr (attribute == AttributeNames::posX) {
491 _r[0] = value;
492 } else if constexpr (attribute == AttributeNames::posY) {
493 _r[1] = value;
494 } else if constexpr (attribute == AttributeNames::posZ) {
495 _r[2] = value;
496 } else if constexpr (attribute == AttributeNames::smth) {
497 setSmoothingLength(value);
498 } else if constexpr (attribute == AttributeNames::density) {
499 setDensity(value);
500 } else if constexpr (attribute == AttributeNames::velX) {
501 _v[0] = value;
502 } else if constexpr (attribute == AttributeNames::velY) {
503 _v[1] = value;
504 } else if constexpr (attribute == AttributeNames::velZ) {
505 _v[2] = value;
506 } else if constexpr (attribute == AttributeNames::soundSpeed) {
507 setSoundSpeed(value);
508 } else if constexpr (attribute == AttributeNames::pressure) {
509 setPressure(value);
510 } else if constexpr (attribute == AttributeNames::vsigmax) {
511 setVSigMax(value);
512 } else if constexpr (attribute == AttributeNames::accX) {
513 _acc[0] = value;
514 } else if constexpr (attribute == AttributeNames::accY) {
515 _acc[1] = value;
516 } else if constexpr (attribute == AttributeNames::accZ) {
517 _acc[2] = value;
518 } else if constexpr (attribute == AttributeNames::engDot) {
519 setEngDot(value);
520 } else if constexpr (attribute == AttributeNames::ownershipState) {
521 _ownershipState = value;
522 } else {
523 autopas::utils::ExceptionHandler::exception("SPHParticle::set: unknown attribute");
524 }
525 }
526
527 private:
528 double _density; // density
529 double _pressure; // pressure
530 double _mass; // mass
531 double _smth; // smoothing length
532 double _snds; // speed of sound
533
534 // temporaries / helpers
535 double _v_sig_max;
536
537 // integrator
538 std::array<double, 3> _acc; // acceleration
539 double _energy_dot; // time derivative of the energy
540 double _energy; // energy
541 double _dt; // local timestep allowed by this particle
542
543 // integrator
544 std::array<double, 3> _vel_half; // velocity at half time-step
545 double _eng_half; // energy at half time-step
546};
547} // namespace sphLib
Minimal definition of a basic particle.
Definition: ParticleBase.h:33
std::array< floatType, 3 > _r
Particle position as 3D coordinates.
Definition: ParticleBase.h:78
OwnershipState _ownershipState
Defines the state of the ownership of the particle.
Definition: ParticleBase.h:105
idType getID() const
Get the id of the particle.
Definition: ParticleBase.h:165
const std::array< double, 3 > & getV() const
Get the velocity of the particle.
Definition: ParticleBase.h:265
std::array< floatType, 3 > _v
Particle velocity as 3D vector.
Definition: ParticleBase.h:90
const std::array< double, 3 > & getR() const
Get the position of the particle.
Definition: ParticleBase.h:177
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
Basic SPHParticle class.
Definition: SPHParticle.h:18
const std::array< double, 3 > & getVel_half() const
Getter for velocity at half-time step (leapfrog)
Definition: SPHParticle.h:271
double getEngDot() const
Getter for the time derivative of the energy of the particle.
Definition: SPHParticle.h:213
void setSoundSpeed(double snds)
Setter for the speed of sound of the particle.
Definition: SPHParticle.h:162
SPHParticle(const std::array< double, 3 > &r, const std::array< double, 3 > &v, unsigned long id)
Constructor of the SPHParticle class.
Definition: SPHParticle.h:45
void addDensity(double density)
Adds the given density to the current density.
Definition: SPHParticle.h:102
void addEnergy(double energy)
Adds the given energy to the energy of the particle.
Definition: SPHParticle.h:244
AttributeNames
Attribute names for the soa arrays.
Definition: SPHParticle.h:382
constexpr std::tuple_element< attribute, SoAArraysType >::type::value_type get() const
Getter, which allows access to an attribute using the corresponding attribute name (defined in Attrib...
Definition: SPHParticle.h:441
void addAcceleration(const std::array< double, 3 > &acc)
Adds the given acceleration on the local acceleration.
Definition: SPHParticle.cpp:15
void setEngDot(double eng_dot)
Setter for the time derivative of the energy.
Definition: SPHParticle.h:226
autopas::utils::SoAType< SPHParticle *, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, autopas::OwnershipState >::Type SoAArraysType
SoA arrays type, cf.
Definition: SPHParticle.h:423
void setDt(double dt)
Set the maximally allowed time step for this particle.
Definition: SPHParticle.h:256
double getSmoothingLength() const
Getter for the smoothing length of the particle.
Definition: SPHParticle.h:144
void setDensity(double density)
Setter for Density.
Definition: SPHParticle.h:108
void setEng_half(double eng_half)
Setter for energy at half-time step (leapfrog)
Definition: SPHParticle.h:289
void setPressure(double pressure)
Setter for the pressure.
Definition: SPHParticle.h:126
void calcPressure()
Calculates the pressure within the particle from the energy and density of the particle and updates t...
Definition: SPHParticle.cpp:25
void setEnergy(double energy)
Setter for the energy of the particle.
Definition: SPHParticle.h:238
double getDensity() const
Getter for the Density.
Definition: SPHParticle.h:96
double getDt() const
Getter for the maximally allowed time step for this particle.
Definition: SPHParticle.h:250
double getPressure() const
Getter for Pressure.
Definition: SPHParticle.h:114
void setVSigMax(double v_sig_max)
Setter for the maximally allowed signal velocity.
Definition: SPHParticle.h:181
~SPHParticle() override=default
Destructor of the SPHParticle.
std::vector< double > serialize() const
function to serialize an SPHParticle
Definition: SPHParticle.h:295
void addEngDot(double eng_dot)
Adds the given value to the current value of the time derivative of the energy.
Definition: SPHParticle.h:220
double getMass() const
Getter for the mass of the particle.
Definition: SPHParticle.h:132
SPHParticle()
Default constructor of SPHParticle.
Definition: SPHParticle.h:24
void checkAndSetVSigMax(double v_sig)
Checks if the given signal velocity is higher than the current (local) one and updates the local one ...
Definition: SPHParticle.h:175
double getVSigMax() const
Getter for the current maximally allowed signal velocity of the particle.
Definition: SPHParticle.h:168
void setAcceleration(const std::array< double, 3 > &acc)
Setter for the acceleration.
Definition: SPHParticle.h:207
void subAcceleration(const std::array< double, 3 > &acc)
Substracts the given acceleration from the local acceleration.
Definition: SPHParticle.cpp:20
const std::array< double, 3 > & getAcceleration() const
Getter for the acceleration of the particle.
Definition: SPHParticle.h:187
SPHParticle(const std::array< double, 3 > &r, const std::array< double, 3 > &v, unsigned long id, double mass, double smth, double snds)
Constructor of the SPHParticle class.
Definition: SPHParticle.h:70
constexpr std::tuple_element< attribute, SoAArraysType >::type::value_type get()
Non-const getter for the pointer of this object.
Definition: SPHParticle.h:431
double getSoundSpeed() const
Getter for the speed of sound of the particle.
Definition: SPHParticle.h:156
void setVel_half(const std::array< double, 3 > &vel_half)
Setter for velocity at half-time step (leapfrog)
Definition: SPHParticle.h:277
constexpr void set(typename std::tuple_element< attribute, SoAArraysType >::type::value_type value)
Setter, which allows set an attribute using the corresponding attribute name (defined in AttributeNam...
Definition: SPHParticle.h:487
void calcDt()
Calculate the maximally allowed time step for the particle based on the smoothing length and the sign...
Definition: SPHParticle.h:262
void setSmoothingLength(double smth)
Setter for the smoothing length.
Definition: SPHParticle.h:150
void setMass(double mass)
Setter for the mass of the particle.
Definition: SPHParticle.h:138
double getEng_half() const
Getter for energy at half-time step (leapfrog)
Definition: SPHParticle.h:283
static SPHParticle deserialize(const double *stream, size_t &index)
funtion to deserialize an SPHParticle
Definition: SPHParticle.h:339
double getEnergy() const
Getter for the energy of the particle.
Definition: SPHParticle.h:232
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
OwnershipState
Enum that specifies the state of ownership.
Definition: OwnershipState.h:19
Helper struct to get a the SoAType.
Definition: SoAType.h:23