-
Notifications
You must be signed in to change notification settings - Fork 1
/
mesh.go
123 lines (104 loc) · 3.17 KB
/
mesh.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
package engine
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
"github.com/mumax/3/util"
)
var globalmesh_ data.Mesh // mesh for m and everything that has the same size
func init() {
DeclFunc("SetGridSize", SetGridSize, `Sets the number of cells for X,Y,Z`)
DeclFunc("SetCellSize", SetCellSize, `Sets the X,Y,Z cell size in meters`)
DeclFunc("SetPBC", SetPBC, `Sets number of repetitions in X,Y,Z`)
DeclFunc("SetMesh", SetMesh, `Sets GridSize, CellSize and PBC in once`)
}
func Mesh() *data.Mesh {
checkMesh()
return &globalmesh_
}
func arg(msg string, test bool) {
if !test {
panic(UserErr(msg + ": illegal arugment"))
}
}
// Set the simulation mesh to Nx x Ny x Nz cells of given size.
// Can be set only once at the beginning of the simulation.
// TODO: dedup arguments from globals
func SetMesh(Nx, Ny, Nz int, cellSizeX, cellSizeY, cellSizeZ float64, pbcx, pbcy, pbcz int) {
SetBusy(true)
defer SetBusy(false)
arg("GridSize", Nx > 0 && Ny > 0 && Nz > 0)
arg("CellSize", cellSizeX > 0 && cellSizeY > 0 && cellSizeZ > 0)
arg("PBC", pbcx >= 0 && pbcy >= 0 && pbcz >= 0)
prevSize := globalmesh_.Size()
pbc := []int{pbcx, pbcy, pbcz}
if globalmesh_.Size() == [3]int{0, 0, 0} {
// first time mesh is set
globalmesh_ = *data.NewMesh(Nx, Ny, Nz, cellSizeX, cellSizeY, cellSizeZ, pbc...)
M.alloc()
regions.alloc()
} else {
// here be dragons
LogOut("resizing...")
// free everything
conv_.Free()
conv_ = nil
mfmconv_.Free()
mfmconv_ = nil
cuda.FreeBuffers()
// resize everything
globalmesh_ = *data.NewMesh(Nx, Ny, Nz, cellSizeX, cellSizeY, cellSizeZ, pbc...)
M.resize()
regions.resize()
geometry.buffer.Free()
geometry.buffer = data.NilSlice(1, Mesh().Size())
geometry.setGeom(geometry.shape)
// remove excitation extra terms if they don't fit anymore
// up to the user to add them again
if Mesh().Size() != prevSize {
B_ext.RemoveExtraTerms()
J.RemoveExtraTerms()
}
if Mesh().Size() != prevSize {
B_therm.noise.Free()
B_therm.noise = nil
}
}
lazy_gridsize = []int{Nx, Ny, Nz}
lazy_cellsize = []float64{cellSizeX, cellSizeY, cellSizeZ}
lazy_pbc = []int{pbcx, pbcy, pbcz}
}
func printf(f float64) float32 {
return float32(f)
}
// for lazy setmesh: set gridsize and cellsize in separate calls
var (
lazy_gridsize []int
lazy_cellsize []float64
lazy_pbc = []int{0, 0, 0}
)
func SetGridSize(Nx, Ny, Nz int) {
lazy_gridsize = []int{Nx, Ny, Nz}
if lazy_cellsize != nil {
SetMesh(Nx, Ny, Nz, lazy_cellsize[X], lazy_cellsize[Y], lazy_cellsize[Z], lazy_pbc[X], lazy_pbc[Y], lazy_pbc[Z])
}
}
func SetCellSize(cx, cy, cz float64) {
lazy_cellsize = []float64{cx, cy, cz}
if lazy_gridsize != nil {
SetMesh(lazy_gridsize[X], lazy_gridsize[Y], lazy_gridsize[Z], cx, cy, cz, lazy_pbc[X], lazy_pbc[Y], lazy_pbc[Z])
}
}
func SetPBC(nx, ny, nz int) {
lazy_pbc = []int{nx, ny, nz}
if lazy_gridsize != nil && lazy_cellsize != nil {
SetMesh(lazy_gridsize[X], lazy_gridsize[Y], lazy_gridsize[Z],
lazy_cellsize[X], lazy_cellsize[Y], lazy_cellsize[Z],
lazy_pbc[X], lazy_pbc[Y], lazy_pbc[Z])
}
}
// check if mesh is set
func checkMesh() {
if globalmesh_.Size() == [3]int{0, 0, 0} {
util.Fatal("need to set mesh first")
}
}