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

GDSII import for Scheme #1175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

GDSII import for Scheme #1175

wants to merge 3 commits into from

Conversation

stevengj
Copy link
Collaborator

@stevengj stevengj commented Apr 9, 2020

Implement functions GDSII-layers, GDSII-vol, and get-GDSII-prisms in Scheme mirroring those in C++ and Python.

@stevengj
Copy link
Collaborator Author

stevengj commented Apr 9, 2020

@oskooi, I think this is basically working now?

@oskooi
Copy link
Collaborator

oskooi commented Apr 9, 2020

GDSII-layers is working but not get-GDSII-prisms as demonstrated with a simple example using coupler.gds in meep/python/examples:

meep> (GDSII-layers "coupler.gds")
$1 = (0 1 2 3 4 5 31 32)
meep> (get-GDSII-prisms (material (make medium (index 3.5))) "coupler.gds" 31 0 0)
fewer than 3 vertices in init_prism

Calling get-GDSII-prisms causes Scheme to abort.

For comparison, here is the output from PyMeep:

>>> import meep as mp
Using MPI version 3.1, 1 processes
>>> mp.GDSII_layers("coupler.gds")
[0, 1, 2, 3, 4, 5, 31, 32]
>>> mp.get_GDSII_prisms(mp.Medium(index=3.5), "coupler.gds", 31, 0, 0)
[<meep.geom.Prism object at 0x7f59cc41d6d8>, <meep.geom.Prism object at 0x7f59cc428128>, <meep.geom.Prism object at 0x7f59cc428588>]

Note that get_GDSII_prisms returns three separate Prism objects in layer 31 of the GDSII file.

@stevengj
Copy link
Collaborator Author

It sounds like that error is happening in the meep_geom::get_GDSII_prisms function…

@oskooi
Copy link
Collaborator

oskooi commented Apr 11, 2020

Note the output from GDSII-vol which is a SWIG object (not a Scheme volume object as it should be):

meep> (GDSII-vol "coupler.gds" 0 0 0)
$2 = #<collectable-swig-pointer meep::volume * 555943558ab0>

@stevengj
Copy link
Collaborator Author

If you do (volume (size 0 0 0) (center 0)) in Scheme it also returns a swig object.

@stevengj
Copy link
Collaborator Author

stevengj commented Apr 11, 2020

The way you call a volume method is via something like (meep-volume-center vol) (this is how SWIG translates C++ method calls)

@stevengj
Copy link
Collaborator Author

stevengj commented Apr 11, 2020

If I do (get-GDSII-prisms air "coupler.gds" 31 0 0) it works for me:

