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.

fortran
  • 67,715
  • 23
  • 125
  • 170
kame
  • 16,824
  • 28
  • 95
  • 142

4 Answers4

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.

ptomato
  • 51,511
  • 13
  • 105
  • 149
  • Hi ptomato, difficult stuff but I think I will read about FDTD at first. Thank you! – kame Feb 10 '11 at 21:58
  • 1
    It seems to difficult to run Meep on Windows. :( – kame Feb 10 '11 at 22:26
  • @kame - 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.) – ptomato Feb 11 '11 at 08:56
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.

Paul
  • 37,258
  • 13
  • 99
  • 114
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.

Ram Rachum
  • 71,442
  • 73
  • 210
  • 338
-4

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

x10
  • 3,522
  • 1
  • 21
  • 32
  • 9
    NumPy is perfectly suitable for computationally expensive things. – ptomato Feb 10 '11 at 11:32
  • Okay, I think I don't have to do it necessarily in python :/ – kame Feb 10 '11 at 11:34
  • 8
    -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 ;) – Andrea Spadaccini Feb 10 '11 at 11:35
  • 2
    @Andrea Spadaccini and do not forget Weave! :-D (it is inside scipy though, but worth to mention explicitly) – fortran Feb 10 '11 at 12:01
  • 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. – Vladimir F Feb 06 '12 at 14:04