Skip to content

Commit

Permalink
Adding string representation for State
Browse files Browse the repository at this point in the history
Now, you can do `print(state)` in python and get all the computed values
  • Loading branch information
andrsd committed Mar 4, 2024
1 parent d3f4379 commit 636f40f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/fprops/State.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <string>

namespace fprops {

/// Computed properties referring to a thermodynamical state
Expand Down Expand Up @@ -56,6 +58,8 @@ struct State {
double k,
double h,
double w);

[[nodiscard]] auto to_string() const -> std::string;
};

} // namespace fprops
3 changes: 2 additions & 1 deletion python/src/fprops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_<Air>(m, "Air")
.def(py::init())
Expand Down
22 changes: 22 additions & 0 deletions python/tests/test_air.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import fprops as fp
import pytest
import math
import re


def test_air_valid():
air = fp.Air()
state = air.p_T(101325, 300)
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])
25 changes: 25 additions & 0 deletions src/State.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "fprops/State.h"
#include "fmt/printf.h"

namespace fprops {

std::string
State::to_string() const

Check warning on line 7 in src/State.cpp

View workflow job for this annotation

GitHub Actions / c++ linter

src/State.cpp:7:8 [modernize-use-trailing-return-type]

use a trailing return type for this function
{
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

0 comments on commit 636f40f

Please sign in to comment.