Skip to content

pyflightstream.workspace.fields (provisional)

Generated from the docstrings at build time: every one of the 24 names in the __all__ of pyflightstream.workspace.fields has an entry below. The names a guide or an example uses come first; the others follow under Advanced.

Field operations that build a custom free-stream file of inputs/freestreams/.

Pipeline role: prepares an INPUT. A row's FREESTREAM key names a file of the workspace's inputs/freestreams/ by its stem, <stem>.txt in the manual's STRUCTURED form (a first line Npts Mpts, then Npts x Mpts rows) or <stem>.dat in its UNSTRUCTURED form (one row per point, no header), every row x y z vx vy vz. The field such a file holds is often built out of other fields: a survey written by the post ([[probes]] reusable_inflow, one fields/<point>_field_NN.inflow.dat per steady point, or one fields/<point>_field_NN_step_<N>.inflow.dat per step of an unsteady one) mirrored, moved, averaged in time, or corrected by another survey. These are the four operations, each a pure function of its fields:

  • :func:mirror_field, through a coordinate plane: through y = 0 a row x y z vx vy vz becomes x -y z vx -vy vz;
  • :func:move_field, so a source point (a hub) lands on a target point: the coordinates are translated, the velocities are unchanged;
  • :func:subtract_fields, point by point on one grid: result = total - (other - reference), with the reference a uniform velocity the caller MUST state (the free stream the other field was solved in, so that only its induced part is removed; (0, 0, 0) where the other field is already induced-only); two grids that are not the same point set are refused, naming both files;
  • :func:time_mean_fields, the mean of per-step fields of one unsteady run over equally spaced steps, every step holding the same points.

UNITS: metres and metres per second, in the global frame, read and written as the files state them. Nothing is converted, rotated or interpolated, as the custom free stream itself is read (FREESTREAM_UNITS is the row's statement of the file's units, and it applies to the result as it applies to any file of the folder).

:func:write_freestream previews by default and writes only with apply=True, into inputs/freestreams/<stem>.<txt|dat> beside a provenance record <stem>.provenance.json naming the operation, its parameters and every input file with its sha256. It never overwrites a file unless asked, and it refuses a result the free-stream reader would refuse (rows not in one YZ plane, fewer than two distinct y or z, a STRUCTURED count that does not match). pyfs-workspace field is its command line.

Examples:

>>> from pyflightstream.workspace.fields import Field, mirror_field, move_field
>>> survey = Field(
...     form="UNSTRUCTURED",
...     rows=((1.0, 0.5, 0.0, 30.0, 2.0, 1.0), (1.0, -0.5, 1.0, 31.0, -2.0, 0.0)),
... )
>>> mirror_field(survey, plane="y").rows[0]
(1.0, -0.5, 0.0, 30.0, -2.0, 1.0)
>>> move_field(survey, source_point_m=(1.0, 0.0, 0.0), target_point_m=(0.0, 0.0, 2.0)).rows[1]
(0.0, -0.5, 3.0, 31.0, -2.0, 0.0)

Used in the guides

pyflightstream.workspace.fields.Field dataclass

Field(
    form: str,
    rows: tuple[Row, ...],
    header: str | None = None,
    source: str | None = None,
)

A custom free-stream field: its form, its rows and where it came from.

Attributes:

Name Type Description
form str

STRUCTURED or UNSTRUCTURED, the two forms of the manual.

rows tuple of tuple of float

One (x, y, z, vx, vy, vz) per point, in metres and metres per second, global frame, in the file's order.

header str or None

"Npts Mpts" of a STRUCTURED field, None for an UNSTRUCTURED one.

source str or None

The file the field was read from, named in every refusal.

Examples:

>>> from pyflightstream.workspace.fields import Field
>>> field = Field("UNSTRUCTURED", ((0.0, 1.0, 0.0, 10.0, 2.0, 0.0),))
>>> field.form, len(field.rows)
('UNSTRUCTURED', 1)

name property

name: str

The name a refusal gives this field: its file, or the field.

pyflightstream.workspace.fields.mirror_field

mirror_field(field: Field, *, plane: str) -> Field

Mirror a field through the coordinate plane <plane> = 0.

The coordinate normal to the plane and the velocity component along it change sign; the other four columns and the row order are kept (a STRUCTURED grid keeps its header and its neighbours).

Parameters:

Name Type Description Default
field Field

The field to mirror.

required
plane str

"x", "y" or "z": the plane x = 0, y = 0 or z = 0.

required

Returns:

Type Description
Field

The mirrored field.

Raises:

Type Description
WorkspaceError

If plane names none of the three.

Examples:

>>> from pyflightstream.workspace.fields import Field
>>> field = Field("UNSTRUCTURED", ((0.0, 1.0, 0.0, 10.0, 2.0, 0.0),))
>>> mirror_field(field, plane="y").rows
((0.0, -1.0, 0.0, 10.0, -2.0, 0.0),)

pyflightstream.workspace.fields.move_field

move_field(
    field: Field,
    *,
    source_point_m: Sequence[float],
    target_point_m: Sequence[float],
) -> Field

Translate a field so its source point lands on the target point.

Every position p becomes (p - source) + target, so the source point itself lands on the target exactly; the velocities are unchanged, since a translation turns no vector. Both points are keyword-only, so the two cannot be swapped by position.

Parameters:

Name Type Description Default
field Field

The field to translate.

required
source_point_m sequence of float

The point of the field that moves, (x, y, z) in metres.

required
target_point_m sequence of float

Where it lands, (x, y, z) in metres.

required

Returns:

Type Description
Field

The translated field.

Raises:

Type Description
WorkspaceError

If either point is not three finite numbers.

Examples:

>>> from pyflightstream.workspace.fields import Field
>>> field = Field("UNSTRUCTURED", ((0.0, 1.0, 0.0, 10.0, 2.0, 0.0),))
>>> move_field(field, source_point_m=(0.0, 0.0, 0.0), target_point_m=(1.0, 0.0, 0.0)).rows
((1.0, 1.0, 0.0, 10.0, 2.0, 0.0),)

pyflightstream.workspace.fields.subtract_fields

subtract_fields(
    total: Field,
    other: Field,
    *,
    reference_m_s: Sequence[float],
    tolerance_m: float = POSITION_TOLERANCE_M,
) -> Field

Subtract one field from another point by point: total - (other - reference).

The two fields must be ONE GRID: the same number of points, each point of total at one point of other within tolerance_m along each axis, and the reverse. They are matched by position, not by row order, so a grid mirrored or moved onto the other's points is matched. The result keeps total's positions, order, form and header.

reference_m_s is a uniform velocity, the free stream other was solved in: other - reference is then the velocity other's body induces, and the result is total with that induced velocity removed. It has no default: a reference left out would silently remove the free stream itself. State (0, 0, 0) where other is already induced-only (a field with its free stream taken out), and the result is the plain difference total - other.

Parameters:

Name Type Description Default
total Field

The field the induced velocity is removed from.

required
other Field

The field whose induced velocity is removed; the same grid as total.

required
reference_m_s sequence of float

The uniform velocity other was solved in, (vx, vy, vz) in m/s.

required
tolerance_m float

The distance in metres within which two positions are the same point, along each axis.

POSITION_TOLERANCE_M

Returns:

Type Description
Field

total - (other - reference) on total's positions.

Raises:

Type Description
WorkspaceError

If the grids differ (a different number of points, or a point of either with no point of the other), naming both fields and the first point that has no partner; if a point of total lies within the tolerance of two points of other not yet matched, so the pairing is not decided by the positions; if a field holds one point twice; or if the reference is not three finite numbers.

Examples:

>>> from pyflightstream.workspace.fields import Field
>>> field = Field("UNSTRUCTURED", ((0.0, 1.0, 0.0, 10.0, 2.0, 0.0),))
>>> other = Field("UNSTRUCTURED", ((0.0, 1.0, 0.0, 12.0, 0.0, 0.0),))
>>> subtract_fields(field, other, reference_m_s=(10.0, 0.0, 0.0)).rows
((0.0, 1.0, 0.0, 8.0, 2.0, 0.0),)

Advanced

pyflightstream.workspace.fields.DEFAULT_R_BODY_M module-attribute

DEFAULT_R_BODY_M = 0.38

pyflightstream.workspace.fields.FIELD_PROVENANCE_SCHEMA module-attribute

FIELD_PROVENANCE_SCHEMA = 'pyfs-field-operation/1'

pyflightstream.workspace.fields.FILL_AZIMUTH_TOLERANCE_RAD module-attribute

FILL_AZIMUTH_TOLERANCE_RAD = 0.001

pyflightstream.workspace.fields.FieldWrite dataclass

FieldWrite(
    target: Path,
    sidecar: Path,
    field: Field,
    provenance: dict[str, object],
    applied: bool,
    overwritten: tuple[Path, ...] = (),
)

What :func:write_freestream wrote, or would write in a preview.

Attributes:

Name Type Description
target Path

inputs/freestreams/<stem>.txt or .dat.

sidecar Path

inputs/freestreams/<stem>.provenance.json.

field Field

The field written.

provenance dict

The record written into the sidecar.

applied bool

True when the files were written, False for a preview.

overwritten tuple of Path

The files that existed and were replaced (overwrite=True).

pyflightstream.workspace.fields.FluctuationReport dataclass

FluctuationReport(
    rows: tuple[
        tuple[
            float, float, float, float, float, float, float
        ],
        ...,
    ],
    steps: tuple[float, ...],
    sources: tuple[str, ...],
)

The fluctuation of per-step fields at each probe over a run's last steps.

Attributes:

Name Type Description
rows tuple of tuple of float

One (x, y, z, std_vx, std_vy, std_vz, std_mag) per probe, in the first step's order: the POPULATION standard deviation (divided by the number of steps) of each component about its time mean, and std_mag = sqrt(std_vx^2 + std_vy^2 + std_vz^2). Metres and m/s.

steps tuple of float

The steps the statistics span, in order.

sources tuple of str

The files read, in step order.

pyflightstream.workspace.fields.FluctuationWrite dataclass

FluctuationWrite(
    target: Path,
    sidecar: Path,
    provenance: dict[str, object],
    applied: bool,
    overwritten: tuple[Path, ...] = (),
)

What :func:write_fluctuation wrote, or would write in a preview.

pyflightstream.workspace.fields.MIRROR_PLANES module-attribute

MIRROR_PLANES: Mapping[str, int] = {'x': 0, 'y': 1, 'z': 2}

pyflightstream.workspace.fields.POSITION_TOLERANCE_M module-attribute

POSITION_TOLERANCE_M = 1e-06

pyflightstream.workspace.fields.StepField dataclass

StepField(step: float, field: Field)

One per-step field of an unsteady run: the step and the field it holds.

pyflightstream.workspace.fields.fill_interior

fill_interior(
    field: Field, *, r_body_m: float = DEFAULT_R_BODY_M
) -> tuple[Field, int]

Fill the probes inside the body from the ray outside it (P0320-FILL-INTERIOR).

Every probe with r < r_body_m (r the distance from the x axis, hypot(y, z)) takes the velocity of the probe at r >= r_body_m with the smallest radius on the same azimuth ray (atan2(z, y) within :data:FILL_AZIMUTH_TOLERANCE_RAD). Positions never change. A probe on the axis itself has azimuth 0 by atan2.

Parameters:

Name Type Description Default
field Field

The field to fill.

required
r_body_m float

The body radius about the x axis, in metres; the probes inside it are replaced.

DEFAULT_R_BODY_M

Returns:

Type Description
(Field, int)

The filled field and the number of probes replaced.

Raises:

Type Description
WorkspaceError

If r_body_m is not a positive finite radius, or an interior probe has no probe at r >= r_body_m on its ray (nothing is guessed).

Examples:

>>> pair = ((0.0, 0.1, 0.0, 1.0, 0.0, 0.0), (0.0, 0.5, 0.0, 9.0, 0.0, 0.0))
>>> filled, count = fill_interior(Field("UNSTRUCTURED", pair), r_body_m=0.38)
>>> count, filled.rows[0][3]
(1, 9.0)

pyflightstream.workspace.fields.fluctuation_extent

fluctuation_extent(
    report: FluctuationReport,
) -> tuple[float, float]

