Skip to content

Commit

Permalink
fix abi compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bpuchala committed Aug 18, 2024
1 parent d4069d2 commit be31144
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
6 changes: 0 additions & 6 deletions include/casm/composition/CompositionCalculator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ class CompositionCalculator {
// Vacancy components are detected using `xtal::is_vacancy`.
std::vector<std::string> m_components;

// Names of allowed occupants for each sublattice
std::vector<std::vector<std::string>> m_allowed_occs;

// Names of vacancy components
std::set<std::string> m_vacancy_names;

// Lookup table for sublattice index and occupant index to component index.
std::vector<std::vector<Index>> m_occ_to_component_index_converter;

Expand Down
35 changes: 24 additions & 11 deletions python/src/composition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ namespace CASMpy {

using namespace CASM;

// TODO: rename and move to composition/io/json
jsonParser &calculator_to_json(composition::CompositionCalculator const &m,
jsonParser &json) {
json = jsonParser::object();
json["components"] = m.components();
json["allowed_occs"] = m.allowed_occs();

// only include vacancy names if there are any not in the defaults
bool custom_vacancy_names = false;
std::set<std::string> default_names = {"Va", "VA", "va"};
for (auto const &name : m.vacancy_names()) {
if (default_names.find(name) == default_names.end()) {
custom_vacancy_names = true;
break;
}
}
if (custom_vacancy_names) {
json["vacancy_names"] = m.vacancy_names();
}
return json;
}

} // namespace CASMpy

PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
Expand Down Expand Up @@ -251,12 +273,7 @@ PYBIND11_MODULE(_composition, m) {
[](composition::CompositionCalculator const &m) {
std::stringstream ss;
jsonParser json;
json["components"] = m.components();
json["allowed_occs"] = m.allowed_occs();
if (m.vacancy_names() !=
std::set<std::string>({"Va", "VA", "va"})) {
json["vacancy_names"] = m.vacancy_names();
}
calculator_to_json(m, json);
return static_cast<nlohmann::json>(json);
},
R"pbdoc(
Expand All @@ -270,11 +287,7 @@ PYBIND11_MODULE(_composition, m) {
.def("__repr__", [](composition::CompositionCalculator const &m) {
std::stringstream ss;
jsonParser json;
json["components"] = m.components();
json["allowed_occs"] = m.allowed_occs();
if (m.vacancy_names() != std::set<std::string>({"Va", "VA", "va"})) {
json["vacancy_names"] = m.vacancy_names();
}
calculator_to_json(m, json);
ss << json;
return ss.str();
});
Expand Down
39 changes: 36 additions & 3 deletions python/tests/composition/test_CompositionCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,49 @@ def test_FormationEnergyCalculator_io_2():

assert isinstance(comp_calculator, CompositionCalculator)

data = comp_calculator.to_dict()
assert isinstance(data, dict)
assert "components" in data
assert data["components"] == comp_calculator.components()
assert "allowed_occs" in data
assert data["allowed_occs"] == comp_calculator.allowed_occs()
assert "vacancy_names" not in data

comp_calculator_in = CompositionCalculator.from_dict(data)
assert comp_calculator_in.components() == comp_calculator.components()
assert comp_calculator_in.allowed_occs() == comp_calculator.allowed_occs()
assert comp_calculator_in.vacancy_names() == comp_calculator.vacancy_names()

import io
from contextlib import redirect_stdout

string_io = io.StringIO()
with redirect_stdout(string_io):
print(comp_calculator)
out = string_io.getvalue()
assert "components" in out
assert "allowed_occs" in out
assert "vacancy_names" not in out


def test_FormationEnergyCalculator_io_3():
components = ["A", "B", "X"]
allowed_occs = [["A", "B", "X"], ["A", "B", "X"]]
vacancy_names = set(["Z", "X", "Y"])

comp_calculator = CompositionCalculator(components, allowed_occs, vacancy_names)
assert comp_calculator.vacancy_names() == set(["X"])

assert isinstance(comp_calculator, CompositionCalculator)

data = comp_calculator.to_dict()
assert isinstance(data, dict)
assert "components" in data
assert data["components"] == comp_calculator.components()
assert "allowed_occs" in data
assert data["allowed_occs"] == comp_calculator.allowed_occs()
assert "vacancy_names" in data
assert sorted(data["vacancy_names"]) == sorted(
list(comp_calculator.vacancy_names())
)
assert set(data["vacancy_names"]) == comp_calculator.vacancy_names()

comp_calculator_in = CompositionCalculator.from_dict(data)
assert comp_calculator_in.components() == comp_calculator.components()
Expand Down
21 changes: 17 additions & 4 deletions src/casm/composition/CompositionCalculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ CompositionCalculator::CompositionCalculator(
std::vector<std::vector<std::string>> const &_allowed_occs,
std::set<std::string> const &_vacancy_names)
: m_components(_components),
m_allowed_occs(_allowed_occs),
m_vacancy_names(_vacancy_names),
m_occ_to_component_index_converter(
make_occ_index_to_component_index_converter(_components,
_allowed_occs)),
Expand All @@ -42,12 +40,27 @@ std::vector<std::string> CompositionCalculator::components() const {
/// \brief The names of allowed occupants for each sublattice
std::vector<std::vector<std::string>> CompositionCalculator::allowed_occs()
const {
return m_allowed_occs;
std::vector<std::vector<std::string>> _allowed_occs;
for (Index b = 0; b < m_occ_to_component_index_converter.size(); ++b) {
std::vector<std::string> sublat_occs;
for (Index occ_index = 0;
occ_index < m_occ_to_component_index_converter[b].size();
++occ_index) {
sublat_occs.push_back(
m_components[m_occ_to_component_index_converter[b][occ_index]]);
}
_allowed_occs.push_back(sublat_occs);
}
return _allowed_occs;
}

/// \brief The names of vacancy components
std::set<std::string> CompositionCalculator::vacancy_names() const {
return m_vacancy_names;
std::set<std::string> _vacancy_names;
if (m_vacancy_allowed) {
_vacancy_names.insert(m_components[m_vacancy_index]);
}
return _vacancy_names;
}

/// \brief The number of sublattices
Expand Down

0 comments on commit be31144

Please sign in to comment.