| 1 | #! /usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # Generic scan of variables (obsolete). Rather use mcrun. |
|---|
| 4 | # |
|---|
| 5 | # Usage: |
|---|
| 6 | # [perl] gscan.pl numpoint sim-exec file (VAL | VAL,VAL) ... |
|---|
| 7 | # |
|---|
| 8 | # Using mcrun.pl instead of this script is recommended |
|---|
| 9 | # |
|---|
| 10 | # This file is part of the McStas neutron ray-trace simulation package |
|---|
| 11 | # Copyright (C) 1997-2004, All rights reserved |
|---|
| 12 | # Risoe National Laborartory, Roskilde, Denmark |
|---|
| 13 | # Institut Laue Langevin, Grenoble, France |
|---|
| 14 | # |
|---|
| 15 | # This program is free software; you can redistribute it and/or modify |
|---|
| 16 | # it under the terms of the GNU General Public License as published by |
|---|
| 17 | # the Free Software Foundation; version 2 of the License. |
|---|
| 18 | # |
|---|
| 19 | # This program is distributed in the hope that it will be useful, |
|---|
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | # GNU General Public License for more details. |
|---|
| 23 | # |
|---|
| 24 | # You should have received a copy of the GNU General Public License |
|---|
| 25 | # along with this program; if not, write to the Free Software |
|---|
| 26 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 27 | |
|---|
| 28 | die "Usage: |
|---|
| 29 | [perl] gscan.pl numpoint numneutrons sim-exec file PARM=(VAL|VAL,VAL) ..." |
|---|
| 30 | unless $#ARGV >= 4; |
|---|
| 31 | |
|---|
| 32 | ($npoints, $numneutrons, $simprog, $simfile, @VALS) = @ARGV; |
|---|
| 33 | |
|---|
| 34 | # Turn off buffering on stdout. This improves the use with the Unix |
|---|
| 35 | # "tee" program to create log files of scans. |
|---|
| 36 | $| = 1; |
|---|
| 37 | |
|---|
| 38 | $i = 0; |
|---|
| 39 | $j = 0; |
|---|
| 40 | @parmname = (); |
|---|
| 41 | @values = (); |
|---|
| 42 | @scanned = (); |
|---|
| 43 | @minval = (); |
|---|
| 44 | @maxval = (); |
|---|
| 45 | while(@VALS) { |
|---|
| 46 | $v = shift @VALS; |
|---|
| 47 | if($v =~ /^([a-zA-Z0-9_]+)=(.+),(.+)$/) { |
|---|
| 48 | # Variable to scan from min to max. |
|---|
| 49 | $parmname[$i] = $1; |
|---|
| 50 | $minval[$j] = $2; |
|---|
| 51 | $maxval[$j] = $3; |
|---|
| 52 | $scanned[$j] = $i; |
|---|
| 53 | if($minval[j] != $maxval[j] && $npoints < 2) { |
|---|
| 54 | die "gscan: Cannot scan variable $parmname[$i] using only one data point"; |
|---|
| 55 | } |
|---|
| 56 | $j++; |
|---|
| 57 | } elsif($v =~ /^([a-zA-Z0-9_]+)=(.+)$/) { |
|---|
| 58 | $parmname[$i] = $1; |
|---|
| 59 | $values[$i] = $2; |
|---|
| 60 | } else { |
|---|
| 61 | die "gscan: Invalid parameter specification '$v'"; |
|---|
| 62 | } |
|---|
| 63 | $i++; |
|---|
| 64 | } |
|---|
| 65 | $numvars = $i; |
|---|
| 66 | |
|---|
| 67 | # Now do the scan. |
|---|
| 68 | open(OUT, ">$simfile"); |
|---|
| 69 | $firsttime = 1; |
|---|
| 70 | $outputs = ""; |
|---|
| 71 | @youts = (); |
|---|
| 72 | for($point = 0; $point < $npoints; $point++) { |
|---|
| 73 | $out = ""; |
|---|
| 74 | for($j = 0; $j <= $#scanned; $j++) { |
|---|
| 75 | $values[$scanned[$j]] = |
|---|
| 76 | ($maxval[$j] - $minval[$j])/($npoints - 1)*$point + $minval[$j]; |
|---|
| 77 | $out .= "$values[$scanned[$j]] "; |
|---|
| 78 | $outputs .= "$parmname[$scanned[$j]] " if $firsttime |
|---|
| 79 | } |
|---|
| 80 | $cmd = "$simprog --ncount=$numneutrons"; |
|---|
| 81 | for($i = 0; $i < $numvars; $i++) { |
|---|
| 82 | $cmd .= " $parmname[$i]=$values[$i]"; |
|---|
| 83 | } |
|---|
| 84 | print "Running '$cmd'\n"; |
|---|
| 85 | open (SIM, "$cmd |") || die "gscan: Could not run mcstas simulation"; |
|---|
| 86 | $got_error = 0; |
|---|
| 87 | while(<SIM>) { |
|---|
| 88 | chomp; |
|---|
| 89 | if(/Detector: ([^ =]+_I) *= *([^ =]+) ([^ =]+_ERR) *= *([^ =]+) ([^ =]+_N) *= *([^ =]+) *(?:"[^"]+" *)?$/) { |
|---|
| 90 | $sim_I = $2; |
|---|
| 91 | $sim_err = $4; |
|---|
| 92 | $sim_N = $6; |
|---|
| 93 | $out .= " $sim_I $sim_err $sim_N"; |
|---|
| 94 | if($firsttime) { |
|---|
| 95 | $outputs .= " $1 $3 $5"; |
|---|
| 96 | push @youts, "($1,$3,$5)"; |
|---|
| 97 | } |
|---|
| 98 | } elsif(m'^Error:') { |
|---|
| 99 | $got_error = 1; |
|---|
| 100 | } |
|---|
| 101 | print "$_\n"; |
|---|
| 102 | } |
|---|
| 103 | close(SIM); |
|---|
| 104 | die "gscan: Exit due to error returned by simulation program" if $got_error; |
|---|
| 105 | print OUT "$out\n"; |
|---|
| 106 | $firsttime = 0; |
|---|
| 107 | } |
|---|
| 108 | close(OUT); |
|---|
| 109 | print "Output file: '$simfile'\nOutput parameters: $outputs\n"; |
|---|
| 110 | |
|---|
| 111 | # Write gscan.sim information file. |
|---|
| 112 | $infofile = "gscan.sim"; |
|---|
| 113 | open(SIM, ">$infofile") || die "gscan: Failed to write info file '$infofile'"; |
|---|
| 114 | $scannedvars = join ", ", (map { $parmname[$scanned[$_]]; } (0..$#scanned)); |
|---|
| 115 | $xvars = join " ", (map { $parmname[$scanned[$_]]; } (0..$#scanned)); |
|---|
| 116 | $yvars = join " ", @youts; |
|---|
| 117 | print SIM <<END; |
|---|
| 118 | begin data |
|---|
| 119 | type: array_1d($npoints) |
|---|
| 120 | title: 'Scan of $scannedvars' |
|---|
| 121 | xvars: $xvars |
|---|
| 122 | yvars: $yvars |
|---|
| 123 | xlimits: $minval[0] $maxval[0] |
|---|
| 124 | filename: $simfile |
|---|
| 125 | params: $outputs |
|---|
| 126 | end data |
|---|
| 127 | END |
|---|
| 128 | close SIM; |
|---|