python模拟上传图片

For today’s recreational coding exercise, we will investigate plasma physics with particle-in-cell (PIC) simulations. We will create a simulation of two electron beams passing through each other in opposite directions. An interesting phenomenon occurs, called the Two-Stream Instability.

对于当今的娱乐性编码练习,我们将使用单元中粒子(PIC)模拟研究等离子体物理。 我们将模拟两个彼此相反的方向穿过的电子束。 发生一个有趣的现象,称为两流不稳定性。

You may find the accompanying Python code on github.

您可以在github上找到随附的Python代码。

Before we begin, below is a gif of what running our simulation looks like:

在开始之前,下面是模拟运行情况的图像:

单元中粒子(PIC)方法 (Particle-In-Cell (PIC) Method)

We will consider a one-dimensional system of electrons in an unmagnetized uniform medium of ions. The electrons will be described by N particles, indexed by i, each having a

我们将考虑在未磁化的离子均匀介质中的一维电子系统。 电子将由N个粒子(由i索引)描述,每个粒子都有一个

  • position rᵢ

    位置rᵢ

  • velocity vᵢ

    速度vᵢ

The electrons feel an acceleration aᵢ which is due to the electric field E at the location of the particle. The equations of motion for the electrons are given by:

电子感到加速度aᵢ ,这是由于粒子位置处的电场E引起的 电子的运动方程式为:

What remains to be done is to calculate the electric field E, which depends on the number density n of the electrons. The electric field E is defined as the negative gradient of the electric potential ϕ, which is given by Poisson’s equation sourced by the density:

剩下要做的是计算电场E ,它取决于电子的数密度n 。 的电场E被定义为电势φ,它是由泊松方程由密度给定源的负梯度:

where n₀ is the average electron density (a constant).

式中n₀是平均电子密度(一个常数)。

The Particle-In-Cell (PIC) Method computes the density and subsequently the potential and electric field on a mesh. Hence it is called a particle-mesh method. The electric field is then extrapolated onto the location of the particles in order to advance them. That’s a long sequence of steps. Let’s dig in!

单元中粒子(PIC)方法可计算密度,然后计算网格上的电势和电场。 因此,它被称为粒子网格方法。 然后将电场外推到粒子的位置,以使粒子前进。 这是一个很长的步骤。 让我们开始吧!

密度计算 (Density Calculation)

The density is obtained by binning the particles onto the mesh gridpoints. Call the separation distance between mesh points Δx. A particle with position rᵢ will lie between a pair of coordinates xⱼ and x_{j+1} on the mesh. Then, the densities nⱼ and n_{j+1} at these two points gain a contribution:

通过将粒子合并到网格网格点上来获得密度。 称网格点之间的间隔距离Δx。 位置为rᵢ的粒子将位于网格上的一对坐标xⱼx_ {j + 1}之间。 然后,在这两个点上的密度nⱼn_ {j + 1}获得贡献:

After collecting contribution from all particles, the density has to be properly normalized so that the average density is the specified n₀.

在收集所有粒子的贡献后,必须适当地对密度进行归一化,以使平均密度为指定的n₀。

潜力计算 (Potential Calculation)

Knowing the density nⱼ on the mesh grid points, we now seek the potential ϕⱼ at these points. The second-derivative operator (called the ‘Laplacian’) in Poisson’s equation can be approximated on the mesh with a finite-different formula:

知道在网格点的密度nⱼ,我们现在寻求在这些点的潜力φⱼ。 泊松方程中的二阶导数算子(称为“拉普拉斯算子”)可以使用有限差分公式在网格上近似:

If all the values for nⱼ are arranged into a column vector, and similarly for ϕⱼ, then the equation can be written as a system of linear equations in matrix form:

如果所有用于nⱼ的值被设置成一个列向量,并且类似地对于φⱼ,则方程可以写为矩阵形式的线性方程系统:

