about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2018-11-20 18:59:47 +0100
committerhanemile <hanemile@protonmail.com>2018-11-20 18:59:47 +0100
commitd768f93edc348e5baa736f4c67c9b4eb0dbc2f94 (patch)
tree269012fa41cca5f485928cfff760de1db38bfd52
parent6af04f30c161711f53e69fbe1895ca9765e83f03 (diff)
Now using a config File to define the values, if no config is provided, the defaults that can be set in the LoadConfiguration function are used.
-rw-r--r--config.json7
-rw-r--r--main.go56
2 files changed, 55 insertions, 8 deletions
diff --git a/config.json b/config.json
new file mode 100644
index 0000000..77884b9
--- /dev/null
+++ b/config.json
@@ -0,0 +1,7 @@
+{
+  "Frames": 1,
+  "RangeStart": 0,
+  "RangeEnd": 1000,
+  "LoadPath": "data/U_ALL.csv",
+  "OutPath": "out_%d.csv"
+}
\ No newline at end of file
diff --git a/main.go b/main.go
index 3d23ad2..bd243a0 100644
--- a/main.go
+++ b/main.go
@@ -1,33 +1,48 @@
 package main
 
 import (
+	"encoding/json"
 	"fmt"
 	"git.darknebu.la/GalaxySimulator/Source/csv"
 	"git.darknebu.la/GalaxySimulator/Source/draw"
 	"git.darknebu.la/GalaxySimulator/Source/forces"
 	"git.darknebu.la/GalaxySimulator/Source/structs"
 	"math"
+	"os"
 )
 
+type Config struct {
+	Threads    int    `json:"Threads"`
+	Frames     int    `json:"Frames"`
+	RangeStart int    `json:"RangeStart"`
+	RangeEnd   int    `json:"RangeEnd"`
+	LoadPath   string `json:"LoadPath"`
+	OutPath    string `json:"OutPath"`
+}
+
 func main() {
-	var threads int = 8
-	var frames int = 1
-	var rangeStart int = 0
-	var rangeEnd int = 1
-	var path string = "data/U_ALL.csv"
+	// Load the config
+	var config Config = LoadConfiguration("config.json")
+
+	fmt.Printf("[+] Utilizing %d threads ", config.Threads)
+	fmt.Printf("for drawing %d Frames, ", config.Frames)
+	fmt.Printf("each containing %d Stars.\n", config.RangeEnd)
+
+	fmt.Printf("[+] Getting previously existing Stars from %s ", config.LoadPath)
+	fmt.Printf("and writing the results to %s.\n", config.OutPath)
 
 	// the slice starsSlice stores the star structures
 	starsSlice := []structs.Star2D{}
-	starsSlice = csv.Import(path, rangeStart, rangeEnd, starsSlice)
+	starsSlice = csv.Import(config.LoadPath, config.RangeStart, config.RangeEnd, starsSlice)
 
 	fmt.Println("Done loading the data")
 
 	// Simulate frames
-	for i := 0; i < frames; i++ {
+	for i := 0; i < config.Frames; i++ {
 		fmt.Println("Calculating the frame")
 
 		starsSlice = forces.NextTimestep(starsSlice, 25*math.Pow(10, 4+7))
-		starsSlice = forces.CalcAllAccelerations(starsSlice, threads)
+		starsSlice = forces.CalcAllAccelerations(starsSlice, config.Threads)
 
 		fmt.Println("Done Calculating")
 
@@ -37,3 +52,28 @@ func main() {
 		fmt.Println("Done drawing all the stars")
 	}
 }
+
+// LoadConfiguration loads a configuration file from a given path and returns a struct with
+// the values that are defined inside of the configuration file.
+func LoadConfiguration(file string) Config {
+
+	// Define some config defaults
+	var config = Config{
+		Threads:    1,
+		Frames:     1,
+		RangeStart: 0,
+		RangeEnd:   1,
+		OutPath:    "",
+	}
+
+	// Reading the config file and closing when done
+	configFile, _ := os.Open(file)
+	defer configFile.Close()
+
+	// Parsing the content and adding it to the config struct
+	jsonParser := json.NewDecoder(configFile)
+	jsonParser.Decode(&config)
+
+	// Returning the config for further use
+	return config
+}