Skip to content

Commit

Permalink
Merge pull request #45 from paulromano/classmethod-fixes
Browse files Browse the repository at this point in the history
Use classmethod decorators on Material class
  • Loading branch information
shimwell authored Oct 1, 2024
2 parents f6153c0 + bd76f7a commit 6275ded
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/neutronics_material_maker/material.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python3

__author__ = "neutronics material maker development team"
from __future__ import annotations

__author__ = "neutronics material maker development team"

import difflib
import json
Expand Down Expand Up @@ -33,7 +34,7 @@
msg = (
"OpenMC python package not found, .openmc_material, "
".serpent_material, .mcnp_material, .fispact_material methods not "
"avaiable"
"available"
)
warnings.warn(msg)

Expand Down Expand Up @@ -826,7 +827,8 @@ def _get_atoms_in_crystal(self):
self.list_of_fractions = list_of_fractions
return sum(list_of_fractions)

def from_json_file(filename: str, name: str, **kwargs):
@classmethod
def from_json_file(cls, filename: str, name: str, **kwargs) -> Material:
with open(filename, "r") as file:
new_data = json.load(file)

Expand All @@ -839,10 +841,11 @@ def from_json_file(filename: str, name: str, **kwargs):
for key, value in kwargs.items():
entry[key] = value

return Material(name=name, **entry)
return cls(name=name, **entry)

def from_library(name: str, **kwargs):
# TODO allow discreat libraries to be searched library: List('str')
@classmethod
def from_library(cls, name: str, **kwargs) -> Material:
# TODO allow discrete libraries to be searched library: List('str')

if name not in material_dict.keys():
closest_match = difflib.get_close_matches(name, material_dict.keys())
Expand All @@ -857,9 +860,11 @@ def from_library(name: str, **kwargs):
for key, value in kwargs.items():
entry[key] = value

return Material(name=name, **entry)
return cls(name=name, **entry)

@classmethod
def from_mixture(
cls,
materials: list,
fracs: List[float],
percent_type: Optional[str] = "vo",
Expand All @@ -874,7 +879,7 @@ def from_mixture(
decimal_places: Optional[int] = 8,
volume_in_cm3: Optional[float] = None,
additional_end_lines: Optional[Dict[str, List[str]]] = None,
):
) -> Material:
"""Creates a material from a mixture of multiple materials.
Args:
Expand Down Expand Up @@ -960,7 +965,7 @@ def from_mixture(
for nuclide in sorted(openmc_material.nuclides):
isotopes[nuclide.name] = nuclide.percent

return Material(
return cls(
percent_type=nuclide.percent_type,
isotopes=isotopes,
density=openmc_material.get_mass_density(),
Expand Down

0 comments on commit 6275ded

Please sign in to comment.