meep> (get-GDSII-prisms air "coupler.gds" 31 0 0)
$1 = (((prism geometric-object) (center . #(+nan.0 +nan.0 +nan.0)) (material (medium material-type) (epsilon-diag . #(1.0 1.0 1.0)) (epsilon-offdiag . #(0.0 0.0 0.0)) (mu-diag . #(1.0 1.0 1.0)) (mu-offdiag . #(0.0 0.0 0.0)) (E-susceptibilities) (H-susceptibilities) (E-chi2-diag . #(0.0 0.0 0.0)) (E-chi3-diag . #(0.0 0.0 0.0)) (H-chi2-diag . #(0.0 0.0 0.0)) (H-chi3-diag . #(0.0 0.0 0.0)) (D-conductivity-diag . #(0.0 0.0 0.0)) (B-conductivity-diag . #(0.0 0.0 0.0))) (vertices #(-0.004 1.5e-4 0.0) #(-0.004108 1.5099999999999998e-4 0.0) #(-0.004215 1.5199999999999998e-4 0.0) #(-0.0043219999999999995 1.55e-4 0.0) #(-0.004429 1.5999999999999999e-4 0.0) #(-0.004535 1.65e-4 0.0) #(-0.004641 1.7099999999999998e-4 0.0) #(-0.0047469999999999995 1.79e-4 0.0) #(-0.0048519999999999995 1.87e-4 0.0) #(-0.005062 2.07e-4 0.0) #(-0.005167 2.1899999999999998e-4 0.0) #(-0.005271 2.3099999999999998e-4 0.0) #(-0.0054789999999999995 2.59e-4 0.0) #(-0.005582 2.74e-4 0.0) #(-0.005685 2.9e-4 0.0) #(-0.005788 3.07e-4 0.0) #(-0.0058909999999999995 3.25e-4 0.0) #(-0.005993 3.43e-4 0.0) #(-0.006095 3.6199999999999996e-4 0.0) #(-0.006196999999999999 3.8199999999999996e-4 0.0) #(-0.006299 4.03e-4 0.0) #(-0.0063999999999999994 4.24e-4 0.0) #(-0.006501 4.46e-4 0.0) #(-0.006703 4.919999999999999e-4 0.0) #(-0.006803 5.149999999999999e-4 0.0) #(-0.0069039999999999995 5.4e-4 0.0) #(-0.0072039999999999995 6.15e-4 0.0) #(-0.007303 6.42e-4 0.0) #(-0.007403 6.68e-4 0.0) #(-0.007502 6.95e-4 0.0) #(-0.007600999999999999 7.23e-4 0.0) #(-0.007699999999999999 7.5e-4 0.0) #(-0.0077989999999999995 7.779999999999999e-4 0.0) #(-0.007898 8.07e-4 0.0) #(-0.007996 8.349999999999999e-4 0.0) #(-0.008095 8.64e-4 0.0) #(-0.008192999999999999 8.929999999999999e-4 0.0) #(-0.008291 9.23e-4 0.0) #(-0.008388999999999999 9.519999999999999e-4 0.0) #(-0.008487 9.82e-4 0.0) #(-0.008584999999999999 0.001011 0.0) #(-0.008780999999999999 0.001071 0.0) #(-0.008877999999999999 0.001101 0.0) #(-0.009073999999999999 0.001161 0.0) #(-0.009268 0.0012209999999999999 0.0) #(-0.009366 0.001251 0.0) #(-0.009463 0.001281 0.0) #(-0.009559999999999999 0.00131 0.0) #(-0.009658 0.00134 0.0) #(-0.009949 0.0014269999999999999 0.0) #(-0.010047 0.0014559999999999998 0.0) #(-0.010144 0.001485 0.0) #(-0.010338 0.0015409999999999998 0.0) #(-0.010435999999999999 0.001568 0.0) #(-0.010532999999999999 0.001596 0.0) #(-0.010629999999999999 0.0016229999999999999 0.0) #(-0.010728 0.0016489999999999999 0.0) #(-0.010922 0.0017009999999999998 0.0) #(-0.01102 0.0017259999999999999 0.0) #(-0.011117 0.0017499999999999998 0.0) #(-0.011215 0.0017749999999999999 0.0) #(-0.011313 0.001798 0.0) #(-0.01141 0.001821 0.0) #(-0.011508 0.001844 0.0) #(-0.011606 0.0018659999999999998 0.0) #(-0.011703999999999999 0.001887 0.0) #(-0.011802 0.0019069999999999998 0.0) #(-0.011901 0.001927 0.0) #(-0.011999 0.0019459999999999998 0.0) #(-0.012098 0.0019649999999999997 0.0) #(-0.012196 0.001983 0.0) #(-0.012294999999999999 0.002 0.0) #(-0.012393999999999999 0.002016 0.0) #(-0.012591999999999999 0.002046 0.0) #(-0.012691 0.0020599999999999998 0.0) #(-0.012791 0.002072 0.0) #(-0.012889999999999999 0.002084 0.0) #(-0.01299 0.002095 0.0) #(-0.013189999999999999 0.0021149999999999997 0.0) #(-0.013290999999999999 0.002123 0.0) #(-0.013392 0.00213 0.0) #(-0.013491999999999999 0.002136 0.0) #(-0.013593 0.002141 0.0) #(-0.013694999999999999 0.0021449999999999998 0.0) #(-0.013796 0.0021479999999999997 0.0) #(-0.013999999999999999 0.00215 0.0) #(-0.0172 0.00215 0.0) #(-0.0172 0.00265 0.0) #(-0.013999999999999999 0.00265 0.0) #(-0.013892 0.002649 0.0) #(-0.013784999999999999 0.0026479999999999997 0.0) #(-0.013678 0.0026449999999999998 0.0) #(-0.013571 0.00264 0.0) #(-0.013465 0.0026349999999999998 0.0) #(-0.013359 0.002629 0.0) #(-0.013253 0.002621 0.0) #(-0.013148 0.002613 0.0) #(-0.012938 0.002593 0.0) #(-0.012832999999999999 0.002581 0.0) #(-0.012728999999999999 0.0025689999999999997 0.0) #(-0.012521 0.002541 0.0) #(-0.012418 0.002526 0.0) #(-0.012315 0.00251 0.0) #(-0.012211999999999999 0.002493 0.0) #(-0.012109 0.0024749999999999998 0.0) #(-0.012007 0.002457 0.0) #(-0.011904999999999999 0.002438 0.0) #(-0.011803 0.002418 0.0) #(-0.011701 0.002397 0.0) #(-0.0116 0.002376 0.0) #(-0.011498999999999999 0.0023539999999999998 0.0) #(-0.011297 0.0023079999999999997 0.0) #(-0.011197 0.0022849999999999997 0.0) #(-0.011096 0.00226 0.0) #(-0.010796 0.002185 0.0) #(-0.010697 0.0021579999999999998 0.0) #(-0.010596999999999999 0.0021319999999999998 0.0) #(-0.010498 0.002105 0.0) #(-0.010399 0.002077 0.0) #(-0.0103 0.0020499999999999997 0.0) #(-0.010201 0.002022 0.0) #(-0.010102 0.001993 0.0) #(-0.010003999999999999 0.0019649999999999997 0.0) #(-0.009904999999999999 0.001936 0.0) #(-0.009807 0.0019069999999999998 0.0) #(-0.009708999999999999 0.001877 0.0) #(-0.009611 0.0018479999999999998 0.0) #(-0.009512999999999999 0.001818 0.0) #(-0.009415 0.001789 0.0) #(-0.009219 0.0017289999999999999 0.0) #(-0.009122 0.001699 0.0) #(-0.008926 0.0016389999999999998 0.0) #(-0.008732 0.0015789999999999999 0.0) #(-0.008634 0.001549 0.0) #(-0.008537 0.001519 0.0) #(-0.00844 0.00149 0.0) #(-0.008342 0.00146 0.0) #(-0.008050999999999999 0.0013729999999999999 0.0) #(-0.007953 0.001344 0.0) #(-0.007856 0.001315 0.0) #(-0.0076619999999999995 0.001259 0.0) #(-0.0075639999999999995 0.001232 0.0) #(-0.007467 0.001204 0.0) #(-0.00737 0.0011769999999999999 0.0) #(-0.007272 0.001151 0.0) #(-0.007078 0.001099 0.0) #(-0.00698 0.0010739999999999999 0.0) #(-0.006882999999999999 0.00105 0.0) #(-0.006784999999999999 0.0010249999999999999 0.0) #(-0.006686999999999999 0.0010019999999999999 0.0) #(-0.0065899999999999995 9.79e-4 0.0) #(-0.0064919999999999995 9.559999999999999e-4 0.0) #(-0.0063939999999999995 9.339999999999999e-4 0.0) #(-0.0062959999999999995 9.13e-4 0.0) #(-0.0061979999999999995 8.929999999999999e-4 0.0) #(-0.006098999999999999 8.73e-4 0.0) #(-0.006000999999999999 8.539999999999999e-4 0.0) #(-0.005902 8.349999999999999e-4 0.0) #(-0.005804 8.169999999999999e-4 0.0) #(-0.005705 7.999999999999999e-4 0.0) #(-0.005606 7.84e-4 0.0) #(-0.005408 7.54e-4 0.0) #(-0.0053089999999999995 7.4e-4 0.0) #(-0.005209 7.279999999999999e-4 0.0) #(-0.00511 7.16e-4 0.0) #(-0.00501 7.05e-4 0.0) #(-0.00481 6.85e-4 0.0) #(-0.004709 6.77e-4 0.0) #(-0.004608 6.7e-4 0.0) #(-0.004508 6.64e-4 0.0) #(-0.0044069999999999995 6.59e-4 0.0) #(-0.004305 6.55e-4 0.0) #(-0.004203999999999999 6.52e-4 0.0) #(-0.004 6.5e-4 0.0)) (height . 0.0) (axis . #(0.0 0.0 1.0)) (sidewall_angle . 0) (vertices_p) (top_polygon_diff_vectors_p) (top_polygon_diff_vectors_scaled_p) (vertices_top_p) (vertices_top) (centroid . #(0.0 0.0 0.0)) (workspace) (m_c2p . #(#(1.0 0.0 0.0) #(0.0 1.0 0.0) #(0.0 0.0 1.0))) (m_p2c . #(#(1.0 0.0 0.0) #(0.0 1.0 0.0) #(0.0 0.0 1.0)))) ((prism geometric-object) (center . #(+nan.0 +nan.0 +nan.0)) (material (medium material-type) (epsilon-diag . #(1.0 1.0 1.0)) (epsilon-offdiag . #(0.0 0.0 0.0)) (mu-diag . #(1.0 1.0 1.0)) (mu-offdiag . #(0.0 0.0 0.0)) (E-susceptibilities) (H-susceptibilities) (E-chi2-diag . #(0.0 0.0 0.0)) (E-chi3-diag . #(0.0 0.0 0.0)) (H-chi2-diag . #(0.0 0.0 0.0)) (H-chi3-diag . #(0.0 0.0 0.0)) (D-conductivity-diag . #(0.0 0.0 0.0)) (B-conductivity-diag . #(0.0 0.0 0.0))) (vertices #(0.004 1.5e-4 0.0) #(0.004 6.5e-4 0.0) #(0.004203999999999999 6.52e-4 0.0) #(0.004305 6.55e-4 0.0) #(0.0044069999999999995 6.59e-4 0.0) #(0.004508 6.64e-4 0.0) #(0.004608 6.7e-4 0.0) #(0.004709 6.77e-4 0.0) #(0.00481 6.85e-4 0.0) #(0.00501 7.05e-4 0.0) #(0.00511 7.16e-4 0.0) #(0.005209 7.279999999999999e-4 0.0) #(0.0053089999999999995 7.4e-4 0.0) #(0.005408 7.54e-4 0.0) #(0.005606 7.84e-4 0.0) #(0.005705 7.999999999999999e-4 0.0) #(0.005804 8.169999999999999e-4 0.0) #(0.005902 8.349999999999999e-4 0.0) #(0.006000999999999999 8.539999999999999e-4 0.0) #(0.006098999999999999 8.73e-4 0.0) #(0.0061979999999999995 8.929999999999999e-4 0.0) #(0.0062959999999999995 9.13e-4 0.0) #(0.0063939999999999995 9.339999999999999e-4 0.0) #(0.0064919999999999995 9.559999999999999e-4 0.0) #(0.0065899999999999995 9.79e-4 0.0) #(0.006686999999999999 0.0010019999999999999 0.0) #(0.006784999999999999 0.0010249999999999999 0.0) #(0.006882999999999999 0.00105 0.0) #(0.00698 0.0010739999999999999 0.0) #(0.007078 0.001099 0.0) #(0.007272 0.001151 0.0) #(0.00737 0.0011769999999999999 0.0) #(0.007467 0.001204 0.0) #(0.0075639999999999995 0.001232 0.0) #(0.0076619999999999995 0.001259 0.0) #(0.007856 0.001315 0.0) #(0.007953 0.001344 0.0) #(0.008050999999999999 0.0013729999999999999 0.0) #(0.008342 0.00146 0.0) #(0.00844 0.00149 0.0) #(0.008537 0.001519 0.0) #(0.008634 0.001549 0.0) #(0.008732 0.0015789999999999999 0.0) #(0.008926 0.0016389999999999998 0.0) #(0.009122 0.001699 0.0) #(0.009219 0.0017289999999999999 0.0) #(0.009415 0.001789 0.0) #(0.009512999999999999 0.001818 0.0) #(0.009611 0.0018479999999999998 0.0) #(0.009708999999999999 0.001877 0.0) #(0.009807 0.0019069999999999998 0.0) #(0.009904999999999999 0.001936 0.0) #(0.010003999999999999 0.0019649999999999997 0.0) #(0.010102 0.001993 0.0) #(0.010201 0.002022 0.0) #(0.0103 0.0020499999999999997 0.0) #(0.010399 0.002077 0.0) #(0.010498 0.002105 0.0) #(0.010596999999999999 0.0021319999999999998 0.0) #(0.010697 0.0021579999999999998 0.0) #(0.010796 0.002185 0.0) #(0.011096 0.00226 0.0) #(0.011197 0.0022849999999999997 0.0) #(0.011297 0.0023079999999999997 0.0) #(0.011498999999999999 0.0023539999999999998 0.0) #(0.0116 0.002376 0.0) #(0.011701 0.002397 0.0) #(0.011803 0.002418 0.0) #(0.011904999999999999 0.002438 0.0) #(0.012007 0.002457 0.0) #(0.012109 0.0024749999999999998 0.0) #(0.012211999999999999 0.002493 0.0) #(0.012315 0.00251 0.0) #(0.012418 0.002526 0.0) #(0.012521 0.002541 0.0) #(0.012728999999999999 0.0025689999999999997 0.0) #(0.012832999999999999 0.002581 0.0) #(0.012938 0.002593 0.0) #(0.013148 0.002613 0.0) #(0.013253 0.002621 0.0) #(0.013359 0.002629 0.0) #(0.013465 0.0026349999999999998 0.0) #(0.013571 0.00264 0.0) #(0.013678 0.0026449999999999998 0.0) #(0.013784999999999999 0.0026479999999999997 0.0) #(0.013892 0.002649 0.0) #(0.013999999999999999 0.00265 0.0) #(0.0172 0.00265 0.0) #(0.0172 0.00215 0.0) #(0.013999999999999999 0.00215 0.0) #(0.013796 0.0021479999999999997 0.0) #(0.013694999999999999 0.0021449999999999998 0.0) #(0.013593 0.002141 0.0) #(0.013491999999999999 0.002136 0.0) #(0.013392 0.00213 0.0) #(0.013290999999999999 0.002123 0.0) #(0.013189999999999999 0.0021149999999999997 0.0) #(0.01299 0.002095 0.0) #(0.012889999999999999 0.002084 0.0) #(0.012791 0.002072 0.0) #(0.012691 0.0020599999999999998 0.0) #(0.012591999999999999 0.002046 0.0) #(0.012393999999999999 0.002016 0.0) #(0.012294999999999999 0.002 0.0) #(0.012196 0.001983 0.0) #(0.012098 0.0019649999999999997 0.0) #(0.011999 0.0019459999999999998 0.0) #(0.011901 0.001927 0.0) #(0.011802 0.0019069999999999998 0.0) #(0.011703999999999999 0.001887 0.0) #(0.011606 0.0018659999999999998 0.0) #(0.011508 0.001844 0.0) #(0.01141 0.001821 0.0) #(0.011313 0.001798 0.0) #(0.011215 0.0017749999999999999 0.0) #(0.011117 0.0017499999999999998 0.0) #(0.01102 0.0017259999999999999 0.0) #(0.010922 0.0017009999999999998 0.0) #(0.010728 0.0016489999999999999 0.0) #(0.010629999999999999 0.0016229999999999999 0.0) #(0.010532999999999999 0.001596 0.0) #(0.010435999999999999 0.001568 0.0) #(0.010338 0.0015409999999999998 0.0) #(0.010144 0.001485 0.0) #(0.010047 0.0014559999999999998 0.0) #(0.009949 0.0014269999999999999 0.0) #(0.009658 0.00134 0.0) #(0.009559999999999999 0.00131 0.0) #(0.009463 0.001281 0.0) #(0.009366 0.001251 0.0) #(0.009268 0.0012209999999999999 0.0) #(0.009073999999999999 0.001161 0.0) #(0.008877999999999999 0.001101 0.0) #(0.008780999999999999 0.001071 0.0) #(0.008584999999999999 0.001011 0.0) #(0.008487 9.82e-4 0.0) #(0.008388999999999999 9.519999999999999e-4 0.0) #(0.008291 9.23e-4 0.0) #(0.008192999999999999 8.929999999999999e-4 0.0) #(0.008095 8.64e-4 0.0) #(0.007996 8.349999999999999e-4 0.0) #(0.007898 8.07e-4 0.0) #(0.0077989999999999995 7.779999999999999e-4 0.0) #(0.007699999999999999 7.5e-4 0.0) #(0.007600999999999999 7.23e-4 0.0) #(0.007502 6.95e-4 0.0) #(0.007403 6.68e-4 0.0) #(0.007303 6.42e-4 0.0) #(0.0072039999999999995 6.15e-4 0.0) #(0.0069039999999999995 5.4e-4 0.0) #(0.006803 5.149999999999999e-4 0.0) #(0.006703 4.919999999999999e-4 0.0) #(0.006501 4.46e-4 0.0) #(0.0063999999999999994 4.24e-4 0.0) #(0.006299 4.03e-4 0.0) #(0.006196999999999999 3.8199999999999996e-4 0.0) #(0.006095 3.6199999999999996e-4 0.0) #(0.005993 3.43e-4 0.0) #(0.0058909999999999995 3.25e-4 0.0) #(0.005788 3.07e-4 0.0) #(0.005685 2.9e-4 0.0) #(0.005582 2.74e-4 0.0) #(0.0054789999999999995 2.59e-4 0.0) #(0.005271 2.3099999999999998e-4 0.0) #(0.005167 2.1899999999999998e-4 0.0) #(0.005062 2.07e-4 0.0) #(0.0048519999999999995 1.87e-4 0.0) #(0.0047469999999999995 1.79e-4 0.0) #(0.004641 1.7099999999999998e-4 0.0) #(0.004535 1.65e-4 0.0) #(0.004429 1.5999999999999999e-4 0.0) #(0.0043219999999999995 1.55e-4 0.0) #(0.004215 1.5199999999999998e-4 0.0) #(0.004108 1.5099999999999998e-4 0.0)) (height . 0.0) (axis . #(0.0 0.0 1.0)) (sidewall_angle . 0) (vertices_p) (top_polygon_diff_vectors_p) (top_polygon_diff_vectors_scaled_p) (vertices_top_p) (vertices_top) (centroid . #(0.0 0.0 0.0)) (workspace) (m_c2p . #(#(1.0 0.0 0.0) #(0.0 1.0 0.0) #(0.0 0.0 1.0))) (m_p2c . #(#(1.0 0.0 0.0) #(0.0 1.0 0.0) #(0.0 0.0 1.0)))) ((prism geometric-object) (center . #(+nan.0 +nan.0 +nan.0)) (material (medium material-type) (epsilon-diag . #(1.0 1.0 1.0)) (epsilon-offdiag . #(0.0 0.0 0.0)) (mu-diag . #(1.0 1.0 1.0)) (mu-offdiag . #(0.0 0.0 0.0)) (E-susceptibilities) (H-susceptibilities) (E-chi2-diag . #(0.0 0.0 0.0)) (E-chi3-diag . #(0.0 0.0 0.0)) (H-chi2-diag . #(0.0 0.0 0.0)) (H-chi3-diag . #(0.0 0.0 0.0)) (D-conductivity-diag . #(0.0 0.0 0.0)) (B-conductivity-diag . #(0.0 0.0 0.0))) (vertices #(-0.004 1.5e-4 0.0) #(-0.004 6.5e-4 0.0) #(0.004 6.5e-4 0.0) #(0.004 1.5e-4 0.0)) (height . 0.0) (axis . #(0.0 0.0 1.0)) (sidewall_angle . 0) (vertices_p) (top_polygon_diff_vectors_p) (top_polygon_diff_vectors_scaled_p) (vertices_top_p) (vertices_top) (centroid . #(0.0 0.0 0.0)) (workspace) (m_c2p . #(#(1.0 0.0 0.0) #(0.0 1.0 0.0) #(0.0 0.0 1.0))) (m_p2c . #(#(1.0 0.0 0.0) #(0.0 1.0 0.0) #(0.0 0.0 1.0)))))

(3 prism objects).

@oskooi
Copy link
Collaborator

oskooi commented Apr 11, 2020

try this script:

(set-param! resolution 50)

(define-param GDSII-file "coupler.gds")
(define-param CELL-LAYER 0)
(define-param PORT1-LAYER 1)
(define-param PORT2-LAYER 2)
(define-param PORT3-LAYER 3)
(define-param PORT4-LAYER 4)
(define-param SOURCE-LAYER 5)
(define-param UPPER-BRANCH-LAYER 31)
(define-param LOWER-BRANCH-LAYER 32)

(define-param d 0.1) ;; branch separation                                                                                              
(define-param t-oxide 1.0)
(define-param t-Si 0.22)
(define-param t-air 0.78)

(define-param dpml 1)
(define cell-thickness (+ dpml t-oxide t-Si t-air dpml))
(define si-zmin 0)

(define oxide (make medium (epsilon 2.25)))
(define silicon (make medium (epsilon 12.0)))

(define-param wvl-cen 1.55)
(define fcen (/ wvl-cen))
(define df (* 0.2 fcen))

(define-param 3d? false)
(define cell-zmax (if 3d? (* 0.5 cell-thickness) 0))
(define cell-zmin (if 3d? (* -0.5 cell-thickness) 0))
(define si-zmax (if 3d? t-Si 0))

;; read cell size, volumes for source region and flux monitors,
;; and coupler geometry from GDSII file
(define upper-branch (get-GDSII-prisms silicon GDSII-file UPPER-BRANCH-LAYER si-zmin si-zmax))
(define lower-branch (get-GDSII-prisms silicon GDSII-file LOWER-BRANCH-LAYER si-zmin si-zmax))

(set! geometry (list upper-branch lower-branch))

(define cell (GDSII-vol GDSII-file CELL-LAYER cell-zmin cell-zmax))
(define p1 (GDSII-vol GDSII-file PORT1-LAYER si-zmin si-zmax))
(define p2 (GDSII-vol GDSII-file PORT2-LAYER si-zmin si-zmax))
(define p3 (GDSII-vol GDSII-file PORT3-LAYER si-zmin si-zmax))
(define p4 (GDSII-vol GDSII-file PORT4-LAYER si-zmin si-zmax))
(define src-vol (GDSII-vol GDSII-file SOURCE-LAYER si-zmin si-zmax))

(run-until 0 (at-beginning output-epsilon))

@oskooi
Copy link
Collaborator

oskooi commented Apr 11, 2020

from the Python tutorial:

>>> import meep as mp
Using MPI version 3.1, 1 processes
>>> gdsII_file = 'coupler.gds'
>>> CELL_LAYER = 0
>>> mp.GDSII_vol(gdsII_file, CELL_LAYER, 0, 0).size
Vector3<34.4, 8.0, 0.0>
>>> mp.GDSII_vol(gdsII_file, CELL_LAYER, 0, 0).center
Vector3<0.0, 0.0, 0.0>

@stevengj
Copy link
Collaborator Author

stevengj commented Apr 11, 2020

With some fixes, I get:
image

The modified file is

(set-param! resolution 20000)

(define-param GDSII-file "coupler.gds")
(define-param CELL-LAYER 0)
(define-param PORT1-LAYER 1)
(define-param PORT2-LAYER 2)
(define-param PORT3-LAYER 3)
(define-param PORT4-LAYER 4)
(define-param SOURCE-LAYER 5)
(define-param UPPER-BRANCH-LAYER 31)
(define-param LOWER-BRANCH-LAYER 32)

(define-param d 0.1) ;; branch separation                                                                                              
(define-param t-oxide 1.0)
(define-param t-Si 0.22)
(define-param t-air 0.78)

(define-param dpml 1)
(define cell-thickness (+ dpml t-oxide t-Si t-air dpml))
(define si-zmin -1)

(define oxide (make medium (epsilon 2.25)))
(define silicon (make medium (epsilon 12.0)))

(define-param wvl-cen 1.55)
(define fcen (/ wvl-cen))
(define df (* 0.2 fcen))

(define-param 3d? false)
(define cell-zmax (if 3d? (* 0.5 cell-thickness) 0))
(define cell-zmin (if 3d? (* -0.5 cell-thickness) 0))
(define si-zmax (if 3d? t-Si 1))

; read cell size, volumes for source region and flux monitors,
; and coupler geometry from GDSII file
(define upper-branch (get-GDSII-prisms silicon GDSII-file UPPER-BRANCH-LAYER si-zmin si-zmax))
(define lower-branch (get-GDSII-prisms silicon GDSII-file LOWER-BRANCH-LAYER si-zmin si-zmax))

(set! geometry (append upper-branch lower-branch))

(define cell (GDSII-vol GDSII-file CELL-LAYER cell-zmin cell-zmax))
(define p1 (GDSII-vol GDSII-file PORT1-LAYER si-zmin si-zmax))
(define p2 (GDSII-vol GDSII-file PORT2-LAYER si-zmin si-zmax))
(define p3 (GDSII-vol GDSII-file PORT3-LAYER si-zmin si-zmax))
(define p4 (GDSII-vol GDSII-file PORT4-LAYER si-zmin si-zmax))
(define src-vol (GDSII-vol GDSII-file SOURCE-LAYER si-zmin si-zmax))

(set! geometry-lattice (make lattice (size (meep-volume-in-direction cell X)
                                           (meep-volume-in-direction cell Y)
                                           no-size)))

(run-until 0 (at-beginning output-epsilon))

@stevengj
Copy link
Collaborator Author

For some reason, the units seem different from Python by a factor of 1000.

@oskooi
Copy link
Collaborator

oskooi commented Apr 11, 2020

For some reason, I am not able to reproduce the results in this PR with the latest commit (get_GDSII_prism_data wrapper) for the function get-GDSII-prisms for either SWIG 3.0.12 or 4.0.1 both of which abort with an error.

SWIG 3.0.12

meep> (get-GDSII-prisms air "coupler.gds" 31 0 0)
fewer than 3 vertices in init_prism
meep> (get-GDSII-prism-data "coupler.gds" 31 0 0)
fewer than 3 vertices in init_prism

SWIG 4.0.1

meep> (get-GDSII-prisms air "coupler.gds" 31 0 0)
out of memory
meep> (get-GDSII-prism-data "coupler.gds" 31 0 0)
out of memory

@ChristopherHogan
Copy link
Contributor

@oskooi asked me to try running (get-GDSII-prism-data "coupler.gds" 31 0 0). I also get "out of memory". Guile 2.0.11, SWIG 3.0.12.

@stevengj
Copy link
Collaborator Author

I am using the HomerReid/libGDSII#11 branch, guile 2.2.6, and SWIG Version 3.0.12.

@oskooi
Copy link
Collaborator

oskooi commented Apr 19, 2020

On a clean Ubuntu 18.04 system, building Meep 1.14 from source with Guile 2.2.3, SWIG 3.0.12, and HomerReid/libGDSII#11, (get-GDSII-prism-data "coupler.gds" 31 0 0) produces a segmentation fault:

meep> (GDSII-layers "coupler.gds")
$1 = (0 1 2 3 4 5 31 32)
meep> (GDSII-vol "coupler.gds" 0 0 0)
$2 = #<collectable-swig-pointer meep::volume * 55b20ee8d470>
meep> (get-GDSII-prism-data "coupler.gds" 31 0 0)
[ip-172-31-85-191:31125] *** Process received signal ***
[ip-172-31-85-191:31125] Signal: Segmentation fault (11)
[ip-172-31-85-191:31125] Signal code: Address not mapped (1)
[ip-172-31-85-191:31125] Failing at address: 0xae
[ip-172-31-85-191:31125] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f802223df20]
[ip-172-31-85-191:31125] [ 1] /lib/x86_64-linux-gnu/libc.so.6(+0x18ece1)[0x7f802238dce1]
[ip-172-31-85-191:31125] [ 2] /home/ubuntu/install/meep/scheme/.libs/meep(make_prism+0xfa)[0x55b20c6e0d2a]
[ip-172-31-85-191:31125] [ 3] /home/ubuntu/install/meep/src/.libs/libmeep.so.19(_ZN9meep_geom16get_GDSII_prismsEPNS_13material_dataEPKcidd+0x202)[0x7f802356bca2]
[ip-172-31-85-191:31125] [ 4] /home/ubuntu/install/meep/scheme/.libs/meep(+0x19c3a)[0x55b20c644c3a]
[ip-172-31-85-191:31125] [ 5] /home/ubuntu/install/meep/scheme/.libs/meep(+0x2db6a)[0x55b20c658b6a]
[ip-172-31-85-191:31125] [ 6] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0xb130c)[0x7f8022fe030c]
[ip-172-31-85-191:31125] [ 7] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_call_n+0x177)[0x7f8022fea137]
[ip-172-31-85-191:31125] [ 8] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_primitive_eval+0x27)[0x7f8022f6d7e7]
[ip-172-31-85-191:31125] [ 9] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_eval+0x53)[0x7f8022f6d843]
[ip-172-31-85-191:31125] [10] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_shell+0x40)[0x7f8022fb8f30]
[ip-172-31-85-191:31125] [11] /home/ubuntu/install/meep/scheme/.libs/meep(+0x9dc19)[0x55b20c6c8c19]
[ip-172-31-85-191:31125] [12] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0x551ad)[0x7f8022f841ad]
[ip-172-31-85-191:31125] [13] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0x37a4a)[0x7f8022f66a4a]
[ip-172-31-85-191:31125] [14] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0xb88ff)[0x7f8022fe78ff]
[ip-172-31-85-191:31125] [15] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_call_n+0x177)[0x7f8022fea137]
[ip-172-31-85-191:31125] [16] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0xa9da2)[0x7f8022fd8da2]
[ip-172-31-85-191:31125] [17] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0x38030)[0x7f8022f67030]
[ip-172-31-85-191:31125] [18] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_c_with_continuation_barrier+0x35)[0x7f8022f670c5]
[ip-172-31-85-191:31125] [19] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(+0xa89ec)[0x7f8022fd79ec]
[ip-172-31-85-191:31125] [20] /usr/lib/x86_64-linux-gnu/libgc.so.1(GC_call_with_stack_base+0x22)[0x7f801f998c42]
[ip-172-31-85-191:31125] [21] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_with_guile+0x28)[0x7f8022fd7d08]
[ip-172-31-85-191:31125] [22] /usr/lib/x86_64-linux-gnu/libguile-2.2.so.1(scm_boot_guile+0x42)[0x7f8022f84342]
[ip-172-31-85-191:31125] [23] /home/ubuntu/install/meep/scheme/.libs/meep(+0x18e6f)[0x55b20c643e6f]
[ip-172-31-85-191:31125] [24] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f8022220b97]
[ip-172-31-85-191:31125] [25] /home/ubuntu/install/meep/scheme/.libs/meep(+0x18eba)[0x55b20c643eba]
[ip-172-31-85-191:31125] *** End of error message ***
Segmentation fault (core dumped)

