Python Simulation
- Nicolas Krause
- Posts: 146
- Joined: Fri Sep 30, 2016 11:36 pm
- Real name: Nicolas Krause
- Location: Canada
- Contact:
Python Simulation
I've decided to break out the work I'm doing into its own thread. As of right now I've completed porting my initial code over to python, which means that I have a 2d representation of the electric potential in a fusor. My next step is to get a simple particle in cell simulation going with a small number of particles.
Re: Python Simulation
You should be aware that a 2D representation of spherical geometry is OK for plotting potential field lines, but 2D will not give you a valid PIC simulation of a spherical system. You have to use full 3D to get valid PIC simulation of spherical geometry. A 2D simulation is only valid when any slice through the system gives the same 2D geometry, e.g. an infinitely long cylindrical geometry.
- Nicolas Krause
- Posts: 146
- Joined: Fri Sep 30, 2016 11:36 pm
- Real name: Nicolas Krause
- Location: Canada
- Contact:
Re: Python Simulation
I'm aware it won't be an accurate representation of a spherical system, but it seemed a good way to get my feet wet seeing as this is the first time I've written a numerical simulation. In particular I was inspired by the following work done by a physicist by the name of Matt Lilley, who used a 2d simulation and some nonlinear dynamics to investigate how star mode might form. I'm hoping to first replicate his results, and then see if I can expand upon them. Either by extending the simulation to 3 dimensions or by pursuing some other avenue that appears along the way.
Re: Python Simulation
Sounds like a good plan. I just wanted to be sure you were aware of the limitations of 2D. Good luck in your endeavors!
-
- Posts: 94
- Joined: Sat Oct 20, 2018 10:01 am
- Real name: Harald Consul
Re: Python Simulation
If you are not planning a scientific publication of your "electric potential in a Farnsworth fusor simulation", it would be interesting to see the code.
I am not too experienced in Python, but I do a lot of R programming (including simulation tasks, also). Usually a numerical code is easy to understand.
I am not too experienced in Python, but I do a lot of R programming (including simulation tasks, also). Usually a numerical code is easy to understand.
- Nicolas Krause
- Posts: 146
- Joined: Fri Sep 30, 2016 11:36 pm
- Real name: Nicolas Krause
- Location: Canada
- Contact:
Re: Python Simulation
Hi Harald,
Definitely not planning to publish anything, but happy to share the code I have so far, it's not very complicated at all. How would you like me to send the file?
Definitely not planning to publish anything, but happy to share the code I have so far, it's not very complicated at all. How would you like me to send the file?
-
- Posts: 94
- Joined: Sat Oct 20, 2018 10:01 am
- Real name: Harald Consul
Re: Python Simulation
Just plug it into the thread using the "</>" Button or the BB-code tags
[/code]
, so that anyone interested can view it.
Code: Select all
[code]
PRG listing
, so that anyone interested can view it.
- Nicolas Krause
- Posts: 146
- Joined: Fri Sep 30, 2016 11:36 pm
- Real name: Nicolas Krause
- Location: Canada
- Contact:
Re: Python Simulation
Here you go! The code uses numpy and matplotlib, two freely available python packages. A matrix called fusor is defined and a finite difference approximation is performed until the matrix converges on a solution. The program is based on some matlab code I found on a professor's website, he has written a nice summary of the general method in the following document.
Code: Select all
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#fusor is a 2d grid of ~10000 points
Nx=101
Ny=101
fusor = np.zeros((Ny,Nx))
#setup fusor initial voltage
Vsource= -45000
#set the resolution in x and y coordinates
res = 0.0015
hx= res
hy= res
#precalculate kx and ky
kx= (hx**2)/(2*(hx**2+hy**2))
ky= (hy**2)/(2*(hx**2+hy**2))
#initial conditions for our while loop
tol = 0.001
dsum=1
iter=0
while dsum > tol:
#sum our squared fusor grid
square1=np.square(fusor)
sum1=square1.sum()
print(sum1)
#iterate through rows
for ny in range(2,100):
#iterate through columns
for nx in range(2,100):
#source voltages are replaced every loop
fusor[50,40]=Vsource
fusor[50,60]=Vsource
fusor[40,47]=Vsource
fusor[60,47]=Vsource
fusor[40,55]=Vsource
fusor[60,55]=Vsource
#perform a finite difference approximation
fusor[ny,nx]=ky*(fusor[ny,nx+1]+fusor[ny,nx-1])+kx*(fusor[ny+1,nx]+fusor[ny-1,nx])
#sum our fusor grid again
square2=np.square(fusor)
sum2=square2.sum()
print(sum2)
#check the absolute value difference between our before and after grid
dsum = sum2-sum1
dsum = abs(dsum)
print(dsum)
#count iterations
iter = iter+1
#output fusor grid using a plot
print(iter)
plt.imshow(fusor)
plt.colorbar()
plt.show()
#electric potential has been calclated, so now we need the E-Field, so we can find the force on our charged particles
#have to integrate that somehow, how we gonna do that in python?
#place particles
#move particles
Re: Python Simulation
For those of you who are new to Python:
If you want to use Python on Windows, by far the best way is to install the newest version of Miniconda and then open an Anaconda Command Prompt and type the following:
and then follow the prompts.
This works much more cleanly than most other methods to install the very complicated Numpy backend.
If you want to use Python on Windows, by far the best way is to install the newest version of Miniconda and then open an Anaconda Command Prompt and type the following:
Code: Select all
conda install ipython numpy scipy matplotlib
This works much more cleanly than most other methods to install the very complicated Numpy backend.
-
- Posts: 94
- Joined: Sat Oct 20, 2018 10:01 am
- Real name: Harald Consul
Re: Python Simulation
Another beautiful possibility for numerical programming is R, which can be downloaded at https://cran.r-project.org/ .
The advantages of R are
The advantages of R are
- Data formats are all the same through out all R
- Missings are handled all the same through out all R
- R installes out of the box an all operating systems
- R code runs platform independent on all OS