使用 Python 进行波浪模拟
我想模拟三维空间中某些物体上的吸收和反射的传播波。我想用 python 来做。我应该使用numpy吗?我应该使用一些特殊的库吗?
如何模拟波浪?可以用波动方程吗?但如果我有反思怎么办? 有更好的方法吗?我应该用向量来做吗?但当光线发散时,强度会降低。难的。
提前致谢。
I want to simulate a propagating wave with absorption and reflection on some bodies in three dimensional space. I want to do it with python. Should I use numpy? Are there some special libraries I should use?
How can I simulate the wave? Can I use the wave equation? But what if I have a reflection?
Is there a better method? Should I do it with vectors? But when the ray diverge the intensity gets lower. Difficult.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在 Python 中进行任何计算密集型数值模拟,您绝对应该使用 NumPy。
模拟任意形状材料中的电磁波的最通用算法是时域有限差分法(FDTD)。它在 3-D 晶格上一次求解一个时间步长的波动方程。不过,自己编程相当复杂,您最好使用专用包,例如 Meep。
有一些关于如何编写自己的 FDTD 模拟的书籍:这里是一个,这里文档,其中包含一维 FDTD 的一些代码以及对多于 1 维的解释,以及 Google 搜索“writing FDTD”会发现更多相同的情况。
您还可以通过假设所有波都是平面波来解决该问题,然后您可以使用矢量和菲涅尔方程< /a>.或者,如果您想对从平面或曲面传输和反射的高斯光束进行建模,您可以使用 ABCD 矩阵形式(也称为 光线传输矩阵)。这考虑了光束的发散。
If you do any computationally intensive numerical simulation in Python, you should definitely use NumPy.
The most general algorithm to simulate an electromagnetic wave in arbitrarily-shaped materials is the finite-difference time domain method (FDTD). It solves the wave equation, one time-step at a time, on a 3-D lattice. It is quite complicated to program yourself, though, and you are probably better off using a dedicated package such as Meep.
There are books on how to write your own FDTD simulations: here's one, here's a document with some code for 1-D FDTD and explanations on more than 1 dimension, and Googling "writing FDTD" will find you more of the same.
You could also approach the problem by assuming all your waves are plane waves, then you could use vectors and the Fresnel equations. Or if you want to model Gaussian beams being transmitted and reflected from flat or curved surfaces, you could use the ABCD matrix formalism (also known as ray transfer matrices). This takes into account the divergence of beams.
如果您正在求解 3D 自定义偏微分方程,我建议至少查看一下 FiPy。它将为您省去从头开始构建大量矩阵调节器和求解器的麻烦。它使用 numpy 和/或 trilinos。以下是一些示例。
If you are solving 3D custom PDEs, I would recommend at least a look at FiPy. It'll save you the trouble of building a lot of your matrix conditioners and solvers from scratch. It uses numpy and/or trilinos. Here are some examples.
我建议您使用我的项目 GarlicSim 作为构建模拟的框架。您仍然需要自己编写算法(可能是在 Numpy 中),但 GarlicSim 可以为您节省大量样板文件,并允许您以灵活的方式探索模拟结果,类似于版本控制系统。
I recommend you use my project GarlicSim as the framework in which you build the simulation. You will still need to write your algorithm yourself, probably in Numpy, but GarlicSim may save you a bunch of boilerplate and allow you to explore your simulation results in a flexible way, similar to version control systems.
不要使用Python。我曾尝试将它用于计算成本较高的事情,但它并不是为此而设计的。
如果您需要在Python程序中模拟波浪,请用C/C++编写必要的代码并将其导出到Python。
以下是 C API 的链接:http://docs.python.org/c-api/
请注意,这不是世界上最简单的 API :)
Don't use Python. I've tried using it for computationally expensive things and it just wasn't made for that.
If you need to simulate a wave in a Python program, write the necessary code in C/C++ and export it to Python.
Here's a link to the C API: http://docs.python.org/c-api/
Be warned, it isn't the easiest API in the world :)