-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriftMeasureDevice.cpp
171 lines (133 loc) · 3.79 KB
/
DriftMeasureDevice.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
#include "DriftMeasureDevice.h"
#include <cmath>
DriftMeasureDevice::DriftMeasureDevice(const PointCharge& charge, const unsigned int trail_size) :
pCharge(&charge), integrating(false), loops(0), relativeAngle(0.0), looped(false),
posTrail(trail_size), velTrail(trail_size)
{
driftFactor = 1.0;
frequencyFactor = 1.0;
}
// Reads fields
void DriftMeasureDevice::setFields(const System* system)
{
if (!integrating)
return;
vec pos = pCharge->getPos();
E = system->E(pos, pCharge);
B = system->B(pos, pCharge);
gradB = system->gradB(pos, pCharge);
L = system->L(pos);
}
// Scale factors
void DriftMeasureDevice::adimensionalize(const DimensionSet& base)
{
driftFactor = base.mass * M_C / (base.charge * base.B * base.time);
frequencyFactor = base.time * base.charge * base.B / (base.mass * M_C);
}
// Does nothing
vec DriftMeasureDevice::EField(const vec& r, const double t) const { return vec(); }
vec DriftMeasureDevice::BField(const vec& r, const double t) const { return vec(); }
// Helper methods -------------------------------------------------------
double DriftMeasureDevice::Kparallel() const
{
return pCharge->getMass() * pCharge->getVel().project(B).norm2() / 2.0;
}
double DriftMeasureDevice::gyroRadius() const
{
vec v = pCharge->getVel();
return (v - v.project(B)).norm() / std::abs(gyroFrequency());
}
vec DriftMeasureDevice::expectedDriftVel() const
{
//revisar
vec Bdrift = driftFactor * (pCharge->K() + Kparallel()) / (pCharge->getCharge() * std::pow(B.norm(), 3)) * cross(B, gradB);
vec polDrift = cross(E, B) / (B.norm() * B.norm());
return Bdrift + polDrift;
}
// Drift calculation
void DriftMeasureDevice::move(const double dt)
{
// If not integrating, quits
if (!integrating)
{
posTrail.push(pCharge->getPos());
velTrail.push(pCharge->getVel());
return;
}
vec pos = pCharge->getPos();
vec vel = pCharge->getVel();
// Get angle change from change in v (tangent to path)
vec tprev = velTrail.front().normalized();
vec t = vel.normalized();
// a dot b = |a||b|cos theta
relativeAngle += std::acos(tprev * t);
// Loop
looped = relativeAngle > 2 * M_PI;
if (looped)
{
relativeAngle -= 2*M_PI;
loops++;
}
// Valid if horizon crossed
if (pos.z * posTrail.front().z <= 0.0)
valid = true;
// Calculate drifts:
totalTime += dt;
totalExpectedDrift += expectedDriftVel() * dt;
totalDrift += vel * dt;
totalEpsilon += epsilon();
posTrail.push(pos);
velTrail.push(vel);
}
// API -----------------------------------
// Negative sign for direction
double DriftMeasureDevice::gyroFrequency() const
{
return -frequencyFactor * pCharge->getCharge() * B.norm() / pCharge->getMass();
}
double DriftMeasureDevice::getMeasuredDrift() const
{
// Project onto theta and remove last element
vec pos = pCharge->getPos();
vec theta = vec(-pos.y, pos.x, 0.0);
return totalDrift.project(theta).norm();
}
double DriftMeasureDevice::getExpectedDrift() const
{
return totalExpectedDrift.norm();
}
double DriftMeasureDevice::getMeanEpsilon() const
{
return totalEpsilon / totalTime;
}
bool DriftMeasureDevice::hasLooped() const
{
return looped;
}
bool DriftMeasureDevice::isValid() const
{
return valid;
}
// r / L
double DriftMeasureDevice::epsilon() const
{
return gyroRadius() / L;
}
// Signals --------------------------------------------
void DriftMeasureDevice::startIntegrating()
{
integrating = true;
valid = false;
// Reset
totalTime = 0.0;
totalEpsilon = 0.0;
totalDrift = vec();
totalExpectedDrift = vec();
relativeAngle = 0.0;
loops = 0;
looped = false;
}
void DriftMeasureDevice::stopIntegrating()
{
integrating = false;
}