carputils
Doxygen code documentation for the python framework controlling openCARP
Classes
carputils.carpio.quantity Namespace Reference

Provides a class to convert physical quantities from a string to numerical values. More...

Classes

class  PhysicalQuantity
 Defines a physical quantity. More...
 

Detailed Description

Provides a class to convert physical quantities from a string to numerical values.

The PhysicalQuantity class can be used in combination with the ArgumentParser class whenever physical quantities are required as arguments. First you have to define your physical quantity:

# supported units and their scale factors to a reference unit, kPa in this case
unitdict = {'Pa': 0.001, 'kPa': 1.0, 'mPa': 1000.0, 'mmHg': 0.133322387415}
# defualt input unit
iunit = 'kPa'
# output unit
ounit = 'mmHg'
PressureQuantity = PhysicalQuantity(unitdict, iunit, ounit)

Next you can define your argument:

parser.add_argument('--pressure', type=PressureQuantity, default=10.0, ....)
args = parser.parse_args()
# `args.pressure` is of type `float` and already converted to the desired output unit

Now the user can state a pressure with different units:

./script.py --pressure 10.0Pa
./script.py --pressure 10.0kPa
./script.py --pressure 10.0mPa
./script.py --pressure 10.0mmHg
./script.py --pressure 10.0       # No unit, default input unit is used