PolyFEM
Loading...
Searching...
No Matches
Units.cpp
Go to the documentation of this file.
1#include "Units.hpp"
2
4
5#include <units/units.hpp>
6
7namespace polyfem
8{
9 void Units::init(const json &json)
10 {
11 const std::string json_length = json["length"];
12 if (!json_length.empty())
13 length_ = json_length;
14
15 const std::string json_mass = json["mass"];
16 if (!json_mass.empty())
17 mass_ = json_mass;
18
19 const std::string json_time = json["time"];
20 if (!json_time.empty())
21 time_ = json_time;
22
23 if (json["characteristic_length"].is_number())
24 {
25 const double cl = json["characteristic_length"];
26 if (cl > 0)
28 }
29 }
30
31 double Units::convert(const json &val, const std::string &unit_type_s)
32 {
33 if (val.is_number())
34 return val.get<double>();
35
36 assert(val.is_object());
37
38 return convert(val["value"].get<double>(), val["unit"].get<std::string>(), unit_type_s);
39 }
40
41 double Units::convert(const double val, const std::string &unit_s, const std::string &unit_type_s)
42 {
43 auto unit = units::unit_from_string(unit_s);
44 auto unit_type = units::unit_from_string(unit_type_s);
45 if (!unit.is_convertible(unit_type))
46 {
47 log_and_throw_error(fmt::format("Cannot convert {} to {}", units::to_string(unit), units::to_string(unit_type)));
48 }
49
50 return units::convert(val, unit, unit_type);
51 }
52
53} // namespace polyfem
double val
Definition Assembler.cpp:86
double characteristic_length_
Definition Units.hpp:38
std::string time_
Definition Units.hpp:37
void init(const json &json)
Definition Units.cpp:9
std::string mass_
Definition Units.hpp:36
static double convert(const json &val, const std::string &unit_type)
Definition Units.cpp:31
std::string length_
Definition Units.hpp:35
nlohmann::json json
Definition Common.hpp:9
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:71