Commit ff6b910
authored
[ENH] Add AD showcase tests, compute_grads config option, and documentation (#1094)
# Description
Adds `compute_grads` as a configurable option in `GemPyEngineConfig` and threads it through to `BackendTensor.change_backend_gempy` when calling `compute_model`. This allows gradient computation to be explicitly controlled via the engine config rather than being hardcoded or inferred.
The nugget optimizer is updated to handle backends that may not support the `grads` keyword argument gracefully, falling back to a call without it while still setting `BackendTensor.COMPUTE_GRADS = True` directly.
A new AD (automatic differentiation) test suite is introduced under `test/test_modules/test_ad/`, covering:
- **Scenario A**: Baseline AD smoke test and regression guard — verifies gradient existence, finiteness, and deterministic norm ranges for surface points and orientations.
- **Scenario B**: Full-grid Jacobian spatial sensitivity — computes the Jacobian of the scalar field with respect to all surface-point coordinates and identifies the most influential points.
- **Scenario C**: Numerical vs. autograd gradient comparison — validates AD correctness against central finite differences with coordinate-space scaling applied.
- **Scenario D**: Multi-target derivatives — compares gradients from the scalar field and lithology block targets, including cosine similarity between gradient vectors.
- **Scenario E**: Dual contouring intermediate sensitivity — backpropagates through DC edge gradients to surface-point coordinates.
A second visualization-focused test file (`test_ad_II.py`) is added, demonstrating AD gradients rendered as PyVista glyph arrows on both a fold model and a combination model, with highlighted mesh vertices, mocked borehole cylinders, and logarithmically scaled gradient magnitude colormaps.
Relates to #
# Checklist
- [x] My code uses type hinting for function and method arguments and return values.
- [x] I have created tests which cover my code.
- [x] The test code either 1. demonstrates at least one valuable use case (e.g. integration tests)
or 2. verifies that outputs are as expected for given inputs (e.g. unit tests).
- [x] New tests pass locally with my changes.8 files changed
Lines changed: 664 additions & 17 deletions
File tree
- gempy
- API
- core/data
- modules/optimize_nuggets
- requirements
- test
- test_model_types
- test_modules/test_ad
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
| 186 | + | |
| 187 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | 8 | | |
| 9 | + | |
15 | 10 | | |
16 | 11 | | |
17 | 12 | | |
| |||
0 commit comments