ansys.dyna.core.lib.deck.Deck#

class ansys.dyna.core.lib.deck.Deck(title: str = None, **kwargs)#

Bases: ansys.dyna.core.lib.validation_mixin.ValidationMixin

Provides a collection of keywords that can read and write to a keyword file.

property comment_header: str | None#

Comment header of the keyword database.

property title: str | None#

Title of the keyword database.

property format: ansys.dyna.core.lib.format_type.format_type#

Format type of the deck.

__add__(other)#

Add two decks together.

clear()#

Clear all keywords from the deck.

property transform_handler: ansys.dyna.core.lib.transform.TransformHandler#

Handles transformations for the deck.

register_import_handler(import_handler: ansys.dyna.core.lib.import_handler.ImportHandler) None#

Registers an ImportHandler object

property parameters: ansys.dyna.core.lib.parameters.ParameterSet#

Deck parameters.

append(keyword: ansys.dyna.core.lib.keyword_base.KeywordBase | str, check=False) None#

Add a keyword to the collection.

Parameters:
keywordUnion[KeywordBase, EncryptedKeyword, str]

Keyword. The keyword can be KeywordBase, EncryptedKeyword, or a string.

checkbool, optional

The default is False.

remove(index: int | list[int]) None#

Remove a keyword from the collection by index.

Parameters:
indexint or list of int, mandatory
property all_keywords: List[str | ansys.dyna.core.lib.keyword_base.KeywordBase | ansys.dyna.core.lib.encrypted_keyword.EncryptedKeyword]#

List of all keywords.

property string_keywords: List[str]#

List of keywords as a raw string.

property encrypted_keywords: List[str]#

List of keywords as a raw string.

property keywords: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all processed keywords as a KeywordCollection.

This provides access to the fluent filtering API for all keywords in the deck. The collection supports indexing, iteration, and comparison with lists for backward compatibility.

Returns:
KeywordCollection

A collection of all KeywordBase instances in the deck.

Examples

>>> # Access by index (backward compatible)
>>> first = deck.keywords[0]
>>> # Iterate (backward compatible)
>>> for kwd in deck.keywords:
...     print(kwd.keyword)
>>> # Filter with fluent API
>>> high_id_sections = deck.keywords.where(
...     lambda k: k.keyword == "SECTION" and k.secid > 100
... )
extend(kwlist: list) None#

Add a list of keywords to the deck.

Parameters:
kwlistlist

List of keywords.

expand(cwd=None, recurse=True, strict=False)#

Get a new deck that is flattened copy of self.

A flattened deck is one where the *INCLUDE keywords are replaced by the contents of the file that is included.

Parameters:
cwdstr, optional

Working directory used to resolve include filenames. Defaults to the current working directory.

recursebool, optional

If True, *INCLUDE keywords within included decks are expanded recursively. Default is True.

strictbool, optional

If True, raise errors when keyword parsing fails for any reason (undefined parameters, invalid field values, malformed data, etc.). If False (default), keywords that fail to parse are retained as raw strings and a warning is emitted. Default is False for backward compatibility. TODO: Consider making strict=True the default in a future version.

dumps() str#

Get the keyword file representation of all keywords as a string.

Returns:
str

Keyword file representation of all keywords as a string.

write(buf: TextIO | None = None, format: ansys.dyna.core.lib.format_type.format_type | None = None, validate: bool = False, retain_parameters: bool = False)#

Write the card in the dyna keyword format.

Parameters:
bufoptional

Buffer to write to. The default is None, in which case the output is returned as a string.

formatoptional

Format to write in. The default is None.

validatebool, optional

If True, validate the deck before writing. The default is False. Validation uses registered validators and raises ValidationError if errors are found.

retain_parametersbool, optional

If True, write original parameter references (e.g., &myvar) instead of substituted values for fields that were read from parameters. Default is False.

loads(value: str, context: ansys.dyna.core.lib.import_handler.ImportContext | None = None) ansys.dyna.keywords.lib.deck_loader.DeckLoaderResult#

Load all keywords from the keyword file as a string.

When adding all keywords from the file, this method overwrites the title and user comment, if any.

Parameters:
valuestr
context: ImportContext

the context

get_kwds_by_type(str_type: str) Iterator[ansys.dyna.core.lib.keyword_base.KeywordBase]#

Get all keywords for a given type.

Parameters:
str_typestr

Keyword type.

Returns:
typing.Iterator[KeywordBase]

Examples

Get all *SECTION_* keywords in the deck.

>>>deck.get_kwds_by_type(“SECTION”)

get_kwds_by_full_type(str_type: str, str_subtype: str) Iterator[ansys.dyna.core.lib.keyword_base.KeywordBase]#

Get all keywords for a given full type.

Parameters:
str_typestr

Keyword type.

str_subtypestr

Keyword subtype or prefix of subtype. Matches keywords where the subkeyword starts with this value.

Returns:
typing.Iterator[KeywordBase]

Examples

Get all *SECTION_SHELL keyword instances in the deck.

>>>deck.get_kwds_by_full_type(“SECTION”, “SHELL”)

Get all *SET_NODE* keyword instances (matches NODE, NODE_LIST, NODE_LIST_TITLE, etc).

>>>deck.get_kwds_by_full_type(“SET”, “NODE”)

get_section_by_id(id: int) ansys.dyna.core.lib.keyword_base.KeywordBase | None#

Get the SECTION keyword in the collection for a given section ID.

Parameters:
idint

Section ID.

Returns:
SECTION keyword or None if there is no SECTION keyword that matches the section ID.
Raises:
Exception

If multiple SECTION keywords use the given section ID.

get_set_by_id(id: int) ansys.dyna.core.lib.keyword_base.KeywordBase | None#

Get the SET keyword in the collection for a given set ID.

Parameters:
idint

Set ID.

Returns:
SET keyword or None if there is no SET keyword that matches the set ID.
Raises:
Exception

If multiple SET keywords use the given set ID.

get(**kwargs) List[ansys.dyna.core.lib.keyword_base.KeywordBase]#

Get a list of keywords.

Parameters:
- *kwargs* (``dict``) –

Keyword arguments. * type (str) – The type of keyword to get. For example, “SECTION” returns all section keywords. * filter (callable) – The filter to apply to the result. Only keywords which pass the filter will be returned.

import_file(path: str, encoding: str = 'utf-8') ansys.dyna.keywords.lib.deck_loader.DeckLoaderResult#

Import a keyword file.

Parameters:
pathstr

Full path for the keyword file.

encoding: str

String encoding used to read the keyword file.

export_file(path: str, encoding='utf-8', validate: bool = False, retain_parameters: bool = False) None#

Export the keyword file to a new keyword file.

Parameters:
pathstr

Full path for the new keyword file.

encodingstr, optional

String encoding for the file. The default is “utf-8”.

validatebool, optional

If True, validate the deck before export. The default is False. Validation uses registered validators and raises ValidationError if errors are found.

retain_parametersbool, optional

If True, write original parameter references (e.g., &myvar) instead of substituted values for fields that were read from parameters. Default is False.

Examples

>>> deck.export_file("output.k", validate=True)  # Validate before export
>>> deck.export_file("output.k", retain_parameters=True)  # Keep parameter references
property keyword_names: List[str]#

Get a list of all keyword names in the deck.

__repr__() str#

Get a console-friendly representation of a list of all keywords in the deck.

__getitem__(key: str | Tuple[str, str]) ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get keywords by type using dict-like access.

Parameters:
keyUnion[str, Tuple[str, str]]

Either a keyword type (e.g., “SECTION”) or a tuple of (type, subtype) (e.g., (“SECTION”, “SHELL”)).

Returns:
KeywordCollection

A collection of matching keywords.

Examples

>>> sections = deck["SECTION"]
>>> shells = deck["SECTION", "SHELL"]
__iter__() Iterator[ansys.dyna.core.lib.keyword_base.KeywordBase | str | ansys.dyna.core.lib.encrypted_keyword.EncryptedKeyword]#

Iterate over all keywords in the deck.

Returns:
Iterator[Union[KeywordBase, str, EncryptedKeyword]]

An iterator over all keywords.

Examples

>>> for kwd in deck:
...     if isinstance(kwd, KeywordBase):
...         print(kwd.keyword)
__len__() int#

Get the number of keywords in the deck.

Returns:
int

The total number of keywords (including strings and encrypted).

Examples

>>> num_keywords = len(deck)
property sections: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all SECTION_* keywords.

Returns:
KeywordCollection

A collection of all section keywords.

Examples

>>> shells = deck.sections.by_subtype("SHELL")
property materials: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all MAT_* keywords.

Returns:
KeywordCollection

A collection of all material keywords.

Examples

>>> elastic_mats = deck.materials.by_subtype("ELASTIC")
property elements: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all ELEMENT_* keywords.

Returns:
KeywordCollection

A collection of all element keywords.

Examples

>>> solid_elements = deck.elements.by_subtype("SOLID")
property nodes: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all NODE keywords.

Returns:
KeywordCollection

A collection of all node keywords.

Examples

>>> all_nodes = deck.nodes.to_list()
property parts: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all PART* keywords.

Returns:
KeywordCollection

A collection of all part keywords.

Examples

>>> parts = deck.parts.to_list()
property sets: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all SET_* keywords.

Returns:
KeywordCollection

A collection of all set keywords.

Examples

>>> node_sets = deck.sets.by_subtype("NODE")
property defines: ansys.dyna.core.lib.keyword_collection.KeywordCollection#

Get all DEFINE_* keywords.

Returns:
KeywordCollection

A collection of all define keywords.

Examples

>>> curves = deck.defines.by_subtype("CURVE")
split(key_func: Callable[[ansys.dyna.core.lib.keyword_base.KeywordBase | str | ansys.dyna.core.lib.encrypted_keyword.EncryptedKeyword], Any]) Dict[Any, Deck]#

Split the deck into multiple decks based on a key function.

The key function is called on each keyword and should return a value that will be used to group keywords. Keywords with the same return value will be placed in the same deck.

Parameters:
key_funcCallable[[Union[KeywordBase, str, EncryptedKeyword]], Any]

A function that takes a keyword and returns a grouping key.

Returns:
Dict[Any, Deck]

A dictionary mapping keys to Deck instances.

Examples

>>> # Split by keyword type
>>> decks_by_type = deck.split(lambda k: k.keyword if isinstance(k, KeywordBase) else "string")
>>> section_deck = decks_by_type["SECTION"]
>>> # Split by domain using the domain mapper
>>> from ansys.dyna.core.lib.domain_mapper import by_domain
>>> decks_by_domain = deck.split(by_domain)
>>> mat_deck = decks_by_domain["mat"]
>>> # Custom splitting logic
>>> def by_id_range(kwd):
...     if isinstance(kwd, KeywordBase) and hasattr(kwd, 'secid'):
...         if kwd.secid < 100:
...             return "low"
...         else:
...             return "high"
...     return "other"
>>> decks = deck.split(by_id_range)
split_by_domain() Dict[str, Deck]#

Split the deck by keyword domain (mat, section, control, etc.).

This is a convenience method that uses the domain mapper to categorize keywords. For custom splitting logic, use the split() method directly.

Returns:
Dict[str, Deck]

A dictionary mapping domain names to Deck instances.

See also

split

Generic splitting method with custom key function.

Examples

>>> decks = deck.split_by_domain()
>>> mat_deck = decks["mat"]
>>> control_deck = decks["control"]
>>> section_deck = decks["section"]
plot(**args)#

Plot the node and element of the mesh using PyVista.

Automatically detects Jupyter notebook environments and configures appropriate rendering backend for display.

Parameters:
cwdstr, optional

Current working directory if the deck and include files are in a separate directory.

jupyter_backendstr, optional

Jupyter backend to use. Options are:

  • 'static' - Static image (default for notebooks)

  • 'server' - Interactive with server backend

  • 'trame' - Interactive with Trame (requires pyvista[trame])

  • None - Disable Jupyter mode, use default windowing

  • 'auto' - Automatically detect (default)

colorstr, optional

Color of the mesh.

scalarsstr, optional

Name of scalars to color by (e.g., ‘part_ids’, ‘element_ids’).

**args

Additional keyword arguments passed to PyVista’s plot method.

Returns:
Depends on backend

Camera position or plot object depending on the backend used.

Examples

Plot a deck in a regular Python script:

>>> deck.plot()

Plot with a specific working directory:

>>> deck.plot(cwd='/path/to/includes')

Plot in a Jupyter notebook with interactive backend:

>>> deck.plot(jupyter_backend='server')

Color by part IDs:

>>> deck.plot(scalars='part_ids')