From 05f2149ab6cbbba940e8d4601fb966820af7f80f Mon Sep 17 00:00:00 2001 From: Hideousmon Date: Tue, 21 May 2024 17:32:53 +0800 Subject: [PATCH] Version 0.5.4 --- docs/gettingstarted.md | 9 +++-- examples/simulation/waveguide.py | 4 +- history.md | 6 ++- splayout/__init__.py | 2 +- splayout/lumericalcommun/fdtdapi.py | 57 ++++++++++++++++++++--------- splayout/utils/utils.py | 20 +++++++++- 6 files changed, 72 insertions(+), 26 deletions(-) diff --git a/docs/gettingstarted.md b/docs/gettingstarted.md index 461cc85..06e5b09 100644 --- a/docs/gettingstarted.md +++ b/docs/gettingstarted.md @@ -37,16 +37,17 @@ Create a simulation for a waveguide. ```python from splayout import * +import matplotlib.pyplot as plt # initialize the simulation frame -fdtd = FDTDSimulation(fdtd_path="C:\\Program Files\\Lumerical\\v202\\api\\python") +fdtd = FDTDSimulation() # draw waveguides on Lumerical waveguide = Waveguide(start_point=Point(-3,0), end_point=Point(3,0), width=1, z_start=-0.11, z_end=0.11, material=Si) waveguide.draw_on_lumerical_CAD(fdtd) -# add simulation region, source, and monitor -fdtd.add_fdtd_region(bottom_left_corner_point=Point(-2, -1.5), top_right_corner_point=Point(2, 1.5), background_index=1.444,dimension=3, height=0.8) +# add simulation region, source, and monitor +fdtd.add_fdtd_region(bottom_left_corner_point=Point(-2, -1.5), top_right_corner_point=Point(2, 1.5), background_index=1.444,dimension=3, height=0.8, use_gpu=0) fdtd.add_mode_source(position=Point(-1.5,0), width=1.5, height=0.8, wavelength_start=1.54, wavelength_end=1.57, mode_number=1) @@ -335,7 +336,7 @@ rectangle.draw(cell,wg_layer) -## Functions for Self-define Components +## Functions for Customizing Components ### AEMD Grating diff --git a/examples/simulation/waveguide.py b/examples/simulation/waveguide.py index 2b6c080..211e446 100644 --- a/examples/simulation/waveguide.py +++ b/examples/simulation/waveguide.py @@ -8,14 +8,14 @@ if __name__ == '__main__': # initialize the simulation frame - fdtd = FDTDSimulation(fdtd_path="C:\\Program Files\\Lumerical\\v202\\api\\python") + fdtd = FDTDSimulation() # draw waveguides on Lumerical waveguide = Waveguide(start_point=Point(-3,0), end_point=Point(3,0), width=1, z_start=-0.11, z_end=0.11, material=Si) waveguide.draw_on_lumerical_CAD(fdtd) # add simulation region, source, and monitor - fdtd.add_fdtd_region(bottom_left_corner_point=Point(-2, -1.5), top_right_corner_point=Point(2, 1.5), background_index=1.444,dimension=3, height=0.8) + fdtd.add_fdtd_region(bottom_left_corner_point=Point(-2, -1.5), top_right_corner_point=Point(2, 1.5), background_index=1.444,dimension=3, height=0.8, use_gpu=0) fdtd.add_mode_source(position=Point(-1.5,0), width=1.5, height=0.8, wavelength_start=1.54, wavelength_end=1.57, mode_number=1) diff --git a/history.md b/history.md index d63e7c4..02c9474 100644 --- a/history.md +++ b/history.md @@ -221,4 +221,8 @@ * Update readme.md * Update api reference for pixelated regions. * New functions in FDTDSimulation. -* Add boolean operations example and docs. \ No newline at end of file +* Add boolean operations example and docs. + +### Version 0.5.4 (May 21, 2024) +* Automatically search for Ansys Lumerical Python API. +* Add use_gpu option for fdtd_region. \ No newline at end of file diff --git a/splayout/__init__.py b/splayout/__init__.py index 9204bea..89aab73 100644 --- a/splayout/__init__.py +++ b/splayout/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.5.3" +__version__ = "0.5.4" ## Submodules from . import utils diff --git a/splayout/lumericalcommun/fdtdapi.py b/splayout/lumericalcommun/fdtdapi.py index 393c43a..6731234 100644 --- a/splayout/lumericalcommun/fdtdapi.py +++ b/splayout/lumericalcommun/fdtdapi.py @@ -18,18 +18,35 @@ class FDTDSimulation: Path to the .fsp file that what want to be loaded (default: None). """ - def __init__(self,hide=0,fdtd_path = "C:\\Program Files\\Lumerical\\v202\\api\\python\\", load_file = None): - sys.path.append(fdtd_path) - sys.path.append(os.path.dirname(__file__)) - try: - os.add_dll_directory(fdtd_path) - except: - pass - try: - import lumapi - except: - raise Exception( - "Lumerical FDTD is not installed in the default path, please specify the python api path with fdtd_path=***.") + def __init__(self, hide=0, fdtd_path=None, load_file = None): + if not fdtd_path is None: + sys.path.append(fdtd_path) + sys.path.append(os.path.dirname(__file__)) + try: + os.add_dll_directory(fdtd_path) + except: + pass + try: + import lumapi + except: + raise Exception( + "Can not find Lumerical FDTD in fdtd_path.") + else: # auto find + fdtd_path = find_lumerical() + if not fdtd_path is None: + sys.path.append(fdtd_path) + sys.path.append(os.path.dirname(__file__)) + try: + os.add_dll_directory(fdtd_path) + except: + pass + try: + import lumapi + except: + raise Exception("Import lumapi error.") + else: # fail to find + raise Exception("Can not find Lumerical FDTD automatically, please set fdtd_path=*** in FDTDSimulation.") + self.lumapi = lumapi self.fdtd = self.lumapi.FDTD(hide=hide) if (type(load_file) != type(None)): @@ -487,7 +504,7 @@ def reset_source_phase(self, source_name, phase): def add_fdtd_region(self,bottom_left_corner_point,top_right_corner_point,simulation_time=5000, background_material = None, background_index=1.444,mesh_order =2,dimension=3,height = 1, z_min = None, - z_max = None, z_symmetric = 0, y_antisymmetric = 0, y_periodic = 0, pml_layers = 8): + z_max = None, z_symmetric = 0, y_antisymmetric = 0, y_periodic = 0, pml_layers = 8, use_gpu = 0): """ Add simulation region in Lumerical FDTD. @@ -519,7 +536,10 @@ def add_fdtd_region(self,bottom_left_corner_point,top_right_corner_point,simulat Whether set anti-symmetric in y-axis (default: 0). y_periodic : Bool or Int Whether set periodic in y-axis (default: 0). - + pml_layers : Int + Number of pml layers (default: 8). + use_gpu : Bool or Int + Whether to use gpu (default: 8). Notes ----- If z_min and z_max are specified, the height property will be invalid. @@ -554,16 +574,19 @@ def add_fdtd_region(self,bottom_left_corner_point,top_right_corner_point,simulat self.fdtd.eval("set(\"mesh accuracy\"," + str(mesh_order) + ");") self.fdtd.eval("set(\"pml layers\"," +str(pml_layers) +");") - if (dimension == 3 and z_symmetric == 1): + if dimension == 3 and z_symmetric == 1: self.fdtd.eval("set(\"z min bc\", \"Symmetric\");") - if (y_antisymmetric == 1): + if y_antisymmetric == 1: self.fdtd.eval("set(\"y min bc\", \"Anti-Symmetric\");") self.fdtd.eval("set(\"force symmetric y mesh\", 1);") - if (y_periodic == 1): + if y_periodic == 1: self.fdtd.eval("set(\"y min bc\", \"Periodic\");") + if use_gpu == 1: + self.fdtd.eval("set(\"express mode\", 1);") + def add_index_region(self, bottom_left_corner_point, top_right_corner_point, height = 1, z_min = None, z_max = None, index_monitor_name="index",dimension = 2): """ Add index monitor (x-y plane) in Lumerical FDTD. diff --git a/splayout/utils/utils.py b/splayout/utils/utils.py index 41d4474..9cd93af 100644 --- a/splayout/utils/utils.py +++ b/splayout/utils/utils.py @@ -1,5 +1,6 @@ import gdspy import math +import os ## "macros" RIGHT = 0 @@ -405,4 +406,21 @@ def make_gdsii_file(filename,cover_source_layer=None,cover_target_layer=None,inv if (filename[-4:] != ".gds"): filename += ".gds" - lib.write_gds(filename) \ No newline at end of file + lib.write_gds(filename) + +def find_lumerical(): + preset_disks = ["C:/", "D:/", "E:/", "F:/", "G:/", "H:/", "I:/", "J:/", + "K:/", "L:/", "M:/", "N:/", "O:/", "P:/", "Q:/", "R:/", + "S:/", "T:/", "U:/", "V:", "W:/", "X:/", "Y:/", "Z:/"] + preset_dir = ["Program Files/Lumerical/", "Lumerical/"] + version_numbers = [str(num) for num in range(202, 250)] + preset_version = [ "v" + version_number + "/api/python" for version_number in version_numbers] + + lumerical_api_path = None + for disk in preset_disks: + for pdir in preset_dir: + for pver in preset_version: + if os.path.exists(disk + pdir + pver): + lumerical_api_path = disk + pdir + pver + + return lumerical_api_path \ No newline at end of file