|
| 1 | +```{eval-rst} |
| 2 | +.. currentmodule:: pymadng |
| 3 | +``` |
| 4 | + |
| 5 | +# Advanced Features in PyMAD-NG |
| 6 | + |
| 7 | +This section covers some of the most powerful capabilities in PyMAD-NG. These features allow you to create scalable and complex accelerator workflows by combining the performance of MAD-NG with Python's expressiveness. |
| 8 | + |
| 9 | +```{contents} |
| 10 | +:depth: 1 |
| 11 | +:local: |
| 12 | +``` |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Understanding `_last[]` Temporary Variables |
| 17 | + |
| 18 | +In MAD-NG, when a command returns a value, it is not automatically captured unless explicitly assigned. PyMAD-NG handles this by assigning results to a set of reserved variables: `_last[1]`, `_last[2]`, etc. |
| 19 | + |
| 20 | +These are managed internally by PyMAD-NG using a helper class {class}`madp_last.last_counter`, and accessed in Python via references. This allows expressions like: |
| 21 | + |
| 22 | +```python |
| 23 | +result = mad.math.sqrt(2) + mad.math.log(10) |
| 24 | +``` |
| 25 | + |
| 26 | +Behind the scenes, each intermediate operation is stored in a new `_last[i]` reference, then combined. You can access or evaluate the result using `.eval()`: |
| 27 | + |
| 28 | +```python |
| 29 | +print(result.eval()) |
| 30 | +``` |
| 31 | + |
| 32 | +These temporary variables are recycled unless manually stored using: |
| 33 | + |
| 34 | +```python |
| 35 | +mad["my_var"] = result |
| 36 | +``` |
| 37 | + |
| 38 | +This is particularly useful in expressions, multi-step computations, and avoiding naming clutter. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Function and Object References in MAD-NG |
| 43 | + |
| 44 | +In PyMAD-NG, accessing or calling any MAD-NG function or object returns a Python reference to that MAD-NG entity, rather than immediately executing or resolving it. This enables symbolic chaining and precise control over execution. |
| 45 | + |
| 46 | +### Example: |
| 47 | +```python |
| 48 | +r = mad.math.exp(1) |
| 49 | +print(type(r)) # high_level_mad_ref |
| 50 | +print(r.eval()) # 2.718... |
| 51 | +``` |
| 52 | + |
| 53 | +You can delay evaluation until needed, allowing reuse: |
| 54 | +```python |
| 55 | +mad["result"] = mad.math.log(10) + mad.math.sin(1) |
| 56 | +``` |
| 57 | + |
| 58 | +This keeps Python responsive and lets MAD-NG do the heavy lifting. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Real-Time Feedback with Python During Matching |
| 63 | + |
| 64 | +MAD-NG supports callbacks and iterative evaluations, which can be tied into Python logic. One common use is during `match` procedures, where you want to receive intermediate updates. |
| 65 | + |
| 66 | +### Example Workflow: |
| 67 | +In MAD: |
| 68 | +```lua |
| 69 | +function twiss_and_send() |
| 70 | + local tbl, flow = twiss {sequence=seq, method=4} |
| 71 | + py:send({tbl.s, tbl.beta11}) |
| 72 | + return tbl, flow |
| 73 | +end |
| 74 | +``` |
| 75 | + |
| 76 | +In Python: |
| 77 | +```python |
| 78 | +mad.match( |
| 79 | + command=mad.twiss_and_send, |
| 80 | + variables=[...], |
| 81 | + equalities=[...], |
| 82 | + objective={"fmin": 1e-3}, |
| 83 | + maxcall=100 |
| 84 | +) |
| 85 | + |
| 86 | +while True: |
| 87 | + data = mad.recv() |
| 88 | + if data is None: |
| 89 | + break |
| 90 | + update_plot(data) |
| 91 | +``` |
| 92 | + |
| 93 | +This is ideal for live visualization, feedback loops, or diagnostics during optimization. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Using PyMAD-NG with Multiprocessing |
| 98 | + |
| 99 | +Because PyMAD-NG communicates with MAD-NG via pipes (not shared memory), you can launch multiple independent MAD processes using `os.fork()` or `multiprocessing`. |
| 100 | + |
| 101 | +### When to Use This: |
| 102 | +- Run parallel simulations or parameter scans |
| 103 | +- Avoid reloading large sequences repeatedly |
| 104 | + |
| 105 | +### Example: |
| 106 | +```python |
| 107 | +import os |
| 108 | +if os.fork() == 0: |
| 109 | + mad = MAD() |
| 110 | + mad.send("... long running setup ...") |
| 111 | + os._exit(0) |
| 112 | +``` |
| 113 | + |
| 114 | +Each process maintains its own MAD instance and data pipeline. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## Loading and Using External MAD Files and Modules |
| 119 | + |
| 120 | +MAD-X and MAD-NG models often consist of `.seq`, `.mad`, `.madx`, or `.str` files. You can load these via the high-level interface: |
| 121 | + |
| 122 | +```python |
| 123 | +mad.MADX.load("'lhc.seq'", "'lhc.mad'") |
| 124 | +mad.load("MADX", "lhcb1") |
| 125 | +``` |
| 126 | + |
| 127 | +Or load additional MAD-NG modules: |
| 128 | +```python |
| 129 | +mad.load("MAD.gphys", "melmcol") |
| 130 | +``` |
| 131 | + |
| 132 | +This loads extended libraries for magnet properties, tracking models, or optics algorithms. |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Exporting Results for External Use |
| 137 | + |
| 138 | +After running a Twiss or Survey, the results are stored in an `mtable`, which can be exported to a TFS file: |
| 139 | + |
| 140 | +```python |
| 141 | +mad.tbl.write("'results.tfs'", mad.quote_strings(["s", "beta11", "mu1"])) |
| 142 | +``` |
| 143 | + |
| 144 | +You can read this file with `tfs-pandas` or use it as input to another tool. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## Combining with NumPy and Pandas |
| 149 | + |
| 150 | +PyMAD-NG integrates cleanly with Python’s data ecosystem: |
| 151 | + |
| 152 | +- Pass `numpy` arrays to MAD-NG using {func}`MAD.send` |
| 153 | +- Use {func}`.to_df` on MAD tables to get Pandas DataFrames |
| 154 | +- Use `tfs-pandas` for rich metadata support |
| 155 | + |
| 156 | +### Example: |
| 157 | +```python |
| 158 | +import numpy as np |
| 159 | +mad.send("my_array = py:recv()") |
| 160 | +mad.send(np.linspace(0, 1, 100)) |
| 161 | +``` |
| 162 | + |
| 163 | +This allows direct use of scientific computation tools in tandem with accelerator modeling. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Managing Larger Workflows |
| 168 | + |
| 169 | +PyMAD-NG supports: |
| 170 | +- Loading full files with `mad.loadfile("mysetup.mad")` |
| 171 | +- Organising expressions using Python variables |
| 172 | +- Retaining command history using: |
| 173 | + |
| 174 | +```python |
| 175 | +print(mad.history()) |
| 176 | +``` |
| 177 | + |
| 178 | +For clean resource management, always use context blocks: |
| 179 | +```python |
| 180 | +with MAD() as mad: |
| 181 | + mad.MADX.load("'lhc.seq'", "'lhc.mad'") |
| 182 | +``` |
| 183 | + |
| 184 | +This ensures the MAD process is correctly shut down when finished. |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Summary of Advanced Features |
| 189 | + |
| 190 | +| Feature | Purpose | |
| 191 | +|---------------------------------|--------------------------------------------------| |
| 192 | +| `_last[]` Variables | Track intermediate return values symbolically | |
| 193 | +| Reference Objects | Access MAD-NG objects with delayed evaluation | |
| 194 | +| Matching Feedback | Monitor intermediate results during match | |
| 195 | +| Multiprocessing | Run multiple MAD-NG simulations in parallel | |
| 196 | +| File and Module Loading | Import sequences, optics files, and Lua modules | |
| 197 | +| Table Export | Write TFS files from MAD tables | |
| 198 | +| NumPy / Pandas Interoperability | Pass data between Python and MAD-NG seamlessly | |
| 199 | +| Project Structuring | Use {func}`MAD.loadfile`, {func}`MAD.history`, and `with` block | |
| 200 | + |
| 201 | +These tools are designed to give you complete control over your simulations while staying fast and maintainable. |
| 202 | + |
| 203 | +Next: head over to **Debugging & Troubleshooting** to diagnose and resolve common issues in real-world workflows. |
| 204 | + |
0 commit comments