TAGGED: 3DFDTD, lumerical, topology-optimization
-
-
February 13, 2025 at 4:30 am
gangin2
SubscriberI 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.
-
February 21, 2025 at 1:29 am
Dev
Ansys EmployeeHello,
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 thesim
attribute .A solution can be modify your code to align with the new API structure, including setting attributes likeÂ
sim
manually.Â
-
February 21, 2025 at 4:29 pm
gangin2
SubscriberThe 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 importsimport numpy as npimport osimport sysimport scipy as spÂ# Optimization specific importsfrom lumopt import CONFIGfrom lumopt.geometries.topology import TopologyOptimization2D, TopologyOptimization3DLayeredfrom lumopt.utilities.load_lumerical_scripts import load_from_lsffrom lumopt.figures_of_merit.modematch import ModeMatchfrom lumopt.optimization import Optimizationfrom lumopt.optimizers.generic_optimizers import ScipyOptimizersfrom 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)Â
-
February 27, 2025 at 4:05 pm
Dev
Ansys EmployeeSince the Lumerical example running without error, I think the Lumerical API is properly installed.Â
- 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 thebase_script=cross_split_base
not being initialized properly. Add a print statement to check: print("Base script content:", cross_split_base)ThanksÂ
-
- You must be logged in to reply to this topic.
- Difference between answers in version 2024 and 2017 lumerical mode solution
- Import material .txt file with script
- Errors Running Ring Modulator Example on Cluster
- Absorption cross-section of AuNR excited by prism-based TIR
- Issues with getting result from interconnent analysis script
- How to measure transmission coefficients on a given plane .
- TE Polarization Fraction + Waveguide TE/TM Fraction
- Edge Coupler EME Example Issue
- Lumerical Python API Topology Optimization Error 2025 R1
- FDE : Simulation waveguides with electrodes
-
2122
-
918
-
599
-
591
-
483
© 2025 Copyright ANSYS, Inc. All rights reserved.