|
| 1 | +import pytest |
| 2 | +from calphy.input import Calculation, read_inputfile |
| 3 | + |
| 4 | + |
| 5 | +def test_correct_element_ordering(): |
| 6 | + """Test that correct element ordering is accepted""" |
| 7 | + calc_dict = { |
| 8 | + 'element': ['Cu', 'Zr'], |
| 9 | + 'mass': [63.546, 91.224], |
| 10 | + 'pair_coeff': ['* * potential.eam.fs Cu Zr'], |
| 11 | + 'pair_style': ['eam/fs'], |
| 12 | + 'mode': 'fe', |
| 13 | + 'temperature': 1000, |
| 14 | + 'pressure': 0, |
| 15 | + 'lattice': 'tests/conf1.data', # Use existing data file |
| 16 | + } |
| 17 | + |
| 18 | + # Should not raise any exception |
| 19 | + calc = Calculation(**calc_dict) |
| 20 | + assert calc.element == ['Cu', 'Zr'] |
| 21 | + assert calc.mass == [63.546, 91.224] |
| 22 | + |
| 23 | + |
| 24 | +def test_wrong_element_ordering(): |
| 25 | + """Test that mismatched element ordering is rejected""" |
| 26 | + calc_dict = { |
| 27 | + 'element': ['Cu', 'Zr'], |
| 28 | + 'mass': [63.546, 91.224], |
| 29 | + 'pair_coeff': ['* * potential.eam.fs Zr Cu'], # Wrong order! |
| 30 | + 'pair_style': ['eam/fs'], |
| 31 | + 'mode': 'fe', |
| 32 | + 'temperature': 1000, |
| 33 | + 'pressure': 0, |
| 34 | + 'lattice': 'fcc', |
| 35 | + 'lattice_constant': 3.61, |
| 36 | + } |
| 37 | + |
| 38 | + with pytest.raises(ValueError) as exc_info: |
| 39 | + calc = Calculation(**calc_dict) |
| 40 | + |
| 41 | + assert 'ordering mismatch' in str(exc_info.value).lower() |
| 42 | + |
| 43 | + |
| 44 | +def test_single_element_no_ordering_issue(): |
| 45 | + """Test that single element systems don't trigger ordering validation""" |
| 46 | + calc_dict = { |
| 47 | + 'element': ['Cu'], |
| 48 | + 'mass': [63.546], |
| 49 | + 'pair_coeff': ['* * potential.eam Cu'], |
| 50 | + 'pair_style': ['eam'], |
| 51 | + 'mode': 'fe', |
| 52 | + 'temperature': 1000, |
| 53 | + 'pressure': 0, |
| 54 | + 'lattice': 'fcc', |
| 55 | + 'lattice_constant': 3.61, |
| 56 | + } |
| 57 | + |
| 58 | + # Should not raise any exception for single element |
| 59 | + calc = Calculation(**calc_dict) |
| 60 | + assert calc.element == ['Cu'] |
| 61 | + |
| 62 | + |
| 63 | +def test_no_elements_in_pair_coeff(): |
| 64 | + """Test that pair_coeff without element names skips validation""" |
| 65 | + calc_dict = { |
| 66 | + 'element': ['Cu', 'Zr'], |
| 67 | + 'mass': [63.546, 91.224], |
| 68 | + 'pair_coeff': ['* * potential.eam'], # No elements specified |
| 69 | + 'pair_style': ['eam'], |
| 70 | + 'mode': 'fe', |
| 71 | + 'temperature': 1000, |
| 72 | + 'pressure': 0, |
| 73 | + 'lattice': 'tests/conf1.data', # Use existing data file |
| 74 | + } |
| 75 | + |
| 76 | + # Should not raise exception when pair_coeff has no elements |
| 77 | + calc = Calculation(**calc_dict) |
| 78 | + assert calc.element == ['Cu', 'Zr'] |
| 79 | + |
| 80 | + |
| 81 | +def test_element_mismatch(): |
| 82 | + """Test that completely different elements in pair_coeff are rejected""" |
| 83 | + calc_dict = { |
| 84 | + 'element': ['Cu', 'Zr'], |
| 85 | + 'mass': [63.546, 91.224], |
| 86 | + 'pair_coeff': ['* * potential.eam.fs Al Ni'], # Different elements! |
| 87 | + 'pair_style': ['eam/fs'], |
| 88 | + 'mode': 'fe', |
| 89 | + 'temperature': 1000, |
| 90 | + 'pressure': 0, |
| 91 | + 'lattice': 'fcc', |
| 92 | + 'lattice_constant': 3.61, |
| 93 | + } |
| 94 | + |
| 95 | + with pytest.raises(ValueError) as exc_info: |
| 96 | + calc = Calculation(**calc_dict) |
| 97 | + |
| 98 | + assert 'element mismatch' in str(exc_info.value).lower() |
| 99 | + |
| 100 | + |
| 101 | +def test_three_element_ordering(): |
| 102 | + """Test ordering validation works for 3+ elements""" |
| 103 | + # Correct ordering |
| 104 | + calc_dict = { |
| 105 | + 'element': ['Cu', 'Zr', 'Al'], |
| 106 | + 'mass': [63.546, 91.224, 26.982], |
| 107 | + 'pair_coeff': ['* * potential.eam.fs Cu Zr Al'], |
| 108 | + 'pair_style': ['eam/fs'], |
| 109 | + 'mode': 'fe', |
| 110 | + 'temperature': 1000, |
| 111 | + 'pressure': 0, |
| 112 | + 'lattice': 'tests/conf1.data', # Use existing data file |
| 113 | + } |
| 114 | + |
| 115 | + calc = Calculation(**calc_dict) |
| 116 | + assert calc.element == ['Cu', 'Zr', 'Al'] |
| 117 | + |
| 118 | + # Wrong ordering |
| 119 | + calc_dict_wrong = { |
| 120 | + 'element': ['Cu', 'Zr', 'Al'], |
| 121 | + 'mass': [63.546, 91.224, 26.982], |
| 122 | + 'pair_coeff': ['* * potential.eam.fs Al Cu Zr'], # Different order |
| 123 | + 'pair_style': ['eam/fs'], |
| 124 | + 'mode': 'fe', |
| 125 | + 'temperature': 1000, |
| 126 | + 'pressure': 0, |
| 127 | + 'lattice': 'tests/conf1.data', # Use existing data file |
| 128 | + } |
| 129 | + |
| 130 | + with pytest.raises(ValueError) as exc_info: |
| 131 | + calc = Calculation(**calc_dict_wrong) |
| 132 | + |
| 133 | + assert 'ordering mismatch' in str(exc_info.value).lower() |
| 134 | + |
| 135 | + |
| 136 | +def test_existing_example_files(): |
| 137 | + """Test that existing example files still load correctly""" |
| 138 | + # Single element examples should work |
| 139 | + calcs = read_inputfile('tests/input.yaml') |
| 140 | + assert len(calcs) > 0 |
| 141 | + assert calcs[0].element is not None |
0 commit comments