Same result for (get-GDSII-prisms air "coupler.gds" 31 0 0).

$ meep --version
Using MPI version 3.1, 1 processes
Meep 1.14, Copyright (C) 2005-2020 Massachusetts Institute of Technology.
Using libctl 4.5.0 and Guile 2.2.3.

As a sanity check, PyMeep works fine:

>>> import meep as mp
Using MPI version 3.1, 1 processes
>>> print(mp.__version__)
1.14.0-3-g4e42ffd7
>>> mp.GDSII_layers("coupler.gds")
[0, 1, 2, 3, 4, 5, 31, 32]
>>> mp.GDSII_vol("coupler.gds", 0, 0, 0)
<meep.simulation.Volume object at 0x7f2ba57a16a0>
>>> mp.get_GDSII_prisms(mp.Medium(index=3.5), "coupler.gds", 31, 0, 0)
[<meep.geom.Prism object at 0x7f2b97d4a908>, <meep.geom.Prism object at 0x7f2b97e08358>, <meep.geom.Prism object at 0x7f2b97e087b8>]

@stevengj
Copy link
Collaborator Author

It's segfaulting in make_prism, which is a libctlgeom function. If you compile libctl with --enable-debug, then hopefully it will tell you the line of the problem.

@oskooi
Copy link
Collaborator

