My Coding >
Numerical simulations >
EMHD >
3D Electric Field lines with Mayavi
3D Electric Field lines with MayaviIn previous chapter I’ve already shown, how to draw continuous electric field lines with Matplotlib. But this was 2D case. In many scientific applications, it is necessary to plot 3dimensional field lines, and I will show how to do it with Mayavi
In is important to remember, that, despite high graphical quality of the plot, Mayavi is not very serious scientific tool and should be used with great caution. Calculating Electric fieldI already describe, how to calculate electric field die to point charges. But in all previous calculations I do use meshgrid coordinate system. Mayavi can only work with mgrid coordinate system of NumPy. Therefore we need to create domain and calculate everything is mgrid coordinate system Because we want to see Electric field lines around charges – our domain is a bit bigger than the space for the charges. Our charges can located in the space from 1 to 1, while all domain is calculation from 3 to 3 to have some space for electric lines around these charges.
After initializing our domain and random charges, we can calculate electric field. Again I do calculate squared distance first and then apply power of 1.5 to have r^{3} at the beginning of calculations.
Plotting Field Lines with MayaviTo use Mayavi plotting sistem, it is necessary to create figure first. When the image will be ready, it is possible to save it to file, or display it with function show()
Drawing spheres with MayaviAt the first step I will show the position of charges and will colour them according to their charges with function points3d().
For this function it is necessary to supply array of X coordinates of all spheres for displaying, then Y and Z coordinates, then optionally sizes of spheres and then optionally some parameters. Colours and sizes will be related. If you want to make size and colour independently, you should call points3d() for every charge. Field linesTo plot streamlines for Electric Field it is necessary to calculate them with function flow() for every charge. This function need to know the coordinate grid and electric field at these coordinates. Also it is necessary to give all parameters about calculating of these streamlines, like position of starting points, direction of integration, amount of starting points, displaying or nor start points, the configuration of starting point around every charge (sphere in this case) and the width of displayed lines All these calculated lines will be stored in one list.
Also, it will be very useful to specify the position of starting point spheres – all starting points will be located on spheres around charges. Unfortunately, the Mayavi documentation is not good enough to find out how to do it and it is necessary to check source codes to understand these details. So, for seed spheres starting points can be given via seed.widget
It is also important to specify initial step for integration and the maximal length of each streamline
Mayavi errors.Also it is important to mention, that Mayavi do not show every field line according to initial settings, and sometimes it is necessary to move a bit sphere with initial points to force it to show all lines. Full code of Field liens with Mayavi

Last 10 artitles
9 popular artitles


© 2020 MyCoding.uk My blog about coding and further learning. This blog was writen with pure Perl and frontend output was performed with TemplateToolkit. 