Field Operations¶
A Field is a three-dimensional NumPy array with simulation-time and grid-spacing metadata. Use extend() to add cells according to boundary conditions, or resample() to change the grid resolution while preserving its physical extent. Both methods return a new Field; they do not modify the original.
Extend a Field¶
Field.extend() adds cells on either side of the selected axes. Specify each extension as (lower, upper) in physical x, y, z order. The underlying array shape remains ordered (z, y, x).
from micpy.bin import read
field = read("A001_Delta_Gamma.conc1", -1)
extended = field.extend(
x=(10, 20),
boundary_x=("insulating", "symmetric"),
)
print(field.shape, extended.shape)
Each axis accepts its own lower and upper boundary conditions:
| Boundary condition | Behavior |
|---|---|
"periodic" | Wraps values from the opposite side of the axis. Both sides must be periodic. |
"insulating" | Reflects values and repeats the outermost cell. |
"symmetric" | Reflects values without repeating the outermost cell. |
The default is zero extension on all axes, with periodic boundary conditions. Extensions must be non-negative. A singleton axis (length 1) cannot be extended; leave its extension at (0, 0). extend() requires a three-dimensional field and preserves its simulation time and spacing.
Resample a Field¶
Field.resample() changes the number of cells without changing the physical size of the domain. Pass the target shape in NumPy (z, y, x) order.
from micpy.bin import read
field = read("A001_Delta_Gamma.conc1", -1)
# Continuous values: interpolate to a finer grid.
fine = field.resample((3000, 1, 1000), method="linear")
# Continuous values: coarsen by cell-overlap-weighted averaging.
coarse = field.resample((150, 1, 50), method="mean")
Choose the method according to the meaning of the data:
| Method | Behavior | Typical use |
|---|---|---|
"nearest" (default) | Uses the nearest source cell; preserves the input data type. | Phase labels, grain IDs, and other categorical fields. |
"linear" | Interpolates between source cell centers; produces floating-point values. | Concentration, temperature, and other continuous fields. |
"mean" | Computes overlap-weighted averages; produces floating-point values. | Conservative coarsening of continuous cell data. |
"mean" only supports coarsening: no target axis may have more cells than its corresponding source axis. Use "nearest" or "linear" for refinement. All target dimensions must be positive integers.
The returned field retains the original simulation time. When spacing metadata is available, the spacing along each axis is adjusted inversely to the change in resolution, preserving the original physical extent. resample() does not modify the original field.
API Reference¶
Return a field extended using MICRESS-style boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | tuple of int | Lower and upper extension in the x direction. Defaults to | (0, 0) |
y | tuple of int | Lower and upper extension in the y direction. Defaults to | (0, 0) |
z | tuple of int | Lower and upper extension in the z direction. Defaults to | (0, 0) |
boundary_x | tuple of str | Lower and upper boundary conditions for x. Defaults to | ('periodic', 'periodic') |
boundary_y | tuple of str | Lower and upper boundary conditions for y. Defaults to | ('periodic', 'periodic') |
boundary_z | tuple of str | Lower and upper boundary conditions for z. Defaults to
| ('periodic', 'periodic') |
Returns:
| Name | Type | Description |
|---|---|---|
Field | Field | Extended field with the original time and spacing. |
Raises:
| Type | Description |
|---|---|
ValueError | If the field is not three-dimensional, an extension is negative, a boundary condition is invalid, or periodic conditions are not used on both sides of an axis. |
Source code in micpy\bin.py
Resample the field to a new cell geometry.
The physical extent of the field is preserved. Consequently, changing the number of cells changes the spacing accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape | Tuple[int, int, int] | Target shape in NumPy axis order, i.e. | required |
method | str | Resampling method. Defaults to | 'nearest' |
Returns:
| Name | Type | Description |
|---|---|---|
Field | Field | Resampled field with updated spacing and preserved time. |
Raises:
| Type | Description |
|---|---|
ValueError | If the target shape or method is invalid, or if |
Source code in micpy\bin.py
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 | |