Config Module
TOML-based configuration module for pymagnet.
Provides a declarative interface to define magnet configurations, run field calculations, and generate plots from TOML files.
Usage
from pymagnet.config import run, load, validate
Run a full simulation from a TOML file
result = run("my_config.toml")
result["results"] is a list of dicts, one per [[grid]]/[[plot]] pair
Each contains: points, field, figure (if plot enabled)
Load and validate only (no calculation)
config = load("my_config.toml")
Validate and get error list
errors = validate("my_config.toml")
load(toml_path)
Load and validate a TOML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toml_path
|
str | Path
|
Path to the TOML file. |
required |
Returns:
| Type | Description |
|---|---|
SimulationConfig
|
SimulationConfig dataclass. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If validation fails. |
Source code in src/pymagnet/config/_loader.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
run(toml_path, *, output_dir=None)
Load config, create magnets, calculate fields, and generate plots.
Each [[grid]]/[[plot]] pair produces one entry in the results lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toml_path
|
str | Path
|
Path to TOML configuration file. |
required |
output_dir
|
str | Path | None
|
Output directory for saved figures. If None, figures are saved relative to the current working directory. |
None
|
Returns:
| Type | Description |
|---|---|
dict with keys
|
config: SimulationConfig magnets: list of magnet instances results: list of dicts, one per grid/plot pair, each containing: points, field, figure (if plot enabled), saved_to (if saved) force: dict with force/torque (if force enabled) |
Source code in src/pymagnet/config/_runner.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
validate(toml_path)
Validate a TOML configuration file without running it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toml_path
|
str | Path
|
Path to the TOML file. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of error messages. Empty list means valid. |
Source code in src/pymagnet/config/__init__.py
32 33 34 35 36 37 38 39 40 41 42 | |