The matrix represents the Laplacian operator and is sparse and tridiagonal. We have assumed periodic boundary conditions, i.e., the endpoints of the mesh wrap around to each other, which add the ‘1’s in the top-right and bottom-left corners of the matrix. This matrix equation can be efficiently numerically inverted and solved by using pre-built linear algebra packages such as the Python Scipy package’s “spsolve” function.

矩阵表示拉普拉斯算子,并且是稀疏的和三对角的。 我们假定了周期性边界条件,即网格的端点彼此环绕,这在矩阵的右上角和左下角加了“ 1”。 通过使用预构建的线性代数包(例如Python Scipy包的“ spsolve”函数),可以有效地对这个矩阵方程进行数值反转和求解。

To start, we will construct the matrix for now with some code:

首先,我们将使用一些代码来构建矩阵:

# Construct matrix Lmtx to computer Laplacian (2nd derivative)
# on mesh size Nx, separation dx
e = np.ones(Nx)
diags = np.array([-1,0,1])
vals  = np.vstack((e,-2*e,e))
Lmtx = sp.spdiags(vals, diags, Nx, Nx);
Lmtx = sp.lil_matrix(Lmtx) # tranform mtx type to modify entries
Lmtx[0,Nx-1] = 1
Lmtx[Nx-1,0] = 1
Lmtx /= dx**2
Lmtx = sp.csr_matrix(Lmtx) # transform mtx type

电场计算(Electric Field Calculation)

Given that we have solved for ϕⱼ on the mesh, we can next solve for the electric field Eⱼ by discretizing the first-derivative operator (called the ‘gradient’):

鉴于我们解决了为φⱼ网格上,我们可以下一个求解由离散一阶导数算子(称为“梯度”)的电场Eⱼ:

Again we can write out the system of equations in matrix form:

同样,我们可以用矩阵形式写出方程组:

The system is simpler this time. There is no need to invert a matrix. The electric field is directly obtained by applying the matrix multiplication.

这次系统更简单。 无需反转矩阵。 通过应用矩阵乘法直接获得电场。

The code to construct the matrix for the gradient operator is shown below:

下面显示了构造用于梯度算子的矩阵的代码:

# Construct matrix G to computer Gradient  (1st derivative)
# on mesh size Nx, separation dx
e = np.ones(Nx)
diags = np.array([-1,1])
vals  = np.vstack((-e,e))
Gmtx = sp.spdiags(vals, diags, Nx, Nx);
Gmtx = sp.lil_matrix(Gmtx) # tranform mtx type to modify entries
Gmtx[0,Nx-1] = -1
Gmtx[Nx-1,0] = 1
Gmtx /= (2*dx)
Gmtx = sp.csr_matrix(Gmtx) # transform mtx type

加速度计算(Acceleration Calculation)

So far we have gone from particles to mesh values of density to potential to electric field. To complete the loop, we need the electric field at the location of the particles. Fortunately, this can now be done with interpolation from the 2 nearest gridpoints xⱼ and x_{j+1}. For particle i, the electric field Eᵢ is:

到目前为止,我们已经从粒子到密度的网格值,再到电场的电势。 为了完成循环,我们需要在粒子位置处施加电场。 幸运的是,现在可以通过从2个最近的网格点xⱼx_ {j + 1}进行插值来完成此操作。 对于粒子i,电场Eᵢ为:

using similar weights as when be binned to get density.

使用与合并时类似的权重以获取密度。

Putting all the steps above together, we can write a function to calculate the acceleration on each particle, given the particle distribution:

将以上所有步骤放在一起,我们可以编写一个函数来计算给定粒子分布的每个粒子的加速度:

