Class: VaspUtils::VasprunXml

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

Defined Under Namespace

Classes: IllegalArgumentError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ VasprunXml

Returns a new instance of VasprunXml.



13
14
15
# File 'lib/vasputils/vasprunxml.rb', line 13

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/vasputils/vasprunxml.rb', line 8

def data
  @data
end

Class Method Details

.load_file(path) ⇒ Object



17
18
19
20
# File 'lib/vasputils/vasprunxml.rb', line 17

def self.load_file(path)
  data = Nokogiri::XML(open(path))
  self.new(data)
end

Instance Method Details

#basesObject



31
32
33
34
35
36
37
# File 'lib/vasputils/vasprunxml.rb', line 31

def bases
  results = @data.xpath("/modeling/calculation").map do |c|
    c.xpath("./structure/crystal/varray[@name='basis']/v").map do |axis|
      axis.text.strip.split.map {|i| i.to_f}
    end
  end
end

#elementsObject



53
54
55
56
57
# File 'lib/vasputils/vasprunxml.rb', line 53

def elements
  @data.xpath("/modeling/atominfo/array[@name='atomtypes']/set/rc").map do |elem|
    elem.xpath('./c').children[1].text
  end
end

#fermi_energyObject



96
97
98
# File 'lib/vasputils/vasprunxml.rb', line 96

def fermi_energy
  @data.xpath("/modeling/calculation/dos/i[@name='efermi']").children.to_s.to_f
end

#num_atomsObject Also known as: num_ions



100
101
102
# File 'lib/vasputils/vasprunxml.rb', line 100

def num_atoms
  @data.xpath("/modeling/atominfo/atoms").children.to_s.to_i
end

#num_spinsObject



105
106
107
# File 'lib/vasputils/vasprunxml.rb', line 105

def num_spins
  @data.xpath("/modeling/parameters/separator[@name='electronic']/separator[@name='electronic spin']/i[@name='ISPIN']").children.to_s.to_i
end

#nums_elementsObject



47
48
49
50
51
# File 'lib/vasputils/vasprunxml.rb', line 47

def nums_elements
  @data.xpath("/modeling/atominfo/array[@name='atomtypes']/set/rc").map do |elem|
    elem.xpath('./c').children[0].text.to_i
  end
end

#partial_dos(ion, spin) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vasputils/vasprunxml.rb', line 81

def partial_dos(ion, spin)
  if (spin < 1) || (num_ions < ion)
    raise IllegalArgumentError, "'ion' is indicated as #{ion}"
  end

  if (spin < 1) || (num_spins < spin)
    raise IllegalArgumentError, "'spin' is indicated as #{spin}"
  end

  result = @data.xpath("/modeling/calculation/dos/partial/array/set/set[@comment='ion #{ion}']/set[@comment='spin #{spin}']/r").children.map do |elem|
    elem.to_s.strip.split.map{|i| i.to_f}
  end
  result
end

#partial_dos_labelsObject



77
78
79
# File 'lib/vasputils/vasprunxml.rb', line 77

def partial_dos_labels
  @data.xpath("/modeling/calculation/dos/partial/array/field").map{|i| i.children.to_s.strip}
end

#positions_listObject



39
40
41
42
43
44
45
# File 'lib/vasputils/vasprunxml.rb', line 39

def positions_list
  @data.xpath("/modeling/calculation").map do |c|
    c.xpath("./structure/varray[@name='positions']/v").map do |pos|
      pos.text.strip.split.map {|i| i.to_f}
    end
  end
end

#stressObject

Return stress tensor of last ionic step



23
24
25
26
27
28
29
# File 'lib/vasputils/vasprunxml.rb', line 23

def stress
  items = @data.xpath("/modeling/calculation/varray[@name='stress']/v").children
  items = items.map do |line|
    line.to_s.strip.split(/ +/).map {|item| item.to_f}
  end
  items[-3..-1]
end

#total_dos(spin) ⇒ Object

Return an array of [energy, total, integrated] for spin. ‘spin’ is indicated by number started from 1 (should be 1 or 2). If the ‘spin’ does not exist, raise IllegalArgumentError



62
63
64
65
66
67
68
69
70
71
# File 'lib/vasputils/vasprunxml.rb', line 62

def total_dos(spin)
  if (spin < 1) || (num_spins < spin)
    raise IllegalArgumentError, "'spin' is indicated as #{spin}"
  end

  result = @data.xpath("/modeling/calculation/dos/total/array/set/set[@comment='spin #{spin}']/r").children.map do |elem|
    elem.to_s.strip.split.map{|i| i.to_f}
  end
  result
end

#total_dos_labelsObject



73
74
75
# File 'lib/vasputils/vasprunxml.rb', line 73

def total_dos_labels
  @data.xpath("/modeling/calculation/dos/total/array/field").map{|i| i.children.to_s}
end