#include <boost/units/conversion.hpp>
#include <boost/units/systems/si.hpp>
#include <iostream>
namespace si = boost::units::si;
namespace md
{
using boost::units::length_dimension;
using boost::units::mass_dimension;
using boost::units::time_dimension;
using boost::units::quantity;
using boost::units::unit;
using boost::units::scaled_base_unit;
using boost::units::scale;
using boost::units::static_rational;
// length
typedef scaled_base_unit<
si::meter_base_unit,
scale<10, static_rational<-9> > > nanometer_base_unit;
// mass
struct dalton_base_unit :
boost::units::base_unit<dalton_base_unit, mass_dimension, 2>
{
static std::string name() { return "Dalton"; }
static std::string symbol() { return "Da"; }
};
// time
typedef scaled_base_unit<
si::second_base_unit,
scale<10, static_rational<-12> > > picosecond_base_unit;
// define quantity types
typedef nanometer_base_unit::unit_type nanometer_unit;
BOOST_UNITS_STATIC_CONSTANT(nanometer, nanometer_unit);
BOOST_UNITS_STATIC_CONSTANT(nanometers, nanometer_unit);
typedef quantity<nanometer_unit> length_t;
typedef dalton_base_unit::unit_type dalton_unit;
BOOST_UNITS_STATIC_CONSTANT(dalton, dalton_unit);
BOOST_UNITS_STATIC_CONSTANT(daltons, dalton_unit);
BOOST_UNITS_STATIC_CONSTANT(atomic_mass_unit, dalton_unit);
BOOST_UNITS_STATIC_CONSTANT(atomic_mass_units, dalton_unit);
typedef quantity<dalton_unit> mass_t;
typedef picosecond_base_unit::unit_type picosecond_unit;
BOOST_UNITS_STATIC_CONSTANT(picosecond, picosecond_unit);
BOOST_UNITS_STATIC_CONSTANT(picoseconds, picosecond_unit);
typedef quantity<picosecond_unit> time_t;
} // namespace md
// mass conversions
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(
md::dalton_base_unit,
si::kilogram_base_unit,
double, 1.6605388628e-27);
using namespace md;
using namespace std;
int main ()
{
md::time_t timeStep = 0.002 * picoseconds;
cout << "time step = " << timeStep << endl;
mass_t carbonMass = 12.0 * daltons;
cout << "carbon mass = " << carbonMass << endl;
length_t bondLength = 0.15 * nanometers;
cout << "bond length = " << bondLength << endl;
// nonsense, but exercises composite dimensions
cout << "composite value = " << timeStep * bondLength << endl;
return 0;
}
This code compiles and runs correctly, giving:
time step = 0.002 ps
carbon mass = 12 Da
bond length = 0.15 nm
composite value = 0.0003 nm ps
In general, it's easiest to avoid explicitly creating unit systems by using the ::unit_type approach. For most cases, the unit system ought to be considered an implementation detail since the library supports heterogeneous units transparently. That is, for any given unit, the appropriate system is just the set of units needed to fully describe the unit itself...
Matthias