oskooi commented Apr 22, 2020

output from running (get-GDSII-prism-data "coupler.gds" 31 0 0) with compiling libctl with --enable-debug:

meep> (get-GDSII-prism-data "coupler.gds" 31 0 0)
fewer than 3 vertices in init_prism

This error is from libctl/utils/geom.c:2571.

@stevengj
Copy link
Collaborator Author

If you can reproduce the segfault with a debug build, that will be easier. Conversely, for the "fewer than 3 vertices error", you could try to step with the debugger to see what that is happening

@oskooi
Copy link
Collaborator

oskooi commented Apr 23, 2020

I have tracked down the cause of the segmentation fault but do not how to go about fixing it. Compiling with --enable-debug and using gdb to step through (get-GDSII-prism-data "coupler.gds" 31 0 0) reveals the problem to be this line:

memcpy(prsm->vertices.items, vertices, num_vertices * sizeof(vector3));

in the function make_slanted_prism_with_center from scheme/geom.cpp:2847.

This is shown in the backtrace:

0  __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:249
#1  0x0000555555649797 in make_slanted_prism_with_center (material=..., center=..., vertices=0xae, num_vertices=174, height=0, axis=..., sidewall_angle=0) at geom.cpp:2847
#2  0x000055555564957f in make_prism_with_center (material=..., center=..., vertices=0xae, num_vertices=174, height=0, axis=...) at geom.cpp:2826
#3  0x00005555556494eb in make_prism (material=..., vertices=0xae, num_vertices=174, height=0, axis=...) at geom.cpp:2820
#4  0x00007ffff7b94ffc in meep_geom::get_GDSII_prisms(meep_geom::material_data*, char const*, int, double, double) () from /usr/local/lib/libmeep.so.19
#5  0x000055555557804e in get_GDSII_prism_data (GDSIIFile=0x555555a42c80 "coupler.gds", Layer=31, zmin=0, zmax=0) at meep.cpp:231
#6  0x0000555555623bf6 in _wrap_get_GDSII_prism_data__SWIG_0 (argc=4, argv=0x7fffffffae10) at meep_wrap.cxx:70851
#7  0x000055555562408f in _wrap_get_GDSII_prism_data (rest=0x5555563ed730) at meep_wrap.cxx:71007
#8  0x00007ffff75ae9e3 in ?? () from /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22
#9  0x00007ffff75cebf1 in ?? () from /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22
#10 0x00007ffff74ff6cb in scm_primitive_eval () from /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22

Note that make_slanted_prism_with_center is actually copied from:

https://github.com/NanoComp/libctl/blob/master/utils/geom.c#L2838-L2853

The reason memcpy fails is due to the array const vector3 *vertices which is first created, initialized, and then passed in from the function get_GDSII_prisms:

https://github.com/NanoComp/meep/blob/master/src/GDSIIgeom.cpp#L147-L152

I verified that the vertices array is correctly initialized in get_GDSII_prisms (there are 174 vertices and the coordinates of the vertices are as expected). However, when vertices is passed from get_GDSII_prisms to make_prism and then finally to make_slanted_prism_with_center, the vertices array is somehow corrupted (i.e., individual elements can no longer be accessed) which causes memcpy to fail.

Finding out what is causing this array to be corrupted during the transfer may provide the fix.

@oskooi
Copy link
Collaborator

oskooi commented Apr 23, 2020

The same error related to the use of the corrupted vertices array in memcpy is reported by valgrind:

==9071== 1 errors in context 1 of 136:
==9071== Invalid read of size 2
==9071==    at 0x4C36750: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9071==    by 0x1FD796: make_slanted_prism_with_center (geom.cpp:2847)
==9071==    by 0x1FD57E: make_prism_with_center (geom.cpp:2826)
==9071==    by 0x1FD4EA: make_prism (geom.cpp:2820)
==9071==    by 0x4F4AFFB: meep_geom::get_GDSII_prisms(meep_geom::material_data*, char const*, int, double, double) (in /usr/local/lib/libmeep.so.19.0.0)
==9071==    by 0x12C04D: get_GDSII_prism_data(char const*, int, double, double) (meep.cpp:231)
==9071==    by 0x1D7BF5: _wrap_get_GDSII_prism_data__SWIG_0(int, scm_unused_struct**) (meep_wrap.cxx:70851)
==9071==    by 0x1D808E: _wrap_get_GDSII_prism_data(scm_unused_struct*) (meep_wrap.cxx:71007)
==9071==    by 0x54CB9E2: ??? (in /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22.8.1)
==9071==    by 0x54EBBF0: ??? (in /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22.8.1)
==9071==    by 0x541C6CA: scm_primitive_eval (in /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22.8.1)
==9071==    by 0x544E546: scm_primitive_load (in /usr/lib/x86_64-linux-gnu/libguile-2.0.so.22.8.1)
==9071==  Address 0xae is not stack'd, malloc'd or (recently) free'd

