-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrator.cc
187 lines (144 loc) · 4.47 KB
/
integrator.cc
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
#define simulation
#include "integrator.h"
#include "sim_dat.h"
#include "interpolate.h"
#include <stddef.h>
#include <iostream>
#include "math.h"
bool Integrator::evaluate_derivative(
float t,
float dt,
const float * d_in,
float * d_out){
float * y_n = particle->state;
bool success;
Particle temp;
//make fake particle at propper time
for (int i =0; i<6; i++){
temp.state[i] = particle->state[i] + d_in[i];
}
temp.mass = particle->mass;
temp.charge = particle->charge;
// spatial output
for (int i =0; i<3; i++){
d_out[i] = dt*temp.state[3+i];
}
//Use fake particle to calc velocity space deriv
success = acc_func(temp, *sd, &d_out[3]);
if (!success){return false;}
for (int i=0;i<3;i++){
d_out[3+i] = d_out[3+i]*dt;
}
return true;
}
bool Integrator::integrate_step(){
bool success = true;
float d0[6] = {0,0,0,0,0,0},d1[6],d2[6],d3[6], d4[6], temp[6];
//This needs to be float checked b/c edited eval deriv func
success = evaluate_derivative( t, dt, d0, d1);
if (!success) {return false;}
for (int i=0; i<6; i++){temp[i] = d1[i]/2.0;}
success = evaluate_derivative( t+dt*0.5f, dt, temp, d2);
if (!success) {return false;}
for (int i=0; i<6; i++){temp[i] = d2[i]/2.0;}
success = evaluate_derivative( t+dt*0.5f, dt, temp, d3);
if (!success) {return false;}
success = evaluate_derivative( t+dt, dt, d3, d4);
if (!success) {return false;}
float ddt[6];
/*
for (int i =0; i<3; i++){
ddt[i] = 1.0f/6.0f* (d1[i]+2.0f* (d2[i]+d3[i])+d4[i]);
std::cout << i<<": "<< particle->state[i]<<", "<< ddt[i] <<", "<< particle->state[i+3]<<", "<<ddt[i+3]<<std::endl;
}*/
for (int i =0; i<6; i++){
ddt[i] = 1.0f/6.0f* (d1[i]+2.0f* (d2[i]+d3[i])+d4[i]);
particle->state[i] = particle->state[i] + ddt[i];
}
t = t+dt;
if (verbose){particle->print_state();}
return success;
}
int Integrator::evaluate_bcs(){
float planet_r2 = 3390.0*3390.0, particle_r2;
for (int axis=0; axis<3; axis++){
if ((particle->state[axis]+2*particle->state[3+axis]*dt < sd->bbox[axis]) ||
(particle->state[axis]+2*particle->state[3+axis]*dt > sd->bbox[axis+3])){
return 1;
}
}
particle_r2 = particle->state[0]*particle->state[0]+
particle->state[1]*particle->state[1]+
particle->state[2]*particle->state[2];
if (particle_r2 < planet_r2){return 2;}
return 0;
}
bool Integrator::evaluate_psphere(){
if (particle->state[0]>0.5*3390+init_state[0]){
return true;}
else {return false;}
}
bool Integrator::evaluate_tail(){
if (particle->state[0]<-1.1*3390){
return true;}
else {return false;}
}
int Integrator::evaluate_final_status(){
float vx,vy,vz,vfinal,dr_x,dr_y,dr_z,dr;
int pstatus;
pstatus = evaluate_bcs();
//escape
if (pstatus == 1){
if (within_psphere){
return 3; //plasmasphere escape
}
else{
vx = particle->state[3];
vy = particle->state[4];
vz = particle->state[5];
vfinal = pow(vx*vx+vy*vy+vz*vz, 0.5);
if (vfinal > 0.95*350){
return 6; //plume escape
}
else {
return 5; //Direct escape
}
}
}
else{ //bound
dr_x = particle->state[0]-init_state[0];
dr_y = particle->state[1]-init_state[1];
dr_z = particle->state[2]-init_state[2];
dr = pow(dr_x*dr_x+dr_y*dr_y+dr_z*dr_z,0.5);
if (dr < 0.5*3390){
return 1; //closely bound
}
else{
if (within_tail){ return 2;} //bound tail
else {return 3;} //bound pshere
}
}
}
int Integrator::integrate(){
int pstatus = 0;
bool success;
while((t<t_final)){
success = integrate_step();
if (!success){
pstatus = -1;
break;
}
//check if still in bounds
if (has_sd) {
pstatus = evaluate_bcs();
if (! within_psphere){
within_psphere = evaluate_psphere();}
if (! within_tail){
within_tail = evaluate_tail();
}
if (pstatus != 0){ break;}
}
}
pstatus = evaluate_final_status();
return pstatus;
}