You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Manage the tracing of an `AbstractBeam` through an optical `system`. The function retraces the `beam` if possible and then proceeds to trace each leaf of the beam tree through the system.
439
439
The condition to stop ray tracing is that the last `beam` intersection is `nothing` or the beam interaction is `nothing`. Then, the system is considered to be solved.
440
-
A maximum number of rays per `beam` (`r_max`) can be specified in order to avoid infinite calculations under resonant conditions, i.e. two facing mirrors.
440
+
A maximum number of rays per `beam` (`r_max`) can be specified in order to avoid infinite calculations under resonant conditions, i.e. two facing mirrors. Likewise, `depth_max` limits how many branching levels are explored when new sub-beams are generated (for example, by beamsplitters) so that the tree cannot grow without bound.
441
441
442
442
# Arguments
443
443
444
444
- `system::System`: The optical system in which the beam will be traced.
445
445
- `beam::AbstractBeam`: The beam object to be traced through the system.
446
446
- `r_max::Int=100` (optional): Maximum number of tracing iterations for each leaf. Default is 100.
447
447
- `retrace::Bool=true` (optional): Flag to indicate if the system should be retraced. Default is true.
448
+
- `depth_max::Int=typemax(Int)` (optional): Maximum number of branching levels explored from the root beam. Default allows unlimited depth.
448
449
"""
449
-
functionsolve_system!(system::AbstractSystem, beam::B; r_max::Int=100, retrace::Bool=true) where {B <:AbstractBeam}
450
-
# Initialize a queue for BFS with the root beam.
451
-
queue = [beam]
450
+
functionsolve_system!(
451
+
system::AbstractSystem,
452
+
beam::B;
453
+
r_max::Int=100,
454
+
retrace::Bool=true,
455
+
depth_max::Int=typemax(Int),
456
+
) where {B <:AbstractBeam}
457
+
queue = Tuple{B, Int}[(beam, 1)]
452
458
while!isempty(queue)
453
-
current =popfirst!(queue) # Process beams in FIFO order.
454
-
# Optionally retrace the current beam.
459
+
# Process beams in FIFO order.
460
+
current, depth =popfirst!(queue)
461
+
# Optionally retrace the current beam.
455
462
if retrace
456
463
retrace_system!(system, current)
457
464
end
458
465
# Process the current leaf beam.
459
466
solve_leaf!(system, current; r_max=r_max)
460
-
# Enqueue all child beams for subsequent processing.
461
-
for child inchildren(current) # 'children' returns an iterable of sub-beams.
462
-
push!(queue, child)
467
+
# Check if the maximum branching depth has been reached.
468
+
if depth <= depth_max
469
+
# Enqueue all child beams for subsequent processing.
470
+
for child inchildren(current) # 'children' returns an iterable of sub-beams.
471
+
push!(queue, (child, depth +1))
472
+
end
473
+
else
474
+
# Maximum braching depth is reached. Remove childrens of the current beam because they will not be solved.
475
+
_drop_beams!(current)
476
+
@debuglazy"Maximum branching depth of $depth_max levels reached."
0 commit comments