valgrind also reports lots of memory leaks:

==9071== LEAK SUMMARY:
==9071==    definitely lost: 2,601 bytes in 22 blocks
==9071==    indirectly lost: 391 bytes in 13 blocks
==9071==      possibly lost: 9,364 bytes in 33 blocks
==9071==    still reachable: 2,915,687 bytes in 11,714 blocks
==9071==         suppressed: 0 bytes in 0 blocks
==9071== Reachable blocks (those to which a pointer was found) are not shown.

Based on this information, is it possible that the different results reported for (get-GDSII-prism-data "coupler.gds" 31 0 0) are due to the different ways libctl/libGDSII/meep are compiled on OSX vs. Linux using llvm or gcc and the compiler's effect on memory management when a memory leak is present? Unfortunately, tracking down the exact memory leak that is causing the error in this case for Linux/gcc just from the valgrind output seems tricky (because there are so many leaks).

@stevengj
Copy link
Collaborator Author

You expect valgrind to report leaks from Scheme because it doesn't know about Scheme's garbage collection. (In any case, the worst that can happen with a leak is running out of memory, so it couldn't cause the problem here.)

@stevengj
Copy link
Collaborator Author

It looks like the problem is that it's calling the wrong geom.cpp. The Scheme code should be calling its own geom.cpp, but libmeep should be calling the routines in libctlgeom.

The right way to fix this is probably to ensure that the routines in geom.cpp are namespaced so that they don't conflict with those in libctlgeom. We are already doing that for ctl-io.cpp.

@stevengj
Copy link
Collaborator Author

This hasn't bitten us before because it's the first time we've tried to use meepgeom from the Scheme interface.

We'd have to set up libctl so that there is an option, e.g. set by a preprocessor #define, to put all of the geom.c routines in a separate namespace.

@oskooi oskooi mentioned this pull request Dec 30, 2021
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

Successfully merging this pull request may close these issues.

3 participants