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