def getAcc( pos, Nx, boxsize, n0, Gmtx, Lmtx ):"""Calculate the acceleration on each particle due to electric fieldpos      is an Nx1 matrix of particle positionsNx       is the number of mesh cellsboxsize  is the domain [0,boxsize]n0       is the electron number densityGmtx     is an Nx x Nx matrix for calculating the gradient on the gridLmtx     is an Nx x Nx matrix for calculating the laplacian on the grida        is an Nx1 matrix of accelerations"""# Calculate Electron Number Density on the Mesh by # placing particles into the 2 nearest bins (j & j+1, with proper weights)# and normalizingN          = pos.shape[0]dx         = boxsize / Nxj          = np.floor(pos/dx).astype(int)jp1        = j+1weight_j   = ( jp1*dx - pos  )/dxweight_jp1 = ( pos    - j*dx )/dxjp1        = np.mod(jp1, Nx)   # periodic BCn  = np.bincount(j[:,0],   weights=weight_j[:,0],   minlength=Nx);n += np.bincount(jp1[:,0], weights=weight_jp1[:,0], minlength=Nx);n *= n0 * boxsize / N / dx # Solve Poisson's Equation: laplacian(phi) = n-n0phi_grid = spsolve(Lmtx, n-n0)# Apply Derivative to get the Electric fieldE_grid = - Gmtx @ phi_grid# Interpolate grid value onto particle locationsE = weight_j * E_grid[j] + weight_jp1 * E_grid[jp1]a = -Ereturn a

时间整合(Time Integration)

We are in the home stretch now! We will loop over timesteps and evolve the electrons using a leap-frog scheme (‘kick-drift-kick’). For each timestep Δt, each particle receives a half-step ‘kick’:

我们现在在家中! 我们将在时间步长上循环,并使用跳越方案('kick-drift-kick')释放电子。 对于每个时间步长Δt ,每个粒子都接收一个半步“踢”:

followed by a full-step ‘drift’:

然后是整步“漂移”:

followed by another half-step ‘kick’.

接下来是半步“踢”。

The evolution is performed in the code using a For-loop and our function for the acceleration from earlier:

使用For循环在代码中执行演化,而我们的函数则用于从早期开始进行加速:

# Simulation Main Loop
for i in range(Nt):# (1/2) kickvel += acc * dt/2.0# drift (and apply periodic boundary conditions)pos += vel * dtpos = np.mod(pos, boxsize)# update accelerationsacc = getAcc( pos, Nx, boxsize, n0, Gmtx, Lmtx )# (1/2) kickvel += acc * dt/2.0# update timet += dt

In case this looks familiar, it might be because this time-stepping algorithm is a common one for particle-based simulations, and we have also used it in our tutorial on gravitational N-body simulations.

如果看起来很熟悉,可能是因为这种时步算法是基于粒子的模拟中的常用算法,并且我们还在引力N体模拟的教程中也使用了它。

初始条件 (Initial Conditions)

The only thing left to do is specify the initial conditions (positions and velocities at time t=0) of the electrons in the simulation. In the code, we implement the example that we have a uniform beam traveling to the right, and another one superimposed, traveling left. Particle positions are drawn from a random uniform distribution. Velocities are drawn from a Gaussian centered about the beam velocity (one positive, one negative). Small initial perturbations in the form of a cosine function are added to the velocity distribution. Feel free to check out the code and modify or the initial conditions or explore your own.

剩下要做的就是指定模拟中电子的初始条件(在时间t = 0时的位置和速度)。 在代码中,我们实现了一个示例,我们有一个均匀的光束向右传播,另一个光束向左传播。 粒子位置是从随机均匀分布中得出的。 速度是从以光束速度为中心的高斯中得出的(一个正,一个负)。 余弦函数形式的小的初始扰动被添加到速度分布中。 随时检查代码并进行修改或初始条件,或者自行探索。

Running the code allows you to visualize the simulation in real time and will yield the figure:

运行代码可以使您实时可视化仿真,并产生图:

We plot the configuration of electrons in what’s called phase-space, where the x-axis is the particle’s position and the y-axis is the velocity. This allows us to visualize all the variables in the system directly. Particles initially belonging to the right-moving beam have a blue color, while particles in the left-moving beam are red. The two beams interact with each other and go unstable in mesmerizing fashion.

