diff --git a/python/src/composition.cpp b/python/src/composition.cpp index 67598c0..5fb3102 100644 --- a/python/src/composition.cpp +++ b/python/src/composition.cpp @@ -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( @@ -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") @@ -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, diff --git a/src/casm/composition/CompositionConverter.cc b/src/casm/composition/CompositionConverter.cc index 0521eb0..1493525 100644 --- a/src/casm/composition/CompositionConverter.cc +++ b/src/casm/composition/CompositionConverter.cc @@ -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); } @@ -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) + ")"; }; @@ -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) + ")"; }; @@ -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) { @@ -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)); }