My Coding >
Numerical simulations >
EMHD >
Electric field potential and intensity due to point charges
Electric field potential and intensity due to point chargesIn this article I will show, how to calculate and visualise with Python Electric field potential and intensity due to point charges. For these calculations I will use Numpy and Matplotlib. Initializing domain for calculationsFor displaying electric field intensity and potential in some area (we call it domain) it is necessary to calculate it in every point of this domain. Usually, domain is slitted into a mesh of cells and this value calculated for every cell.
I will use squared 2dimensional domain, therefore range in x and y axes will be the same. I ill use range from 1 to 1 with 100 steps, and I will generate randomly 5 (NQ) charges with charge from 10 to 10. This is all x values xlist = np.linspace(xrange[0], xrange[1], step) and the same for ylist. At the next step I will create domain for X and Y with function np.meshgrid. This will create domains X and Y and fill every cell of this domain with values of x and y in these cells respectively. From on hand it can looks a bit excessive and overloading of repetitive information, but it will make all numpy matrix calculations much easier. Then we need to create random list of charges as a numpy array for example, and we can do it with np.random.uniform with providing list of minimal and maximal values and in size specify the size of this array. At the last step – create numpy arrays for E_{x}, E_{y} and V (X and Y component of Electric intensity and Electric potential), filled with 0. Because these array will represent our domain, we can just make a similar arrays like out X or Y arrays with function np.zeros_like Calculation of Electric field potential and intensityElectric field potential and Electric field intensity in stationary case are linear and it is possible to calculate it for every charge and then add them together. This will make all calculations much easier. Also electric field intensity is a vector value and we van calculate X and Y component according to the basis of our domain.
At the beginning, we calculating the distance matrix in every point to the charge given, but we are calculating distance squared. It doesn’t matter, because later we will take square rood of it. For performance optimisation, it is important to do not make the same operation few times, and we can optimize it by calculating distance cubed first and then take cubic root for electric potential calculations. We will reduce one power operation but the code will be a bit more complicated. And finally, we do not need to return calculated values, because we working with references and the original data will be updated anyway, but I will return them just in case. At the last step It is necessary to calculate absolute value of electric field intensity and to make it more easy to visualize, take a log of it. Visualisation
Electric field linesLast step is visualisation. Because we will join few graphs on the same plot, we need to use axes. First of all we can plot electric field intensity as electric field lines with streamplot function.
So, we’ve create figure to make two plots with mentioning size in inches. Then we’ve create axes for first subplot at the position 1 (121) in the field with height=1 and width=2. Then we make a title for this plot and finally draw lines with streamplot, using X and Y coordinates, with E_{x}, E_{y} values and define some parameters of these lines. Contour line mapFor plotting contour map we need to define levels for plotting. Use np.linespace to define all levels. Despite electric field intensity is always positive, the way of plotting these contour map can approximate it to negative values, so it is better to start make levels from negative value. Secondly, because the step of domain can leat do different accuracy of calculations, we need to limit top value to a pretty reasonable value. Very high accuracy can give unlimitedly high value near the point charge and in our linear layer distribution, all layers will be concentrated around charge, So it is better to restrict it.
Also it is necessary to chose nice colourmap. For values from 0 to high values, I do advise to use cmap='jet'. For values from negative to positive values, I do advise to use cmap='seismic' colour scheme. The same we will do for the electric potential energy map. Final code

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. 