class TopasInput

Attributes

base_name[R]
k1[R]
name[R]
r_wp[R]
restrains[R]
text[R]

Public Class Methods

new(text, base_name = nil) click to toggle source
# File lib/topas-tools/TopasInput.rb, line 2
def initialize text, base_name = nil
  @text = text.dup
  @k1 = get_k1
  @base_name  = get_base_name base_name
  @name = get_name
  @restrains = get_restrains
  @r_wp = get_rwp
end

Public Instance Methods

restrain_names() click to toggle source
# File lib/topas-tools/TopasInput.rb, line 26
def restrain_names
  @restrains.map{|r| r[:name]}
end
set_k1(num) click to toggle source
# File lib/topas-tools/TopasInput.rb, line 22
def set_k1(num)
  dup.set_k1!(num)
end
set_k1!(num) click to toggle source
# File lib/topas-tools/TopasInput.rb, line 14
def set_k1!(num)
  @k1 = num.to_f
  @text = @text.sub(/(penalties_weighting_K1\s+)[\d.]+/ , "\\1#{@k1}").
        sub(/[\d.]*(\.cif)/ , "#{@k1}\\1")
  @name = get_name
  self
end
set_restrain(name, new) click to toggle source
# File lib/topas-tools/TopasInput.rb, line 40
def set_restrain(name, new)
  dup.set_restrain!(name, new)
end
set_restrain!(name, new) click to toggle source
# File lib/topas-tools/TopasInput.rb, line 30
def set_restrain!(name, new)
  if r = @restrains.select{|r| r[:name] == name}[0]
    r[:restrain] = new
    @text = write_restrains
    self
  else
    raise  "No restrain with the name #{name}"
  end
end

Private Instance Methods

get_base_name(bn) click to toggle source
# File lib/topas-tools/TopasInput.rb, line 57
def get_base_name bn
  if   bn
    base_name = File.basename(bn, File.extname(bn))
    @text = @text.sub(/phase_name\s+".+"/, %Q[phase_name "#{bn}"])
    base_name
  else
    base_name = @text.match(/phase_name\s+"(.+)"/)[1]
    base_name
  end
end
get_k1() click to toggle source
# File lib/topas-tools/TopasInput.rb, line 48
def get_k1
  @text.scan(/penalties_weighting_K1\s+([\d.]+)/)[0][0].to_f
end
get_name() click to toggle source
# File lib/topas-tools/TopasInput.rb, line 71
def get_name
    "#{@base_name}_#{@k1}"
end
get_restrains() click to toggle source
# File lib/topas-tools/TopasInput.rb, line 76
def get_restrains
  restrains_pattern = /(Distance_Restrain(?:_Breakable|_Morse)?)\(\s*(\w+\s+\w+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)[`_\d.]*\s*,\s*([\d.]+\s*,\s*[\d.]+)\s*\)/
  if @text =~ restrains_pattern
    @text.scan(restrains_pattern).reduce([]){|memo, (type ,name, restrain, value, rest)|
      memo << {name:name, type:type, restrain:restrain.to_f, value:value.to_f, rest:rest}
      memo}
  else
    nil
  end
end
get_rwp() click to toggle source
# File lib/topas-tools/TopasInput.rb, line 52
def get_rwp
  @text.scan(/r_wp\s+([\d.]+)/)[0][0].to_f
end
write_restrains() click to toggle source
# File lib/topas-tools/TopasInput.rb, line 87
def write_restrains
  @restrains.reduce(@text) { |memo, r|
    pattern = /#{r[:type]}\s*\(\s*#{r[:name]}\s*,\s*[\d.]+\s*,\s*[\d.]+[`_\d.]*\s*,\s*[\d.]+\s*,\s*[\d.]+\s*\)/ 
    memo.sub(pattern, "#{r[:type]}(#{r[:name]}, #{r[:restrain]}, #{r[:value]}, #{r[:rest]})")        
  }
end