Skip to content

Commit d7a9b9a

Browse files
fixifixi (#6)
1 parent 3a3d604 commit d7a9b9a

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v0.1.10
2+
- improve docstrings
3+
- fix bad sign of torque
4+
15
# v0.1.9
26
- fix docstrings
37
- bugfix remove pint dependency from development

magpylib_force/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
# module level dunders
8-
__version__ = "0.1.9"
8+
__version__ = "0.1.10"
99
__author__ = "SAL"
1010
__all__ = [
1111
"getFT",

magpylib_force/force.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
def getFT(sources, targets, anchor=None, eps=1e-5, squeeze=True):
1313
"""
14-
Compute force and torque between sources and targets.
14+
Compute magnetic force and torque acting on the targets that are exposed
15+
to the magnetic field of the sources.
1516
SI units are assumed for all inputs and outputs.
1617
1718
Parameters
@@ -181,7 +182,7 @@ def getFTmagnet(sources, targets, eps=1e-5, anchor=None):
181182
T = np.array([np.sum(Ts[insti[i]:insti[i+1]],axis=0) for i in range(tgt_number)])
182183
F = np.array([np.sum(Fs[insti[i]:insti[i+1]],axis=0) for i in range(tgt_number)])
183184

184-
return np.array((F, T))
185+
return np.array((F, -T))
185186

186187

187188

@@ -330,4 +331,4 @@ def getFTcurrent(sources, targets, anchor=None, eps=None):
330331
# sumup force for every target
331332
F = np.array([np.sum(F[insti[i]:insti[i+1]],axis=0) for i in range(tgt_number)])
332333

333-
return np.array((F, T))
334+
return np.array((F, -T))

tests/test_FEM.py

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_ANSYS_cube_cube():
3535
for i,poz in enumerate(tgt_pos):
3636
tgt.position = poz
3737
F,T = getFT(gen, tgt)
38+
T *= -1 #bad sign in original implementation
3839

3940
errF = np.linalg.norm(F - F_fe[i])/np.linalg.norm(F)
4041
assert errF < 0.04
@@ -144,7 +145,9 @@ def test_ANSYS_loop_magnet():
144145
loop.meshing = 1000
145146
magnet.meshing=(10,20,10)
146147
F3,T3 = getFT(sources=loop, targets=magnet, anchor=(0,0,0))
148+
T3*=-1 #bad sign at initial test design
147149
F4,T4 = getFT(sources=magnet, targets=loop, anchor=(0,0,0))
150+
T4*=-1 #bad sign at initial test design
148151
F3 *= 1e3
149152
F4 *= 1e3
150153
T3 *= 1e6
@@ -229,6 +232,7 @@ def test_ANSYS_magnet_current_close():
229232

230233
F1,_ = getFT(wires, magnet, anchor=(0,0,0))
231234
F2,T2 = np.sum(getFT(magnet, wires, anchor=(0,0,0)), axis=0)
235+
T2*=-1 #bad sign at initial test design
232236

233237
assert np.linalg.norm(F1+F2)/np.linalg.norm(F1) < 1e-3
234238
assert np.linalg.norm(f2-F2)/np.linalg.norm(F2) < 1e-2

tests/test_physics.py

+31
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def func(field, observers):
8484

8585
# with anchor
8686
F,T = getFT(hom, cloop, anchor=cloop.position)
87+
T*=-1 #bad sign at initial test design
8788
assert np.amax(abs(F)) < 1e-14
8889
assert abs(T[0]) < 1e-14
8990
assert abs(T[1] - np.pi ) < 1e-3
@@ -106,6 +107,7 @@ def func(field, observers):
106107

107108
# with anchor
108109
F,T = getFT(hom, rloop, anchor=rloop.position)
110+
T*=-1 #bad sign at initial test design
109111
assert np.amax(abs(F)) < 1e-14
110112
assert abs(T[0]) < 1e-14
111113
assert abs(T[1] + 4 ) < 1e-3
@@ -262,3 +264,32 @@ def test_sphere_cube_at_distance():
262264

263265
assert max(abs(errF)) < 1e-5
264266
assert max(abs(errT)) < 1e-5
267+
268+
269+
def test_torque_sign():
270+
""" make sure that torque sign is in the right direction"""
271+
272+
# Cuboid -> Cuboid
273+
mag1 = magpy.magnet.Cuboid(position=(2,0,0), polarization=(1,0,0), dimension=(2,1,1))
274+
mag2 = magpy.magnet.Cuboid(position=(-2,0,0), polarization=(1,0,0), dimension=(2,1,1))
275+
276+
mag1.rotate_from_angax(15, "y")
277+
mag1.meshing=(3,3,3)
278+
279+
_,T = getFT(mag2, mag1)
280+
281+
assert T[1] < 0
282+
283+
# Cuboid -> Polyline
284+
mag = magpy.magnet.Cuboid(polarization=(0,0,1), dimension=(1,1,2))
285+
286+
ts = np.linspace(0,2*np.pi,10)
287+
verts = [(2*np.cos(t), 2*np.sin(t),0) for t in ts]
288+
loop = magpy.current.Polyline(vertices=verts, current=1)
289+
loop.rotate_from_angax(15, "y")
290+
291+
loop.meshing=2
292+
293+
_,T = getFT(mag, loop, anchor=(0,0,0))
294+
295+
assert T[1] < 0

0 commit comments

Comments
 (0)