Wave Simulation with Python
Asked Answered
K

4

10

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.

Kiersten answered 10/2, 2011 at 11:11 Comment(1)
Pick an algorithm first, then ask about implementation in Python.Schlenger
I
14

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.

Irby answered 10/2, 2011 at 11:39 Comment(3)
Hi ptomato, difficult stuff but I think I will read about FDTD at first. Thank you!Kiersten
It seems to difficult to run Meep on Windows. :(Kiersten
@Kiersten - I know. That's very unfortunate. However, it's the only open source solution I'm aware of - otherwise you can either write your own (see edit to the answer) or use an expensive commercial program (RSoft, Lumerical, etc.)Irby
I
3

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.

Ipswich answered 10/2, 2011 at 11:30 Comment(0)
C
0

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.

Coelenterate answered 10/2, 2011 at 15:31 Comment(0)
T
-5

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 :)

Taunyataupe answered 10/2, 2011 at 11:28 Comment(5)
NumPy is perfectly suitable for computationally expensive things.Irby
Okay, I think I don't have to do it necessarily in python :/Kiersten
-1 because Python has widely used scientific libraries like numpy/scipy. Have also a look at other ways of optimizing python code like cython, psyco. Finally, use efficient algorithms ;)Kidskin
@Andrea Spadaccini and do not forget Weave! :-D (it is inside scipy though, but worth to mention explicitly)Contain
Well, for PDE's there will simply remain an order of magnitude difference between NumPy and C/Fortran and alike. No-one showed an counterexample yet, but if you have one, feel free to show it to me.Narceine

© 2022 - 2024 — McMap. All rights reserved.