Unit management
[GridLAB-D Core]


Detailed Description

The unit manager converts values from one unit to another.

The unit manager reads the file unitfile.txt. The file loaded is the first found according the following search sequence:

Units are based on 6 physical/economic constants:

	#c=2.997925e8 ; speed of light (m/s)
	#e=1.602189246e-19 ; electron charge (C)
	#h=6.62617636e-24 ; Plank's constant (kg.m.m/s)
	#k=1.38066244e-13 ; Boltzman's constant (kg.m.m/s.s.K)
	#m=9.10953447e-31 ; electron rest mass (kg)
	#s=1.233270e4 ; average price of gold in 1990 ($/kg - 383.59$/oz)
	

From these are derived all the fundamental SI units:

	m=-1,0,1,0,-1,0,4.121487e01,0,7
	kg=0,0,0,0,1,0,1.09775094e30,0,10
	s=-2,0,1,0,-1,0,1.235591e10,0,7
	A=2,1,-1,0,1,0,5.051397e08,0,7
	K=2,0,0,-1,1,0,1.686358e00,0,7
	cd=4,0,-1,0,2,0,1.447328E+00,0,7
	

An addition currency unit is defined because it's really useful:

	1990$=0,0,0,0,1,1,1.097751e30,0,7
	

Based on these units, all the other units are derived using simple formulae, e.g.:

	; Length
	cm=0.01 m
	mm=0.001 m
	km=1000 m
	in=2.54 cm
	ft=12 in
	yd=3 ft
	mile=5280 ft
	

of Operation

The theory behind the unit management system is based on the fact that every physical quantity can be expressed in terms of a linear combination of exponents of the five basic physical constants (to which a currency constant is added to allow cost-based units). This is best understood by considering the equation

\[ quantity = value * unit \]

where

\[ unit = c^k_c * e^k_e * h^k_h * k^k_k * m^k_m * s^k_s \]

where c, e, h, k, m, and s are the constants defined above, and the k terms are the unit terms defined for that unit.

Thus, two values can only be added (or subtracted) only if their units are the same (i.e., unit is a common factor), and when two values are multiplied (or divided), the exponential terms of the units are added (or subtracted).

For example, 1 second is defined as

\[ 1.235591x10^{10} h/mc^2 \]


Classes

struct  s_unit

Typedefs

typedef s_unit UNIT
 the UNIT structure

Functions

int unit_constant (char name, double value)
int unit_convert (char *from, char *to, double *pValue)
 Convert a value from one unit to another.
int unit_convert_ex (UNIT *pFrom, UNIT *pTo, double *pValue)
 Convert a value from one unit to another.
int unit_derived (char *name, char *derivation)
UNITunit_find (char *unit)
 Find a unit.
UNITunit_find_underived (char *unit)
void unit_init (void)
 Initialize the unit manager.
int unit_precision (char *term)
int unit_primary (char *name, double c, double e, double h, double k, double m, double s, double a, double b, int prec)
int unit_test (void)
 Test the unit manager.


Function Documentation

int unit_convert ( char *  from,
char *  to,
double *  pValue 
)

Convert a value from one unit to another.

Returns:
1 if successful, 0 if failed

Definition at line 474 of file unit.c.

References unit_convert_ex(), and unit_find().

Referenced by unit_test().

int unit_convert_ex ( UNIT pFrom,
UNIT pTo,
double *  pValue 
)

Convert a value from one unit to another.

Returns:
1 if successful, 0 if failed

Definition at line 486 of file unit.c.

References s_unit::a, s_unit::b, s_unit::c, s_unit::e, s_unit::h, s_unit::k, s_unit::m, and s_unit::s.

Referenced by unit_convert().

UNIT * unit_find ( char *  unit  ) 

Find a unit.

Returns:
a pointer to the UNIT structure
Parameters:
unit  the name of the unit

Definition at line 500 of file unit.c.

References s_unit::name, s_unit::next, unit_derived(), and unit_init().

Referenced by class_define_map(), global_create(), object_get_unit(), and unit_convert().

void unit_init ( void   ) 

Initialize the unit manager.

Todo:
scalars on units are ignored right now (ticket #60)

Definition at line 353 of file unit.c.

References find_file(), output_error(), output_verbose(), output_warning(), throw_exception(), unit_constant(), unit_derived(), and unit_primary().

Referenced by unit_find().

int unit_test ( void   ) 

Test the unit manager.

Returns:
the number of failed tests

Definition at line 524 of file unit.c.

References output_test(), output_verbose(), PI, unit_convert(), and VALUE.

Referenced by cmdarg_load().


GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy