-
Notifications
You must be signed in to change notification settings - Fork 279
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
ENH: use Lagrangian tesselation for smoothing N-body plotting #4306
base: main
Are you sure you want to change the base?
Conversation
Too soon for a review I guess but just wanted to say this looks amazing.
Can you clarify what you compared ? (since the quality can be adjusted I assume increasing it has a performance cost) |
I've updated the PR description, so hopefully, that should be better. The most crucial bit is that the SPH-interpolation scales as The reason there needs to be some sampling is that we need to compute the volume of the intersection of each tetrahedron with each pixel. In principle, this could be computed exactly but the lazy way is just to (randomly) sample the tetrahedron to estimate the volume. |
Interesting. I'm guessing there should be a point (critical number of samples) at which the lazy evaluation becomes as expensive as the exact evaluation. Is there an estimate for it ? It's not critical, but if it's known it should probably be documented. |
Correct bug where points were drawn in cube rather than tetrahedron
TBH I'd eventually want to use https://github.com/devonmpowell/r3d/ to compute the intersection between voxels and tetrahedron, but that may be too optimistic. |
Another option that would work well in your cythonic code would be to use the recursive algorithm I first did in IDL and we describe in this
paper: Noiseless Gravitational Lensing Simulations
https://arxiv.org/abs/1309.1161
There you split tetrahedra recursively until they are smaller than a voxel and then just deposit them as nearest grid point deposit.
Exciting to see this capability in yt!
Tom
|
After comments from @yipihey, I went ahead and implemented an "exact" integrator. The resulting picture looks like this 🍿 It works by noticing that the projection of a uniformly-sampled tetrahedron onto the plane is a simple geometrical figure (see below). There are only two cases to treat: either the projection of the tetrahedron is a triangle or a quadrilateral.
My implementation works as follows:
At this stage, we have an estimate of the density of all the pixels with centre in the hull. Unfortunately, if we sum these values, we don't get exactly the right integral due to aliasing issues.
The method is actually fairly accurate: since the projected density is a linear function, the mean value in a pixel is exactly the value at the pixel centre, except around the edges. |
Very nice! There is a fast way we developed for projections that is particularly efficient on GPUs that we implemented here (see section 4.4) A Novel Approach to Visualizing Dark Matter Simulations
https://arxiv.org/pdf/1208.3206.pdf
You first find the back and the front faces of tetrahedra by dotting the normal of each face with the unit vector defining the direction of your view. Then you add the density times the depth of the face (for each image pixel ) of the front facing faces into one image and for the back facing one’s in another image. When you are done with all tetrahedra you subtract the two images and you get back the integrated density along the line of sight.
Voila exact projection of the linear elements with few if statements. Hope this approach may help to speed also your current implementation.
Tom
|
This looks awesome! Excited to see this functionality. Thanks for the work on this, @cphyc ! |
One solution to plot smooth-looking N-body data is to approximate them as SPH particles.
This PR explores an alternative (see https://github.com/yipihey/LagrangianPhaseSpaceSheetEasy, credits to @yipihey) using "Lagrangian tesselation".
In the initial conditions, DM particles reside on a grid, from which we can compute a tesselation made of tetrahedron only. As the simulation progresses, these tetrahedrons will be distorted. The method implemented here samples positions within each individual tetrahedron to reconstruct a smoother version of the density field.
Comparison
This is obtained for a 64^3 tiny dataset.
The cherry on the cake: this PR is actually much faster than SPH interpolation because there's no need to build the KD-tree necessary for the SPH interpolation.
TODO: