Skip to content

crashmetrics.deck

deck

LS-DYNA keyword decks: read, check, summarise and edit, before a run.

  1. Read (read_deck): every file of a deck through *INCLUDE, *INCLUDE_PATH and *INCLUDE_TRANSFORM, with *PARAMETER values and expressions, in fixed, long and comma formats. Every line is kept, so a deck written back is identical except where it was edited. Typed values of any described keyword: Deck.records. Version information: Deck.info.
  2. Model tables (model_summary, time_steps): every part with its elements, section, thickness, material, density, mass and centre of gravity; the total mass and centre; estimated element time steps and the mass that mass scaling would add.
  3. Checks (check_deck): missing includes and parameters, duplicate IDs (also between include files), references to undefined IDs, parts without elements, thickness or density, degenerate elements, free nodes, parts in no contact, spot welds without a tied contact, unused definitions.
  4. Load case (load_case, output_requirements): termination, time step, energy settings, initial velocities, walls, motions, gravity, contacts, outputs; whether the outputs give what crashmetrics' analyses read (model quality, load paths, spot welds, node channels).
  5. Edit (set_parameter, set_thickness, set_material, set_end_time, set_output, prepare_outputs ...) and write DOE variants (write_variants).

The layouts are written from the keyword file format itself; keywords without a layout are kept, listed (DeckInfo.unknown) and written back unchanged.

UNIT_SYSTEMS module-attribute

UNIT_SYSTEMS: dict[str, tuple[float, float, float]] = {
    "mm-ms-kg": (1.0, 1.0, 1.0),
    "mm-s-t": (1.0, 1000.0, 1000.0),
    "mm-s-kg": (1.0, 1000.0, 1.0),
    "m-s-kg": (1000.0, 1000.0, 1.0),
}

Unit system -> factors to mm, ms and kg (length, time, mass).

ANALYSES module-attribute

ANALYSES: dict[str, str] = {
    "quality": "model quality and energy checks (crashmetrics.quality, .energy_check)",
    "load_paths": "load paths and energy across runs (crashmetrics.loadpath)",
    "spot_welds": "spot weld failure (crashmetrics.spotweld)",
    "nodes": "head and dummy criteria, intrusions and pulses from nodout (crashmetrics.pedestrian.clearance, .intrusion, .pulse)",
    "contacts": "contact forces (rcforc)",
    "walls": "rigid wall forces (rwforc)",
}

Analyses output_requirements checks for.

Deck

Deck()

A keyword deck: its files, blocks in reading order, parameters.

blocks property

blocks: list[Block]

