Skip to content

Commit 1c941bf

Browse files
committed
doku fix all examples and references
1 parent 74f3124 commit 1c941bf

11 files changed

+111
-90
lines changed

docs/_pages/0_documentation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ Then top level function ``displaySystem(c)`` can be used to quickly check the ge
416416
* ``suppress=True`` for suppressing the immediate figure output when the function is called. To do so it is necessary to deactivate the interactive mode by calling ``pyplot.ioff()``. With `Spyder's <https://www.spyder-ide.org/>`_ IPython *Inline* plotting, graphs made with :meth:`~magpylib.displaySystem()` can be blank if the ``suppress=True`` option is not used. Set IPython Graphics backend to *Automatic* or *Qt5* instead of *Inline* in settings/IPython console/Graphics method to address this.
417417
* ``direc=True`` for displaying current and magnetization directions in the figure.
418418
* ``subplotAx=None`` for displaying the plot on a designated figure subplot instance.
419+
* ``figsize=(8,8)`` for setting the size of the output graphic.
419420

420421
The following example code shows how to use ``displaySystem()``:
421422

docs/_pages/2_guideExamples.rst

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@
44
Example Codes
55
*******************************
66

7-
This section includes a few code examples that show how the library can be used. Detailed information about the library structure can be found in the :ref:`docu`.
7+
This section includes a few code examples that show how the library can be used and what i can be used for. A detailed technical library documentation can be found in the :ref:`docu`.
8+
9+
10+
Contents
11+
########
12+
13+
* :ref:`examples-simplest`
14+
* :ref:`examples-basic`
15+
* :ref:`examples-sourceObjects`
16+
* :ref:`examples-motionBasics`
17+
* :ref:`examples-joystick`
18+
* :ref:`examples-complexShapes`
19+
* :ref:`examples-vector`
820

921

1022

@@ -30,7 +42,7 @@ The simplest possible example - calculate the B-field of a cylinder with 3 lines
3042
Basic Functionality: The Field of a Collection
3143
###############################################
3244

33-
In this example the basic functionality is outlined by calculating the field of two magnets. The magnets are geometrically manipulated and the system geometry is displayed using the `displaySystem` method. The field is then calculated on a grid and displayed in the xz-plane.
45+
In this example the basic functionality is outlined. Two magnet objects are created and geometrically manipulated. The system geometry is then displayed using the ``displaySystem`` function. Finally, the field is calculated on a grid and displayed in the xz-plane.
3446

3547
.. plot:: pyplots/examples/01_SimpleCollection.py
3648
:include-source:
@@ -44,7 +56,7 @@ In this example the basic functionality is outlined by calculating the field of
4456
The Source Objects and their Fields
4557
###################################
4658

47-
In this example we define all existing source objects and display their fields. Notice that the respective magnetization vectors are chosen arbitrarily.
59+
In this example we define all currently implemented source objects and display their fields. Notice that the respective magnetization vectors are chosen arbitrarily.
4860

4961
.. plot:: pyplots/examples/01b_AllSources.py
5062
:include-source:
@@ -58,34 +70,38 @@ In this example we define all existing source objects and display their fields.
5870
Translation, Orientation and Rotation Basics
5971
#############################################
6072

61-
Translation of magnets can be realized in three ways, using the methods `move` and `setPosition`, or by directly setting the object `position` attribute.
73+
Translation of magnets can be realized in three ways, using the methods ``move`` and ``setPosition``, or by directly setting the object ``position`` attribute.
6274

6375
.. plot:: pyplots/examples/00a_Trans.py
6476
:include-source:
6577

6678
:download:`00a_Trans.py <../pyplots/examples/00a_Trans.py>`
6779

68-
The next example shows a cubical magnet initialized with four different orientations defined by the classical Euler angle rotations about the three Cartesian axes. Notice that the magnetization direction is defined in the INIT ORIENTATION so that different orientations results in a rotation of the magnetization vector.
80+
81+
Initialize magnets with different orientations defined by classical Euler angle rotations about the three Cartesian axes. Notice that the magnetization direction is fixed with respect to the **init orientation** of the magnet and will rotate together with the magnet.
6982

7083
.. plot:: pyplots/examples/00b_OrientRot1.py
7184
:include-source:
7285