我们在所谓的相空间中绘制电子的构图,其中x轴是粒子的位置,y轴是速度。 这使我们可以直接可视化系统中的所有变量。 最初属于向右移动光束的粒子为蓝色,而向左移动的光束中的粒子为红色。 两束光相互影响,并以令人着迷的方式变得不稳定。

So there you have it. We’ve created a plasma PIC simulation from scratch. I have here assumed the simplest scenario where electrons are non-relativistic and ions form a static background, and we’ve already learned an interesting instability can occur from counter-streaming plasma beams. Astro- and plasma physicists use these types of simulations to study all sorts of physical systems, from magnetic reconnection in the Sun to the design of tokamaks. In tokamak design, for example, the goal is to figure out a way to confine plasma to produce energy, without triggering disruptive plasma instabilities. It’s a very hard problem, but some day the technology may be an efficient way to produce clean energy.

所以你有它。 我们从头开始创建了等离子PIC仿真。 我在这里假设了最简单的情况,其中电子是非相对论的,并且离子形成了静态背景,而且我们已经了解到,逆流等离子体束会产生有趣的不稳定性。 天体物理学家和等离子物理学家使用这些类型的模拟来研究各种物理系统,从太阳的磁重联到托卡马克的设计。 例如,在托卡马克设计中,目标是找到一种方法来限制等离子体产生能量,而不会触发破坏性的等离子体不稳定性。 这是一个非常棘手的问题,但是总有一天该技术可能是生产清洁能源的有效方法。

演示地址

Solar flares involve magnetic reconnection and release of energy
太阳耀斑涉及磁场的重新连接和能量的释放
wikimedia]wikimedia ]

Download the Python code on github for our PIC algorithm to visualize the simulation in real time and play around with the initial conditions. Enjoy!

在github上为我们的PIC算法下载Python代码,以实时可视化仿真并处理初始条件。 请享用!

翻译自: https://medium.com/swlh/create-your-own-plasma-pic-simulation-with-python-39145c66578b

python模拟上传图片


http://www.taodudu.cc/news/show-2731808.html

相关文章:

  • 运动健身耳机什么好?四款运动蓝牙耳机之好评
  • matlab emd功率谱密度,基于EMD方法的地心运动时间序列分析
  • 超分辨率综述
  • 关于X509证书和密钥的概念
  • Taq DNA聚合酶的种类与应用现状
  • 二维Poisson方程五点差分格式及简单求解方法Python实现
  • TREG(Transformed Regression for Accurate Tracking)
  • Day02-线性代数-矩阵(DataWhale)
  • 2020年中级数据库系统工程师考试笔记7—关系数据库1
  • 密码学的安全性浅析-3
  • 密码学的安全性浅析3
  • High Performance Visual Tracking with Siamese Region Proposal Network全文翻译
  • Chapter5.1:频率响应法
  • 【数据结构与算法】之深入解析RSA加密算法的实现原理
  • 密码学基础知识总结
  • 密码学之RSA加密原理解析
  • 苹果手机内屏幕出现彩色条纹怎么办
  • 苹果手机屏幕上的圆点如何设置呢?
  • 苹果手机屏幕上有白点怎么办
  • 苹果主屏幕按钮怎么设置_苹果手机屏幕横屏怎么调
  • iphone屏幕上的圆圈怎么设置_苹果手机上的小圆圈在哪设置【方法介绍】
  • 苹果手机10秒解除锁屏_苹果密码忘了不想刷机怎么办_苹果手机10秒解除锁屏
  • 苹果手机屏幕如何投射到win10?
  • 使用苹果手机/PAD做树莓派的外接屏幕
  • 苹果手机计算机无法横屏,苹果手机怎么设置屏幕旋转失灵了怎么办
  • 白苹果修复_苹果手机突然屏幕变白怎么办
  • vue中播放flv流视频
  • 如何将FLV格式视频转换成高清MP4格式方法
  • 通过Vue+flvjs在HTML5中播放flv格式视频文件—demo及api
  • 如何快速不借用转换工具将FLV格式视频转换成MP4

