File:  [LON-CAPA] / loncom / mupad_utils / units / unit_list_maker.pl
Revision 1.1: download - view: text, annotated - select for diffs
Thu Aug 10 20:17:57 2000 UTC (23 years, 9 months ago) by tyszkabe
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, version_0_6_2, version_0_6, version_0_5_1, version_0_5, version_0_4, stable_2002_spring, stable_2002_july, stable_2002_april, stable_2001_fall, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, conference_2003, bz6209-base, bz6209, bz5969, bz2851, STABLE, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Added the perl file that generates a Mupad readable units file from an easy to read input_file
these files are all that is needed to configure Mupad to handle units correctly.
Ben

    1: #!/usr/bin/perl
    2: #
    3: # 
    4: #
    5: # This program generates a MuPad readable file
    6: # of physical units definitions based from a user
    7: # defined file. At the end of this file and in
    8: # the file unit_list are instructions
    9: # regarding the format of the file unit_list.
   10: #
   11: # To read the file, once in MuPad, use the following cmd:
   12: #     read("units"):
   13: #
   14: #                 --Benjamin Tyszka, 7/2000
   15: #
   16: #######################################
   17: 
   18: use strict;
   19: 
   20: my $input_file = "unit_list";
   21: my %prefix = (
   22: 	   "Y" => "10^(24)",
   23: 	   "Z" => "10^(21)",
   24: 	   "E" => "10^(18)",
   25: 	   "P" => "10^(15)",
   26: 	   "T" => "10^(12)",
   27: 	   "G" => "10^(9)",
   28: 	   "M" => "10^(6)",
   29: 	   "k" => "10^(3)",
   30: 	   "h" => "10^(2)",
   31: 	   "d" => "10^(-1)",
   32: 	   "c" => "10^(-2)",
   33: 	   "m" => "10^(-3)",
   34: 	   "u" => "10^(-6)",
   35: 	   "n" => "10^(-9)",
   36: 	   "p" => "10^(-12)",
   37: 	   "f" => "10^(-15)",
   38: 	   "a" => "10^(-18)",
   39: 	   "z" => "10^(-21)",
   40: 	   "y" => "10^(-24)",
   41: 	  );
   42: my $category;
   43: my %def_units;
   44: my @current_units;
   45: 
   46: open(UNITLIST, $input_file) or die "Can't open $input_file: $!\n";
   47: open(UNITS, ">units") or die "Can't open units: $!\n";
   48: 
   49: while(<UNITLIST>) {
   50:     s/\#.*//;                # strip comments
   51:     chomp;                   #
   52:     s/^\s+//;                # strip leading spaces
   53:     s/\s+$//;                # strip ending spaces
   54:     next unless length;      # skip blank lines
   55: 
   56: 
   57:     if ( /^(\S+)\s+units/ ) {         # if starting new category of units
   58: 
   59: 	$category=$1;
   60: 	$def_units{$category}=[()];
   61: 
   62:     }
   63:     elsif ( /^(\S+?)(\=\S+|\s+|$)\s*(\S*)/ ) {    # else if definition line
   64: 
   65:       my $unit=$1; my $def=$2; my $omit=$3;
   66: 
   67:       if ( $def=~ s/=/:=/ ) {            # definition exists? then print it
   68: 	print UNITS "$unit$def;\n";
   69:       }
   70: 
   71:       print UNITS "protect( $unit );\n";
   72:       push @{ $def_units{$category} }, "$unit";
   73: 
   74:       unless ( $omit =~ /constant/ ) {      # unless constant, print prefixes
   75: 	foreach my $current (keys %prefix) {
   76: 	  $_ = $omit;
   77: 	  unless (/$current/) {
   78: 	    print UNITS "$current$unit := ( $prefix{$current}*$unit ):\nprotect($current$unit);\n";
   79: 	    push @{ $def_units{$category} }, "$current$unit";
   80: 	  }
   81: 	}
   82:       }
   83:     }
   84:     else {
   85:       die("Improperly formatted line");
   86:     }
   87: 
   88:     print UNITS "\n\n";
   89: 
   90: }
   91: 
   92: close UNITLIST;
   93: 
   94: #########################
   95: #
   96: # Now that it is done defining units we print a mupad function
   97: #   that unassigns units by category.
   98: #
   99: #
  100: 
  101: print UNITS "unassignunits:=proc(n)\n begin\n";
  102: foreach my $current ( keys %def_units ) {
  103:     print UNITS "if n=\"$current\" then\n";
  104:     print UNITS "unassign( ".join( ", ", @{$def_units{$current}} )."):\n";
  105:     print UNITS "end_if;\n";
  106: }
  107:     print UNITS "end_proc;\n";
  108: 
  109: 
  110: close UNITS;
  111: 
  112: 
  113: ##############################
  114: #
  115: # The format of the input file 'unit_list' is as follows:
  116: #
  117: #
  118: # 1. Units must be broken up into categories. Start each category
  119: #    with the following line:
  120: #
  121: #        name units
  122: #
  123: #    where 'name' is the name of your category (e.g. SI or English).
  124: #    Use no spaces in 'name' of your category.
  125: #
  126: #
  127: # 2. Next, define all units within a category using:
  128: #
  129: #	unit[=expression] [excluded prefixes or 'constant']
  130: #
  131: #    where unit is the name of your units and exptression is what your
  132: #    units are equivalent to.
  133: #    Note that NO spaces can be used in the expression, before/after
  134: #    the '=', or in the excluded prefixes.
  135: #    The only spaces MUST be between the unit and the excluded prefixes (if there
  136: #    are any excluded prefixes)
  137: #
  138: #    'constant' can be used in place of excluded prefixes in order to exclude
  139: #    all prefixes. Here are some examples:
  140: #
  141: #       Example units                 # defines category named 'Example'. The
  142: #                                     #   following will be in that category:
  143: #       s                             # defines seconds
  144: #       hr=(3600*s)                   # defines hours using an expression
  145: #       ft=(12*inch) f                # defines feet. The f excludes fft 
  146: #                                     #   because this
  147: #                                     #   this fft is a function in Mupad
  148: #       c=(2.99*10^8*m/s) constant    # defines the constant speed of light
  149: #
  150: # 3. Blank lines do not effect anything and everything following a '#'
  151: #    is a comment.
  152: #
  153: #  
  154: #
  155: #########################

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>