File: unitconv

package info (click to toggle)
xcrysden 1.6.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,040 kB
  • sloc: ansic: 36,624; tcl: 33,824; fortran: 6,744; sh: 2,219; makefile: 759; f90: 429; awk: 382; pascal: 30
file content (127 lines) | stat: -rwxr-xr-x 2,488 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh
# \
# this line restarts tclsh \
exec tclsh $0 "$@"

#
# Usage: unitconv unit value 
#    or: unitconv unit expr
#
# 

# supported units

set energy_units {
    au    
    ry    
    ev
    kjm    
    kcalm
    kj
    kcal
    erg
}
set length_units {
    angs
    bohr
    m
}
set allowed_units "$energy_units $length_units"


# check for correctness of specified unit on command-line

set unit [string tolower [lindex $argv 0]]

if { [lsearch -nocase $allowed_units $unit] < 0 } {
    puts stderr "input unit not known; must be one of [join $allowed_units {, }]"
    exit
}


# ------------------------------------------------------------------------
# PHYSICAL CONSTANTS
#

# Physical constants, SI (NIST CODATA 2006), Web Version 5.1
# http://physics.nist.gov/constants

set j   1.000000000000
set au  4.35974394e-18;  # J
set ev  1.602176487e-19; # J
set mol 6.02214129e23

# thermo-kilocalorie (International Standard ISO 31-4: Quantities and
# units – Part 4: Heat. Annex B (informative): Other units given for
# information, especially regarding the conversion
# factor. International Organization for Standardization, 1992.)

set kcal 4184;  # J

# from wikipedia:
set erg 1e-7; # J

#--------------------------------
# Energy Units (use J as default)
#--------------------------------

set kj     1000.00000000000000
set ry     [expr $au   / 2.0]
set kjm    [expr $kj   / $mol]
set kcalm  [expr $kcal / $mol]


#-----------------------------------------
# Distance Units (use Angstrom as default)
#-----------------------------------------
set angs 1.0
set bohr 0.52917720859
set m    1.e10
# ------------------------------------------------------------------------


#
# evaluate & convert user specified expression/value
#

upvar #0 $unit un_fac

# parse the command line arguments

foreach item [lrange $argv 1 end] {
    append numbers [format "%s " $item]
}
set orig_value [expr 1.0 * ($numbers)]; 

# store result either in J or Angstroms !!!

set value [expr $un_fac * $orig_value]; 


#
# PRINTOUT
#
puts stdout "$orig_value $unit = "

if { [lsearch -nocase $energy_units $unit] > -1 } {

    # do we have energy ?
    
    foreach _un $energy_units {
	upvar #0 $_un unval
	set val [expr $value / $unval]
	puts stdout "    = $val $_un"
    }

} elseif { [lsearch -nocase $length_units $unit] > -1 }  {

    # ... or length ?

    foreach _un $length_units {
	upvar #0 $_un unval
	set val [expr $value / $unval]
	puts stdout "    = $val $_un"
    }
}

exit 0