Topology Optimization Python API Crashing at “Calculating gradients”

    • rchap


      I am trying to use the Topology optimization to design an MMI. I generate the .fsp in Python, then use Python to run the optimization.

      My code for running the optimization is very similar to the example:

      I am limited in character number - so can't paste the code here!

      But I continue to get this error message:

      "C:Program FilesLumericalv211python-3.6.8-embed-amd64python.exe" C:/Users/chapman/Desktop/Lumerical/Inverse_Design/MMI_Switch/Test1/
      CONFIGURATION FILE {'root': 'C:\Program Files\Lumerical\v211\api\python', 'lumapi': 'C:\Program Files\Lumerical\v211\api\python'}
      Wavelength range of source object will be superseded by the global settings.
      Making forward solve
      Making adjoint solve
      Runing solves
      Processing forward solve
      FOM = 0.9354406732153691 (1 - 0.06455932678463094)
      Processing adjoint solve
      Calculating gradients
      Traceback (most recent call last):
        File "C:/Users/chapman/Desktop/Lumerical/Inverse_Design/MMI_Switch/Test1/", line 495, in 
          runSim(initial_cond, eps_min, eps_max, x_pos, y_pos, filter_R)
        File "C:/Users/chapman/Desktop/Lumerical/Inverse_Design/MMI_Switch/Test1/", line 492, in runSim

        File "C:Program", line 578, in run
        File "C:Program", line 683, in initialize
          self.optimizer.initialize(start_params = start_params, callable_fom = callable_fom, callable_jac = callable_jac, bounds = bounds, plotting_function = plotting_function_fwd)
        File "C:Program", line 100, in initialize
          self.reset_start_params(start_params, self.scale_initial_gradient_to)
        File "C:Program", line 108, in reset_start_params
        File "C:Program", line 115, in auto_detect_scaling
          gradients = self.callable_jac(params)
        File "C:Program", line 34, in callable_jac_local
          fom_gradients = callable_jac(params_over_scaling_factor) / self.scaling_factor
        File "C:Program", line 879, in callable_jac
          grad = self.calculate_gradients()
        File "C:Program", line 910, in calculate_gradients
          grad_name = self.geometry.calculate_gradients_on_cad(self.sim, 'forward_fields', 'adjoint_fields', self.scaling_factor)               
        File "C:Program FilesLumericalv211apipythonlumoptgeometries", line 298, in calculate_gradients_on_cad
          'dF_dp = topoparamstogradient(params,topo_rho,dF_dEps);').format(self.eps_min,self.eps_max,self.filter_R,self.beta,self.eta,self.dx,self.dy) )
        File "C:Program", line 1188, in eval
          evalScript(self.handle, code)
        File "C:Program", line 251, in evalScript
          raise LumApiError("Failed to evaluate code")
      lumapi.LumApiError: 'Failed to evaluate code'
      Process finished with exit code 1

      I step through the code and it crashes at the evalScript function:

      def evalScript(handle, code):
          ec = iapi.appEvalScript(handle, code.encode())
          if ec < 0:
              raise LumApiError("Failed to evaluate code")

      Are there specific parameters I am missing?


    • Taylor Robertson
      Ansys Employee
      Hello rchap
      Most likely the problem is related to a size mismatch between the x, y , z positions passed to the topology object, and the FDTD grid.
      Ensure that the mesh override agrees with the x, y, z points that you have passed. Usually the mesh override should accomplish this, unless the mesh accuracy set in the FDTD object results in a mesh that is smaller then what is specified in the mesh override object. Maybe set a uniform in mesh in FDTD or reduce the mesh accuracy so that the mesh override does what it is supposed to. You can check the FDTD grid values in the simulation. Compare this with the points you have defined.

    • rchap
      Hello troberts
      Thanks for the help.
      Unfortunately I am still getting the same errors.

      I have attached here a .rar with the base .fsp file that I use, the Python script I run, and the initial Forward and Adjoint simulations. (I can't attach the .fsp directly!)
      I have checked throughout and am using a 40nm mesh.

      Interestingly, if I include an "initial_guess", and have inital_cond=None, then I get a different error about the bounds of the parameters...

    • rchap
      Hi troberts
      So it looks like the size of the MMI should be a multiple of the grid spacing. I had a 5um square MMi with a 40nm grid - so I guess that is why it did not work.
      It seems to be working now - and can optimise the design. More testing of parameters is necessary.

      Do you know why the maximum number of iterations is not observed? It seems to run indefinitely despite the fixed max iterations.
    • Taylor Robertson
      Ansys Employee
      Glad to hear that worked. If you refer to the Optimizable geometry page
      max_terations is for greyscale phase it still needs to proceed through binarization and optionally design for manufacturing.
Viewing 4 reply threads
  • You must be logged in to reply to this topic.