Skip to content

Commit

Permalink
Merge pull request #14 from xivh/composition_converter
Browse files Browse the repository at this point in the history
Composition converter
  • Loading branch information
bpuchala authored Aug 17, 2024
2 parents 89e7af9 + cfea98c commit b7595e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
10 changes: 5 additions & 5 deletions python/src/composition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ PYBIND11_MODULE(_composition, m) {
.def("independent_compositions",
&composition::CompositionConverter::independent_compositions,
R"pbdoc(
The dimensionality of the composition space, :math:`k`.
The dimensionality of the composition space, :math:`k`. This is the number of parametric composition axes.
)pbdoc")
.def("axes", &composition::CompositionConverter::axes,
R"pbdoc(
Expand Down Expand Up @@ -547,7 +547,7 @@ PYBIND11_MODULE(_composition, m) {
R"pbdoc(
Return formula for :math:`\vec{x}` in terms of :math:`\vec{n}` (ex: \"a(0.5+0.5A-0.5B)\").
)pbdoc")
.def("origin_formula", &composition::CompositionConverter::param_formula,
.def("origin_formula", &composition::CompositionConverter::origin_formula,
R"pbdoc(
Return formula for the origin composition, :math:`\vec{n}_0`.
)pbdoc")
Expand All @@ -559,18 +559,18 @@ PYBIND11_MODULE(_composition, m) {
.def("param_component_formula",
&composition::CompositionConverter::comp_formula, py::arg("i"),
R"pbdoc(
Return formula the i-th parametric composition component, :math:`x_i`, in terms of :math:`\vec{n}`.
Return formula for the i-th parametric composition component, :math:`x_i`, in terms of :math:`\vec{n}`.
)pbdoc")
.def("mol_component_formula",
&composition::CompositionConverter::comp_n_formula, py::arg("i"),
R"pbdoc(
Return formula the i-th mol composition component, :math:`n_i`, in terms of :math:`\vec{x}`.
Return formula for the i-th mol composition component, :math:`n_i`, in terms of :math:`\vec{x}`.
)pbdoc")
.def("param_chem_pot_formula",
&composition::CompositionConverter::param_chem_pot_formula,
py::arg("i"),
R"pbdoc(
Return formula the parametric composition conjugate potential in terms of the chemical potentials.
Return formula for the parametric composition conjugate potential in terms of the chemical potentials.
)pbdoc");

m.def("make_composition_space", &composition::composition_space,
Expand Down
25 changes: 22 additions & 3 deletions src/casm/composition/CompositionConverter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Eigen::VectorXd CompositionConverter::origin() const { return m_origin; }
/// - Matches order from components()
///
Eigen::VectorXd CompositionConverter::end_member(size_type i) const {
if (i >= m_end_members.cols()) {
throw std::runtime_error(
std::string("Error: Requested end member index is too large."));
}
return m_end_members.col(i);
}

Expand Down Expand Up @@ -276,7 +280,11 @@ std::string CompositionConverter::param_formula() const {
/// \brief Return formula for comp(i) in terms of comp_n(A), comp_n(B), ...
std::string CompositionConverter::comp_formula(size_type i) const {
// comp(i) = m_to_x(i,j)*(comp_n(j) - m_origin(j)) + ...

if (i >= independent_compositions()) {
throw std::runtime_error(
std::string("Error: Requested parametric component is too large."));
}

std::stringstream ss;

auto comp_x_str = [&]() { return "comp(" + comp_var(i) + ")"; };
Expand Down Expand Up @@ -335,7 +343,10 @@ std::string CompositionConverter::comp_formula(size_type i) const {
/// ...
std::string CompositionConverter::comp_n_formula(size_type i) const {
// comp_n(i) = m_origin(j) + m_to_n(i,j)*comp(j) + ...

if (i >= m_components.size()) {
throw std::runtime_error(
std::string("Error: Requested component index is too large."));
}
std::stringstream ss;

auto comp_x_str = [&](int j) { return "comp(" + comp_var(j) + ")"; };
Expand Down Expand Up @@ -404,7 +415,11 @@ std::string CompositionConverter::param_chem_pot_formula(size_type i) const {
// dG = chem_pot.trans * dn = chem_pot.trans * m_to_n * dx
// -> param_chem_pot.trans = chem_pot.trans * m_to_n
// -> param_chem_pot = m_to_n.trans * chem_pot

if (i >= independent_compositions()) {
throw std::runtime_error(
std::string("Error: Requested parametric chemical potential index is too large."));
}

std::stringstream ss;

auto print_chem_pot = [&](int j) {
Expand Down Expand Up @@ -462,6 +477,10 @@ std::string CompositionConverter::origin_formula() const {

/// \brief Return formula for end member
std::string CompositionConverter::end_member_formula(size_type i) const {
if (i >= m_end_members.cols()) {
throw std::runtime_error(
std::string("Error: Requested end member index is too large."));
}
return _n_formula(m_end_members.col(i));
}

Expand Down

0 comments on commit b7595e6

Please sign in to comment.