Class: GaussianParser::DataProcessor

Inherits:
Object
  • Object
show all
Includes:
Cli
Defined in:
lib/gaussian_parser/data_processor.rb

Constant Summary

Constants included from Cli

Cli::ERROR_COLOR, Cli::SUCCESS_COLOR

Instance Method Summary collapse

Methods included from Cli

#print_as_error, #print_as_success, #print_as_usual

Constructor Details

#initialize(file) ⇒ DataProcessor

Returns a new instance of DataProcessor.



9
10
11
# File 'lib/gaussian_parser/data_processor.rb', line 9

def initialize(file)
  @file_lines = file.readlines
end

Instance Method Details

#generate_mo_regexp(last_number) ⇒ Object



237
238
239
240
# File 'lib/gaussian_parser/data_processor.rb', line 237

def generate_mo_regexp(last_number)
  str = (1..2).to_a.inject('^') {|memo, e| memo += "\\s+#{last_number + e}"; memo }
  Regexp.new(str)
end

#hartri_to_ev(hartri) ⇒ Object



232
233
234
235
# File 'lib/gaussian_parser/data_processor.rb', line 232

def hartri_to_ev(hartri)
  to_ev = 27.2107
  (hartri.to_f * to_ev).round 6
end

#has_normal_termination?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/gaussian_parser/data_processor.rb', line 13

def has_normal_termination?
  # programm terminates correctly when
  # "normal termination" line is found
  @file_lines.each do |line|
    if line =~ /Normal termination/
      return true
    end
  end
  return false
end

#parseObject



24
25
26
27
28
29
30
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/gaussian_parser/data_processor.rb', line 24

