def Bfield_Dipole(M, pos): R = pos rr = fastSum3D(R * R) mr = fastSum3D(M * R) if rr == 0: warn('Warning: getB Position directly at moment position', RuntimeWarning) return array([NaN, NaN, NaN]) return (3 * R * mr - M * rr) / rr**(5 / 2) / (4 * pi)
def Bfield_Sphere(MAG, pos, D): # returns array, takes (arr3, arr3, float) radius = D/2 r = fastNorm3D(pos) if r > radius: return radius**3/3*(-MAG/r**3 + 3*fastSum3D(MAG*pos)*pos/r**5) elif r == radius: warn('Warning: getB Position directly on magnet surface', RuntimeWarning) return array([NaN, NaN, NaN]) else: return 2/3*MAG
def test_algebraic(): assert round(getPhi(1, 2), 4) == 1.1071, "bad getPhi result at (1,2)" assert round(getPhi(1, 0), 4) == 0.0, "bad getPhi result at (1,0)" assert round(getPhi(-1, 0), 4) == 3.1416, "bad getPhi result at (-1,0)" assert round(getPhi(0, 0), 4) == 0.0, "bad getPhi result at (0,0)" assert round(arccosSTABLE(2), 4) == 0.0, "bad arccosStable at (2)" assert round(arccosSTABLE(-2), 4) == 3.1416, "bad arccosStable at (-2)" assert all(fastCross3D([1, 2, 3], [3, 2, 1]) == array( [-4, 8, -4])), "bad fastCross3D" assert round(fastSum3D([2.3, 5.6, 2.0]), 2) == 9.90, "bad fastSum3D" assert round(fastNorm3D([58.2, 25, 25]), 4) == 68.0973, "bad fastNorm3D"
def rotate(self, angle, axis, anchor='self.position'): """ This method rotates the source about `axis` by `angle`. The axis passes through the center of rotation anchor. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int). Parameters ---------- angle : scalar [deg] Set angle of rotation in units of [deg] axis : vec3 [] Set axis of rotation anchor : vec3 [mm] Specify the Center of rotation which defines the position of the axis of rotation. If not specified the source will rotate about its own center. Returns ------- None Example ------- >>> from magpylib import source >>> pm = source.magnet.Sphere(mag=[0,0,1000], dim=1) >>> print(pm.position, pm.angle, pm.axis) [0. 0. 0.] 0.0 [0. 0. 1.] >>> pm.rotate(90, [0,1,0], anchor=[1,0,0]) >>> print(pm.position, pm.angle, pm.axis) [1., 0., 1.] 90.0 [0., 1., 0.] """ # secure type ax = array(axis, dtype=float64, copy=False) try: ang = float(angle) except ValueError: sys.exit('Bad angle input') if str(anchor) == 'self.position': anchor = self.position else: anchor = array(anchor, dtype=float64, copy=False) # check input if any(isnan(ax)) or len(ax) != 3: sys.exit('Bad axis input') if fastSum3D(ax**2) == 0: sys.exit('Bad axis input') if any(isnan(anchor)) or len(anchor) != 3: sys.exit('Bad anchor input') # determine Rotation Quaternion Q from self.axis-angle Q = getRotQuat(self.angle, self.axis) # determine rotation Quaternion P from rot input P = getRotQuat(ang, ax) # determine new orientation quaternion which follows from P.Q v (P.Q)* R = Qmult(P, Q) # reconstruct new axis-angle from new orientation quaternion ang3 = arccosSTABLE(R[0]) * 180 / pi * 2 ax3 = R[1:] # konstanter mult faktor ist wurscht für ax3 self.angle = ang3 if ang3 == 0: # avoid returning a [0,0,0] axis self.axis = array([0, 0, 1]) else: Lax3 = fastNorm3D(ax3) self.axis = array(ax3) / Lax3 # set new position using P.v.P* posOld = self.position - anchor Vold = [0] + [p for p in posOld] Vnew = Qmult(P, Qmult(Vold, Qconj(P))) self.position = array(Vnew[1:]) + anchor