Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
5fc5151
add volume
OrtnerMichael Jul 21, 2025
d6c31ae
add tests
OrtnerMichael Jul 21, 2025
4d1b491
rename testfile
OrtnerMichael Jul 21, 2025
8379d7d
lint
OrtnerMichael Jul 21, 2025
4ee67c5
pylint
OrtnerMichael Jul 21, 2025
64f1a35
nox
OrtnerMichael Jul 21, 2025
4a6a042
add to changelog
OrtnerMichael Jul 21, 2025
8832ba7
style: pre-commit fixes
pre-commit-ci[bot] Jul 21, 2025
ef88616
implement in baseGeo
OrtnerMichael Jul 23, 2025
8795381
pylint
OrtnerMichael Jul 23, 2025
c9395a1
centroid implementation
OrtnerMichael Jul 23, 2025
efe6d35
add tests
OrtnerMichael Jul 23, 2025
8e71c57
fixed tests
OrtnerMichael Jul 23, 2025
50cf95e
nox lint
OrtnerMichael Jul 23, 2025
3db904e
pylint
OrtnerMichael Jul 23, 2025
299098b
style: pre-commit fixes
pre-commit-ci[bot] Jul 23, 2025
c4e7d81
centroid to changelog
OrtnerMichael Jul 23, 2025
c094ad3
Merge branch 'volume' of https://github.com/magpylib/magpylib into vo…
OrtnerMichael Jul 23, 2025
4e7ddef
first step
OrtnerMichael Jul 24, 2025
46d7e79
WIP
OrtnerMichael Jul 25, 2025
3d1fefd
FT working first time
OrtnerMichael Jul 28, 2025
971c348
working even better now
OrtnerMichael Jul 28, 2025
52f50e5
magnet force looking good
OrtnerMichael Jul 29, 2025
2cd913c
first working current codes
OrtnerMichael Jul 29, 2025
41e2d31
adding dipole
OrtnerMichael Jul 30, 2025
0cd2f08
dipole fix
OrtnerMichael Jul 30, 2025
0ab619d
add sphere
OrtnerMichael Jul 30, 2025
0d1a617
computation proofed by tests
OrtnerMichael Jul 30, 2025
7a13b2e
remove print
OrtnerMichael Jul 30, 2025
dca5830
temp broadcasting solution
OrtnerMichael Jul 31, 2025
2e880dd
cylinder segment force
OrtnerMichael Jul 31, 2025
c298669
sphere fix, add tetra, HCP mesh (not very good)
OrtnerMichael Jul 31, 2025
38b3d9d
HCP mesh + tetra
OrtnerMichael Jul 31, 2025
c6e382a
fixing tetra meshing issues
OrtnerMichael Jul 31, 2025
94c655d
adding trimesh :)
OrtnerMichael Jul 31, 2025
8dc8f9b
improve and complete meshings and tests
OrtnerMichael Jul 31, 2025
328610c
Collection now works as target
OrtnerMichael Aug 1, 2025
54515c5
meshing and checking workflow
OrtnerMichael Aug 1, 2025
898978c
fixing meshing docstrings
OrtnerMichael Aug 1, 2025
eaf6d69
add meshreport to getFT
OrtnerMichael Aug 1, 2025
856b469
improve meshing
OrtnerMichael Aug 2, 2025
96a2381
temp tests
OrtnerMichael Aug 2, 2025
b66804a
tests + Polyline fix
OrtnerMichael Aug 2, 2025
1faf3f6
wip
OrtnerMichael Aug 3, 2025
826289d
Merge branch 'main' of https://github.com/magpylib/magpylib into forc…
OrtnerMichael Aug 3, 2025
6eab063
add _centroid, fix tests
OrtnerMichael Aug 3, 2025
99204fb
wip
OrtnerMichael Aug 3, 2025
becee71
WIP ;|
OrtnerMichael Aug 9, 2025
e120d5f
first working path WIP
OrtnerMichael Aug 9, 2025
f329faa
WIP
OrtnerMichael Aug 10, 2025
87432f7
improve getFT flow
OrtnerMichael Aug 11, 2025
7f49d2b
WIP path tests, flow improve
OrtnerMichael Aug 11, 2025
49726bf
WIP reshapeing
OrtnerMichael Aug 11, 2025
2cc2cdd
wip advanced indexing
OrtnerMichael Aug 11, 2025
c89a0c2
WIP imprve getFT flow
OrtnerMichael Aug 11, 2025
c3a06ab
WIP nightmare :(
OrtnerMichael Aug 12, 2025
460c7a3
WIP F***
OrtnerMichael Aug 13, 2025
bdeec20
WIP fail#3
OrtnerMichael Aug 14, 2025
49b0999
WIP omg its working
OrtnerMichael Aug 14, 2025
1c3ee44
WIP getFT with path FINALLY SEEMS TO WORK ;););)
OrtnerMichael Aug 16, 2025
ef1cbcb
improve tests - improve workflow - looking good
OrtnerMichael Aug 16, 2025
9f435e7
tests
OrtnerMichael Aug 17, 2025
32dd0b9
tests
OrtnerMichael Aug 17, 2025
c22b9b1
bringing sphere back
OrtnerMichael Aug 17, 2025
91a966b
cuboid online
OrtnerMichael Aug 17, 2025
98b746b
brining more tests back online
OrtnerMichael Aug 17, 2025
a53ad29
cylinder online
OrtnerMichael Aug 17, 2025
ca65693
CylinderSegment online
OrtnerMichael Aug 17, 2025
3449070
reintroduce tests
OrtnerMichael Aug 18, 2025
b5d1ac2
tetrahedron online
OrtnerMichael Aug 18, 2025
4e26f2d
reintro TriMesh
OrtnerMichael Aug 18, 2025
e6ae59b
add meshing=1 tests, massive sphere improvement
OrtnerMichael Aug 18, 2025
61cb3c0
complete testing
OrtnerMichael Aug 18, 2025
1a2d1d5
improve pivot workflow
OrtnerMichael Aug 18, 2025
b3bcd48
moving tests to test dir
OrtnerMichael Aug 19, 2025
cbe5f2f
clean up and fix tests
OrtnerMichael Aug 19, 2025
13907f6
clean up field_FT
OrtnerMichael Aug 19, 2025
e98a787
review BaseTarget
OrtnerMichael Aug 19, 2025
991ad30
doc strings
OrtnerMichael Aug 19, 2025
7e5e503
nox tests, nox lint
OrtnerMichael Aug 20, 2025
b1bde0e
nox it
OrtnerMichael Aug 20, 2025
3da1b86
docstring error
OrtnerMichael Aug 20, 2025
832cecc
terminology
OrtnerMichael Aug 20, 2025
67917e6
Merge branch 'main' of https://github.com/magpylib/magpylib into forc…
OrtnerMichael Aug 20, 2025
2700f52
WIP
OrtnerMichael Aug 21, 2025
7f7d212
bugfix meshing validation
OrtnerMichael Aug 21, 2025
698c0a0
WIP
OrtnerMichael Aug 21, 2025
e757177
fix sheet strip centroids
OrtnerMichael Aug 21, 2025
f7a0c52
trisheet force working
OrtnerMichael Aug 21, 2025
910eca5
object docstrings
OrtnerMichael Aug 21, 2025
815fc56
triangle strip now working
OrtnerMichael Aug 22, 2025
ca245e9
style: pre-commit fixes
pre-commit-ci[bot] Aug 22, 2025
ba4b237
style: pre-commit fixes
pre-commit-ci[bot] Aug 23, 2025
905bbe9
major broadcasting bug fixed :0
OrtnerMichael Aug 23, 2025
07cfa81
style: pre-commit fixes
pre-commit-ci[bot] Aug 24, 2025
3e79886
add bug tests, add trisheet tests
OrtnerMichael Aug 24, 2025
837aab3
Merge branch 'force_integration' of https://github.com/magpylib/magpy…
OrtnerMichael Aug 24, 2025
3c0b4e9
nox
OrtnerMichael Aug 24, 2025
bf331ea
Merge branch 'force_integration' of https://github.com/magpylib/magpy…
OrtnerMichael Aug 24, 2025
6ded500
improve getFT, remove current
OrtnerMichael Aug 25, 2025
fcd0417
review improve tests
OrtnerMichael Aug 25, 2025
d884ff2
nox
OrtnerMichael Aug 25, 2025
0a39489
fix macos test
OrtnerMichael Aug 25, 2025
019beb0
add returnmesh parameter
OrtnerMichael Aug 27, 2025
c41eb95
docs update with force ++
OrtnerMichael Sep 5, 2025
0d4fba2
nox lint
OrtnerMichael Sep 5, 2025
0e3129a
nox nox
OrtnerMichael Sep 5, 2025
46720be
changelog
OrtnerMichael Sep 5, 2025
c484347
style: pre-commit fixes
pre-commit-ci[bot] Sep 5, 2025
fb1fc4f
admonition on front page
OrtnerMichael Sep 5, 2025
fd96386
docs review titles, -----, physics & comp,
OrtnerMichael Sep 7, 2025
dff8192
changelog
OrtnerMichael Sep 7, 2025
34e626f
fixing some figures
OrtnerMichael Sep 7, 2025
92e092f
review example magnet colors
OrtnerMichael Sep 7, 2025
5b15d49
review examples_vis_animations
OrtnerMichael Sep 7, 2025
b7be3a4
nox fix
OrtnerMichael Sep 7, 2025
7c969fe
adding dipole_moment property
OrtnerMichael Sep 10, 2025
934c1ff
changelog
OrtnerMichael Sep 10, 2025
6afbebe
style: pre-commit fixes
pre-commit-ci[bot] Sep 10, 2025
519b8c8
separate volume, apply only to relevant classes
OrtnerMichael Sep 10, 2025
029c73f
Merge branch 'force_integration' of https://github.com/magpylib/magpy…
OrtnerMichael Sep 10, 2025
d1d8645
style: pre-commit fixes
pre-commit-ci[bot] Sep 10, 2025
9c152b5
separate dipole moment property, extend only to sensible classes
OrtnerMichael Sep 10, 2025
8983f5c
Merge branch 'force_integration' of https://github.com/magpylib/magpy…
OrtnerMichael Sep 10, 2025
ad797c5
nox fixes
OrtnerMichael Sep 10, 2025
9cecf1e
Scipy/Numpy Docstyle
OrtnerMichael Sep 10, 2025
f216950
Merge branch 'main' of https://github.com/magpylib/magpylib into forc…
Alexboiboi Sep 19, 2025
9ad4b06
Remove unnecessary dependency 'magpylib-force' from docs requirements
Alexboiboi Sep 19, 2025
4714825
linting: update test_force.py to ignore torque values in force tests
Alexboiboi Sep 19, 2025
fef8e38
docs: add image alt text for WIP
Alexboiboi Sep 19, 2025
b282888
docs: standardize parameter naming from `returnmesh` to `return_mesh`…
Alexboiboi Sep 19, 2025
58dce50
Merge branch 'main' of https://github.com/magpylib/magpylib into forc…
OrtnerMichael Sep 24, 2025
de95d18
style: pre-commit fixes
pre-commit-ci[bot] Sep 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding dipole_moment property
  • Loading branch information
