|
Boost Users : |
Subject: Re: [Boost-users] [units] conversion factor for non-base
From: Matthias Schabel (boost_at_[hidden])
Date: 2010-04-23 18:26:59
> "The flow coefficient is Q/sqrt(dP), a volumetric flow/sqrt(pressure)."
>
> The units, specifically, are:
>
> Kv = (m3/h)/sqrt(bar)
> Cv = (usgal/min)/sqrt(psi)
>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/base_units/metric/bar.hpp>
#include <boost/units/base_units/metric/hour.hpp>
#include <boost/units/base_units/metric/minute.hpp>
#include <boost/units/base_units/us/gallon.hpp>
#include <boost/units/base_units/us/inch.hpp>
#include <boost/units/base_units/us/pound_force.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/pressure.hpp>
#include <boost/units/systems/si/time.hpp>
#include <boost/units/systems/si/volume.hpp>
#include <iostream>
namespace boost {
namespace units {
namespace us {
typedef pound_force_base_unit::unit_type pound_force;
typedef inch_base_unit::unit_type inch;
} // namespace us
} // namespace units
} // namespace boost
using namespace boost::units;
typedef typeof(us::pound_force()/pow<2>(us::inch())) psi;
typedef typeof(si::cubic_meter/metric::hour_base_unit::unit_type()/root<2>(metric::bar_base_unit::unit_type())) Kv;
typedef typeof(us::gallon_base_unit::unit_type()/metric::minute_base_unit::unit_type()/root<2>(us::pound_force()/pow<2>(us::inch()))) Cv;
void f1(const quantity<Kv>& arg)
{
std::cout << arg << std::endl;
}
int main(void)
{
std::cout << 1.0*Kv() << std::endl;
std::cout << 1.0*Cv() << std::endl;
f1(1.0*Kv());
f1(quantity<Kv>(1.0*Cv()));
}
gives
1 m^3 h^-1 bar^(-1/2)
1 lbf^(-1/2) gal in min^-1
1 m^3 h^-1 bar^(-1/2)
0.864978 m^3 h^-1 bar^(-1/2)
You need a compiler that supports typeof for this to work... If you want a function that will take any quantity that is dimensionally-equivalent to (volumetric flow/sqrt(pressure)) then you will have to template it something like this :
template<class Y,class System>
void f(const quantity<unit<Dim,System>,Y>& arg);
where you use derived_dimension to define the dimension list Dim for your units.
Matthias
If
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net