|
| 1 | +# CLAUDE.md |
| 2 | +Claude Code Operating Rules for This Repository |
| 3 | + |
| 4 | +This file defines **hard constraints** and **preferred behaviors** for Claude |
| 5 | +working in this repository. These rules exist to protect numerical correctness, |
| 6 | +performance, and long-term maintainability of a high-order spectral element |
| 7 | +codebase. |
| 8 | + |
| 9 | +Follow all rules below unless explicitly instructed otherwise by a human |
| 10 | +maintainer. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## 1. Project Purpose |
| 15 | + |
| 16 | +This project implements **high-order spectral element methods** for solving |
| 17 | +systems of conservation laws (e.g., Euler, shallow water, Maxwell, MHD) on |
| 18 | +structured and unstructured meshes. |
| 19 | + |
| 20 | +Primary goals: |
| 21 | +- Numerical correctness and stability |
| 22 | +- Conservation properties |
| 23 | +- High performance on CPUs and GPUs |
| 24 | +- MPI scalability |
| 25 | + |
| 26 | +Changes must NOT degrade: |
| 27 | +- Accuracy order |
| 28 | +- Stability properties |
| 29 | +- Parallel scaling |
| 30 | +- Memory behavior |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## 2. Fortran Standards & Toolchain |
| 35 | + |
| 36 | +### Language Standard |
| 37 | +- Target: **Fortran 2008** |
| 38 | +- Code must remain compatible with: |
| 39 | + - gfortran ≥ 11 |
| 40 | + - ifx |
| 41 | + - nvfortran |
| 42 | + - amdflang |
| 43 | + |
| 44 | +### Prohibited Language Features |
| 45 | +Do NOT introduce: |
| 46 | +- Coarrays |
| 47 | +- Fortran 2018+ features |
| 48 | +- Compiler-specific extensions |
| 49 | +- Automatic polymorphism in performance-critical paths |
| 50 | + |
| 51 | +### Formatting & Conventions |
| 52 | +- Free-form source |
| 53 | +- `implicit none` required in all program units |
| 54 | +- Explicit `intent(in|out|inout)` on all dummy arguments |
| 55 | +- Lowercase keywords preferred |
| 56 | +- Line length ≤ 100 characters |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## 3. Numerical & Algorithmic Constraints |
| 61 | + |
| 62 | +### Hard Rules |
| 63 | +- Do NOT change the mathematical formulation without approval |
| 64 | +- Do NOT change discretization order |
| 65 | +- Do NOT change basis, quadrature, or nodal ordering |
| 66 | +- Do NOT reorder floating-point reductions |
| 67 | +- Do NOT alter time integration schemes |
| 68 | + |
| 69 | +### Floating-Point Behavior |
| 70 | +- Bitwise reproducibility may be required |
| 71 | +- Preserve operation ordering in loops |
| 72 | +- Avoid algebraic “simplifications” unless mathematically justified |
| 73 | + |
| 74 | +### Array Semantics |
| 75 | +- Do NOT replace explicit loops with array syntax unless equivalence is proven |
| 76 | +- Avoid implicit temporaries |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## 4. Performance Rules (Critical) |
| 81 | + |
| 82 | +This is an HPC codebase. Performance regressions are unacceptable. |
| 83 | + |
| 84 | +### Memory |
| 85 | +- Avoid temporary allocations in hot paths |
| 86 | +- No automatic arrays in tight loops |
| 87 | +- No hidden allocations via array slicing |
| 88 | + |
| 89 | +### Loops |
| 90 | +- Preserve loop ordering for cache locality |
| 91 | +- Do NOT replace DO loops with WHERE / FORALL |
| 92 | +- Vectorization-friendly structure must be preserved |
| 93 | + |
| 94 | +### Abstraction |
| 95 | +- Do NOT introduce runtime polymorphism in kernels |
| 96 | +- Avoid excessive modularization inside hot loops |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## 5. Parallel Programming |
| 101 | + |
| 102 | +### MPI |
| 103 | +- MPI calls must remain explicit |
| 104 | +- Do NOT introduce blocking collectives inside time-stepping loops |
| 105 | +- Do NOT change communicator usage |
| 106 | +- Preserve rank-local data ownership |
| 107 | + |
| 108 | +### OpenMP / GPU |
| 109 | +- Preserve OpenMP semantics |
| 110 | +- Do NOT move data regions without explicit instruction |
| 111 | +- GPU kernels must preserve memory access patterns |
| 112 | +- No implicit host/device transfers |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## 6. Code Organization & APIs |
| 117 | + |
| 118 | +### File & Module Structure |
| 119 | +- Do NOT rename modules |
| 120 | +- Do NOT move files between directories |
| 121 | +- Do NOT change public interfaces without approval |
| 122 | +- Preserve module dependency order |
| 123 | + |
| 124 | +### Public APIs |
| 125 | +- Public procedures are considered **stable** |
| 126 | +- Backward compatibility is required unless stated otherwise |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## 7. Testing & Validation |
| 131 | + |
| 132 | +### Required |
| 133 | +- All existing regression tests must pass |
| 134 | +- Do NOT modify reference output files |
| 135 | +- Numerical differences must be justified |
| 136 | + |
| 137 | +### New Code |
| 138 | +- New features require: |
| 139 | + - A test case |
| 140 | + - Clear validation criteria |
| 141 | +- MPI tests must work on ≥ 2 ranks |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 8. Documentation & Comments |
| 146 | + |
| 147 | +### Preserve Scientific Meaning |
| 148 | +- Do NOT remove comments describing: |
| 149 | + - Equations |
| 150 | + - Algorithms |
| 151 | + - Numerical assumptions |
| 152 | + |
| 153 | +### New Routines |
| 154 | +Must include: |
| 155 | +- Mathematical description |
| 156 | +- Variable meaning and units |
| 157 | +- Expected input ranges |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## 9. Code Formatting |
| 162 | + |
| 163 | +All Fortran source must be formatted with [`fprettify`](https://pypi.org/project/fprettify/) using the project's `fprettify.config`. PRs are checked for formatting before any other tests run. |
| 164 | + |
| 165 | +To format all source files manually: |
| 166 | + |
| 167 | +```shell |
| 168 | +fprettify './src/' --config-file ./fprettify.config --recursive --case 1 1 1 1 |
| 169 | +fprettify './test/' --config-file ./fprettify.config --recursive --case 1 1 1 1 |
| 170 | +fprettify './examples/' --config-file ./fprettify.config --recursive --case 1 1 1 1 |
| 171 | +``` |
| 172 | + |
| 173 | +Alternatively, install the provided `pre-commit` hook to apply formatting automatically on each commit: |
| 174 | + |
| 175 | +```shell |
| 176 | +pip install pre-commit fprettify |
| 177 | +pre-commit install # run from repository root |
| 178 | +``` |
| 179 | + |
| 180 | +When editing Fortran files, apply `fprettify` before committing. Do NOT manually reformat code by hand in ways that deviate from `fprettify` output. |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## 10. Prohibited Actions (Explicit) |
| 185 | + |
| 186 | +Do NOT: |
| 187 | +- Rewrite code in another language |
| 188 | +- Convert procedural code to OO Fortran |
| 189 | +- Replace MPI with coarrays |
| 190 | +- Introduce external dependencies |
| 191 | +- “Modernize” syntax without benefit |
| 192 | +- Delete legacy code without explanation |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +## 11. Domain-Specific Assumptions |
| 197 | + |
| 198 | +- Grid indexing follows project conventions (do NOT reorder indices) |
| 199 | +- Jacobians and metric terms are precomputed |
| 200 | +- Flux routines assume nodal basis ordering |
| 201 | +- Element-local operations must remain element-local |
| 202 | +- Halo exchange patterns are fixed |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## 12. Preferred Behavior |
| 207 | + |
| 208 | +DO: |
| 209 | +- Ask before changing algorithms |
| 210 | +- Explain numerical and performance implications |
| 211 | +- Provide minimal diffs |
| 212 | +- Reference existing patterns in the codebase |
| 213 | +- Flag any uncertainty explicitly |
| 214 | + |
| 215 | +DO NOT: |
| 216 | +- Make large refactors unless requested |
| 217 | +- Assume intent beyond the explicit request |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## 13. When in Doubt |
| 222 | + |
| 223 | +If a change could affect: |
| 224 | +- Numerical accuracy |
| 225 | +- Stability |
| 226 | +- Performance |
| 227 | +- Parallel behavior |
| 228 | + |
| 229 | +STOP and ask for clarification. |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +End of CLAUDE.md |
| 234 | + |
0 commit comments