Lumerical Python API: Issue with getVar()

I’m having an issue with the lumapi.getVar functionality. I’m trying to extract some variables from the project’s workspace but I’m receiving the following error

'Failed to get variable'
  C:\...\lumapi.py:144

Which is an exception raised when:

ec = iapi.appGetVar(handle, varname.encode(), byref(value))
if ec < 0:
    raise LumApiError("Failed to get variable")

Can anyone please point me on to why I’m having this issue when I’m extracting a simple variable from MODE? Also, what does the ec variable in the API stand for here?

I have also noticed an issue with Lumerical applications crashing when Python API getVar calls non-existent variable. Can there be an except raised in the API instead of crashing the application?

Cheers,
Mustafa

Below is a simple script that demonstrates the issue:

#%% find lumapi 
import sys, os

paath = 'C:/Program Files/Lumerical/INTERCONNECT/api/python'

if os.path.exists(os.path.join(paath,'lumapi.py')):
    print('Found lumapi path at' + ': ' +paath)
else:
    print('lumapi path does not exist, edit paath variable')
    
os.chdir(paath)
import lumapi
#%% run FDTD, initiate variable, and read it back

fdtd = lumapi.open('fdtd')

# plug in variable into workspace
inputValue = '1' 
lumapi.evalScript(fdtd, "a = '%s';" % inputValue)

# read back the variable
outputValue = lumapi.getVar(fdtd, "a")
print('FDTD: input value is: ' + inputValue + '       read value is: ' + outputValue)

#%% run MODE, initiate variable, and read it back

mode = lumapi.open('mode')

# plug in variable into workspace
inputValue = '1' 
lumapi.evalScript(mode, "a = '%s';" % inputValue)

# read back the variable
outputValue = lumapi.getVar(mode, "a")
print('MODE: input value is: ' + inputValue + '       read value is: ' + outputValue)

Hi @mustafa.sh

Thanks for the script. I ran it with INTERCONNECT, FDTD, and MODE python path and they all worked just fine with no error:

It is possible that you are using different versions of the products. Can you please check and make sure that they all belong to the same release?
https://www.lumerical.com/downloads/customer.html

You can also try using MODE python path to see if you get the same error again.

I need to discuss it with our developers if you were interested to know further. Please let me know.

I tried with a modified code to retrieve a non existent “b” value:

mode = lumapi.open(‘mode’)

# plug in variable into workspace
inputValue = '1' 
lumapi.evalScript(mode, "a = '%s';" % inputValue)

# read back the variable
outputValue = lumapi.getVar(mode, "b")
print('MODE: input value is: ' + inputValue + '       read value is: ' + outputValue)

This returned a LumApiError of ‘Failed to get variable’, with no crash. Is this what you were referring to?

Please keep me updated with your findings.
Thanks

Thanks @bkhanaliloo, you are correct, I ran the script on a different machine and seems to work just fine. I have the latest versions installed, but I’ll reinstall and debug further.

Cheers,
Mustafa

1 Like

Hi @mustafa.sh

Thanks for updating us. Please let me know if you still had any other issues.

The following commands yield a LumApiError and crash for me, with the latest version of FDTD for Mac OS:

fdtd = lumapi.open(‘fdtd’)
outputValue = lumapi.getVar(fdtd, “b”)
print('FDTD: read value is: ’ + outputValue)

Is there a way to avoid FDTD crashing under these circumstances, as suggested above?

Hi @bkhanaliloo, should I open a new ticket for this? If so, let me know.

Hi @william.whitney

I checked this in Mac with python 2.7 and latest version of FDTD, and it was working fine. When the parameter “b” was defined in FDTD, it retrieved it otherwise raised an error that the parameter does not exist.

The crash in unlikely. Can you please provide more details about your FDTD and python version? We support both 2 and 3. Any more details is highly appreciated to resolve the issue. Can you also try your code in windows and see if you can reproduce the crash?

Thanks