Skip to content

Commit ff4276d

Browse files
Merge pull request #25 from FABLE-3DXRD/powder_simulation
Major refactor, performance upgrades, feature implementatons etc. This reflects a major version bump for xrd_simulator.
2 parents 71e42d2 + 264bc93 commit ff4276d

122 files changed

Lines changed: 15708 additions & 6642 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: ubuntu-linux conda-build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build-linux:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
max-parallel: 5
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Setup Miniconda (conda)
19+
uses: conda-incubator/setup-miniconda@v3
20+
with:
21+
activate-environment: xrd-sim-env
22+
python-version: "3.13"
23+
channels: conda-forge
24+
channel-priority: strict
25+
auto-activate-base: false
26+
auto-update-conda: true
27+
use-mamba: false
28+
cache: true
29+
30+
- name: Show conda info
31+
shell: bash -l {0}
32+
run: |
33+
conda info
34+
conda list
35+
python --version
36+
37+
- name: Install dependencies
38+
shell: bash -l {0}
39+
run: |
40+
conda env list
41+
conda install -y -n xrd-sim-env -c conda-forge netcdf4 pip
42+
python -m pip install --upgrade pip
43+
pip install -e .
44+
45+
- name: Test with pytest
46+
shell: bash -l {0}
47+
run: |
48+
conda install -y -n xrd-sim-env -c conda-forge pytest
49+
pytest -v

.github/workflows/python-package-run-tests-linux-py38.yml

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: macos conda-build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build-mac:
11+
runs-on: macos-latest
12+
strategy:
13+
max-parallel: 5
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Setup Miniconda (conda)
19+
uses: conda-incubator/setup-miniconda@v3
20+
with:
21+
activate-environment: xrd-sim-env
22+
python-version: "3.13"
23+
channels: conda-forge
24+
channel-priority: strict
25+
auto-activate-base: false
26+
auto-update-conda: true
27+
use-mamba: false
28+
cache: true
29+
30+
- name: Show conda info
31+
shell: bash -l {0}
32+
run: |
33+
conda info
34+
conda list
35+
python --version
36+
37+
- name: Install dependencies
38+
shell: bash -l {0}
39+
run: |
40+
conda env list
41+
conda install -y -n xrd-sim-env -c conda-forge pip
42+
python -m pip install --upgrade pip
43+
pip install -e .
44+
45+
- name: Test with pytest
46+
shell: bash -l {0}
47+
run: |
48+
conda install -y -n xrd-sim-env -c conda-forge pytest
49+
pytest -v

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ dmypy.json
146146
# Pyre type checker
147147
.pyre/
148148

149+
# Test reports (generated outputs)
150+
tests/test_reports/*
151+
!tests/test_reports/.gitkeep
152+
149153
# extra saved files
150154
extras/voroni_sample/
151155
extras/bench/*
@@ -154,3 +158,10 @@ cspell.json
154158
tmp_profile_dump
155159
pypi_release/
156160
extras/
161+
scripts/
162+
163+
# Example artifacts (h5, pc, xdmf files generated by examples)
164+
docs/source/examples/test_artifacts/
165+
166+
# Test reports (images and outputs from tests)
167+
tests/test_reports/

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/xrd_simulator.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PULL_REQUEST.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Powder Diffraction Simulation with PyTorch Acceleration
2+
3+
## Summary
4+
5+
This PR introduces a major overhaul of `xrd_simulator` to support **powder diffraction simulation** with **GPU-accelerated rendering** via PyTorch. The refactor enables realistic simulation of polycrystalline samples across all grain size regimes—from nanocrystals to large single crystals—with physically accurate peak broadening models.
6+
7+
## Key Features
8+
9+
### 🚀 PyTorch Backend
10+
- Complete migration from NumPy to PyTorch for all core computations
11+
- Automatic CPU/GPU device selection via new `xrd_simulator.cuda` module
12+
- Memory-efficient batch processing for millions of grains
13+
14+
### 🔬 Multi-Scale Rendering Methods
15+
New rendering system with three physically-motivated methods:
16+
17+
| Method | Grain Size | Description |
18+
|--------|------------|-------------|
19+
| `nano` | < 0.1³ µm³ | Airy disk patterns with Scherrer broadening for nanocrystals |
20+
| `micro` | 0.1³ - pixel³ µm³ | Fast Gaussian profiles for powder patterns |
21+
| `macro` | > pixel³ µm³ | 3D volume projection showing crystal morphology |
22+
| `auto` | All sizes | Automatic method selection based on grain volume |
23+
24+
### 📊 Physical Corrections
25+
- **Lorentz factor**: Proper intensity corrections for diffraction geometry
26+
- **Polarization factor**: X-ray polarization effects
27+
- **Structure factors**: Crystallographic intensity modulation
28+
- New `scattering_factors.py` module with Scherrer formula implementation
29+
30+
### ⚡ Performance Improvements
31+
- Batch rendering of 1M+ grains in seconds on GPU
32+
- Intelligent memory management with automatic batching
33+
- Removed NumPy dependency from hot paths
34+
35+
## Breaking Changes
36+
37+
- `ScatteringUnit` class removed (functionality merged into `Polycrystal` and `Detector`)
38+
- Rendering method names changed: `centroid``micro`, `profiles``nano`, `volumes``macro`
39+
- Minimum Python version: 3.10
40+
- PyTorch 2.5+ required
41+
42+
## Testing
43+
44+
-**82/82 tests passing**
45+
- New end-to-end tests for powder diffraction with pyFAI peak finding
46+
- New crystallite size broadening validation tests
47+
- CUDA device configuration tests
48+
49+
## Files Changed
50+
51+
- **100 files** modified (+10,139 / -5,764 lines)
52+
- Core modules refactored: `detector.py`, `polycrystal.py`, `motion.py`, `mesh.py`, `laue.py`
53+
- New modules: `cuda.py`, `scattering_factors.py`
54+
- Documentation rebuilt with Sphinx
55+
56+
## Dependencies Updated
57+
58+
```
59+
torch>=2.5.0
60+
numpy>=1.24.0
61+
scipy>=1.11.0
62+
pandas>=2.0.0
63+
```
64+
65+
## Commits (20)
66+
67+
1. Vectorization merge and initial refactoring
68+
2. PyTorch backend implementation through `_diffract`
69+
3. Torch/NumPy equivalence achieved for full simulation pipeline
70+
4. Multi-frame rendering support
71+
5. Powder diffraction mode with PSF convolution
72+
6. Lorentz, polarization, and structure factor corrections
73+
7. Memory-efficient batch processing
74+
8. Removed NumPy from computations
75+
9. Volume projection for macro grains
76+
10. Gaussian interpolation for micro grains
77+
11. Airy disk patterns for nano grains
78+
12. Auto mode with grain-size-based method selection
79+
13. Vectorized `RigidBodyMotion` for batch operations
80+
14. End-to-end powder diffraction tests
81+
15. CUDA device configuration module
82+
16. Scherrer formula implementation
83+
17. Documentation and docstring updates
84+
18. CI/CD workflow updates for Python 3.13
85+
19. Repository cleanup
86+
20. Sphinx documentation rebuild
87+
88+
---
89+
90+
**Ready for review and merge into `main`.**

0 commit comments

Comments
 (0)