# Set the initial values of the unit information.
#
# Copyright (c) 1993, 1994 Pacific Marketing and Consulting
# Author:  Wayne A. Christopher, wayne@pmac.com
#          Pacific Marketing and Consulting, Berkeley, California
#

# The first one in this list is the SI one, and the second one is the default
# English unit.

array set unit_choices {
    length          { m ft cm mm microns in mil }
    1/length        { 1/m 1/ft 1/cm 1/mm 1/in }
    area            { m2 ft2 cm2 mm2 in2 }
    volume          { m3 ft3 cm3 mm3 in3 }
    1/area          { 1/m2 1/ft2 1/cm2 1/mm2 1/in2 }
    speed           { m/s ft/s cm/s mm/s in/s fpm }
    acceleration    { m/s2 ft/s2 cm/s2 mm/s2 in/s2 }
    angle           { deg }
    time            { s }
    1/time          { 1/s }
    concentration   { fraction ppmv }
    
    pressure        { N/m2 lbf/ft2 Pa atm psi torr dyn/cm2 in_water}
    temperature     { C F K R }
    temp_change     { Cd Fd Kd Rd }
    power           { W BTU/s BTU/hr HP erg/s}
    heat            { W BTU/s BTU/hr HP erg/s}
    power/volume    { W/m3 BTU/s-ft3 BTU/h-ft3 erg/cm3-s}
    power/area      { W/m2 BTU/s-ft2 BTU/h-ft2 erg/cm2-s met}
    heat_flux       { W/m2 BTU/s-ft2 BTU/h-ft2 erg/cm2-s met}
    rad_flux        { W BTU/s BTU/hr HP erg/s}
    
    heat_tr_coeff   { W/K-m2 BTU/ft2-s-F BTU/ft2-h-F W/C-m2 BTU/ft2-h-R 
                      BTU/ft2-s-R  }
    roughness       { m ft cm mm microns in }
    vol_expansion   { 1/K 1/R 1/C 1/F }
    density         { kg/m3 lbm/ft3 slug/ft3 g/cm3 }
    sp_heat         { J/kg-K BTU/lb-F J/kg-C BTU/lb-R cal/g-K cal/g-C
	              kcal/kg-K kcal/kg-C erg/g-K }
    heat_capacity   { J/kg-K }
    conductivity    { W/m-K BTU/s-ft-F BTU/h-ft-R W/m-C BTU/h-ft-F cal/s-m-C
	              cal/s-m-K erg/cm-s-K }
    resistance      { C/W F-s/BTU K/W R-s/BTU }
    impedance       { C-m2/W C-in2/W C-mm2/W }
    conductance     { W/C BTU/F-s W/K BTU/R-s BTU/F-h BTU/R-h }
    viscosity       { kg/m-s lbm/ft-s poise lb/ft-s slugs/ft-s }
    mass_flow       { kg/s lbm/s lbm/min g/s }
    mass_flow/area  { kg/s-m2 lbm/s-ft2 g/cm2-s}

    volume_flow     { m3/s ft3/min m3/min ft3/s cfm cm3/s}
    
    diffusivity     { m2/s ft2/s cm2/s}

    mass            { kg lbm g gr }
    mass/area       { kg/m2 oz/ft2 }
    
    mol_weight      { kg/kmol g/mol}
    
    e_resistance    { Ohm-m }
    e_current       { Amp }
    
    turbulent_k     { m2/s2 ft2/s2 }
    turbulent_o     { m2/s3 ft2/s3 }
    
    gr/lbm          { gr/lbm }  
    g/kg            { g/kg }  
    emissivity      { "" }
    losscoeff       { "" }
    fan_curve       { "" }
    viscosity_ratio { "" }
    xxx             { "" }
}

if {$app_name == "icepak"} {
    lappend unit_choices(length) Cu-oz/ft2
}

if {$app_name == "emax"} {
    lappend unit_choices(speed) c
}

array set unit_choices_unused {
    resistance_old { m2-K/W s-ft2-F/BTU h-ft2-F/BTU m2-C/W h-ft2-R/BTU 
		     s-m2-C/cal s-m2-K/cal cm2-s-K/erg }
}

array set unit_names {
    length         "Length"
    1/length       "1 / length"
    area           "Area"
    volume         "Volume"
    1/area         "1 / area"
    1/time         "1 / time"
    speed          "Speed"
    acceleration   "Acceleration"
    angle          "Angle"
    time           "Time"
    pressure       "Pressure"
    temperature    "Temperature"
    temp_change    "Temperature change"
    power          "Power"
    power/area     "Power / area"
    power/volume   "Power / volume"
    heat           "Heat"
    heat_flux      "Heat flux"
    rad_flux       "Radiative heat flow"
    heat_tr_coeff  "Heat tr. coeff"
    roughness      "Roughness"
    vol_expansion  "Vol. expansion"
    density        "Density"
    sp_heat        "Specific heat"
    heat_capacity  "Heat capacity"
    conductivity   "Conductivity"
    resistance     "Resistance"
    impedance      "Thermal impedance"
    conductance    "Conductance"
    viscosity      "Viscosity"
    viscosity_ratio "Viscosity ratio"
    mass_flow      "Mass flow"
    mass_flow/area "Mass flow / area"
    mass           "Mass"
    mass/area      "Mass / area"
    emissivity     "Emissivity"
    losscoeff      "Loss coeff."
    fan_curve      "Fan curve"
    diffusivity    "Diffusivity"
    mol_weight     "Mol. weight"
    volume_flow    "Volume flow"
    e_resistance   "Elect. resistance"
    e_current      "Elect. current"
    turbulent_k    "Turbulent K"
    turbulent_o    "Turbulent Dissipation"
    concentration  "Concentration"
    xxx ""
}
foreach {n v} [array get unit_names] { set unit_names_ext($v) $n }

array set unit_names_ext {
    Velocity speed
    X length
    Y length
    Z length
    UX speed
    UY speed
    UZ speed
}

# To get to the unit from SI : val = c * (SI + x0) + y0
# To get back : SI = (val - y0) / c - x0

#   NAME               y0       c          x0 types             editable

array set unit_data {
    ""               { 0        1          0 "" }
    
    m                { 0        1          0 "length roughness" 	0 }
    ft               { 0        3.28084    0 "length roughness" 	0 }
    cm               { 0        100        0 "length roughness" 	0 }
    mm  	     { 0        1000       0 "length roughness"         0 }
    microns 	     { 0        1e6	   0 "length roughness"         0 }
    in               { 0        39.37008   0 "length roughness" 	0 }
    mil              { 0        39370.08   0 "length roughness" 	0 }
    Cu-oz/ft2        { 0        29362.4    0 "length roughness" 	0 }
    
    1/m              { 0        1          0 1/length                   0 }
    1/ft             { 0        0.3048     0 1/length                   0 }
    1/cm             { 0        1e-2       0 1/length                   0 }
    1/mm             { 0        1e-3       0 1/length                   0 }
    1/in             { 0        0.0254     0 1/length                   0 }
    
    m2               { 0        1          0 area               	0 }
    ft2              { 0        10.76391   0 area               	0 }
    cm2              { 0        1e4        0 area               	0 }
    mm2              { 0        1e6        0 area               	0 }
    in2              { 0        1550.003   0 area               	0 }
    
    m3               { 0        1          0 volume               	0 }
    ft3              { 0        35.3147    0 volume               	0 }
    cm3              { 0        1e6        0 volume               	0 }
    mm3              { 0        1e9        0 volume               	0 }
    in3              { 0        61023.7    0 volume               	0 }
    
    1/m2             { 0        1          0 1/area             	0 }
    1/ft2            { 0        0.09290304 0 1/area             	0 }
    1/cm2            { 0        1e-4       0 1/area             	0 }
    1/mm2            { 0        1e-6       0 1/area             	0 }
    1/in2            { 0        6.45161e-4 0 1/area             	0 }
    
    m/s              { 0        1          0 "speed velocity"     	0 }
    ft/s             { 0        3.28084    0 "speed velocity"           0 }
    fpm              { 0        196.8504   0 "speed velocity"           0 }
    cm/s             { 0        100        0 "speed velocity"           0 }
    mm/s             { 0        1000       0 "speed velocity"           0 }
    in/s             { 0        39.37008   0 "speed velocity"           0 }
    c                { 0        2.99793e+8 0 "speed velocity"           0 }
    
    m/s2             { 0        1          0 acceleration        	0 }
    ft/s2            { 0        3.28084    0 acceleration               0 }
    cm/s2            { 0        100        0 acceleration               0 }
    mm/s2            { 0        1000       0 acceleration               0 }
    in/s2            { 0        39.37008   0 acceleration               0 }
    
    rad              { 0        1          0 angle              	0 }
    deg              { 0        57.2957805 0 angle              	0 }
    
    s                { 0        1          0 time               	0 }
    
    1/s              { 0        1          0 1/time               	0 }
    
    fraction         { 0        1          0 concentration              0 }
    ppmv             { 0        1e6        0 concentration              0 }
    
    N/m2             { 0        1          0 pressure           	0 }
    lbf/ft2          { 0        2.08855e-2 0 pressure           	0 }
    Pa               { 0        1          0 pressure                   0 }
    atm              { 0        9.86923e-6 0 pressure                   0 }
    psi              { 0        1.45038e-4 0 pressure                   0 }
    torr             { 0        7.50063e-3 0 pressure                   0 }
    dyn/cm2          { 0        10         0 pressure                   0 }
    in_water         { 0        4.01606e-3 0 pressure   		0 }
    
    C                { -273.15  1          0 temperature        	0 }
    F                { -459.67  1.8        0 temperature        	0 }
    K                { 0        1          0 temperature        	0 }
    R                { 0        1.8        0 temperature        	0 }
    
    Cd               { 0        1          0 temp_change        	0 }
    Fd               { 0        1.8        0 temp_change        	0 }
    Kd               { 0        1          0 temp_change        	0 }
    Rd               { 0        1.8        0 temp_change        	0 }
    
    W                { 0        1          0 "power heat rad_flux"  	0 }
    BTU/s            { 0        9.48047e-4 0 "power heat rad_flux"      0 }
    BTU/hr           { 0        3.4129693  0 "power heat rad_flux"   	0 }
    HP               { 0        1.34102e-3 0 "power heat rad_flux"      0 }
    erg/s            { 0        1e7        0 "power heat rad_flux"      0 }
    
    W/m2             { 0        1          0 "power/area heat_flux"     0 }
    BTU/s-ft2        { 0        8.8114e-5  0 "power/area heat_flux"     0 }
    BTU/h-ft2        { 0        0.3170878  0 "power/area heat_flux"     0 }
    erg/cm2-s        { 0        1000       0 "power/area heat_flux"     0 }
    met              { 0        0.017182130584193 0 "power/area heat_flux"     0 }
        
    W/m3             { 0        1          0 power/volume               0 }
    BTU/s-ft3        { 0        8.8114e-5  0 power/volume               0 }
    BTU/h-ft3        { 0        0.3170878  0 power/volume               0 }
    erg/cm3-s        { 0        1000       0 power/volume               0 }
    
    W/K-m2           { 0        1          0 heat_tr_coeff      	0 }
    BTU/ft2-s-F      { 0        4.89167e-5 0 heat_tr_coeff     	 	0 }
    W/C-m2           { 0        1          0 heat_tr_coeff              0 }
    BTU/ft2-h-R      { 0        0.17614937 0 heat_tr_coeff              0 }
    BTU/ft2-h-F      { 0        0.17614937 0 heat_tr_coeff              0 }
    BTU/ft2-s-R      { 0        4.89167e-5 0 heat_tr_coeff              0 }
    
    kg/m3            { 0        1          0 density            	0 }
    lbm/ft3          { 0        0.062426   0 density            	0 }
    slug/ft3         { 0        1.94032e-3 0 density            	0 }
    g/cm3            { 0        0.001      0 density            	0 }
    
    1/K              { 0        1          0 vol_expansion      	0 }
    1/C              { 0        1          0 vol_expansion		0 }
    1/F              { 0        .555555555 0 vol_expansion      	0 }
    1/R              { 0        .555555555 0 vol_expansion      	0 }
    
    J/kg-K           { 0        1          0 "sp_heat heat_capacity"    0 }
    BTU/lb-F         { 0        2.38852e-4 0 sp_heat            	0 }
    J/kg-C           { 0        1          0 sp_heat            	0 }
    BTU/lb-R         { 0        2.38852e-4 0 sp_heat            	0 }
    cal/g-K          { 0        2.38852e-4 0 sp_heat            	0 }
    cal/g-C          { 0        2.38852e-4 0 sp_heat            	0 }
    kcal/kg-K        { 0        2.38852e-4 0 sp_heat            	0 }
    kcal/kg-C        { 0        2.38852e-4 0 sp_heat            	0 }
    erg/g-K          { 0        10000      0 sp_heat            	0 }
    
    kg/m-s           { 0        1          0 viscosity          	0 }
    lbm/ft-s         { 0        0.671969   0 viscosity          	0 }
    poise            { 0        10         0 viscosity          	0 }
    lb/ft-s          { 0        0.671969   0 viscosity          	0 }
    slugs/ft-s       { 0        0.02088555 0 viscosity          	0 }
    
    W/m-K            { 0        1          0 conductivity       	0 }
    BTU/s-ft-F       { 0        1.60537e-4 0 conductivity               0 }
    W/m-C            { 0        1          0 conductivity       	0 }
    BTU/h-ft-R       { 0        0.577934   0 conductivity       	0 }
    BTU/h-ft-F       { 0        0.577934   0 conductivity       	0 }
    cal/s-m-C        { 0        0.23890    0 conductivity       	0 }
    cal/s-m-K        { 0        0.23890    0 conductivity       	0 }
    erg/cm-s-K       { 0        1e5        0 conductivity       	0 }
    
    C/W              { 0        1          0 resistance          	0 }
    F-s/BTU          { 0        1898.627   0 resistance          	0 }
    K/W              { 0        1          0 resistance          	0 }
    R-s/BTU          { 0        1898.627   0 resistance          	0 }
    
    C-m2/W           { 0        1          0 impedance          	0 }
    C-in2/W          { 0        1550.003   0 impedance          	0 }
    C-mm2/W          { 0        1e6        0 impedance          	0 }

    W/C              { 0        1          0 conductance          	0 }
    BTU/F-s          { 0        5.26696e-4 0 conductance          	0 }
    W/K              { 0        1          0 conductance          	0 }
    BTU/R-s          { 0        5.26696e-4 0 conductance          	0 }
    BTU/F-h          { 0        1.8961056  0 conductance          	0 }
    BTU/R-h          { 0        1.8961056  0 conductance          	0 }
    
    kg/s             { 0        1          0 mass_flow          	0 }
    lbm/s            { 0        2.20462    0 mass_flow          	0 }
    lbm/min          { 0        132.2774   0 mass_flow                  0 }
    g/s              { 0        1000       0 mass_flow                  0 }

    kg/s-m2          { 0        1          0 mass_flow/area     	0 }
    lbm/s-ft2        { 0        0.20483    0 mass_flow/area     	0 }
    g/cm2-s          { 0        0.1        0 mass_flow/area             0 }
    
    m3/s	     { 0        1	   0 volume_flow	        0 }
    m3/min           { 0        60         0 volume_flow	        0 }
    ft3/s            { 0        35.31467   0 volume_flow                0 }
    ft3/min          { 0        2118.8802  0 volume_flow                0 }
    cfm		     { 0        2118.8802  0 volume_flow		0 }
    cm3/s            { 0        1e6        0 volume_flow                0 }

    m2/s             { 0        1          0 diffusivity        	0 }
    ft2/s            { 0        10.76391   0 diffusivity                0 }
    cm2/s            { 0        1e4        0 diffusivity                0 }
    
    kg               { 0        1          0 mass                	0 }
    lbm              { 0        2.20462    0 mass                	0 }
    g                { 0        1000       0 mass                	0 }
    gr               { 0        14172.34   0 mass                	0 }

    kg/m2            { 0        1          0 mass/area                	0 }
    oz/ft2           { 0        3.2765     0 mass/area                	0 }

    kg/kmol          { 0        1          0 mol_weight         	0 }
    g/mol            { 0        1          0 mol_weight                 0 }
    
    Ohm-m            { 0        1          0 e_resistance               0 }
    
    Amp              { 0        1          0 e_current                  0 }
    
    m2/s2            { 0        1          0 turbulent_k                0 }
    ft2/s2           { 0        10.76391   0 turbulent_k                0 }
    
    m2/s3            { 0        1          0 turbulent_o                0 }
    ft2/s3           { 0        10.76391   0 turbulent_o                0 }

    g/kg             { 0        1          0 g/kg                       0 }
    gr/lbm           { 0        1          0 gr/lbm                     0 }
    identity         { 0        1          0 ""                         0 }
}

array set unit_data_unused {
    m2-K/W            { 0        1          0 resistance         	0 }
    s-ft2-F/BTU       { 0        20442.916  0 resistance                0 }
    m2-C/W            { 0        1          0 resistance         	0 }
    h-ft2-R/BTU       { 0        5.677      0 resistance         	0 }
    h-ft2-F/BTU       { 0        5.677      0 resistance         	0 }
    s-m2-C/cal        { 0        4.18585    0 resistance         	0 }
    s-m2-K/cal        { 0        4.18585    0 resistance         	0 }
    cm2-s-K/erg       { 0        1e-3       0 resistance         	0 }
}

if [info exists allow_wavelength_units] {
#    lappend unit_choices(length) wavlen
#    set unit_data(wavlen)  { 0 1 0 length 0 }
}

# check the consistency of the above tables ....

foreach u [array names unit_choices] {
    if ![info exists unit_default($u)] {
	set unit_default($u) [lindex $unit_choices($u) 0]
	set unit_default_si($u) [lindex $unit_choices($u) 0]
    } else {
	set unit_default_si($u) $unit_default($u)
    }
    set unit_default_english($u) [lindex $unit_choices($u) 1]
    if {$unit_default_english($u) == ""} {
	set unit_default_english($u) $unit_default_si($u)
    }
    foreach ch $unit_choices($u) {
	if ![info exists unit_data($ch)] {
	    puts "ERROR: no unit data for $u $ch"
	} elseif {$ch != ""} {
	    if {[lsearch [lindex $unit_data($ch) 3] $u] < 0} {
		puts "ERROR: $u not found in the unit data types for $ch"
	    }
	}
    }
}
foreach d [array names unit_data] {
    foreach u [lindex $d 3] {
	if {[lsearch $unit_choices($u) $d] < 0} {
	    puts "ERROR: $d not found in the unit choices for $u"
	}
    }
}

foreach un [array names unit_names] {
    set unit_name_internal($unit_names($un)) $un
}
set "unit_name_internal(Heat flow)" heat
set unit_name_internal(Vorticity) 1/time
foreach i {UX UY UZ Velocity} { set unit_name_internal($i) speed }
foreach i {X Y Z} { set unit_name_internal($i) length }

array set preset_unit_choices [array get unit_choices]
array set preset_unit_data [array get unit_data]
array set preset_unit_default [array get unit_default]

# this shouldn't be here but ...

foreach p [array names post_unit_types] {
    set u $post_unit_types($p)
    if {$u != ""} {
	set post_units($u) $unit_default($u)
    }
}
set post_units(viscosity_ratio) identity
set post_units(angle) deg
