magpylib icon indicating copy to clipboard operation
magpylib copied to clipboard

How to plot magnetic gradient map ?

Open caimingxue opened this issue 1 year ago • 5 comments

How to plot magnetic gradient map ?

caimingxue avatar Jul 27 '22 16:07 caimingxue

Hey @caimingxue

Long time no see :). Can you explain in detail what you mean by gradient map and what kind of output you would like to generate ?

sincerely, Michael

OrtnerMichael avatar Jul 28 '22 21:07 OrtnerMichael

Thanks for remembering me. If we have a magnetic source, and a magnetic agent, we can plot the field distribution around the magnetic agent, including 2D and 3D map, just like your current work. Meanwhile, if we want to study the magnetic force for the magnetic agent, we also can plot the gradient map around the agent. Finally, we can get the magnetic force according to the momentum of the agent. Just like heat map. In conclusion, if we have magnetic source and agent, we need to visualize the gradient around the agent, even the force map, if we know the agent's momentum. Thanks!

caimingxue avatar Jul 29 '22 16:07 caimingxue

Hey @caimingxue

Sorry for the late reply. Is your agent soft or hard magnetic ?

OrtnerMichael avatar Aug 03 '22 12:08 OrtnerMichael

sorry for the delay. My agent is hard magnetic

caimingxue avatar Aug 15 '22 16:08 caimingxue

Hi @caimingxue

If your agent is hard magnetic, with moment m, most likely you would like to apply the force equations F = grad(m.B) and T = m x B + F x r for force and torque respectively ?

I propose that you make use of finite difference in the following way:

# some source that generates a B-field
src = magpy.current.Loop(...)

# finite difference parameter
dx = 1e-6

# derivative of B with respect to x at position (x0,y0,z0)
dx_B = [src.getB((x0+dx,y0,z0)) - src.getB((x0-dx,y0,z0))] / 2 / dx

Example code for the derivative with respect to x, for the example in the documentation

import numpy as np
import matplotlib.pyplot as plt
import magpylib as magpy

fig, ax = plt.subplots(1, 1, figsize=(5,5))

# create an observer grid in the xz-symmetry plane
ts = np.linspace(-3, 3, 30)
grid = np.array([[(x,0,z) for x in ts] for z in ts])

# compute derivative with respect to x through finite difference
cube = magpy.magnet.Cuboid(magnetization=(500,0,500), dimension=(2,2,2))
eps = 1e-6
dx = np.array((eps,0,0))
dB_dx = (cube.getB(grid + dx) - cube.getB(grid - dx)) / (2*eps)

# display field with Pyplot
ax.streamplot(grid[:,:,0], grid[:,:,2], dB_dx[:,:,0], dB_dx[:,:,2], density=2,
    color=np.log(np.linalg.norm(dB_dx, axis=2)), linewidth=1, cmap='autumn')

# outline magnet boundary
ax.plot([1,1,-1,-1,1], [1,-1,-1,1,1], 'k--')

plt.tight_layout()
plt.show()

This actually looks nice :)

image

OrtnerMichael avatar Aug 17 '22 20:08 OrtnerMichael