My Coding >
Numerical simulations >
Integrating 2D electric field lines with SciPy
Integrating 2D electric field lines with SciPy
In previous chapters I’ve describe how to plot continuous electric field lines with with Matplotlib and with Mayavi. The main disadvantage of these methods, it that you almost have no control over these lines, receiving some kind of final picture. And matplotlib do not give continuous lines in 3D space. In this tutorial we will use SciPy module to calculate these lines by data given only on the meshgrid.
NOTE! It is important to understand, that 2D case is not natural case and for electric field lines it is important calculate 3D space, but this tasks is good for understanding how to integrate this kind of vector fields.
Preparation for E-field line calculation
For these calculations we need to load NumPy and Matplotlib as a standard. And also we need to load RegularGridInterpolator from SciPy for interpolation of our grid data to continuous domain, and finally ode from SciPy for integration of our vector data.
Defining domain parameters.
At this step we will define all necessary domain parameters and will also calculate Electric Field on the given mesh-grid. This simple task was described in this tutorial.
So briefly, we need to have domain parameters. We will use domain from -3 to 3 in X and Y dimensions with the step of 0.01. The coordinates grid I will do with mgrid function.
NQ=3 charges we will randomly place in the area from -1 to 1. This difference is important to have trace of the electric field lines beyond the charge box.
Then we initialize empty arrays for electric field, by multiplying our coordinate grid by 0 and then calculate electric field in every point.
At the last step I will make linear approximation of out electric field within the domain, as it is described here.
Electric field line integration parameters.
For integration of our electric field lines, we need to specify integration model. In ode from SciPy you can use the following models:
Then it is necessary to limit maximal number of integration steps – to avoid cyclical and endless solutions (2000), give minimal integration step (0.01).
Next, it is necessary to give start points for electric line calculations and endpoints. Around every charge we will make a circle of radius 0.15 and uniformly distribute 10 start points on it. The end of the line will be at the border of the domain, or when it will approach to other charge to a close distance (less than radius).
Also here I will create function Intergrate_Line with take integration time (default but not used parameter here) and coordinates and return the Field value for the given point. This function will be used by ode integrator. If the function will fails to find the electric field values (out of the domain, or near the border), then it will reset abort switch.
When everything is ready – initialize space for plotting image
Now I will calculate electric lines itself. I will do cycle for every charge. In every cycle I will set up initial step from the default value and then if the charge is negative – will use it with opposite sign, to calculate this line in different direction.
Then for charge I will create uniformly distributed start dots around every charge. It is easy to do in 2D space with linspace function. It is also possible to place these dots randomly – for some tasks it give better line distribution.
Then for every angle I will calculate it relative electric field line with the following steps: set up function for integrator, then specify integration model used for calculation.
After I will set up initial spot lx and ly around the sphere in according with the given angle and give these values ti the integrator set_initial_value with 0 time (it is dummy parameter not used here)
Then I do reset my step counted and reset the abort status.
After this, I will run integration until it returns successful status with cycle while. On every step I do update my step counter, integrate coordinates for the given step, and check the coordinates. If the coordinates are not False, then I do add them to my list with this line coordinates and check, that they are not within the allowed radius from any charger. If yes, then I do abort calculations.
If my last point are out of the domain area, then I do abort this line calculation. If I do exceed maximal number of steps I do about calculation as well.
And now I do plot this line. If it is aborted due to number of integration steps, then I do plot it with the red colour, otherwise it will be black.
At the next step I do draw big circles for charges. Blue for negative charge and red for positive (in video I do mix them)
Then I do set the lomits for the plotting domain and that it. Job done.
If you have some question, please watch our video:
Share it, subscrive to the channel and comment there
Finally you can find the full working code for integrating electric field lines on the domain from few point charges here:
Last 10 artitles
9 popular artitles
|© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.