All blocks in the order LS-DYNA reads them (an include file's blocks where its *INCLUDE is).

find

find(pattern: str) -> list[Block]

Blocks whose keyword matches a pattern (SECTION_SHELL*, CONTACT_*; case ignored).

records

records(pattern: str) -> Iterator[Record]

Typed records of every block matching a pattern.

text

text() -> str

The main file's text.

refresh

refresh() -> None

Read the parameters again and forget derived tables (after parameter edits).

write

write(
    folder: str | Path, *, only_modified: bool = False
) -> list[Path]

Write the deck into a folder: the main file and the include files inside its folder at the same relative paths. Include files outside it are not copied; their *INCLUDE lines are changed to their absolute paths. Returns the files written.

DeckFile

DeckFile(
    deck: Deck,
    path: Path,
    text: str,
    parent: DeckFile | None,
    include: Block | None = None,
)

One file of a deck.

include instance-attribute

include = include

The *INCLUDE block that read this file (None for the main file).

comments

comments() -> list[str]

Comment lines before the first keyword and right after *KEYWORD.

insert

insert(
    block_text: str, before: Block | None = None
) -> Block

Add a keyword block (its text, keyword line first) before a block, or before *END (the end of the file without one).

DeckInfo dataclass

DeckInfo(
    keyword_line: str,
    title: str,
    long_format: bool,
    creators: tuple[str, ...],
    files: int,
    keywords: int,
    unknown: tuple[str, ...],
)

What a deck says about itself and its origin.

creators instance-attribute

creators: tuple[str, ...]

Comment lines about the tool and version that wrote the files.

unknown instance-attribute

unknown: tuple[str, ...]

Keywords without a layout here (kept unchanged).

version property

version: str

The writing tool and version, from the first creator line that names one (empty if none).

Block

Block(file: DeckFile, lines: list[str], start: int)

One keyword with its lines (lines[0] is the keyword line).

start instance-attribute

start = start

Line number of the keyword line in its file (from 1).

long property

long: bool

Long format (20-character fields).

data_lines

data_lines() -> list[tuple[int, str]]

(line number in the block, text) of the data lines: no comments; blank lines count as cards of default values, except at the end.

records

records() -> list[Record]

Typed records (empty for keywords without a layout).

Parameter dataclass

Parameter(
    name: str,
    kind: str,
    value: Any,
    file: DeckFile,
    block: Block,
    record: int = 0,
    local: bool = False,
    expression: str = "",
    slot: tuple[int, Any] | None = None,
)

kind instance-attribute

kind: str

R (real), I (integer) or C (text).

slot class-attribute instance-attribute

slot: tuple[int, Any] | None = None

(line number in the block, Slot) of the value, for editing.

IncludeTransform dataclass

IncludeTransform(
    idnoff: int = 0,
    ideoff: int = 0,
    idpoff: int = 0,
    idmoff: int = 0,
    idsoff: int = 0,
    idfoff: int = 0,
    iddoff: int = 0,
    idroff: int = 0,
    prefix: str = "",
    suffix: str = "",
    fctmas: float = 1.0,
    fcttim: float = 1.0,
    fctlen: float = 1.0,
    fcttem: str = "",
    tranid: int = 0,
)

*INCLUDE_TRANSFORM settings of an include file.

offset

offset(namespace: str) -> int

The offset that applies to IDs of a namespace.

ModelSummary dataclass

ModelSummary(
    parts: tuple[PartInfo, ...],
    lumped_mass: float,
    total_mass: float,
    cg: tuple[float, float, float],
    nodes: int,
    elements: dict[str, int],
    unit_system: str | None,
    notes: tuple[str, ...] = (),
)

lumped_mass instance-attribute

lumped_mass: float

Mass elements without a part.

mass_kg property

mass_kg: float

Total mass in kg (NaN without a unit system).

table

table(top: int | None = None, by: str = 'mass') -> str

Markdown table of the parts (heaviest first by default).

PartInfo dataclass

PartInfo(
    id: int,
    title: str,
    elements: dict[str, int],
    section: int,
    section_type: str,
    elform: int,
    thickness: float,
    material: int,
    material_type: str,
    density: float,
    rigid: bool,
    mass: float,
    cg: tuple[float, float, float],
    file: str,
    note: str = "",
)

elements instance-attribute

elements: dict[str, int]

Element kind -> count.

thickness instance-attribute

thickness: float

Shell thickness (model units; NaN for other parts).

mass instance-attribute

mass: float

Model mass unit.

TimeStepEstimate dataclass

TimeStepEstimate(
    smallest: tuple[ElementTimeStep, ...],
    by_part: dict[int, float],
    tssfac: float,
    dt2ms: float,
    added_mass: float,
    model_mass: float,
    skipped: dict[str, int] = dict(),
    unit_system: str | None = None,
)

added_mass instance-attribute

added_mass: float

Mass that mass scaling would add (model units; 0 without it).

skipped class-attribute instance-attribute

skipped: dict[str, int] = field(default_factory=dict)

Reason -> number of elements without an estimate (rigid, no elastic constants known here, ...).

dt property

dt: float

Smallest estimated time step (model units).

DeckSheet dataclass

DeckSheet(deck: str, checks: tuple[DeckCheck, ...])

DeckCheck dataclass

DeckCheck(
    group: str,
    name: str,
    status: str,
    value: str,
    items: tuple[str, ...] = (),
)

status instance-attribute

status: str

PASS, FAIL, WARN or INFO.

items class-attribute instance-attribute

items: tuple[str, ...] = ()

The items concerned, with where they are (file:line).

LoadCase dataclass

LoadCase(
    title: str,
    unit_system: str | None,
    end_time: float,
    tssfac: float,
    dt2ms: float,
    energy: dict[str, int],
    hourglass: dict[str, float],
    velocities: tuple[str, ...],
    speeds: tuple[float, ...],
    walls: tuple[str, ...],
    motions: tuple[str, ...],
    gravity: tuple[str, ...],
    contacts: tuple[str, ...],
    outputs: dict[str, Output],
    history_nodes: tuple[int, ...],
    history_node_sets: tuple[int, ...],
    cross_sections: tuple[str, ...],
    implicit: bool = False,
    notes: tuple[str, ...] = tuple(),
)

end_time instance-attribute

end_time: float

Model time units (0: not set).

energy instance-attribute

energy: dict[str, int]

CONTROL_ENERGY flags (hgen, rwen, slnten, rylen).

speeds instance-attribute

speeds: tuple[float, ...]

Initial speeds, model velocity units.

kmh

kmh(speed: float) -> float

A model velocity in km/h (NaN without a unit system).

Output dataclass

Output(name: str, dt: float, binary: int, where: str)

name instance-attribute

name: str

GLSTAT, NODOUT ... or BINARY_D3PLOT.

dt instance-attribute

dt: float

Model time units (0: not written at an interval).

ascii property

ascii: bool

Written as an ASCII file (BINARY 1 or 3, or the default).

read_deck

read_deck(path: str | Path) -> Deck

Read a keyword deck and every file it includes.

Problems that do not stop reading (missing include files, undefined parameters, unreadable fields) are listed in Deck.notes.

parse_deck

parse_deck(text: str, name: str = 'deck.k') -> Deck

A deck from text (includes are looked for next to name).

nodes

nodes(deck: Deck) -> Nodes

All *NODE rows (later duplicates are kept; the checks report them).

elements

elements(deck: Deck) -> dict[str, Elements]

Elements by kind (shell, solid, beam, tshell, discrete, seatbelt).

mass_elements

mass_elements(deck: Deck) -> list[MassElement]

sets

sets(deck: Deck) -> dict[tuple[str, int], SetInfo]

All sets by (kind, ID); _ADD sets are expanded.

curves

curves(deck: Deck) -> dict[int, Curve]

model_summary

model_summary(
    deck: Deck, unit_system: str | None = None
) -> ModelSummary

Parts, mass and centre of gravity of a deck (see the module notes).

time_steps

time_steps(
    deck: Deck,
    top: int = 20,
    unit_system: str | None = None,
) -> TimeStepEstimate

Estimated explicit time step of every shell, solid, thick shell and beam (see the module notes): characteristic length over sound speed, times TSSFAC. Shells (ISDO 0): area over the longest side (twice that for triangles); ISDO 1: over the longest diagonal. Solids: volume over the largest face (three times that for tetrahedra). Beams: the length. Rigid parts and materials without E, PR in their first card are skipped.

guess_units

guess_units(deck: Deck) -> str | None

The unit system from *CONTROL_UNITS, else the one most materials' density and Young's modulus agree with (None if unclear).

check_deck

check_deck(
    deck: Deck,
    *,
    unit_system: str | None = None,
    mass_scaling_limit: float = 5.0,
) -> DeckSheet

Check a deck before running it (see the module notes).

mass_scaling_limit: added mass that mass scaling would add, % of the model mass, above which the time step check warns.

load_case

load_case(
    deck: Deck, unit_system: str | None = None
) -> LoadCase

How a deck is set up to run (see the module notes).

output_requirements

output_requirements(
    deck: Deck,
    analyses: Sequence[str] = tuple(ANALYSES),
    *,
    unit_system: str | None = None,
    cfc: float = 1000.0,
) -> DeckSheet

Does the deck write what these analyses read? (keys of ANALYSES)

cfc: filter class of the node channels; the nodout interval must give at least 10 samples per CFC cutoff (CFC 1000: 0.1 ms, 10 kHz).

set_parameter

set_parameter(
    deck: Deck, name: str, value: float | int | str
) -> None

Change a *PARAMETER value (or an expression's text, given as a string); fields that use it read the new value.

part_card

part_card(deck: Deck, part: int) -> Record

The *PART record of a part ID.

set_thickness

set_thickness(
    deck: Deck,
    part: int,
    thickness: float,
    *,
    shared: str = "error",
) -> int

Set the shell thickness of a part (T1 to T4 of its *SECTION_SHELL). If other parts use the section: shared="error" raises, "all" changes it for all of them, "copy" gives this part its own copy of the section (a new section ID, returned).

set_material

set_material(deck: Deck, part: int, material: int) -> None

Give a part another material (the material must exist).

set_section

set_section(deck: Deck, part: int, section: int) -> None

Give a part another section (the section must exist).

set_end_time

set_end_time(deck: Deck, end_time: float) -> None

*CONTROL_TERMINATION ENDTIM (model time units; added if missing).

set_mass_scaling

set_mass_scaling(
    deck: Deck, dt2ms: float, tssfac: float | None = None
) -> None

*CONTROL_TIMESTEP DT2MS (negative: mass added only where the time step is below TSSFAC x |DT2MS|) and optionally TSSFAC.

set_initial_velocity

set_initial_velocity(
    deck: Deck,
    velocity: Sequence[float] | None = None,
    *,
    speed: float | None = None,
    which: int | None = None,
) -> int

Set the initial velocity of *INITIAL_VELOCITY_GENERATION, *INITIAL_VELOCITY or *INITIAL_VELOCITY_RIGID_BODY cards: the components, or only the speed (model units) in the same direction. which: index of the card (all cards by default). Returns the number of cards changed.

set_output

set_output(
    deck: Deck,
    name: str,
    dt: float,
    *,
    binary: int | None = None,
) -> Block

Write an ASCII output (GLSTAT, NODOUT ...) every dt (model time units); binary 1 ASCII, 2 binout, 3 both. Added to the main file if missing.

add_history_nodes

add_history_nodes(
    deck: Deck, nodes: Iterable[int]
) -> Block

Add a *DATABASE_HISTORY_NODE with these nodes (to the main file).

prepare_outputs

prepare_outputs(
    deck: Deck,
    analyses: Sequence[str],
    dt: float,
    *,
    node_dt: float | None = None,
    energy: bool = True,
    unit_system: str | None = None,
) -> list[str]

Add the outputs and energy settings output_requirements finds missing for these analyses (every dt; nodout every node_dt, model time units). Cross sections and history nodes are not made up; they stay as findings. Returns what was changed.

write_variants

write_variants(
    deck: Deck | str | Path,
    design: Any,
    folder: str | Path,
    *,
    setters: Mapping[str, Callable[[Deck, float], None]]
    | None = None,
    name: str = "run_{:03d}",
) -> Path

One deck per design row in folder/run_001, run_002 ...

design: a crashmetrics.doe.Design or (names, rows). A variable with a setter in setters (lambda deck, value: ...) is applied with it, any other must be a *PARAMETER of the deck. Each variant is read fresh from the original files. Writes design.csv (run folder and values) and returns its path.

main

main(argv: list[str] | None = None) -> int

crashmetrics-deck MAIN.k: model summary, checks, load case and output requirements of a deck. Exit code 1 if a check fails.