def parse
  processors = {}

  atom_count = {}
  molecular_orbitals = []
  harmonic_frequencies = []
  index = 0

  was_stationary_point = false
  was_standard_orientation = false
  was_mo_coefficients = false

  while index < @file_lines.length
    line = @file_lines[index]
    if line =~ /Stationary point found/ && !was_stationary_point
      print_as_success("Stationary point found")
      was_stationary_point = true
      index += 7
      results = []
      while @file_lines[index] =~ /^\s*!/
        results.push(@file_lines[index])
        index += 1
      end
      processors[:results_processor] = Processors::ResultsProcessor.new(results)
    end

    if line =~ /Standard orientation:/ && was_stationary_point
      unless was_standard_orientation
        print_as_usual("Standard orientation processing")
        was_standard_orientation = true
        index += 5
        atom_data = []
        while @file_lines[index] =~ /^(\s+\d+){3}/
          atom_data.push(@file_lines[index])
          index += 1
        end
        processors[:atom_processor] = Processors::AtomProcessor.new(atom_data)
      end
    end
    
    if line =~ /Molecular Orbital Coefficients/ && was_standard_orientation
      unless was_mo_coefficients
        print_as_usual("Molecular orbital processing")
        was_mo_coefficients = true
        mo_position = index + 1
        current_line = @file_lines[mo_position].split(/\s+/) 
        current_line.delete ""
        last_number = current_line.first.to_i - 1
        mo_regexp = generate_mo_regexp(last_number)
        while mo_position < @file_lines.length
          prev_zayniatist = nil
          if @file_lines[mo_position] =~ mo_regexp &&
            @file_lines[mo_position + 2] =~ /Eigenvalues --/                

            current_line = @file_lines[mo_position].split(/\s+/)
            last_number = current_line.last.to_i
            mo_regexp = generate_mo_regexp(last_number)
            current_line.delete ""
            energy_types = @file_lines[mo_position+=1].split(/\s+/)
            energy_types.delete ""
            energy_values = @file_lines[mo_position+=1].scan(/-*\d+\.*\d+/)
            energy_values.delete ""

            current_line.each_with_index do |elem,i|
              symetry = energy_types[i].split('--').first.scan(/\w/).join
              zayniatist = energy_types[i].split('--').last
              prev_zayniatist ||= zayniatist
              hartri = energy_values[i]
              s = []
              s << elem
              s << symetry
              s << hartri
              s << hartri_to_ev(hartri) 
              s << zayniatist
              s << if prev_zayniatist != zayniatist
                previous = molecular_orbitals.last
                previous.pop()
                previous.push "HOMO"
                molecular_orbitals.pop()
                molecular_orbitals.push(previous) 
                "LUMO"
              else
                ""
              end
              prev_zayniatist = zayniatist
              molecular_orbitals << s
            end
          end
          mo_position += 1
        end
      end
    end
    
    if line =~ /Harmonic frequencies/
      print_as_usual("Harmonic frequencies processing")
      index += 4
      current_line = @file_lines[index].split(/\s+/)
      current_line.delete ""
      last_number = current_line.first.to_i
      hf_regexp = /^\s+#{last_number}/
      while !(@file_lines[index] =~ /Thermochemistry/)
        if @file_lines[index] =~ hf_regexp
          harmonic_frequencies_data = []
          # save index position for return back 
          # if it is wrong line
          saved_position = index + 1
          # save current line for futher analyze
          saved_line = @file_lines[index]
          
          # Begin proccessing
          mode_numbers = @file_lines[index].split(/\s+/)
          mode_numbers.delete ""
          harmonic_frequencies_data << mode_numbers
          
          symmetries = @file_lines[index+=1].split(/\s+/)
          symmetries.delete ""
          harmonic_frequencies_data << symmetries

          poss_frequencies = @file_lines[index+=1].split(/ --\s+/)
          unless poss_frequencies.first =~ /Frequencies/
            index = saved_position
            next
          end
          frequencies = poss_frequencies.last.split(/\s+/)
          harmonic_frequencies_data << frequencies

          poss_red_masses = @file_lines[index+=1].split(/ --\s+/)
          unless poss_red_masses.first =~ /Red. masses/
            index = saved_position
            next
          end
          red_masses = poss_red_masses.last.split(/\s+/)
          harmonic_frequencies_data << red_masses

          poss_frc_consts = @file_lines[index+=1].split(/ --\s+/)
          unless poss_frc_consts.first =~ /Frc consts/
            index = saved_position
            next
          end
          frc_consts = poss_frc_consts.last.split(/\s+/)
          harmonic_frequencies_data << frc_consts

          poss_ir_inten = @file_lines[index+=1].split(/ --\s+/)
          unless poss_ir_inten.first =~ /IR Inten/
            index = saved_position
            next
          end
          ir_inten = poss_ir_inten.last.split(/\s+/)
          harmonic_frequencies_data << ir_inten

          poss_raman_activ = @file_lines[index+=1].split(/ --\s+/)
          unless poss_raman_activ.first =~ /Raman Activ/
            index = saved_position
            next
          end
          raman_activ = poss_raman_activ.last.split(/\s+/)
          harmonic_frequencies_data << raman_activ

          poss_depolar_p = @file_lines[index+=1].split(/ --\s+/)
          unless poss_depolar_p.first =~ /Depolar \(P\)/
            index = saved_position
            next
          end
          depolar_p = poss_depolar_p.last.split(/\s+/)
          harmonic_frequencies_data << depolar_p

          poss_depolar_u = @file_lines[index+=1].split(/ --\s+/)
          unless poss_depolar_u.first =~ /Depolar \(U\)/
            index = saved_position
            next
          end
          depolar_u = poss_depolar_u.last.split(/\s+/)
          harmonic_frequencies_data << depolar_u

          current_harmonic_position = harmonic_frequencies.size
          harmonic_frequencies_data[0].size.times do |i|
            harmonic_frequencies[current_harmonic_position + i] = []
            harmonic_frequencies_data.each do |el|
              harmonic_frequencies[current_harmonic_position + i] << el[i]
            end
          end              
          
          # ex.  1                      2                      3
          # Split line with mode numbers
          current_line = saved_line.split(/\s+/)
          current_line.delete ""
          # Get last mode number for futher search
          last_number = current_line.last.to_i
          # next search for line that consists two neccessary numbers
          # separated with whitespace
          # TODO!
          hf_regexp = /^\s+#{last_number+=1}/
        end
        index += 1
      end
      break
    end

    index += 1
  end 
  return [
    processors[:results_processor].process,
    processors[:atom_processor].process,
    molecular_orbitals,
    harmonic_frequencies
  ]
end