Photonics

Photonics

Lumerical Interconnect Python Co-simulation

    • lipee36
      Subscriber

      I have a question about co-simulating python with Interconnect. I've ran all the examples successfully, and I was able to reproduce simple scenarios based on them.

      My question is regarding the capabilities of the co-simulation itself. What I am looking to do is to create a circuit on interconnect and execute it on a loop, using dynamic inputs generated by my python script. A very dummy example of this functionality:

    • Guanhui Wang
      Ansys Employee
      Hi lipee36
      Thank you for the post. I believe what you have described is possible using the Python API. Please check out this example here: https://support.lumerical.com/hc/en-us/articles/360034936773-Python-co-simulation-with-INTERCONNECT
    • lipee36
      Subscriber

      thank you very much for your suggestion. Indeed, I've tried to replicate this example. If I run the example, it returns an error (something like "co_simulation_example.icp could not be read" - although I can open the project and executes it from the interface, or using the .lsf script.
      Anyhow, I've tried to adapt it to a very simple example, and yet I have errors:
      in lumapi.evalScript(h,'''File "F:\Programs\Lumerical\api\python\lumapi.py", line 251, in evalScriptraise LumApiError("Failed to evaluate code")lumapi.LumApiError: 'Failed to evaluate code'
      This is the simple circuit I am trying to use as example:
      and this is the adapted script:
      import sys, os, random, pdb
      import numpy as np
      os.add_dll_directory('F:\\Programs\\Lumerical\\api\\python')

      sys.path.append("F:\\Programs\\Lumerical\\api\\python")
      CWD = os.path.dirname(os.path.abspath(__file__)) ## No need to run this line if using python console
      os.chdir("F:\\Programs\\Lumerical\\api\\python")

      import lumapi
      h = lumapi.open("interconnect", hide=True)

      #####################################################
      # Define user parameters
      #
      time_window = 5e-9
      Nsamples = 8192
      power_in_1 = 100
      power_in_2 = 0
      frequency_in = 1550
      phase_in = 0
      # end user parameters
      #####################################################

      # we must load the file after starting INTERCONNECT
      lumapi.evalScript(h,''' load("MZI.icp"); ''');

      # intialize simply by pushing data to Lumerical script workspace
      lumapi.putDouble(h,"time_window",time_window);
      lumapi.putDouble(h,"Nsamples",Nsamples);
      lumapi.putDouble(h,"power_in_1",power_in_1);
      lumapi.putDouble(h,"power_in_2",power_in_2);
      lumapi.putDouble(h,"frequency_in",frequency_in);
      lumapi.putDouble(h,"phase_in",phase_in);
      lumapi.evalScript(h,'''
      # turn off history and redrawing
      redrawoff;
      historyoff;

      # time vectors
      t = linspace(0,time_window,Nsamples+1);
      t = t(1:end-1);
      dt = t(2)-t(1);

      # restore design mode in case we are in analysis mode
      switchtodesign;

      setnamed("::Root Element","time window",time_window);
      setnamed("::Root Element","number of samples",Nsamples);

      setnamed("CWL_1","power",power_in_1);
      setnamed("CWL_1","frequency",frequency_in);

      setnamed("CWL_2","power",power_in_2);
      setnamed("CWL_2","frequency",frequency_in);

      setnamed("MZI_1","phase_shift",phase_in);

      # intialize the simulation
      runinitialize;

      # initialize struct used for passing co-sim data
      v = struct;

      # End initialization
      #####################################################

      ''')

      output = np.empty((314,2))

      counter = 0
      while [phase_in < 3.15]:
      lumapi.evalScript(h,'''

      #set parameters
      setvalue("CWL_1","power",power_in_1);
      setvalue("CWL_1","frequency",frequency_in);
      setvalue("CWL_2","power",power_in_2);
      setvalue("CWL_2","frequency",frequency_in);

      setvalue("MZI_1","phase_shift",phase_in);

      # run INTERCONNECT step
      runstep;

      # read signals from INTERCONNECT simulation
      v.index = 1;
      v1 = getvalue("COSIM_1","port",v);
      v.index = 2;
      v2 = getvalue("COSIM_1","port",v);
      ''');
      output[counter][0] = lumapi.getVar(h,"v1")
      output[counter][1] = lumapi.getVar(h,"v2")

      phase_in = phase_in + 0.01

      # run finalize of INTERCONNECT
      lumapi.evalScript(h,"runfinalize;")

      print(output)
      lumapi.close(h)

      The error I've mentioned before is due the line I've highlighted bold . Can you see any obvious mistake/problem?
      Thanks again!


    • liqiande
      Subscriber
      õ¢áÕÑ¢´╝îµêæþø«Õë쵡úÕ£¿ÕüÜÞ┐ÖþºìÕÀÑõ¢£´╝îµêæÞâ¢Úù«õ¢áÕÉù´╝ƒ
Viewing 3 reply threads
  • You must be logged in to reply to this topic.