Coordinates & Operators¶
Coordinate geometry definitions and geometry-aware discrete differential operators (divergence, curl, gradient).
coordinates
¶
Coordinate geometry definitions and discrete differential operators.
CoordinateGeometry
dataclass
¶
Coordinate system with axis metadata and metric scale factors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
GeometryType
|
The coordinate geometry type. |
required |
axis_names
|
tuple[str, str, str]
|
Human-readable axis labels, e.g. |
required |
axis_units
|
tuple[str, str, str]
|
Dimension kind per axis: |
required |
Examples:
>>> CARTESIAN.axis_names
('x', 'y', 'z')
>>> SPHERICAL.axis_names
('r', 'θ', 'φ')
>>> CYLINDRICAL.axis_units
('length', 'angle', 'length')
Source code in src/pypic/coordinates/geometry.py
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 | |
metric_factors(x1, x2, x3)
¶
Compute metric scale factors \((h_1, h_2, h_3)\) for this geometry.
The line element is \(ds^2 = h_1^2 dx_1^2 + h_2^2 dx_2^2 + h_3^2 dx_3^2\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Numeric
|
First coordinate: \(x\) (Cartesian), \(r\) (spherical/cylindrical). |
required |
x2
|
Numeric
|
Second coordinate: \(y\) (Cartesian), \(θ\) (spherical), \(φ\) (cylindrical). |
required |
x3
|
Numeric
|
Third coordinate: \(z\) (Cartesian), \(φ\) (spherical), \(z\) (cylindrical). |
required |
Returns:
| Type | Description |
|---|---|
tuple[ScaleFactor, ScaleFactor, ScaleFactor]
|
Scale factors \((h_1, h_2, h_3)\). Constant factors are returned as
|
Examples:
>>> CARTESIAN.metric_factors(1.0, 2.0, 3.0)
(np.float64(1.0), np.float64(1.0), np.float64(1.0))
>>> h1, h2, h3 = SPHERICAL.metric_factors(2.0, np.pi / 2, 0.0)
>>> float(h1), float(h2), float(h3)
(1.0, 2.0, 2.0)
>>> h1, h2, h3 = CYLINDRICAL.metric_factors(3.0, 0.0, 1.0)
>>> float(h1), float(h2), float(h3)
(1.0, 3.0, 1.0)
Source code in src/pypic/coordinates/geometry.py
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 | |
GeometryType
¶
Bases: StrEnum
Coordinate geometry type.
Values match the [coordinates] geometry key in schema.md.
Examples:
>>> GeometryType.CARTESIAN
<GeometryType.CARTESIAN: 'cartesian'>
>>> GeometryType("spherical")
<GeometryType.SPHERICAL: 'spherical'>
Source code in src/pypic/coordinates/geometry.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
FrameTransform
dataclass
¶
Affine transformation between coordinate reference frames.
Transforms a point \(\mathbf{x}\) from the source frame to the target:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_frame
|
str
|
Name of the source frame (e.g. |
required |
target_frame
|
str
|
Name of the target frame (e.g. |
required |
origin
|
tuple[float, float, float]
|
Source-frame coordinates of the target-frame origin. Subtracted before rotation. |
(0.0, 0.0, 0.0)
|
rotation
|
Rotation3x3
|
3×3 orthogonal rotation matrix as nested tuples. |
_IDENTITY_3X3
|
scale
|
float
|
Converts code length units to target-frame units.
|
1.0
|
target_axis_names
|
tuple[str, str, str] | None
|
Axis names in the target frame. |
None
|
Examples:
>>> t = FrameTransform("sim", "GSM", origin=(52.0, 26.0, 64.0))
>>> t.source_frame
'sim'
>>> t.is_identity
False
>>> FrameTransform("a", "a").is_identity
True
Source code in src/pypic/coordinates/transforms.py
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 159 160 161 162 163 164 165 166 167 168 169 170 | |
rotation_matrix
property
¶
Return the rotation as a (3, 3) NumPy array.
Examples:
>>> FrameTransform("a", "b").rotation_matrix.shape
(3, 3)
is_identity
property
¶
True if this transform is a no-op.
Examples:
>>> FrameTransform("a", "a").is_identity
True
>>> FrameTransform("a", "b", origin=(1.0, 0.0, 0.0)).is_identity
False
inverse()
¶
Return the inverse transform (target → source).
For \(\mathbf{x}_t = s R (\mathbf{x}_s - \mathbf{o})\), the inverse is \(\mathbf{x}_s = R^T \mathbf{x}_t / s + \mathbf{o}\).
Examples:
>>> t = FrameTransform("a", "b", origin=(1.0, 2.0, 3.0), scale=2.0)
>>> inv = t.inverse()
>>> inv.source_frame, inv.target_frame
('b', 'a')
Source code in src/pypic/coordinates/transforms.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
curl(f1, f2, f3, d1, d2, d3=None, *, geometry=GeometryType.CARTESIAN)
¶
Compute the curl of a vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f1
|
NDArray
|
First component of the vector field, shape |
required |
f2
|
NDArray
|
Second component of the vector field, same shape as f1. |
required |
f3
|
NDArray
|
Third component of the vector field, same shape as f1. |
required |
d1
|
float
|
Grid spacing along the first axis. |
required |
d2
|
float
|
Grid spacing along the second axis. |
required |
d3
|
float or None
|
Grid spacing along the third axis, or |
None
|
geometry
|
GeometryType
|
Coordinate geometry. Only |
CARTESIAN
|
Returns:
| Type | Description |
|---|---|
tuple[NDArray, NDArray, NDArray]
|
Curl components |
Raises:
| Type | Description |
|---|---|
GeometryUnsupportedError
|
If |
Examples:
>>> import numpy as np
>>> f = np.ones((4, 4, 4))
>>> c1, c2, c3 = curl(f, f, f, 1.0, 1.0, 1.0)
>>> np.max(np.abs(c1))
np.float64(0.0)
Source code in src/pypic/coordinates/operators.py
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 | |
divergence(f1, f2, f3, d1, d2, d3=None, *, geometry=GeometryType.CARTESIAN)
¶
Compute the divergence of a vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f1
|
NDArray
|
First component of the vector field, shape |
required |
f2
|
NDArray
|
Second component of the vector field, same shape as f1. |
required |
f3
|
NDArray
|
Third component of the vector field, same shape as f1. |
required |
d1
|
float
|
Grid spacing along the first axis. |
required |
d2
|
float
|
Grid spacing along the second axis. |
required |
d3
|
float or None
|
Grid spacing along the third axis, or |
None
|
geometry
|
GeometryType
|
Coordinate geometry. Only |
CARTESIAN
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Divergence field, same shape as the input arrays. |
Raises:
| Type | Description |
|---|---|
GeometryUnsupportedError
|
If |
Examples:
>>> import numpy as np
>>> f = np.ones((4, 4, 4))
>>> np.max(np.abs(divergence(f, f, f, 1.0, 1.0, 1.0)))
np.float64(0.0)
Source code in src/pypic/coordinates/operators.py
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 | |
gradient(f, d1, d2, d3=None, *, geometry=GeometryType.CARTESIAN)
¶
Compute the gradient of a scalar field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
NDArray
|
Scalar field, shape |
required |
d1
|
float
|
Grid spacing along the first axis. |
required |
d2
|
float
|
Grid spacing along the second axis. |
required |
d3
|
float or None
|
Grid spacing along the third axis, or |
None
|
geometry
|
GeometryType
|
Coordinate geometry. Only |
CARTESIAN
|
Returns:
| Type | Description |
|---|---|
tuple[NDArray, NDArray, NDArray]
|
Gradient components |
Raises:
| Type | Description |
|---|---|
GeometryUnsupportedError
|
If |
Examples:
>>> import numpy as np
>>> f = np.ones((4, 4, 4))
>>> g1, g2, g3 = gradient(f, 1.0, 1.0, 1.0)
>>> np.max(np.abs(g1))
np.float64(0.0)
Source code in src/pypic/coordinates/operators.py
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 | |
compose_transforms(first, second)
¶
Compose two transforms: apply first then second.
If first maps A→B and second maps B→C, the result maps A→C.
The composition follows from:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
FrameTransform
|
Transform applied first (A→B). |
required |
second
|
FrameTransform
|
Transform applied second (B→C). |
required |
Returns:
| Type | Description |
|---|---|
FrameTransform
|
Composed transform (A→C). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> a_to_b = FrameTransform("A", "B", origin=(1.0, 0.0, 0.0))
>>> b_to_c = FrameTransform("B", "C", origin=(0.0, 2.0, 0.0))
>>> a_to_c = compose_transforms(a_to_b, b_to_c)
>>> a_to_c.source_frame, a_to_c.target_frame
('A', 'C')
Source code in src/pypic/coordinates/transforms.py
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 | |
find_pressure_tensor_groups(field_names)
¶
Group pressure tensor fields into complete symmetric tensors.
Returns (P_11, P_22, P_33, P_12, P_13, P_23) tuples for each complete
set. Handles per-species tensors (P_s0_11 etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_names
|
Iterable[str]
|
All field names in the dataset. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str, str, str, str]]
|
|
Examples:
>>> fields = ["P_11", "P_22", "P_33", "P_12", "P_13", "P_23"]
>>> find_pressure_tensor_groups(fields)
[('P_11', 'P_22', 'P_33', 'P_12', 'P_13', 'P_23')]
Source code in src/pypic/coordinates/transforms.py
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 | |
find_vector_triplets(field_names)
¶
Group field names into vector triplets needing rotation.
Returns a list of (name1, name2, name3) tuples for each complete
vector field, stored or derived, that the field registry knows as a
vector: total fields (B_1, B_2, B_3), per-species fields
(J_s0_1, J_s0_2, J_s0_3) and derived vectors (E_prime_1, ...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_names
|
Iterable[str]
|
All field names in the dataset. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str]]
|
Complete vector triplets. |
Examples:
>>> find_vector_triplets(["B_1", "B_2", "B_3", "rho_c"])
[('B_1', 'B_2', 'B_3')]
>>> find_vector_triplets(["J_s0_1", "J_s0_2", "J_s0_3", "J_s1_1"])
[('J_s0_1', 'J_s0_2', 'J_s0_3')]
Source code in src/pypic/coordinates/transforms.py
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 | |
identity_transform(frame)
¶
Return a no-op transform within a single frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
str
|
Frame name used as both source and target. |
required |
Returns:
| Type | Description |
|---|---|
FrameTransform
|
Transform with no translation, rotation, or scaling, for which
|
Examples:
>>> identity_transform("sim").is_identity
True
Source code in src/pypic/coordinates/transforms.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
resolve_transform(source_frame, target_frame, transforms)
¶
Find or compose a transform from source_frame to target_frame.
Tries direct lookup first, then searches for a one-hop chain (A→B→C) through intermediate frames. Also checks inverse transforms (if A→B is registered, B→A is available via inverse).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_frame
|
str
|
Current frame name. |
required |
target_frame
|
str
|
Desired frame name. |
required |
transforms
|
dict[str, FrameTransform]
|
Registry mapping target frame names to transforms. |
required |
Returns:
| Type | Description |
|---|---|
FrameTransform
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no transform path exists. |
Examples:
>>> t = FrameTransform("sim", "GSM", origin=(1.0, 0.0, 0.0))
>>> resolve_transform("sim", "GSM", {"GSM": t}).target_frame
'GSM'
Source code in src/pypic/coordinates/transforms.py
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 | |
rotate_pressure_tensor(p11, p22, p33, p12, p13, p23, rotation)
¶
Rotate a symmetric pressure tensor by a rotation matrix.
The trace \(P_{11} + P_{22} + P_{33}\) is invariant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p11
|
FloatArray
|
Independent components of the symmetric tensor. |
required |
p22
|
FloatArray
|
Independent components of the symmetric tensor. |
required |
p33
|
FloatArray
|
Independent components of the symmetric tensor. |
required |
p12
|
FloatArray
|
Independent components of the symmetric tensor. |
required |
p13
|
FloatArray
|
Independent components of the symmetric tensor. |
required |
p23
|
FloatArray
|
Independent components of the symmetric tensor. |
required |
rotation
|
FloatArray
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[FloatArray, ...]
|
|
Examples:
>>> import numpy as np
>>> I = np.eye(3)
>>> p = rotate_pressure_tensor(
... np.array([1.0]), np.array([2.0]), np.array([3.0]),
... np.array([0.0]), np.array([0.0]), np.array([0.0]), I,
... )
>>> [float(x[0]) for x in p]
[1.0, 2.0, 3.0, 0.0, 0.0, 0.0]
Source code in src/pypic/coordinates/transforms.py
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 | |
rotate_vector_components(v1, v2, v3, rotation)
¶
Rotate vector field components by a 3×3 rotation matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v1
|
FloatArray
|
Vector field components (arbitrary shape, must match). |
required |
v2
|
FloatArray
|
Vector field components (arbitrary shape, must match). |
required |
v3
|
FloatArray
|
Vector field components (arbitrary shape, must match). |
required |
rotation
|
FloatArray
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[FloatArray, FloatArray, FloatArray]
|
|
Examples:
>>> import numpy as np
>>> v1, v2, v3 = np.array([1.0]), np.array([0.0]), np.array([0.0])
>>> R = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]], dtype=float)
>>> r1, r2, r3 = rotate_vector_components(v1, v2, v3, R)
>>> float(r1[0]), float(r2[0]), float(r3[0])
(0.0, 1.0, 0.0)
Source code in src/pypic/coordinates/transforms.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 | |