Skip to content

Commit

Permalink
Added Data Card Subclasses #1
Browse files Browse the repository at this point in the history
  • Loading branch information
BitterB0NG0 committed Aug 20, 2024
1 parent b332dd7 commit 0fe841a
Show file tree
Hide file tree
Showing 4 changed files with 7,389 additions and 341 deletions.
13 changes: 13 additions & 0 deletions src/pymcnp/files/_utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class MCNPSyntaxCodes(Enum):
TOOFEW_SURFACE_ENTRIES = -30
TOOMANY_SURFACE_ENTRIES = 30

TOOFEW_DATUM_ENTRIES = -40
TOOMANY_DATUM_ENTRIES = 40


class MCNPSemanticCodes(Enum):
"""
Expand Down Expand Up @@ -64,6 +67,16 @@ class MCNPSemanticCodes(Enum):
INVALID_SURFACE_TRANSFORMPERIODIC = 12
INVALID_SURFACE_PARAMETER = 13

INVALID_DATUM_MNEMONIC = 20
INVALID_DATUM_PARAMETER = 21
INVALID_DATUM_SUFFIX = 22
INVALID_DAWWG_KEYWORD = 23
INVALID_DAWWG_VALUE = 24
INVALID_EMBED_KEYWORD = 25
INVALID_EMBED_VALUE = 26
INVALID_MATERIAL_KEYWORD = 27
INVALID_MATERIAL_VALUE = 28


class MCNPSyntaxError(Exception):
"""
Expand Down
178 changes: 173 additions & 5 deletions src/pymcnp/files/_utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,133 @@


import re
from typing import Self, Callable
from enum import StrEnum
from typing import Self, Callable

ELEMENTS = {
"H": 1,
"He": 2,
"Li": 3,
"Be": 4,
"B": 5,
"C": 6,
"N": 7,
"O": 8,
"F": 9,
"Ne": 10,
"Na": 11,
"Mg": 12,
"Al": 13,
"Si": 14,
"P": 15,
"S": 16,
"Cl": 17,
"Ar": 18,
"K": 19,
"Ca": 20,
"Sc": 21,
"Ti": 22,
"V": 23,
"Cr": 24,
"Mn": 25,
"Fe": 26,
"Co": 27,
"Ni": 28,
"Cu": 29,
"Zn": 30,
"Ga": 31,
"Ge": 32,
"As": 33,
"Se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pt|Au|Hg|Tl|Pb|Bi|Po|At|Rn|Fr|Ra|Ac|Th|Pa|U|Np|Pu|Am|Cm|Bk|Cf|Es|Fm|Md|No|Lr|Rf|Db|Sg|Bh|Hs|Mt|Ds|Rg|Cn|Nh|Fl|Mc|Lv|Ts|Og"
)
Z_PATTERN = re.compile(r"\A[+-]?[0-9]+\Z")
R_PATTERN = re.compile(
r"\A[+-]?(([0-9]+)|([0-9]+[.][0-9]*)|([.][0-9]+))([Ee]([+-][0-9]+))?\Z"
Expand Down Expand Up @@ -44,6 +167,47 @@ def cast_fortran_real(
return None


class Zaid:
"""
'Zaid'
"""

def __init__(self) -> Self:
"""
'__init__' initializes 'Zaid'.
"""

self.z: int = None
self.a: int = None
self.abx: str = None

@classmethod
def cast_mcnp_zaid(
cls, string: str, hook: Callable[Self, bool] = lambda _: True
) -> Self:
"""
'cast_mcnp_zaid'
"""

string = string.lower()

try:
if "." in string:
zaid.abx = string[-3:]
string = string[:-4]

zaid.a = int(string[-3:-1])
zaid.z = int(string[:-3])

if hook(zaid):
return zaid

except IndexError:
pass

return None


class Designator(StrEnum):
"""
'Designator'
Expand Down Expand Up @@ -88,7 +252,9 @@ class Designator(StrEnum):
HEAVY_IONS = "#"

@classmethod
def cast_mcnp_designator(cls, string: str) -> tuple[Self]:
def cast_mcnp_designator(
cls, string: str, hook: Callable[Self, bool] = lambda _: True
) -> tuple[Self]:
"""
'cast_mcnp_designator'
"""
Expand All @@ -99,8 +265,10 @@ def cast_mcnp_designator(cls, string: str) -> tuple[Self]:

for substring in string.split(","):
try:
designators.append(cls(substring))
value = designators.append(cls(substring))
if hook(value):
return tuple(designators) if designators else None
except ValueError:
return None
pass

return tuple(designators) if designators else None
return None
15 changes: 11 additions & 4 deletions src/pymcnp/files/inp/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class CellKeyword(StrEnum):
"""
'CellKeyword' represents cell card keywords.
'CellParameter' functions as a data type for 'CellParameter'
'CellKeyword' functions as a data type for 'CellParameter'
and 'Cell'. It represents cell card parameter keywords
as abstract syntax elements.
"""
Expand Down Expand Up @@ -214,32 +214,39 @@ def cast_cell_keyword(
strings. If the string is invalid or the hook returns false,
it returns None.
Parameters:
string: String to cast.
hook: Postcast check.
Returns:
Cell parameter keyword from string.
"""

string = string.lower()

if string.startswith("*"):
# Handling Star Modifier
if string == "*trcl":
string = string[:1]

# Handling Suffixes
if string.startswith(("wwn", "dxc", "tmp")):
if (
len(string) < 4
and types.cast_fortran_integer(string[:3]) is None
or types.cast_fortran_integer(string[3:]) is None
):
return None

string = string[:3]
elif string.startswith("pd"):
if (
len(string) < 3
and types.cast_fortran_integer(string[:2]) is None
and types.cast_fortran_integer(string[2:]) is None
):
return None

string = string[:2]

# Attempting Type Cast
try:
keyword = cls(string)

Expand Down
Loading

0 comments on commit 0fe841a

Please sign in to comment.