7386
:download:`00b_OrientRot1.py <../pyplots/examples/00b_OrientRot1.py>`
7487

75-
The following example shows a general form of orientation for different angles about an axis (1,-1,1). The upper three boxes are initialized with different orientations. The lower three boxes are all initialized with INIT ORIENTATION and are then rotated (about themselves) to achieve the same result as above.
88+
89+
The following example shows functionality beyond Euler angle rotation. This means rotation about an arbitrary axis of choice, here ``(1,-1,1)``. The upper three boxes are initialized with different orientations. The lower three boxes are all initialized with **init orientation** and are then rotated (about themselves) to achieve the same result as above.
7690

7791
.. plot:: pyplots/examples/00c_OrientRot2.py
7892
:include-source:
7993

8094
:download:`00c_OrientRot2.py <../pyplots/examples/00c_OrientRot2.py>`
8195

82-
The following example shows rotations with designated anchor-axis combinations. Here we distinguish between pivot points (the closest point on the rotation axis to the magnet) and anchor points which are simply required to define an axis in 3D space (together with the direction).
96+
97+
The following example shows rotations with designated anchor-axis combinations. Here we distinguish between pivot points (the closest point on the rotation axis to the magnet) and anchor points which are simply required to define an axis in 3D space (together with a direction).
8398

8499
.. plot:: pyplots/examples/00d_OrientRot3.py
85100
:include-source:
86101

87102
:download:`00d_OrientRot3.py <../pyplots/examples/00d_OrientRot3.py>`
88103

104+
89105
Collections can be manipulated using the previous logic as well. Notice how objects can be grouped into collections and sub-collections for common manipulation. For rotations keep in mind that if an anchor is not provided, all objects will rotate relative to their own center.
90106

91107
.. plot:: pyplots/examples/00e_ColTransRot.py
@@ -100,9 +116,9 @@ Collections can be manipulated using the previous logic as well. Notice how obje
100116
Magnet Motion: Simulating a Magnetic Joystick
101117
##############################################
102118

