From 6af04f30c161711f53e69fbe1895ca9765e83f03 Mon Sep 17 00:00:00 2001 From: hanemile Date: Fri, 16 Nov 2018 15:51:30 +0100 Subject: wrote tests for the forces package, code-coverage: 100% :D --- forces/forces.go | 17 ++++++++++++----- main.go | 36 ++++++++++++------------------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/forces/forces.go b/forces/forces.go index d4ded26..ff67994 100644 --- a/forces/forces.go +++ b/forces/forces.go @@ -3,14 +3,13 @@ package forces import ( "fmt" "git.darknebu.la/GalaxySimulator/Source/structs" - "git.darknebu.la/bit/logplus" "gopkg.in/cheggaaa/pb.v1" "math" ) // forces_acting calculates the force in between the two given stars s1 and s2 // The function return the force -func accelerationActing(s1 structs.Star2D, s2 structs.Star2D) structs.Vec2 { +func AccelerationActing(s1 structs.Star2D, s2 structs.Star2D) structs.Vec2 { // Gravitational constant var G = 6.674E-11 @@ -21,7 +20,6 @@ func accelerationActing(s1 structs.Star2D, s2 structs.Star2D) structs.Vec2 { // the distance between the stars var deltaR = r12.GetLength() - // Define the vector components (scalar and direction) scalarA := G * (s2.M) / math.Pow(deltaR, 2) directionVectorA := r12.GetDirVector() @@ -29,6 +27,13 @@ func accelerationActing(s1 structs.Star2D, s2 structs.Star2D) structs.Vec2 { // acceleration of the star A := directionVectorA.Multiply(scalarA) + if math.IsNaN(A.X) { + A.X = 0 + } + if math.IsNaN(A.Y) { + A.Y = 0 + } + return A } @@ -45,7 +50,7 @@ func accelerations(stars_arr []structs.Star2D, nr int) structs.Vec2 { if index != nr { // calculate the acceleration and add it to the overall acceleration of the star - aa := accelerationActing(stars_arr[nr], stars_arr[index]) + aa := AccelerationActing(stars_arr[nr], stars_arr[index]) accelerationStructure = accelerationStructure.Add(aa) } @@ -84,6 +89,8 @@ func accelerationThread(starSlice []structs.Star2D, localRangeStart int, localRa // CalcAllAccelerations calculates all the accelerations acting in between all the stars in the given starSlice slice and // returns a "new" slice containing the stars with their new velocities func CalcAllAccelerations(starSlice []structs.Star2D, threads int) []structs.Star2D { + fmt.Println("Calculate all the acceletarions") + // create a channel for bundling the stars generated in the go-methods channel := make(chan structs.Star2D, 1000) @@ -96,7 +103,7 @@ func CalcAllAccelerations(starSlice []structs.Star2D, threads int) []structs.Sta // generate a new slice for storing the stars var newSlice []structs.Star2D - logplus.LogNeutral(fmt.Sprintf("Starting %d workers, each processing %d stars", threads, localRangeLen)) + //logplus.LogNeutral(fmt.Sprintf("Starting %d workers, each processing %d stars", threads, localRangeLen)) // start n go-methods each covering a part of the whole slice for i := 0; i < threads; i++ { diff --git a/main.go b/main.go index e831ef3..3d23ad2 100644 --- a/main.go +++ b/main.go @@ -4,48 +4,36 @@ import ( "fmt" "git.darknebu.la/GalaxySimulator/Source/csv" "git.darknebu.la/GalaxySimulator/Source/draw" - // "git.darknebu.la/GalaxySimulator/Source/forces" + "git.darknebu.la/GalaxySimulator/Source/forces" "git.darknebu.la/GalaxySimulator/Source/structs" - "git.darknebu.la/bit/logplus" - // "math" - "os" + "math" ) func main() { - // Define a logging level for logplus - logplus.SetLogLevel(logplus.LevelAll) - - // var threads int = 8 + var threads int = 8 var frames int = 1 var rangeStart int = 0 - - // Error handling (panic if there enouth arguments are provided) - if len(os.Args) < 2 { - panic("It seems like you forgot to supply a number of stars!") - } - rangeEnd, _ := os.Args[1] + var rangeEnd int = 1 + var path string = "data/U_ALL.csv" // the slice starsSlice stores the star structures starsSlice := []structs.Star2D{} + starsSlice = csv.Import(path, rangeStart, rangeEnd, starsSlice) - // Import data from a csv - logplus.LogNeutralf("Opening the csv") - starsSlice = csv.Import("data/U_ALL.csv", rangeStart, rangeEnd, starsSlice) + fmt.Println("Done loading the data") // Simulate frames for i := 0; i < frames; i++ { - logplus.LogPositivef("--- --- --- --- ---") - // logplus.LogPositive(fmt.Sprintf("Frames %d/%d", i, frames)) logplus.LogPositive("Done drawing the quadtree") + fmt.Println("Calculating the frame") - // logplus.LogNeutral("Calculate the new Star positions") - // starsSlice = forces.NextTimestep(starsSlice, 25*math.Pow(10, 4+7)) + starsSlice = forces.NextTimestep(starsSlice, 25*math.Pow(10, 4+7)) + starsSlice = forces.CalcAllAccelerations(starsSlice, threads) - // logplus.LogNeutral("Calculate the acting accelerations") - // starsSlice = forces.CalcAllAccelerations(starsSlice, threads) + fmt.Println("Done Calculating") // draw the galaxy outputName := fmt.Sprintf("out_%d.png", i+4) - logplus.LogNeutralf(fmt.Sprintf("draw the slice and save it to %s", outputName)) draw.Slice(starsSlice, outputName) + fmt.Println("Done drawing all the stars") } } -- cgit 1.4.1