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 const std::string json_temperature = json["temperature"];
24 if (!json_temperature.empty())
25 temperature_ = json_temperature;
26
27 if (json["characteristic_length"].is_number())
28 {
29 const double cl = json["characteristic_length"];
30 if (cl > 0)
32 }
33 }
34
35 double Units::convert(const json &val, const std::string &unit_type_s)
36 {
37 if (val.is_number())
38 return val.get<double>();
39
40 assert(val.is_object());
41
42 return convert(val["value"].get<double>(), val["unit"].get<std::string>(), unit_type_s);
43 }
44
45 double Units::convert(const double val, const std::string &unit_s, const std::string &unit_type_s)
46 {
47 auto unit = units::unit_from_string(unit_s);
48 auto unit_type = units::unit_from_string(unit_type_s);
49 if (!unit.is_convertible(unit_type))
50 {
51 log_and_throw_error(fmt::format("Cannot convert {} to {}", units::to_string(unit), units::to_string(unit_type)));
52 }
53
54 return units::convert(val, unit, unit_type);
55 }
56
57} // namespace polyfem
double val
Definition Assembler.cpp:89
double characteristic_length_
Definition Units.hpp:45
std::string time_
Definition Units.hpp:44
void init(const json &json)
Definition Units.cpp:9
std::string mass_
Definition Units.hpp:43
std::string temperature_
Definition Units.hpp:47
static double convert(const json &val, const std::string &unit_type)
Definition Units.cpp:35
std::string length_
Definition Units.hpp:42
nlohmann::json json
Definition Common.hpp:9
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73