×

Notice

The forum is in read only mode.

Python module for easy unit conversion in .comm

  • Stefan
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 years 11 months ago - 11 years 11 months ago #7029 by Stefan
Hi!

Since Units are always causing mistakes by conversions, I started a Python module, which
- Makes it more human readable in .comm files
-Make automatically the correct conversions

Installation:

Change into your desired directory (e.g. your home directory) and execute
git clone https://github.com/maldun/UnitCalculator.git
Example usage in Python:

We assume here that the module is installed in the home directory. In (I)Python:
In [1]: import os
In [2]: os.chdir(os.path.expanduser("~"))
In [3]: from UnitCalculator import *
In [4]: kg() # Default
Out[4]: 1.0
In [5]: kg(T) # Convert kg to Ton
Out[5]: 0.001
In [6]: auto_converter(mmNS) # Start automatic conversion to mm and T system
In [7]: kg()
Out[7]: 0.001
In [8]: auto_converter(SI) # Convert to SI
In [9]: 1000*mm() # Convert 1000mm to SI m
Out[9]: 1.0
In [10]: auto_converter(mmNS) 
In [11]: 8050*(kg()/m()**3) # Converts density of steel from kg/m^3 to T/mm^3
Out[11]: 8.05e-09
In [12]: N/m**2 # Since Version 0.2 direct algebra use is also supported
Out[12]: 1e-06
In [13]: gramm = KiloGrammUnit(1e-3) # Easy (re)definition of units
In [14]: 8050*(kg/m**3) # Converts density of steel from kg/m^3 to T/mm^3 since v0.2
Out[14]: 8.05e-09
In fact the commands kg(), kg(T), m() etc. only return the conversion factors between two units,
so don't forget the multiplication symbol!

Update unit algebra is supported now, i.e. the expression 1*N/m**2 now makes sense

Usage in Code_Aster:
Add to your .comm file (Assuming the module is installed in home):
import sys

sys.path.append(os.path.expanduser("~"))

from UnitCalculator import *

Example in .comm file:
import sys

# Assuming module is in home directory

sys.path.append(os.path.expanduser("~"))

from UnitCalculator import *

auto_converter(mmNS)

DEBUT();

#define material --> steel
MA=DEFI_MATERIAU(ELAS=_F(E=2.1e+11*Pa, # automatically --> 2.1e+5 MPa 
                         NU=0.3,
			 RHO=8050*(kg/m**3), # automatically --> 8.05e-09 T/mm**3
			 ),
		);
...
I hope it is useful!
I distribute it under MIT License so feel free to use and modify it.
If you want to add more units, write me on Github or contribute directly!

This module is under steady development, so be aware of changes!
Last edit: 11 years 11 months ago by Stefan.
Moderators: catux
Time to create page: 0.117 seconds
Powered by Kunena Forum