python模拟上传图片_用python创建自己的等离子图片模拟相关推荐

  1. python自动上传图片_使用Python实现一个简单的图片上传存储服务

    使用flask实现的一个简单的图片上传服务 设计初衷 对于图片的存储,有很多选择,一般采用云服务如(七牛,又拍等),但是国内的服务像七牛 自定义域名竟然需要域名备案(Excuse me,当初就是因为备 ...

  2. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  3. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  4. python笛卡尔_用Python 3来模拟笛卡尔积

    在数学中,两个集合 和 的笛卡尔积,是所有可能的有序对组成的集合,其中有序对的第一个对象是 的成员,第二个对象是 的成员.在集合论中表示为 ,例子如下: . 例如,集合 , ,那么这两个集合的笛卡尔积 ...

  5. 使用python预测基金_使用python先知3 1创建预测

    使用python预测基金 This tutorial was created to democratize data science for business users (i.e., minimiz ...

  6. python模拟输入回车键_用Python模拟键盘输入

    import win32apiimport win32conwin32api.keybd_event(17,0,0,0) #ctrl键位码是17win32api.keybd_event(86,0,0, ...

  7. python元胞自动机模拟交通_基于立体网格的放射性污染物扩散过程模拟与表达

    作 者 信 息 施加松1,余接情2,常芸芬1,童晓冲3 (1.防化研究院,北京 102205:2.中国矿业大学 环境与测绘学院,江苏 徐州 221116:3.信息工程大学,河南 郑州 450001) ...

  8. python 创意项目_针对python开发人员的10个很棒的python项目创意

    python 创意项目 The joy of coding Python should be in seeing short, concise, readable classes that expre ...

  9. python集群_使用Python集群文档

    python集群 Natural Language Processing has made huge advancements in the last years. Currently, variou ...

最新文章

  1. Spring Cloud - Feign调用问题
  2. RBF(Radial Basis Function Network)+径向基网络
  3. oralce mysql_Oralce和Mysql的3个区别
  4. flutter_web 实战之文章列表与详情
  5. SpringHibernate3
  6. openjpa_以编程方式向OpenJPA注册实体类型
  7. 【Java学习笔记十一】图形用户界面
  8. 创建优秀团队文化的四大要点
  9. 晨哥真有料丨等女神分了我上位!
  10. python函数参数列表_python函数列表
  11. zul页面报org.xml.sax.SAXParseException
  12. ANSYS网格转化为模型、ANSYS网格导入到workbench分析
  13. python爬取大众点评数据_爬虫爬取大众点评评论数
  14. c语言 误差函数erf代码,高斯(余补)误差函数erf和erfc
  15. 戴尔DELLEMC服务器重装CentOS 7系统
  16. DEJA_VU3D - Cesium功能集 之 007-军事标绘系列一:简单箭头
  17. 李雅普诺夫理论基础(1)
  18. three.js 法线贴图
  19. java ee7教程_JavaEE 7.0 Web技术教程 -解道Jdon
  20. PyTorch - GAN与WGAN及其实战

热门文章

  1. android自定义播放器按钮,android – 使用exo播放器添加全屏视频按钮
  2. usg防火墙l2tp ipsec安全策略
  3. 如何正确的向领导汇报工作?
  4. (附代码)数独大作业【读取数独,解数独,生成唯一解数独(随机,特定形状,不同难度生成),玩数独】
  5. VS报错:没有足够的内存继续执行程序
  6. php面试题狼兔,面试题总结 - 疯狂的兔子的个人空间 - OSCHINA - 中文开源技术交流社区...
  7. 美团外卖订单小票打印规范
  8. 编写程序FooBizBaz.java,从1循环到150并且在每行打印一个值,另外在每个3的倍数行上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印输出“baz”。
  9. 使用PyTorch Lightning自动训练你的深度神经网络
  10. C# 操作American_America.US7ASCII编码的Oracle数据库出现乱码的问题。