bf.viewer¶
view ¶
Returns the viewer for a device (spec D7).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stack
|
Any
|
a :class: |
required |
save_dir
|
Path | str | None
|
where a server-side save writes its PNG (spec D14). Defaults to the process working directory. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
Viewer
|
class: |
Source code in bifrost/viewer/widget.py
Viewer ¶
Bases: AnyWidget
The app-shell widget returned by :func:view (spec D7, D9).
Source code in bifrost/viewer/widget.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | |
constraints
property
¶
The prepared placement constraints, computed on access (spec D11).
last_error
property
¶
The last run's exception, if it failed (spec Q20).
last_result
property
¶
The last completed run's :class:~bifrost.viewer.sim.RunResult (spec Q20).
objects
property
¶
The prepared simulation objects, computed on access (spec D11).
__init__ ¶
Builds the device's geometry and prepares the widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stack
|
Any
|
a :class: |
required |
save_dir
|
Path | str | None
|
where the save button writes a PNG when the browser
prefers a server-side save (spec D14). Defaults to the process
working directory, resolved once at construction so a later
|
None
|
**kwargs
|
Any
|
passed to :class: |
{}
|
Source code in bifrost/viewer/widget.py
load_result ¶
load_result(objects: Any, arrays: Any, config: Any, stack: Any = None, *, settings: Any = None) -> None
Displays a completed simulation from arbitrary user code (spec R16, D18).
Takes the three pieces fdtdx hands out directly —
:func:fdtdx.place_objects returns (objects, config) and
:func:fdtdx.run_fdtd returns (time_step, arrays) — so nothing is
assembled for it; the widget's own run result passes as
load_result(*result). The result lands in the viewer's result
world (spec R17): a separate state built from stack (defaulting
to the stack the widget was built from) that the Result tab shows —
its own geometry, one read-only adopted spec per source and detector in
objects (spec R16), and the phasor-family detectors shaded from
arrays.detector_states. The editor's geometry and styles are
untouched. When the result is on the widget's own stack, the editor
lists the same sources and detectors, as read-only copies, so the two
tabs agree.
The adopted sources and detectors are excluded from a later
:meth:run, and a run (or a second load_result) replaces them: the
setup you add yourself is what runs, and is never re-created from them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
Any
|
the result's placed :class: |
required |
arrays
|
Any
|
the result's fdtdx |
required |
config
|
Any
|
the result's :class: |
required |
stack
|
Any
|
the :class: |
None
|
settings
|
Any
|
kernel-side :class: |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if a run is in flight (spec D15). |
ValueError
|
if the result lacks a required piece, a phasor detector
could never shade, or |
Source code in bifrost/viewer/widget.py
run ¶
Places and runs the simulation on a background thread (spec D15).
Returns immediately; the widget stays interactive. While the run is in
flight the setup is locked (state['running'] is true and setup edits
are rejected). On completion run_result carries a summary and
state['running'] returns to false; detector results are on
self.last_result.arrays.detector_states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_done
|
Callable[[Any], None] | None
|
called with the :class: |
None
|
key
|
Any
|
an optional |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if a run is already in flight. |
Source code in bifrost/viewer/widget.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
show_result ¶
Adopts a result the user produced elsewhere as the current result (R15).
The common case is a user who ran the simulation under srun and
wants to look at the fields in the widget: this adopts that result's
detector arrays and redraws every overlay, with no re-run and no
re-send of the geometry. It does not adopt the result's geometry or
objects — the widget's own stack and specs stay authoritative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
a :class: |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the result's detectors do not correspond to the widget's current setup by name, grid shape, grid spacing and plane position (spec Q23). The previous result and its overlays are left untouched. |
Source code in bifrost/viewer/widget.py
stop ¶
Asks the in-flight run to stop (spec D17).
Sets the run's cancel flag; the worker notices it at the next step or
phase boundary and unwinds. Idempotent, and a no-op when no run is in
flight. Does not clear running — the worker's finally owns that,
so the widget never claims a run has ended before it has.
Returns:
| Type | Description |
|---|---|
bool
|
True if a run was in flight and the stop was requested, False |
bool
|
otherwise. |
Source code in bifrost/viewer/widget.py
SimSettings
dataclass
¶
The setup-wide simulation settings (spec R13).
Attributes:
| Name | Type | Description |
|---|---|---|
wavelength_um |
float
|
the default wavelength, in µm, for new sources/detectors. |
pulse_width_frac |
float
|
the Gaussian pulse's spectral width as a fraction of
the center frequency; |
port_padding |
tuple[float, float]
|
|
pml_thickness |
int
|
the PML thickness, in grid cells. |
resolution_um |
float
|
the grid resolution, in µm. |
time_s |
float
|
the simulation time, in seconds. |
boundary_offset |
tuple[float, float, float, float, float, float]
|
|
Source code in bifrost/viewer/sim.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
from_wire
classmethod
¶
Rebuilds settings from a browser patch, validating each field.
Source code in bifrost/viewer/sim.py
to_wire ¶
The settings as JSON for the browser (spec R13).
Source code in bifrost/viewer/sim.py
ObjectSpec
dataclass
¶
A source or detector as data, not a live fdtdx object (spec D11).
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
a key of :data: |
port |
str
|
the port name the object sits on, or |
params |
dict[str, Any]
|
the editable parameters ( |
id |
str
|
a stable identity for update/remove/selection. Assigned by
:class: |
glyph |
dict[str, Any] | None
|
an override for the wire glyph (spec D18). Set on adopted
foreign detectors, whose glyph is derived from their placed
|
Source code in bifrost/viewer/sim.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
field_params ¶
The detector's display settings with defaults filled in (spec R15).
Only a detector has these; a source returns an empty dict. Absent keys
fall back to the defaults, so the browser always gets a complete set.
An unpicked component defaults to |E| (see
:func:_default_component); one set to None — "(none)" picked — stays
None. The outline defaults off.
Source code in bifrost/viewer/sim.py
from_wire
classmethod
¶
Rebuilds a spec from a browser patch, validating the type and port.
Source code in bifrost/viewer/sim.py
to_wire ¶
RunResult ¶
Bases: NamedTuple
The outcome of a mode-1 run (spec R14, Q20).
arrays is the fdtdx :class:ArrayContainer; detector results are read
from arrays.detector_states keyed by detector name. objects is the
placed :class:ObjectContainer, so objects[name].compute_overlap(...)
works as in the reference example.
Source code in bifrost/viewer/sim.py
export_demo ¶
export_demo(viewer: Any, out_dir: str | Path, *, section_step_um: float = 0.1, field_defaults: dict[str, dict[str, Any]] | None = None, unchecked: Sequence[str] = (), manifest: dict[str, Any] | None = None) -> dict[str, Any]
Writes a viewer that holds a result (load_result or run) as demo files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewer
|
Any
|
a :class: |
required |
out_dir
|
str | Path
|
the directory to write; created if missing. |
required |
section_step_um
|
float
|
the spacing of the stored 2D slices along each plane's normal. The positions include the box center, which is where the field plane of a device centered on its core sits. |
0.1
|
field_defaults
|
dict[str, dict[str, Any]] | None
|
how each detector's field is shown when the demo opens, by detector
name: any of the |
None
|
unchecked
|
Sequence[str]
|
names of sources and detectors that open with their checkbox off (hidden, with their field), in the editor's tabs and the Result tab alike. |
()
|
manifest
|
dict[str, Any] | None
|
extra entries for |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The manifest, also written to |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the viewer holds no result. |
Source code in bifrost/viewer/export.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |