-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (138 loc) · 5.65 KB
/
main.cpp
File metadata and controls
172 lines (138 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @file sph-main-mpi.cpp
* @date 10.04.2018
* @author seckler
*/
#include <mpi.h>
#include <array>
#include <cmath>
#include <iostream>
#include "autopas/AutoPas.h"
// #include "autopas/particles/ParticleDefinitions.h"
#include "SPHParticle.cpp"
using Particle = SPHParticle;
using AutoPasContainer = autopas::AutoPas<Particle>;
// using AutoPasContainer = autopas::AutoPas<autopas::ParticleBaseFP64>;
void SetupIC(AutoPasContainer &sphSystem, double *dt, double *end_time, const std::array<double, 3> &bBoxMax) {
// Place SPH particles
AutoPasLog(INFO, "Setup started");
const double dx = 1.0 / 2.0;
unsigned int i = 0;
for (double x = 0; x < bBoxMax[0]; x += dx) { // NOLINT
for (double y = 0; y < bBoxMax[1]; y += dx) { // NOLINT
for (double z = 0; z < bBoxMax[2]; z += dx) { // NOLINT
Particle ith({x, y, z}, {0, 0, 0}, i++, 0.75, 0.012, 0.);
ith.setDensity(1.0);
ith.setEnergy(2.5);
sphSystem.addParticle(ith);
}
}
}
// Set dt and end time
*dt = .002;
*end_time = .024;
AutoPasLog(INFO, "Setup completed");
AutoPasLog(INFO, "Number of particles: {}", i);
}
void Initialize(AutoPasContainer &sphSystem) {
AutoPasLog(INFO, "Initialization started");
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
part->calcPressure();
}
AutoPasLog(INFO, "Initialization completed");
}
void LogParticlePositions(AutoPasContainer &sphSystem) {
std::array<double, 3> position;
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
position = part->getR();
AutoPasLog(INFO, "Position of particle {}: {}, {}, {}", part->getID(), position[0], position[1], position[2]);
}
}
void leapfrogInitialKick(AutoPasContainer &sphSystem, const double dt) {
using namespace autopas::utils::ArrayMath::literals;
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
part->setVel_half(part->getV() + (part->getAcceleration() * (0.5 * dt)));
}
}
void leapfrogFullDrift(AutoPasContainer &sphSystem, const double dt) {
using namespace autopas::utils::ArrayMath::literals;
// time becomes t + dt;
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
part->addR(part->getVel_half() * dt);
}
}
void leapfrogPredict(AutoPasContainer &sphSystem, const double dt) {
using namespace autopas::utils::ArrayMath::literals;
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
part->addV(part->getAcceleration() * dt);
}
}
void leapfrogFinalKick(AutoPasContainer &sphSystem, const double dt) {
using namespace autopas::utils::ArrayMath::literals;
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
part->setV(part->getVel_half() + (part->getAcceleration() * (0.5 * dt)));
}
}
void applyConstantForce(AutoPasContainer &sphSystem) {
using namespace autopas::utils::ArrayMath::literals;
for (auto part = sphSystem.begin(autopas::IteratorBehavior::owned); part.isValid(); ++part) {
part->setAcceleration({2e3, 1e3, 0.0});
}
}
void addEnteringParticles(AutoPasContainer &sphSystem, std::vector<Particle> &invalidParticles) {
std::array<double, 3> boxMin = sphSystem.getBoxMin();
std::array<double, 3> boxMax = sphSystem.getBoxMax();
for (auto &p : invalidParticles) {
// first we have to correct the position of the particles, s.t. they lie inside of the box.
auto pos = p.getR();
for (auto dim = 0; dim < 3; dim++) {
if (pos[dim] < boxMin[dim]) {
// has to be smaller than boxMax
pos[dim] = std::min(std::nextafter(boxMax[dim], -1), boxMin[dim] + (boxMin[dim] - pos[dim]));
} else if (pos[dim] >= boxMax[dim]) {
// should at least be boxMin
pos[dim] = std::max(boxMin[dim], boxMax[dim] - (pos[dim] - boxMax[dim]));
}
}
p.setR(pos);
// add moved particles again
sphSystem.addParticle(p);
}
}
int main() {
std::array<double, 3> boxMin({0., 0., 0.}), boxMax{};
boxMax[0] = boxMax[1] = boxMax[2] = 1.;
double cutoff = 0.03; // 0.012*2.5=0.03; where 2.5 = kernel support radius
unsigned int rebuildFrequency = 6; // has to be multiple of two, as there are two functor calls per iteration.
double skinToCutoffRatio = 0.15;
AutoPasContainer sphSystem;
sphSystem.setNumSamples(
6); // has to be multiple of 2, should also be multiple of rebuildFrequency (but this is not necessary).
sphSystem.setBoxMin(boxMin);
sphSystem.setBoxMax(boxMax);
sphSystem.setCutoff(cutoff);
sphSystem.setVerletSkin(skinToCutoffRatio * cutoff);
sphSystem.setVerletRebuildFrequency(rebuildFrequency);
std::set<autopas::ContainerOption> allowedContainers{autopas::ContainerOption::linkedCells,
autopas::ContainerOption::verletLists,
autopas::ContainerOption::verletListsCells};
sphSystem.setAllowedContainers(allowedContainers);
sphSystem.init();
double dt;
double t_end;
SetupIC(sphSystem, &dt, &t_end, boxMax);
Initialize(sphSystem);
LogParticlePositions(sphSystem);
size_t step = 0;
for (double time = 0.; time < t_end; time += dt, ++step) {
leapfrogInitialKick(sphSystem, dt);
leapfrogFullDrift(sphSystem, dt);
auto invalidParticles = sphSystem.updateContainer();
addEnteringParticles(sphSystem, invalidParticles);
leapfrogPredict(sphSystem, dt);
applyConstantForce(sphSystem);
leapfrogFinalKick(sphSystem, dt);
AutoPasLog(INFO, "Iteration {} completed", step);
}
LogParticlePositions(sphSystem);
}