Skip to content
Snippets Groups Projects
Commit 42e569f5 authored by arnaud.devevey's avatar arnaud.devevey
Browse files

19h

parent febcce80
Branches master
No related tags found
No related merge requests found
Pipeline #6096 failed
......@@ -2,9 +2,9 @@ CC = gcc
CARGS = -g -std=c11 -Wall -Wextra -fsanitize=address -fsanitize=leak -fsanitize=undefined
LARGS = -lm -lSDL2
all = box galaxy quadtree star vector
all : galaxy
galaxy : galaxy.o box.o quadtree.o star.o vector.o
galaxy : vector.o box.o star.o galaxy.o quadtree.o
$(CC) $(CARGS) -o galaxy $^ $(LARGS)
......@@ -27,7 +27,7 @@ vector.o : vector.c
clean :
rm -f *.o galaxy
re : clean all
# --- RAPPELS ---
# fichier.o : fichier objet (fichier .c compilé)
......
......@@ -40,7 +40,8 @@ Box* divide_in_four(Box box) {
| |
| |
| |
------------------- (x1 ; y1)
-------------------
(x1 ; y1)
box0 (ul) : (x0;y0) (x1/2;y1/2)
box1 (ur) : (x1/2;y0) (x1;y1/2)
......@@ -54,17 +55,12 @@ Box* divide_in_four(Box box) {
Box b2 = new_box(box.x0, box.y1/2.0, box.x1/2.0, box.y1);
Box b3 = new_box(box.x1/2.0, box.y1/2.0, box.x1, box.y1);
// pointeur ou pas?
Box* sub_boxes = malloc(4 * sizeof(Box));
sub_boxes[0] = b0;
sub_boxes[1] = b1;
sub_boxes[2] = b2;
sub_boxes[3] = b3;
return sub_boxes;
}
......@@ -108,20 +104,20 @@ void tests_box() {
Box b1 = new_box(0.0, 0.0, 3.0, 3.0);
Box* b2 = divide_in_four(b1);
tests_box_divide(b2);
tests_box_include(b1);
free(b2);
printf("\n");
}
void tests_box_divide(Box* b) {
Box box_test = new_box(1.5, 0.0, 3.0, 1.5);
TEST(box_test.x0 == b[1].x0 && box_test.x1 == b[1].x1 && box_test.y0 == b[1].y0 && box_test.y1 == b[1].y1);
}
void tests_box_include(Box b1) {
Vector* vector = new_vec(1.0, 2.0);
TEST(is_inside(b1, *vector));
......
......@@ -16,6 +16,8 @@ typedef struct __box {
/* * * * * * * * * * * * * * * * *
* *
* --- PROTOTYPES --- *
......@@ -39,7 +41,6 @@ void print_box(Box box);
void tests_box();
// void tests_box_new(Box b);
void tests_box_divide(Box* b);
void tests_box_include(Box b);
......
No preview for this file type
......@@ -5,36 +5,25 @@
* Date : xx.06.2019 *
* * * * * * * * * * * * * * * */
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include <math.h>
// #include <assert.h>
// #include <stdint.h>
// #include <time.h>
// #include <libgen.h>
#include "galaxy.h"
#include "quadtree.h"
/* * * * * * * * * * * * * * * * *
* *
* --- MAIN --- *
* *
* * * * * * * * * * * * * * * * */
int main(int argc, char **argv) {
if (argc == 3) {
// convertit argv[#] en int
int nb_stars = atoi(argv[1]);
double theta = atoi(argv[2]);
//double theta = atoi(argv[2]);
printf("\n\n");
......@@ -81,16 +70,19 @@ int main(int argc, char **argv) {
* * * * * * * * * * * * * * * * */
Galaxy* create_and_init_galaxy(int nb_bodies, Box box, double delta_t) {
Star** list_stars = malloc(sizeof(Star*) * nb_bodies);
Vector* v_null = new_vec(0, 0);
Star* temp;
Star* list_stars = malloc(sizeof(Star) * nb_bodies);
Vector v_null = {0, 0};
Star temp;
for (int i = 0; i < nb_bodies; i++) {
if (i == 0) {
// on crée une étoile en (0;0) qui a comme masse 10^6 * masse solaire
temp = new_star_vel(*v_null, *v_null, *v_null, 1e6 * MASSE_SOLAIRE, 0.0);
//temp = *new_star_vel(*v_null, *v_null, *v_null, 1e6 * MASSE_SOLAIRE, delta_t);
temp.pos = v_null;
temp.previous_pos = v_null;
temp.acc = v_null;
temp.mass = 1e6 * MASSE_SOLAIRE;
} else {
double m_i = random_mass();
Vector r_i;
......@@ -100,13 +92,17 @@ Galaxy* create_and_init_galaxy(int nb_bodies, Box box, double delta_t) {
Vector v_i = random_speed(r_i, m_i);
temp = new_star_vel(r_i, v_i, *v_null, m_i, 0.0);
//temp = *new_star_vel(r_i, v_i, *v_null, m_i, 0.0);
temp.pos = r_i;
temp.previous_pos = v_i;
temp.acc = v_null;
temp.mass = m_i;
}
list_stars[i] = temp;
// on regarde comment cette étoile se fait attirer par le "soleil"
update_acc(list_stars[i], list_stars[0]);
update_acc(&list_stars[i], &list_stars[0]);
}
......@@ -139,13 +135,8 @@ void update_pos_galaxy(Galaxy* galaxy, double delta_t) {
void free_galaxy(Galaxy* galaxy) {
for (int i = 0; i < galaxy -> nb_bodies; i++) {
if (!is_inside(galaxy -> box, galaxy -> stars[i].pos)) {
free(&(galaxy -> stars[i]));
galaxy -> nb_bodies--;
}
}
free(galaxy->stars);
free(galaxy);
}
......
......@@ -8,8 +8,6 @@
#include "vector.h"
#include "star.h"
#define DELTA_T 1e10 // discrétisation temporelle
/* * * * * * * * * * * * * * * * *
......@@ -58,19 +56,6 @@ void free_galaxy(Galaxy* galaxy);
// lorsqu'une étoile s sort de la box de la galaxie, celle-ci doit être effacée du tableau d'étoiles contenu dans la galaxie (et sa mémoire libérée)
void resize_galaxy(Galaxy* galaxy);
// fait les tests de toutes les librairies
void tests();
void tests_galaxy();
void insert_star(Node *n, Star *s);
/* PSEUDO CODE
si (s est dans le box de n et n est alloué) {
......@@ -102,4 +87,9 @@ void insert_star(Node *n, Star *s);
// fait les tests de toutes les librairies
void tests();
void tests_galaxy();
#endif
\ No newline at end of file
......@@ -45,13 +45,14 @@ void reset_acc(Star* star) {
void update_acc(Star* target, const Star* const s2) {
Vector* rj_ri = sub_vec(&(s2 -> pos), &(target -> pos));
int G_mi_mj = FORCE_GRAVITATION * target -> mass * s2 -> mass;
double G_mi_mj = FORCE_GRAVITATION * target -> mass * s2 -> mass;
Vector* temp = mul_vec(rj_ri, G_mi_mj);
target -> acc = *mul_vec(temp, (1 / pow(norm(rj_ri), 3)));
Vector* acc = mul_vec(temp, (1 / pow(norm(rj_ri), 3)));
target -> acc = *acc;
free(rj_ri);
free(temp);
free(acc);
}
......@@ -63,52 +64,6 @@ void update_pos_star(Star* star, double delta_t) {
}
void print_star(const Star* const star) {
printf("\tprint_star() : \tposition = (%f ; %f) \n\t\t \
position precedente = (%f ; %f) \n\t\t \
acceleration = (%f ; %f) \n\t\t \
masse = %f \n", star -> pos.x, star -> pos.y, star -> previous_pos.x, star -> previous_pos.y, star -> acc.x, star -> acc.y, star -> mass);
}
int random_mass() {
return MASSE_MIN + (random_10() * MASSE_SOLAIRE);
}
Vector random_position() {
// R * (math.log(1 - random_1()) / 1.8) * chaque composante du vecteur v(0.5 - random_1() ; 0.5 - random_1())
// où R = math.pow(10, 18) [m] et R(0) est R(0) un nombre aléatoire entre 0 et 1
Vector position;
double temp = 1e18 * (log(1 - random_1()) / 1.8);
position.x = temp * (0.5 - random_1());
temp = 1e18 * (log(1 - random_1()) / 1.8);
position.y = temp * (0.5 - random_1());
return position;
}
Vector random_speed(Vector r_i, double m_i) {
// math.sqrt(FORCE_GRAVITATION * (star -> mass + )) * chaque composante du vecteur v(-sin(phi) ; cos(phi))
// où phi = atan2(star -> position.y / star -> position.x) // tan⁻¹
Vector* p_norm = new_vec(r_i.x, r_i.y);
double temp = sqrt(FORCE_GRAVITATION * (m_i + (1e6 * MASSE_SOLAIRE)) / norm(p_norm));
double phi = atan2(r_i.y, r_i.x);
Vector speed;
speed.x = temp * (-sin(phi));
speed.y = temp * (cos(phi));
return speed;
}
Star* super_star(Star* list_stars, int selection, int size_list) {
Star* super_star = malloc(sizeof(Star));
......@@ -149,6 +104,7 @@ Vector* resultante(Star** list_stars, int size_list) {
return Fi;
}
void new_position(Star* star, Vector resultante) {
Vector* acc_tmp = add_vec(&(star -> acc), &resultante);
star -> acc = *acc_tmp;
......@@ -160,6 +116,59 @@ void new_position(Star* star, Vector resultante) {
// UTILITAIRES
void print_star(const Star* const star) {
printf("\tprint_star() : \tposition = (%f ; %f) \n\t\t \
position precedente = (%f ; %f) \n\t\t \
acceleration = (%f ; %f) \n\t\t \
masse = %f \n", star -> pos.x, star -> pos.y, star -> previous_pos.x, star -> previous_pos.y, star -> acc.x, star -> acc.y, star -> mass);
}
int random_mass() {
return MASSE_MIN + (random_10() * MASSE_SOLAIRE);
}
Vector random_position() {
// R * (math.log(1 - random_1()) / 1.8) * chaque composante du vecteur v(0.5 - random_1() ; 0.5 - random_1())
// où R = math.pow(10, 18) [m] et R(0) est R(0) un nombre aléatoire entre 0 et 1
Vector position;
double temp = 1e18 * (log(1 - random_1()) / 1.8);
position.x = temp * (0.5 - random_1());
temp = 1e18 * (log(1 - random_1()) / 1.8);
position.y = temp * (0.5 - random_1());
return position;
}
Vector random_speed(Vector r_i, double m_i) {
// math.sqrt(FORCE_GRAVITATION * (star -> mass + )) * chaque composante du vecteur v(-sin(phi) ; cos(phi))
// où phi = atan2(star -> position.y / star -> position.x) // tan⁻¹
Vector* p_norm = new_vec(r_i.x, r_i.y);
double temp = sqrt(FORCE_GRAVITATION * (m_i + (1e6 * MASSE_SOLAIRE)) / norm(p_norm));
double phi = atan2(r_i.y, r_i.x);
Vector speed;
speed.x = temp * (-sin(phi));
speed.y = temp * (cos(phi));
free(p_norm);
return speed;
}
int random_10() {
return ((rand() % 10) + 1);
}
......
......@@ -33,10 +33,10 @@ Star* new_star_vel(Vector pos, Vector speed, Vector acc, double mass, double dt)
// previous_pos = pos - (dt * speed);
// remet à 0 l'accélération d'une étoile
void reset_acc_star(Star* star);
void reset_acc(Star* star);
// met à jour l'accélération d'une étoile cible à cause de l'attraction gravitationnelle d'une autre étoile
void update_acc_star(Star* target, const Star* const s2);
void update_acc(Star* target, const Star* const s2);
// met à jour la position d'une étoile d'après la discrétisation temporelle
......@@ -79,9 +79,7 @@ double random_1();
void tests_star();
void tests_star_new(Star* s);
void tests_stars_res(Star* star);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment