gdsfactory / kfactory

gdsfactory with a klayout backend
https://gdsfactory.github.io/kfactory/
MIT License
34 stars 13 forks source link

Can't import VKCells into PDK repo #475

Closed josepfargas closed 2 months ago

josepfargas commented 2 months ago

When trying to import an euler bend in a PDK repo, if I use the virtual version I get an error

This is fine bend = kf.cells.euler.bend_euler_factory(pdk)(width=1, radius=10, angle=15, layer=pdk.infos.MYLAYER)

This gives an error: e_bend = kf.cells.virtual.euler.virtual_bend_euler(pdk)(width=1, radius=10, angle=15, layer=pdk.infos.MYLAYER) TypeError: unhashable type: 'MY_PDK'

Is this a bug, or am I doing something wrong?

Thanks

sebastian-goeldi commented 2 months ago

You need to use the factories for this. Tje cells are registered to the default KCLayout, i.e. kf.kcl. The factories allow you to register default functions to arbitrary KCLayouts/PDK. Also important, you need to make sure you only register it once, if you do it twice, you will have a cache miss and it will create a duplicate cell.

So, do:

/path/to/pdk/my_submodule_for_virtuals.py ```python bend_euler_virtual = kf.factories.virtual.bend_euler_factory(MYPDK) ```
/path/to/pdk/other_submodule.py ```python # this of course also works if this is not in the pdk # In that case just use `from pdk.my_submoduo_for_virtuals import bend_euler_virtual` from .. import bend_euler_virtual @MYPDK.cell def cell_with_virtual(...) -> kf.KCell: c = MYPDK.kcell() # ... bend = c << bendeuler_virtual(width=1, radius=10, angle=15, layer=pdk.infos.MYLAYER) ```

The problem in your case is that you pass the pdk where kfactory expects the first argument of the cell function. But it gets shadowed because KCLayout is not hashable. I think I can kind of fix that by makung it hashable through the name. But I need to think first whether that is a good idea or not.

josepfargas commented 1 month ago

Sorry, just got to this, It seems that creating the factory first bend_euler_virtual = kf.factories.virtual.bend_euler_factory(MYPDK) creates the same error as before TypeError: unhashable type: 'MY_PDK'

josepfargas commented 1 month ago

s_bend = c.create_vinst(kf.cells.euler.bend_euler_factory(MYPDK)(width=1, radius=10, angle=15, layer=pdk.infos.MYLAYER,)) This worked tho.