Core Containers¶
The data structures every other part of pypic is written against.
FieldDataset is the central type: readers produce it, derived
quantities and selections consume it, and the I/O layer serializes it.
Dependency direction is one-way — grid ← containers ← dataset —
so the geometry description never has to know about the data it
describes.
| Type | Module | Role |
|---|---|---|
FieldDataset |
pypic.dataset |
Field arrays plus grid, normalization, and species metadata. Wraps xarray.Dataset; computation happens on raw NumPy. |
GridInfo |
pypic.grid |
Dimensions, spacing, extent, and geometry of the co-located grid. |
SimulationConfig |
pypic.containers |
Validated simulation.toml contents in internal form. |
TabularData |
pypic.containers |
Time-series and other column-oriented output (probes, energy histories). |
ParticleData |
pypic.containers |
Per-macroparticle positions, velocities, and weights. |
StaggerInfo |
pypic.containers |
Provenance record of the source code's mesh convention. Readers destagger on load; this documents what they destaggered from. |
FieldDataset¶
dataset
¶
FieldDataset: the xarray-backed container every reader returns.
compute and reductions sit above this module in the dependency
order (grid ← containers ← dataset ← everything else), so the methods
that delegate to them import inside the method body. Every other import
is at module scope.
FieldDataset
¶
Universal container for simulation field data.
Wraps an xr.Dataset with grid metadata, normalization info, species
definitions, and geometry-aware field aliases (e.g. "Bx" → "B_1").
The full alias hierarchy — geometry/Cartesian aliases, species-name
aliases (n_electrons→n_s0), and the e/i library-convenience
forms (Pe↔P_s0) — is documented at docs/aliases.md. The
e/i shortcuts are pypic-only ergonomics and are not part of the
cross-tool schema contract in docs/schema.md.
Use from_arrays to construct from raw NumPy arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The underlying xarray dataset. |
required |
grid
|
GridInfo
|
Grid metadata. |
required |
normalization
|
Normalization
|
Unit normalization for this data. |
required |
species
|
Sequence[SpeciesInfo] | None
|
Species definitions, if applicable. |
None
|
physics
|
PhysicsParams | None
|
Physics parameters (adiabatic index, speed of light, etc.). |
None
|
metadata
|
Mapping[str, Any] | None
|
Arbitrary metadata (run name, code version, etc.). |
None
|
aliases
|
Mapping[str, str] | None
|
Extra field-name aliases merged with geometry defaults. Aliases whose canonical target is absent from the dataset are silently dropped (they become inactive). |
None
|
frame
|
str
|
Name of the reference frame these arrays are expressed in.
Defaults to |
'simulation'
|
transforms
|
Mapping[str, FrameTransform] | None
|
Frame transforms reachable from frame, keyed by target-frame
name. Consumed by |
None
|
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(4, 3), spacing=(1.0, 1.0), origin=(0.0, 0.0),
... geometry=CARTESIAN,
... )
>>> fields = {"B_1": np.ones((4, 3)), "rho_c": np.zeros((4, 3))}
>>> ds = FieldDataset.from_arrays(fields, grid, Normalization.identity())
>>> ds["B_1"].shape
(4, 3)
>>> ds.has_field("Bx")
True
Source code in src/pypic/dataset.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 | |
grid
property
¶
Grid metadata.
normalization
property
¶
Unit normalization.
species
property
¶
Species definitions.
physics
property
¶
Physics parameters.
metadata
property
¶
Arbitrary metadata (read-only view).
time
property
¶
Snapshot time in code units, or None when unknown.
metadata["time"] when the reader recorded one, otherwise
metadata["step"] * grid.dt when both are known.
Examples:
>>> import numpy as np
>>> grid = GridInfo(dimensions=(2,), spacing=(1.0,), dt=0.5)
>>> FieldDataset.from_arrays(
... {"B_1": np.zeros(2)}, grid, metadata={"step": 4}
... ).time
2.0
>>> FieldDataset.from_arrays({"B_1": np.zeros(2)}, grid).time is None
True
aliases
property
¶
Active field-name aliases (read-only view).
frame
property
¶
Current reference frame label.
transforms
property
¶
Registered frame transforms (read-only view).
available_frames
property
¶
Frame names reachable via registered transforms.
xr
property
¶
Raw xarray Dataset.
from_arrays(fields, grid, normalization=None, *, species=None, physics=None, metadata=None, aliases=None, frame='simulation', transforms=None, coords=None, strict_fields=True)
classmethod
¶
Build a FieldDataset from a dict of NumPy arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
Mapping[str, FloatArray]
|
Mapping of field names to arrays. Shapes must match
|
required |
grid
|
GridInfo
|
Grid metadata. |
required |
normalization
|
Normalization | None
|
Unit normalization. Defaults to
|
None
|
species
|
Sequence[SpeciesInfo] | None
|
Species definitions, if applicable. |
None
|
physics
|
PhysicsParams | None
|
Physics parameters. |
None
|
metadata
|
Mapping[str, Any] | None
|
Arbitrary metadata. |
None
|
aliases
|
Mapping[str, str] | None
|
Extra field-name aliases. |
None
|
frame
|
str
|
Name of the reference frame these arrays are expressed in.
Defaults to |
'simulation'
|
transforms
|
Mapping[str, FrameTransform] | None
|
Frame transforms reachable from frame, keyed by
target-frame name. Consumed by |
None
|
coords
|
Mapping[str, FloatArray] | None
|
Coordinate arrays keyed by axis name, replacing the uniform
ones derived from grid on those axes. For non-uniform
meshes whose |
None
|
strict_fields
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
|
Raises:
| Type | Description |
|---|---|
UnknownFieldError
|
When |
ValueError
|
When coords names an axis the grid does not have. |
Examples:
>>> import numpy as np
>>> grid = GridInfo(dimensions=(2,), spacing=(1.0,))
>>> ds = FieldDataset.from_arrays({"B_1": np.array([1.0, 2.0])}, grid)
>>> ds["B_1"]
array([1., 2.])
Source code in src/pypic/dataset.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
transform_to(target)
¶
Transform this dataset to a different coordinate reference frame.
Applies an affine transformation: translates the grid origin, scales coordinates, and rotates vector field components and pressure tensors. Scalar values pass through unchanged. When the rotation includes an axis swap or reflection, field arrays are copied to a contiguous buffer in the new axis order; pure translations and identity rotations only touch metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str or FrameTransform
|
Target frame name (looked up in the transform registry) or
a |
required |
Returns:
| Type | Description |
|---|---|
FieldDataset
|
New dataset in the target frame. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no transform path exists from the current frame. |
KeyError
|
If target is a string and no transforms are registered. |
Examples:
A -90° rotation about \(z\) sends \(\hat{x}\) to \(-\hat{y}\), and swaps the two grid axes with it:
>>> import numpy as np
>>> from pypic.coordinates import FrameTransform
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> gsm = FrameTransform(
... "simulation", "GSM",
... rotation=((0.0, 1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.ones((4, 3, 2)),
... "B_2": np.zeros((4, 3, 2)),
... "B_3": np.zeros((4, 3, 2))},
... grid, Normalization.identity(), transforms={"GSM": gsm},
... )
>>> rotated = ds.transform_to("GSM")
>>> rotated.frame, rotated.grid.dimensions
('GSM', (3, 4, 2))
>>> float(rotated["B_2"][0, 0, 0])
-1.0
Source code in src/pypic/dataset.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
resolve_key(key)
¶
Resolve a field key through aliases to the canonical name.
Returns the canonical (data-vars) name for key, which may itself
be a canonical name or an alias from this dataset's alias table.
Raises KeyError (with close-match suggestions) if key
matches neither.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Canonical field name or alias. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Canonical name in |
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(2,), spacing=(1.0,), origin=(0.0,),
... geometry=CARTESIAN,
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.ones(2)}, grid, Normalization.identity()
... )
>>> ds.resolve_key("Bx") # Cartesian alias
'B_1'
>>> ds.resolve_key("B_1") # canonical
'B_1'
Source code in src/pypic/dataset.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
__getitem__(key)
¶
Return field data as a NumPy array (zero-copy when possible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Canonical field name or alias. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
|
Source code in src/pypic/dataset.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
has_field(key)
¶
Check whether a field exists (canonical or alias).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Field name to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(2,), spacing=(1.0,), origin=(0.0,),
... geometry=CARTESIAN,
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.array([1.0, 2.0])}, grid, Normalization.identity(),
... )
>>> ds.has_field("B_1"), ds.has_field("Bx"), ds.has_field("rho")
(True, True, False)
Source code in src/pypic/dataset.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | |
field_names()
¶
Return canonical field names (no aliases).
Returns:
| Type | Description |
|---|---|
list[str]
|
|
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(2,), spacing=(1.0,), origin=(0.0,),
... geometry=CARTESIAN,
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.array([1.0, 2.0]), "rho_c": np.array([0.5, 0.5])},
... grid, Normalization.identity(),
... )
>>> sorted(ds.field_names())
['B_1', 'rho_c']
Source code in src/pypic/dataset.py
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | |
select_fields(names)
¶
Return a new FieldDataset containing only the specified fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
Iterable[str]
|
Field names to keep (canonical or alias). |
required |
Returns:
| Type | Description |
|---|---|
FieldDataset
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If any name is not found. |
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(2,), spacing=(1.0,), origin=(0.0,),
... geometry=CARTESIAN,
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.array([1.0, 2.0]), "B_2": np.array([3.0, 4.0]),
... "rho_c": np.array([0.5, 0.5])},
... grid, Normalization.identity(),
... )
>>> sub = ds.select_fields(["rho_c", "B_1"])
>>> sub.field_names()
['rho_c', 'B_1']
Source code in src/pypic/dataset.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 | |
sel(indexers=None, *, method=None, **kwargs)
¶
Label-based selection, returning a new FieldDataset.
Accepts indexers as a dict (useful for Unicode dim names like
"θ" that can't be keyword arguments) and/or **kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indexers
|
dict[str, Any] | None
|
Dimension-name → label mapping. |
None
|
method
|
str | None
|
Passed to |
None
|
**kwargs
|
Any
|
Additional dimension selections. |
{}
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
|
Examples:
Cell centres sit at half-integer positions, so a label that
falls between them needs method="nearest":
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.arange(24.0).reshape(4, 3, 2)},
... grid, Normalization.identity(),
... )
>>> midplane = ds.sel({"z": 1.4}, method="nearest")
>>> midplane.grid.dimensions, midplane.grid.surviving_axis_names
((4, 3), ('x', 'y'))
Source code in src/pypic/dataset.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 | |
compute(name)
¶
Compute a derived quantity by name. Returns code units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Derived quantity name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
Computed array in code units. |
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0),
... geometry=CARTESIAN,
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.full((4,3,2), 3.0),
... "B_2": np.full((4,3,2), 4.0),
... "B_3": np.zeros((4,3,2))},
... grid, Normalization.identity(),
... )
>>> ds.compute("|B|")[0, 0, 0]
np.float64(5.0)
Source code in src/pypic/dataset.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 | |
with_field(name, data, quantity_type=None, *, long_name='', latex='')
¶
Return a new FieldDataset with an additional custom field.
The field's quantity_type is stored in xarray attrs, so
in_si(), field_info(), and unit conversion work without
global register_field() calls.
When quantity_type is None, metadata is looked up from the
field registry automatically. For unregistered fields, provide
quantity_type explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field name. |
required |
data
|
FloatArray
|
Array matching the grid dimensions. |
required |
quantity_type
|
QuantityType | str | None
|
Physical quantity type (e.g. |
None
|
long_name
|
str
|
Human-readable label for plot titles. |
''
|
latex
|
str
|
LaTeX symbol for plot labels. |
''
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
New dataset with the field added. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If quantity_type is not recognized, or is |
Source code in src/pypic/dataset.py
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |
with_derived(*names)
¶
Return a new dataset with derived fields computed and stored.
For each name, computes the field (if not already present), looks up metadata from the field registry, and attaches it with full metadata. Fields already in the dataset are skipped.
When a vector-component recipe is encountered (e.g. "S_1"
from Poynting flux), all sibling components ("S_2", "S_3")
are computed from a single function call and stored together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*names
|
str
|
Derived quantity names (e.g. |
()
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
New dataset with the computed fields attached. |
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(
... dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0),
... geometry=CARTESIAN,
... )
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.full((4,3,2), 3.0),
... "B_2": np.full((4,3,2), 4.0),
... "B_3": np.zeros((4,3,2))},
... grid, Normalization.identity(),
... )
>>> ds = ds.with_derived("|B|")
>>> ds.has_field("|B|")
True
>>> ds["|B|"][0, 0, 0]
np.float64(5.0)
Source code in src/pypic/dataset.py
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | |
field_info(name)
¶
Return metadata for a field or derived quantity.
Checks xarray DataArray attrs first (set by with_field()
or from_arrays()), then falls back to the global registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field or derived quantity name (canonical or alias). |
required |
Returns:
| Type | Description |
|---|---|
FieldInfo
|
|
Source code in src/pypic/dataset.py
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 | |
in_si(name)
¶
Return a field or derived quantity in SI units.
Checks xarray DataArray attrs first (set by with_field()
or from_arrays()), then falls back to the global registry.
For fields that have been reduced via
pypic.reductions.reduce with
reduction="integrate",
the attrs["reduction"]["length_axes"] provenance stamp
records how many length-dimension factors the integration
added; in_si multiplies the registry SI factor by
normalization.length_ref ** length_axes so column
densities, line-of-sight integrals, etc. come out in the
right SI units (e.g. column density m\ :sup:-2 instead of
m\ :sup:-3). Weighted integrate does not stamp
length_axes because the length factor cancels in
∫ f w dx / ∫ w dx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field or derived quantity name. |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
Values in SI units. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no SI conversion is registered for name — a field
carrying no |
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> norm = Normalization.mhd_standard(6.371e6, 1.67e-17, 5.0e-9)
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.full((4, 3, 2), 2.0)}, grid, norm,
... )
>>> float(ds.in_si("B_1")[0, 0, 0]) # 2 code units at B_ref = 5 nT
1e-08
Source code in src/pypic/dataset.py
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 | |
in_units(name, unit_str)
¶
Return a field or derived quantity in display units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field or derived quantity name. |
required |
unit_str
|
str
|
Target unit (e.g. |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
Values in the requested units. |
Notes
Temperature is stored in energy units (J), so use
in_units(name, "eV") (or "keV", "MeV") for the
plasma working unit, or in_units(name, "K") for the
Boltzmann-factor-converted form. Magnetic field defaults to
T in SI; in_units(name, "nT") is the space-physics
idiom. See _DISPLAY_UNITS in pypic.units for the
full vocabulary.
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> norm = Normalization.mhd_standard(6.371e6, 1.67e-17, 5.0e-9)
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.full((4, 3, 2), 2.0)}, grid, norm,
... )
>>> float(ds.in_units("B_1", "nT")[0, 0, 0])
10.0
Source code in src/pypic/dataset.py
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 | |
isel(indexers=None, **kwargs)
¶
Integer-index selection, returning a new FieldDataset.
Accepts indexers as a dict and/or **kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indexers
|
dict[str, Any] | None
|
Dimension-name → integer index or slice mapping. |
None
|
**kwargs
|
Any
|
Additional dimension selections. |
{}
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
|
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.arange(24.0).reshape(4, 3, 2)},
... grid, Normalization.identity(),
... )
>>> face = ds.isel(x=0)
>>> face.grid.dimensions, face.grid.surviving_axis_names
((3, 2), ('y', 'z'))
Source code in src/pypic/dataset.py
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 | |
where(cond, other=np.nan)
¶
Mask fields where cond is False.
Returns a new FieldDataset with the same grid shape.
Points where cond is False are set to other (default
NaN). Useful for spatial masks (spherical cutouts, boundary
regions) without reducing dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cond
|
BoolArray
|
Boolean array with shape matching the grid dimensions.
|
required |
other
|
float
|
Fill value for masked points (default |
nan
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
|
Examples:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.arange(24.0).reshape(4, 3, 2)},
... grid, Normalization.identity(),
... )
>>> x, _, _ = np.meshgrid(*grid.coordinate_arrays(), indexing="ij")
>>> inner = ds.where(x < 2.0)
>>> inner["B_1"].shape, int(np.isnan(inner["B_1"]).sum())
((4, 3, 2), 12)
Source code in src/pypic/dataset.py
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 | |
reduce(axis, **kwargs)
¶
Reduce this dataset along one or more axes.
See pypic.reduce. Convenience method equivalent to
pypic.reduce(self, axis, **kwargs). Enables fluent
chaining: ds.where(mask).reduce("z", reduction="integrate").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
str or tuple of str
|
Surviving-axis name(s) to reduce away. |
required |
**kwargs
|
Any
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
FieldDataset
|
With axis (or every name in the tuple) removed from the grid. |
Examples:
Integrating a unit field over two cells of unit spacing gives the trapezoidal path length between the two cell centres:
>>> import numpy as np
>>> from pypic.units import Normalization
>>> grid = GridInfo(dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0))
>>> ds = FieldDataset.from_arrays(
... {"B_1": np.ones((4, 3, 2))}, grid, Normalization.identity(),
... )
>>> column = ds.reduce("z", reduction="integrate")
>>> column.grid.dimensions, float(column["B_1"][0, 0])
((4, 3), 1.0)
Source code in src/pypic/dataset.py
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 | |
GridInfo¶
grid
¶
GridInfo: the structured-grid metadata every FieldDataset carries.
GridInfo
dataclass
¶
Structured grid metadata for 1D/2D/3D simulation domains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
tuple[int, ...]
|
Number of cells along each axis. |
required |
spacing
|
tuple[float, ...]
|
Cell size along each axis in code units. |
required |
origin
|
tuple[float, ...]
|
Lower-left corner coordinate of the domain. |
()
|
geometry
|
CoordinateGeometry
|
Coordinate system (Cartesian, spherical, cylindrical). |
CARTESIAN
|
dt
|
float | None
|
Timestep size in code units, if known. |
None
|
boundary
|
tuple[str, ...] | None
|
Boundary condition per axis (e.g. |
None
|
Examples:
>>> grid = GridInfo(
... dimensions=(4,), spacing=(0.5,), origin=(0.0,),
... geometry=CARTESIAN,
... )
>>> grid.coordinate_arrays()[0]
array([0.25, 0.75, 1.25, 1.75])
Source code in src/pypic/grid.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
surviving_axis_names
property
¶
Axis names for the current dimensions.
After slicing, returns only the names of axes that survived
(e.g. ("x", "z") after removing the y-axis). When no
slicing has occurred, returns the first ndim names from
the geometry.
Examples:
>>> grid = GridInfo(
... dimensions=(4, 3, 2), spacing=(1.0, 1.0, 1.0),
... geometry=CARTESIAN,
... )
>>> grid.surviving_axis_names
('x', 'y', 'z')
>>> import copy
>>> sliced = copy.replace(
... grid, dimensions=(4, 2), spacing=(1.0, 1.0),
... origin=(0.0, 0.0), surviving_axes=(0, 2),
... )
>>> sliced.surviving_axis_names
('x', 'z')
coordinate_arrays()
¶
Cell-centered coordinate arrays for each axis.
Returns:
| Type | Description |
|---|---|
tuple[FloatArray, ...]
|
One 1-D array per axis: |
Examples:
>>> grid = GridInfo(
... dimensions=(3, 2), spacing=(1.0, 2.0), origin=(0.0, 0.0),
... geometry=CARTESIAN,
... )
>>> x, y = grid.coordinate_arrays()
>>> x
array([0.5, 1.5, 2.5])
>>> y
array([1., 3.])
Source code in src/pypic/grid.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
Simulation, particle, and tabular containers¶
containers
¶
Immutable data containers: SimulationConfig, TabularData, ParticleData.
StaggerInfo
dataclass
¶
Provenance record of the original grid stagger convention.
Readers destagger to co-located grids on load. StaggerInfo
documents what the grid looked like before destaggering — purely
informational, never used in computation or operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
convention
|
str
|
Overall grid type: |
required |
field_locations
|
Mapping[str, str] | None
|
Per-field-group stagger locations, e.g.
|
None
|
position
|
Mapping[str, tuple[float, ...]] | None
|
Per-component stagger offsets in |
None
|
interpolation_order
|
int | None
|
Order of interpolation used during destaggering (1 = linear,
2 = quadratic). |
None
|
notes
|
str | None
|
Free-text provenance (e.g. |
None
|
Examples:
>>> si = StaggerInfo(convention="node")
>>> si.convention
'node'
>>> si.field_locations is None
True
>>> si = StaggerInfo(
... convention="staggered",
... field_locations={"B": "face", "E": "edge"},
... notes="Yee mesh",
... )
>>> si.field_locations["B"]
'face'
>>> si = StaggerInfo(
... convention="staggered",
... position={"B_1": (0.5, 0.0, 0.0), "E_1": (0.0, 0.5, 0.5)},
... )
>>> si.position["B_1"]
(0.5, 0.0, 0.0)
Source code in src/pypic/containers.py
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 112 113 114 115 116 | |
SimulationConfig
dataclass
¶
Parsed simulation configuration from a TOML config file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Human-readable name for the simulation run. |
required |
model_type
|
Literal['PIC', 'MHD', 'hybrid', 'vlasov', 'gyrokinetic']
|
Simulation type identifier — uppercase for fluid/PIC families,
lowercase for kinetic continuum codes. Matches |
required |
grid
|
GridInfo
|
Grid metadata (includes coordinate geometry). |
required |
normalization
|
Normalization
|
Unit system. |
required |
species
|
tuple[SpeciesInfo, ...]
|
Species definitions (tuple for immutability). |
()
|
physics
|
PhysicsParams
|
Physics parameters (frozen dataclass). |
PhysicsParams()
|
frame
|
str
|
Reference frame label (e.g. |
'simulation'
|
transforms
|
Mapping[str, FrameTransform]
|
Validated |
dict()
|
initial_conditions
|
InitialConditions | None
|
Validated |
None
|
output
|
Output | None
|
Validated |
None
|
bodies
|
tuple[Body, ...]
|
Validated |
()
|
drivers
|
tuple[Driver, ...]
|
Validated |
()
|
restart
|
Restart | None
|
Validated |
None
|
run
|
Run | None
|
Validated |
None
|
probes
|
tuple[Probe, ...]
|
Validated |
()
|
collisions
|
tuple[Collision, ...]
|
Validated |
()
|
phase_space
|
PhaseSpace | None
|
Validated |
None
|
metadata
|
Mapping[str, Any]
|
Free-form annotations from readers (stagger, scaling, version, description, ...). Schema-typed sections live on dedicated attributes above rather than as opaque dict entries here. |
dict()
|
Examples:
>>> from pypic.units import Normalization, SpeciesInfo
>>> cfg = SimulationConfig(
... model_name="test", model_type="PIC",
... grid=GridInfo(
... dimensions=(4,), spacing=(1.0,), origin=(0.0,),
... geometry=CARTESIAN,
... ),
... normalization=Normalization.identity(),
... species=(SpeciesInfo(name="e", charge=-1.0, mass=1.0),),
... )
>>> cfg.model_name
'test'
Source code in src/pypic/containers.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
TabularData
dataclass
¶
Generic columnar container for auxiliary time-series data.
Stores named 1-D arrays sharing a common length, with optional index column designation. Used for conserved quantities, solver diagnostics, virtual satellite probes, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dataset label (e.g. |
required |
columns
|
Mapping[str, FloatArray]
|
Column name → 1-D array mapping. All arrays must have the same length. |
required |
index_column
|
str | None
|
Which column serves as the index (e.g. |
None
|
metadata
|
Mapping[str, Any]
|
Source info (reader name, file path, etc.). |
dict()
|
Examples:
>>> import numpy as np
>>> tab = TabularData(
... name="diagnostics",
... columns={"cycle": np.array([0.0, 1.0, 2.0]),
... "energy": np.array([1.0, 0.9, 0.8])},
... index_column="cycle",
... )
>>> tab["energy"]
array([1. , 0.9, 0.8])
>>> len(tab)
3
>>> "cycle" in tab
True
>>> tab.column_names
['cycle', 'energy']
Source code in src/pypic/containers.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
column_names
property
¶
Sorted list of column names.
index
property
¶
Index array: the designated index column, or np.arange(len).
__getitem__(key)
¶
Return a column by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Column name. |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If key is not a column name. |
Source code in src/pypic/containers.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
__contains__(key)
¶
Check whether key is a column name.
Source code in src/pypic/containers.py
312 313 314 | |
__len__()
¶
Return the number of rows (common array length).
Source code in src/pypic/containers.py
316 317 318 319 320 | |
ParticleData
dataclass
¶
Container for particle data from a single species at one timestep.
Canonical per-macroparticle representation: every macroparticle carries
a weight (number of physical particles it represents); the species
as a whole carries scalar species_charge and species_mass. The
per-macroparticle charge and mass that enter moments and the equations
of motion are derived on demand via macro_charge and
macro_mass. This shape is code-agnostic — readers for combined-
storage codes (iPIC3D, OSIRIS) split the native q_s × w column into
weight + scalars on load; separate-storage codes (VPIC, WarpX,
Smilei, PIConGPU, TRISTAN-MP) populate the same fields directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species_index
|
int
|
Zero-based species index. |
required |
species_name
|
str
|
Human-readable species name (e.g. |
required |
position
|
FloatArray | None
|
Particle positions, shape |
required |
velocity
|
FloatArray | None
|
Particle velocities, shape |
required |
n_particles
|
int
|
Total particle count. |
required |
id
|
IntArray | None
|
Integer particle tracking IDs, shape |
None
|
weight
|
FloatArray | None
|
Per-macroparticle weight \(w\) (number of physical particles per
macroparticle), shape |
None
|
species_charge
|
float | None
|
Scalar species charge \(q_s\) in code units (e.g. |
None
|
species_mass
|
float | None
|
Scalar species mass \(m_s\) in code units. |
None
|
metadata
|
Mapping[str, Any]
|
Source info (file path, format, etc.). |
required |
Examples:
>>> import numpy as np
>>> pcl = ParticleData(
... species_index=0, species_name="electrons",
... position=np.zeros((10, 3)),
... velocity=np.ones((10, 3)),
... n_particles=10, metadata={},
... weight=np.ones(10), species_charge=-1.0, species_mass=1.0,
... )
>>> pcl.x.shape
(10,)
>>> len(pcl)
10
Source code in src/pypic/containers.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
x
property
¶
X positions (view into position[:, 0]).
y
property
¶
Y positions (view into position[:, 1]).
z
property
¶
Z positions (view into position[:, 2]).
vx
property
¶
X velocities (view into velocity[:, 0]).
vy
property
¶
Y velocities (view into velocity[:, 1]).
vz
property
¶
Z velocities (view into velocity[:, 2]).
macro_charge
property
¶
Per-macroparticle charge \(q_s w\).
macro_mass
property
¶
Per-macroparticle mass \(m_s w\).