-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregions.go
186 lines (159 loc) · 4.29 KB
/
regions.go
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
package engine
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
"github.com/mumax/3/util"
"log"
)
var regions = Regions{info: Info(1, "regions", "")} // global regions map
const NREGION = 256 // maximum number of regions. (!) duplicated in CUDA
func init() {
DeclFunc("DefRegion", DefRegion, "Define a material region with given index (0-255) and shape")
//DeclFunc("DefRegionCell", DefRegionCell, "Set a material region in one cell by index")
DeclROnly("regions", ®ions, "Outputs the region index for each cell")
}
// stores the region index for each cell
type Regions struct {
gpuCache *cuda.Bytes // TODO: rename: buffer
deflist []regionHist // history
info
}
// keeps history of region definitions
type regionHist struct {
region int
shape Shape
}
func (r *Regions) alloc() {
mesh := r.Mesh()
r.gpuCache = cuda.NewBytes(mesh.NCell())
DefRegion(0, universe)
}
func (r *Regions) resize() {
newSize := Mesh().Size()
r.gpuCache.Free()
r.gpuCache = cuda.NewBytes(prod(newSize))
for _, d := range r.deflist {
r.render(d.region, d.shape)
}
}
// Define a region with id (0-255) to be inside the Shape.
func DefRegion(id int, s Shape) {
defRegionId(id)
regions.render(id, s)
regions.deflist = append(regions.deflist, regionHist{id, s})
}
// renders (rasterizes) shape, filling it with region number #id, between x1 and x2
func (r *Regions) render(id int, s Shape) {
n := Mesh().Size()
cpu := make([]byte, Mesh().NCell())
arr := reshapeBytes(cpu, n)
for iz := 0; iz < n[Z]; iz++ {
for iy := 0; iy < n[Y]; iy++ {
for ix := 0; ix < n[X]; ix++ {
r := Index2Coord(ix, iy, iz)
if s(r[X], r[Y], r[Z]) { // inside
arr[iz][iy][ix] = byte(id)
}
}
}
}
log.Print("regions.upload")
r.gpuCache.Upload(cpu)
}
func (r *Regions) get(R data.Vector) int {
for i := len(r.deflist) - 1; i >= 0; i-- {
d := r.deflist[i]
if d.shape(R[X], R[Y], R[Z]) {
return d.region
}
}
return 0
}
// TODO: re-enable
//func DefRegionCell(id int, x, y, z int) {
// defRegionId(id)
// regions.arr[z][y][x] = byte(id)
//}
func defRegionId(id int) {
if id < 0 || id > NREGION {
util.Fatalf("region id should be 0 -%v, have: %v", NREGION, id)
}
checkMesh()
}
// normalized volume (0..1) of region.
func (r *Regions) volume(region int) float64 {
panic("todo: region volume")
//vol := 0
//reg := byte(region)
//for _, c := range r.cpu {
// if c == reg {
// vol++
// }
//}
//return float64(vol) / float64(r.Mesh().NCell())
}
// Set the region of one cell
func (r *Regions) SetCell(ix, iy, iz int, region int) {
size := Mesh().Size()
i := data.Index(size, ix, iy, iz)
r.gpuCache.Set(i, byte(region))
}
// Get the region data on GPU
func (r *Regions) Gpu() *cuda.Bytes {
return r.gpuCache
}
var unitMap inputParam // unit map used to output regions quantity
func init() {
unitMap.init(1, "unit", "", nil)
for r := 0; r < NREGION; r++ {
unitMap.setRegion(r, []float64{float64(r)})
}
}
// Get returns the regions as a slice of floats, so it can be output.
func (r *Regions) Slice() (*data.Slice, bool) {
buf := cuda.Buffer(1, r.Mesh().Size())
cuda.RegionDecode(buf, unitMap.gpuLUT1(), regions.Gpu())
return buf, true
}
// Re-interpret a contiguous array as a multi-dimensional array of given size.
func reshapeBytes(array []byte, size [3]int) [][][]byte {
Nx, Ny, Nz := size[X], size[Y], size[Z]
util.Argument(Nx*Ny*Nz == len(array))
sliced := make([][][]byte, Nz)
for i := range sliced {
sliced[i] = make([][]byte, Ny)
}
for i := range sliced {
for j := range sliced[i] {
sliced[i][j] = array[(i*Ny+j)*Nx+0 : (i*Ny+j)*Nx+Nx]
}
}
return sliced
}
func (b *Regions) shift(dx int) {
// TODO: return if no regions defined
log.Println("regionshift", dx)
r1 := b.Gpu()
r2 := cuda.NewBytes(b.Mesh().NCell()) // TODO: somehow recycle
defer r2.Free()
newreg := byte(0) // new region at edge
cuda.ShiftBytes(r2, r1, b.Mesh(), dx, newreg)
r1.Copy(r2)
n := Mesh().Size()
x1, x2 := shiftDirtyRange(dx)
for iz := 0; iz < n[Z]; iz++ {
for iy := 0; iy < n[Y]; iy++ {
for ix := x1; ix < x2; ix++ {
r := Index2Coord(ix, iy, iz) // includes shift
reg := b.get(r)
if reg != 0 {
b.SetCell(ix, iy, iz, reg) // a bit slowish, but hardly reached
}
}
}
}
}
func (r *Regions) Mesh() *data.Mesh { return Mesh() }
func prod(s [3]int) int {
return s[0] * s[1] * s[2]
}