Skip to content
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

Fix n-bodies stack-overflow error with new type Bodies #95

Merged
merged 13 commits into from
Feb 17, 2024

Conversation

b-fg
Copy link
Member

@b-fg b-fg commented Jan 8, 2024

New AutoBodies type is implemented that can solve the n-bodies problem reported in #88. It holds a vector of type AutoBody and implements superposition of sdf and map functions by iteration instead of recursion.

Tests show same results as the recursive implementation of the superposition, while n-bodies can be handled now without hitting the stackoverflow error. Also, the measure function is much faster than before when superposing more than 5 bodies. The cost of the iterative method does not really scale when adding more bodies, while the recursive method seems to scale exponentially.

Here are some test when adding number of bodies n=6 and n=11 (max. I can handle in my computer with recursion)

radius = rand(n)
circles = [(x,t) -> sum(abs2,x)-r for r  radius]
body = AutoBody(circles[1])
for i  2:n
    body += AutoBody(circles[i])
end
bodies = AutoBodies([AutoBody(c) for c  circles], +)
xy = rand(2)

n=6

@btime measure($body, $xy, 1.)
# 5.436 μs (25 allocations: 22.44 KiB)
# 3.670 μs (57 allocations: 2.05 KiB)

n=11

@btime measure($bodies, $xy, 1.)
# 549.630 μs (25 allocations: 4.96 MiB)
# 3.735 μs (63 allocations: 2.20 KiB)

I do not particularly like that there is currently AutoBody and AutoBodies, but I did not want to get rid/break the previous implementation. Maybe this can be merged in a nicer way though.

New AutoBodies type is implemented. It holds a vector of type AutoBody and implements superposition of sdf and map functions by iteration instead of recursion.
∙ Tests show same results as the recursive implementation of the superposition, while n-bodies can be handled now without hitting the stackoverflow error.
∙ Also, the measure function is much faster than before since the iterative method scales linearly, while the recursive method seems to scale exponentially.
@b-fg b-fg added the bug Something isn't working label Jan 8, 2024
@b-fg b-fg linked an issue Jan 8, 2024 that may be closed by this pull request
Copy link

codecov bot commented Jan 8, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (3bbbca1) 96.55% compared to head (fa2bd1d) 96.12%.

Files Patch % Lines
src/AutoBody.jl 90.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #95      +/-   ##
==========================================
- Coverage   96.55%   96.12%   -0.44%     
==========================================
  Files          12       12              
  Lines         465      490      +25     
==========================================
+ Hits          449      471      +22     
- Misses         16       19       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@b-fg b-fg requested a review from weymouth January 8, 2024 23:33
test/runtests.jl Outdated Show resolved Hide resolved
@b-fg
Copy link
Member Author

b-fg commented Jan 20, 2024

Sadly I cannot test on MacOS, so I am not sure. But the fix on that test is because it fails in Julia 1.10.0 for any OS.

Other than this, the PR is mostly finished. The main new features of this PR is that Bodies can account for many AutoBody and perform measure without maxing out the stack. Also, individual body forces can now be extracted using

force = WaterLily.∮nds(sim.flow.p,sim.flow.f,sim.body.bodies[i],t)

@b-fg b-fg changed the title Fix n-bodies stack-overflow error with new type AutoBodies Fix n-bodies stack-overflow error with new type Bodies Jan 21, 2024
@b-fg
Copy link
Member Author

b-fg commented Jan 21, 2024

Ready for review.

@b-fg
Copy link
Member Author

b-fg commented Feb 6, 2024

Should we merge this or just close it? @weymouth

@weymouth
Copy link
Collaborator

We should merge it.

@b-fg b-fg merged commit 33421ae into master Feb 17, 2024
12 of 14 checks passed
@b-fg b-fg deleted the fix-nbodies-stackoverflow branch February 17, 2024 15:25
@weymouth weymouth mentioned this pull request Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Repeated AutoBody additions cause StackOverflow
2 participants