Class: VaspUtils::Xdatcar

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

Overview

Class to manage XDATCAR format of VASP.

基本的に、読み込みだけを行う。

Defined Under Namespace

Classes: ElementMismatchError, ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Xdatcar

Returns a new instance of Xdatcar.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vasputils/xdatcar.rb', line 16

def initialize(hash)
  hash.each do |key,val|
    @comment            = val if :comment            ==key
    @scale              = val if :scale              ==key
    @axes               = val if :axes               ==key
    @elements           = val if :elements           ==key
    @nums_elements      = val if :nums_elements      ==key
    #@selective_dynamics = val if :selective_dynamics ==key
    @direct             = val if :direct             ==key
    @steps_positions     = val if :steps_positions          ==key
  end
end

Instance Attribute Details

#axesObject

Returns the value of attribute axes.



14
15
16
# File 'lib/vasputils/xdatcar.rb', line 14

def axes
  @axes
end

#commentObject (readonly)

Returns the value of attribute comment.



12
13
14
# File 'lib/vasputils/xdatcar.rb', line 12

def comment
  @comment
end

#directObject (readonly)

Returns the value of attribute direct.



12
13
14
# File 'lib/vasputils/xdatcar.rb', line 12

def direct
  @direct
end

#elementsObject (readonly)

Returns the value of attribute elements.



12
13
14
# File 'lib/vasputils/xdatcar.rb', line 12

def elements
  @elements
end

#nums_elementsObject (readonly)

Returns the value of attribute nums_elements.



12
13
14
# File 'lib/vasputils/xdatcar.rb', line 12

def nums_elements
  @nums_elements
end

#scaleObject (readonly)

Returns the value of attribute scale.



12
13
14
# File 'lib/vasputils/xdatcar.rb', line 12

def scale
  @scale
end

#steps_positionsObject (readonly)

Returns the value of attribute steps_positions.



12
13
14
# File 'lib/vasputils/xdatcar.rb', line 12

def steps_positions
  @steps_positions
end

Class Method Details

.load_file(file) ⇒ Object

file で与えられた名前のファイルを読み込んで self クラスインスタンスを返す。構文解析できなければ例外 Xdatcar::ParseError を投げる。



85
86
87
88
# File 'lib/vasputils/xdatcar.rb', line 85

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

.parse(io) ⇒ Object

io を読み込んで Xdatcar クラスインスタンスを返す。構文解析できなければ例外 Xdatcar::ParseError を投げる。



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vasputils/xdatcar.rb', line 31

def self.parse(io)
  # analyze XDATCAR.

  begin
    #line 1: comment (string)
    comment = io.readline.chomp

    #line 2: universal scaling factor (float)
    scale = io.readline.to_f
    raise "Xdatcar.load_file cannot use negative scaling factor.\n" if scale < 0

    #line 3-5: axes (3x3 Array of float)
    axes = []
    3.times do |i| #each axis of a, b, c.
      vec = io.readline.strip.split(/\s+/) #in x,y,z directions
      axes << vec.collect! { |j| j.to_f * scale } #multiply scaling factor
    end

    vals = io.readline.strip.split(/\s+/)
    elements = nil
    if vals[0].to_i == 0
      elements = vals
      vals = io.readline.strip.split(/\s+/)
    end
    nums_elements = vals.map{|i| i.to_i}

    io.readline # should be empty line
    steps_positions = []
    index = 0
    io.readlines.each do |line|
      steps_positions[index] ||= []
      if line =~ /^\s*$/
        index += 1
      else
        steps_positions[index] << line.strip.split(/\s+/).map {|coord| coord.to_f}
      end
    end
  rescue EOFError
    raise ParseError, "end of file reached"
  end

  options = {
    :comment            => comment           ,
    :scale              => scale             ,
    :axes               => axes              ,
    :elements           => elements          ,
    :nums_elements      => nums_elements     ,
    :steps_positions          => steps_positions         ,
  }
  self.new(options)
end