-
Notifications
You must be signed in to change notification settings - Fork 2
Add 3d ray intersect #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add 3d ray intersect #269
Conversation
Should be ready to go. This has extensive tests that should cover almost every line. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a bunch of suggestions to Julia-fy the code, but does the existing toroidal_intersection()
do what you want?
src/geometry.jl
Outdated
t_interval_z = [0.0 Inf] | ||
end | ||
else | ||
t_interval_z = sort![(z_min - z0) / dz, (z_max - z0) / dz] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort![...]
can't be right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t_interval_z = sort![(z_min - z0) / dz, (z_max - z0) / dz] | |
t_interval_z = sort([(z_min - z0) / dz, (z_max - z0) / dz]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort!([(z_min - z0) / dz, (z_max - z0) / dz])
at the every least, but sort(@SVector[(z_min - z0) / dz, (z_max - z0) / dz])
would be better.
src/geometry.jl
Outdated
crossing = zeros(Float64, 4) # +1 -> into domain | ||
# -1 -> out of | ||
# 0 -> no crossing | ||
into_domain = [1.0, -1.0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these small, fixed-sized arrays should either be Tuple
s or SVector
/MVector
from StaticArrays.jl. This will avoid unnecessary allocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
into_domain = [1.0, -1.0] | |
into_domain = Tuple([1.0, 1.0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
into_domain = (1.0, 1.0)
src/geometry.jl
Outdated
if (r0 > r_min && r0 < r_max) | ||
return [[0.0 Inf];] | ||
else | ||
return zeros(Float64, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want an empty array? Float64[]
is more idiomatic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return zeros(Float64, 0) | |
return Float64[] |
src/geometry.jl
Outdated
if dx == 0 && dy == 0 | ||
# Handle vertical ray | ||
if (r0 > r_min && r0 < r_max) | ||
return [[0.0 Inf];] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[0.0 Inf]
will give you a 1x2 Matrix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return [[0.0 Inf];] | |
return [0.0 Inf] |
src/geometry.jl
Outdated
t_crossings[2*(i_ref-1) + 1] = t1 >= 0 ? t1/ (dx^2 + dy^2) : Inf | ||
t_crossings[2*(i_ref-1) + 2] = t2 >= 0 ? t2/ (dx^2 + dy^2) : Inf | ||
crossing[2*(i_ref-1) + 1] = t1 >= 0 ? into_domain[i_ref]*sign(x0*dx + dx^2*t1 + y0 * dy + dy^2*t1) : 0.0 | ||
crossing[2*(i_ref-1) + 2] = t2 >= 0 ? into_domain[i_ref]*sign(x0*dx + dx^2*t2 + y0 * dy + dy^2*t2) : 0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this ever becomes computationally intensive, there are plenty of redundant FLOPs here that could be avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is run once per beam so it should not come up too often and cost negligible time compared to the actual raytracing.
src/geometry.jl
Outdated
append!(crossing, 1.0) | ||
end | ||
i_sort = sortperm(t_crossings) | ||
intervals = zeros(Float64, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Float64[]
src/geometry.jl
Outdated
end | ||
end | ||
if length(intervals) % 2 != 0 | ||
throw(ErrorException("Somehow only found odd number of intersections which is impossible.")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure grazing intersections are properly handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my specific use-case grazing intersection can raise an error. A beam that hits the rectangular flux matrix parallel to one of its boundaries indicates an error in how the beam is set up.
src/geometry.jl
Outdated
|
||
function solve_r_intersect(x0::T, y0::T, dx::T, dy::T, r_min::T, r_max::T) where {T<:Real} | ||
r0 = sqrt(x0^2 + y0^2) | ||
t_crossings = zeros(Float64, 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t_crossings = zeros(Float64, 4) | |
t_crossings = Float64[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changed the size.
src/geometry.jl
Outdated
append!(crossing, 1.0) | ||
end | ||
i_sort = sortperm(t_crossings) | ||
intervals = zeros(Float64, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
intervals = zeros(Float64, 0) | |
intervals = Float64[] |
As far as I can tell |
IMAS.jl/src/physics/particles.jl Lines 296 to 379 in ad841d4
|
@bclyons12 you were right, the existing routines do what I need to do. I am not sure why I didn't want to use them initially. I almost everything. Now all this PR does is add another dispatch to the |
(I also tested the regression test in |
This adds to geometry routines that compute intersections between a ray and a torus with a rectangular cross section.
Still work in progress because I need to remove
ray_rect_intersection
since that is untested LLM code that I don't need.