OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [bench/] [shootout/] [nbody.c] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
/*
2
Redistribution and use in source and binary forms, with or without
3
modification, are permitted provided that the following conditions are met:
4
 
5
    * Redistributions of source code must retain the above copyright
6
    notice, this list of conditions and the following disclaimer.
7
 
8
    * Redistributions in binary form must reproduce the above copyright
9
    notice, this list of conditions and the following disclaimer in the
10
    documentation and/or other materials provided with the distribution.
11
 
12
    * Neither the name of "The Computer Language Benchmarks Game" nor the
13
    name of "The Computer Language Shootout Benchmarks" nor the names of
14
    its contributors may be used to endorse or promote products derived
15
    from this software without specific prior written permission.
16
 
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
POSSIBILITY OF SUCH DAMAGE.
28
*/
29
 
30
/*
31
 * The Great Computer Language Shootout
32
 * http://shootout.alioth.debian.org/
33
 *
34
 * contributed by Christoph Bauer
35
 *
36
 */
37
 
38
#include <math.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
 
42
#define pi 3.141592653589793
43
#define solar_mass (4 * pi * pi)
44
#define days_per_year 365.24
45
 
46
struct planet {
47
  double x, y, z;
48
  double vx, vy, vz;
49
  double mass;
50
};
51
 
52
void advance(int nbodies, struct planet * bodies, double dt)
53
{
54
  int i, j;
55
 
56
  for (i = 0; i < nbodies; i++) {
57
    struct planet * b = &(bodies[i]);
58
    for (j = i + 1; j < nbodies; j++) {
59
      struct planet * b2 = &(bodies[j]);
60
      double dx = b->x - b2->x;
61
      double dy = b->y - b2->y;
62
      double dz = b->z - b2->z;
63
      double distance = sqrt(dx * dx + dy * dy + dz * dz);
64
      double mag = dt / (distance * distance * distance);
65
      b->vx -= dx * b2->mass * mag;
66
      b->vy -= dy * b2->mass * mag;
67
      b->vz -= dz * b2->mass * mag;
68
      b2->vx += dx * b->mass * mag;
69
      b2->vy += dy * b->mass * mag;
70
      b2->vz += dz * b->mass * mag;
71
    }
72
  }
73
  for (i = 0; i < nbodies; i++) {
74
    struct planet * b = &(bodies[i]);
75
    b->x += dt * b->vx;
76
    b->y += dt * b->vy;
77
    b->z += dt * b->vz;
78
  }
79
}
80
 
81
double energy(int nbodies, struct planet * bodies)
82
{
83
  double e;
84
  int i, j;
85
 
86
  e = 0.0;
87
  for (i = 0; i < nbodies; i++) {
88
    struct planet * b = &(bodies[i]);
89
    e += 0.5 * b->mass * (b->vx * b->vx + b->vy * b->vy + b->vz * b->vz);
90
    for (j = i + 1; j < nbodies; j++) {
91
      struct planet * b2 = &(bodies[j]);
92
      double dx = b->x - b2->x;
93
      double dy = b->y - b2->y;
94
      double dz = b->z - b2->z;
95
      double distance = sqrt(dx * dx + dy * dy + dz * dz);
96
      e -= (b->mass * b2->mass) / distance;
97
    }
98
  }
99
  return e;
100
}
101
 
102
void offset_momentum(int nbodies, struct planet * bodies)
103
{
104
  double px = 0.0, py = 0.0, pz = 0.0;
105
  int i;
106
  for (i = 0; i < nbodies; i++) {
107
    px += bodies[i].vx * bodies[i].mass;
108
    py += bodies[i].vy * bodies[i].mass;
109
    pz += bodies[i].vz * bodies[i].mass;
110
  }
111
  bodies[0].vx = - px / solar_mass;
112
  bodies[0].vy = - py / solar_mass;
113
  bodies[0].vz = - pz / solar_mass;
114
}
115
 
116
#define NBODIES 5
117
struct planet bodies[NBODIES] = {
118
  {                               /* sun */
119
    0, 0, 0, 0, 0, 0, solar_mass
120
  },
121
  {                               /* jupiter */
122
    4.84143144246472090e+00,
123
    -1.16032004402742839e+00,
124
    -1.03622044471123109e-01,
125
    1.66007664274403694e-03 * days_per_year,
126
    7.69901118419740425e-03 * days_per_year,
127
    -6.90460016972063023e-05 * days_per_year,
128
    9.54791938424326609e-04 * solar_mass
129
  },
130
  {                               /* saturn */
131
    8.34336671824457987e+00,
132
    4.12479856412430479e+00,
133
    -4.03523417114321381e-01,
134
    -2.76742510726862411e-03 * days_per_year,
135
    4.99852801234917238e-03 * days_per_year,
136
    2.30417297573763929e-05 * days_per_year,
137
    2.85885980666130812e-04 * solar_mass
138
  },
139
  {                               /* uranus */
140
    1.28943695621391310e+01,
141
    -1.51111514016986312e+01,
142
    -2.23307578892655734e-01,
143
    2.96460137564761618e-03 * days_per_year,
144
    2.37847173959480950e-03 * days_per_year,
145
    -2.96589568540237556e-05 * days_per_year,
146
    4.36624404335156298e-05 * solar_mass
147
  },
148
  {                               /* neptune */
149
    1.53796971148509165e+01,
150
    -2.59193146099879641e+01,
151
    1.79258772950371181e-01,
152
    2.68067772490389322e-03 * days_per_year,
153
    1.62824170038242295e-03 * days_per_year,
154
    -9.51592254519715870e-05 * days_per_year,
155
    5.15138902046611451e-05 * solar_mass
156
  }
157
};
158
 
159
int main(int argc, char ** argv)
160
{
161
  int n = atoi(argv[1]);
162
  int i;
163
 
164
  offset_momentum(NBODIES, bodies);
165
  printf ("%.9f\n", energy(NBODIES, bodies));
166
  for (i = 1; i <= n; i++)
167
    advance(NBODIES, bodies, 0.01);
168
  printf ("%.9f\n", energy(NBODIES, bodies));
169
  return 0;
170
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.