Running simulations through the cloud#

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 don't 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, don't 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:08:12 -03 Created task 'webAPI' with resource_id
             'fdve-039a9031-c9b5-4cd3-a8e7-201739e9805c' and task_type 'FDTD'.
             Task folder: 'default'.
15:08:16 -03 Estimated FlexCredit cost: 0.025. Minimum cost depends on task
             execution details. Use 'web.real_cost(task_id)' to get the billed
             FlexCredit cost after a simulation run.
15:08:18 -03 status = queued
             To cancel the simulation, use 'web.abort(task_id)' or
             'web.delete(task_id)' or abort/delete the task in the web UI.
             Terminating the Python script will not stop the job running on the
             cloud.
15:08:30 -03 starting up solver
15:08:31 -03 running solver
15:08:32 -03 status = success
15:08:36 -03 Loading simulation from data/sim.hdf5

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 simulations, mainly because it eliminates the need for keeping track of the task_id and original Simulation.

We can get the cost estimate of running the task before actually running it. This prevents us from accidentally running large jobs that we set up by mistake. The estimated cost is the maximum cost corresponding to running all the time steps.

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

# estimate the maximum cost
estimated_cost = web.estimate_cost(job.task_id)
             Created task 'job' with resource_id
             'fdve-45cb3319-1886-4545-bf5f-85ee04c45fbf' and task_type 'FDTD'.
             Task folder: 'default'.
15:08:41 -03 Estimated FlexCredit cost: 0.025. Minimum cost depends on task
             execution details. Use 'web.real_cost(task_id)' to get the billed
             FlexCredit cost after a simulation run.

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.

[5]:
# start job, monitor, and load results
sim_data = job.run(path="data/sim.hdf5")
15:08:42 -03 Estimated FlexCredit cost: 0.025. Minimum cost depends on task
             execution details. Use 'web.real_cost(task_id)' to get the billed
             FlexCredit cost after a simulation run.
15:08:44 -03 status = success
15:08:48 -03 Loading simulation from data/sim.hdf5

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.

[6]:
# 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:08:51 -03 Loading simulation from data/sim.hdf5

Running multiple simulations with web.run#

Commonly one needs to submit multiple simulations.

As of version 2.10, web.run can directly submit and monitor any nested combination of dictionaries, lists, and tuples of simulations.

This makes it a convenient replacement for batch-style workflows while preserving the input structure in the returned results.

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

# run all simulations and return a dictionary with the same task names
batch_results = web.run(sims, verbose=verbose)
             Created task 'fdtd_2026-04-20_15-08-51' with resource_id
             'fdve-1c4a0c97-16e7-45db-9cc7-16edcfecf547' and task_type 'FDTD'.
             Task folder: 'default'.
15:08:54 -03 Estimated FlexCredit cost: 0.025. Minimum cost depends on task
             execution details. Use 'web.real_cost(task_id)' to get the billed
             FlexCredit cost after a simulation run.
15:08:56 -03 status = success
15:09:00 -03 Loading simulation from simulation_data.hdf5

The returned object preserves the input structure. Since we used a dictionary of simulations, the result is also a dictionary keyed by task name.

Each value is a SimulationData object, so the results can be indexed directly with batch_results[task_name] or iterated with batch_results.items().

This makes it straightforward to organize parameter scans and other grouped studies without explicitly creating a Batch container.

[8]:
# 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[0])
    intensities[task_name] = sum_intensity

print(intensities)
{'sim_0': 6473210.5, 'sim_1': 6473210.5, 'sim_2': 6473210.5}

After going through this tutorial, you have learned how to run simulations with Tidy3D via the web API. If you are new to Tidy3D or the finite-difference time-domain (FDTD) method, we highly recommend going through our FDTD101 tutorials and example library before starting your own simulation adventure.