import typing
import matplotlib.pylab as plt
import numpy as np
import tidy3d as td
import tidy3d.plugins.design as tdd
td.config.logging.level = "ERROR"The Design plugin (tidy3d.plugins.design) is a user-friendly tool that simplifies the process of defining and optimizing experiments within the Tidy3D environment. This plugin helps you efficiently explore different design possibilities by setting up a structured approach to test and refine your ideas.
To get started, define the key Parameters of your design problem and choose a Method that will suggest possible solutions. The Method can sample the design space systematically or randomly using MethodGrid, MethodZip or MethodMonteCarlo, or can optimize for a given problem through iterative search and evaluation using techniques like Bayesian optimization (MethodBayOpt), genetic algorithm (MethodGenAlg) or particle swarm optimization (MethodParticleSwarm). These choices are combined into a DesignSpace object, which serves to manage the search process.
Next, provide a function that describes the relationship between your input variables (the dimensions of your design) and the desired outputs. This function can take any inputs and return any outputs that you want to investigate.
The results of the search are automatically stored in a Result object. This object contains all the details of the search and can be quickly converted into a pandas.DataFrame for further analysis, post-processing, or visualization. In this DataFrame, each column represents an input or output from your function, and each row represents a single data point from your experiment.
Here’s a diagram to details the process step-by-step.
Another example of the Design plugin can be seen about halfway down our Parameter Scan notebook. A simple practical case study of the Design plugin is available in the All-Dielectric Structural Color notebook, or more complex case studies in the following optimization notebooks:
The plugin is imported from tidy3d.plugins.design so it’s convenient to import this namespace first along with the other packages we need.
import typing
import matplotlib.pylab as plt
import numpy as np
import tidy3d as td
import tidy3d.plugins.design as tdd
td.config.logging.level = "ERROR"While the Design plugin is built for Tidy3D and has special features for handling FDTD simulations, it can also be run with any general design problem. Here is a quick, complete example before diving into the use of the plugin with Tidy3D simulations.
Here we want to sample a design space with two dimensions x and y with Monte Carlo. Our function simply returns a Gaussian centered at (0,0) and we’ll evaluate the result for Gaussians with 3 different variances.
# Define your design space (parameters (x,y) within certain ranges)
param_x = tdd.ParameterFloat(name="x", span=(-15, 15))
param_y = tdd.ParameterFloat(name="y", span=(-15, 15))
# Define your sampling method, Monte Carlo method with 10,000 points
method = tdd.MethodMonteCarlo(num_points=10000)
# Put everything together in a `DesignSpace` container
design_space = tdd.DesignSpace(method=method, parameters=[param_x, param_y])
# Define your fitness function / figure of merit. Here we compute a gaussian as a function of x and y with different values for the width.
sigmas = {f"sigma={s}": s for s in [5, 10, 20]}
def f(x: float, y: float) -> typing.Dict[str, float]:
"""Gaussian distribution as a function of x and y."""
r2 = x**2 + y**2
def gaussian(sigma: float) -> float:
return np.exp(-r2 / sigma**2)
# return a dictionary, where the key is used to label the output names
return {key: gaussian(sigma) for key, sigma in sigmas.items()}
# Call .run on the DesignSpace with our function and convert the results to a pandas.DataFrame
df = design_space.run(f).to_dataframe()
# Plot the results using pandas.DataFrame builtins
f, axes = plt.subplots(1, 3, figsize=(10, 3), tight_layout=True)
for ax, C in zip(axes, sigmas.keys()):
_ = df.plot.hexbin(x="x", y="y", gridsize=20, C=C, cmap="magma", ax=ax)
ax.set_title(C)# Look at the first 5 outputs
df.head()