diff --git a/perso/Makefile b/perso/Makefile index 8639da9848b2e6c37826783a1ab4203b8b3ca709..316b1fc9e0d5524e915087a1538f6af80b300db2 100644 --- a/perso/Makefile +++ b/perso/Makefile @@ -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é) diff --git a/perso/box.c b/perso/box.c index 2a21e0d1a10d5ba0d0b5501ad8457fc66ac424cd..e7da8e3e62ba31f084121ea913d7783e2e0dd5ec 100644 --- a/perso/box.c +++ b/perso/box.c @@ -35,12 +35,13 @@ Box new_box(double new_x0, double new_y0, double new_x1, double new_y1) { Box* divide_in_four(Box box) { /* - (x0 ; y0) +(x0 ; y0) ------------------- | | | | | | - ------------------- (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)); diff --git a/perso/box.h b/perso/box.h index 48a5a5d988f91ac1ce01b0030c448f162b7de7f9..d7ae3119481f17e1376b0455d98eb1389a7982da 100644 --- a/perso/box.h +++ b/perso/box.h @@ -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); diff --git a/perso/galaxy b/perso/galaxy index 28e639b86f069e6be68216aef884df0304c25a08..6ab26c579dca97930f6e92613b08433c8d1550ca 100755 Binary files a/perso/galaxy and b/perso/galaxy differ diff --git a/perso/galaxy.c b/perso/galaxy.c index 2588f3265950a0b3141521a05966dbf9f56a27b8..2846fb48625a1a256c7c8ce0303e92c52e72717e 100644 --- a/perso/galaxy.c +++ b/perso/galaxy.c @@ -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); } diff --git a/perso/galaxy.h b/perso/galaxy.h index e787e83f87896ce98ca9bc1a1161eaee04f20875..7b049aa5add6c35cf1d13deb8eef8e7927b04175 100644 --- a/perso/galaxy.h +++ b/perso/galaxy.h @@ -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 diff --git a/perso/star.c b/perso/star.c index 99b022072e88e71fa8f749b705250761dbc7a87e..61625d613a454634eab7613d4cf300f4a5539639 100644 --- a/perso/star.c +++ b/perso/star.c @@ -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); } diff --git a/perso/star.h b/perso/star.h index d63c8b68a3709c770dcf7a762d5d3eb83eef7799..d50efb3b19f1ccc1ee8fd02b9d0edbf3725c3668 100644 --- a/perso/star.h +++ b/perso/star.h @@ -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);