Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make plotting utilities for field line trajectories, |B| from MagneticField objects, and B vector arrow plots in 3D #1413

Open
aknath23 opened this issue Dec 1, 2024 · 5 comments
Labels
P2 Medium Priority, not urgent but should be on the near-term agend plotting stuff about plotting routines

Comments

@aknath23
Copy link

aknath23 commented Dec 1, 2024

Hi,
I am quite new at using DESC.In the Stage-Two Filamentary Coil Optimization tutorial , i want to compute the magnetic field due to coils using the following command [magnetic_field = coilset.compute_magnetic_field(coords, basis="rpz")] after that how can i 3d plot the magnetic field along with the coilset.

@YigitElma
Copy link
Collaborator

YigitElma commented Dec 2, 2024

For now, plot_3d only works for Equilibrium quantities or B*n which is the normal field error on the last closed flux surface given field information. Maybe we can consider adding that functionality? @dpanici What do you think?

But for now, you can achieve what you want by changing the source code slightly like this. Btw, I assume you want to be able to plot the magnitude of the magnetic field created by coils on the last closed flux surface. Here,

Bnorm = jnp.sum(B * surf_normal, axis=-1)

change the line 347 by

Bnorm = jnp.linalg.norm(B, axis=-1)

This should change the plotted quantity to be the magnitude of the magnetic field created by coils. You may also want to change the title of the 3D figure here,

label = r"$\mathbf{B} \cdot \hat{n} ~(\mathrm{T})$"

Something like,

label = r"$\mathbf{|B|} ~(\mathrm{T})$"

This is a hacky way of doing it. So, I only recommend using it for plotting purposes, not during actual optimization!

@dpanici
Copy link
Collaborator

dpanici commented Dec 2, 2024

What do you mean by "3D magnetic field" specifically? our plot_3d function currently is actually plotting the quantity of interest (say B*n) on a surface in space, and takes in an Equilibrium or Surface object bc that is what provides the underlying surface geometry upon which to plot in 3d.

If you want to plot something in 3D from only a MagneticField object, you'd have to specify what surface in space you want to plot on (if you want something like plot_3d). If instead you want to visualize the magnetic field vectors (like an arrow plot but in 3D) that is a separate funcionality (but doable for sure). If you want to instead plot the magneitc field line trajectories in 3D space, that again is a different functionality that would have to go through our field_line_integrate function, but that also is doable.

Which of these is the desired functionality?

@dpanici dpanici added P2 Medium Priority, not urgent but should be on the near-term agend Needs Info plotting stuff about plotting routines labels Dec 2, 2024
@aknath23
Copy link
Author

aknath23 commented Dec 3, 2024

Currently we can plot Bn using plot_3d :
fig = plot_3d(eq.surface, "B
n", field=optimized_coilset, field_grid=coil_grid, grid=plot_grid)

what if i want to plot this :
fig = plot_3d(eq.surface, "|B|", field=optimized_coilset, field_grid=coil_grid, grid=plot_grid)
I have tried the solution provided by @YigitElma , but it didn’t work could you please confirm if it works, maybe i made some mistake.

Also ,it would be great if we could plot magnetic field vector (arrow plot) or magnetic field line trajectories.

@dpanici dpanici removed the Needs Info label Dec 3, 2024
@YigitElma
Copy link
Collaborator

I have tried the solution provided by @YigitElma , but it didn’t work could you please confirm if it works, maybe i made some mistake.

So, if you make the changes I provided and call the function as it is,

fig = plot_3d(
    eq.surface, "B*n", field=optimized_coilset, field_grid=coil_grid, grid=plot_grid
)

fig = plot_coils(optimized_coilset, fig=fig)
fig.show()

It should work. I tested it. Maybe you didn't restart your kernel and the changes in the source files is not reflected...

fig = plot_3d(eq.surface, "|B|", field=optimized_coilset, field_grid=coil_grid, grid=plot_grid)

If you call it like this, the |B| will be computed by the surface information not the coil information (so the additional kwargs field, field_grid won't be used). Currently, the only implemented way to use plot_3d for coils is to call it with a surface and ask for B*n by giving the field info. My solution was a hacky way to use the same inputs to plot |B|, of course the proper way of doing it will require some extra logic in plot_3d.

Also ,it would be great if we could plot magnetic field vector (arrow plot) or magnetic field line trajectories.

I believe this can be done. For vector plots, we can use quiver or something equivalent, but I am not sure how digestible that figure would be, bunch of arrows on 3D plot. Plotting the magnetic field lines could be better for visualization purposes, and as @dpanici mentioned it requires calling field_line_integrate which is computationally expensive and might take a couple minutes to generate the plot. But totally doable.

@aknath23
Copy link
Author

aknath23 commented Dec 4, 2024

It worked thanks for the suggestion @YigitElma.
You guys can think of implementing it in a proper way for future work.

@dpanici dpanici changed the title Plotting CoilSet.compute_magnetic_field Make plotting utilities for field line trajectories, |B| from MagneticField objects, and B vector arrow plots in 3D Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Medium Priority, not urgent but should be on the near-term agend plotting stuff about plotting routines
Projects
None yet
Development

No branches or pull requests

3 participants