Class: AimsProject::GeometryFile

Inherits:
Object
  • Object
show all
Includes:
Aims, Observable
Defined in:
lib/aims_project/geometry_file.rb

Overview

The geometry model utilized by this application

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, _binding = nil) ⇒ GeometryFile

Parse a geometry input Evaluate any embedded ruby using the given binding

Parameters:

  • input (defaults to: nil)

    If input is a File, the file is read and evaluated with ERB If input is a String, the input is directly evaluated with ERB

  • _binding (defaults to: nil)

    The binding to use when evaluating with EB



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
# File 'lib/aims_project/geometry_file.rb', line 46

def initialize(input = nil, _binding=nil)
  
  if input.nil?
    @raw_input = ""
    @input_geometry = ""
    return
  end
  
  if input.respond_to? :read
    @file = input
    @raw_input = @file.read
  elsif input.is_a? String
    @raw_input = input
  elsif input.is_a? Aims::Geometry
    @aims_geometry = input
    @input_geometry = @aims_geometry.format_geometry_in
    @raw_input = @input_geometry
  end
  
  begin
    # Attempt to evaluate the raw input, but don't require it.
    @binding = _binding
    evaluate(@binding)
  rescue
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Delegate calls to the Aims::Geometry object if it exists.



98
99
100
101
102
103
104
# File 'lib/aims_project/geometry_file.rb', line 98

def method_missing(symbol, *args, &block)
  if @aims_geometry.nil?
    raise GeometryEvaluationException.new
  else
    @aims_geometry.send(symbol, *args, &block)
  end
end

Instance Attribute Details

#aims_geometryObject (readonly)

The Aims::Geometry object



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

def aims_geometry
  @aims_geometry
end

#fileObject (readonly)

The filename of the geometry file



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

def file
  @file
end

#input_geometryObject (readonly)

The string representation of the parsed input geometry



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

def input_geometry
  @input_geometry
end

#raw_inputObject

The raw unevaluated input.



39
40
41
# File 'lib/aims_project/geometry_file.rb', line 39

def raw_input
  @raw_input
end

Class Method Details

.eval_geometry(str, _binding = nil) ⇒ Object

Read and parse a geometry file. Use the given binding to evaluate any embedded ruby



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

def GeometryFile.eval_geometry(str, _binding=nil)

  b = if _binding
    _binding
  else
    binding()
  end

  erb = ERB.new(str)
  erb.result(b)

end

Instance Method Details

#evaluate(_binding = nil) ⇒ Object

Evaluate the raw input and return a geometry String formatted in the Aims geometry.in format



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

def evaluate(_binding=nil)
  if _binding
    @binding = _binding
  end
  
  @input_geometry = GeometryFile.eval_geometry(@raw_input, @binding)
  @aims_geometry = GeometryParser.parse_string(@input_geometry) 
  
  changed
  notify_observers
  
  @aims_geometry
end

#is_valid?Boolean

Check the consistency between the raw, the evaluated and the object-space geometries

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
# File 'lib/aims_project/geometry_file.rb', line 107

def is_valid?
  begin
    validate
    return true
  rescue Exception => e
    return false
  end
end

#saveObject

Save this geometry raises an InvalidFilenameException unless @file is not nil

Returns:

  • self



154
155
156
# File 'lib/aims_project/geometry_file.rb', line 154

def save
  save_as(@file)
end

#save_as(file) ⇒ Object

Save the raw input to file if it passes validation raises an InvalidFilenameException unless @file is not nil raises a GeometryValidationException if it fails to pass Validation

Returns:

  • A new GeometryFile object representing the geometry in the newly created file or this GeometryFile object if file = nil



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/aims_project/geometry_file.rb', line 163

def save_as(file)

    if file.nil?
      raise InvalidFilenameException(nil) unless @file
      file = @file
    end
    
    # validate
    
    File.open(file, 'w') {|f|
      f.write @raw_input
    }
    
    return self
    
end

#validateObject

Check the consistency of all internal data models

Returns:

  • true, otherwise raises a GeoemtryValidationException



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/aims_project/geometry_file.rb', line 118

def validate
    # The raw input evaluated must match the input_geometry
    erb = ERB.new(@raw_input)
    res = erb.result(@binding)
    unless  res == @input_geometry
      raise GeometryValidationException.new("raw input doesn't match evaluated input")
    end
  
    # Also need to somehow validate against the Aims::Geometry
    g = GeometryParser.parse_string(res)
    unless g.atoms.size == @aims_geometry.atoms.size
      raise GeometryValidationException("input geometry atom count doesn't match aims_geometry")
    end
    
    unless g.lattice_vectors.size == @aims_geometry.lattice_vectors.size
      raise GeometryValidationException("input geometry lattice vector count doesn't match aims_geometry")
    end
  
    g.atoms.each{|a| 
      unless @aims_geometry.atoms.member?(a)
        raise GeometryValidationException("atom mismatch")
      end
    }
    
    g.lattice_vectors.each{|v| 
      unless @aims_geometry.lattice_vectors.member?(v)
        raise GeometryValidationException("lattice vector mismatch")
      end
    }

    return true
end