Skip to content
Snippets Groups Projects
Select Git revision
  • febcce80e08ae849fbcfbdb54ef869839c9b2fa6
  • master default protected
2 results

galaxy.c

Blame
  • galaxy.c 3.90 KiB
    /* * * * * * * * * * * * * * * *
    * Projet : GALAXY SIMULATION   *
    * 			   			       *
    * Auteur :  Arnaud de Vevey    *
    * 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]);
    
    
            printf("\n\n");
            tests();
    
    
    
            double zone = 1e10;
            Box box_initial = new_box(-zone, zone, zone, -zone);
            Galaxy* galaxy = create_and_init_galaxy(nb_stars, box_initial, DELTA_T);
            
            
    
    
    
            /*
            allocation_mémoire_et_initialisation_de_la_galaxie();
            itérations_temporelles {
                liberation_des_etoiles_qui_sortent_du_domaine();
                affichage();mise_a_zero_des_accelerations();
                creation_quad_tree();
                mise_a_jour_des_accelerations_via_le_quad_tree();
                mise_a_jour_des_positions();
                destruction_du_quad_tree();
    
            destruction_galaxie();
            */
            free_galaxy(galaxy);
    
        } else {
            printf("\nNombre d'arguments non valide \n\n");
            exit(1);
        }
    }
    
    
    
    
    
    /* * * * * * * * * * * * * * * * *
    * 				                 *
    *        --- FONCTIONS ---       *
    *				                 *
    * * * * * * * * * * * * * * * * */
    
    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;
    
        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);
    
            } else {
                double m_i = random_mass();
                Vector r_i;
                do {
                    r_i = random_position();
                } while (!is_inside(box, r_i));
                Vector v_i = random_speed(r_i, m_i);
                
    
                temp = new_star_vel(r_i, v_i, *v_null, m_i, 0.0);
            }
            
            list_stars[i] = temp;
    
            // on regarde comment cette étoile se fait attirer par le "soleil"
            update_acc(list_stars[i], list_stars[0]);
        }
    
    
        Galaxy* galaxy = malloc(sizeof(Galaxy));
        galaxy -> nb_bodies = nb_bodies;
        galaxy -> stars = list_stars;
        galaxy -> box = box;
        
    
        return galaxy;
    }
    
    
    void reset_acc_galaxy(Galaxy* galaxy) {
        Vector* v_null = new_vec(0, 0); 
    
        for (int i = 0; i < galaxy -> nb_bodies; i++) {
            galaxy -> stars[i].acc = *v_null;
        }
    
        free(v_null);
    }
    
    
    void update_pos_galaxy(Galaxy* galaxy, double delta_t) {
        for(int i = 0; i < galaxy -> nb_bodies; i++) {
            update_pos_star(&(galaxy -> stars[i]), 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--;
            }
        }
        
    }
    
    
    void resize_galaxy(Galaxy* galaxy) {
        double enlarge = 10.0;
    
        galaxy -> box.x0 -= enlarge;
        galaxy -> box.y0 += enlarge;
    
        galaxy -> box.x1 += enlarge;
        galaxy -> box.y1 -= enlarge;
    }
    
    
    void tests() {
        tests_vector();
        tests_box();
        tests_star();
        tests_galaxy();
        // tests_quadtree();
    }
    
    
    
    
    
    /* * * * * * * * * * * * * * * * *
    *                                *
    *          --- TESTS ---         *
    *                                *
    * * * * * * * * * * * * * * * * */
    
    void tests_galaxy() {
        printf("TESTS GALAXY : \n");
    
    
    
        printf("\n");
    }