.. index:: pair: namespace; rgpot::units .. _doxid-namespacergpot_1_1units: namespace rgpot::units ====================== .. toctree:: :hidden: enum_rgpot_units_DimIndex.rst enum_rgpot_units_TokenType.rst struct_rgpot_units_Dimension.rst struct_rgpot_units_Token.rst struct_rgpot_units_UnitExpr.rst struct_rgpot_units_UnitValue.rst Overview ~~~~~~~~ Physical constants, unit conversion factors, and runtime unit parser. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block namespace units { // typedefs typedef std::unique_ptr<:ref:`UnitExpr`> :target:`UnitExprPtr`; // enums enum :ref:`DimIndex`; enum :ref:`TokenType`; // structs struct :ref:`Dimension`; struct :ref:`Token`; struct :ref:`UnitExpr`; struct :ref:`UnitValue`; // global variables static const :ref:`Dimension` :target:`DIM_LENGTH` = {{1, 0, 0, 0, 0}}; static const :ref:`Dimension` :target:`DIM_TIME` = {{0, 1, 0, 0, 0}}; static const :ref:`Dimension` :target:`DIM_MASS` = {{0, 0, 1, 0, 0}}; static const :ref:`Dimension` :target:`DIM_CHARGE` = {{0, 0, 0, 1, 0}}; static const :ref:`Dimension` :target:`DIM_ENERGY` = {{2, -2, 1, 0, 0}}; static const :ref:`Dimension` :target:`DIM_NONE` = {{0, 0, 0, 0, 0}}; static const auto :target:`BASE_UNITS`; static const auto :target:`QUANTITY_DIMS` = std::unordered_map`>{ {"length", DIM_LENGTH}, {"energy", DIM_ENERGY}, {"force", {{1, -2, 1, 0, 0}}}, {"pressure", {{-1, -2, 1, 0, 0}}}, {"momentum", {{1, -1, 1, 0, 0}}}, {"mass", DIM_MASS}, {"velocity", {{1, -1, 0, 0, 0}}}, {"charge", DIM_CHARGE}, }; double :target:`BOHR_TO_ANGSTROM` = 0.529177210903; double :target:`ANGSTROM_TO_BOHR` = 1.0 / BOHR_TO_ANGSTROM; double :target:`HARTREE_TO_EV` = 27.211386245988; double :target:`EV_TO_HARTREE` = 1.0 / HARTREE_TO_EV; double :target:`HARTREE_BOHR_TO_EV_ANGSTROM` = HARTREE_TO_EV / BOHR_TO_ANGSTROM; double :target:`NEG_GRAD_TO_FORCE` = -HARTREE_BOHR_TO_EV_ANGSTROM; double :target:`KB_HARTREE` = 3.1668115634556e-6; double :target:`KB_EV` = 8.617333262e-5; // global functions static std::string :target:`to_lower`(std::string s); static std::vector<:ref:`Token`> :target:`tokenize`(const std::string& unit); static std::vector<:ref:`Token`> :target:`shunting_yard`(const std::vector<:ref:`Token`>& tokens); static UnitExprPtr :target:`read_expr`(std::vector<:ref:`Token`>& stream); static :ref:`UnitValue` :target:`parse_unit_expression`(const std::string& unit); double :ref:`unit_conversion_factor`(const std::string& from_unit, const std::string& to_unit); void :ref:`validate_unit`(const std::string& quantity, const std::string& unit); } // namespace units .. _details-namespacergpot_1_1units: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Physical constants, unit conversion factors, and runtime unit parser. Compile-time constants use CODATA 2018 recommended values. rgpot's native unit system: energy = eV, length = Angstrom, force = eV/Angstrom. The runtime expression parser (unit_conversion_factor) is derived from metatomic-torch (`https://github.com/metatensor/metatomic `__), specifically PR #173 by Rohit Goswami. Original code: BSD-3-Clause licensed, copyright metatensor developers. The parser uses the Shunting-Yard algorithm for compound unit expressions like "kJ/mol/A^2" or "(eV\*u)^(1/2)" with SI-based dimensional analysis. Global Functions ---------------- .. index:: pair: function; unit_conversion_factor .. _doxid-namespacergpot_1_1units_1ad9fd973ab92395903989f89dd938c37f: .. ref-code-block:: cpp :class: doxyrest-title-code-block double unit_conversion_factor(const std::string& from_unit, const std::string& to_unit) Get the multiplicative conversion factor from ``from_unit`` to ``to_unit``. Both arguments are parsed as compound unit expressions. Operators: ``*`` (multiply), ``/`` (divide), ``^`` (power), ``()`` (grouping). Whitespace is ignored, lookup is case-insensitive. Examples: * ``unit_conversion_factor("angstrom", "bohr")`` length * ``unit_conversion_factor("eV/A", "hartree/bohr")`` force * ``unit_conversion_factor("kJ/mol", "eV")`` energy per particle * ``unit_conversion_factor("(eV*u)^(1/2)", "u*A/fs")`` momentum Supported base units: * Length: angstrom (A), bohr, nanometer (nm), meter (m), cm, mm, um * Energy: eV, meV, Hartree, Ry, Joule (J), kcal, kJ * Time: second (s), ms, us, ns, ps, femtosecond (fs) * Mass: dalton (u), kilogram (kg), gram (g), electron_mass (m_e) * Charge: e, coulomb (c) * Dimensionless: mol * Derived: hbar Throws std::invalid_argument on dimension mismatch or unknown unit. .. note:: Derived from metatomic-torch (metatensor/metatomic PR #173). .. index:: pair: function; validate_unit .. _doxid-namespacergpot_1_1units_1a7fe0822d8d392d957c6a1e52dc197b69: .. ref-code-block:: cpp :class: doxyrest-title-code-block void validate_unit(const std::string& quantity, const std::string& unit) Check that ``unit`` is a valid expression with dimensions matching ``quantity``. Known quantities: "length", "energy", "force", "pressure", "momentum", "mass", "velocity", "charge". Throws std::invalid_argument on mismatch or parse error.