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

Use classmethod decorators on Material class #45

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
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
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