Passing Data - Python API
来自 https://support.lumerical.com/hc/en-us/articles/360041401434-Passing-Data-Python-API
从 Python API 驱动 Lumerical 的工具时,环境之间建立了连接,但它们不共享工作区。 相反,当变量来回传递时,会根据 getv() 和 putv() 中定义的类型转换创建一个精确的副本。 截至 2020a R4 的典型传输速度约为 300 MB,并将传输数据所需的内存开销减少到 3 倍。 这不会在保真度方面出现问题,也不会在速度方面出现瓶颈; 然而,当处理非常大的数据集时,如果效率是必要的,考虑到这一点可能很重要。
转换数据类型的快速参考指南。
一言以概之,getdata()可以返回参数以及单个的场数据,如Ex;
getresult()直接可以返回数据集
一、原始数据
with lumapi.FDTD() as fdtd:
fdtd.addfdtd(dimension="2D", x=0.0e-9, y=0.0e-9, x_span=3.0e-6, y_span=1.0e-6)
fdtd.addgaussian(name = 'source', x=0., y=-0.4e-6, injection_axis="y", waist_radius_w0=0.2e-6, wavelength_start=0.5e-6, wavelength_stop=0.6e-6)
fdtd.addring( x=0.0e-9, y=0.0e-9, z=0.0e-9, inner_radius=0.1e-6, outer_radius=0.2e-6, index=2.0)
fdtd.addmesh(dx=10.0e-9, dy=10.0e-9, x=0., y=0., x_span=0.4e-6, y_span=0.4e-6)
fdtd.addtime(name="time", x=0.0e-9, y=0.0e-9)
fdtd.addprofile(name="profile", x=0., x_span=3.0e-6, y=0.)
# Dict ordering is not guaranteed, so if there properties dependant on other properties an ordered dict is necessary
# In this case 'override global monitor settings' must be true before 'frequency points' can be set
# 不保证字典排序,所以如果有依赖于其他属性的属性,则需要有序字典
# 在这种情况下,“覆盖全局监视器设置”必须为真,然后才能设置“频点”
props = OrderedDict([("name", "power"),
("override global monitor settings", True),
("x", 0.),("y", 0.4e-6),("monitor type", "linear x"),
("frequency points", 10.0)])
fdtd.addpower(properties=props)
fdtd.save("fdtd_file.fsp")
fdtd.run()
#Return raw E field data
Ex = fdtd.getdata("profile","Ex")
f = fdtd.getdata("profile","f")
x = fdtd.getdata("profile","x")
y = fdtd.getdata("profile","y")
print('Frequency field profile data Ex is type', type(Ex),' with shape', str(Ex.shape ))
print('Frequency field profile data f is type', type(f), 'with shape', str(f.shape ))
print('Frequency field profile data x is type', type(x), 'with shape', str(x.shape ))
print('Frequency field profile data y is type', type(y), 'with shape', str(y.shape )
返回
Frequency field profile result Ex is type <class 'numpy.ndarray'> with shape (101, 61, 1, 5)
Frequency field profile result f is type <class 'numpy.ndarray'> with shape (5, 1)
Frequency field profile result x is type <class 'numpy.ndarray'> with shape (101, 1)
Frequency field profile result y is type <class 'numpy.ndarray'> with shape (61, 1)
这里f大小为5是因为获取的是profile的数据,默认频点数确实为5
以上程序运行了FDTD Solutions依然闪退
二、数据集
with lumapi.FDTD('fdtd_file.fsp') as fdtd: # 打开名为fdtd_file的文件
#Return 2 different types of rectilinear datasets #返回2种不同类型的直线数据集
T, time = fdtd.getresult("power", "T"), fdtd.getresult("time","E")
#Create an unstructured dataset 创建一个非结构化数据集
fdtd.eval('x = [0;1;2];y = [0;sqrt(3);0];z = [0;0;0];C = [1,3,2];ds = unstructureddataset(x,y,z,C);')
ds = fdtd.getv('ds')
print('Transmission result T is type', type(T),' with keys', str(T.keys()) )
print('Time monitor result E is type', type(time),' with keys', str(time.keys()) )
print('Unstructured dataset is type', type(ds),' with keys', str(ds.keys()) )
返回
Transmission result T is type <class 'dict'> with keys dict_keys(['lambda', 'f', 'T', 'Lumerical_dataset'])
Time monitor result E is type <class 'dict'> with keys dict_keys(['t', 'x', 'y', 'z', 'E', 'Lumerical_dataset'])
Unstructured dataset is type <class 'dict'> with keys dict_keys(['x', 'y', 'z', 'connectivity', 'Lumerical_dataset'])
以上中的C矩阵是非结构化数据集的连接矩阵,所以返回键名为connectivity