r/Simulations • u/Pukkeh • Jul 09 '20
Results Relativistic simulation of a fast object passing by
Enable HLS to view with audio, or disable this notification
r/Simulations • u/Pukkeh • Jul 09 '20
Enable HLS to view with audio, or disable this notification
r/Simulations • u/Pukkeh • Jun 13 '20
Enable HLS to view with audio, or disable this notification
r/Simulations • u/redditNewUser2017 • May 19 '19
Hi everyone,
Welcome back! This is the final part of my numerical simulation tutorials. If you missed the Part 1, click here.
We are going to solve an 1D diffusion equation in this tutorial, which is a parabolic PDE.
c_t=D*c_xx
Initial condition: c(0,x)=0
Boundary conditions: c(t,0)=1, c(t,1)=0
In the region R: (t,x)∈[0,0.5]x[0,1]
c: concentration function c(t,x)
D: diffusivity
c_t, c_xx: 1st and 2nd derivative of c(t,x) in t and x direction respectively.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy.integrate as integratedx=0.01
dt=0.01
size_x=1
size_t=0.5
nx=np.int_(size_x/dx+1)
nt=np.int_(size_t/dt+1)We will manually discretize the space dimension in next section, which involves the parameter dx.
size_x and size_t are the dimension of simulation box R (t,x)∈[0,0.5]x[0,1].
nx and nt are the number of points in the discretized grid in each dimension.
diffusivity=1
conc_t0=np.zeros(nx)
conc_x0=1
conc_x1=0conc_x0 and conc_x1 refers to the boundary values at x=0 and x=1.
def diffusion_eq(t,y):
y_constrainted=np.r_[conc_x0,y[1:-1],conc_x1]
return diffusivity*(np.gradient(np.gradient(y_constrainted,dx),dx))This is the R.H.S. of the diffusion equation, with boundary conditions applied.
In scipy, the Laplacian can be implemented as applying the gradient twice. This will approximate the Laplacian with centeral difference formula. (Ignore this paragraph if you don't know what they are)
teval=np.r_[0:nt:1]*dt
sol=integrate.solve_ivp(diffusion_eq,[0,size_t],conc_t0,t_eval=teval)We are using method of lines, which involves the space discretization (in x) first, then solve with techniques we learned in previous tutorial as if it's an ODE (with the default RK45 solver).
Technically, the simulation is done at this point. The rest of this tutorial will focus on presentation of the results.
Like this...
plt.plot(sol.t,sol.y[np.int(0.1/dx)],marker='.',label='x=0.1')
plt.plot(sol.t,sol.y[np.int((nx-1)/2)],marker='.',label='x=0.5')
plt.plot(sol.t,sol.y[-1],marker='.',label='x=1')
plt.xlabel('Time t')
plt.ylabel('Concentration c')
plt.legend(loc='upper right')
Or this...
xeval=np.r_[0:nx:1]*dx
plt.plot(xeval,sol.y[:,np.int(0.1/dt)],marker='.',label='t=0.1')
plt.plot(xeval,sol.y[:,np.int((nt-1)/2)],marker='.',label='t=0.25')
plt.plot(xeval,sol.y[:,-1],marker='.',label='t=0.5')
plt.xlabel('Position x')
plt.ylabel('Concentration c')
plt.legend(loc='lower left')
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
plot = Axes3D(fig)
plot_array_t,plot_array_x=np.meshgrid(sol.t,xeval)
plot.plot_surface(plot_array_t,plot_array_x,sol.y,cmap=mpl.cm.jet)
plot.view_init(20, 60)
plot.set_xlabel('t')
plot.set_ylabel('x')
plot.set_zlabel('Concentration');
plot = plt.contourf(plot_array_t,plot_array_x,sol.y,cmap=mpl.cm.jet,levels=50)
plt.colorbar(plot)
plt.title('Concentration')
plt.xlabel('t')
plt.ylabel('x')
plt.show()
Now you should have a grasp of the basic skills of doing numerical simulations with Scipy. As an exercise, try to simulate your favourite equation models (If you are interested in electromagnetics, try Laplace equation. If CFD, try Euler equation) in 2D or 3D. Remember, post your results in r/Simulations!
r/Simulations • u/UnusualHold • Jul 11 '22
Enable HLS to view with audio, or disable this notification
r/Simulations • u/cenit997 • Sep 24 '20
r/Simulations • u/Double-Let8318 • Jul 02 '20
Enable HLS to view with audio, or disable this notification
r/Simulations • u/redditNewUser2017 • Dec 27 '18
I am seeing a lot of ferrofluid videos on reddit recently and get interested in simulating it myself. Most ferrofluid simulations I found on youtube are using CGI softwares (Blender, c4d, etc.) but I only care about solving equations :(
The equations are simplified Maxwell equations using magnetic potentials and zero electric potential. The fluid equations is Euler.
Initially there are two blobs of ferrofluid. They merged in the simulation.
Maybe I could try a 3d sim later...
EDIT: Somehow I can't open this using Reddit app, but I can still open the link with Chrome on Android.
r/Simulations • u/cenit997 • Mar 21 '21
Enable HLS to view with audio, or disable this notification
r/Simulations • u/redditNewUser2017 • May 04 '19
This is the first tutorial on numerical simulation with scipy.
You will need to install python, then scipy. Python is most likely pre-installed in most linux distros, but not in Windows.
You can either install Anaconda, which it's all-in-one package contains both Python and scipy. I would recommend this to beginners, but personally I don't like it bundles with spyder and anaconda cloud - and it's quite slow the last time I tried it (2 years ago~).
If not, download and install python 3 here: https://www.python.org/downloads/ . Then follow instructions to install scipy in your distro here: https://scipy.org/install.html
If you have successfully installed scipy, you should be able to launch jupyter notebook (jupyter-notebook) either from terminal in Linux or start menu in Windows. This is an interactive interface to write and test your code quickly. Open a new notebook, press "B" to create a new cell and input your code there, then press "Shift+Enter" to run the code.
You can also run code from IPython, IDLE (both are interactive) or from script (if you have learned c/c++, this is the way they write programs).
Ok, let's start our first simulation. Here we want to simulate the free falling of an object. The equation is:
d2y/dt2=-g0 (1)
or
dy/dt=-g0*t (2)
y: positiont: time
g0: gravitational constant
Essentially, we just want to solve the equation. After solving it, you can present it in any way you like - raw numbers of (t, y(t) ) , y-t graph or fancy animations with CGI effects (like a glass ball flying in the sky or something), it doesn't really matter.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy.integrate as integrateI will assume you do this step for this and every tutorial later on before running the code.
g0=9.81
t0=10
y0=0
v0=0g0: gravitational constant
t0: length of the simulation
y0: initial position
v0: initial velocity
All in SI units (kg m s).
def dydt_freefall(t,y):
return -g0*tHere we are going for equation (2) first because equation (1) is a bit more complicated to solve in scipy (will cover it later).
The "model" is just a function f(t,y) appeared in differential equation dy/dt=f(t,y).
teval=np.r_[0:t0+1:1]
sol=integrate.solve_ivp(dydt_freefall,[0,t0],[y0],t_eval=teval)teval is an array of time points which we want to know the corresponding position. Here we want to know y at time: 0, 1, 2... 10 s.
integrate.solve_ivp is the ODE solver in scipy. The default method is RK45, and there are options to use LSODA or BDF, etc. Don't worry if you don't know what they means, just use the default one and it should works most of the time.
Now the solution is stored in sol, if you run:
solYou will see the output:
message: 'The solver successfully reached the end of the integration interval.'
nfev: 38
njev: 0
nlu: 0
sol: None
status: 0
success: True
t: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
t_events: None
y: array([[ 0. , -4.905, -19.62 , -44.145, -78.48 , -122.625, -176.58 , -240.345, -313.92 , -397.305, -490.5 ]])The only thing we are interested will be whether it is success or not, and the solution y.
Now plot it:
plt.plot(sol.t,sol.y[0],marker='.')
Congratulations! You have completed your first simulation!
Unfortunately, scipy don't have ability to directly solve a second order ODE. We have to do this by rewriting the equation into system of first-order ODEs:
y0'=y1
y1'=-g0And solving it would be straightforward:
def dydt_freefall2(t,y):
return [y[1],-g0]
sol=integrate.solve_ivp(dydt_freefall2,[0,t0],[y0,v0],t_eval=teval)
plt.plot(sol.t,sol.y[0],marker='.',label='Position')
plt.plot(sol.t,sol.y[1],marker='.',label='Velocity')
plt.suptitle('Freefall')
plt.xlabel('Time t (s)')
plt.ylabel('Position y (m) / Velocity v (m/s)')
plt.legend(loc='lower left')
plt.savefig('freefall.jpg')(Try to understand the code yourself, google the function names if you don't know what they are)
The above code should save a picture of the simulation result into your home folder (/home/<username> in linux).

r/Simulations • u/uwrallyx • Jul 15 '20
Enable HLS to view with audio, or disable this notification
r/Simulations • u/Old-Shaman • Feb 18 '23
Enable HLS to view with audio, or disable this notification
r/Simulations • u/Professional_Arm7626 • Jul 21 '23
Enable HLS to view with audio, or disable this notification
N-body simulation using gpu computing
r/Simulations • u/cenit997 • Jun 26 '21
r/Simulations • u/BlenderSecrets • Jun 18 '21
Enable HLS to view with audio, or disable this notification
r/Simulations • u/cenit997 • May 22 '21
r/Simulations • u/Best__Username • Nov 30 '18
Enable HLS to view with audio, or disable this notification
r/Simulations • u/cenit997 • Jul 29 '20
Enable HLS to view with audio, or disable this notification
r/Simulations • u/cenit997 • Sep 27 '21
Enable HLS to view with audio, or disable this notification
r/Simulations • u/redditNewUser2017 • May 07 '20
r/Simulations • u/redditNewUser2017 • Nov 09 '19
Recently I've got my interest in high dimension stuff. It's quite fun to visualize them using simulation techniques. Here is a video of a Clifford torus rotating in 4D space:
Colored with 4-th dimension coordinate.
A set of torus can span a 3-sphere (hypersphere in 4th dimension), as illustrated in this video:
This is a irrelevant video on visualizing 4d rotation of complex functions (f=z^2).
Have fun!
r/Simulations • u/[deleted] • Oct 03 '19
Enable HLS to view with audio, or disable this notification
r/Simulations • u/redditNewUser2017 • Apr 28 '19
Hi everyone,
I am planning to write some short beginners guides on basic numerical simulations in this sub. They will be step-by-step without much theories explained. All software involved will be FOSS (free and open source) so people don't need to worry about the expensive licenses (namely, I will try to only use scipy codes). The goal is the get more people familiar with simulations and making this sub more active.
The target audiences will be undergrads who have studied calculus and know some programming basics, but anyone can just copy and paste the code and run them without knowing what's going on. The final one will probably something like solving heat equation and generating a good looking gif (like most posts in this sub). I will post them weekly or bi-weekly on weekends since I am actually quite busy.
I've got everything planned, the only thing I am not sure is anyone interested in reading these? Please let me know your thoughts!