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

class ansys.dyna.core.lib.parameters.ParameterSet(parent: ParameterSet | None = None)#

Hierarchical parameter storage for LS-DYNA deck parameters.

Child scopes see parent parameters but local parameters don’t leak upward.

get(param: str) Any#

Get a parameter by name, checking local then parent scopes.

Parameter lookup is case-insensitive, matching LS-DYNA behaviour. Parameters are stored with their original casing; lookup is case-insensitive.

Parameters:
paramstr

Parameter name to lookup.

Returns:
Any

Parameter value.

Raises:
KeyError

If parameter is not found in this scope or any parent scope.

add(param: str, value: Any) None#

Add a parameter to the local scope.

This method is for global parameters (PARAMETER keyword). They are added to the local scope but will be visible to child scopes. Parameters are stored with their original casing; lookup is case-insensitive.

Parameters:
paramstr

Parameter name.

valueAny

Parameter value.

add_local(param: str, value: Any) None#

Add a parameter to local scope only (PARAMETER_LOCAL).

Local parameters are only visible within the current scope and child scopes created from it, but won’t leak to parent or sibling scopes. Parameters are stored with their original casing; lookup is case-insensitive.

Parameters:
paramstr

Parameter name.

valueAny

Parameter value.

copy_with_child_scope() ParameterSet#

Create a new ParameterSet with this as the parent scope.

The child scope will be able to see parameters from this scope, but parameters added to the child won’t leak back to this scope.

Returns:
ParameterSet

New ParameterSet with this as parent.

scope(segment: str) Generator[None, None, None]#

Context manager to scope a URI segment during card reading.

As the read stack descends (keyword -> card -> field), each layer pushes its segment and pops on exit. Parameter references are recorded at the current full URI path.

Parameters:
segmentstr

URI segment to push (e.g., keyword id, card index, field name).

Yields:
None

Examples

>>> with parameter_set.scope("12345"):  # keyword id
...     with parameter_set.scope("card0"):
...         parameter_set.record_ref("secid", "&mysec")
...         # Records at URI: "12345/card0/secid"
set_context(context: _RefContext | None) None#

Set the current context for subsequent record_ref calls.

The context is stored opaquely; the caller decides the convention (e.g. ImportContext with path/line_number for deck loading).

record_ref(field: str, ref: str) None#

Record a parameter reference at the current URI path.

Called during card reading when a parameter reference (e.g., &myvar) is resolved. Stores the original reference string so it can be written back instead of the resolved value.

Parameters:
fieldstr

Field name or index to append to current URI path.

refstr

The original parameter reference string (e.g., “&myvar”, “-&density”).

get_ref(*path_segments: str) str | None#

Get the parameter reference string for a URI path.

Called during card writing to check if a field value came from a parameter reference that should be written instead of the value.

Parameters:
*path_segmentsstr

URI path segments to join (e.g., keyword_id, card_index, field_name).

Returns:
str or None

The original parameter reference string if one was recorded, or None if the value was a literal.

get_current_uri() str#

Get the current URI path as a string.

Returns:
str

The current URI path joined with ‘/’.

merge_refs_from(other: ParameterSet) None#

Copy parameter reference recordings from another ParameterSet.

Used when a keyword is assigned to a deck: the keyword’s refs are merged into the deck’s parameters so the deck owns both values and refs.

extract_refs_for_prefix(prefix: str, exclude_params: Set[str] | None = None) Tuple[Dict[str, str], Dict[str, _RefContext]]#

Extract and remove refs whose URI starts with the given prefix.

Used when a keyword is removed from a deck: extract its refs so they can be restored to an independent ParameterSet.

When exclude_params is provided (e.g. local param names from an include), refs to those params are removed but not returned. This bakes in local parameter values during deck expansion.

Parameters:
prefixstr

URI prefix to match.

exclude_paramsset of str, optional

Param names to exclude. Refs to these are dropped (not extracted).

Returns:
tuple of (dict, dict)

Extracted refs (uri -> ref string) and locations (uri -> opaque context).

add_refs(refs: Dict[str, str], locations: Dict[str, _RefContext] | None = None) None#

Add refs from a dict (uri -> ref string). Used when restoring refs to an independent set.

get_unresolved_param_names(uri_prefix: str) List[str]#

Get parameter names for refs under uri_prefix that are not defined in this set.

Used after loading to check if any parameter references could not be resolved. Extracts param names from ref strings (e.g. &base, &base+5, -&density) and returns those that are not in the parameter set.

Parameters:
uri_prefixstr

URI prefix to filter refs (e.g. keyword id from scope).

Returns:
list of str

Parameter names that were referenced but not defined.

get_all_unresolved_param_names() List[Tuple[str, _RefContext]]#

Get all parameter names referenced in this set that are not defined.

Unlike get_unresolved_param_names(), this checks all refs without filtering by prefix. Used at the end of deck.expand() to warn about any unresolved parameters.

Returns:
list of tuple

Each tuple is (param_name, context). Context is opaque; caller interprets it (e.g. ImportContext with path/line_number for deck loading).

get_local_param_names() Set[str]#

Get names of local parameters defined in this scope only.

Used when extracting refs during deck expansion: refs to local params should not be carried to the expanded deck (values are baked in).

Returns:
set of str

Local parameter names in this scope.

get_global_params() Dict[str, Any]#

Get only the global parameters defined in this scope.

Returns only parameters added via add() (PARAMETER keyword), not parameters added via add_local() (PARAMETER_LOCAL keyword).

Returns:
dict

Dictionary of global parameters defined in this scope only.