# Script Objects
USER GUIDE for Python (opens new window)
Script Objects is a powerful tool to give GUI users the same flexibility shown by the python interface to create complex structures, set up sources, and define monitors.
script name: Name of the script object. All the simulation objects returned by the script will be prefixed with the script name.
editor: Embedded Python console where users can edit and run a function to create structures, sources, and monitor objects.
An initial code example is provided and should be changed to match the user's needs. Bellow we have an explanation of the main code sections:
# Library imports
Here the user should include all the necessary libraries.
import tidy3d as td
import numpy as np
from tidy3d_lambda import entrypoint
# Function definition
The @entrypoint decorator “tells” the Python interpreter that the function generate_object(param) must be executed when running the script. Users should not change this.
@entrypoint
def generate_object(param):
The parameter param contains a reference to all the user-defined Variables on the Parameter tab, so it is possible to get their values within the function scope. For instance, if the user has defined a variable a = 0.1587, the code t = f"The value of a is {param.a:.2f}" will attribute the string “The value of a is 0.15” to the variable t.
# Function body
This code defines the variables lambda0, freq0, and fwidth, and defines the medium dielectric to create the structure square. Finally, it creates the source and monitor objects.
# can write formula
lambda0 = 1.0
freq0 = td.C_0 / lambda0
# create structure like in tidy3d python client
dielectric = td.Medium.from_nk(n=2, k=0, freq=freq0)
square = td.Structure(
geometry=td.Box(center=(0, 0, 0), size=(1.5, 1.5, 1.5)), medium=dielectric
)
lambda0 = 1.0
freq0 = td.C_0 / lambda0
fwidth = freq0 / 10.0
source = td.UniformCurrentSource(
center=(-1.5, 0, 0),
size=(0, 0.4, 0.4),
source_time=td.GaussianPulse(freq0=freq0, fwidth=fwidth),
polarization="Ey",
)
monitor = td.FieldMonitor(
fields=["Ex", "Ey", "Hz"],
center=(0, 0, 0),
size=(td.inf, td.inf, 0),
freqs=[freq0],
name="fields_on_plane",
)
# Function return
In the last part of the code, a Python List containing the created structure, source, and monitor is returned to the GUI engine.
return [square, source, monitor]
# Example
Below we have another example of using the Script Object to create a waveguide Structure, a ModeSource, and a ModeMonitor. One can follow the script comments to have all the details.
import tidy3d as td
import numpy as np
from tidy3d_lambda import entrypoint
# Instructions:
# Scripts are used to create Structures, Sources, and Monitors.
# Define your custom Structures, Sources, and Monitors inside the
# generate_object function and return them in the end.
# After running the script, the returned objects will be
# added to your simulation.
# Note: see "https://docs.flexcompute.com/projects/tidy3d/en/latest/index.html"
# for the python interface documentation.
@entrypoint
def generate_object(param):
# The input argument "param" carry everything defined
# under the Parameter tab. For example, the Variable
# called "lambda0" can be used in the script as "param.lambda0".
# To access a user-defined medium, for instance, a material called
# as "Si" in the Medium section, the syntax param.medium.Si must
# be used.
# Central frequency and bandwidth of the simulation.
freq0 = td.C_0 / param.lambda0
fwidth = freq0 / 10.0
# Creates a Structure; like in tidy3d python client.
# Here, the Variables "length", "width", "height", and the medium "Si"
# were previously defined in the Parameter tab.
waveguide = td.Structure(
geometry=td.Box(center=(0, 0, 0),
size=(param.length, param.width, param.height)),
medium=param.medium.Si
)
# Creates a ModeSource.
# Here, the Variables "size_x", "size_y", and "size_z" are
# the dimensions of the simulation domain.
pulse = td.GaussianPulse(freq0=freq0, fwidth=fwidth)
mode_spec = td.ModeSpec(num_modes=1)
mode_source = td.ModeSource(
center=(-param.size_x/2 + param.lambda0, 0, 0),
size=(0, param.size_y, param.size_z),
source_time=pulse,
mode_spec=mode_spec,
mode_index=0,
direction='+')
# Creates a ModeMonitor.
mode_monitor = td.ModeMonitor(
center=(param.size_x/2 - param.lambda0, 0, 0),
size=(0, param.size_y, param.size_z),
freqs=[freq0],
mode_spec=mode_spec,
name='mode_monitor')
# Finally, it returns the simulation objects.
return [waveguide, mode_source, mode_monitor]