mag_field1.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Drag the red points to modify the wire path
  2. Press "b" to compute the magnetic field"""
  3. import numpy as np
  4. from vedo import settings, mag, utils
  5. from vedo import Arrows, Points, Axes, Plotter, Text2D, Circle
  6. def func(evt):
  7. if evt.keypress != "b":
  8. return
  9. txt.text("..computing field in space, please wait!")
  10. txt.c('red5').background('yellow7')
  11. plt.render()
  12. pts = sptool.spline().points # extract the current spline
  13. field = []
  14. for probe in probes:
  15. B = np.zeros(3)
  16. for p0,p1 in zip(pts, np.roll(pts,1, axis=0)):
  17. p = (p0+p1)/2
  18. r = mag(p-probe)
  19. B += np.cross(p1-p0, p-probe)/r**3 # Biot-Savart law
  20. B /= max(1, mag(B)) # clamp the field magnitude near the wire
  21. field.append(B)
  22. field = np.array(field)
  23. arrows = Arrows(probes, probes+field/5).c('black')
  24. txt.text(__doc__).c('black').background(None)
  25. ppts1 = Points(probes)
  26. ppts1.pointdata["BField"] = field
  27. domain = ppts1.tovolume(n=4, dims=(10,10,10)) # interpolate
  28. ppts2 = ppts1.clone() # make a copy
  29. ppts2.pointdata["BFieldIntensity"] = mag(field*255/3).astype(np.uint8)
  30. vol = ppts2.tovolume(n=4, dims=(10,10,10)).crop(back=0.5)
  31. isos = vol.isosurface(np.arange(0,250, 12)).smooth()
  32. isos.cmap("rainbow").lighting('off').alpha(0.5).add_scalarbar()
  33. isos.name = "Isosurfaces"
  34. streamlines = domain.compute_streamlines(
  35. probes,
  36. max_propagation=0.5,
  37. initial_step_size=0.01,
  38. direction="both",
  39. )
  40. streamlines.c('black').linewidth(2)
  41. plt.remove("Arrows", "StreamLines", "Isosurfaces", "Axes")
  42. plt.add(arrows, streamlines, isos, Axes(streamlines)).render()
  43. probes = utils.pack_spheres([-2,2, -2,2, -2,2], radius=0.7)
  44. settings.use_depth_peeling = True
  45. settings.multi_samples = 0
  46. plt = Plotter()
  47. plt.add_callback("key press", func)
  48. txt = Text2D(__doc__, font="Kanopus")
  49. plt += txt
  50. # Create a set of points in space to form a spline
  51. circle = Circle(res=8) # resolution = 8 points
  52. sptool = plt.add_spline_tool(circle, pc='red', lw=4, closed=True)
  53. plt.show().close()