Comments (6)
Hey @GenB31415 ,
Moving sources is not really supported by this package. However you can probably hack it as follows:
# define the position x, y and z of the source at each time t here:
x = ...
y = ...
z = ...
t = ...
for i, t in enumerate(t):
grid.step()
source = grid.sources.pop(-1) # make sure the source is no longer part of the grid
grid[x[i], y[i], z[i]] = source
from fdtd.
from fdtd.
(repetition)
Dear @flaport , many thanks for the idea.
I did that but I the error is generated: The grid already has an attribute with name pointsource
My code is bellow (slightly modified code 01-basic-example.py ). Please let me know how to overcome this issue.
Thank you.
#fdtd.readthedocs.io/en/latest/examples/01-basic-example.html
import matplotlib.pyplot as plt
import fdtd
import fdtd.backend as bd
fdtd.set_backend("numpy")
# Constants
WAVELENGTH = 1550e-9
SPEED_LIGHT: float = 299_792_458.0 # [m/s] speed of light
# Simulation
# create FDTD Grid
grid = fdtd.Grid(
(2.5e-5, 1.5e-5, 1),
grid_spacing=0.1 * WAVELENGTH,
permittivity=1.0,
permeability=1.0,
)
# boundaries
# grid[0, :, :] = fdtd.PeriodicBoundary(name="xbounds")
grid[0:10, :, :] = fdtd.PML(name="pml_xlow")
grid[-10:, :, :] = fdtd.PML(name="pml_xhigh")
# grid[:, 0, :] = fdtd.PeriodicBoundary(name="ybounds")
grid[:, 0:10, :] = fdtd.PML(name="pml_ylow")
grid[:, -10:, :] = fdtd.PML(name="pml_yhigh")
grid[:, :, 0] = fdtd.PeriodicBoundary(name="zbounds")
# sources
# grid[50:55, 70:75, 0] = fdtd.LineSource(
# period=WAVELENGTH / SPEED_LIGHT, name="linesource"
# )
#grid[100, 60, 0] = fdtd.PointSource(
grid[120, 50, 0] = fdtd.PointSource(
period=WAVELENGTH / SPEED_LIGHT, name="pointsource",
)
# detectors
#grid[12e-6, :, 0] = fdtd.LineDetector(name="detector")
# objects
grid[11:32, 30:84, 0:1] = fdtd.AnisotropicObject(permittivity=2.5, name="object")
# - - - - - - start - - - - - - - -
# define the position x, y and z of the source at each time t here:
x0 = 120
y0 = 50
z0 = 0
tFin=50
t = range(0,tFin)
velocity = 0.1 # <---- velocity of a point source only in x direction
for i, t in enumerate(t):
grid.step()
posX = x0-velocity*t # <---- move source in next position x at time increase
posY = y0 # <---- fixed position y
posZ = z0 # <---- fixed position t
source = grid.sources.pop(-1) # make sure the source is no longer part of the grid
grid[posX, posY, posZ] = source
# - - - - - end- - - - - - - - -
# Run simulation
grid.run(tFin, progress_bar=False)
# Visualization
fig, axes = plt.subplots(2, 3, squeeze=False)
titles = ["Ex: xy", "Ey: xy", "Ez: xy", "Hx: xy", "Hy: xy", "Hz: xy"]
fields = bd.stack(
[
grid.E[:, :, 0, 0],
grid.E[:, :, 0, 1],
grid.E[:, :, 0, 2],
grid.H[:, :, 0, 0],
grid.H[:, :, 0, 1],
grid.H[:, :, 0, 2],
]
)
m = max(abs(fields.min().item()), abs(fields.max().item()))
for ax, field, title in zip(axes.ravel(), fields, titles):
ax.set_axis_off()
ax.set_title(title)
ax.imshow(bd.numpy(field), vmin=-m, vmax=m, cmap="RdBu")
plt.show()
plt.figure()
grid.visualize(z=0)
from fdtd.
You gave your source the name 'pointsource', so it's available as an attribute to the grid, i.e. grid.pointsource
. In that case you also have to delete that attribute in the loop:
source = grid.sources.pop(-1)
del grid.pointsource
from fdtd.
Hey @flaport, thanks, now it works.
The code (run or uncomment corresponding line)
import matplotlib.pyplot as plt
import fdtd
import fdtd.backend as bd
fdtd.set_backend("numpy")
OneMicron=1e-6
WAVELENGTH = OneMicron
LengthScale = OneMicron
SPEED_LIGHT: float = 299_792_458.0 # [m/s] speed of light
indRefr=1.15
grid = fdtd.Grid(
(25*LengthScale, 25*LengthScale, 1),
grid_spacing=0.1 * LengthScale, permittivity=indRefr**2, permeability=1.0,
)
grid[0:10, :, :] = fdtd.PML(name="pml_xlow")
grid[-10:, :, :] = fdtd.PML(name="pml_xhigh")
grid[:, 0:10, :] = fdtd.PML(name="pml_ylow")
grid[:, -10:, :] = fdtd.PML(name="pml_yhigh")
x0 = 17.0*LengthScale; y0 = 12.5*LengthScale; z0 = 0;
periodTime= LengthScale / SPEED_LIGHT
grid[x0, y0, z0] = fdtd.PointSource( period = periodTime )
# velocity of a point source only in x direction
#velocity = 0.69; tFin=20*1
#velocity = 0.6; tFin=20*1
#velocity = 0.5; tFin=20*1
#velocity = 0.3; tFin=20*2
velocity = 0.2; tFin=20*3
#velocity = 0.1; tFin=20*4
#velocity = 0.05; tFin=20*4
#velocity = 0.005; tFin=20*5
tAll = range(0, tFin)
for i, t in enumerate(tAll):
grid.step()
tt = t*LengthScale
posX = (x0-velocity*tt) # <---- move source in next position x when time increases
posY = y0 # <---- fixed position y
posZ = z0 # <---- fixed position z
source = grid.sources.pop(-1) # make sure the source is no longer part of the grid
source = fdtd.PointSource(period=periodTime, name=(f"Cher: i={i}"))
grid[posX, posY, posZ] = source
grid.run(tFin, progress_bar=False)
fig, axes = plt.subplots(2, 3, squeeze=False)
titles = ["Ex: xy", "Ey: xy", "Ez: xy", "Hx: xy", "Hy: xy", "Hz: xy"]
fields = bd.stack(
[
grid.E[:, :, 0, 0],
grid.E[:, :, 0, 1],
grid.E[:, :, 0, 2],
grid.H[:, :, 0, 0],
grid.H[:, :, 0, 1],
grid.H[:, :, 0, 2],
]
)
m = max(abs(fields.min().item()), abs(fields.max().item()))
import numpy as np
for ax, field, title in zip(axes.ravel(), fields, titles):
ax.set_axis_off()
ax.set_title(title)
ax.imshow(bd.numpy(field), vmin=-m, vmax=m, cmap="hsv")
plt.show()
plt.figure()
plt.title(source.name +f", v={velocity}, n={indRefr}")
grid.visualize(z=0,cmap="nipy_spectral_r",)
from fdtd.
great to hear it's working now :)
from fdtd.
Related Issues (20)
- Regarding the detected value
- Field in position of source HOT 3
- Can I simulate birefringent material (eg. liquid crystal) with this simulation? HOT 1
- is the pytorch backend differentiable? HOT 3
- Unit correction HOT 1
- parallel computation HOT 2
- Error using Cuda HOT 1
- Polarization of plane source HOT 2
- How should I implement pattern lighting? HOT 1
- Is fdtd suitable for antenna design? HOT 1
- How do I define a custom electromagnetic field profile as source, like the Import Source in the Lumerical FDTD?
- Hello! How to obtain the phase of electromagnetic field?
- ffmpeg not installed? HOT 15
- `RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!` HOT 7
- Loading Spatial Distributions HOT 1
- Polarization for Linear Source
- Version on pypi doesn't have commit "don't use bd.float directly for type casting" and is erroring HOT 1
- The kernel appears to have died. It will restart automatically.
- Simulation of metals (complex refractive index) HOT 3
- development dependencies not downloading HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fdtd.