Web API#

This notebook is a tutorial of the API used for submitting simulations to our servers.

[1]:
import tidy3d as td
import tidy3d.web as web

# set logging level to ERROR because we'll only running simulations to demonstrate the web API, we dont care about the results.
td.config.logging_level = "ERROR"

Setup#

Let’s set up a simple simulation to get started.

[2]:
# whether to print output in web functions, note: if False (default) they will all run silently
verbose = True

# set up parameters of simulation
dl = 0.05
pml = td.PML()
sim_size = [4, 4, 4]
freq0 = 3e14
fwidth = 1e13
run_time = 1 / fwidth

# create structure
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
)

# create source
source = td.UniformCurrentSource(
    center=(-1.5, 0, 0),
    size=(0, 0.4, 0.4),
    source_time=td.GaussianPulse(freq0=freq0, fwidth=fwidth),
    polarization="Ex",
)

# create monitor
monitor = td.FieldMonitor(
    fields=["Ex", "Ey", "Ez"],
    center=(0, 0, 0),
    size=(td.inf, td.inf, 0),
    freqs=[freq0],
    name="field",
)

# Initialize simulation
sim = td.Simulation(
    size=sim_size,
    grid_spec=td.GridSpec.uniform(dl),
    structures=[square],
    sources=[source],
    monitors=[monitor],
    run_time=run_time,
    boundary_spec=td.BoundarySpec.all_sides(boundary=pml),
)

Running simulation manually#

For the most control, you can run the simulation through the Tidy3D web API. Each simulation running on the server is identified by a task_id, which must be specified in the web API. Let’s walk through submitting a simulation this way.

[3]:
# upload the simulation to our servers
task_id = web.upload(sim, task_name="webAPI", verbose=verbose)

# start the simulation running
web.start(task_id)

# monitor the simulation, dont move on to next line until completed.
web.monitor(task_id, verbose=verbose)

# download the results and load into a simulation data object for plotting, post processing etc.
sim_data = web.load(task_id, path="data/sim.hdf5", verbose=verbose)

[15:42:09] Created task 'webAPI' with task_id 'fdve-33b402be-fbf9-4893-a62d-19490cab0815v1'.          webapi.py:139
[15:42:11] status = queued                                                                            webapi.py:269
[15:42:14] status = preprocess                                                                        webapi.py:263
[15:42:18] Maximum FlexCredit cost: 0.025. Use 'web.real_cost(task_id)' to get the billed FlexCredit  webapi.py:286
           cost after a simulation run.                                                                            
           starting up solver                                                                         webapi.py:290
           running solver                                                                             webapi.py:300
[15:42:24] early shutoff detected, exiting.                                                           webapi.py:313
           status = postprocess                                                                       webapi.py:330
[15:42:28] status = success                                                                           webapi.py:337
[15:42:31] loading SimulationData from data/sim.hdf5                                                  webapi.py:512

While we broke down each of the individual steps above, one can also perform the entire process in one line by calling the web.run() function as follows.

sim_data = web.run(sim, task_name='webAPI', path='data/sim.hdf5')

(We won’t run it again in this notebook to save time).

Sometimes this is more convenient, but other times it can be helpful to have the steps broken down one by one, for example if the simulation is long, you may want to web.start and then exit the session and load the results at a later time using web.load.

Job Container#

The web.Job interface provides a more convenient way to manage single simuations, mainly because it eliminates the need for keeping track of the task_id and original Simulation.

While Job has methods with names identical to the web API functions above, which give some more granular control, it is often most convenient to call [.run()] so we will do that now.

[4]:
# initializes job, puts task on server (but doesnt run it)
job = web.Job(simulation=sim, task_name="job", verbose=verbose)

# start job, monitor, and load results
sim_data = job.run(path="data/sim.hdf5")

[15:42:32] Created task 'job' with task_id 'fdve-f6854233-cebf-4ff1-9c08-85404b504179v1'.             webapi.py:139
[15:42:34] status = queued                                                                            webapi.py:269
[15:42:36] status = preprocess                                                                        webapi.py:263
[15:42:40] Maximum FlexCredit cost: 0.025. Use 'web.real_cost(task_id)' to get the billed FlexCredit  webapi.py:286
           cost after a simulation run.                                                                            
           starting up solver                                                                         webapi.py:290
           running solver                                                                             webapi.py:300
[15:42:46] early shutoff detected, exiting.                                                           webapi.py:313
           status = postprocess                                                                       webapi.py:330
