Utils Module
pymagnets.utils
This module imports the classes and functions in the private modules to create a public API, including:
- Quaternion()
- Global Constants
- Point structures
- Vector structures
DemagResult
dataclass
Result of the demagnetization solver.
Source code in src/pymagnet/utils/_demag.py
20 21 22 23 24 25 26 27 | |
Field1
Bases: Point_Array1
1D Field vector This is used to contain one component (Bz as z), and the units ('T', 'mT', etc)
Source code in src/pymagnet/utils/_vector_structs.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
__init__(z, unit='T')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
ndarray
|
Magnetic field component |
required |
unit
|
str
|
Unit of field. Defaults to "T". |
'T'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. T, mT, uT, nT |
Source code in src/pymagnet/utils/_vector_structs.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
change_unit(new_unit, get_unit_value=get_unit_value_tesla)
Converts field array to a different unit. e.g from 'T' to 'mT'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_tesla. |
get_unit_value_tesla
|
Source code in src/pymagnet/utils/_vector_structs.py
283 284 285 286 287 288 289 290 | |
Field2
Bases: Point_Array2
2D Field vector This is used to contain two components (x, y), and the units ('T', 'mT', etc)
Source code in src/pymagnet/utils/_vector_structs.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
__init__(x, y, unit='T')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Magnetic field component Bx |
required |
y
|
ndarray
|
Magnetic field component By |
required |
unit
|
str
|
Unit of field. Defaults to "T". |
'T'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. T, mT, uT, nT |
Source code in src/pymagnet/utils/_vector_structs.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
calc_norm()
Calculates the norm of the 2D vector
Source code in src/pymagnet/utils/_vector_structs.py
317 318 319 | |
change_unit(new_unit, get_unit_value=get_unit_value_tesla)
Converts field array to a different unit. e.g from 'T' to 'mT'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_tesla. |
get_unit_value_tesla
|
Source code in src/pymagnet/utils/_vector_structs.py
327 328 329 330 331 332 333 334 | |
Field3
Bases: Point_Array3
3D Field vector This is used to contain three components (x, y), and the units ('T', 'mT', etc)
Source code in src/pymagnet/utils/_vector_structs.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
__init__(x, y, z, unit='T')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Magnetic field component Bx |
required |
y
|
ndarray
|
Magnetic field component By |
required |
z
|
ndarray
|
Magnetic field component Bz |
required |
unit
|
str
|
Unit of field. Defaults to "T". |
'T'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. T, mT, uT, nT |
Source code in src/pymagnet/utils/_vector_structs.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
calc_norm()
Calculates the norm of the 3D vector
Source code in src/pymagnet/utils/_vector_structs.py
359 360 361 | |
change_unit(new_unit, get_unit_value=get_unit_value_tesla)
Converts field array to a different unit. e.g from 'T' to 'mT'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_tesla. |
get_unit_value_tesla
|
Source code in src/pymagnet/utils/_vector_structs.py
363 364 365 366 367 368 369 370 | |
Jacobian2
dataclass
2D Jacobian tensor of the magnetic field: J_ij = dB_i/dx_j.
Attributes:
| Name | Type | Description |
|---|---|---|
dBx_dx |
ndarray
|
Partial derivative of Bx with respect to x. |
dBx_dy |
ndarray
|
Partial derivative of Bx with respect to y. |
dBy_dx |
ndarray
|
Partial derivative of By with respect to x. |
dBy_dy |
ndarray
|
Partial derivative of By with respect to y. |
Source code in src/pymagnet/utils/_vector_structs.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
Jacobian3
dataclass
3D Jacobian tensor of the magnetic field: J_ij = dB_i/dx_j.
Attributes:
| Name | Type | Description |
|---|---|---|
dBx_dx |
ndarray
|
Partial derivative of Bx with respect to x. |
dBx_dy |
ndarray
|
Partial derivative of Bx with respect to y. |
dBx_dz |
ndarray
|
Partial derivative of Bx with respect to z. |
dBy_dx |
ndarray
|
Partial derivative of By with respect to x. |
dBy_dy |
ndarray
|
Partial derivative of By with respect to y. |
dBy_dz |
ndarray
|
Partial derivative of By with respect to z. |
dBz_dx |
ndarray
|
Partial derivative of Bz with respect to x. |
dBz_dy |
ndarray
|
Partial derivative of Bz with respect to y. |
dBz_dz |
ndarray
|
Partial derivative of Bz with respect to z. |
Source code in src/pymagnet/utils/_vector_structs.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
Point2
2D point class
Note that multiplication of two points is done elementwise, dot product is a separate method.
Source code in src/pymagnet/utils/_point_structs.py
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 | |
__init__(x, y)
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
Source code in src/pymagnet/utils/_point_structs.py
20 21 22 23 24 25 26 27 28 | |
distance_to(point)
Calculates distance to a point
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
Point2
|
Target point |
required |
Returns:
| Type | Description |
|---|---|
float
|
distance |
Source code in src/pymagnet/utils/_point_structs.py
76 77 78 79 80 81 82 83 84 85 | |
distance_to_origin()
Calculates distance to a origin
Returns:
| Type | Description |
|---|---|
float
|
distance |
Source code in src/pymagnet/utils/_point_structs.py
87 88 89 90 91 92 93 | |
Point3
Bases: Point2
3D point class
Note that multiplication of two points is done elementwise, dot product is a separate method.
Source code in src/pymagnet/utils/_point_structs.py
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 | |
Point_Array2
2D point structure This is used to contain two position arrays (x, y), and the units ('mm', 'cm', etc)
Source code in src/pymagnet/utils/_vector_structs.py
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 | |
__init__(x, y, unit='mm')
Init Method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
unit
|
str
|
Unit of length. Defaults to "mm". |
'mm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. km, m, cm, mm |
Source code in src/pymagnet/utils/_vector_structs.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
change_unit(new_unit, get_unit_value=get_unit_value_meter)
Converts point array to a different unit. e.g from 'cm' to 'mm'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_meter. |
get_unit_value_meter
|
Source code in src/pymagnet/utils/_vector_structs.py
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 | |
Point_Array3
Bases: Point_Array2
3D point structure This is used to contain three position arrays (x, y, z), and the units ('mm', 'cm', etc)
Source code in src/pymagnet/utils/_vector_structs.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
__init__(x, y, z, unit='mm')
Init Method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
unit
|
str
|
Unit of length. Defaults to "mm". |
'mm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. km, m, cm, mm |
Source code in src/pymagnet/utils/_vector_structs.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
change_unit(new_unit, get_unit_value=get_unit_value_meter)
Converts point array to a different unit. e.g from 'cm' to 'mm'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_meter. |
get_unit_value_meter
|
Source code in src/pymagnet/utils/_vector_structs.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
rotate(alpha, beta, gamma)
Rotates set of Point_Array3 points by angles alpha, beta, gamma
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Angle to rotate about z in degrees |
required |
beta
|
float
|
Angle to rotate about y in degrees |
required |
gamma
|
float
|
Angle to rotate about x in degrees |
required |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
rotated set of points |
Source code in src/pymagnet/utils/_vector_structs.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
Quaternion
Quaternion class. overloading of multiplication symbol allows easy quaternion multiplications
Source code in src/pymagnet/utils/_quaternion.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
__init__(w=1.0, x=0.0, y=0.0, z=0.0)
Initialse a pure quaternion (1; 0, 0, 0)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
ndarray
|
scalar quaternion. Defaults to 1.0. |
1.0
|
x
|
ndarray
|
vector component. Defaults to 0.0. |
0.0
|
y
|
ndarray
|
vector component. Defaults to 0.0. |
0.0
|
z
|
ndarray
|
vector component. Defaults to 0.0. |
0.0
|
Source code in src/pymagnet/utils/_quaternion.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
as_tuple()
Returns quaternion as tuple of arrays
Returns:
| Type | Description |
|---|---|
tuple
|
w (array), x (array), y (array), z (array) |
Source code in src/pymagnet/utils/_quaternion.py
193 194 195 196 197 198 199 | |
euler_to_quaternion(alpha, beta, gamma)
staticmethod
Converts Euler angles to quaternion
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
angle to z-axis |
required |
beta
|
float
|
angle to y-axis |
required |
gamma
|
float
|
angle to x-axis |
required |
Returns:
| Type | Description |
|---|---|
Quaternion
|
Euler angles as a Quaternion |
Source code in src/pymagnet/utils/_quaternion.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
gen_rotation_quaternion(alpha_rad=0.0, beta_rad=0.0, gamma_rad=0.0)
staticmethod
Generates quaternion for rotation around z, y, x axes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha_rad
|
float
|
angle to z-axis. Defaults to 0.0. |
0.0
|
beta_rad
|
float
|
angle to y-axis. Defaults to 0.0. |
0.0
|
gamma_rad
|
float
|
angle to x-axis. Defaults to 0.0. |
0.0
|
Returns:
| Type | Description |
|---|---|
Quaternion
|
rotation quaternion |
Source code in src/pymagnet/utils/_quaternion.py
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 | |
get_conjugate()
Returns quaternion conjugate
Returns:
| Type | Description |
|---|---|
quaternion
|
quaternion conjugate |
Source code in src/pymagnet/utils/_quaternion.py
53 54 55 56 57 58 59 | |
vec_norm(x, y, z)
staticmethod
Normalises each x,y,z vector
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x array |
required |
y
|
ndarray
|
y array |
required |
z
|
ndarray
|
z array |
required |
Returns:
| Type | Description |
|---|---|
array
|
3xN array of normalised vectors |
Source code in src/pymagnet/utils/_quaternion.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
BdotgradB_2D(B, x, y)
Computes (B . grad)B using the full Jacobian tensor.
Calculates the directional derivative of B along B
[(Bยทโ)B]_x = Bx * dBx/dx + By * dBx/dy [(Bยทโ)B]_y = Bx * dBy/dx + By * dBy/dy
This is the force-relevant quantity for magnetic gradient forces on paramagnetic materials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field2
|
Magnetic field vector with .x, .y components |
required |
x
|
ndarray
|
x coordinates (2D grid) |
required |
y
|
ndarray
|
y coordinates (2D grid) |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
(Bยทโ)B vector and its norm |
Source code in src/pymagnet/utils/_routines2D.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
BdotgradB_3D(B, x, y, z)
Computes (B . grad)B using the full Jacobian tensor.
Calculates the directional derivative of B along B
[(Bยทโ)B]_x = Bx * dBx/dx + By * dBx/dy + Bz * dBx/dz [(Bยทโ)B]_y = Bx * dBy/dx + By * dBy/dy + Bz * dBy/dz [(Bยทโ)B]_z = Bx * dBz/dx + By * dBz/dy + Bz * dBz/dz
Supports both full 3D grids and 2D slices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field3
|
Magnetic field vector with .x, .y, .z components |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
(Bยทโ)B vector and its norm |
Source code in src/pymagnet/utils/_routines3D.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
FgradB_2D(B, x, y, chi_m, c)
Calculates the magnetic field gradient force for a 2D field.
Computes F = (chi_m / mu_0) * c * |B| * grad(|B|). This is a scalar approximation of the gradient force.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field2
|
Magnetic field vector (must have .n for magnitude) |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
chi_m
|
float
|
Magnetic susceptibility |
required |
c
|
float
|
Material constant |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
Magnetic field gradient force vector |
Source code in src/pymagnet/utils/_routines2D.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
FgradB_3D(B, x, y, z, chi_m, c)
Calculates the magnetic field gradient force for a 3D field.
Computes F = (chi_m / mu_0) * c * |B| * grad(|B|). This is a scalar approximation of the gradient force.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field3
|
Magnetic field vector (must have .n for magnitude) |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
chi_m
|
float
|
Magnetic susceptibility |
required |
c
|
float
|
Material constant |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
Magnetic field gradient force vector |
Source code in src/pymagnet/utils/_routines3D.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
altitude(a, b, c)
Gets altitude to side a of a triangle using Heron's formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
base side (altitude is perpendicular to this) |
required |
b
|
float
|
second side |
required |
c
|
float
|
third side |
required |
Returns:
| Type | Description |
|---|---|
float
|
altitude to side |
Raises:
| Type | Description |
|---|---|
ValueError
|
If triangle is degenerate (sides violate triangle inequality) |
Source code in src/pymagnet/utils/_trigonometry3D.py
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 | |
build_MH_interpolator(H_data, M_data)
Build a monotonicity-preserving interpolator from an MH curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
H_data
|
ndarray
|
Applied field values, strictly monotonically increasing. |
required |
M_data
|
ndarray
|
Magnetization values, monotonically non-decreasing. |
required |
Returns:
| Type | Description |
|---|---|
PchipInterpolator
|
PchipInterpolator mapping H -> M. |
Source code in src/pymagnet/utils/_demag.py
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 | |
get_field_2D(Point_Array2)
Calculates magnetic field at an array of points due to every instantated
Magnet2D magnet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Point_Array2
|
Point_Array2
|
array of x,y points and associated unit, defaults to 'mm' |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
array of Bx,By,|B| values and associated unit (defaults to 'T') |
Source code in src/pymagnet/utils/_routines2D.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
get_field_3D(points)
Calculates magnetic field at an array of points due to every instantated
Magnet3D magnet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Point_Array3
|
array of x,y,z points and associated unit, defaults to 'mm' |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
array of Bx,By,Bz,|B| values and associated unit (defaults to 'T') |
Source code in src/pymagnet/utils/_routines3D.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
gradB_2D(B, x, y)
Calculates the spatial gradient of the magnetic field magnitude.
Computes grad(|B|) using finite differences. Uses numba-accelerated kernels for grids >= 10k points, np.gradient for smaller grids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
ndarray
|
Magnetic field magnitude |B| (2D array) |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
Gradient vector (d|B|/dx, d|B|/dy) and its norm |
Source code in src/pymagnet/utils/_routines2D.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
gradB_3D(B, x, y, z)
Calculates the spatial gradient of the magnetic field magnitude.
Computes grad(|B|) using finite differences. Uses numba-accelerated kernels for grids >= 10k points, np.gradient for smaller grids. Supports both full 3D grids (from grid3D) and 2D slices (from slice3D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
ndarray
|
Magnetic field magnitude |B| |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
Gradient vector (d|B|/dx, d|B|/dy, d|B|/dz) and its norm |
Source code in src/pymagnet/utils/_routines3D.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
grid2D(xmax, ymax, **kwargs)
Generates grid of x and y points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xmax
|
float
|
maximum x value |
required |
ymax
|
float
|
maximum y value |
required |
Kwargs
num_points (int): Number of points in each direction. Defaults to 100 xmin (float): minimum x value. Defaults to -xmax ymin (float): minimum y value. Defaults to -ymax unit (str): unit length. Defaults to 'mm'
Returns:
| Type | Description |
|---|---|
Point_Array2
|
array of x and y values of shape (num_points, num_points) and associated unit |
Source code in src/pymagnet/utils/_routines2D.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
grid3D(xmax, ymax, zmax, **kwargs)
Generates grid of x, y, z points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xmax
|
float
|
maximum x value |
required |
ymax
|
float
|
maximum y value |
required |
zmax
|
float
|
maximum y value |
required |
Kwargs
num_points (int): Number of points in each direction. Defaults to 100 xmin (float): minimum x value. Defaults to -xmax ymin (float): minimum y value. Defaults to -ymax zmin (float): minimum y value. Defaults to -zmax unit (string): unit length. Defaults to 'mm'
Returns:
| Type | Description |
|---|---|
Point_Array2
|
array of x and y values of shape (num_points, num_points) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
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 | |
jacobian_B_2D(B, x, y)
Calculates the Jacobian of the 2D magnetic field vector.
Computes J_ij = dB_i/dx_j, the full tensor gradient of the vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field2
|
Magnetic field vector with .x, .y components |
required |
x
|
ndarray
|
x coordinates (2D grid) |
required |
y
|
ndarray
|
y coordinates (2D grid) |
required |
Returns:
| Type | Description |
|---|---|
Jacobian2
|
Dataclass with components dBx_dx, dBx_dy, dBy_dx, dBy_dy |
Source code in src/pymagnet/utils/_routines2D.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
jacobian_B_3D(B, x, y, z)
Calculates the Jacobian of the 3D magnetic field vector.
Computes J_ij = dB_i/dx_j, the full tensor gradient of the vector field. Supports both full 3D grids (from grid3D) and 2D slices (from slice3D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field3
|
Magnetic field vector with .x, .y, .z components |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
Jacobian3
|
Dataclass with all 9 partial derivative components |
Source code in src/pymagnet/utils/_routines3D.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
line3D(start, end, num_points=100, **kwargs)
Generates a line of points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
tuple
|
Starting point (x1,y1,z1) |
required |
end
|
tuple
|
End point (x2,y2,z2) |
required |
num_points
|
int
|
number of points to generate. Defaults to 100 |
100
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
unit |
str
|
length scale units. Defaults to "mm". |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
array of x, y, and z values of shape (num_points) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
norm_plane(vec)
Calculates the normal to a triangular plane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
ndarray
|
(3,3) array of triangle vertices |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit normal vector (3,) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If triangle is degenerate (collinear vertices) |
Source code in src/pymagnet/utils/_trigonometry3D.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
rotate_points(points, rotation_quaternion)
Rotates a set of points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
[type]
|
[description] |
required |
rotation_quaternion
|
[type]
|
[description] |
required |
Returns:
| Type | Description |
|---|---|
[type]
|
[description] |
Source code in src/pymagnet/utils/_trigonometry3D.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
rotate_points_2D(x, y, alpha)
Counter-clockwise rotation of points x,y
Rotates 2D coordinates using a rotation matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
array of x coordinates |
required |
y
|
ndarray
|
array of x coordinates |
required |
alpha
|
float
|
rotation angle w.r.t. x-axis |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(x', y') rotated array of points |
Source code in src/pymagnet/utils/_routines2D.py
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 | |
signed_area(triangle)
Calculates signed area of a triangle. Area area < 0 for clockwise ordering. Assumes the triangle is in the xz plane (i.e. with the normal parallel to y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
3x3 array of vertices |
required |
Returns:
| Type | Description |
|---|---|
float
|
signed area |
Source code in src/pymagnet/utils/_trigonometry3D.py
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 | |
slice3D(plane='xy', max1=1.0, max2=1.0, slice_value=0.0, unit='mm', **kwargs)
Generates a planar slice of values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plane
|
str
|
plane. Defaults to "xy". |
'xy'
|
max1
|
float
|
maximum along axis 1. Defaults to 1.0. |
1.0
|
max2
|
float
|
maximum along axis 2. Defaults to 1.0. |
1.0
|
slice_value
|
float
|
constant value for third axis. Defaults to 0.0. |
0.0
|
unit
|
str
|
length scale units. Defaults to "mm". |
'mm'
|
Kwargs
num_points (int): Number of points in each direction. Defaults to 100 min1 (float): minimum along axis 1. Defaults to -min1 min2 (float): minimum along axis 2. Defaults to -min2
Raises:
| Type | Description |
|---|---|
Exception
|
plane type, must be one of 'xy', 'xz, 'yz', or 'custom' |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
array of x, y, and z values of shape (num_points, num_points) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
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 | |
solve_demag_tanh(H_ext, Ms, chi, N=0.5)
Solve demagnetization for the tanh model (single scalar H_ext).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
H_ext
|
float
|
External applied field magnitude. |
required |
Ms
|
float
|
Saturation magnetization. |
required |
chi
|
float
|
Dimensionless initial susceptibility. |
required |
N
|
float
|
Demagnetizing factor (default 0.5). |
0.5
|
Returns:
| Type | Description |
|---|---|
tuple
|
(M_solution, H_int, converged). |
Source code in src/pymagnet/utils/_demag.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
solve_demag_tanh_batch(H_ext_array, Ms, chi, N=0.5)
Solve demagnetization for an array of H_ext values in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
H_ext_array
|
ndarray
|
1D array of external field values. |
required |
Ms
|
float
|
Saturation magnetization. |
required |
chi
|
float
|
Dimensionless initial susceptibility. |
required |
N
|
float
|
Demagnetizing factor (default 0.5). |
0.5
|
Returns:
| Type | Description |
|---|---|
tuple
|
(M_solutions, H_int_solutions) arrays of the same shape as H_ext_array. |
Source code in src/pymagnet/utils/_demag.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
solve_demagnetization(H_ext, MH_interp, M_sat, N=0.5)
Solve the self-consistent demagnetization equation.
Finds M such that M = f_MH(H_ext - N * M) by solving g(M) = M - f_MH(H_ext - N * M) = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
H_ext
|
float
|
External applied field magnitude. |
required |
MH_interp
|
Callable
|
Callable mapping H -> M. Can be a PchipInterpolator from build_MH_interpolator or an analytical model from tanh_MH_model. |
required |
M_sat
|
float
|
Saturation magnetization (upper bracket bound). |
required |
N
|
float
|
Demagnetizing factor. Default 0.5 (2D circle / infinite cylinder). |
0.5
|
Returns:
| Type | Description |
|---|---|
DemagResult
|
DemagResult with M_solution, H_int, convergence flag, and solver used. |
Source code in src/pymagnet/utils/_demag.py
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 | |
tanh_MH_model(Ms, chi)
Return an analytical M(H) function: M = Ms * tanh(chi * H / Ms).
At low fields this gives M โ chi * H (linear regime), and saturates smoothly to Ms at high fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Ms
|
float
|
Saturation magnetization. |
required |
chi
|
float
|
Dimensionless initial susceptibility (slope dM/dH at H=0). |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
mapping H (float or array) -> M. |
Source code in src/pymagnet/utils/_demag.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
Routines for converting between coordinate systems, between 2D cartesian and polar, as well as 3D cartesian, cylindrical, and spherical.
cart2pol(x, y)
Converts from cartesian to polar coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
rho, phi |
Source code in src/pymagnet/utils/_conversions.py
15 16 17 18 19 20 21 22 23 24 25 26 27 | |
cart2sph(x, y, z)
Converts from cartesian to spherical coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
r, theta, phi |
Source code in src/pymagnet/utils/_conversions.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
get_unit_value_meter(unit)
Returns a queried metre unit as a number Example: factor = get_unit_value_meter('cm') print(f"factor for 'cm' is {factor}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
string
|
SI length unit |
required |
Returns:
| Type | Description |
|---|---|
float
|
SI prefix factor |
Source code in src/pymagnet/utils/_conversions.py
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 | |
get_unit_value_tesla(unit)
Returns a queried magnetic flux density unit as a number Example: factor = get_unit_value_meter('mT') print(f"factor for 'mT' is {factor}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
string
|
SI length unit |
required |
Returns:
| Type | Description |
|---|---|
float
|
SI prefix factor |
Source code in src/pymagnet/utils/_conversions.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
pol2cart(rho, phi)
Converts from polar to cartesian coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rho
|
ndarray
|
radial coordinates |
required |
phi
|
ndarray
|
azimuthal coordinates |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
x,y |
Source code in src/pymagnet/utils/_conversions.py
30 31 32 33 34 35 36 37 38 39 40 41 42 | |
sph2cart(r, theta, phi)
Converts from spherical to cartesian coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
ndarray
|
radial coordinates |
required |
theta
|
ndarray
|
azimuthal angles |
required |
phi
|
ndarray
|
polar angle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
x,y,z |
Source code in src/pymagnet/utils/_conversions.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
sphere_sph2cart(Br, Btheta, theta, phi)
Converts magnetic field of a sphere from spherical to cartesian coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Br
|
ndarray
|
radial vector component |
required |
Btheta
|
ndarray
|
polar vector component |
required |
theta
|
ndarray
|
azimuthal angles |
required |
phi
|
ndarray
|
polar angle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Bx,By,Bz |
Source code in src/pymagnet/utils/_conversions.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
vector_pol2cart(Brho, Bphi, phi)
Converts Vectors from polar to cartesian coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Brho
|
ndarray
|
radial vector component |
required |
Bphi
|
ndarray
|
azimuthal vector component |
required |
phi
|
ndarray
|
azimuthal coordinates |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Bx, By |
Source code in src/pymagnet/utils/_conversions.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
vector_sph2cart(Br, Btheta, Bphi, theta, phi)
Converts Vectors from spherical to cartesian coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Br
|
ndarray
|
radial vector component |
required |
Btheta
|
ndarray
|
polar vector component |
required |
Bphi
|
ndarray
|
azimuthal vector component |
required |
theta
|
ndarray
|
azimuthal angles |
required |
phi
|
ndarray
|
polar angle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Bx,By,Bz |
Source code in src/pymagnet/utils/_conversions.py
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 | |
pymagnets.utils._point_structs
Private module consiting of point classes and their methods.
Point2
2D point class
Note that multiplication of two points is done elementwise, dot product is a separate method.
Source code in src/pymagnet/utils/_point_structs.py
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 | |
__init__(x, y)
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
Source code in src/pymagnet/utils/_point_structs.py
20 21 22 23 24 25 26 27 28 | |
distance_to(point)
Calculates distance to a point
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
Point2
|
Target point |
required |
Returns:
| Type | Description |
|---|---|
float
|
distance |
Source code in src/pymagnet/utils/_point_structs.py
76 77 78 79 80 81 82 83 84 85 | |
distance_to_origin()
Calculates distance to a origin
Returns:
| Type | Description |
|---|---|
float
|
distance |
Source code in src/pymagnet/utils/_point_structs.py
87 88 89 90 91 92 93 | |
Point3
Bases: Point2
3D point class
Note that multiplication of two points is done elementwise, dot product is a separate method.
Source code in src/pymagnet/utils/_point_structs.py
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 | |
Quaternion module
Implements quaternion multiplication for convenient rotation of vectors in 3D.
Example
Rotation of a vector about the x-axis:
import numpy as np
import pymagnet as pm
vector1 = np.array([1,0,0])
rotate_about_z = pm.magnets.q_angle_from_axis(np.pi/2, (0, 0, 1))
vector2 = rotate_about_z * vector1
Quaternion
Quaternion class. overloading of multiplication symbol allows easy quaternion multiplications
Source code in src/pymagnet/utils/_quaternion.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
__init__(w=1.0, x=0.0, y=0.0, z=0.0)
Initialse a pure quaternion (1; 0, 0, 0)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
ndarray
|
scalar quaternion. Defaults to 1.0. |
1.0
|
x
|
ndarray
|
vector component. Defaults to 0.0. |
0.0
|
y
|
ndarray
|
vector component. Defaults to 0.0. |
0.0
|
z
|
ndarray
|
vector component. Defaults to 0.0. |
0.0
|
Source code in src/pymagnet/utils/_quaternion.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
as_tuple()
Returns quaternion as tuple of arrays
Returns:
| Type | Description |
|---|---|
tuple
|
w (array), x (array), y (array), z (array) |
Source code in src/pymagnet/utils/_quaternion.py
193 194 195 196 197 198 199 | |
euler_to_quaternion(alpha, beta, gamma)
staticmethod
Converts Euler angles to quaternion
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
angle to z-axis |
required |
beta
|
float
|
angle to y-axis |
required |
gamma
|
float
|
angle to x-axis |
required |
Returns:
| Type | Description |
|---|---|
Quaternion
|
Euler angles as a Quaternion |
Source code in src/pymagnet/utils/_quaternion.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
gen_rotation_quaternion(alpha_rad=0.0, beta_rad=0.0, gamma_rad=0.0)
staticmethod
Generates quaternion for rotation around z, y, x axes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha_rad
|
float
|
angle to z-axis. Defaults to 0.0. |
0.0
|
beta_rad
|
float
|
angle to y-axis. Defaults to 0.0. |
0.0
|
gamma_rad
|
float
|
angle to x-axis. Defaults to 0.0. |
0.0
|
Returns:
| Type | Description |
|---|---|
Quaternion
|
rotation quaternion |
Source code in src/pymagnet/utils/_quaternion.py
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 | |
get_conjugate()
Returns quaternion conjugate
Returns:
| Type | Description |
|---|---|
quaternion
|
quaternion conjugate |
Source code in src/pymagnet/utils/_quaternion.py
53 54 55 56 57 58 59 | |
vec_norm(x, y, z)
staticmethod
Normalises each x,y,z vector
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x array |
required |
y
|
ndarray
|
y array |
required |
z
|
ndarray
|
z array |
required |
Returns:
| Type | Description |
|---|---|
array
|
3xN array of normalised vectors |
Source code in src/pymagnet/utils/_quaternion.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
q_angle_from_axis(theta, vec)
Generates a rotation quaternion for an angle theta about an axis vec
This is a normalised, i.e. unit quaternion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
angle of rotation |
required |
vec
|
tuple / array
|
axis vector |
required |
Example
90 degree rotation about the x axis:
rotation_quaternion = q_angle_from_axis(np.pi/2, (1, 0, 0) )
Returns:
| Type | Description |
|---|---|
Quaternion
|
rotation quaternion |
Source code in src/pymagnet/utils/_quaternion.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
Routines for Two Dimensional Magnet Classes
BdotgradB_2D(B, x, y)
Computes (B . grad)B using the full Jacobian tensor.
Calculates the directional derivative of B along B
[(Bยทโ)B]_x = Bx * dBx/dx + By * dBx/dy [(Bยทโ)B]_y = Bx * dBy/dx + By * dBy/dy
This is the force-relevant quantity for magnetic gradient forces on paramagnetic materials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field2
|
Magnetic field vector with .x, .y components |
required |
x
|
ndarray
|
x coordinates (2D grid) |
required |
y
|
ndarray
|
y coordinates (2D grid) |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
(Bยทโ)B vector and its norm |
Source code in src/pymagnet/utils/_routines2D.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
FgradB_2D(B, x, y, chi_m, c)
Calculates the magnetic field gradient force for a 2D field.
Computes F = (chi_m / mu_0) * c * |B| * grad(|B|). This is a scalar approximation of the gradient force.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field2
|
Magnetic field vector (must have .n for magnitude) |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
chi_m
|
float
|
Magnetic susceptibility |
required |
c
|
float
|
Material constant |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
Magnetic field gradient force vector |
Source code in src/pymagnet/utils/_routines2D.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
get_field_2D(Point_Array2)
Calculates magnetic field at an array of points due to every instantated
Magnet2D magnet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Point_Array2
|
Point_Array2
|
array of x,y points and associated unit, defaults to 'mm' |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
array of Bx,By,|B| values and associated unit (defaults to 'T') |
Source code in src/pymagnet/utils/_routines2D.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
gradB_2D(B, x, y)
Calculates the spatial gradient of the magnetic field magnitude.
Computes grad(|B|) using finite differences. Uses numba-accelerated kernels for grids >= 10k points, np.gradient for smaller grids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
ndarray
|
Magnetic field magnitude |B| (2D array) |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
Returns:
| Type | Description |
|---|---|
Field2
|
Gradient vector (d|B|/dx, d|B|/dy) and its norm |
Source code in src/pymagnet/utils/_routines2D.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
grid2D(xmax, ymax, **kwargs)
Generates grid of x and y points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xmax
|
float
|
maximum x value |
required |
ymax
|
float
|
maximum y value |
required |
Kwargs
num_points (int): Number of points in each direction. Defaults to 100 xmin (float): minimum x value. Defaults to -xmax ymin (float): minimum y value. Defaults to -ymax unit (str): unit length. Defaults to 'mm'
Returns:
| Type | Description |
|---|---|
Point_Array2
|
array of x and y values of shape (num_points, num_points) and associated unit |
Source code in src/pymagnet/utils/_routines2D.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
jacobian_B_2D(B, x, y)
Calculates the Jacobian of the 2D magnetic field vector.
Computes J_ij = dB_i/dx_j, the full tensor gradient of the vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field2
|
Magnetic field vector with .x, .y components |
required |
x
|
ndarray
|
x coordinates (2D grid) |
required |
y
|
ndarray
|
y coordinates (2D grid) |
required |
Returns:
| Type | Description |
|---|---|
Jacobian2
|
Dataclass with components dBx_dx, dBx_dy, dBy_dx, dBy_dy |
Source code in src/pymagnet/utils/_routines2D.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
rotate_points_2D(x, y, alpha)
Counter-clockwise rotation of points x,y
Rotates 2D coordinates using a rotation matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
array of x coordinates |
required |
y
|
ndarray
|
array of x coordinates |
required |
alpha
|
float
|
rotation angle w.r.t. x-axis |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(x', y') rotated array of points |
Source code in src/pymagnet/utils/_routines2D.py
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 | |
Routines for Three Dimensional Magnet Classes
BdotgradB_3D(B, x, y, z)
Computes (B . grad)B using the full Jacobian tensor.
Calculates the directional derivative of B along B
[(Bยทโ)B]_x = Bx * dBx/dx + By * dBx/dy + Bz * dBx/dz [(Bยทโ)B]_y = Bx * dBy/dx + By * dBy/dy + Bz * dBy/dz [(Bยทโ)B]_z = Bx * dBz/dx + By * dBz/dy + Bz * dBz/dz
Supports both full 3D grids and 2D slices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field3
|
Magnetic field vector with .x, .y, .z components |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
(Bยทโ)B vector and its norm |
Source code in src/pymagnet/utils/_routines3D.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
FgradB_3D(B, x, y, z, chi_m, c)
Calculates the magnetic field gradient force for a 3D field.
Computes F = (chi_m / mu_0) * c * |B| * grad(|B|). This is a scalar approximation of the gradient force.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field3
|
Magnetic field vector (must have .n for magnitude) |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
chi_m
|
float
|
Magnetic susceptibility |
required |
c
|
float
|
Material constant |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
Magnetic field gradient force vector |
Source code in src/pymagnet/utils/_routines3D.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
get_field_3D(points)
Calculates magnetic field at an array of points due to every instantated
Magnet3D magnet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Point_Array3
|
array of x,y,z points and associated unit, defaults to 'mm' |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
array of Bx,By,Bz,|B| values and associated unit (defaults to 'T') |
Source code in src/pymagnet/utils/_routines3D.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
gradB_3D(B, x, y, z)
Calculates the spatial gradient of the magnetic field magnitude.
Computes grad(|B|) using finite differences. Uses numba-accelerated kernels for grids >= 10k points, np.gradient for smaller grids. Supports both full 3D grids (from grid3D) and 2D slices (from slice3D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
ndarray
|
Magnetic field magnitude |B| |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
Field3
|
Gradient vector (d|B|/dx, d|B|/dy, d|B|/dz) and its norm |
Source code in src/pymagnet/utils/_routines3D.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
grid3D(xmax, ymax, zmax, **kwargs)
Generates grid of x, y, z points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xmax
|
float
|
maximum x value |
required |
ymax
|
float
|
maximum y value |
required |
zmax
|
float
|
maximum y value |
required |
Kwargs
num_points (int): Number of points in each direction. Defaults to 100 xmin (float): minimum x value. Defaults to -xmax ymin (float): minimum y value. Defaults to -ymax zmin (float): minimum y value. Defaults to -zmax unit (string): unit length. Defaults to 'mm'
Returns:
| Type | Description |
|---|---|
Point_Array2
|
array of x and y values of shape (num_points, num_points) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
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 | |
jacobian_B_3D(B, x, y, z)
Calculates the Jacobian of the 3D magnetic field vector.
Computes J_ij = dB_i/dx_j, the full tensor gradient of the vector field. Supports both full 3D grids (from grid3D) and 2D slices (from slice3D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
Field3
|
Magnetic field vector with .x, .y, .z components |
required |
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
Returns:
| Type | Description |
|---|---|
Jacobian3
|
Dataclass with all 9 partial derivative components |
Source code in src/pymagnet/utils/_routines3D.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
line3D(start, end, num_points=100, **kwargs)
Generates a line of points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
tuple
|
Starting point (x1,y1,z1) |
required |
end
|
tuple
|
End point (x2,y2,z2) |
required |
num_points
|
int
|
number of points to generate. Defaults to 100 |
100
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
unit |
str
|
length scale units. Defaults to "mm". |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
array of x, y, and z values of shape (num_points) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
plane3D(origin, point_a, point_b, num_points=100)
Generates a plane of points defined by
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
tuple
|
Origin of plane |
required |
point_a
|
tuple
|
Point defining end of vector in x-direction |
required |
point_b
|
tuple
|
Point defining end of vector in y-direction |
required |
num_points
|
int
|
Number of points in each direction, for a total of num_points^2. Defaults to 100. |
100
|
Returns:
| Type | Description |
|---|---|
Point_Array3
|
Struct containing the x,y,z coordinates |
Source code in src/pymagnet/utils/_routines3D.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
point3D(point, **kwargs)
Returns a single point
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
tuple
|
Coordinates (x, y, z) of the point. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
unit |
str
|
length scale units. Defaults to "mm". |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
struct of x, y, and z values of shape (1) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
slice3D(plane='xy', max1=1.0, max2=1.0, slice_value=0.0, unit='mm', **kwargs)
Generates a planar slice of values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plane
|
str
|
plane. Defaults to "xy". |
'xy'
|
max1
|
float
|
maximum along axis 1. Defaults to 1.0. |
1.0
|
max2
|
float
|
maximum along axis 2. Defaults to 1.0. |
1.0
|
slice_value
|
float
|
constant value for third axis. Defaults to 0.0. |
0.0
|
unit
|
str
|
length scale units. Defaults to "mm". |
'mm'
|
Kwargs
num_points (int): Number of points in each direction. Defaults to 100 min1 (float): minimum along axis 1. Defaults to -min1 min2 (float): minimum along axis 2. Defaults to -min2
Raises:
| Type | Description |
|---|---|
Exception
|
plane type, must be one of 'xy', 'xz, 'yz', or 'custom' |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
array of x, y, and z values of shape (num_points, num_points) and associated unit |
Source code in src/pymagnet/utils/_routines3D.py
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 | |
Contains functions needed to rotate and translate a triangle to lie in the xz plane and to divide it into two right angled triangles
align_triangle_to_y(triangle, rot_axis, norm_vec)
Rotates and translates a triangle in lie in the xz plane
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
vertices of a triangle |
required |
rot_axis
|
ndarray
|
axis about which to rotate triangle |
required |
norm_vec
|
ndarray
|
normal to triangle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
aligned_triangle (ndarray, rotated triangle), first_rotation (quaternion, align to y-axis) |
Source code in src/pymagnet/utils/_trigonometry3D.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
align_triangle_to_y_njit(triangle, rot_axis, norm_vec)
Rotates a triangle to lie in the xz plane (numba-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
(3,3) vertices of a triangle |
required |
rot_axis
|
ndarray
|
(3,) axis about which to rotate triangle |
required |
norm_vec
|
ndarray
|
(3,) normal to triangle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(aligned_triangle, first_rotation_quat) |
Source code in src/pymagnet/utils/_trigonometry3D.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
align_triangle_xz(triangle, longest_side)
Returns quaternions needed to rotate a triangle lying in the xz plane to be aligned with its longest side along x and altitude along z
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
vertices of triangle |
required |
longest_side
|
int
|
index of the longest side of triangle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
second_rotation (quaternion, align to x-axis), third_rotation (quaternion, align to z-axis): |
Source code in src/pymagnet/utils/_trigonometry3D.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
align_triangle_xz_njit(triangle, longest_side)
Aligns triangle with longest side along x and altitude along z.
Numba-compatible version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
(3,3) vertices of triangle (should be in xz plane) |
required |
longest_side
|
int
|
index of the longest side of triangle |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(second_rotation_quat, third_rotation_quat) |
Source code in src/pymagnet/utils/_trigonometry3D.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
altitude(a, b, c)
Gets altitude to side a of a triangle using Heron's formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
base side (altitude is perpendicular to this) |
required |
b
|
float
|
second side |
required |
c
|
float
|
third side |
required |
Returns:
| Type | Description |
|---|---|
float
|
altitude to side |
Raises:
| Type | Description |
|---|---|
ValueError
|
If triangle is degenerate (sides violate triangle inequality) |
Source code in src/pymagnet/utils/_trigonometry3D.py
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 | |
altitude_njit(a, b, c)
Gets altitude to side a using Heron's formula (numba-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
base side (altitude is perpendicular to this) |
required |
b
|
float
|
second side |
required |
c
|
float
|
third side |
required |
Returns:
| Type | Description |
|---|---|
float
|
altitude to side |
Source code in src/pymagnet/utils/_trigonometry3D.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
check_sign(vector_1, vector_2)
Returns true if the signs of all elements of two arrays are the same
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector_1
|
ndarray
|
input array 2 |
required |
vector_2
|
ndarray
|
input array 2 |
required |
Returns:
| Type | Description |
|---|---|
boolean
|
True if elements in two arrays have the same sign |
Source code in src/pymagnet/utils/_trigonometry3D.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
norm_plane(vec)
Calculates the normal to a triangular plane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
ndarray
|
(3,3) array of triangle vertices |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit normal vector (3,) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If triangle is degenerate (collinear vertices) |
Source code in src/pymagnet/utils/_trigonometry3D.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
norm_plane_njit(vec)
Calculates the normal to a triangular plane (numba-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
ndarray
|
(3,3) array of triangle vertices |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit normal vector (3,), or zero vector if degenerate |
Source code in src/pymagnet/utils/_trigonometry3D.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
return_axis_vector(triangle, longest_side)
Returns unit vector along the longest side of a triangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
(3,3) array of triangle vertices |
required |
longest_side
|
int
|
index of longest edge (0, 1, or 2) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit vector (3,) along longest edge |
Source code in src/pymagnet/utils/_trigonometry3D.py
208 209 210 211 212 213 214 215 216 217 218 219 220 | |
return_axis_vector_njit(triangle, longest_side)
Returns unit vector along the longest side of a triangle (numba-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
(3,3) array of triangle vertices |
required |
longest_side
|
int
|
index of longest edge (0, 1, or 2) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit vector (3,) along longest edge |
Source code in src/pymagnet/utils/_trigonometry3D.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
return_z_vector(triangle, longest_side)
Returns unit altitude vector from longest side toward opposite vertex.
For a triangle aligned in the xz plane with longest side along x, this returns the direction toward the apex (along z).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
(3,3) array of triangle vertices |
required |
longest_side
|
int
|
index of longest edge (0, 1, or 2) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit altitude vector (3,) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If altitude has zero length (degenerate triangle) |
Source code in src/pymagnet/utils/_trigonometry3D.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
return_z_vector_njit(triangle, longest_side)
Returns unit altitude vector from longest side toward opposite vertex.
Numba-compatible version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
(3,3) array of triangle vertices |
required |
longest_side
|
int
|
index of longest edge (0, 1, or 2) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
unit altitude vector (3,) |
Source code in src/pymagnet/utils/_trigonometry3D.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
rotate_points(points, rotation_quaternion)
Rotates a set of points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
[type]
|
[description] |
required |
rotation_quaternion
|
[type]
|
[description] |
required |
Returns:
| Type | Description |
|---|---|
[type]
|
[description] |
Source code in src/pymagnet/utils/_trigonometry3D.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
rotate_vector_by_quat_inverse_njit(q, x, y, z)
Rotate coordinate arrays by inverse of quaternion.
Numba-compatible function for inverse rotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
ndarray
|
(4,) quaternion [w, x, y, z] |
required |
x
|
ndarray
|
x coordinates (flattened) |
required |
y
|
ndarray
|
y coordinates (flattened) |
required |
z
|
ndarray
|
z coordinates (flattened) |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(x_rot, y_rot, z_rot) rotated coordinates |
Source code in src/pymagnet/utils/_trigonometry3D.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 | |
rotate_vector_by_quat_njit(q, x, y, z)
Rotate coordinate arrays by quaternion.
Numba-compatible function to rotate arrays of coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
ndarray
|
(4,) quaternion [w, x, y, z] |
required |
x
|
ndarray
|
x coordinates (flattened) |
required |
y
|
ndarray
|
y coordinates (flattened) |
required |
z
|
ndarray
|
z coordinates (flattened) |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(x_rot, y_rot, z_rot) rotated coordinates |
Source code in src/pymagnet/utils/_trigonometry3D.py
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | |
signed_area(triangle)
Calculates signed area of a triangle. Area area < 0 for clockwise ordering. Assumes the triangle is in the xz plane (i.e. with the normal parallel to y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triangle
|
ndarray
|
3x3 array of vertices |
required |
Returns:
| Type | Description |
|---|---|
float
|
signed area |
Source code in src/pymagnet/utils/_trigonometry3D.py
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 | |
pymagnets.utils._vector_structs
Private module consiting of vector and point array classes and their methods.
Field1
Bases: Point_Array1
1D Field vector This is used to contain one component (Bz as z), and the units ('T', 'mT', etc)
Source code in src/pymagnet/utils/_vector_structs.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
__init__(z, unit='T')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
ndarray
|
Magnetic field component |
required |
unit
|
str
|
Unit of field. Defaults to "T". |
'T'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. T, mT, uT, nT |
Source code in src/pymagnet/utils/_vector_structs.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
change_unit(new_unit, get_unit_value=get_unit_value_tesla)
Converts field array to a different unit. e.g from 'T' to 'mT'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_tesla. |
get_unit_value_tesla
|
Source code in src/pymagnet/utils/_vector_structs.py
283 284 285 286 287 288 289 290 | |
Field2
Bases: Point_Array2
2D Field vector This is used to contain two components (x, y), and the units ('T', 'mT', etc)
Source code in src/pymagnet/utils/_vector_structs.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
__init__(x, y, unit='T')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Magnetic field component Bx |
required |
y
|
ndarray
|
Magnetic field component By |
required |
unit
|
str
|
Unit of field. Defaults to "T". |
'T'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. T, mT, uT, nT |
Source code in src/pymagnet/utils/_vector_structs.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
calc_norm()
Calculates the norm of the 2D vector
Source code in src/pymagnet/utils/_vector_structs.py
317 318 319 | |
change_unit(new_unit, get_unit_value=get_unit_value_tesla)
Converts field array to a different unit. e.g from 'T' to 'mT'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_tesla. |
get_unit_value_tesla
|
Source code in src/pymagnet/utils/_vector_structs.py
327 328 329 330 331 332 333 334 | |
Field3
Bases: Point_Array3
3D Field vector This is used to contain three components (x, y), and the units ('T', 'mT', etc)
Source code in src/pymagnet/utils/_vector_structs.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
__init__(x, y, z, unit='T')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Magnetic field component Bx |
required |
y
|
ndarray
|
Magnetic field component By |
required |
z
|
ndarray
|
Magnetic field component Bz |
required |
unit
|
str
|
Unit of field. Defaults to "T". |
'T'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. T, mT, uT, nT |
Source code in src/pymagnet/utils/_vector_structs.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
calc_norm()
Calculates the norm of the 3D vector
Source code in src/pymagnet/utils/_vector_structs.py
359 360 361 | |
change_unit(new_unit, get_unit_value=get_unit_value_tesla)
Converts field array to a different unit. e.g from 'T' to 'mT'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_tesla. |
get_unit_value_tesla
|
Source code in src/pymagnet/utils/_vector_structs.py
363 364 365 366 367 368 369 370 | |
Jacobian2
dataclass
2D Jacobian tensor of the magnetic field: J_ij = dB_i/dx_j.
Attributes:
| Name | Type | Description |
|---|---|---|
dBx_dx |
ndarray
|
Partial derivative of Bx with respect to x. |
dBx_dy |
ndarray
|
Partial derivative of Bx with respect to y. |
dBy_dx |
ndarray
|
Partial derivative of By with respect to x. |
dBy_dy |
ndarray
|
Partial derivative of By with respect to y. |
Source code in src/pymagnet/utils/_vector_structs.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
Jacobian3
dataclass
3D Jacobian tensor of the magnetic field: J_ij = dB_i/dx_j.
Attributes:
| Name | Type | Description |
|---|---|---|
dBx_dx |
ndarray
|
Partial derivative of Bx with respect to x. |
dBx_dy |
ndarray
|
Partial derivative of Bx with respect to y. |
dBx_dz |
ndarray
|
Partial derivative of Bx with respect to z. |
dBy_dx |
ndarray
|
Partial derivative of By with respect to x. |
dBy_dy |
ndarray
|
Partial derivative of By with respect to y. |
dBy_dz |
ndarray
|
Partial derivative of By with respect to z. |
dBz_dx |
ndarray
|
Partial derivative of Bz with respect to x. |
dBz_dy |
ndarray
|
Partial derivative of Bz with respect to y. |
dBz_dz |
ndarray
|
Partial derivative of Bz with respect to z. |
Source code in src/pymagnet/utils/_vector_structs.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
Point_Array1
1D point structure This is used to contain one position array (z), and the units ('mm', 'cm', etc)
Source code in src/pymagnet/utils/_vector_structs.py
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 | |
__init__(z, unit='mm')
Init method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
ndarray
|
z coordinates |
required |
unit
|
str
|
Unit of length. Defaults to "mm". |
'mm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. km, m, cm, mm |
Source code in src/pymagnet/utils/_vector_structs.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
change_unit(new_unit, get_unit_value=get_unit_value_meter)
Converts point array to a different unit. e.g from 'cm' to 'mm'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_meter. |
get_unit_value_meter
|
Source code in src/pymagnet/utils/_vector_structs.py
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 | |
get_unit()
Gets unit
Returns:
| Type | Description |
|---|---|
str
|
unit |
Source code in src/pymagnet/utils/_vector_structs.py
47 48 49 50 51 52 53 | |
Point_Array2
2D point structure This is used to contain two position arrays (x, y), and the units ('mm', 'cm', etc)
Source code in src/pymagnet/utils/_vector_structs.py
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 | |
__init__(x, y, unit='mm')
Init Method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
unit
|
str
|
Unit of length. Defaults to "mm". |
'mm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. km, m, cm, mm |
Source code in src/pymagnet/utils/_vector_structs.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
change_unit(new_unit, get_unit_value=get_unit_value_meter)
Converts point array to a different unit. e.g from 'cm' to 'mm'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_meter. |
get_unit_value_meter
|
Source code in src/pymagnet/utils/_vector_structs.py
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 | |
Point_Array3
Bases: Point_Array2
3D point structure This is used to contain three position arrays (x, y, z), and the units ('mm', 'cm', etc)
Source code in src/pymagnet/utils/_vector_structs.py
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
__init__(x, y, z, unit='mm')
Init Method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates |
required |
y
|
ndarray
|
y coordinates |
required |
z
|
ndarray
|
z coordinates |
required |
unit
|
str
|
Unit of length. Defaults to "mm". |
'mm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unit must an SI prefix, e.g. km, m, cm, mm |
Source code in src/pymagnet/utils/_vector_structs.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
change_unit(new_unit, get_unit_value=get_unit_value_meter)
Converts point array to a different unit. e.g from 'cm' to 'mm'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_unit
|
str
|
unit to be converted to |
required |
get_unit_value
|
function
|
Function for checking unit type. Defaults to get_unit_value_meter. |
get_unit_value_meter
|
Source code in src/pymagnet/utils/_vector_structs.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
rotate(alpha, beta, gamma)
Rotates set of Point_Array3 points by angles alpha, beta, gamma
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Angle to rotate about z in degrees |
required |
beta
|
float
|
Angle to rotate about y in degrees |
required |
gamma
|
float
|
Angle to rotate about x in degrees |
required |
Returns:
| Type | Description |
|---|---|
Point_Array3
|
rotated set of points |
Source code in src/pymagnet/utils/_vector_structs.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |