4 # Created by Toshi Nagata on 2009/11/22.
5 # Copyright 2009 Toshi Nagata. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation version 2 of the License.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
18 def Molecule.read_gamess_basis_sets(fname)
19 $gamess_basis = Hash.new unless $gamess_basis
20 $gamess_ecp = Hash.new unless $gamess_ecp
21 basename = File.basename(fname, ".*")
23 File.open(fname, "r") { |fp|
25 ss, bas = (s.split)[0..1] # Tokens delimited by whitespaces
26 next if ss == nil || ss == ""
28 # Read the ECP section
29 ecp = $gamess_ecp[basename]
32 $gamess_ecp[basename] = ecp
34 keys << basename unless keys.include?(basename)
37 ary = s.split # (PNAME, PTYPE, IZCORE, LMAX+1)
38 elem = ary[0].match(/\w{1,2}/).to_a[0] # ary[0] should begin with an element name
39 ap = Parameter.atoms.find { |p| p.name.casecmp(elem) == 0 }
40 raise MolbyError, "the ECP definition does not begin with an element name: #{s}" if !ap
43 (0..Integer(ary[3])).each {
45 raise MolbyError, "the ECP definition ends unexpectedly at line #{ln} for: #{s}" if !s
47 ary = s.split # (NGPOT, comment)
49 (1..Integer(ary[0])).each {
51 raise MolbyError, "the ECP definition ends unexpectedly at line #{ln} for: #{s}" if !s
56 ecp[ap.index] = ecpdef
59 # Comments or other unrecognizable lines
61 elsif (ap = Parameter.atoms.find { |p| p.name.casecmp(ss) == 0 || p.fullname.casecmp(ss) == 0 })
62 # Valid basis definition
63 if bas == nil || bas =~ /\W/
66 basis = $gamess_basis[bas]
69 $gamess_basis[bas] = basis
71 keys << bas unless keys.include?(bas)
73 while (s = fp.gets) && s =~ /\S/
76 basis[ap.index] = basdef
78 raise MolbyError, "ss is not a valid element symbol or name: #{s}"
84 def export_gamess(fname, hash)
87 basename = File.basename(fname, ".*")
90 icharg = hash["charge"]
92 runtyp = ["ENERGY", "PROP", "OPTIMIZE"][Integer(hash["runtype"])]
93 scftyp = ["RHF", "ROHF", "UHF"][Integer(hash["scftype"])]
94 bssname = hash["basis"]
95 bssname2 = hash["secondary_basis"]
96 if hash["use_secondary_basis"] != 0 && bssname2 != bssname
98 element2 = hash["secondary_elements"].split(/[\s,]+/).map { |name| name.capitalize }
103 basis = $gamess_basis[bssname]
104 basis2 = $gamess_basis[bssname2]
106 raise MolbyError, "Unknown basis set name??? \"#{bssname}\" or \"#{bssname2}\""
109 # Use effective core potentials?
110 ecp = $gamess_ecp[bssname]
111 ecp2 = $gamess_ecp[bssname2]
112 ecp_read = (ecp || ecp2 ? "ECP=READ " : "")
114 # Use only one built-in basis set?
121 gbasis = "STO NGAUSS=3"
123 gbasis = "N21 NGAUSS=3"
125 gbasis = "N31 NGAUSS=6"
127 gbasis = "N31 NGAUSS=6 NDFUNC=1"
129 gbasis = "N31 NGAUSS=6 NDFUNC=1 NPFUNC=1"
131 gbasis = "N311 NGAUSS=6"
133 gbasis = "N311 NGAUSS=6 NDFUNC=1 NPFUNC=1"
137 File.open(fname, "wb") { |fp|
138 fp.print "! GAMESS input\n"
139 fp.print "! Generated by Molby at #{now}\n"
140 fp.print " $CONTRL COORD=UNIQUE EXETYP=RUN ICHARG=#{icharg}\n"
141 fp.print " ICUT=20 INTTYP=HONDO ITOL=30\n"
142 fp.print " MAXIT=200 MOLPLT=.T. MPLEVL=0\n"
143 fp.print " MULT=#{mult} QMTTOL=1e-08 RUNTYP=#{runtyp}\n"
145 dfttyp = hash["dfttype"]
146 fp.print " DFTTYP=#{dfttyp}\n"
148 fp.print " SCFTYP=#{scftyp} #{ecp_read}UNITS=ANGS $END\n"
149 fp.print " $SCF CONV=1.0E-06 DIRSCF=.T. FDIFF=.T. DAMP=.T. $END\n"
150 fp.print " $STATPT NSTEP=400 OPTTOL=1.0E-06 $END\n"
151 fp.print " $SYSTEM MEMDDI=0 MWORDS=16 TIMLIM=50000 $END\n"
152 fp.print " $GUESS GUESS=HUCKEL $END\n"
154 fp.print " $BASIS GBASIS=#{gbasis} $END\n"
157 fp.print " $ELPOT IEPOT=1 OUTPUT=PUNCH WHERE=PDC $END\n"
158 fp.print " $PDC CONSTR=NONE PTSEL=CONNOLLY $END\n"
160 fp.print " $DATA\n#{basename}\nC1 0\n"
163 fp.printf "%-6s %4d %10.6f %10.6f %10.6f\n", ap.name, ap.atomic_number, ap.r.x, ap.r.y, ap.r.z
164 if use_2nd && element2.include?(ap.element)
165 secondary[ap.index] = true
168 # Basis specification followed by a blank line
169 bas = (secondary[ap.index] ? basis2 : basis)
171 bas = bas[ap.atomic_number]
174 raise MolbyError, "Basis set is not defined for atom #{ap.index}, element #{ap.element}"
185 an = ap.atomic_number
186 ecpp = (secondary[ap.index] ? ecp2 : ecp)
187 e = ecp_ary[an] || (ecpp && ecpp[an])
189 # Cache the PNAME of the $ECP entry and re-use it
190 ecp_ary[an] ||= (e.split)[0] + "\n"
192 e = ap.element.upcase + "-ECP NONE\n"
202 def cmd_create_gamess_input
204 raise MolbyError, "cannot create GAMESS input; the molecule is empty"
207 # Descriptive text and internal string for popup menus
208 bset_desc = ["PM3", "STO-3G", "3-21G", "6-31G", "6-31G(d)", "6-31G(d,p)", "6-311G", "6-311G(d,p)", "LanL2DZ"]
209 bset_internal = ["PM3", "STO3G", "321G", "631G", "631Gd", "631Gdp", "6311G", "6311Gdp", "LanL2DZ"]
211 dft_internal = ["B3LYP"]
213 defaults = {"scftype"=>0, "runtype"=>0, "charge"=>"0", "mult"=>"1",
214 "basis"=>4, "use_secondary_basis"=>0, "secondary_elements"=>"",
215 "secondary_basis"=>8, "esp"=>0}
217 hash = RubyDialog.run("GAMESS Export") {
223 set_global_settings("gamess.#{tag}", attr(i, :value))
230 item(:text, :title=>"SCF type"),
231 item(:popup, :subitems=>["RHF", "ROHF", "UHF"], :tag=>"scftype"),
232 item(:text, :title=>"Run type"),
233 item(:popup, :subitems=>["Energy", "Property", "Optimize"], :tag=>"runtype"),
235 item(:text, :title=>"Charge"),
236 item(:textfield, :width=>80, :tag=>"charge"),
237 item(:text, :title=>"Multiplicity"),
238 item(:textfield, :width=>80, :tag=>"mult"),
240 item(:checkbox, :title=>"Use DFT", :tag=>"dft",
241 :action=>proc { |n| set_attr("dfttype", :enabled=>(attr(n, :value) != 0)) } ),
243 item(:text, :title=>"DFT type"),
244 item(:popup, :subitems=>dft_desc, :tag=>"dfttype"),
249 item(:text, :title=>"Basis set"),
250 item(:popup, :subitems=>bset_desc, :tag=>"basis"),
253 item(:checkbox, :title=>"Use secondary basis set", :tag=>"use_secondary_basis",
255 flag = (attr(n, :value) != 0)
256 set_attr("secondary_elements", :enabled=>flag)
257 set_attr("secondary_basis", :enabled=>flag)
261 item(:text, :title=>" Elements"),
262 item(:textfield, :width=>80, :tag=>"secondary_elements"),
263 item(:text, :title=>"Basis set"),
264 item(:popup, :subitems=>bset_desc, :tag=>"secondary_basis"),
269 item(:checkbox, :title=>"Calculate electrostatic potential (ESP)", :tag=>"esp"),
279 values[tag] = (get_global_settings("gamess.#{tag}") || defaults[tag])
280 set_attr(i, :value=>values[tag])
283 set_attr("secondary_elements", :enabled=>(values["use_secondary_basis"] == 1))
284 set_attr("secondary_basis", :enabled=>(values["use_secondary_basis"] == 1))
285 set_attr("dfttype", :enabled=>(values["dft"] == 1))
288 # Specify basis by internal keys
289 hash["basis"] = bset_internal[hash["basis"]]
290 hash["secondary_basis"] = bset_internal[hash["secondary_basis"]]
291 hash["dfttype"] = dft_internal[hash["dfttype"]]
292 basename = (self.path ? File.basename(self.path, ".*") : self.name)
293 fname = RubyDialog.save_panel("GAMESS input file name", self.dir, basename + ".inp", "GAMESS input file (*.inp)|*.inp|All files|*.*")
295 export_gamess(fname, hash)
303 Molecule.read_gamess_basis_sets("LanL2DZ.txt")
304 Molecule.read_gamess_basis_sets("631Gd.txt")
305 Molecule.read_gamess_basis_sets("631Gdp.txt")
306 Molecule.read_gamess_basis_sets("6311Gdp.txt")
307 $gamess_basis["PM3"] = " PM3 0\n"
308 $gamess_basis["STO3G"] = " STO 3\n"
309 $gamess_basis["321G"] = " N21 3\n"
310 $gamess_basis["631G"] = " N31 6\n"