Python Simulation

This area is for ADVANCED theory and discussions only. If you just joined fusor.net, chances are this is NOT the area for you to be posting.
User avatar
Nicolas Krause
Posts: 81
Joined: Fri Sep 30, 2016 11:36 pm
Real name: Nicolas Krause
Location: Canada
Contact:

Python Simulation

Post by Nicolas Krause » Thu Mar 14, 2019 7:30 pm

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.
2dVoltagepotential.png

Dan Knapp
Posts: 342
Joined: Wed Aug 06, 2008 12:34 pm
Real name: Dan Knapp

Re: Python Simulation

Post by Dan Knapp » Thu Mar 14, 2019 10:11 pm

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.

User avatar
Nicolas Krause
Posts: 81
Joined: Fri Sep 30, 2016 11:36 pm
Real name: Nicolas Krause
Location: Canada
Contact:

Re: Python Simulation

Post by Nicolas Krause » Fri Mar 15, 2019 1:48 am

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.

Dan Knapp
Posts: 342
Joined: Wed Aug 06, 2008 12:34 pm
Real name: Dan Knapp

Re: Python Simulation

Post by Dan Knapp » Fri Mar 15, 2019 12:13 pm

Sounds like a good plan. I just wanted to be sure you were aware of the limitations of 2D. Good luck in your endeavors!

Harald_Consul
Posts: 94
Joined: Sat Oct 20, 2018 10:01 am
Real name: Harald Consul

Re: Python Simulation

Post by Harald_Consul » Fri Mar 15, 2019 7:51 pm

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.

User avatar
Nicolas Krause
Posts: 81
Joined: Fri Sep 30, 2016 11:36 pm
Real name: Nicolas Krause
Location: Canada
Contact:

Re: Python Simulation

Post by Nicolas Krause » Sat Mar 16, 2019 3:59 am

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?

Harald_Consul
Posts: 94
Joined: Sat Oct 20, 2018 10:01 am
Real name: Harald Consul

Re: Python Simulation

Post by Harald_Consul » Sat Mar 16, 2019 5:08 pm

Just plug it into the thread using the "</>" Button or the BB-code tags

Code: Select all

[code]
PRG listing
[/code]
, so that anyone interested can view it.

User avatar
Nicolas Krause
Posts: 81
Joined: Fri Sep 30, 2016 11:36 pm
Real name: Nicolas Krause
Location: Canada
Contact:

Re: Python Simulation

Post by Nicolas Krause » Sat Mar 16, 2019 6:50 pm

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

ian_krase
Posts: 628
Joined: Mon Nov 28, 2016 7:48 am
Real name: Ian Krase

Re: Python Simulation

Post by ian_krase » Tue Mar 19, 2019 8:00 am

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:

Code: Select all

conda install ipython numpy scipy matplotlib 
and then follow the prompts.

This works much more cleanly than most other methods to install the very complicated Numpy backend.

Harald_Consul
Posts: 94
Joined: Sat Oct 20, 2018 10:01 am
Real name: Harald Consul

Re: Python Simulation

Post by Harald_Consul » Wed Mar 20, 2019 6:00 pm

Another beautiful possibility for numerical programming is R, which can be downloaded at https://cran.r-project.org/ .

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

Post Reply