Version 4 of Thinking about physical computations

Updated 2004-06-18 10:24:35 by AM

Arjen Markus (17 june 2004) Here is a first almost trivial example of how Tcl could be used to facilitate certain physical computations ... I hope I can make this grow into a more serious package ;)

AM (18 june 2004) One thing that might be very useful in such a package: a short explanation of the various procedures and constants, especially if you are using it interactively. BTW, the unit converter is a much better way to achieve the unit conversions I haphazardly implemented here.


 # physparam.tcl --
 #    Musings over the question of units in phyiscal computations
 #
 #    Simple example:
 #    Conversion of temperature to absolute temperature
 #    In many formulas you need to specify the absolute temperature
 #    rather than the temperature most people are used to (degrees
 #    centigrade or the infamous degrees Fahrenheit).
 #    Mistakes are easily made ... so include the unit in the value.
 #
 #

 # physics --
 #    Namespace for the physical computations
 #
 namespace eval ::physics {

    #
    # Add common constants etc.
    #

    namespace export abs_temperature wien_wavelength
 }

 # Conversions --
 #    Set of conversion procedures
 #
 proc ::physics::Conv_K_K   {t} {return $t}
 proc ::physics::Conv_oC_K  {t} {expr {$t+273.15}}
 proc ::physics::Conv_oF_K  {t} {Conv_oC_K [Conv_oF_oC $t]}
 proc ::physics::Conv_oF_oC {t} {expr {5.0*$t/9.0+32.0}}

 # abs_temperature --
 #    Return the absolute temperature
 #
 # Arguments:
 #    temp         (Relative) temperature
 #
 # Result:
 #    The temperature in kelvin (no capital, there was only one
 #    lord Kelvin, AFAIK :)
 #
 proc ::physics::abs_temperature {temp} {

    if { [llength $temp] == 2 } {
       set temp [Conv_[lindex $temp 1]_K [lindex $temp 0]]
    }
    list $temp K
 }

 # wien_wavelength --
 #    Return the wave length at which a perfect black body emits most
 #    energy, given the temperature
 #
 # Arguments:
 #    temp         (Relative) temperature
 #
 # Result:
 #    The wave length in micrometers (um)
 #
 proc ::physics::wien_wavelength {temp} {
    list [expr {2898.0/[lindex [abs_temperature $temp] 0]}] um
 }

 # main --
 #    A simple demonstration
 #
 namespace import ::physics::*

 puts "20 degrees centigrade = [abs_temperature {20 oC}]"
 puts "50 degrees Fahrenheit = [abs_temperature {50 oF}]"
 puts "At 20 degrees centigrade a black body emits most
 energy with a wave length of [wien_wavelength {20 oC}]"

How does this relate to Unit converter and Unit math ?

AM Closely - though my intentions are different than "merely" converting from one unit to another. I want to make the unit an integral part of the quantity's value. This way I can use such quantities in all kinds of simple computations (that is, computations that do not require the solution of partial differential equations and the like).

Something like: I want to know the density of sea water at a given temperature and salinity - so use one of the constitutional equations that people have developed.


[ Category Physics ]