#!/usr/bin/ruby require "ftools" if ARGV.length<4 then puts "Argumets: [Poisson input file] [h1] [h2] [step] [procs]" puts " where h1 and h2 represent the range of corrections to the top of magnet yoke" puts " to introduce i.e. H+h1, H+(h1+step) ... H+h2" puts " The options \"procs\" parameter specified the number of processors to utilize." exit end # Read inn and interpret arguments IN7file="Gap_to_hodo.in7" InFName=ARGV[0] FName_base=get_fname_base(InFName); h1=ARGV[1].to_f h2=ARGV[2].to_f hstep=ARGV[3].to_f Procs= ARGV[4]==nil ? 2 : ARGV[4].to_i if h1>h2 then h=h1; h1=h2; h2=h; else h=h1 end # prepare run environment outdir=FName_base.chop+"_HeightScan_#{h1},#{hstep}_#{h2}" prep_workspace(outdir) File.copy("../"+InFName,".") i=1; run_queue=[] while h<=h2 # prepare path organization strings puts (runpath="run_h#{h}") runbase=FName_base.chop+"_h#{h}" runpathbase=runpath+"/"+runbase Dir.mkdir(runpath) # make run subdir # generate new input file from base and adjust current gen_amfile(h,InFName,runpathbase+".am") system("echo #{h} `ruby ../AdjCur.rb "+runpathbase+".am 15000 25000 18000`>>h_vs_I.txt") # Generate interpolation file File.open(runpath+"/"+IN7file,"w") {|fid| fid.printf("Line\r\n0 -30 0 100\r\n281\r\nEnd")} # generate batch file for running the Poisson sequence fid=File.open(runpathbase+".bat","w") {|fid| fid.write("@echo off\r\n start /low /w /min automesh "+runbase+\ ".am\r\n start /low /w /min poisson\r\n sf7 "+\ IN7file+" "+runbase+".T35") fid.chmod(0755) } # run batch system("cmd /c \"cd "+runpath+" & "+runbase+".bat\""+\ ((i % Procs)==0 ? "" : " &")) run_queue << runpath # collect run paths for result collection later h+=hstep; i+=1; end run_queue.each {|run| proc_results(run)} # result collection system("unix2dos h_vs_I.txt") # Functions BEGIN{ def gen_amfile(h,fname_in,fname_out) fout=File.open(fname_out,"w") File.open(fname_in) {|fin| while (line=fin.gets) != nil if line.index("HalfHeightVar")!=nil then sl=line.split(/[=,]/) fout.write(sl[0]+"=#{sl[1].to_f+h},") else fout.write(line) end end } fout.close end def proc_results(runpath) if (existResFile=File.exist?("Gap_to_hodo_field.txt")) then finold=File.open("Gap_to_hodo_field.txt") end fout=File.open("TmpResFile.txt","w") File.open(runpath+"/OUTSF7.TXT") {|fin| startrec=false while (str=fin.gets) != nil str2=str.split(' ') if startrec && str2.length==9 then if existResFile then str1=finold.gets.chop else str1=str2[1] end fout.write(str1+" "+str2[4]+"\r\n") else startrec=(str2[1]=="(cm)" && str2[2]=="(G)") end end } fout.close; if existResFile then finold.close end File.move("TmpResFile.txt","Gap_to_hodo_field.txt") end def prep_workspace(outdir) Dir.mkdir(outdir) Dir.chdir(outdir) end def get_fname_base(fname) fname_split=fname.split('.'); return fname.chomp(fname_split[fname_split.length-1]) end }