Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing objects doesn't reset grid permittivity #4

Open
tlambert03 opened this issue Oct 2, 2019 · 9 comments
Open

removing objects doesn't reset grid permittivity #4

tlambert03 opened this issue Oct 2, 2019 · 9 comments
Assignees

Comments

@tlambert03
Copy link

Very cool project! I look forward to seeing where it goes.

Small observation here, after playing around a little bit:
When you add an object, and then later remove it, you end up with a section of the grid with altered permittivity, but without an object present in the visualization. Lacking a grid.remove_object() method, I naively just popped the object from grid.objects... and used grid.reset(). but obviously, that doesn't undo the changes to the grid that occurred when the object was registered.

I'd definitely understand if support for removing items (rather than just re-instantiating a new grid) was low priority for now, but just figured I'd mention it.
thanks again!

@flaport
Copy link
Owner

flaport commented Oct 2, 2019

Thanks for bringing that up!

It's indeed something I haven't implemented yet [read: haven't really though of so far;) ]. I'll see what I can do in the coming days!

cheers!

@flaport flaport self-assigned this Oct 2, 2019
@tlambert03
Copy link
Author

by the way... i'm having a ton of fun with this code. in case it gives you more motivation to keep working on it, I wanted to share a small simulation i did with it. thanks again
https://www.youtube.com/watch?v=amlgKhnpGO4

@flaport
Copy link
Owner

flaport commented Oct 5, 2019

Hey that's really cool! Thanks 😊

Right now I'm near the end of my PhD, so development of this library is kinda on hold until I've submitted, but after that I'll definitely resume working on it!

Also... the examples folder in this library is quite empty.. If you're willing to share your code, you're definitely welcome to make a pull request and upload your simulation 😉

flaport added a commit that referenced this issue Oct 10, 2019
The following methods are tested to work:
```
    grid.objects.pop(idx)
    del grid.objects[idx]
    del grid.object_name
```
@flaport
Copy link
Owner

flaport commented Oct 10, 2019

Objects can now be removed from the grid. I made it so that the naive implementation you tried just works, for example

grid.objects.pop(idx)
del grid.objects[idx]
del grid.object_name

all work.

To implement this, I had to change the behavior of each object. Internally, when adding an object to the grid, it will only encode the difference between its inverse permittivity and the global inverse permittivity in the grid (instead of encoding the full inverse permittivity and setting the grid index to zero at the object's location). By implementing it this way, objects can safely be removed from the grid without affecting the global grid index.

@flaport flaport closed this as completed Oct 10, 2019
@tlambert03
Copy link
Author

thanks!

@flaport
Copy link
Owner

flaport commented Oct 14, 2019

opened again as previous solution introduced bugs in the PML (#6)

@Kiranalu
Copy link

by the way... i'm having a ton of fun with this code. in case it gives you more motivation to keep working on it, I wanted to share a small simulation i did with it. thanks again
https://www.youtube.com/watch?v=amlgKhnpGO4

Hello Sir, how do you manage to see the filed at every time , like how did you made the movie?

@0xDBFB7
Copy link
Contributor

0xDBFB7 commented May 30, 2020

@Kiranalu I don't know how the OP did it, but something like this might work (untested):

for i in range(0, N_timesteps):
    grid.step()
    grid.visualize(show=False)
    plt.savefig(str(i) + ".png")

and then use something like ffmpeg to make the .png images into a movie.

Matplotlib also has an animation module that might be easier, but I haven't tried it.

Good luck!

@0xDBFB7
Copy link
Contributor

0xDBFB7 commented Jun 5, 2020

@Kiranalu There's an even easier method; use the pyEVTK library and Paraview!

    def dump_to_vtk(self, filename, iteration, Ex_dump=False, Ey_dump=False, Ez_dump=False, Emag_dump=True, objects_dump=True, ports_dump=True):
        '''
        Extension is automatically chosen, you don't need to supply it

        thanks
        https://pyscience.wordpress.com/2014/09/06/numpy-to-vtk-converting-your-numpy-arrays-to-vtk-arrays-and-files/
        https://bitbucket.org/pauloh/pyevtk/src/default/src/hl.py

        Paraview needs a threshold operation to view the objects correctly.

        '''


        x = np.linspace(0, self.cell_size*self.grid.Nx, self.grid.Nx+1) #there might be an off-by-one error here.
        y = np.linspace(0, self.cell_size*self.grid.Ny, self.grid.Ny+1)
        z = np.linspace(0, self.cell_size*self.grid.Nz, self.grid.Nz+1)

        cellData = {}

        if(not isinstance(fdtd.backend, NumpyBackend)):
            E_copy = self.grid.E.cpu()
        else:
            E_copy = self.grid.E

        if(objects_dump):
            objects = np.zeros_like(E_copy[:,:,:,X])
            for obj in self.grid.objects:
                objects[obj.x.start:obj.x.stop, obj.y.start:obj.y.stop, obj.z.start:obj.z.stop] = 1
            cellData['objects'] = objects

        if(Ex_dump):
            cellData['Ex'] = np.ascontiguousarray(E_copy[:,:,:,X])
        if(Ey_dump):
            cellData['Ey'] = np.ascontiguousarray(E_copy[:,:,:,Y])
        if(Ez_dump):
            cellData['Ez'] = np.ascontiguousarray(E_copy[:,:,:,Z])
        if(Emag_dump):
            cellData['Emag'] = np.ascontiguousarray(self.E_magnitude(E_copy)) # gridToVTK expects a contiguous array.


        gridToVTK(filename + str(iteration), x, y, z, cellData = cellData)

Paraview makes very pretty videos.

woot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants