Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an ab initio parse method to quickly make new objects from an MCNP string. #595

Merged
merged 46 commits into from
Jan 13, 2025
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
3883295
Made all objects parsable from a bare string.
MicahGale Nov 22, 2024
013d15a
Updated tests to test using a str init.
MicahGale Nov 22, 2024
2c5350b
Fixed case of overriding subclass var.
MicahGale Nov 22, 2024
1772873
Added number parameter to all numbered mcnp objects.
MicahGale Nov 22, 2024
e2a7079
added universal parse method.
MicahGale Nov 22, 2024
0009c78
Test number init.
MicahGale Nov 22, 2024
d3d1acc
Fixed various bugs with how default numbers are loaded.
MicahGale Nov 22, 2024
350accd
Added #88 to changelog.
MicahGale Nov 22, 2024
c8b5187
Tested parse.
MicahGale Nov 22, 2024
a2d6081
Actually appended the objects to self.
MicahGale Nov 22, 2024
c8dd2e4
Updated starting guide to use number constructor.
MicahGale Nov 22, 2024
085cc3f
test type enforcement.
MicahGale Nov 22, 2024
fb68afb
Documented how to use parse.
MicahGale Nov 22, 2024
cb17eac
Formatted with black.
MicahGale Nov 22, 2024
a194a45
Fixed typo in doctest.
MicahGale Nov 22, 2024
5fb2b83
Merge branch 'alpha-test-dev' into parse_method
MicahGale Dec 5, 2024
813d35f
Post-merge black formatting.
MicahGale Dec 5, 2024
eba0d1d
Fixed name errors.
MicahGale Dec 5, 2024
403fa45
Made lazy pretty print, and abandoned parens.
MicahGale Dec 6, 2024
a1897ba
Made sure mat number is never padded.
MicahGale Dec 6, 2024
624cbd8
Switched positional to keyword arg.
MicahGale Dec 6, 2024
2cf7f2a
Tested pretty str.
MicahGale Dec 6, 2024
eca2d48
Handled more pretty str edge cases.
MicahGale Dec 6, 2024
c54e5bb
Merge branch 'alpha-test-dev' into parse_method
MicahGale Dec 12, 2024
2d92f36
Updated changelog to point to issue and not PR.
MicahGale Dec 14, 2024
7dcbbd7
Removed errant doc string.
MicahGale Dec 14, 2024
5f83b97
Updated all object init to use a type alias hint.
MicahGale Dec 14, 2024
e855ca2
Updated typeerror with more guidance.
MicahGale Dec 14, 2024
09cf9f9
Py39 can't check isisntance of a Union type.
MicahGale Dec 15, 2024
39bdd02
Merge branch 'develop' into parse_method
MicahGale Dec 15, 2024
736851a
Fixed typo with pyproject from merge.
MicahGale Dec 15, 2024
74f9af9
Merge branch 'alpha-test-dev' into parse_method
MicahGale Dec 16, 2024
0c21e86
Merge branch 'alpha-test-dev' into parse_method
MicahGale Dec 16, 2024
52ae985
Merge branch 'alpha-test-dev' into parse_method
MicahGale Jan 6, 2025
f418865
Merge branch 'alpha-test-dev' into parse_method
MicahGale Jan 9, 2025
9556f84
Fixed circular import.
MicahGale Jan 11, 2025
7f18ee7
Added append option to MCNP_Problem.parse.
MicahGale Jan 11, 2025
ffcb68d
Tested parse append.
MicahGale Jan 11, 2025
6b9baf7
Promoted parsing functions to be top level functions.
MicahGale Jan 11, 2025
565d19c
Made parse_surface function name more pythonic.
MicahGale Jan 11, 2025
e76db62
hid pretty_str as it's not ready yet.
MicahGale Jan 11, 2025
2663481
Added demo of parse functions.
MicahGale Jan 11, 2025
843780d
Added demo of append option.
MicahGale Jan 11, 2025
8b184de
Updated tests for pretty_str change.
MicahGale Jan 11, 2025
c8abb71
Updated references to deprecated surface_builder.
MicahGale Jan 11, 2025
4838a86
Removed all version change markers for 0.2.0
MicahGale Jan 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added number parameter to all numbered mcnp objects.
MicahGale committed Nov 22, 2024
commit 177287301a4f88190be92272ca58b8cf62ca9627
3 changes: 2 additions & 1 deletion montepy/__init__.py
Original file line number Diff line number Diff line change
@@ -7,14 +7,15 @@
You will receive an MCNP_Problem object that you will interact with.
"""

from . import data_inputs
from . import input_parser
from . import constants
import importlib.metadata
from .input_parser.input_reader import read_input
from montepy.cell import Cell
from montepy.mcnp_problem import MCNP_Problem
from montepy.data_inputs.material import Material
from montepy.data_inputs.transform import Transform
from montepy.mcnp_problem import MCNP_Problem
from montepy.geometry_operators import Operator
from montepy import geometry_operators
from montepy.input_parser.mcnp_input import Jump
15 changes: 12 additions & 3 deletions montepy/cell.py
Original file line number Diff line number Diff line change
@@ -35,9 +35,15 @@ class Cell(Numbered_MCNP_Object):
.. versionchanged:: 0.2.0
Removed the ``comments`` argument due to overall simplification of init process.

.. versionchanged:: 1.0.0

Added number parameter


:param input: The Input syntax object this will wrap and parse.
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int

.. seealso::

@@ -74,7 +80,11 @@ class Cell(Numbered_MCNP_Object):
}
_parser = CellParser()

def __init__(self, input=None):
def __init__(
self,
input: Union[montepy.input_parser.mcnp_input.Input, str] = None,
number: int = None,
):
self._BLOCK_TYPE = montepy.input_parser.block_type.BlockType.CELL
self._material = None
self._old_number = self._generate_default_node(int, -1)
@@ -83,8 +93,7 @@ def __init__(self, input=None):
self._density_node = self._generate_default_node(float, None)
self._surfaces = Surfaces()
self._complements = Cells()
self._number = self._generate_default_node(int, -1)
super().__init__(input, self._parser)
super().__init__(input, self._parser, number)
if not input:
self._generate_default_tree()
self._old_number = copy.deepcopy(self._tree["cell_num"])
30 changes: 22 additions & 8 deletions montepy/data_inputs/material.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from __future__ import annotations
import copy
import itertools
import re
from typing import Union
import warnings

from montepy.data_inputs import data_input, thermal_scattering
from montepy.data_inputs.isotope import Isotope
from montepy.data_inputs.material_component import MaterialComponent
@@ -9,10 +15,7 @@
from montepy.numbered_mcnp_object import Numbered_MCNP_Object
from montepy.errors import *
from montepy.utilities import *
import itertools
import re

import warnings
import montepy


class Material(data_input.DataInputAbstract, Numbered_MCNP_Object):
@@ -23,19 +26,30 @@ class Material(data_input.DataInputAbstract, Numbered_MCNP_Object):

There is a known bug (:issue:`182`) that valid MCNP material definitions cannot be parsed.

.. versionchanged:: 1.0.0

Added number parameter

:param input: the input card that contains the data
:type input: Input
:param input: The Input syntax object this will wrap and parse.
:type input: Union[Input, str]
:param parser: The parser object to parse the input with.
:type parser: MCNP_Parser
:param number: The number to set for this object.
:type number: int
"""

_parser = MaterialParser()

def __init__(self, input=None):
def __init__(
self,
input: Union[montepy.input_parser.mcnp_input.Input, str] = None,
number: int = None,
):
self._material_components = {}
self._thermal_scattering = None
self._is_atom_fraction = True
self._number = self._generate_default_node(int, -1)
super().__init__(input)
super().__init__(input, number)
if input:
num = self._input_number
self._old_number = copy.deepcopy(num)
27 changes: 21 additions & 6 deletions montepy/data_inputs/transform.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from __future__ import annotations
import copy
import numpy as np
import re
from typing import Union

import montepy
from montepy import mcnp_object
from montepy.data_inputs import data_input
from montepy.errors import *
from montepy.numbered_mcnp_object import Numbered_MCNP_Object
from montepy.utilities import *
import numpy as np
import re


class Transform(data_input.DataInputAbstract, Numbered_MCNP_Object):
"""
Input to represent a transform input (TR).

:param input: The Input syntax object this will wrap and parse.
:type input: Input
.. versionchanged:: 1.0.0

Added number parameter

:param input: The Input object representing the input
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int
"""

def __init__(self, input=None, pass_through=False):
def __init__(
self,
input: union[montepy.input_parser.mcnp_input.input, str] = None,
pass_through: bool = False,
number: int = None,
):
self._pass_through = pass_through
self._number = self._generate_default_node(int, -1)
self._old_number = self._generate_default_node(int, -1)
self._displacement_vector = np.array([])
self._rotation_matrix = np.array([])
self._is_in_degrees = False
self._is_main_to_aux = True
super().__init__(input)
super().__init__(input, number)
if input:
words = self._tree["data"]
i = 0
5 changes: 1 addition & 4 deletions montepy/data_inputs/universe_input.py
Original file line number Diff line number Diff line change
@@ -42,10 +42,7 @@ def __init__(self, input=None, in_cell_block=False, key=None, value=None):
for node in self.data:
try:
node.is_negatable_identifier = True
if node.value is not None:
self._old_numbers.append(node)
else:
self._old_numbers.append(node)
self._old_numbers.append(node)
except ValueError:
raise MalformedInputError(
input,
4 changes: 2 additions & 2 deletions montepy/input_parser/input_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from montepy import mcnp_problem
import montepy
from montepy.constants import DEFAULT_VERSION


@@ -26,7 +26,7 @@ def read_input(destination, mcnp_version=DEFAULT_VERSION, replace=True):
:raises BrokenObjectLinkError: If a reference is made to an object that is not in the input file.
:raises UnknownElement: If an isotope is specified for an unknown element.
"""
problem = mcnp_problem.MCNP_Problem(destination)
problem = montepy.mcnp_problem.MCNP_Problem(destination)
problem.mcnp_version = mcnp_version
problem.parse_input(replace=replace)
return problem
2 changes: 1 addition & 1 deletion montepy/mcnp_object.py
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ class MCNP_Object(ABC, metaclass=_ExceptionContextAdder):
:param input: The Input syntax object this will wrap and parse.
:type input: Union[Input, str]
:param parser: The parser object to parse the input with.
:type parser: MCNP_Lexer
:type parser: MCNP_Parser
"""

"""
35 changes: 35 additions & 0 deletions montepy/numbered_mcnp_object.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from __future__ import annotations
from abc import abstractmethod
import copy
import itertools
from typing import Union


from montepy.errors import NumberConflictError
from montepy.mcnp_object import MCNP_Object
import montepy
@@ -30,6 +34,37 @@ def _number_validator(self, number):


class Numbered_MCNP_Object(MCNP_Object):
"""
An abstract class to represent an mcnp object that has a number.

.. versionchanged:: 1.0.0

Added number parameter

:param input: The Input syntax object this will wrap and parse.
:type input: Union[Input, str]
:param parser: The parser object to parse the input with.
:type parser: MCNP_Parser
:param number: The number to set for this object.
:type number: int
"""

def __init__(
self,
input: Union[montepy.input_parser.mcnp_input.Input, str],
parser: montepy.input_parser.parser_base.MCNP_Parser,
number: int = None,
):
self._number = self._generate_default_node(int, -1)
super().__init__(input, parser)
if number is not None:
if not isinstance(number, int):
raise TypeError(
f"Number must be an int. {number} of type {type(number)} given."
)
if number < 0:
raise ValueError(f"Number must be 0 or greater. {number} given.")
self.number = number

@make_prop_val_node("_number", int, validator=_number_validator)
def number(self):
12 changes: 9 additions & 3 deletions montepy/surfaces/axis_plane.py
Original file line number Diff line number Diff line change
@@ -9,15 +9,21 @@ class AxisPlane(Surface):
"""
Represents PX, PY, PZ

.. versionchanged:: 1.0.0

Added number parameter

:param input: The Input object representing the input
:type input: Input
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int
"""

COORDINATE = {SurfaceType.PX: "x", SurfaceType.PY: "y", SurfaceType.PZ: "z"}

def __init__(self, input=None):
def __init__(self, input=None, number: int = None):
self._location = self._generate_default_node(float, None)
super().__init__(input)
super().__init__(input, number)
ST = SurfaceType
if input:
if self.surface_type not in [ST.PX, ST.PY, ST.PZ]:
13 changes: 10 additions & 3 deletions montepy/surfaces/cylinder_on_axis.py
Original file line number Diff line number Diff line change
@@ -14,13 +14,20 @@ class CylinderOnAxis(Surface):
"""
Represents surfaces: CX, CY, CZ

.. versionchanged:: 1.0.0

Added number parameter


:param input: The Input object representing the input
:type input: Input
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int
"""

def __init__(self, input=None):
def __init__(self, input=None, number: int = None):
self._radius = self._generate_default_node(float, None)
super().__init__(input)
super().__init__(input, number)
ST = SurfaceType
if input:
if self.surface_type not in [ST.CX, ST.CY, ST.CZ]:
12 changes: 9 additions & 3 deletions montepy/surfaces/cylinder_par_axis.py
Original file line number Diff line number Diff line change
@@ -14,8 +14,14 @@ class CylinderParAxis(Surface):
"""
Represents surfaces: C/X, C/Y, C/Z

.. versionchanged:: 1.0.0

Added number parameter

:param input: The Input object representing the input
:type input: Input
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int
"""

COORDINATE_PAIRS = {
@@ -26,13 +32,13 @@ class CylinderParAxis(Surface):
"""Which coordinate is what value for each cylinder type.
"""

def __init__(self, input=None):
def __init__(self, input=None, number: int = None):
self._coordinates = [
self._generate_default_node(float, None),
self._generate_default_node(float, None),
]
self._radius = self._generate_default_node(float, None)
super().__init__(input)
super().__init__(input, number)
ST = SurfaceType
if input:
if self.surface_type not in [ST.C_X, ST.C_Y, ST.C_Z]:
19 changes: 17 additions & 2 deletions montepy/surfaces/general_plane.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from typing import Union

import montepy
from montepy.errors import *
from montepy.surfaces.surface_type import SurfaceType
from montepy.surfaces.surface import Surface
@@ -8,12 +11,24 @@ class GeneralPlane(Surface):
"""
Represents P

.. versionchanged:: 1.0.0

Added number parameter

:param input: The Input object representing the input
:type input: Input
:param input: The Input object representing the input
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int
"""

def __init__(self, input=None):
super().__init__(input)
def __init__(
self,
input: Union[montepy.input_parser.mcnp_input.Input, str] = None,
number: int = None,
):
super().__init__(input, number)
if input:
if self.surface_type != SurfaceType.P:
raise ValueError("A GeneralPlane must be a surface of type P")
14 changes: 12 additions & 2 deletions montepy/surfaces/surface.py
Original file line number Diff line number Diff line change
@@ -19,15 +19,25 @@ class Surface(Numbered_MCNP_Object):
"""
Object to hold a single MCNP surface

.. versionchanged:: 1.0.0

Added number parameter

:param input: The Input object representing the input
:type input: Union[Input, str]
:param number: The number to set for this object.
:type number: int
"""

_parser = SurfaceParser()

def __init__(self, input: Union[montepy.input_parser.mcnp_input.Input, str] = None):
def __init__(
self,
input: union[montepy.input_parser.mcnp_input.input, str] = None,
number: int = None,
):
self._BLOCK_TYPE = montepy.input_parser.block_type.BlockType.SURFACE
super().__init__(input, self._parser)
super().__init__(input, self._parser, number)
self._periodic_surface = None
self._old_periodic_surface = self._generate_default_node(int, None)
self._transform = None
Loading