|
| 1 | +# VS Code Debugging Guide for Devito |
| 2 | + |
| 3 | +## Method 1: Using VS Code Debugger with Breakpoints |
| 4 | + |
| 5 | +### Step 1: Set Up Your Debug Environment |
| 6 | +1. Open your Python file (e.g., `debug_basic_example.py`) |
| 7 | +2. Ensure you have the Python extension installed in VS Code |
| 8 | + |
| 9 | +### Step 2: Set Breakpoints |
| 10 | +- Click in the left margin next to any line number |
| 11 | +- A red dot appears = breakpoint is set |
| 12 | +- Click the red dot again to remove the breakpoint |
| 13 | + |
| 14 | +### Step 3: Start Debugging |
| 15 | +- Press `F5` or go to **Run → Start Debugging** |
| 16 | +- Select the Python debugger configuration |
| 17 | +- Or use the debug panel (Ctrl+Shift+D) |
| 18 | + |
| 19 | +### Step 4: Debug Controls |
| 20 | +When the debugger hits a breakpoint: |
| 21 | +- **F10** - Step Over (execute current line) |
| 22 | +- **F11** - Step Into (enter function calls) |
| 23 | +- **Shift+F11** - Step Out (exit current function) |
| 24 | +- **F5** - Continue (run to next breakpoint) |
| 25 | +- **Shift+F5** - Stop debugging |
| 26 | + |
| 27 | +### Step 5: Inspect Variables |
| 28 | +When paused at a breakpoint, you can: |
| 29 | +1. **Variables Panel** (left side): Shows all local/global variables |
| 30 | +2. **Watch Panel**: Add expressions to monitor (e.g., `u.data.shape`) |
| 31 | +3. **Hover**: Hover over variables in the code to see their values |
| 32 | +4. **Debug Console**: Type variable names or expressions at the bottom |
| 33 | + |
| 34 | +## Method 2: Using `breakpoint()` in Code |
| 35 | + |
| 36 | +Add `breakpoint()` anywhere in your Python code: |
| 37 | +```python |
| 38 | +# Your code here |
| 39 | +u = TimeFunction(name="u", grid=grid, time_order=2, space_order=2) |
| 40 | +breakpoint() # Execution will pause here |
| 41 | +# More code here |
| 42 | +``` |
| 43 | + |
| 44 | +When you run the script, it will pause at `breakpoint()` and drop into an interactive debugger. |
| 45 | + |
| 46 | +## Method 3: Using `pdb` Debugger Commands |
| 47 | + |
| 48 | +When paused at a `breakpoint()`, you can use these commands: |
| 49 | +- `l` or `list` - Show current code |
| 50 | +- `n` or `next` - Execute next line |
| 51 | +- `s` or `step` - Step into functions |
| 52 | +- `c` or `continue` - Continue execution |
| 53 | +- `p variable_name` - Print variable value |
| 54 | +- `pp variable_name` - Pretty print variable |
| 55 | +- `w` or `where` - Show call stack |
| 56 | +- `u` or `up` - Move up in call stack |
| 57 | +- `d` or `down` - Move down in call stack |
| 58 | +- `q` or `quit` - Quit debugger |
| 59 | + |
| 60 | +## Method 4: Debugging Jupyter Notebooks |
| 61 | + |
| 62 | +### Option A: Use VS Code's Jupyter debugging |
| 63 | +1. Open your `.ipynb` file in VS Code |
| 64 | +2. Set breakpoints by clicking in the margin of code cells |
| 65 | +3. Run the cell with `Ctrl+Enter` |
| 66 | +4. The debugger will pause at breakpoints |
| 67 | + |
| 68 | +### Option B: Use `%debug` magic command |
| 69 | +In a Jupyter cell, after an error occurs: |
| 70 | +```python |
| 71 | +%debug |
| 72 | +``` |
| 73 | +This will start the debugger at the point where the error occurred. |
| 74 | + |
| 75 | +### Option C: Use `%pdb` magic command |
| 76 | +At the start of your notebook: |
| 77 | +```python |
| 78 | +%pdb on |
| 79 | +``` |
| 80 | +This will automatically start the debugger when an exception occurs. |
| 81 | + |
| 82 | +## Useful Debugging Tips for Devito |
| 83 | + |
| 84 | +### 1. Inspect Grid Properties |
| 85 | +```python |
| 86 | +breakpoint() |
| 87 | +print(f"Grid shape: {grid.shape}") |
| 88 | +print(f"Grid dimensions: {grid.dimensions}") |
| 89 | +print(f"Grid spacing: {grid.spacing}") |
| 90 | +print(f"Grid extent: {grid.extent}") |
| 91 | +``` |
| 92 | + |
| 93 | +### 2. Inspect Function Data |
| 94 | +```python |
| 95 | +breakpoint() |
| 96 | +print(f"Function data shape: {u.data.shape}") |
| 97 | +print(f"Function data type: {u.data.dtype}") |
| 98 | +print(f"Min/Max values: {u.data.min():.6f} / {u.data.max():.6f}") |
| 99 | +print(f"Function space order: {u.space_order}") |
| 100 | +print(f"Function time order: {u.time_order}") |
| 101 | +``` |
| 102 | + |
| 103 | +### 3. Inspect Operators |
| 104 | +```python |
| 105 | +breakpoint() |
| 106 | +print(f"Operator: {op}") |
| 107 | +print(f"Operator body:\n{op.ccode}") # See generated C code |
| 108 | +``` |
| 109 | + |
| 110 | +### 4. Monitor Performance |
| 111 | +```python |
| 112 | +import time |
| 113 | +start_time = time.time() |
| 114 | +breakpoint() # Check variables before execution |
| 115 | +op(time=10, dt=0.001) |
| 116 | +breakpoint() # Check results after execution |
| 117 | +end_time = time.time() |
| 118 | +print(f"Execution time: {end_time - start_time:.4f} seconds") |
| 119 | +``` |
| 120 | + |
| 121 | +## Debugging Common Devito Issues |
| 122 | + |
| 123 | +### Issue 1: Array Shape Mismatches |
| 124 | +```python |
| 125 | +breakpoint() |
| 126 | +print(f"Expected shape: {expected_shape}") |
| 127 | +print(f"Actual shape: {actual_array.shape}") |
| 128 | +print(f"Array strides: {actual_array.strides}") |
| 129 | +``` |
| 130 | + |
| 131 | +### Issue 2: Boundary Conditions |
| 132 | +```python |
| 133 | +breakpoint() |
| 134 | +print(f"Boundary data: {u.data[:, 0]}") # Check boundary values |
| 135 | +print(f"Interior data sample: {u.data[50:55, 50:55]}") # Check interior |
| 136 | +``` |
| 137 | + |
| 138 | +### Issue 3: Time Stepping Issues |
| 139 | +```python |
| 140 | +breakpoint() |
| 141 | +print(f"Current time index: {u._time_position}") |
| 142 | +print(f"Time buffer content: {u.data}") |
| 143 | +``` |
0 commit comments