about summary refs log tree commit diff
path: root/main.go
blob: e39e8e5e714fbbe9930a160bd5bc66f87dcd90c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main

import (
	"encoding/csv"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"math"
	"strconv"
	"strings"
	"time"
)

var (
	amountOfStars int
)

func main() {
	var tests = []int64{
		1000,
		2000,
		4000,
		8000,
		16000,
		32000,
		64000,
		128000,
	}
	data := ReadData("data/200000.csv")

	for _, element := range tests {
		data = data[:element]
		var processedStars []Star
		var cores = 8
		amountOfStars = len(data)
		processedChannel := make(chan Star, 1)

		var starsPerCore = amountOfStars / cores
		overallStartTime := time.Now()

		// start n threads processing stars
		for i := 0; i < cores; i++ {
			//fmt.Printf("Core %d processing #%d-#%d\n", i, i*starsPerCore, (i*starsPerCore)+(starsPerCore-1))

			var begin = i * starsPerCore
			var end = (i * starsPerCore) + (starsPerCore)

			go ProcessStar(data, begin, end, processedChannel)
		}

		processedChannel <- Star{}

		doneStartingThreads := time.Now()

		// get the results through the processedChannel
		for i := 0; i < amountOfStars+1; i++ {
			newStar := <-processedChannel
			processedStars = append(processedStars, newStar)
		}

		doneGettingResults := time.Now()

		threadStartTime := doneStartingThreads.Sub(overallStartTime)
		calculationTime := doneGettingResults.Sub(overallStartTime)

		fmt.Printf("--------------------------------------------------------------------------------")
		fmt.Printf("Amount of Stars: %d\n", amountOfStars)
		fmt.Printf("Amount of Threads: %d\n", cores)
		fmt.Printf("Amount of stars per thread: %d\n", starsPerCore)
		fmt.Printf("Thread start time: %f sec.\n", threadStartTime.Seconds())
		fmt.Printf("Calculation time: %f sec.\n", calculationTime.Seconds())
		fmt.Printf("Calculation time per star: %f sec.\n", calculationTime.Seconds()/float64(amountOfStars))
		fmt.Printf("Calculation per second: %f sec.\n", calculationTime.Seconds()/float64(amountOfStars))
	}

}

type Star struct {
	C, V Vec3
	M    float64
}

type Vec3 struct {
	X, Y, Z float64
}

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func ReadData(filepath string) []Star {
	// open the .csv
	dat, err := ioutil.ReadFile(filepath)
	check(err)

	// create a new csv reader
	r := csv.NewReader(strings.NewReader(string(dat)))

	var arrayOfStars []Star

	for {
		record, err := r.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}

		// parse the values
		x, parseErr := strconv.ParseFloat(record[0], 64)
		y, parseErr := strconv.ParseFloat(record[0], 64)
		z, parseErr := strconv.ParseFloat(record[0], 64)
		check(parseErr)

		// define a star
		var localStar = Star{Vec3{x, y, z}, Vec3{0, 0, 0}, 2e30}

		// append the star to the list of stars
		arrayOfStars = append(arrayOfStars, localStar)
	}

	return arrayOfStars
}

// CalcForce calculates the force exerted on s1 by s2 and returns a vector representing that force
func CalcForce(s1 Star, s2 Star) Vec3 {
	G := 6.6726 * math.Pow(10, -11)

	// calculate the force acting
	var combinedMass = s1.M * s2.M
	var distance = math.Sqrt(math.Pow(math.Abs(s1.C.X-s2.C.X), 2) + math.Pow(math.Abs(s1.C.Y-s2.C.Y), 2))

	var scalar = G * ((combinedMass) / math.Pow(distance, 2))

	// define a unit vector pointing from s1 to s2
	var vector = Vec3{s2.C.X - s1.C.X, s2.C.Y - s1.C.Y, s2.C.Z - s1.C.Z}
	var UnitVector = Vec3{vector.X / distance, vector.Y / distance, vector.Z / distance}

	// multiply the vector with the force to get a vector representing the force acting
	var force = UnitVector.Multiply(scalar)

	// return the force exerted on s1 by s2
	return force
}

// Multiply returns the product of the vector and a scalar s
func (v *Vec3) Multiply(s float64) Vec3 {
	return Vec3{v.X * s, v.Y * s, v.Z * s}
}

func ProcessStar(data []Star, begin int, end int, returnChannel chan Star) {

	// iterate over the given range of data
	for i := begin; i < end; i++ {
		localStar := data[i]
		var Force Vec3

		// iterate over all the other stars
		for index := range data {
			if index != i {

				// calculate the force acting on the star
				force := CalcForce(localStar, data[index])

				// add the force onto the overall force
				Force.X += force.X
				Force.Y += force.Y
				Force.Z += force.Z
			}
		}

		newStar := Star{
			C: Vec3{
				X: data[i].C.X,
				Y: data[i].C.Y,
				Z: data[i].C.Z,
			},
			V: Vec3{
				X: Force.X / float64(amountOfStars),
				Y: Force.Y / float64(amountOfStars),
				Z: Force.Z / float64(amountOfStars),
			},
			M: data[i].M,
		}

		returnChannel <- newStar
	}

	return
}