Class: VaspUtils::Incar

Inherits:
Hash
  • Object
show all
Defined in:
lib/vasputils/incar.rb

Overview

Class to utilize INCAR file of VASP. まず、自分で使う範囲だけ作る。あとで余力があれば精密化する。

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/vasputils/incar.rb', line 22

def data
  @data
end

Class Method Details

.load_file(file) ⇒ Object

与えられた名前のファイルを INCAR として解析したハッシュを返す。



51
52
53
54
# File 'lib/vasputils/incar.rb', line 51

def self.load_file(file)
  io = File.open(file, "r")
  return self.parse(io)
end

.parse(io) ⇒ Object

与えられた IO を読み込み、INCAR として解析したハッシュを返す。



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vasputils/incar.rb', line 25

def self.parse(io)
  results = self.new
  io.each_line do |line|
    line.sub!(/\#.*/, "") # コメント文字以降を削除
    next unless /=/ =~ line
    if /(.*)=(.*)/ =~ line
      key = $1.strip
      val = $2.strip
      val.sub!(/\s.*$/, "")
      next if key.empty?
      if val.integer?
        val = val.to_i
      elsif val.float?
        val = val.to_f
      elsif val == ".TRUE."
        val = true
      elsif val == ".FALSE."
        val = false
      end
      results[key] = val
    end
  end
  results
end

Instance Method Details

#append(setting_name, setting = VaspUtils::Setting.new) ⇒ Object

Load setting with ‘setting_name’ in setting file, i.e., ~/.vasputils, and append to self.



58
59
60
# File 'lib/vasputils/incar.rb', line 58

def append(setting_name, setting = VaspUtils::Setting.new)
  self.merge!(setting['incar'][setting_name])
end

#dump(io = nil) ⇒ Object

io に書き出す。io が nil の場合は INCAR 形式文字列を返す。(改行文字を埋め込んでおり、配列化していない)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vasputils/incar.rb', line 65

def dump(io = nil)
  result = self.map { |key, val|
    if val == true
      val = ".TRUE."
    elsif val == false
      val = ".FALSE."
    end
    sprintf("%-8s = %s", key, val.to_s)
  }.join("\n")

  if io # is defined
    io.print result
  else
    return result
  end
end