Mixing scaled and derived units un boost::units

757 Views Asked by At

I would like to use a boost::units in a project to use dimensional analysis and automatic conversions between unit systems. I would like to express quantities in the code with standard engineering units, that are often scaled versions of other units. Let me explain this with an example. Suppose I define the following system

typedef make_system<
    us::foot_base_unit,
    us::pound_base_unit,
    si::second_base_unit>::type my_system;

BOOST_UNITS_STATIC_CONSTANT(feet,length);

BOOST_UNITS_STATIC_CONSTANT(pound,mass);

Then length units would be defined in feet, force in lb*ft*s^-2 and pressure in lb*ft^-1*s^-2. However, I would like to use force in pound-force units and pressure in PSI which are pound-force per square inch. I thought I could use scaled units to express these and use them interchangeably but this doesn't seem to be the case.

I tried this:

struct poundforcescale {
    typedef double value_type;
    double value() const { return 32.174049; }
};

typedef make_scaled_unit<force, poundforcescale>::type poundForce;
namespace boost {

  namespace units {
    std::string name_string(const field::poundForce&) { return "pound-force"; }
    std::string symbol_string(const field::poundForce&) { return "lbf"; }
 }
}

Which compiled without a problem. But then When I tried to use the scaled unit like this:

poundForce poundForceInst;
quantity<field::poundForce> f2 = 1*poundForceInst;
quantity<field::force> f3 = f2;

The compilation failed with an "no viable conversion error". I thought that the point of scaled units was to make these conversions automatically. And also the documentation made me think that I only needed to define name_string and symbol_string to be able to print the pound-force quantities, but this

std::cout << "f2 = " << f2 << std::endl;

resulted in a "no member named symbol in boost::units::scale_list_dim" error. Apparently overloading these functions doesn't work for scaled units.

Maybe I should define another system like this

typedef make_system<
    us::foot_base_unit,
    us::pound_force_base_unit,
    si::second_base_unit>::type my_system;

But I would need conversions anyway if I want to express length in ft and pressure in psi.

I would be glad if someone has a better solution.

0

There are 0 best solutions below