#!/usr/bin/ruby require "ftools" if ARGV.length<4 then puts "Argumets: [Poisson input file] [h1] [h2] [step] [b1] [b2] [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" IvshbFile="I_vs_h,b.txt" InFName=ARGV[0] FName_base=get_fname_base(InFName); h1=ARGV[1].to_f h2=ARGV[2].to_f hstep=ARGV[3].to_f b1=ARGV[4].to_f b2=ARGV[5].to_f bstep=ARGV[6].to_f Procs= ARGV[7]==nil ? 2 : ARGV[7].to_i if h1>h2 then h=h1; h1=h2; h2=h; else h=h1 end if b1>b2 then b=b1; b1=b2; b2=b; else b=b1 end # prepare run environment outdir=FName_base.chop+"_ScanOutDim_h#{h1},#{hstep}_#{h2}_b#{b1},#{bstep}_#{b2}" prep_workspace(outdir) File.copy("../"+InFName,".") i=1; run_queue=[] while h<=h2 while b<=b2 # prepare path organization strings puts (runpath="run_h#{h}_b#{b}") runbase=FName_base.chop+"_h#{h}_b#{b}" runpathbase=runpath+"/"+runbase Dir.mkdir(runpath) # make run subdir # generate new input file from base and adjust current gen_amfile(h,b,InFName,runpathbase+".am") system("echo #{h} #{b} `ruby ../AdjCur.rb "+runpathbase+".am 20000 46900 18000 0.001 `>> #{IvshbFile}") sleep(2) # Generate interpolation file File.open(runpath+"/"+IN7file,"w") {|fid| fid.printf("Line\r\n30 30 30 200\r\n340\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 b+=bstep; i+=1; sleep(2) end b=b1; h+=hstep; end sleep(5) res2mat(IvshbFile,"Mat_"+IvshbFile) #run_queue.each {|run| proc_results(run)} # result collection #system("unix2dos h_vs_I.txt") # Functions BEGIN{ def res2mat(fname_in,fname_out) c1="" fout=File.open(fname_out,"w") File.open(fname_in) {|fin| while (line=fin.gets) != nil sl=line.split(' ') if c1!=sl[0] && c1!="" then fout.write("\r\n") end c1=sl[0] fout.write("#{sl[2]} ") end } fout.close end def gen_amfile(h,b,fname_in,fname_out) fout=File.open(fname_out,"w") File.open(fname_in) {|fin| while (line=fin.gets) != nil #sleep 0.5 if line.index("HalfHeightVar")!=nil then sl=line.split(/[=,]/) fout.write(sl[0]+"=#{sl[1].to_f+h},\r\n") elsif line.index("BackWidVar")!=nil then sl=line.split(/[=,]/) fout.write(sl[0]+"=#{sl[1].to_f-b} &\r\n") 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") startrec=false File.open(runpath+"/OUTSF7.TXT") {|fin| while (str=fin.gets) != nil str2=str.split(' ') if startrec and str2.length==9 then if existResFile then str1=finold.gets.chop else str1=str2[1] end fout.write(str1+" "+str2[4]+"\n") end startrec= (startrec || (str2[1]=="(cm)" && str2[2]=="(G)")) end } fout.close; if existResFile then finold.close end dummy=runpath.split('_'); File.copy("TmpResFile.txt",dummy[dummy.length-1] + ".txt") 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 }