diff --git a/include/fprops/State.h b/include/fprops/State.h index 33b6b01..a8da6a2 100644 --- a/include/fprops/State.h +++ b/include/fprops/State.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace fprops { /// Computed properties referring to a thermodynamical state @@ -56,6 +58,8 @@ struct State { double k, double h, double w); + + [[nodiscard]] auto to_string() const -> std::string; }; } // namespace fprops diff --git a/python/src/fprops.cpp b/python/src/fprops.cpp index ddbef0c..4cbcba2 100644 --- a/python/src/fprops.cpp +++ b/python/src/fprops.cpp @@ -31,7 +31,8 @@ PYBIND11_MODULE(fprops, m) .def_readwrite("s", &State::s) .def_readwrite("k", &State::k) .def_readwrite("h", &State::h) - .def_readwrite("w", &State::w); + .def_readwrite("w", &State::w) + .def("__repr__", &State::to_string); py::class_(m, "Air") .def(py::init()) diff --git a/python/tests/test_air.py b/python/tests/test_air.py index b0c2246..9d96085 100644 --- a/python/tests/test_air.py +++ b/python/tests/test_air.py @@ -1,6 +1,8 @@ import fprops as fp import pytest import math +import re + def test_air_valid(): air = fp.Air() @@ -8,3 +10,23 @@ def test_air_valid(): assert state.p == 101325 assert state.T == 300 assert math.isclose(state.rho, 1.1769510785919943, abs_tol=1e-14) + + +def test_air_repr(capfd): + air = fp.Air() + state = air.p_T(101325, 300) + print(state) + out, err = capfd.readouterr() + lines = out.splitlines() + assert re.match("rho = [0-9.e-]+ kg/m\\^3", lines[0]) + assert re.match("p = [0-9.e-]+ Pa", lines[1]) + assert re.match("T = [0-9.e-]+ K", lines[2]) + assert re.match("e = [0-9.e-]+ J/kg", lines[3]) + assert re.match("v = [0-9.e-]+ m\\^3/kg", lines[4]) + assert re.match("cp = [0-9.e-]+ J/\\(kg-K\\)", lines[5]) + assert re.match("cv = [0-9.e-]+ J/\\(kg-K\\)", lines[6]) + assert re.match("s = [0-9.e-]+ J/\\(kg-K\\)", lines[7]) + assert re.match("h = [0-9.e-]+ J/kg", lines[8]) + assert re.match("c = [0-9.e-]+ m/s", lines[9]) + assert re.match("mu = [0-9.e-]+ Pa-s", lines[10]) + assert re.match("k = [0-9.e-]+ W/\\(m-K\\)", lines[11]) diff --git a/src/State.cpp b/src/State.cpp new file mode 100644 index 0000000..100ac69 --- /dev/null +++ b/src/State.cpp @@ -0,0 +1,25 @@ +#include "fprops/State.h" +#include "fmt/printf.h" + +namespace fprops { + +auto +State::to_string() const -> std::string +{ + std::string str; + str += fmt::format("rho = {} kg/m^3\n", this->rho); + str += fmt::format("p = {} Pa\n", this->p); + str += fmt::format("T = {} K\n", this->T); + str += fmt::format("e = {} J/kg\n", this->u); + str += fmt::format("v = {} m^3/kg\n", this->v); + str += fmt::format("cp = {} J/(kg-K)\n", this->cp); + str += fmt::format("cv = {} J/(kg-K)\n", this->cv); + str += fmt::format("s = {} J/(kg-K)\n", this->s); + str += fmt::format("h = {} J/kg\n", this->h); + str += fmt::format("c = {} m/s\n", this->w); + str += fmt::format("mu = {} Pa-s\n", this->mu); + str += fmt::format("k = {} W/(m-K)\n", this->k); + return str; +} + +} // namespace fprops