Skip to content

Commit 13eced6

Browse files
Let me try again the task assignment
1 parent 2cc3b1f commit 13eced6

File tree

2 files changed

+160
-4
lines changed

2 files changed

+160
-4
lines changed

tidy3d/plugins/smatrix/run.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
def create_batch(
1919
modeler: ComponentModelerType,
2020
path_dir: str = DEFAULT_DATA_DIR,
21+
parent_batch_id: str = None,
2122
file_name: str = "batch.hdf5",
2223
**kwargs,
2324
) -> Batch:
@@ -33,7 +34,15 @@ def create_batch(
3334
The configured `Batch` object ready for execution.
3435
"""
3536
filepath = os.path.join(path_dir, file_name)
36-
batch = Batch(simulations=modeler.sim_dict, **kwargs)
37+
38+
if parent_batch_id is not None:
39+
parent_task_dict = dict()
40+
for key in modeler.sim_dict.keys():
41+
parent_task_dict[key] = tuple(parent_batch_id,)
42+
else:
43+
parent_task_dict = None
44+
45+
batch = Batch(simulations=modeler.sim_dict, parent_tasks=parent_task_dict, **kwargs)
3746
batch.to_file(filepath)
3847
return batch
3948

tidy3d/plugins/smatrix/web/api/webapi.py

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def run(
177177
Monitor progress of each of the running tasks.
178178
"""
179179
if isinstance(simulation, ComponentModelerType):
180-
batch_id = upload(
180+
batch_task = upload_modeler(
181181
simulation=simulation,
182182
task_name=task_name,
183183
folder_name=folder_name,
@@ -193,8 +193,9 @@ def run(
193193
batch = create_batch(
194194
modeler=simulation,
195195
folder_name=folder_name,
196-
solver_version=solver_version
197-
# parent_tasks=[task_id]
196+
solver_version=solver_version,
197+
parent_batch_id=batch_task.batch_id,
198+
group_id=batch_task.group_id
198199
)
199200
batch.run()
200201
# postprocess(
@@ -398,6 +399,152 @@ def upload(
398399
return task.task_id
399400

400401

402+
@wait_for_connection
403+
def upload_modeler(
404+
simulation: SimulationType,
405+
task_name: str,
406+
folder_name: str = "default",
407+
callback_url: Optional[str] = None,
408+
verbose: bool = True,
409+
progress_callback: Optional[Callable[[float], None]] = None,
410+
simulation_type: str = "tidy3d",
411+
parent_tasks: Optional[list[str]] = None,
412+
source_required: bool = True,
413+
solver_version: Optional[str] = None,
414+
reduce_simulation: Literal["auto", True, False] = "auto",
415+
) -> TaskId:
416+
"""
417+
Upload simulation to server, but do not start running :class:`.Simulation`.
418+
419+
Parameters
420+
----------
421+
simulation : Union[:class:`.Simulation`, :class:`.HeatSimulation`, :class:`.EMESimulation`]
422+
Simulation to upload to server.
423+
task_name : str
424+
Name of task.
425+
folder_name : str
426+
Name of folder to store task on web UI
427+
callback_url : str = None
428+
Http PUT url to receive simulation finish event. The body content is a json file with
429+
fields ``{'id', 'status', 'name', 'workUnit', 'solverVersion'}``.
430+
verbose : bool = True
431+
If ``True``, will print progressbars and status, otherwise, will run silently.
432+
progress_callback : Callable[[float], None] = None
433+
Optional callback function called when uploading file with ``bytes_in_chunk`` as argument.
434+
simulation_type : str = "tidy3d"
435+
Type of simulation being uploaded.
436+
parent_tasks : List[str]
437+
List of related task ids.
438+
source_required: bool = True
439+
If ``True``, simulations without sources will raise an error before being uploaded.
440+
solver_version: str = None
441+
target solver version.
442+
reduce_simulation: Literal["auto", True, False] = "auto"
443+
Whether to reduce structures in the simulation to the simulation domain only. Note: currently only implemented for the mode solver.
444+
445+
Returns
446+
-------
447+
str
448+
Unique identifier of task on server.
449+
450+
451+
Notes
452+
-----
453+
454+
Once you've created a ``job`` object using :class:`tidy3d.plugins.smatrix.web.api.container.Job`, you can upload it to our servers with:
455+
456+
.. code-block:: python
457+
458+
web.upload(simulation, task_name="task_name", verbose=verbose)
459+
460+
It will not run until you explicitly tell it to do so with :meth:`tidy3d.plugins.smatrix.web.api.webapi.start`.
461+
462+
"""
463+
464+
if isinstance(simulation, (ModeSolver, ModeSimulation)):
465+
simulation = get_reduced_simulation(simulation, reduce_simulation)
466+
467+
if isinstance(simulation, ComponentModelerType):
468+
port_name_list = [port.name for port in simulation.ports]
469+
else:
470+
port_name_list = []
471+
472+
stub = Tidy3dStub(simulation=simulation)
473+
stub.validate_pre_upload(source_required=source_required)
474+
log.debug("Creating task.")
475+
476+
task_type = stub.get_type()
477+
if (task_type == "COMPONENT_MODELER") or (task_type == "TERMINAL_COMPONENT_MODELER"): # TODO REMOVE
478+
task_type = "RF"
479+
480+
task = SimulationTask.create(
481+
task_type, task_name, folder_name, callback_url, simulation_type, parent_tasks, file_type="Gz", port_name_list=port_name_list
482+
)
483+
print(task)
484+
if verbose:
485+
console = get_logging_console()
486+
if isinstance(simulation, ComponentModelerType):
487+
console.log(
488+
f"Created modeler task '{task_name}' with batch_id '{task.batch_id}' and task_type '{task_type}'."
489+
)
490+
else:
491+
console.log(
492+
f"Created task '{task_name}' with task_id '{task.task_id}' and task_type '{task_type}'."
493+
)
494+
if task_type in BETA_TASK_TYPES:
495+
solver_name = SOLVER_NAME[task_type]
496+
console.log(
497+
f"Tidy3D's {solver_name} solver is currently in the beta stage. "
498+
f"Cost of {solver_name} simulations is subject to change in the future."
499+
)
500+
if task_type in GUI_SUPPORTED_TASK_TYPES:
501+
if isinstance(simulation, ComponentModelerType):
502+
url = _get_url(resource_id=task.group_id, resource_type="group")
503+
folder_url = _get_folder_url(task.folder_id)
504+
console.log(f"View task using web UI at [link={url}]'{url}'[/link].")
505+
console.log(f"Task folder: [link={folder_url}]'{task.folder_name}'[/link].")
506+
else:
507+
url = _get_url(task.task_id)
508+
folder_url = _get_folder_url(task.folder_id)
509+
console.log(f"View task using web UI at [link={url}]'{url}'[/link].")
510+
console.log(f"Task folder: [link={folder_url}]'{task.folder_name}'[/link].")
511+
512+
remote_sim_file = SIM_FILE_HDF5_GZ
513+
if task_type == "MODE_SOLVER":
514+
remote_sim_file = MODE_FILE_HDF5_GZ
515+
elif isinstance(simulation, ComponentModelerType):
516+
remote_sim_file = MODELER_FILE_HDF5_GZ
517+
518+
if isinstance(simulation, ComponentModelerType):
519+
task.upload_simulation(
520+
stub=stub,
521+
verbose=verbose,
522+
progress_callback=progress_callback,
523+
remote_sim_file=remote_sim_file,
524+
)
525+
# TODO talk momchil
526+
# estimate_cost(task_id=task.task_id, solver_version=solver_version, verbose=verbose)
527+
528+
task.validate_post_upload(parent_tasks=parent_tasks)
529+
530+
# log the url for the task in the web UI
531+
log.debug(f"{Env.current.website_endpoint}/folders/{task.folder_id}/tasks/{task.group_id}")
532+
return task.batch_id
533+
else:
534+
task.upload_simulation(
535+
stub=stub,
536+
verbose=verbose,
537+
progress_callback=progress_callback,
538+
remote_sim_file=remote_sim_file,
539+
)
540+
estimate_cost(task_id=task.task_id, solver_version=solver_version, verbose=verbose)
541+
542+
task.validate_post_upload(parent_tasks=parent_tasks)
543+
544+
# log the url for the task in the web UI
545+
log.debug(f"{Env.current.website_endpoint}/folders/{task.folder_id}/tasks/{task.task_id}")
546+
return task
547+
401548
def get_reduced_simulation(simulation, reduce_simulation):
402549
"""
403550
Adjust the given simulation object based on the reduce_simulation parameter. Currently only

0 commit comments

Comments
 (0)