Skip to content

Commit 06d614e

Browse files
committed
goroutines
1 parent fef8ed2 commit 06d614e

File tree

16 files changed

+10386
-10327
lines changed

16 files changed

+10386
-10327
lines changed

pkg/sumdb/sum.golang.org/latest

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
go.sum database tree
2-
1464884
3-
SlduoaOAa2h+kG0FhoGf0liamIkYA9sCe/Ly5E32rvo=
2+
1499078
3+
NhGB7d0P5DpXsHvi4rFrUSac2bzweWcc+7XLZWeZwJ4=
44

5-
— sum.golang.org Az3grhuKBElG+JPcIyDHChadtxED2FTwUypIHwuJeqhkLunXbcM7WkEjb8Q+/KAOowGs6fKdXrD3pTKECovq2nlALAg=
5+
— sum.golang.org Az3grpqNeOVgU90vaqupg0E14F+6CMGdA5QVkgXSST3OUK0WBxzNOJvgSrxN5pDS/MvatyD9M+KacSOw/hOaWL5B1Ac=

src/cmd/forest/main.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
package main
22

33
import (
4+
"log"
45
"simulation/forest"
56
"simulation/shared"
7+
"time"
68
)
79

810
func main() {
9-
p1 := shared.Coord{Lat: 38.908577, Lon: -9.400535, Alt: 0.0} //38.914104, -9.400535
10-
p2 := shared.Coord{Lat: 38.914104, Lon: -9.384442, Alt: 0.0} //38.908577, -9.384442
11+
p1 := shared.Coord{Lat: 38.848577, Lon: -9.410535, Alt: 0.0} //38.944104, -9.400535
12+
p2 := shared.Coord{Lat: 38.924104, Lon: -9.354442, Alt: 0.0} //38.908577, -9.364442
1113

12-
f := forest.ForestGeneration(p1, p2, 100)
14+
f := forest.ForestGeneration(p1, p2, 100, 1.0)
15+
t0 := time.Now()
16+
f.GetNeighbours(1.3)
17+
t1 := time.Since(t0)
18+
log.Printf("serial took %s", t1)
19+
20+
t2 := time.Now()
21+
f.DistributedNeighbour(1.3)
1322

1423
for i := 0; i < 36; i++ {
1524
f.RecordFrame()
1625
}
26+
1727
f.Plot_forest()
28+
t3 := time.Since(t2)
29+
log.Printf("parallel took %s", t3)
1830
}

src/cmd/run/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"simulation/forest"
6+
"simulation/shared"
7+
)
8+
9+
func main() {
10+
p1 := shared.Coord{Lat: 38.793613, Lon: -9.453429, Alt: 0.0}
11+
p2 := shared.Coord{Lat: 38.813257, Lon: -9.434138, Alt: 0.0}
12+
13+
// t := terrain.GenerateTerrain(p1, p2, 500)
14+
// terrain.CallPythonScripts(p1, p2, 0, "structures")
15+
16+
samples := 10000
17+
dist := 1.0
18+
f := forest.ForestGeneration(p1, p2, samples, dist)
19+
if false {
20+
fmt.Println(f)
21+
}
22+
23+
}

src/cmd/testing/main.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/simulation/forest/forest.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package forest
22

33
import (
4-
"fmt"
54
"math"
65

76
"math/rand"
@@ -42,7 +41,7 @@ func (p TreeCoord) Distance(c vptree.Comparable) float64 {
4241
return math.Sqrt(math.Pow(Dxy, 2) + math.Pow(Dz, 2))
4342
}
4443

45-
func ForestGeneration(p1, p2 shared.Coord, samples int) (f Forest) {
44+
func ForestGeneration(p1, p2 shared.Coord, samples int, dist float64) (f Forest) {
4645
rand.Seed(1999)
4746
t := terrain.GenerateTerrain(p1, p2, samples) // terrain should not be controlled by samples, but instead by SRTM resolution
4847

@@ -94,29 +93,12 @@ func ForestGeneration(p1, p2 shared.Coord, samples int) (f Forest) {
9493
}
9594

9695
f.DetermineSample(10000)
97-
f.GetNeighbours(3.0)
96+
9897
f.RecordFrame()
9998

10099
return f
101100
}
102101

103-
func (f *Forest) GetNeighbours(d float64) {
104-
// handle vp, err :=....
105-
VP, _ := vptree.New(f.Tree_Coords, 0, nil)
106-
fmt.Println("VP found")
107-
108-
for i, q := range f.Tree_Coords {
109-
var keep vptree.Keeper
110-
keep = vptree.NewDistKeeper(d)
111-
VP.NearestSet(keep, q)
112-
113-
for _, neighbour_tree := range keep.(*vptree.DistKeeper).Heap {
114-
tree := neighbour_tree.Comparable.(TreeCoord)
115-
f.Tree_lst[i].Neighbours = append(f.Tree_lst[i].Neighbours, tree.ID)
116-
}
117-
}
118-
}
119-
120102
func (f *Forest) DetermineSample(size int) {
121103
selected := make(map[int]bool, size)
122104

src/simulation/forest/neighbours.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package forest
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
7+
"gonum.org/v1/gonum/spatial/vptree"
8+
)
9+
10+
const workerNum = 4
11+
12+
// GetNeighbours finds trees closer than a distance d
13+
func (f *Forest) GetNeighbours(d float64) {
14+
// handle vp, err :=....
15+
VP, _ := vptree.New(f.Tree_Coords, 0, nil)
16+
fmt.Println("VP found")
17+
18+
for i, q := range f.Tree_Coords {
19+
var keep vptree.Keeper
20+
keep = vptree.NewDistKeeper(d)
21+
VP.NearestSet(keep, q)
22+
23+
for _, neighbourTree := range keep.(*vptree.DistKeeper).Heap {
24+
tree := neighbourTree.Comparable.(TreeCoord)
25+
f.Tree_lst[i].Neighbours = append(f.Tree_lst[i].Neighbours, tree.ID)
26+
}
27+
}
28+
fmt.Println("Neighbours found")
29+
}
30+
31+
// DistributedNeighbour finds adjacent trees over const workerNum of cores
32+
func (f *Forest) DistributedNeighbour(d float64) {
33+
// VP, _ := vptree.New(f.Tree_Coords, 0, nil)
34+
fmt.Println("VP found")
35+
36+
wg := sync.WaitGroup{}
37+
for i := 0; i < workerNum; i++ {
38+
wg.Add(1)
39+
go f.run(i, 3.0, &wg)
40+
}
41+
42+
}
43+
44+
func (f *Forest) run(worker int, d float64, wg *sync.WaitGroup) {
45+
VP, _ := vptree.New(f.Tree_Coords, 0, nil)
46+
47+
for i, q := range f.Tree_Coords {
48+
if i%workerNum != worker {
49+
continue
50+
}
51+
52+
var keep vptree.Keeper
53+
keep = vptree.NewDistKeeper(d)
54+
VP.NearestSet(keep, q)
55+
56+
for _, neighbourTree := range keep.(*vptree.DistKeeper).Heap {
57+
tree := neighbourTree.Comparable.(TreeCoord)
58+
f.Tree_lst[i].Neighbours = append(f.Tree_lst[i].Neighbours, tree.ID)
59+
}
60+
}
61+
// let the wait group know we finished
62+
wg.Done()
63+
64+
}

src/simulation/fuel/shrub.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package fuel
22

3-
type shrub_static struct {
4-
height float32
5-
surface_area float32
3+
type shrubStatic struct {
4+
height float32
5+
surfaceArea float32
66
}
77

8-
type shrub_dynamic struct {
8+
type shrubDynamic struct {
99
moisture float32
1010
state string
1111
}

src/simulation/fuel/tree.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,32 @@ type Tree_dynamic struct {
2323
Flame fire.Flame
2424
}
2525

26+
// Tree_data holds static and dynamic information about each tree
2627
type Tree_data struct {
2728
ID int
2829
species string
2930
Coords s.Coord
3031
Static Tree_static
3132
Dynamic Tree_dynamic
3233

33-
North_facing string
34-
Neighbours []int
34+
Aspect string
35+
Neighbours []int
3536
}
3637

37-
func CreateTree(id int, p s.Coord, species string, tree_db map[string][5]float64) Tree_data {
38+
// CreateTree generates a tree based on tree_db details
39+
func CreateTree(id int, p s.Coord, species string, treeDB map[string][5]float64) Tree_data {
3840
t := Tree_data{ID: id}
3941
t.species = species
4042
t.Coords = s.Coord{Lat: p.Lat, Lon: p.Lon, Alt: p.Alt}
4143

42-
t.initStatic(tree_db)
44+
t.initStatic(treeDB)
4345
t.initBiomass()
46+
t.Dynamic.State = "tree"
4447
return t
4548
}
4649

47-
func (t *Tree_data) initStatic(tree_db map[string][5]float64) {
48-
dims := tree_db[t.species]
50+
func (t *Tree_data) initStatic(treeDB map[string][5]float64) {
51+
dims := treeDB[t.species]
4952

5053
t.Static.Height = dims[0]
5154
t.Static.DiameterBreastHeight = dims[1]
@@ -63,14 +66,14 @@ func (t *Tree_data) initBiomass() {
6366
t.Dynamic.Trunk = t.Static.Height * math.Pi * math.Pow(t.Static.DiameterBreastHeight, 2) / 4.0
6467
t.Dynamic.Canopy = (0.5 * t.Static.Height) * (math.Pi * math.Pow(t.Static.CrownRadius, 2) / 4.0) * t.Static.SparsenessFactor
6568

66-
t.Dynamic.State = "tree"
6769
}
6870

71+
// UpdateMoisture controls the water content depending on the temperature of the surrounding temperature
6972
func (t *Tree_data) UpdateMoisture(temperature float64) {
70-
temperature_diff := temperature - 25.0 // 25ºC. This would be an equilibirum point where no water transfers occur
73+
temperatureDiff := temperature - 25.0 // 25ºC. This would be an equilibirum point where no water transfers occur
7174
diff := 0.0
72-
if temperature_diff > 0 {
73-
diff = 0.01 * temperature_diff
75+
if temperatureDiff > 0 {
76+
diff = 0.01 * temperatureDiff
7477
} else {
7578
diff = 0
7679
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package interactions
2+
3+
// // TreeImport fuel.Tree_data
4+
// type TreeImport fuel.Tree_data
5+
6+
// UpdateMoisture controls the water content depending on the temperature of the surrounding temperature
7+
func (t *TreeImport) UpdateMoisture(temperature float64) {
8+
temperatureDiff := temperature - 25.0 // 25ºC. This would be an equilibirum point where no water transfers occur
9+
diff := 0.0
10+
if temperatureDiff > 0 {
11+
diff = 0.01 * temperatureDiff
12+
} else {
13+
diff = 0
14+
}
15+
t.Dynamic.Moisture = t.Dynamic.Moisture - (diff / t.Static.BarkThickness)
16+
}

0 commit comments

Comments
 (0)