thliebig / openEMS

openEMS is a free and open-source electromagnetic field solver using the EC-FDTD method.
http://openEMS.de
GNU General Public License v3.0
413 stars 146 forks source link

Implement mesh_estimate_cfl_timestep #108

Closed BatchDrake closed 1 year ago

BatchDrake commented 1 year ago

This PR provides mesh_estimate_cfl_timestep, a function that accepts a CSRectGrid mesh and provides a crude estimate of the maximum CFL time step (in seconds), assuming vacuum propagation. Since adding materials with different permittivities / permeabilities will simply slow the propagation of EM waves in the medium, this is actually an underestimation of the true maximum time step.

thliebig commented 1 year ago

Some questions/comments:

I mean this should be plenty fast enough even for large meshes, so the speed improvement should be neglectable ;)

BatchDrake commented 1 year ago

Why did you name the argument "box" instead of e.g. mesh? Could be bit confusing reading the doc?

My bad, somehow I though that that was how you were calling the mesh in other functions, but now that I read them better it is obvious that box is something else. I am changing it.

Why not use lines = mesh.GetLines('x') to get an array and then just np.min(np.diff(lines)) for example?

That was in fact my first approach, but after looking into the implementation, I saw that it called self.thisptr.GetLines and looped through its output to build the line list. Since the (Python) loop was unavoidable, I though that maybe calling GetLine was somewhat more direct. But anyways, if the improvement is barely noticeable, I'd also prefer to favor readability.

0xCoto commented 1 year ago

Would it make sense to have this function in openEMS.pyx or CSRectGrid.pyx instead? I've personally never used automesh.py (as I've been using my own automatic mesh-generation algorithm), and I'm not sure I would expect to find a timestep calculation function in a file whose name hints to be related with automatic meshing.

E.g. if I wanted to simply check the timestep in Simple_Patch_Antenna.py, it would be a bit more convenient to not have to do another import.

Not a big deal either way though.

thliebig commented 1 year ago

Well no place is ideal, but there it makes most sense? Because the automesh algorithms could use it to check if the generated mesh is within spec for example. Furthermore this should stay pure python code. In any *.pyx it would be compiled cython code where debugging and so on is always much harder... Additionally most user will probably not (and maybe should not) use it, as it is not easy to determine if a time-step is okay or not.