-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
213 lines (178 loc) · 7.75 KB
/
main.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <eigen3/Eigen/Core>
#include <functional>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
using namespace Eigen;
// Create springs
// Create masses
// Connectivity
// springs know masses
// loop on springs
struct Mass {
float m;
Vector2f position, velocity, forcesApplied;
bool isFixed;
Mass(float x, float y, float m) : m(m), position(Vector2f(x, y)), velocity(0,0), isFixed(false) {}
Mass(float x, float y) : m(0.f), position(Vector2f(x, y)), velocity(0,0), isFixed(true) {}
friend std::ostream& operator<<(std::ostream& os, const Mass& m) {
os << m.position(0) << ' ' << m.position(1) << ' ';
// << m.velocity(0) << ' ' << m.velocity(1) << ' ';
return os;
}
};
struct Spring {
float k, l;
Mass *mass1, *mass2;
inline Vector2f force() const {
const Vector2f& x1 = mass1->position;
const Vector2f& x2 = mass2->position;
const Vector2f direction = 1./(x2-x1).norm() * (x2-x1);
return k*((x2-x1).norm()-l)*direction;
}
};
struct System {
std::vector<Mass> masses;
std::vector<Spring> springs;
using Connectivity = std::map<std::size_t, std::pair<std::size_t, std::size_t> >;
System(const std::vector<Mass>& masses_,
const std::vector<Spring>& springs_,
Connectivity connectivity) :
masses(std::move(masses_)),
springs(std::move(springs_))
{
for(const auto& c : connectivity) {
springs[c.first].mass1 = &masses[c.second.first];
springs[c.first].mass2 = &masses[c.second.second];
}
}
void forcesZero() {
for(auto & m : masses)
m.forcesApplied = Vector2f::Zero();
}
void updateForces() {
for(auto &s : springs) {
s.mass1->forcesApplied += s.force();
s.mass2->forcesApplied -= s.force();
}
}
void applyForces(float dt) {
for(auto& m : masses) {
if (!m.isFixed) {
m.velocity += (dt/m.m) * m.forcesApplied;
m.position += dt * m.velocity;
}
}
}
void process(float dt) {
forcesZero();
updateForces();
applyForces(dt);
}
void displayMass(size_t n, std::ostream& os = std::cout) {
os << masses[n];
}
};
struct Test {
bool enabled;
std::string description;
std::function<void(void)> runFunction;
void execute() const {
std::cout << description << "...";
runFunction();
std::cout << "done" << std::endl;
}
};
static const std::vector<Test> tests = {
{
.enabled = true,
.description = "One spring, one moving mass",
.runFunction = []{
std::vector<Mass> masses = {
Mass(0, 0),
Mass(0, -3, 3)
};
std::vector<Spring> springs = {
{.k=3, .l=2}
};
// Spring 0 connects masses 0 and 1
System::Connectivity connectivity = {
{0, {0, 1}}
};
System s(masses, springs, connectivity);
const float dt = .1;
const std::size_t timesteps = 1000;
std::ofstream f("1m1s.dat");
for (size_t ii = 0; ii < timesteps; ii++) {
s.displayMass(1, f);
f << '\n';
s.process(dt);
}
}
},
{
.enabled = true,
.description = "Three masses, three springs attached",
.runFunction = []{
std::vector<Mass> masses = {
Mass(0, -1, 1),
Mass(0, 1, 1),
Mass(1, .2, 1)
};
std::vector<Spring> springs = {
{.k=2, .l=3},
{.k=1, .l=3},
{.k=3, .l=3}
};
System::Connectivity connectivity = {
{0, {0, 2}},
{1, {1, 2}},
{2, {0, 1}}
};
System s(masses, springs, connectivity);
const float dt = .01;
const std::size_t timesteps = 5000;
std::ofstream f("3m3s.dat");
for (size_t ii = 0; ii < timesteps; ii++) {
for(int jj = 0; jj < 3; jj++)
s.displayMass(jj, f);
f << '\n';
s.process(dt);
}
}
},
{
.enabled = true,
.description = "One mass, four springs attached",
.runFunction = []{
std::vector<Mass> masses = {
Mass(0, 0),
Mass(1, 0),
Mass(0, 1),
Mass(1, 1),
Mass(.2, .6, 1)
};
std::vector<Spring> springs;
for (std::size_t ii = 0; ii < 4; ii++)
springs.push_back({.k=2, .l=2});
System::Connectivity connectivity;
for (std::size_t ii = 0; ii < 4; ii++)
connectivity[ii] = std::make_pair(ii, 4);
System s(masses, springs, connectivity);
const float dt = .01;
const std::size_t timesteps = 5000;
std::ofstream f("1m4s.dat");
for (size_t ii = 0; ii < timesteps; ii++) {
s.displayMass(4, f);
f << '\n';
s.process(dt);
}
}
}
};
int main() {
for(auto& test : tests)
if (test.enabled)
test.execute();
}