Class: Atom

Inherits:
Object show all
Defined in:
lib/lib/atom/atom.rb,
lib/lib/atom/lib/atom.rb,
lib/lib/atom/lib/elements.rb

Constant Summary collapse

Elements =
{
  :O => {
    :z => 8,
    :m => 15.999,
    :fname => "Oxygen",
    :name => :O,
    :pp => []
  },
  :Ti => {
    :z => 22,
    :m => 47.867,
    :fname => "Titanium",
    :name => :Ti,
    :pp => []
  },
  :X => {
    :z => 1,
    :m => 1,
    :fname => "Unspecified",
    :name => :X,
    :pp => []
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Atom

Returns a new instance of Atom.



5
6
7
8
9
10
11
# File 'lib/lib/atom/atom.rb', line 5

def initialize params = {}
  @type = Elements[params[:type]] || Elements[:X]
  @name = (params[:name]||@type.name).to_sym
  @pos = (params[:pos].to_v rescue nil)
  @crystal = params[:crystal]
  @fixed = params[:fixed]
end

Instance Attribute Details

#crystalObject

Returns the value of attribute crystal.



4
5
6
# File 'lib/lib/atom/atom.rb', line 4

def crystal
  @crystal
end

#fixedObject

Returns the value of attribute fixed.



4
5
6
# File 'lib/lib/atom/atom.rb', line 4

def fixed
  @fixed
end

#posObject

Returns the value of attribute pos.



4
5
6
# File 'lib/lib/atom/atom.rb', line 4

def pos
  @pos
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/lib/atom/atom.rb', line 4

def type
  @type
end

Instance Method Details

#dist(other) ⇒ Object



23
24
25
26
# File 'lib/lib/atom/lib/atom.rb', line 23

def dist other
  pos = other.is_a?(Atom) ? other.pos : other.to_v
  has_crystal? ? crystal.dist(self, other):(@pos - pos).magnitude
end

#dupObject



27
28
29
# File 'lib/lib/atom/lib/atom.rb', line 27

def dup
  Atom.new(to_hash)
end

#evaluate(str, b = nil) ⇒ Object



30
31
32
33
# File 'lib/lib/atom/atom.rb', line 30

def evaluate str, b=nil
  str = str.gsub(/={1,2}/,"==").gsub(/(\[[^\]]*\])/,'\1.to_v')
  eval(str) ? true : false
end

#has_crystal?Boolean

Returns:

  • (Boolean)


14
# File 'lib/lib/atom/lib/atom.rb', line 14

def has_crystal?() !(@crystal.nil?) end

#inspectObject



36
37
38
# File 'lib/lib/atom/lib/atom.rb', line 36

def inspect
  to_hash.to_s
end

#move(v, n = 1) ⇒ Object



19
20
21
22
# File 'lib/lib/atom/lib/atom.rb', line 19

def move v, n = 1
  ret = self.dup
  ret.move! v,n
end

#move!(v, n = 1) ⇒ Object



15
16
17
18
# File 'lib/lib/atom/lib/atom.rb', line 15

def move! v, n = 1
  @pos += v.to_v * n
  self
end

#nameObject



16
# File 'lib/lib/atom/atom.rb', line 16

def name() @name.to_s end

#name=(s) ⇒ Object



17
18
19
# File 'lib/lib/atom/atom.rb', line 17

def name=(s)
  @name = s.to_sym
end

#to_hashObject



30
31
32
# File 'lib/lib/atom/lib/atom.rb', line 30

def to_hash
  { :type => @type.name.to_sym, :name => @name, :pos => @pos, :fixed => @fixed, :crystal => @crystal }
end

#to_s(f = '%8.4f') ⇒ Object



33
34
35
# File 'lib/lib/atom/lib/atom.rb', line 33

def to_s f = '%8.4f'
  ("%-6s" + "#{f} " * 3) % ([name] + pos.to_a) + (fixed.nil? ? "" : fixed.map{|v| v ? 0 : 1}.join(" "))
end

#xObject



2
# File 'lib/lib/atom/lib/atom.rb', line 2

def x() @pos[0] end

#x=(p) ⇒ Object



5
6
7
# File 'lib/lib/atom/lib/atom.rb', line 5

def x=(p) 
  @pos = Vector[p,y,z] 
end

#yObject



3
# File 'lib/lib/atom/lib/atom.rb', line 3

def y() @pos[1] end

#y=(p) ⇒ Object



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

def y=(p) 
  @pos = Vector[x,p,z] 
end

#zObject



4
# File 'lib/lib/atom/lib/atom.rb', line 4

def z() @pos[2] end

#z=(p) ⇒ Object



11
12
13
# File 'lib/lib/atom/lib/atom.rb', line 11

def z=(p) 
  @pos = Vector[x,y,p] 
end