-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_corepos.go
59 lines (49 loc) · 1.36 KB
/
ext_corepos.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
package engine
var CorePos = NewGetVector("ext_corepos", "m", "Vortex core position (x,y) + polarization (z)", corePos)
func corePos() []float64 {
m, _ := M.Slice()
m_z := m.Comp(Z).HostCopy().Scalars()
s := m.Size()
Nx, Ny, Nz := s[X], s[Y], s[Z]
max := float32(-1.0)
var maxX, maxY, maxZ int
for z := 0; z < Nz; z++ {
// Avoid the boundaries so the neighbor interpolation can't go out of bounds.
for y := 1; y < Ny-1; y++ {
for x := 1; x < Nx-1; x++ {
m := abs(m_z[z][y][x])
if m > max {
maxX, maxY, maxZ = x, y, z
max = m
}
}
}
}
pos := make([]float64, 3)
mz := m_z[maxZ]
// sub-cell interpolation in X and Y, but not Z
pos[X] = float64(maxX) + interpolate_maxpos(
max, -1, abs(mz[maxY][maxX-1]), 1, abs(mz[maxY][maxX+1])) -
float64(Nx)/2 + 0.5
pos[Y] = float64(maxY) + interpolate_maxpos(
max, -1, abs(mz[maxY-1][maxX]), 1, abs(mz[maxY+1][maxX])) -
float64(Ny)/2 + 0.5
c := Mesh().CellSize()
pos[X] *= c[X]
pos[Y] *= c[Y]
pos[Z] = float64(m_z[maxZ][maxY][maxX]) // 3rd coordinate is core polarization
pos[X] += GetShiftPos() // add simulation window shift
return pos
}
func interpolate_maxpos(f0, d1, f1, d2, f2 float32) float64 {
b := (f2 - f1) / (d2 - d1)
a := ((f2-f0)/d2 - (f0-f1)/(-d1)) / (d2 - d1)
return float64(-b / (2 * a))
}
func abs(x float32) float32 {
if x > 0 {
return x
} else {
return -x
}
}