montepy.mcnp_object module#

Classes:

MCNP_Object(input, parser)

Abstract class for semantic representations of MCNP inputs.

class montepy.mcnp_object.MCNP_Object(input: Input | str, parser: MCNP_Parser)#

Bases: ABC

Abstract class for semantic representations of MCNP inputs.

Parameters:
  • input (Union[Input, str]) – The Input syntax object this will wrap and parse.

  • parser (MCNP_Parser) – The parser object to parse the input with.

Methods:

_generate_default_node(value_type, default)

Generates a "default" or blank ValueNode.

_update_values()

Method to update values in syntax tree with new values.

clone()

Create a new independent instance of this object.

format_for_mcnp_input(mcnp_version)

Creates a list of strings representing this MCNP_Object that can be written to file.

link_to_problem(problem)

Links the input to the parent problem for this input.

mcnp_str([mcnp_version])

Returns a string of this input as it would appear in an MCNP input file.

validate()

Validates that the object is in a usable state.

wrap_string_for_mcnp(string, mcnp_version, ...)

Wraps the list of the words to be a well formed MCNP input.

Attributes:

comments

The comments associated with this input if any.

leading_comments

Any comments that come before the beginning of the input proper.

parameters

A dictionary of the additional parameters for the object.

trailing_comment

The trailing comments and padding of an input.

static _generate_default_node(value_type: type, default: str, padding: str = ' ', never_pad: bool = False)#

Generates a “default” or blank ValueNode.

None is generally a safe default value to provide.

Changed in version 1.0.0: Added never_pad argument.

Parameters:
  • value_type (Class) – the data type for the ValueNode.

  • default (value_type) – the default value to provide (type needs to agree with value_type)

  • padding (str, None) – the string to provide to the PaddingNode. If None no PaddingNode will be added.

  • never_pad (bool) – Whether to never add trailing padding. True means extra padding is suppressed.

Returns:

a new ValueNode with the requested information.

Return type:

ValueNode

static wrap_string_for_mcnp(string, mcnp_version, is_first_line, suppress_blank_end=True) list[str]#

Wraps the list of the words to be a well formed MCNP input.

multi-line inputs will be handled by using the indentation format, and not the “&” method.

Parameters:
  • string (str) – A long string with new lines in it, that needs to be chunked appropriately for MCNP inputs

  • mcnp_version (tuple) – the tuple for the MCNP that must be formatted for.

  • is_first_line (bool) – If true this will be the beginning of an MCNP input. The first line will not be indented.

  • suppress_blank_end (bool) – Whether or not to suppress any blank lines that would be added to the end. Good for anywhere but cell modifiers in the cell block.

Returns:

A list of strings that can be written to an input file, one item to a line.

Return type:

list

abstractmethod _update_values()#

Method to update values in syntax tree with new values.

Generally when make_prop_val_node() this is not necessary to do, but when make_prop_pointer() is used it is necessary. The most common need is to update a value based on the number for an object pointed at, e.g., the material number in a cell definition.

clone() MCNP_Object#

Create a new independent instance of this object.

Returns:

a new instance identical to this object.

Return type:

type(self)

format_for_mcnp_input(mcnp_version: tuple[int]) list[str]#

Creates a list of strings representing this MCNP_Object that can be written to file.

Parameters:

mcnp_version (tuple[int]) – The tuple for the MCNP version that must be exported to.

Returns:

a list of strings for the lines that this input will occupy.

Return type:

list

Links the input to the parent problem for this input.

This is done so that inputs can find links to other objects.

Parameters:

problem (MCNP_Problem) – The problem to link this input to.

mcnp_str(mcnp_version: tuple[int] = None)#

Returns a string of this input as it would appear in an MCNP input file.

..versionadded:: 1.0.0

Parameters:

mcnp_version (tuple[int]) – The tuple for the MCNP version that must be exported to.

Returns:

The string that would have been printed in a file

Return type:

str

validate()#

Validates that the object is in a usable state.

property comments: list[PaddingNode]#

The comments associated with this input if any.

This includes all C comments before this card that aren’t part of another card, and any comments that are inside this card.

Returns:

a list of the comments associated with this comment.

Return type:

list

property leading_comments: list[PaddingNode]#

Any comments that come before the beginning of the input proper.

Returns:

the leading comments.

Return type:

list

property parameters: dict[str, str]#

A dictionary of the additional parameters for the object.

e.g.: 1 0 -1 u=1 imp:n=0.5 has the parameters {"U": "1", "IMP:N": "0.5"}

Returns:

a dictionary of the key-value pairs of the parameters.

Return type:

unknown

Rytpe:

dict

property trailing_comment: list[PaddingNode]#

The trailing comments and padding of an input.

Generally this will be blank as these will be moved to be a leading comment for the next input.

Returns:

the trailing c style comments and intermixed padding (e.g., new lines)

Return type:

list