103-
In this example a joystick is simulated. A magnetic joystick is realized by a rod that can tilt freely (two degrees of freedom) about a center of tilt. The upper part of the rod is the joystick handle. At the bottom of the rod a cylindrical magnet (dimension *D/H*) with axial magnetization (amplitude *M0*) is fixed. The magnet lies at a distance *d* below the center of tilt. The system is constructed such that, when the joystick is in the center position a sensor lies at distance *gap* below the magnet and in the origin of a Cartesian coordinate system. The magnet thus moves with the joystick above the fixed sensor.
119+
In this example a joystick is simulated. A magnetic joystick is realized by a rod that can tilt freely (two degrees of freedom) about a center of tilt. The upper part of the rod is the joystick handle. At the bottom of the rod a cylindrical magnet (``dim=(D,H)``) with axial magnetization ```mag=[0,0,M0]`` is fixed. The magnet lies at a distance ``d`` below the center of tilt. The system is constructed such that, when the joystick is in the center position a sensor lies at distance ``gap`` below the magnet and in the origin of a Cartesian coordinate system. The magnet thus moves with the joystick above the fixed sensor.
104120

105-
In the following program the magnetic field is calculated for all degrees of freedom. Different tilt angles are set by rotation about the center of tilt by the angle *th* (different colors). Then the tilt direction is varied from 0 to 360 degrees by simulating the magnet 'motion' as rotation about the z-axis, see also the following sketch.
121+
In the following program the magnetic field is calculated for all degrees of freedom. Different tilt angles are set by rotation about the center of tilt by the angle ``th`` (different colors). Then the tilt direction is varied from 0 to 360 degrees by simulating the magnet motion as rotation about the z-axis, see also the following sketch.
106122

107123
.. image:: ../_static/images/examples/JoystickExample1.JPG
108124
:align: center
@@ -120,7 +136,7 @@ In the following program the magnetic field is calculated for all degrees of fre
120136
Complex Magnet Shapes: Hollow Cylinder
121137
###########################################
122138

123-
The superposition principle allows us to calculate complex magnet shapes by 'addition' and 'subtraction' operations. A common application for this is the field of an axially magnetized hollow cylinder. The hollow part is cut out of the first cylinder by placing a second, smaller cylinder inside with opposite magnetization. Unfortunately the `displaySystem` method cannot properly display such objects intersecting with each other.
139+
The superposition principle allows us to calculate complex magnet shapes by *addition* and *subtraction* operations. An example application for this is the field of an axially magnetized hollow cylinder. The hollow part is cut out from the outer cylinder by placing a second, smaller cylinder inside with opposite magnetization. Unfortunately the ``displaySystem`` method cannot properly display such objects intersecting with each other.
124140

125141
.. plot:: pyplots/examples/04_ComplexShape.py
126142
:include-source:

docs/pyplots/examples/00a_Trans.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from magpylib.source.magnet import Box
1+
import numpy as np
22
import magpylib as magpy
3-
from numpy import array
4-
import matplotlib.pyplot as plt
3+
from magpylib.source.magnet import Box
54

6-
#fixed magnet parameters
5+
# fixed magnet parameters
76
M = [0,0,1] #magnetization
87
D = [2,2,2] #dimension
98

@@ -21,13 +20,10 @@
2120
s4.setPosition([2,0,-2])
2221

2322
s5 = Box(mag=M, dim=D, pos = [ 4,0, 4])
24-
s5.position = array([4,0,0])
23+
s5.position = np.array([4,0,0])
2524

2625
#collection
2726
c = magpy.Collection(s1,s2,s3,s4,s5)
2827

2928
#display collection
30-
fig = magpy.displaySystem(c,suppress=True)
31-
fig.set_size_inches(6, 6)
32-
33-
plt.show(fig)
29+
magpy.displaySystem(c,figsize=(6,6))
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from magpylib.source.magnet import Box
22
import magpylib as magpy
33

4-
#fixed magnet parameters
4+
# fixed magnet parameters
55
M = [1,0,0] #magnetization
6-
D = [3,3,3] #dimension
6+
D = [4,2,2] #dimension
77

88
# magnets with Euler angle orientations
99
s1 = Box(mag=M, dim=D, pos = [-4,0, 4])
1010
s2 = Box(mag=M, dim=D, pos = [ 4,0, 4], angle=45, axis=[0,0,1])
1111
s3 = Box(mag=M, dim=D, pos = [-4,0,-4], angle=45, axis=[0,1,0])
1212
s4 = Box(mag=M, dim=D, pos = [ 4,0,-4], angle=45, axis=[1,0,0])
1313

14-
#collection
14+
# collection
1515
c = magpy.Collection(s1,s2,s3,s4)
1616

17-
#display collection
18-
magpy.displaySystem(c,direc=True)
17+
# display collection
18+
magpy.displaySystem(c,direc=True,figsize=(6,6))
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import magpylib as magpy
22
from magpylib.source.magnet import Box
33

4-
#fixed magnet parameters
4+
# fixed magnet parameters
55
M = [0,0,1] #magnetization
66
D = [3,3,3] #dimension
77

8-
#rotation axis
8+
# rotation axis
99
rax = [-1,1,-1]
1010

11-
#magnets with different orientations
11+
# magnets with different orientations
1212
s1 = Box(mag=M, dim=D, pos=[-6,0,4], angle=0, axis=rax)
1313
s2 = Box(mag=M, dim=D, pos=[ 0,0,4], angle=45, axis=rax)
1414
s3 = Box(mag=M, dim=D, pos=[ 6,0,4], angle=90, axis=rax)
1515

16-
#magnets that are rotated differently
16+
# magnets that are rotated differently
1717
s4 = Box(mag=M, dim=D, pos=[-6,0,-4])
1818
s5 = Box(mag=M, dim=D, pos=[ 0,0,-4])
1919
s5.rotate(45,rax)
2020
s6 = Box(mag=M, dim=D, pos=[ 6,0,-4])
2121
s6.rotate(90,rax)
2222

23-
#collect all
23+
# collect all sources
2424
c = magpy.Collection(s1,s2,s3,s4,s5,s6)
2525

26-
#display collection
27-
magpy.displaySystem(c)
26+
# display collection
27+
magpy.displaySystem(c,figsize=(6,6))

docs/pyplots/examples/00d_OrientRot3.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from magpylib.source.magnet import Box
33
import matplotlib.pyplot as plt
44

5-
#define figure
6-
fig = plt.figure(figsize=(5, 5))
5+
# define figure
6+
fig = plt.figure(figsize=(6,6))
77
ax = fig.add_subplot(1,1,1, projection='3d')
88

9-
#fixed magnet parameters
9+
# fixed magnet parameters
1010
M = [0,0,1] #magnetization
1111
D = [2,4,1] #dimension
1212

13-
#define magnets rotated with different pivot and anchor points
13+
# define magnets rotated with different pivot and anchor points
1414
piv1 = [-7,0,5]
1515
s1 = Box(mag=M, dim=D, pos = [-7,-3,5])
1616

@@ -36,15 +36,19 @@
3636
s6 = Box(mag=M, dim=D, pos = [7,-3,-5])
3737
s6.rotate(-45,[0,0,1],anchor=anch6)
3838

39-
#collect all sources
39+
# collect all sources
4040
c = magpy.Collection(s1,s2,s3,s4,s5,s6)
4141

42-
#draw rotation axes
42+
# draw rotation axes
4343
for x in [-7,0,7]:
4444
for z in [-5,5]:
4545
ax.plot([x,x],[0,0],[z-3,z+4],color='.3')
4646

47-
#display collection and markers
47+
# define markers
4848
Ms = [piv1+['piv1'], piv2+['piv2'], piv3+['piv3'], piv4+['piv4'],
4949
piv5+['piv5'], piv6+['piv6'], anch4+['anch4'],anch5+['anch5'],anch6+['anch6']]
50-
magpy.displaySystem(c,subplotAx=ax,markers=Ms)
50+
51+
# display system
52+
magpy.displaySystem(c,subplotAx=ax,markers=Ms,suppress=True)
53+
54+
plt.show()

docs/pyplots/examples/00e_ColTransRot.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@
22
from magpylib.source.current import Circular
33
from numpy import linspace
44

5-
#windings of three parts of a coil at x/y = 0/0
6-
coil1a = [Circular(curr=1,dim=3,pos=[0,0,z]) for z in linspace( -3,-1,20)]
7-
coil1b = [Circular(curr=1,dim=3,pos=[0,0,z]) for z in linspace( -1, 1,20)]
8-
coil1c = [Circular(curr=1,dim=3,pos=[0,0,z]) for z in linspace( 1, 3,20)]
5+
# windings of three parts of a coil
6+
coil1a = [Circular(curr=1,dim=3,pos=[0,0,z]) for z in linspace( -3,-1,10)]
7+
coil1b = [Circular(curr=1,dim=3,pos=[0,0,z]) for z in linspace( -1, 1,10)]
8+
coil1c = [Circular(curr=1,dim=3,pos=[0,0,z]) for z in linspace( 1, 3,10)]
99

10-
#create collection and manipulate step by step
10+
# create collection and manipulate step by step
1111
c1 = magpy.Collection(coil1a)
1212
c1.move([-1,-1,0])
1313
c1.addSources(coil1b)
1414
c1.move([-1,-1,0])
1515
c1.addSources(coil1c)
1616
c1.move([-1,-1,0])
1717

18-
#windings of three parts of another coil at x/y = 3/3
19-
coil2a = [Circular(curr=1,dim=3,pos=[3,3,z]) for z in linspace(-3,-1,20)]
20-
coil2b = [Circular(curr=1,dim=3,pos=[3,3,z]) for z in linspace( -1,1,20)]
21-
coil2c = [Circular(curr=1,dim=3,pos=[3,3,z]) for z in linspace( 1,3,20)]
18+
# windings of three parts of another coil
19+
coil2a = [Circular(curr=1,dim=3,pos=[3,3,z]) for z in linspace(-3,-1,15)]
20+
coil2b = [Circular(curr=1,dim=3,pos=[3,3,z]) for z in linspace( -1,1,15)]
21+
coil2c = [Circular(curr=1,dim=3,pos=[3,3,z]) for z in linspace( 1,3,15)]
2222

23-
#create individual sub-collections
23+
# create individual sub-collections
2424
c2a = magpy.Collection(coil2a)
2525
c2b = magpy.Collection(coil2b)
2626
c2c = magpy.Collection(coil2c)
2727

28-
#combine sub-collections to one big collection
28+
# combine sub-collections to one big collection
2929
c2 = magpy.Collection(c2a,c2b,c2c)
3030

31-
#still manipulate each individual sub-collection
31+
# still manipulate each individual sub-collection
3232
c2a.rotate(-15,[1,-1,0],anchor=[0,0,0])
3333
c2c.rotate(15,[1,-1,0],anchor=[0,0,0])
3434

35-
#combine all collectins and display system
36-
magpy.displaySystem([c1,c2])
35+
# combine all collections and display system
36+
c3 = magpy.Collection(c1,c2)
37+
magpy.displaySystem(c3,figsize=(6,6))

docs/pyplots/examples/01_SimpleCollection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
# create figure
77
fig = plt.figure(figsize=(8,4))
8-
ax1 = fig.add_subplot(121, projection='3d')
9-
ax2 = fig.add_subplot(122)
8+
ax1 = fig.add_subplot(121, projection='3d') # this is a 3D-plotting-axis
9+
ax2 = fig.add_subplot(122) # this is a 2D-plotting-axis
1010

1111
# create magnets
1212
s1 = magpy.source.magnet.Box(mag=[0,0,600],dim=[3,3,3],pos=[-4,0,3])
@@ -19,7 +19,7 @@
1919
# create collection
2020
c = magpy.Collection(s1,s2)
2121

22-
# display system geometry
22+
# display system geometry on ax1
2323
magpy.displaySystem(c,subplotAx=ax1,suppress=True)
2424

2525
# calculate B-field on a grid
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import magpylib as magpy
2-
from numpy import array, linspace, meshgrid
3-
from numpy.linalg import norm
2+
import numpy as np
43
from matplotlib import pyplot as plt
54

6-
#set font size and define figures
5+
# set font size and define figures
76
plt.rcParams.update({'font.size': 6})
87

98
fig1 = plt.figure(figsize=(8, 5))
@@ -14,12 +13,12 @@
1413
axsA += [fig2.add_subplot(2,3,i, projection='3d') for i in range(1,4)]
1514
axsB += [fig2.add_subplot(2,3,i) for i in range(4,7)]
1615

17-
#position grid
18-
ts = linspace(-6,6,50)
19-
posis = array([(x,0,z) for z in ts for x in ts])
20-
X,Y = meshgrid(ts,ts)
16+
# position grid
17+
ts = np.linspace(-6,6,50)
18+
posis = np.array([(x,0,z) for z in ts for x in ts])
19+
X,Y = np.meshgrid(ts,ts)
2120

22-
# source definitions
21+
# create the source objects
2322
s1 = magpy.source.magnet.Box(mag=[500,0,500], dim=[4,4,4]) #Box
2423
s2 = magpy.source.magnet.Cylinder(mag=[0,0,500], dim=[3,5]) #Cylinder
2524
s3 = magpy.source.magnet.Sphere(mag=[-200,0,500], dim=5) #Sphere
@@ -29,14 +28,13 @@
2928

3029
for i,s in enumerate([s1,s2,s3,s4,s5,s6]):
3130

32-
#plot geometry in memory
31+
# display system on respective axes, use marker to zoom out
3332
magpy.displaySystem(s,subplotAx=axsA[i],markers=[(6,0,6)],suppress=True)
3433
axsA[i].plot([-6,6,6,-6,-6],[0,0,0,0,0],[-6,-6,6,6,-6])
3534

36-
#plot field in memory
37-
B = array([s.getB(p) for p in posis]).reshape(50,50,3)
38-
axsB[i].pcolor(X,Y,norm(B,axis=2),cmap=plt.cm.get_cmap('coolwarm'))
39-
axsB[i].streamplot(X, Y, B[:,:,0], B[:,:,2], color='k',linewidth=1)
35+
# plot field on respective axes
36+
B = np.array([s.getB(p) for p in posis]).reshape(50,50,3)
37+
axsB[i].pcolor(X,Y,np.linalg.norm(B,axis=2),cmap=plt.cm.get_cmap('coolwarm')) # amplitude
38+
axsB[i].streamplot(X, Y, B[:,:,0], B[:,:,2], color='k',linewidth=1) # field lines
4039

41-
#display plots
4240
plt.show()

0 commit comments

Comments
 (0)