#!/usr/bin/ruby require "ftools" if ARGV.length<4 || !File.exist?(ARGV[0]) then puts "Argumets: [Poisson input file] [I1] [I2] [Btarg] [precision]" puts " where I1 and I2 represent the current range to search for the Btarg" puts " field. 0.5% of Btarg is the default if precision is not specified" exit end WorkFilePre="CurAdj_" Dir.chdir(File.dirname(ARGV[0])) InFName=File.basename(ARGV[0]) FName_base=get_fname_base(InFName) File.open("TaggerPoleCenter.in7","w") {|file| file.printf("Line\n0 0 0 0\n1\nEnd")} i1=ARGV[1].to_f i2=ARGV[2].to_f Btarg=ARGV[3].to_f Bsol_eps= ARGV[4]==nil ? Btarg*0.005 : ARGV[4].to_f if i1>i2 then i=i1; i1=i2; i2=i; end # Get initial B value bounds b1=get_B0(InFName,i1) ; b=b2=get_B0(InFName,i2) # Iterate via secant method match the current to Btarg while (b-Btarg).abs() > Bsol_eps i=i1+(Btarg-b1)*(i2-i1)/(b2-b1) b=get_B0(InFName,i) if b>Btarg then b2=b; i2=i else b1=b; i1=i; end end File.copy(InFName,InFName+".bak") File.copy(WorkFilePre+InFName,InFName) #File.move((WorkFilePre+FName_base).upcase+"T35",FName_base.upcase+"T35") puts i # Functions: BEGIN{ # Define origin B-field search function in SF7 output file def get_B0(fname,current) fname2=get_upd_cur_fname(fname,current) system("cmd /c start /low /w /min automesh " + fname2) system("cmd /c start /low /w /min poisson") system("SF7 TaggerPoleCenter.in7 " + get_fname_base(fname2) + "T35") File.open("OUTSF7.TXT") do |io| io.each {|line| line.chomp! return line.split(' ')[2].to_f.abs() if line.include? " 0.00000 0.00000"} end 0 end def get_fname_base(fname) fname_split=fname.split('.'); return fname.chomp(fname_split[fname_split.length-1]) end def get_upd_cur_fname(fin,current) fout=WorkFilePre+get_fname_base(fin)+"am" fid_in=File.open(fin,"r") fid_out=File.open(fout,"w") fid_in.readlines.each {|line| if line.index("cur")!=nil then fid_out.write("cur="+(line.gsub(' ','').index("=-") ? "-":"")+"#{current}\r\n") else fid_out.write(line) end } fid_in.close; fid_out.close return fout end }