Repository navigation
Expand file tree
/
Copy pathModelMagPend.cpp
More file actions
executable file
·203 lines (171 loc) · 5.04 KB
/
Copy pathModelMagPend.cpp
File metadata and controls
executable file
·203 lines (171 loc) · 5.04 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
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
/*
* File: ModelMagneticPendulum.cpp
* Author: user
*
* Created on 29. April 2009, 23:08
*/
#include <cstdlib>
#include <iostream>
#include <cmath>
#include "ModelMagPend.h"
using namespace std;
ModelMagPend::Source::Source(double a_x,
double a_y,
double a_z,
double a_r,
double a_s,
int a_type,
const gfx::Color &a_col)
:x(a_x)
,y(a_y)
,r(a_r)
,s(a_s)
,col(a_col)
,type(a_type)
{}
//------------------------------------------------------------------------
ModelMagPend::ModelMagPend(double rad)
:IModel("Magnetisches Pendel (2D)", 4)
,m_rk(0.2)
,m_k(0.3)
,m_magnets()
,m_nIdxMag(-1)
{
// The pendulum
m_magnets.push_back(Source(0, // xpos
0, // ypos
0, // zpos
0.05, // rad
m_k, // strength
1,
gfx::Color(255, 255, 255)));
int col=1;
rad = 0.7;
// Magnets are located at a ring with radius 0.5
for (int i=0; i<360; i+=72, ++col)
{
m_magnets.push_back( Source(rad*sin(i*M_PI/180.0), // xpos
rad*cos(i*M_PI/180.0), // ypos
-0.02, // zpos
0.04, // rad
0.2, // strength
0, // index of the force law
gfx::Color::FromPalette(col)) );
}
rad = 0.4;
for (int i=36; i<396; i+=72, ++col)
{
m_magnets.push_back( Source(rad*sin(i*M_PI/180.0), // xpos
rad*cos(i*M_PI/180.0), // ypos
-0.02,
0.04, // rad
0.1, // strength
0,
gfx::Color::FromPalette(col)) );
}
// m_magnets.push_back( Source(0, 0.5, 0.05, 1.0, 0) );
// m_magnets.push_back( Source(0, -0.5, 0.05, 1.0, 0) );
}
//------------------------------------------------------------------------
void ModelMagPend::Eval(double *state, double time, double *deriv)
{
// velocity
double &vel_x(state[0]);
double &vel_y(state[1]);
// position
double &pos_x(state[2]);
double &pos_y(state[3]);
// Pendel
double acc_x(0);
double acc_y(0);
bool bCheckAbort = false;
for (unsigned i=0; i<m_magnets.size(); ++i)
{
Source &src = m_magnets[i];
if (src.type==1)
{
acc_x += src.s * (src.x - pos_x);
acc_y += src.s * (src.y - pos_y);
}
else
{
double d = sqrt( (src.x - pos_x) * (src.x - pos_x) +
(src.y - pos_y) * (src.y - pos_y) );
bCheckAbort |= (d<src.r);
//d += 0.0025; // soften the forcefield a bit for very close range to avoid div by zero
double dddd = d*d*d*d;
acc_x += (src.s / (dddd+0.001)) * (src.x - pos_x);
acc_y += (src.s / (dddd+0.001)) * (src.y - pos_y);
}
}
// Friction
acc_x -= m_rk * vel_x;
acc_y -= m_rk * vel_y;
// derivation of the state array
deriv[0] = acc_x;
deriv[1] = acc_y;
deriv[2] = vel_x;
deriv[3] = vel_y;
m_bAbort = bCheckAbort && (/*(acc_x*acc_x + acc_y*acc_y) < 1e-2 &&*/
(vel_x*vel_x + vel_y*vel_y) < 0.005);
}
//------------------------------------------------------------------------
int ModelMagPend::GetMagNum() const
{
return m_magnets.size();
}
//------------------------------------------------------------------------
int ModelMagPend::GetStopIdx() const
{
return m_nIdxMag;
}
//------------------------------------------------------------------------
const ModelMagPend::src_vec& ModelMagPend::GetSources() const
{
return m_magnets;
}
//------------------------------------------------------------------------
bool ModelMagPend::IsFinished(double *state)
{
if (m_bAbort)
{
double &pos_x(state[2]);
double &pos_y(state[3]);
double minDist = 999;
for (unsigned i=0; i<m_magnets.size(); ++i)
{
const Source &src = m_magnets[i];
double dist = sqrt( (src.x-pos_x) * (src.x-pos_x) +
(src.y-pos_y) * (src.y-pos_y) );
if (dist<minDist)
{
m_nIdxMag = i;
minDist = dist;
}
}
}
return m_bAbort;
/*
double v = sqrt(vel_x*vel_x + vel_y*vel_y);
m_nIdxMag = -1;
std::cout << "v=" << v << " ";
// Stopbedingung:
// Pendel ist langsam und in der Nähe einer Quelle
if (v<0.0001)
{
for (unsigned i=0; i<m_magnets.size(); ++i)
{
const Source &src = m_magnets[i];
double dist = sqrt( (src.x-pos_x) * (src.x-pos_x) +
(src.y-pos_y) * (src.y-pos_y) );
std::cout << "d=" << dist << " ";
if (dist<src.r)
{
m_nIdxMag = i;
return true;
}
}
}
return false;
*/
}