Annotation of loncom/mupad_utils/units/unit_list_maker.pl, revision 1.1
1.1 ! tyszkabe 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>