Return the largest std_mag over the probes and its root mean square, in m/s.

Parameters:

Name Type Description Default
report FluctuationReport

The report of :func:fluctuation_report.

required

Returns:

Type Description
tuple of (float, float)

The largest std_mag and the root mean square of std_mag over the probes, in m/s.

Raises:

Type Description
WorkspaceError

If the report holds no probe, so there is no fluctuation to summarise.

pyflightstream.workspace.fields.fluctuation_report

fluctuation_report(
    fields: Sequence[StepField],
    *,
    tolerance_m: float = POSITION_TOLERANCE_M,
) -> FluctuationReport

Return the per-probe fluctuation of per-step fields (P0320-INFLOW-FLUCTUATION).

The steps must be consecutive integers (an evenly exported run's last K steps) and hold the same probes as the first step within tolerance_m, the survey rule of :func:time_mean_fields. Each probe's time mean and population standard deviation are taken per component; the magnitude combines the three.

Parameters:

Name Type Description Default
fields sequence of StepField

The per-step fields, each with its step.

required
tolerance_m float

The distance in metres within which two positions are the same point, along each axis.

POSITION_TOLERANCE_M

Returns:

Type Description
FluctuationReport

Per probe its position, the population standard deviation of each velocity component and std_mag, the root-sum-square of those three (not the standard deviation of the speed), with the steps and the source files.

Raises:

Type Description
WorkspaceError

If fewer than two steps are given (a steady field has no fluctuation), a step is NaN or infinite, the steps are not consecutive integers, or a step's probes differ from the first step's (naming file and row).

Examples:

>>> steps = [
...     StepField(1.0, Field("UNSTRUCTURED", ((0.0, 0.0, 0.0, 1.0, 0.0, 0.0),))),
...     StepField(2.0, Field("UNSTRUCTURED", ((0.0, 0.0, 0.0, 3.0, 0.0, 0.0),))),
... ]
>>> fluctuation_report(steps).rows[0][3:]
(1.0, 0.0, 0.0, 1.0)

pyflightstream.workspace.fields.read_field

read_field(path: str | Path) -> Field

Read a field file; its extension states its form, as the matrix binding reads it.

.txt is STRUCTURED and .dat UNSTRUCTURED (a post's *.inflow.dat is therefore UNSTRUCTURED). Every row is six finite numbers, and a STRUCTURED file opens with two positive counts whose product is its number of rows.

Parameters:

Name Type Description Default
path str or Path

The field file, .txt or .dat.

required

Returns:

Type Description
Field

The field, its form, rows, header and source.

Raises:

Type Description
WorkspaceError

If the extension names no form, the file cannot be read, or its rows are not the form's.

pyflightstream.workspace.fields.read_step_fields

read_step_fields(
    paths: Sequence[str | Path], *, last: int | None = None
) -> list[StepField]

Read per-step field files, ordered by the step each names, keeping the last ones.

Parameters:

Name Type Description Default
paths sequence of str or Path

The per-step field files, ..._step_<N>.inflow.dat.

required
last int

Keep only the last this many steps; every step when None.

None

Returns:

Type Description
list of StepField

The fields ordered by step.

Raises:

Type Description
WorkspaceError

If no file is given, a file names no step, two files name one step, or fewer steps are on disk than last asks for.

pyflightstream.workspace.fields.render_field

render_field(field: Field) -> str

Return the file text of a field: the header of a STRUCTURED one, then its rows.

Parameters:

Name Type Description Default
field Field

The field to render.

required

Returns:

Type Description
str

The file text, ending in a newline.

pyflightstream.workspace.fields.render_fluctuation

render_fluctuation(report: FluctuationReport) -> str

Return the text of <stem>.fluctuation.csv: x,y,z,std_vx,...,std_mag, %.9g.

Parameters:

Name Type Description Default
report FluctuationReport

The report of :func:fluctuation_report.

required

Returns:

Type Description
str

The CSV text, ending in a newline.

pyflightstream.workspace.fields.step_of

step_of(path: str | Path) -> float | None

Return the step a per-step field file names, ..._step_<N>.inflow.dat, or None.

Parameters:

Name Type Description Default
path str or Path

A field file.

required

Returns:

Type Description
float or None

The step the name states, or None when it states none or it is not finite.

pyflightstream.workspace.fields.time_mean_fields

time_mean_fields(
    fields: Sequence[StepField],
    *,
    tolerance_m: float = POSITION_TOLERANCE_M,
) -> Field

Average per-step fields of one unsteady run into one field.

Every step must hold the same points in the same order (one survey of one run), and the steps must be equally spaced, so the arithmetic mean of the samples is the time mean over their span. The result keeps the earliest step's positions, form and header.

Parameters:

Name Type Description Default
fields sequence of StepField

The per-step fields of one run.

required
tolerance_m float

The distance in metres within which two positions are the same point, along each axis.

POSITION_TOLERANCE_M

Returns:

Type Description
Field

The time-mean field.

Raises:

Type Description
WorkspaceError

If no step is given, a step is NaN or infinite (naming the file), two steps coincide, the spacing is not uniform (naming the steps), or a step's points differ from the first step's (naming the file and the row).

pyflightstream.workspace.fields.write_fluctuation

write_fluctuation(
    root: str | Path,
    stem: str,
    report: FluctuationReport,
    *,
    parameters: Mapping[str, object],
    inputs: Sequence[str | Path],
    apply: bool = False,
    overwrite: bool = False,
) -> FluctuationWrite

Write only the fluctuation report of a run's last steps, with its provenance, or preview it.

The files are <root>/inputs/freestreams/<stem>.fluctuation.csv and <stem>.fluctuation.provenance.json (no field is written, so the stem names no free stream). The overwrite rule is :func:write_freestream's.

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required
stem str

The name the files are written under, a plain name.

required
report FluctuationReport

The report of :func:fluctuation_report.

required
parameters mapping of str to object

The operation's parameters, recorded in the provenance.

required
inputs sequence of str or Path

Every input file, recorded in the provenance with its sha256.

required
apply bool

Write the files; without it nothing is written and the same refusals fire.

False
overwrite bool

Replace a file or record that already exists instead of refusing it.

False

Returns:

Type Description
FluctuationWrite

The report's path, its provenance record's path and content, whether it was written, and the files it replaced.

Raises:

Type Description
WorkspaceError

If the stem is not a plain name, or a target exists and overwrite is not set.

pyflightstream.workspace.fields.write_freestream

write_freestream(
    root: str | Path,
    stem: str,
    field: Field,
    *,
    operation: str,
    parameters: Mapping[str, object],
    inputs: Sequence[str | Path],
    apply: bool = False,
    overwrite: bool = False,
    sidecars: Mapping[str, str] | None = None,
) -> FieldWrite

Write a field into <root>/inputs/freestreams/ with its provenance, or preview it.

The file is <stem>.txt for a STRUCTURED field and <stem>.dat for an UNSTRUCTURED one; the record <stem>.provenance.json names the operation, its parameters, every input file with its sha256, and the written file's own sha256. Without apply nothing is written and the same refusals fire, so a preview says what apply would do.

sidecars maps a suffix (".fluctuation.csv") to the text of a file written beside the field as <stem><suffix>: each is subject to the same overwrite rule and is named in the record, with its sha256, under "sidecars".

Parameters:

Name Type Description Default
root str or Path

The workspace root.

required
stem str

The name the files are written under, a plain name.

required
field Field

The field to write.

required
operation str

The field operation that made it, recorded in the provenance.

required
parameters mapping of str to object

The operation's parameters, recorded in the provenance.

required
inputs sequence of str or Path

Every input file, recorded in the provenance with its sha256.

required
apply bool

Write the files; without it nothing is written and the same refusals fire.

False
overwrite bool

Replace a file or record that already exists instead of refusing it.

False
sidecars mapping of str to str

Suffix to the text of a file written beside the field.

None

Returns:

Type Description
FieldWrite

The field's path, its provenance record's path and content, the field, whether it was written, and the files it replaced.

Raises:

Type Description
WorkspaceError

If the stem is not a plain name; if the field is not one the free-stream reader accepts; if the folder already holds the stem in the other form (one stem names one file); or if the file or its record exists and overwrite is not set.