Skip to content

Commit

Permalink
Merge pull request #16 from pyiron/add_format
Browse files Browse the repository at this point in the history
Add format
  • Loading branch information
samwaseda authored Nov 21, 2024
2 parents bb5fe56 + f2c9170 commit 8e20312
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions stinx/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ def format_value(v, indent=0):
if len(v) == 0:
return " {}"
else:
return " {\n" + to_sphinx(v, indent + 1) + indent * "\t" + "}"
return (
" {\n"
+ to_sphinx(v, indent + 1, include_format=False)
+ indent * "\t"
+ "}"
)
else:
if isinstance(v, np.ndarray):
v = v.tolist()
return " = {!s};".format(v)


def to_sphinx(obj, indent=0):
line = ""
def to_sphinx(obj, indent=0, include_format=True):
if include_format and "PAWHamiltonian" in obj:
line = "format paw;\n\n"
elif include_format and "PWHamiltonian" in obj and "pseudoPot" in obj:
line = "format sphinx;\n\n"
else:
line = ""
for k, v in obj.items():
current_line = indent * "\t" + k.split("___")[0]
if isinstance(v, list) and isinstance(v[0], dict):
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_input.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from stinx.input import sphinx
from stinx.toolkit import to_sphinx


class TestStinx(unittest.TestCase):
Expand All @@ -17,6 +18,19 @@ def test_wrap_string(self):
)
self.assertEqual(without_wrap_string, {'potential': 'my_potential_path', 'potType': 'AtomPAW'})

def test_include_format(self):
paw_group = sphinx.PAWHamiltonian.create(xc=1, spinPolarized=False, ekt=0.2)
input_sx = sphinx.create(PAWHamiltonian=paw_group)
self.assertTrue("format paw;" in to_sphinx(input_sx))
self.assertFalse("format paw;" in to_sphinx(input_sx, include_format=False))
basis_group = sphinx.basis.create(eCut=25, kPoint=sphinx.basis.kPoint.create(coords=3 * [0.5]))
input_sx = sphinx.create(basis=basis_group)
self.assertFalse("format paw;" in to_sphinx(input_sx))
pw = sphinx.PWHamiltonian.create(xc="PBE")
pseudo = sphinx.pseudoPot.create()
input_sx = sphinx.create(PWHamiltonian=pw, pseudoPot=pseudo)
self.assertTrue("format sphinx;" in to_sphinx(input_sx))


if __name__ == "__main__":
unittest.main()

0 comments on commit 8e20312

Please sign in to comment.