[15:42:49] status = success                                                                           webapi.py:337
[15:42:51] loading SimulationData from data/sim.hdf5                                                  webapi.py:512

Another convenient thing about Job objects is that they can be saved and loaded just like other Tidy3d components.

This is convenient if you want to save and load up the results of a job that has already finished, without needing to know the task_id.

[5]:
# saves the job metadata to a single file
job.to_file("data/job.json")

# can exit session, break here, or continue in new session.

# load the job metadata from file
job_loaded = web.Job.from_file("data/job.json")

# download the data from the server and load it into a SimulationData object.
sim_data = job_loaded.load(path="data/sim.hdf5")

[15:42:55] loading SimulationData from data/sim.hdf5                                                  webapi.py:512

Batch Processing#

Commonly one needs to submit a batch of Simulations. One way to approach this using the web API is to upload, start, monitor, and load a series of tasks one by one, but this is clumsy and you can lose your data if the session gets interrupted.

A better way is to use the built-in Batch object. The batch object is like a Job, but stores task metadata for a series of simulations.

[6]:
# make a dictionary of  {task name : simulation} for demonstration
sims = {f"sim_{i}": sim for i in range(3)}

# initialize a batch and run them all
batch = web.Batch(simulations=sims, verbose=verbose)

# run the batch and store all of the data in the `data/` dir.
batch_results = batch.run(path_dir="data")

[15:42:55] Created task 'sim_0' with task_id 'fdve-214acefc-7676-437a-a43c-5413224bd460v1'.           webapi.py:139
[15:42:56] Created task 'sim_1' with task_id 'fdve-c238e0ab-2b9e-4b6d-a867-81a871d3eecfv1'.           webapi.py:139
[15:42:57] Created task 'sim_2' with task_id 'fdve-58e5650c-efd5-4e8d-97ac-6fd65585d0dav1'.           webapi.py:139
[15:43:00] Started working on Batch.                                                               container.py:402
[15:43:20] Batch complete.                                                                         container.py:436

When the batch is completed, the output is not a SimulationData but rather a BatchData. The data within this BatchData object can either be indexed directly batch_results[task_name] or can be looped through batch_results.items() to get the SimulationData for each task.

This was chosen to reduce the memory strain from loading all SimulationData objects at once.

Alternatively, the batch can be looped through (several times) using the .items() method, similar to a dictionary.

[7]:
# grab the sum of intensities in the simulation one by one (to save memory)
intensities = {}
for task_name, sim_data in batch_results.items():
    intensity = sim_data.get_intensity("field").sel(f=freq0)
    sum_intensity = float(intensity.sum(("x", "y")).values)
    intensities[task_name] = sum_intensity

print(intensities)

[15:43:23] loading SimulationData from data/fdve-214acefc-7676-437a-a43c-5413224bd460v1.hdf5          webapi.py:512
[15:43:27] loading SimulationData from data/fdve-c238e0ab-2b9e-4b6d-a867-81a871d3eecfv1.hdf5          webapi.py:512
[15:43:28] loading SimulationData from data/fdve-58e5650c-efd5-4e8d-97ac-6fd65585d0dav1.hdf5          webapi.py:512
{'sim_0': 13540217.099674325, 'sim_1': 13540217.099674325, 'sim_2': 13540217.099674325}

Asynchronous Batching#

Finally, one can make use of the asyncio package to perform asynchronous processing of several simulations.

For this purpose, a web.run_async function is provided, which works like the regular web.run but accepts a dictionary of simulations.

Here is the previous example repeated using this feature.

[8]:
batch_results = web.run_async(simulations=sims, verbose=verbose)

[9]:
# grab the sum of intensities in the simulation one by one (to save memory)
intensities = {}
for task_name, sim_data in batch_results.items():
    intensity = sim_data.get_intensity("field").sel(f=freq0)
    sum_intensity = float(intensity.sum(("x", "y")).values)
    intensities[task_name] = sum_intensity

print(intensities)

[15:43:57] loading SimulationData from ./fdve-0d149f2a-697c-432b-be92-52fb7046c70bv1.hdf5             webapi.py:512
[15:44:01] loading SimulationData from ./fdve-d21d30de-c75d-431b-b52a-ddb1e43c6bb6v1.hdf5             webapi.py:512
[15:44:04] loading SimulationData from ./fdve-4e60e74d-5d1b-4490-a3b2-1c7ab82db683v1.hdf5             webapi.py:512
{'sim_0': 13540217.099674325, 'sim_1': 13540217.099674325, 'sim_2': 13540217.099674325}
[ ]: