-
Notifications
You must be signed in to change notification settings - Fork 1
/
mesh.go
208 lines (178 loc) · 6 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package engine
import (
"github.com/kuchkin/mumax3-gneb/cuda"
"github.com/kuchkin/mumax3-gneb/data"
)
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("SetGNEB", SetGNEB, `Sets the dimensions of images`)
DeclFunc("SetImagesNumber", SetImagesNumber, `Sets the number of images `)
DeclFunc("SetMesh", SetMesh, `Sets GridSize, CellSize and PBC at the same time`)
DeclFunc("SetMeshGNEB", SetMeshGNEB, `Sets GridSize, CellSize, PBC and Gneb parameters at the same time`)
DeclFunc("SetPBC", SetPBC, "Sets the number of repetitions in X,Y,Z to create periodic boundary "+
"conditions. The number of repetitions determines the cutoff range for the demagnetization.")
}
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 SetMeshGNEB(Nx, Ny, Nz, noi int, cellSizeX, cellSizeY, cellSizeZ float64, pbcx, pbcy, pbcz, gneb2D, gneb3D 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)
arg("GNEB", gneb2D >= 0 && gneb3D >= 0)
prevSize := globalmesh_.Size()
pbc := []int{pbcx, pbcy, pbcz}
// gneb := []int{gneb2D, gneb3D}
if globalmesh_.Size() == [3]int{0, 0, 0} {
// first time mesh is set
globalmesh_ = *data.NewMeshGneb(Nx, Ny, Nz, noi, gneb2D, gneb3D, 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.NewMeshGneb(Nx, Ny, Nz, noi, gneb2D, gneb3D, 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}
lazy_gneb = []int{gneb2D, gneb3D}
}
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}
lazy_imsize = []int{1}
lazy_gneb = []int{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])
}
}
func SetGNEB(gneb2D, gneb3D int) {
lazy_gneb = []int{gneb2D, gneb3D}
if lazy_gridsize != nil && lazy_cellsize != nil {
SetMeshGNEB(lazy_gridsize[X], lazy_gridsize[Y], lazy_gridsize[Z], lazy_imsize[0],
lazy_cellsize[X], lazy_cellsize[Y], lazy_cellsize[Z],
lazy_pbc[X], lazy_pbc[Y], lazy_pbc[Z], lazy_gneb[X], lazy_gneb[Y])
}
}
func SetImagesNumber(Noi int) {
lazy_imsize = []int{Noi}
if lazy_gridsize != nil && lazy_cellsize != nil {
SetMeshGNEB(lazy_gridsize[X], lazy_gridsize[Y], lazy_gridsize[Z], lazy_imsize[0],
lazy_cellsize[X], lazy_cellsize[Y], lazy_cellsize[Z],
lazy_pbc[X], lazy_pbc[Y], lazy_pbc[Z], lazy_gneb[X], lazy_gneb[Y])
}
}
// check if mesh is set
func checkMesh() {
if globalmesh_.Size() == [3]int{0, 0, 0} {
panic("need to set mesh first")
}
// if(lazy_imsize[0]>globalmesh_.Size()[2]){
// panic("Number of images can not be bigger than Nz")
// }
// if((globalmesh_.Size()[2])%lazy_imsize[0] != 0){
// panic("Size of each image has to be an integer value. Check Nz/NumberOfImages")
// }
}