source: proiecte/NBody/PlummerModel/ModelParallel.c @ 164

Last change on this file since 164 was 164, checked in by (none), 14 years ago
File size: 3.3 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <math.h>
4#include <stdlib.h>
5#include <time.h>
6#include <omp.h>
7
8#include "Model.h"
9
10body* bodies; //vector that contains all the bodies in the system
11
12void setBody(body* b, double mass, double pos[3], double vel[3]){
13               
14        b->mass = mass;
15        memcpy(b->pos, pos, 3 * sizeof(double));
16        memcpy(b->vel, vel, 3 * sizeof(double));
17}
18
19
20void initializeNBody(int n){
21        int i;
22        double mass = 0;
23        double pos[3] = {0, 0, 0};
24        double vel[3] = {0, 0, 0};
25       
26        #pragma omp parallel for default(none) private(i, mass, pos, vel) shared(n, bodies)
27        for (i = 0; i < n; i++){
28                setBody(&bodies[i], mass, pos, vel);
29                //printf("%f %f\n", bodies[i].mass, bodies[i].pos[2]);
30        }
31        #pragma omp barrier
32}
33
34//generate random float number between [low, high)
35double frand(double low, double high)
36{
37        double temp;
38
39        /* swap low & high around if the user makes no sense */
40        if (low > high){
41                temp = low;
42                low = high;
43                high = temp;
44        }
45
46        /* calculate the random number & return it */
47        temp = (rand() / ( RAND_MAX + 1.0))
48                * (high - low) + low;
49        return temp;
50}
51
52
53
54void mkplummer(int n, unsigned int seed){
55        int i;
56        double mass;
57        double pos[3];
58        double vel[3];
59        double radius, theta, phi;
60        double x, y;
61        double velocity;
62
63        if(seed == 0)
64                srand(time(NULL)); //different succession of results in the subsequent calls to rand
65        else srand(seed);   //used for debugging
66
67        //initializeNBody(n);
68       
69        #pragma omp parallel for default(none)\
70                private(i, mass,radius,theta, phi, pos, vel, x, y, velocity) \
71                shared(n, bodies)
72        for(i = 0;i < n; i++){
73                mass = 0;
74                pos[0] = 0; pos[1] = 0; pos[2] = 0;
75                vel[0] = 0; vel[1] = 0; vel[2] = 0;
76                setBody(&bodies[i], mass, pos, vel);
77                mass = 1.0 / n;
78                radius = 1.0 / sqrt(pow(frand(0, 1),-(2.0 / 3.0)) - 1.0);
79                theta = acos(frand(-1, 1));
80                phi = frand(0, 2 * PI);
81                pos[0] = radius * sin(theta) * cos(phi);
82                pos[1] = radius * sin(theta) * sin(phi);
83                pos[2] = radius * cos(theta);
84                x = 0.0;
85                y = 0.1;
86                while( y > x * x * pow (1.0 - x*x, 3.5)){
87                        x = frand(0, 1);
88                        y = frand(0, 0.1);
89                }
90                velocity = x * sqrt(2.0) * pow(1.0 + radius * radius, -0.25);
91                theta = acos(frand(-1, 1));
92                phi = frand(0, 2 * PI);
93                vel[0] = velocity * sin(theta) * cos(phi);
94                vel[1] = velocity * sin(theta) * sin(phi);
95                vel[2] = velocity * cos(theta);
96                setBody(&bodies[i], mass, pos, vel);
97        }
98
99        #pragma omp barrier
100
101}
102
103void writeData(int n){
104        char line[256];
105        int i; 
106        FILE *f = fopen(FILE_NAME, "w+");
107        if(f == NULL){
108                printf("Error opening file\n");
109                return;
110        }
111        sprintf(line, "%d\n", n);
112        fputs(line, f);
113        sprintf(line, "%d\n", 0);
114        fputs(line, f);
115        for(i = 0; i < n; i++){
116                //printf("here\n");
117                sprintf(line, "%f %f %f %f %f %f %f\n", 
118                        bodies[i].mass,
119                        bodies[i].pos[0], bodies[i].pos[1], bodies[i].pos[2],
120                        bodies[i].vel[0], bodies[i].vel[1], bodies[i].vel[2]);
121                fputs(line, f);
122        }
123        fclose(f);
124
125}
126
127int main(int argv, char** argc){
128        int n;
129        unsigned int seed;
130        if(argv != 3){
131                printf("usage ./Model nr_bodies seed\n");
132                return 0;
133        }
134
135        n = atoi(argc[1]);
136        seed = atoi(argc[2]);
137
138        bodies = (body*) malloc(n * sizeof(body));
139        mkplummer(n, seed);
140        /*int id;
141        #pragma omp master{
142                id = omp_get_thread_num();
143                printf("thread %d\n", id);
144                writeData(n);
145        }*/
146        //writeData(n);
147        return 1;
148}
Note: See TracBrowser for help on using the repository browser.