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

Vectorization of xrd_simulator #9

Merged
merged 321 commits into from
Jun 13, 2024
Merged

Vectorization of xrd_simulator #9

merged 321 commits into from
Jun 13, 2024

Conversation

Marcraven
Copy link
Collaborator

@Marcraven Marcraven commented Apr 11, 2024

This pull request is a generealized vectorization of most procedures in xrd_simulator.

The main idea behind is to increase computation speed an facilitate code parallelization whenever possible. The motivation behind was my intention to use this code to simulate powder samples with several million tetrahedra.

The main files affected are:

mesh.py

Once the mesh is created by meshio/pygalmesh, subsequent computations related to bounding spheres, centroids etc. have all been vectorized.

polycrystal.py

The _diffract method has been extensively modified to compute all diffraction points simultaneously for each phase present in the sample. There is still work to be done on the side of the tetrahedron-beam intersection, in which a faster bounding-box approximation has been implemented. The convex-hull calculation for the ScatteringUnit is still a bottleneck, but so far I have no idea how to improve it.

Another minor improvement has been adding zd and yd to the scattering unit to prevent repeating the calculation later in the rendering.

utils.py

Geometric calculations for circumspheres have been added here which help accelerate the mesh.py script a lot. A printvars utility has been added to help debugging memory issues during code execution.

Current bottlenecks

  • The tetrahedra-beam intersection
  • The convex-hull calculation
  • The intensity calculation during rendering

Vectorization of these last steps is not trivial and has to be thought through. The last one is probably doable although I could not find the time.

Suggestions

  • Include beam absorption physics
  • Allow for a range of wavelengths to be simulated rather than a single one
  • Do not impose >0 rotation for rigid body motions, perhaps one only wants to translate.

@Marcraven
Copy link
Collaborator Author

This is ready to ship. The docstrings should still be verified to fit with current changes though.

The key issue limiting performance right now are convexhull operations. I dont know any vectorized way to intersect many tetrahedra with some polyhedra simultaneously, probably there is some GPU way of doing it.

@AxelHenningsson
Copy link
Collaborator

Fantastic! This looks good to me in general! 🙂

A corner case for the mesh spheres came up in my testing on your fork - it is related to loss of precision in the line segment and triangle sphere wrapper functions. The fix is just to expand the sphere radii very slightly to ensure that the triangle and line segments are actually containing the nodes they claim to contain. Without this I get bounding sphere radii that change as a function of pure rigid body motions which makes testing awkward. see commit d145326

I think we should add docstrings to the functions that are user exposed before we can merge this. It would be a shame to have a bunch of docs that are lying about the code. The starting point would be to hide the functions that are not supposed to be used by the user using the underscore notation _hidden_function. For the remaining functions that changed the docs should follow the same sphinx format as before, this will enable the docs to get autogenerated. It looks like you have already added docstrings in some places?

Cheers
Axel

@AxelHenningsson
Copy link
Collaborator

AxelHenningsson commented Jun 11, 2024

@Marcraven : Please let me know if you will have the possibility to get the docs for this major pull request in shape (don't need to be perfect). It would be a shame to loose all the good progress. ❤️

Once this gets merged I think we deserve to roll up the version count and move from alpha into beta with a new releases on pypi and conda channels.

Cheers
Axel

@Marcraven
Copy link
Collaborator Author

Marcraven commented Jun 11, 2024 via email

@Marcraven
Copy link
Collaborator Author

The fix is just to expand the sphere radii very slightly to ensure that the triangle and line segments are actually containing the nodes they claim to contain.

I increased the radii by 0.1% in the cases of line and triangle segments. ¿Would that be enough?

@AxelHenningsson
Copy link
Collaborator

The fix is just to expand the sphere radii very slightly to ensure that the triangle and line segments are actually containing the nodes they claim to contain.

I increased the radii by 0.1% in the cases of line and triangle segments. ¿Would that be enough?

Sounds good!

@Marcraven Marcraven merged commit 030f530 into FABLE-3DXRD:main Jun 13, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants