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.go] - 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
/* The Computer Language Benchmarks Game
31
 * http://shootout.alioth.debian.org/
32
 *
33
 * contributed by The Go Authors.
34
 * based on C program by Christoph Bauer
35
 */
36
 
37
package main
38
 
39
import (
40
        "flag"
41
        "fmt"
42
        "math"
43
)
44
 
45
var n = flag.Int("n", 1000, "number of iterations")
46
 
47
type Body struct {
48
        x, y, z, vx, vy, vz, mass float64
49
}
50
 
51
const (
52
        solarMass   = 4 * math.Pi * math.Pi
53
        daysPerYear = 365.24
54
)
55
 
56
func (b *Body) offsetMomentum(px, py, pz float64) {
57
        b.vx = -px / solarMass
58
        b.vy = -py / solarMass
59
        b.vz = -pz / solarMass
60
}
61
 
62
type System []*Body
63
 
64
func NewSystem(body []Body) System {
65
        n := make(System, len(body))
66
        for i := 0; i < len(body); i++ {
67
                n[i] = new(Body) // copy to avoid overwriting the inputs
68
                *n[i] = body[i]
69
        }
70
        var px, py, pz float64
71
        for _, body := range n {
72
                px += body.vx * body.mass
73
                py += body.vy * body.mass
74
                pz += body.vz * body.mass
75
        }
76
        n[0].offsetMomentum(px, py, pz)
77
        return n
78
}
79
 
80
func (sys System) energy() float64 {
81
        var e float64
82
        for i, body := range sys {
83
                e += 0.5 * body.mass *
84
                        (body.vx*body.vx + body.vy*body.vy + body.vz*body.vz)
85
                for j := i + 1; j < len(sys); j++ {
86
                        body2 := sys[j]
87
                        dx := body.x - body2.x
88
                        dy := body.y - body2.y
89
                        dz := body.z - body2.z
90
                        distance := math.Sqrt(dx*dx + dy*dy + dz*dz)
91
                        e -= (body.mass * body2.mass) / distance
92
                }
93
        }
94
        return e
95
}
96
 
97
func (sys System) advance(dt float64) {
98
        for i, body := range sys {
99
                for j := i + 1; j < len(sys); j++ {
100
                        body2 := sys[j]
101
                        dx := body.x - body2.x
102
                        dy := body.y - body2.y
103
                        dz := body.z - body2.z
104
 
105
                        dSquared := dx*dx + dy*dy + dz*dz
106
                        distance := math.Sqrt(dSquared)
107
                        mag := dt / (dSquared * distance)
108
 
109
                        body.vx -= dx * body2.mass * mag
110
                        body.vy -= dy * body2.mass * mag
111
                        body.vz -= dz * body2.mass * mag
112
 
113
                        body2.vx += dx * body.mass * mag
114
                        body2.vy += dy * body.mass * mag
115
                        body2.vz += dz * body.mass * mag
116
                }
117
        }
118
 
119
        for _, body := range sys {
120
                body.x += dt * body.vx
121
                body.y += dt * body.vy
122
                body.z += dt * body.vz
123
        }
124
}
125
 
126
var (
127
        jupiter = Body{
128
                x:    4.84143144246472090e+00,
129
                y:    -1.16032004402742839e+00,
130
                z:    -1.03622044471123109e-01,
131
                vx:   1.66007664274403694e-03 * daysPerYear,
132
                vy:   7.69901118419740425e-03 * daysPerYear,
133
                vz:   -6.90460016972063023e-05 * daysPerYear,
134
                mass: 9.54791938424326609e-04 * solarMass,
135
        }
136
        saturn = Body{
137
                x:    8.34336671824457987e+00,
138
                y:    4.12479856412430479e+00,
139
                z:    -4.03523417114321381e-01,
140
                vx:   -2.76742510726862411e-03 * daysPerYear,
141
                vy:   4.99852801234917238e-03 * daysPerYear,
142
                vz:   2.30417297573763929e-05 * daysPerYear,
143
                mass: 2.85885980666130812e-04 * solarMass,
144
        }
145
        uranus = Body{
146
                x:    1.28943695621391310e+01,
147
                y:    -1.51111514016986312e+01,
148
                z:    -2.23307578892655734e-01,
149
                vx:   2.96460137564761618e-03 * daysPerYear,
150
                vy:   2.37847173959480950e-03 * daysPerYear,
151
                vz:   -2.96589568540237556e-05 * daysPerYear,
152
                mass: 4.36624404335156298e-05 * solarMass,
153
        }
154
        neptune = Body{
155
                x:    1.53796971148509165e+01,
156
                y:    -2.59193146099879641e+01,
157
                z:    1.79258772950371181e-01,
158
                vx:   2.68067772490389322e-03 * daysPerYear,
159
                vy:   1.62824170038242295e-03 * daysPerYear,
160
                vz:   -9.51592254519715870e-05 * daysPerYear,
161
                mass: 5.15138902046611451e-05 * solarMass,
162
        }
163
        sun = Body{
164
                mass: solarMass,
165
        }
166
)
167
 
168
func main() {
169
        flag.Parse()
170
 
171
        system := NewSystem([]Body{sun, jupiter, saturn, uranus, neptune})
172
        fmt.Printf("%.9f\n", system.energy())
173
        for i := 0; i < *n; i++ {
174
                system.advance(0.01)
175
        }
176
        fmt.Printf("%.9f\n", system.energy())
177
}

powered by: WebSVN 2.1.0

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