flaport / fdtd

A 3D electromagnetic FDTD simulator written in Python with optional GPU support
https://fdtd.readthedocs.io
MIT License
484 stars 117 forks source link

Cylinders #35

Closed GenB31415 closed 2 years ago

GenB31415 commented 2 years ago

Hi @flaport, I have to add a cylinder aligned || z in 3D. There are various references in general how to create a cylinder. https://stackoverflow.com/questions/26989131/add-cylinder-to-plot https://stackoverflow.com/questions/64725784/color-of-the-cylinder-in-python

But what may be the optimal way to do that in fdtd? [03-objects-of-arbitrary-shape] Thank you in advance.

aizvorski commented 2 years ago

@GenB31415 I think you can modify the 03-objects-of-arbitrary-shape example into 3d to make the circle into a cylinder, like this:

grid = fdtd.Grid(shape = (300, 300, 100), ...)
...
refractive_index = 1.7
x = y = np.linspace(-1,1,100)
X, Y = np.meshgrid(x, y)
circle_mask = X**2 + Y**2 < 1
permittivity = np.ones((100,100,10,3)) + circle_mask[:,:,None,None]*(refractive_index**2 - 1)
grid[170:270, 100:200, 45:55] = fdtd.Object(permittivity=permittivity, name="object")

That should give a z-axis aligned cylinder with diameter 100 and height 10. There's nothing really special about a cylinder, it's just a stack of circles; the None,None adds two extra size-1 axes (one for z and one for anisotropic permittivity), and numpy broadcasting makes a stack of the circle_mask along the extra axes to match the (100,100,10,3) shape.

flaport commented 2 years ago

Indeed. stacking circles is the recommended way.

GenB31415 commented 2 years ago

Hi @aizvorski @flaport Many thanks, this works. Is it possible by similar manner to create the the walled empty cylinder with the internal r1 and external r2 radiuses respectively? Thanks again.

flaport commented 2 years ago

Ideally this would be done by creating two overlapping Objects, however overlapping objects is currently not really supported. Alternatively you can change the grid.inverse_permittivity array manually. First create the bigger cylinder directly into the grid.inverse_permittivity array, then create the smaller one.

GenB31415 commented 2 years ago

Hi @flaport I've followed your comments in the attached files. In addition, I expanded the task a little. I usually prefer to study a 3D system, so.

  1. I have placed a cylinder with walls in the center.
  2. To visualize the structure of the field by slices, I use the well-known standard approach, which allows us to move and view the slices of the field along the z-axis using the mouse wheel.
  3. I also applied the simplest Monte Carlo technique to see the geometry of the field. To this end I use N = 1000 randomly distributed point sources, please see the attached figure, where is shown the field slice for z=50. Thanks for the comments. circular_object00e.zip SliceOf-3DField
GenB31415 commented 2 years ago

Sorry, the number of sources is N=55 (not 1000)

flaport commented 2 years ago

Hey @GenB31415 , this looks cool! Can you add this to the examples folder and create a PR for this?