OrtnerMichael committed Sep 10, 2025
commit 7c969fea59058de83352cdc01195c755e6069706
16 changes: 10 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [Unreleased] - YYYY-MM-DD
## [5.2.0] - 2024-MM-DD

- Documentation update ([#876](https://github.com/magpylib/magpylib/issues/876))
- Adding force computation `magpylib.getFT()` to the library.
Expand All @@ -10,11 +10,15 @@
- Added the `current_sheet_Hfield` core computation function, and the classes
`TriangleStrip` and `TriangleSheet` enabling current sheets in the object
oriented interface. ([#788](https://github.com/magpylib/magpylib/issues/788))
- Adding the `centroid` property to all Magpylib objects. This gives the same as
the `barycenter` property for the objects that have it.
([#864](https://github.com/magpylib/magpylib/issues/864))
- Adding the `volume` property to all Magpylib objects
([#864](https://github.com/magpylib/magpylib/issues/864))
- Adding new objects properties
- `centroid`: computes and returns object centroid. Gives the same as
`barycenter` for the objects that have it.
([#864](https://github.com/magpylib/magpylib/issues/864))
- `volume` computes and returns magnet volumes
([#864](https://github.com/magpylib/magpylib/issues/864))
- `dipole_moment` computes and returns object dipole moment (magnets and
closed currents only)
([#886](https://github.com/magpylib/magpylib/issues/886))
- Improved docstrings by adding examples where missing and by using rounding in
all examples to avoid doctest fails.
- Fixed a bug where a core getH would return the B-field.
Expand Down
19 changes: 19 additions & 0 deletions src/magpylib/_src/obj_classes/class_BaseGeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class BaseGeo(BaseTransform, ABC):
- orientation
- volume
- centroid
- moment
- style

Methods of BaseGeo
Expand Down Expand Up @@ -181,6 +182,19 @@ def _get_centroid(self, squeeze=True):
Centroid coordinates [(x, y, z), ...] in m.
"""

@abstractmethod
def _get_dipole_moment(self):
"""
Calculate and return the magnetic moment of the object in units Am² in the local object coordinates.

This method must be implemented by all subclasses.

Returns
-------
numpy.ndarray, shape (n,3) when there is a path, or squeeze(1,3) when not
Centroid coordinates [(x, y, z), ...] in m.
"""

# properties ----------------------------------------------------
@property
def parent(self):
Expand Down Expand Up @@ -304,6 +318,11 @@ def _centroid(self):
"""Centroid of object in units of m."""
return self._get_centroid(squeeze=False)

@property
def dipole_moment(self):
"""Magnetic moment of object in units of Am²."""
return self._get_dipole_moment()

@property
def style(self):
"""
Expand Down
10 changes: 9 additions & 1 deletion src/magpylib/_src/obj_classes/class_Collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,10 @@ class Collection(BaseGeo, BaseCollection):
Read-only. Collection centroid in units of m computed via the volume-weighted average of
all child centroids.

dipole_moment: np.ndarray, shape (3,)
Read-only. Total Collection dipole moment in units of A*m² computes from the sum of all
child dipole moments.

override_parent: bool, default=False
If False thrown an error when an attempt is made to add an object that
has already a parent to a Collection. If True, allow adding the object
Expand Down Expand Up @@ -988,7 +992,7 @@ def __init__(
# Abstract methods implementation
def _get_volume(self):
"""Volume of all objects in units of m³."""
return sum(child.volume for child in self.children_all)
return sum([child.volume for child in self.children_all]) # pylint: disable=consider-using-generator

def _get_centroid(self):
"""Centroid of collection weighted by children volumes in units of m."""
Expand All @@ -1005,3 +1009,7 @@ def _get_centroid(self):
if total_volume > 0:
return weighted_centroid / total_volume
return self.position

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
return np.sum([child.dipole_moment for child in self.children_all], axis=0)
4 changes: 4 additions & 0 deletions src/magpylib/_src/obj_classes/class_Sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ def _get_volume(self):
"""Volume of object in units of m³."""
return 0.0

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
return np.zeros(3)

def _get_centroid(self):
"""Centroid of object in units of m."""
if self.pixel is not None:
Expand Down
13 changes: 13 additions & 0 deletions src/magpylib/_src/obj_classes/class_current_Circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import warnings
from typing import ClassVar

import numpy as np

from magpylib._src.display.traces_core import make_Circle
from magpylib._src.exceptions import MagpylibDeprecationWarning
from magpylib._src.fields.field_BH_circle import BHJM_circle
Expand Down Expand Up @@ -53,6 +55,9 @@ class Circle(BaseCurrent, BaseTarget):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -139,6 +144,14 @@ def _get_centroid(self, squeeze=True):
return self.position
return self._position

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.diameter is None or self.current is None:
return np.array((0.0, 0.0, 0.0))

return self.diameter**2 / 4 * np.pi * self.current * np.array((0, 0, 1))

def _generate_mesh(self):
"""Generate mesh for force computation."""
return target_mesh_circle(self.diameter / 2, self.meshing, self.current)
Expand Down
21 changes: 21 additions & 0 deletions src/magpylib/_src/obj_classes/class_current_Polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Polyline(BaseCurrent, BaseTarget):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m - set to mean of vertices for this class.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -154,6 +157,24 @@ def _get_centroid(self, squeeze=True):
return np.mean(self.vertices, axis=0) + self._position
return self._position

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.vertices is None or self.current is None:
return np.array((0.0, 0.0, 0.0))
# test for closed polyline
if (len(self.vertices) > 1) and (np.all(self.vertices[0] == self.vertices[-1])):
return (
self.current
/ 2
* np.sum(np.cross(self.vertices[:-1], self.vertices[1:]), axis=0)
)
msg = (
f"Cannot compute dipole moment of {self}. Dipole moment is only defined for closed "
"Polylines (first and last vertex must be identical)."
)
raise ValueError(msg)

def _generate_mesh(self):
"""Generate mesh for force computation."""
# Tests in getFT ensure that meshing, dimension and excitation are set
Expand Down
9 changes: 9 additions & 0 deletions src/magpylib/_src/obj_classes/class_current_TriangleSheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import warnings
from typing import ClassVar

import numpy as np
Expand Down Expand Up @@ -195,6 +196,14 @@ def _get_centroid(self, squeeze=True):
return np.squeeze(centr)
return centr

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
warnings.warn(
"Magnetic moment is not implemented for TriangleSheet. Return 0.",
stacklevel=1,
)
return np.zeros(3)

def _generate_mesh(self):
"""Generate mesh for force computation."""
return target_mesh_triangle_current(
Expand Down
50 changes: 50 additions & 0 deletions src/magpylib/_src/obj_classes/class_current_TriangleStrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class TriangleStrip(BaseCurrent, BaseTarget):
Read-only. Object centroid in units of m given by mean of vertices.
m is the path length.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -158,6 +161,53 @@ def _get_centroid(self, squeeze=True):
return np.squeeze(centr)
return centr

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.vertices is None or self.current is None:
return np.array((0.0, 0.0, 0.0))
# test closed
if not np.allclose(self.vertices[:2], self.vertices[-2:]):
msg = (
f"Cannot compute dipole moment of {self}. Dipole moment is only defined for closed "
"CurrentStrips (first two and last two vertices must be identical)."
)
raise ValueError(msg)

# number of triangles
no_tris = len(self.vertices) - 2

# create triangles
trias = np.array([self.vertices[:-2], self.vertices[1:-1], self.vertices[2:]])
trias = np.swapaxes(trias, 0, 1)

centroids = np.array([(t[0] + t[1] + t[2]) / 3 for t in trias])
areas = 0.5 * np.linalg.norm(
np.cross(trias[:, 1] - trias[:, 0], trias[:, 2] - trias[:, 0]), axis=1
)

# create current density input
v1 = trias[:, 1] - trias[:, 0]
v2 = trias[:, 2] - trias[:, 0]
v1v1 = np.sum(v1 * v1, axis=1)
v2v2 = np.sum(v2 * v2, axis=1)
v1v2 = np.sum(v1 * v2, axis=1)

curr_densities = np.zeros((no_tris, 3), dtype=float)
# catch two times the same vertex in one triangle, and set CD to zero there
mask = (v2v2 != 0) * (v1v1 != 0)
h = np.sqrt(v1v1[mask] - (v1v2[mask] ** 2 / v2v2[mask]))
curr_densities[mask] = (
v2[mask]
/ (
np.sqrt(v2v2[mask]) * h / np.repeat(self.current, no_tris, axis=0)[mask]
)[:, np.newaxis]
)
# moment of one triangle: A / 2 * cent x curr_density
return np.sum(
areas[:, np.newaxis] / 2 * np.cross(centroids, curr_densities), axis=0
)

def _generate_mesh(self):
"""Generate mesh for force computation."""
triangles = np.array(
Expand Down
10 changes: 10 additions & 0 deletions src/magpylib/_src/obj_classes/class_magnet_Cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class Cuboid(BaseMagnet, BaseTarget):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -158,6 +161,13 @@ def _get_centroid(self, squeeze=True):
return self.position
return self._position

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.magnetization is None or self.dimension is None:
return np.array((0.0, 0.0, 0.0))
return self.magnetization * self.volume

def _generate_mesh(self):
"""Generate mesh for force computation."""
return target_mesh_cuboid(self.meshing, self.dimension, self.magnetization)
Expand Down
10 changes: 10 additions & 0 deletions src/magpylib/_src/obj_classes/class_magnet_Cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Cylinder(BaseMagnet, BaseTarget):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -157,6 +160,13 @@ def _get_centroid(self, squeeze=True):
return self.position
return self._position

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.magnetization is None or self.dimension is None:
return np.array((0.0, 0.0, 0.0))
return self.magnetization * self.volume

def _generate_mesh(self):
"""Generate mesh for force computation."""
# Tests in getFT ensure that meshing, dimension and excitation are set
Expand Down
10 changes: 10 additions & 0 deletions src/magpylib/_src/obj_classes/class_magnet_CylinderSegment.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class CylinderSegment(BaseMagnet, BaseTarget):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -177,6 +180,13 @@ def _get_centroid(self, squeeze=True):
return self.barycenter
return self._barycenter

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.magnetization is None or self.dimension is None:
return np.array((0.0, 0.0, 0.0))
return self.magnetization * self.volume

def _generate_mesh(self):
"""Generate mesh for force computation."""
# Tests in getFT ensure that meshing, dimension and excitation are set
Expand Down
10 changes: 10 additions & 0 deletions src/magpylib/_src/obj_classes/class_magnet_Sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Sphere(BaseMagnet):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -143,6 +146,13 @@ def _get_centroid(self, squeeze=True):
return self.position
return self._position

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.magnetization is None or self.diameter is None:
return np.array((0.0, 0.0, 0.0))
return self.magnetization * self.volume

def _generate_mesh(self):
"""Generate mesh for force computation."""
points = np.array([(0, 0, 0)])
Expand Down
10 changes: 10 additions & 0 deletions src/magpylib/_src/obj_classes/class_magnet_Tetrahedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Tetrahedron(BaseMagnet, BaseTarget):
centroid: np.ndarray, shape (3,) or (m,3)
Read-only. Object centroid in units of m.

dipole_moment: np.ndarray, shape (3,)
Read-only. Object dipole moment in units of A*m² in the local object coordinates.

parent: `Collection` object or `None`
The object is a child of it's parent collection.

Expand Down Expand Up @@ -183,6 +186,13 @@ def _get_centroid(self, squeeze=True):
return self.barycenter
return self._barycenter

def _get_dipole_moment(self):
"""Magnetic moment of object in units Am²."""
# test init
if self.magnetization is None or self.vertices is None:
return np.array((0.0, 0.0, 0.0))
return self.magnetization * self.volume

def _generate_mesh(self):
"""Generate mesh for force computation."""
return target_mesh_tetrahedron(self.meshing, self.vertices, self.magnetization)
Expand Down
Loading