We have an exciting announcement about badges coming in May 2025. Until then, we will temporarily stop issuing new badges for course completions and certifications. However, all completions will be recorded and fulfilled after May 2025.
Photonics

Photonics

Topics related to Lumerical and more.

Lumerical Python API Topology Optimization Error 2025 R1

    • gangin2
      Subscriber

      I have python code that does topology optimization inverse design and it works for an older version of lumerical (2023 R2.3) on our computer, but it fails when I use it for the latest version, 2025 R1.  The error is below:

      CONFIGURATION FILE {'root': 'C:\\Program Files\\ANSYS Inc\\v251\\Lumerical\\api\\python', 'lumapi': 'C:\\Program Files\\ANSYS Inc\\v251\\Lumerical\\api\\python'}
      Initializing super optimization
      Checking for one forward simulation :   One forward simulation is in progress
      One forward simulation is in progress
      One forward simulation is in progress
      Wavelength range of source object will be superseded by the global settings.
      Traceback (most recent call last):
        File "C:\Users\LAB1\Documents\Inverse Design\cross_split\cross_split_opt.py", line 93, in
          runSim(initial_cond, eps_min, eps_max, x_pos, y_pos, filter_R*1e-9, working_dir)
        File "C:\Users\LAB1\Documents\Nick\Inverse Design\cross_split\cross_split_opt.py", line 64, in runSim
          opt.run(working_dir = working_dir)
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimization.py", line 471, in run
          self.initialize(working_dir=working_dir)
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimization.py", line 361, in initialize
          self.optimizer.initialize(start_params=start_params,
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimizers\optimizer.py", line 100, in initialize
          self.reset_start_params(start_params, self.scale_initial_gradient_to)
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimizers\optimizer.py", line 108, in reset_start_params
          self.auto_detect_scaling(scale_initial_gradient_to)
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimizers\optimizer.py", line 115, in auto_detect_scaling
          gradients = self.callable_jac(params)
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimizers\minimizer.py", line 34, in callable_jac_local
          fom_gradients = callable_jac(params_over_scaling_factor) / self.scaling_factor
        File "C:\Program Files\ANSYS Inc\v251\Lumerical\api\python\lumopt\optimization.py", line 223, in callable_jac
          canMakeAdjointSim = canMakeAdjointSim & opt.fom.can_make_adjoint_sim(opt.sim)
      AttributeError: 'Optimization' object has no attribute 'sim'
      >>>

       

      Any guidance on what changes are necessary to get it to run on the latest version would be appreciated.

    • Dev
      Ansys Employee

      Hello,

      Sorry for the delayed response.

      Could you please let me know if the Python code is from a Lumerical Application Gallery example or if you wrote it yourself?

      Seems like the the Optimization object no longer possesses the sim attribute .

      A solution can be modify your code to align with the new API structure, including setting attributes like sim manually.

       

    • gangin2
      Subscriber

      The pyhton code is written myself.  I based it off of https://optics.ansys.com/hc/en-us/articles/1500007188582-Topology-Optimization-of-a-4-channel-wavelength-demultiplexer-2D-TE.  But I can successfully run the example without issue.

      I think my code is very similar to the example and I am not sure where I am using the sim attribute.  Can you help me identify where I am doing this?  Here is my optimization script:

      ######## IMPORTS ########
      # General purpose imports
      import numpy as np
      import os
      import sys
      import scipy as sp
       
      # Optimization specific imports
      from lumopt import CONFIG
      from lumopt.geometries.topology import TopologyOptimization2D, TopologyOptimization3DLayered
      from lumopt.utilities.load_lumerical_scripts import load_from_lsf
      from lumopt.figures_of_merit.modematch import ModeMatch
      from lumopt.optimization import Optimization
      from lumopt.optimizers.generic_optimizers import ScipyOptimizers
      from lumopt.utilities.wavelengths import Wavelengths
       
      sys.path.append(os.path.dirname(__file__))
      from cross_split_base import cross_split_base_init_
      cross_split_base = cross_split_base_init_
       
      cur_path = os.path.dirname(os.path.abspath(__file__))
       
      ######## RUNS TOPOLOGY OPTIMIZATION OF A 2D STRUCTURE ########
      def runSim(params, eps_min, eps_max, x_pos, y_pos, filter_R, working_dir):
       
          ######## DEFINE A 2D TOPOLOGY OPTIMIZATION REGION ########
          geometry = TopologyOptimization2D(params=params, eps_min=eps_min, eps_max=eps_max, x=x_pos, y=y_pos, z=0, filter_R=filter_R, min_feature_size=filter_R)
          
          ######## DEFINE FIGURE OF MERIT ########
          # The base simulation script defines a field monitor named 'fom' at the point where we want to modematch to the fundamental TE mode
          fom0 = ModeMatch(monitor_name = 'fom 0',
                          mode_number = 'fundamental TE mode',
                          direction = 'Backward',
                          target_T_fwd = lambda wl: 0.25*np.ones(wl.size),
                          norm_p = 1)    
          fom1 = ModeMatch(monitor_name = 'fom 1',
                          mode_number = 'fundamental TE mode',
                          direction = 'Forward',
                          target_T_fwd = lambda wl: 0.25*np.ones(wl.size),
                          norm_p = 1) 
          fom2 = ModeMatch(monitor_name = 'fom 2',
                          mode_number = 'fundamental TE mode',
                          direction = 'Forward',
                          target_T_fwd = lambda wl: 0.25*np.ones(wl.size),
                          norm_p = 1) 
          fom3 = ModeMatch(monitor_name = 'fom 3',
                          mode_number = 'fundamental TE mode',
                          direction = 'Backward',
                          target_T_fwd = lambda wl: 0.25*np.ones(wl.size),
                          norm_p = 1) 
          ######## DEFINE OPTIMIZATION ALGORITHM ########
          optimizer = ScipyOptimizers(max_iter=50, method='L-BFGS-B', scaling_factor=1, pgtol=1e-6, ftol=1e-5, scale_initial_gradient_to=0.25)
       
          ######## SETTING UP THE OPTIMIZER ########
          wavelengths = Wavelengths(start = 1545e-9, stop = 1555e-9, points = 11)
          opt0 = Optimization(base_script=cross_split_base, wavelengths = wavelengths, fom=fom0, geometry=geometry, optimizer=optimizer, use_deps=False, use_var_fdtd = False, hide_fdtd_cad=False, plot_history=True, store_all_simulations=False, save_global_index=True)
          opt1 = Optimization(base_script=cross_split_base, wavelengths = wavelengths, fom=fom1, geometry=geometry, optimizer=optimizer, use_deps=False, use_var_fdtd = False, hide_fdtd_cad=False, plot_history=True, store_all_simulations=False, save_global_index=False)
          opt2 = Optimization(base_script=cross_split_base, wavelengths = wavelengths, fom=fom2, geometry=geometry, optimizer=optimizer, use_deps=False, use_var_fdtd = False, hide_fdtd_cad=False, plot_history=True, store_all_simulations=False, save_global_index=False)
          opt3 = Optimization(base_script=cross_split_base, wavelengths = wavelengths, fom=fom3, geometry=geometry, optimizer=optimizer, use_deps=False, use_var_fdtd = False, hide_fdtd_cad=False, plot_history=True, store_all_simulations=False, save_global_index=False)
          #opt.continuation_max_iter = 20 #< How many iterations per binarization step (default is 20)
       
          ######## RUN THE OPTIMIZER ########
          opt = opt0+opt1+opt2+opt3
          opt.run(working_dir = working_dir)
       
      if __name__ == '__main__':
          size_x = 5000 #< Length of the device (in nm). Longer devices typically lead to better performance
          delta_x = 20 #< Size of a pixel along x-axis (in nm)
       
          size_y = 5000 #< Since we use symmetry, this is only have the extent along the y-axis (in nm)
          delta_y = 20 #< Size of a pixel along y-axis (in nm)
       
          filter_R = 100 #< Radius of the smoothing filter which removes small features and sharp corners (in nm)
          
          eps_max = 2.848152**2 #< Effective permittivity for a Silicon waveguide with a thickness of 220nm
          eps_min = 1.44**2 #< Permittivity of the SiO2 cladding
       
          x_points=int(size_x/delta_x)+1
          y_points=int(size_y/delta_y)+1
       
          x_pos = np.linspace(-size_x/2,size_x/2,x_points)*1e-9
          y_pos = np.linspace(-size_y/2,size_y/2,y_points)*1e-9
       
          ## Set initial conditions
          initial_cond = 0.5*np.ones((x_points,y_points))   #< Start with the domain filled with (eps_max+eps_min)/2
       
          ## Alternative initial conditions
          #initial_cond = None                               #< Use the structure 'initial_guess' as defined in the project file
          #initial_cond = np.ones((x_points,y_points))       #< Start with the domain filled with eps_max
          #initial_cond = np.zeros((x_points,y_points))      #< Start with the domain filled with eps_min         
          
          working_dir = os.path.join(cur_path, 'cross_split_x{:04d}_y{:04d}_f{:04d}'.format(size_x,size_y,int(filter_R)))
          runSim(initial_cond, eps_min, eps_max, x_pos, y_pos, filter_R*1e-9, working_dir)

       

    • Dev
      Ansys Employee

      Since the Lumerical example running without error, I think the Lumerical API is properly installed. 

      1. Try running the script: 

      import lumapi

      fdtd = lumapi.FDTD()

      fdtd.close()

      If this fails, ensure:Lumerical API is correctly installed. Python API overview – Ansys Optics

      2. Confirm that the 'sim' attribute is properly defined and initialized in your script before it's used within the 'Optimization' object.

      3. The function cross_split_base_init_() is defined correctly. The error might be due to the base_script=cross_split_base not being initialized properly. Add a print statement to check: print("Base script content:", cross_split_base)

      Thanks 

Viewing 3 reply threads
  • You must be logged in to reply to this topic.