-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
286 lines (256 loc) · 6.62 KB
/
main.c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
void copy_array(double* array1, double array2[], int size)
{
int i;
for (i=0; i<size; i++)
{
array2[i] = *(array1 + i);
}
}
void print_array(const double v[], int len)
{
int j;
for (j=0; j < len-1; j++)
{
printf("%f, ", v[j]);
}
if (len) printf("%f\n", v[j]);
else printf("Print_array: Array with no size\n");
}
// Stolen Quake3 fast inverse square root function
float InvSqrt(float x)
{
float xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759df - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
double mitjana(double array[], int size)
{
double avg = 0;
int i;
for (i=0; i<size; i++){
avg += array[i];
}
return avg/size;
}
// Calculates a step of RK4
//
// EXAMPLE: Harmonic oscilator
//
// diferential eq dv_z/dt = -kz (where v_z = dz/dt)
// dz/dt = v_z
//
// => y = (v_z, z) => dy/dx = f(x,y) = (dv_z/dz, dz/dx) = ( -kz, v_z))
// x = t
//
// Dependent Indepenedent Number of Function dy/dx = f(x,y) Integration OUTPUT
// variables variable dependent step |
// (positions, (time) variables | |
// velocities) | |
// x y[] f(x,y) size | |
void rungekutta4(const double y[], double x, int size, int (*func)(double, const double*, double*, int), double h, double* y1)
{
int i;
double K1[size], K2[size], K3[size], K4[size], ycache[size];
// Sets K1 = f(x,y)
func(x,y,K1,size);
// Sets K2 = f(x + h/2, y + h/2*K1)
for (i=0; i<size; i++)
{
ycache[i] = y[i] + h/2*K1[i];
}
func(x + h/2, ycache, K2, size);
// Sets K3 = f(x + h/2, y + h/2*K2)
for (i=0; i<size; i++)
{
ycache[i] = y[i] + h/2*K2[i];
}
func(x + h/2, ycache, K3, size);
// Sets K4 = f(x + h, y + h*K3)
for (i=0; i<size; i++)
{
ycache[i] = y[i] + h*K3[i];
}
func(x + h, ycache, K4, size);
// Computes the final answer
for (i=0; i<size; i++)
{
y1[i] = y[i] + h/6*(K1[i] + 2*(K2[i]+K3[i]) + K4[i]);
}
}
// Calculates a simple harmonic oscilator for testing RK4 porpuses
int harmonic(double x,const double y[], double f[], int size)
{
double k = 9.8; // Elasticity regulator constant
f[0] = y[1];
f[1] = -k*y[0];
return 0;
}
#define DIM 2
#define MAX_SIZE 8000
#define G_STR 800.0
// Calculates the gravitational force for N bodies of the same mass in DIM dimensions
// Takes y[] as an argument y[] = {x1,...,xn,v1,...,vn} (NULL terminated)
// f[] is the output and has the form f[] = {v1,...,vn,a1,...,an}
// where v are the velocities and a the acceleration
int gravity(double x, const double y[], double f[], int size)
{
int i, j, k;
const int SIZE = size;
const int SIZE2 = size/2; // SIZE/2
const int N=SIZE/(2*DIM); // We have N particles, DIM coordinates and DIM velocities => number of dimentions of y[] f[]
const double G = G_STR; // Gravitational constant multiplied by m
double sum2, dist;
// Initialize f
for (i=0; i<SIZE2; i++)
{
f[i] = y[SIZE2+i]; // dz/dx = v_z
f[SIZE2+i] = 0;
}
for (i=0; i<SIZE2 -DIM; i+=DIM)
{
for (j=i+DIM; j<SIZE2; j+=DIM)
{
sum2 = 0;
// CALCULATE THE DISTANCE
for (k=0; k<DIM; k++)
{
sum2 +=(y[j+k]-y[i+k])*(y[j+k]-y[i+k]); // x**2 + y**2 + z**2
}
dist = InvSqrt(sum2);
// COMPUTES THE ACCELERATION
for (k=0; k<DIM; k++)
{
double f_module = G*(dist*dist*dist)*(y[j+k]-y[i+k]);
f[SIZE2+i+k] += f_module;
f[SIZE2+j+k] += -f_module;
}
}
}
return 0;
}
// Draws the particles in the wondow in 2D
void DrawParticles(SDL_Renderer *renderer, double coord[], int N)
{
SDL_SetRenderDrawColor(renderer, 200, 200, 200, 0); // Particle color
double x, y, radius, radius2;
radius = 14;
radius2 = 7; // half the radius
for (int j=0; j<N*DIM; j=j+2){
x = coord[j];
y = coord[j+1];
SDL_Rect rect = {
.x = x -radius2,
.y = y -radius2,
.w = radius,
.h = radius,
};
SDL_RenderFillRect(renderer, &rect);
}
}
int Add_Particle(double y[], const double coord[], int *size){
double y_copy[*size];
int size2 = (*size)/2; // half of the size
if (*size + 4< MAX_SIZE){
for (int i=0; i<*size; i++) y_copy[i]=y[i]; // Copy the array
for (int i=0; i<(*size)/2; i+=2){
y[i]=y_copy[i];
y[i+1]=y_copy[i+1];
y[size2+2+i]=y_copy[size2+i];
y[size2+3+i]=y_copy[size2+i+1];
}
y[size2] = coord[0];
y[size2+1] = coord[1];
y[*size+2] = coord[2];
y[(*size)+3] = coord[3];
*size += 4;
return 0;
for (int i=0; i<4; i++){
y[*size+i] = coord[i];
}
*size += 4;
return 0;
}
else{
printf("Exceeded maximum number of particles");
return -1;
}
}
#define get_size(x) DIM*x*2
int main()
{
int N_particles = 0;
int size = get_size(N_particles);
double y[MAX_SIZE], y1[MAX_SIZE], t, h;
double coord[4] = { 0., 0., 0., 0. };
int i, j, iterations;
// FINITE ELEMENTS VARIABLES
h = 0.1;
// INITIAL CONDITIONS
t = 0;
for (i=0; i < size; i++)
{
y[i] = 0;
}
// GRAPHICAL INTERFACE WITH SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "ERROR: Could not initialize SDL: %s\n",SDL_GetError());
exit(1);
}
SDL_Window *window = SDL_CreateWindow("window test", 0, 0, 800, 600, SDL_WINDOW_RESIZABLE);
if ( window == NULL ) {
fprintf(stderr, "ERROR: Could not create a window %s\n", SDL_GetError());
exit(1);
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if ( renderer == NULL ){
fprintf(stderr, "ERROR: Could not create a renderer %s\n", SDL_GetError());
exit(1);
}
// Window Main loop
bool quit=false;
int mouseX, mouseY;
while (!quit) {
// EVENT LOOP
SDL_Event event;
while (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
quit=true;
case SDL_MOUSEBUTTONDOWN:
SDL_GetMouseState(&mouseX, &mouseY); // Where is the mouse?
coord[0] = mouseX;
coord[1] = mouseY;
Add_Particle(y, coord, &size);
N_particles++;
break;
}
}
SDL_Delay(17); // Waits to display frames
// BACKGROUND COLOR
SDL_SetRenderDrawColor(renderer, 48, 40, 60 ,0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);
SDL_Rect rect = {
.x = 50,
.y = 50,
.w = 50,
.h = 50,
};
SDL_RenderFillRect(renderer, &rect);
// TRAJECTORY CALCULATIONS
rungekutta4(y, t, size, gravity, h, y1); // Runge Kutta step
copy_array(y1, y, size); // Updates y to y1
// DISPLAY THE PARTICLES
DrawParticles(renderer, y, N_particles);
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}