GithubHelp home page GithubHelp logo

moving source about fdtd HOT 6 CLOSED

flaport avatar flaport commented on July 26, 2024
moving source

from fdtd.

Comments (6)

flaport avatar flaport commented on July 26, 2024

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.

GenB31415 avatar GenB31415 commented on July 26, 2024

from fdtd.

GenB31415 avatar GenB31415 commented on July 26, 2024

(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.

flaport avatar flaport commented on July 26, 2024

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.

GenB31415 avatar GenB31415 commented on July 26, 2024

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.

flaport avatar flaport commented on July 26, 2024

great to hear it's working now :)

from fdtd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.