-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.C
More file actions
194 lines (176 loc) · 5.61 KB
/
sim.C
File metadata and controls
194 lines (176 loc) · 5.61 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
// preamble ////////////////////////////////////////////////////////////////////
#include <boost/python.hpp>
#include <cmath>
namespace py = boost::python;
/*******************************************************************************
c++ module
*******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
// class vector ////////////////////////////////////////////////////////////////
/* simple personalized vector
*
*/
class vec{
private:
double x;
double y;
public:
// constructor
vec( double x, double y) : x{x}, y{y} {}
// methods
double get_x(){ return x; }
double get_y(){ return y; }
double get_length(){ return std::sqrt( x*x + y*y ); }
double get_angle(){ return std::atan(y/x); }
py::list get_vec();
vec& operator= ( vec v);
vec operator+( vec v);
vec operator*( vec v);
void set_vec( double length, double angle );
};
// method for python lists
py::list vec::get_vec(){
py::list p;
p.append(get_x());
p.append(get_y());
return p;
}
// methods to simplify expresions
vec& vec::operator= ( vec v){
x = v.get_x();
y = v.get_y();
return *this;
}
vec vec::operator+ ( vec v){
return vec((v.get_x() + this->get_x()) , (v.get_y() + this->get_y()));
}
vec vec::operator* ( vec v){
return vec((v.get_x() * this->get_x()) , (v.get_y() * this->get_y()));
}
vec operator*(double a, vec v){
return vec( (v.get_x())*a , (v.get_y())*a );
}
void vec::set_vec( double length, double angle){
*this = vec( length * std::cos(angle) , length * std::sin(angle));
}
////////////////////////////////////////////////////////////////////////////////
// class external //////////////////////////////////////////////////////////////
/* contain info about: ait density, speed of wind and gravitation
*/
class external{
private:
double density;
vec velocity;
vec gravitation;
public:
// constructor
external( double d, vec v , vec g )
: density{d} , velocity{v} , gravitation{g} { }
// methods
double get_density(){ return density; }
vec get_wind(){ return velocity; }
vec get_gravitation(){ return gravitation; }
};
////////////////////////////////////////////////////////////////////////////////
// class bullet ////////////////////////////////////////////////////////////////
/* bullet - class that contains key methods
*
*/
class bullet{
private:
double mass;
vec position;
vec velocity;
double drag_cs;
public:
// constructor
bullet(double m, vec p, vec v, double c)
: mass{m}, position{p}, velocity{v}, drag_cs{c} { }
// methods
double get_mass(){ return mass; }
vec get_position(){ return position; }
vec get_velocity(){ return velocity; }
void next_v( external conditions , double time_step );
void next_point( external conditions, double time_step);
};
// we assume that air drag is proportional to square of velocity
void bullet::next_v( external conditions, double time_step){
// calculate of drag value
// R = C*S*rho*v^2 /2
vec drag_force(0,0);
vec v_r = conditions.get_wind() + velocity; // v
double df_length = v_r.get_length() * v_r.get_length(); // |v|^2
df_length *= (conditions.get_density() / 2.) * drag_cs; // C*S*rho*v^2 /2
drag_force.set_vec(df_length, v_r.get_angle() + 3.14159265);
velocity = velocity + // from II N. p.
mass * time_step * (drag_force + conditions.get_gravitation());
}
void bullet::next_point( external conditions, double time_step){
position = position + time_step * velocity;
next_v( conditions, time_step);
}
////////////////////////////////////////////////////////////////////////////////
// funkcja symulujaca rzut /////////////////////////////////////////////////////
class simulation{
private:
// axis
double x_max, y_max, x_min, y_min;
// python list
py::list out;
public:
simulation( bullet bull, external conditions, double time_step);
py::list get_out(){ return out; }
double get_x_max(){ return x_max; }
double get_y_max(){ return y_max; }
double get_x_min(){ return x_min; }
double get_y_min(){ return y_min; }
};
simulation::simulation( bullet bull, external conditions , double time_step){
x_max = x_min = bull.get_position().get_x();
y_max = y_min = bull.get_position().get_y();
py::list out_x, out_y;
long t = 0;
long t_safe = 50000;
double X, Y;
while( bull.get_position().get_y() > -1){
if( t > t_safe ) break;
++t;
X = bull.get_position().get_x();
Y = bull.get_position().get_y();
out_x.append( X );
out_y.append( Y );
bull.next_point( conditions, time_step);
}
// wpisywanie do listy wyjsciowej
out.append( out_x );
out.append( out_y );
}
/*******************************************************************************
boost module
*******************************************************************************/
using namespace py;
BOOST_PYTHON_MODULE(sim)
{
class_<vec>("vector", init<double, double>())
.def("read_y", &vec::get_y )
.def("read_x", &vec::get_x )
.def("read_length", &vec::get_length )
.def("read_vector" , &vec::get_vec )
;
class_<external>("external", init<double, vec, vec>())
;
class_<bullet>("bullet", init<double, vec, vec, double>())
.def("read_mass", &bullet::get_mass )
.def("read_position", &bullet::get_position )
.def("read_velocity", &bullet::get_velocity )
.def( "next_point", &bullet::next_point )
;
class_<simulation>("simulation", init< bullet, external, double>())
.def("read_x_max", &simulation::get_x_max )
.def("read_x_min", &simulation::get_x_min )
.def("read_y_max", &simulation::get_y_max )
.def("read_y_min", &simulation::get_y_min )
.def("read_out", &simulation::get_out )
;
}
////////////////////////////////////////////////////////////////////////////////