In CalcTree, you can easily use units across your calculations. Here, we explain how to reference a quantity inside Python, perform operations on it, and reference the quantities defined in Python elsewhere on the CalcTree page.
When using formulae on a CalcTree page, you can use units of measurement to define quantities. For example, in a math parameter:
Using units in a math parameter
Let's start by referencing this quantity inside a Python source.
Referencing a quantity in Python
You can reference an existing quantity from your calculations inside Python. For example, the parameter defined above, can be referenced in Python as _page_distance, which will be of quantity type:
Referencing the distance parameter in Python
check_type
:<class 'pint.Quantity'>
you can get the magnitude and the unit of a quantity using the magnitude and unit methods:
Accessing magnitude and unit of a quantity
Defining a new quantity in Python
For defining a quantity in Python, you can use the ct.quantity method. For example to define a quantity of 1.5 seconds:
Defining a quantity in Python
You can also define a quantity of 1.0 magnitude of a unit, using the ct.units method:
Defining a quantity of one magnitude of a unit
Operations on quantities
You can multiply or divide quantities, which will result in a derived quantity. For example:
Creating a derived quantity
You can also add or subtract quantities of the same type. The necessary unit conversions will happen automatically. For example:
Adding two compatible quantities
Note that you can't use math.sqrt() on a quantity. you can use the power operator instead:
use the ** operator instead of math.sqrt()
Built-in quality control with units
If a quantity of incompatible type is tried in a Python addition or subtraction operation, CalcTree will prevent the calculation from proceeding and shows the relevant error. For example this line will inforce a quantity of Length for the parameter distance when received inside Python:
Enforce a quantity type of Length to be passed to Python
thus, sending a quantity other than length as the distance will not be accepted:
Quality control with units
if you have a Python script that assumes certain quantities for its inputs, you can add quality control to it using the above capability.
Unit conversion
The conversion of units will be done automatically inside Python, for example when adding and subtracting compatible quantities. If you want to explicitly convert a quantity into a desired unit, you can use the .to() method inside Python, or the to keyword in Math parameters on the page:
unit conversion inside Python
unit conversion in math parameters
Automatic prefix selection
When the magnitude of a quantity becomes too small or too large, CalcTree automatically picks an appropriate unit prefix, so that the magnitude becomes a more readable number, for example:
you can override this behavior by using the to conversion keyword mentioned in the previous section:
Underlying library used for units
CalcTree uses a Python library called pint to handle units. You can access pint documentation here. Still have more questions?
Join the conversation on Slack below!