The Physics Library
inside Zero
born out of the idea of are we able to code a Physics degree in C++?
For that, we designed here a full set of tools that allows the user to naturally work with concepts related
with a job of a physicist. This includes a library for handling physical quantities, a set of predefined
constant values (typically known as physical constants), tools for make problems solving, based on
the ecuations, systems of ecuations and the other tools provided by the math library of Zero
...
This library contains types and operations to reflect the natural measurement and calculations of physics quantities,
in the same terms that are defined by the SI
.
-
The library is designed to be completely
compile-time
usable, providing a high-efficient, type safe API. -
Provides a whole set of basic arithmetic operations (
+
,-
,*
,/
), that are usable between quantities that holds magnitudes with the same dimension (for bases) or the same dimensionality (for derived) -
Provides convenient member functions to retrieve data about the types that made any of the entities. i.e: print the dimensions of a speed, or the units of Kilogram.
-
Provides convenient member functions to
convert
between units with the samedimension
ordimensionality
-
Provides all the
SI
definedbase dimensions
andbase units
- Length [L] - meter [m]
- Time [T] - second [s]
- Mass [M] kilogram [kg]
- Temperature [θ] - kelvin [K]
- Electric Current [I] - ampere [A]
- Amount of Substance [N]- mole [mol]
- Luminous Intensity [J] - candela [cd]
- Dimensionless [l] - unitless []
-
Provides in addition, some of the "equivalent by ratio" units, for example
Kilometer (m³)
,Hour (s²)
and so on. -
Provides a set of well known
derived dimensions
andderived units
, likeMetersPerSecond
,Newton
orKilometersPerHour
. -
Provides an easy-to-extend client API. This means, that you, as user, can define a new whole entity, like a
base unit
or aderived unit
, by simply tag your types with the correct base types that we provide, making your entities completely interoperable with the ones already defined in the library
Almost everything in the library is defined as a (strong) type. We can categorize it as:
quantity
- The aggregate type that holds the value for a certain physical quantity, and provides the the convenient operations to manipulate themUnits
base_unit
- Defines what a base unit is, given abase_dimension
, a ratioand a
symbol`derived_unit
- Defines what a derived unit is, given an already definedderived dimension
Dimensions
base_dimension
- A CRTP base class that defines the base dimensions of theSI
. Depends on an scalar value, calleddimension exponent
, that serves to indicate whenever a unit is a numerator or a denominator in the units declared for aquantity
, ie:MetersPerSecond
impliesm¹ / s¹
or, as we use it in our code,m¹ * s⁻¹
derived_dimension
- Defines a derived dimension by taking a variadic pack orBaseUnit...
ratio
- Predefined declarations for the ratios of everySI
unit. Basically, is a power, represented as a template that takes a type for determine the value type of the other two template arguments, which are the relevant ones here:Base
andExponent
. Given these, we defined a set oftype alias
for representing the ratio of aSI
unit. ie:using kilo = ratio<short, 10, 3>;
gives us a ratio for use in abase_unit
, declaring that the targetbase_unit
is a unit with the dimension specified by the other mandatory dimension tag and with a ratio of kilo. For example:struct Kilogram: public mass<1>, public base_unit<kilo, kg> {};
unit_symbol
- A CRTP base type for define strong types for the units. As seen above,kg
in the strong type for define the symbol of kilogram, according to theSI
We provide in the API an easy way to convert quantities of the same dimension (for the bases) or with the
same dimensionality (for derived) with a simple template member function called .to<Magnitude>()
.
Here's an example:
constexpr auto velocity = quantity<MetersPerSecond>(300'000);
quantity<KilometersPerHour> kmph = velocity.to<KilometersPerHour>();
quantity<MetersPerSecond> mps = kmph.to<MetersPerSecond>();
std::cout << "Converting meters per second to kilometers per hour: " << kmph << "\n"; // Prints 1'080'000
std::cout << "Converting kilometers per hour to meters per second: " << mps << "\n"; // Prints 300'000