Source code for tracesuite.post.block
"""Provide python representations of POST block, solution and field."""
from __future__ import annotations
import typing
import numpy as np
if typing.TYPE_CHECKING:
from . import _post
[docs]
class Block:
"""Representation of a POST block including coordinates, solutions and metadata."""
def __init__(self, post_dataset: _post.Dataset, name: str):
self._ds = post_dataset
self._name = name
@property
def _block(self):
return self._ds.get_block_by_name(self.name)
def __getattr__(self, solution_name: str) -> Solution:
return Solution(self, solution_name)
def __repr__(self):
return f"<Block ({self._block})>"
@property
def name(self):
"""Return the blocks name."""
return self._name
@property
def is_local(self):
"""Return True if the blocks is local to this process."""
return self._block.is_local
@property
def dim(self):
"""Return the blocks dimension."""
return self._block.dimension
@property
def shape(self):
"""Return the blocks shape."""
return self._block.get_shape()
@property
def is_structured(self):
"""Return True if the block is structured."""
return self._block.is_structured
@property
def coordinates(self):
"""Get all the blocks coordinates."""
if not self.is_local:
msg = (f"The block {self.name} is not local to this process. "
f"You are only allowed to access coordinates where 'block.is_local' evaluates to True")
raise RuntimeError(msg)
return np.array(self._block.coordinates)
@property
def x(self):
"""Get the blocks x coordinates."""
return self.coordinates[0]
@property
def y(self):
"""Get the blocks y coordinates."""
if not self._block.mesh.is_cartesian:
raise Exception("Property is only allowed for Cartesian meshes.")
return self.coordinates[1]
@property
def z(self):
"""Get the blocks z coordinates."""
if not self._block.mesh.is_cartesian:
raise Exception("Property is only allowed for Cartesian meshes.")
return self.coordinates[2]
@property
def r(self):
"""Get the blocks r coordinates."""
if not self._block.mesh.is_cylindrical:
raise Exception("Property is only allowed for cylindrical meshes.")
return self.coordinates[1]
@property
def theta(self):
"""Get the blocks theta coordinates."""
if not self._block.mesh.is_cylindrical:
raise Exception("Property is only allowed for cylindrical meshes.")
return self.coordinates[2]
@property
def solutions(self):
"""Get a list of all solutions of this block."""
return [Solution(self, solution.name) for solution in self._block.solutions]
[docs]
class Solution:
"""Contains a set of variable fields which form a solution."""
def __init__(self, block: Block, name: str):
self._block = block
self._name = name
@property
def _solution(self):
return self._block._block.get_solution(self.name)
def __repr__(self):
return f"<Solution@{self._solution}>"
@property
def name(self):
"""Return the name of the solution."""
return self._name
@property
def location(self):
"""Return the grid location of the solution values (Cell/Vertex)."""
return self._solution.location
@property
def fields(self):
"""Return the fields of the current solution."""
return {field.name: Field(self._block, self, field.name) for field in self._solution.fields}
[docs]
def append_field(self, name, value=None):
"""Add a new field to this solution."""
if value is None and self._block.is_local:
msg = "append_field needs a numpyArray as second argument for local blocks."
raise TypeError(msg)
if value is not None and not self._block.is_local:
msg = "append_field only allows None as second argument for non local blocks."
raise TypeError(msg)
self._solution.create_field(self._block._block.mesh, name)
new_field = Field(self._block, self, name)
if value is not None:
new_field.v = value
return new_field
[docs]
class Field:
"""Represents a POST variable field. The variable field provides read and write access."""
def __init__(self, block: Block, solution: Solution, name: str):
self._block = block
self._solution = solution
self._name = name
@property
def _field(self):
return self._solution._solution.get_field(self.name)
def __repr__(self):
return f"<Field@{id(self._field.get_field(self._block._block, self._solution._solution))}>"
def __len__(self): # @todo: Remove if possible
return self.shape.prod()
@property
def shape(self) -> np.ndarray:
"""Get the shape."""
return np.array(self._field.get_shape(self._block._block, self._solution._solution))
@property
def name(self):
"""Return the name of the field/variable."""
return self._name
@property
def is_complex(self):
"""Return true if the field contains complex values."""
return self._field.is_complex
@property
def _memory_view(self) -> tuple[np.ndarray, np.ndarray | None]:
assert self._block.is_local
real, imag = self._field.get_field(self._block._block, self._solution._solution)
return np.array(real, copy=False), np.array(imag, copy=False) if self.is_complex else None
@property
def v(self) -> np.ndarray[float | complex]:
"""Get a numpy array of the dimensionalized fields values."""
if not self._block.is_local:
msg = (f"The block {self._block.name} is not local to this process. "
f"You are only allowed to use gain fieldAccess where field.is_local evaluates to 'True'.")
raise RuntimeError(msg)
real, imag = self._memory_view
return self._field.dimension_factor * ((real + 1j * imag) if self.is_complex else real)
@v.setter
def v(self, x: np.ndarray) -> None:
"""Set the fields values."""
if not self._block.is_local:
msg = (f"The block {self._block.name} is not local to this process. "
"You are only allowed to use gain fieldAccess where field.is_local evaluates to 'True'.")
raise RuntimeError(msg)
shape = self._block._block.get_shape(self._solution.location)
if not np.array_equal(shape, x.shape):
msg = f"Field to be assigned is no numpy field of correct size: '{shape}' != '{x.shape}'"
raise ValueError(msg)
real, imag = self._memory_view
real[:] = x.real / self._field.dimension_factor
if self.is_complex:
imag[:] = x.imag / self._field.dimension_factor