5 # Created by Toshi Nagata on 2013/11/01.
6 # Copyright 2013 Toshi Nagata. All rights reserved.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation version 2 of the License.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
20 # inpname is the input file
21 # mol (optional) is the molecule from which the MOPAC input was built.
22 # Currently not used (mopac6 is not bundled)
23 def Molecule.execute_mopac(inpname, mol = nil)
26 mopexe = "#{ResourcePath}/mopac/mopac606"
28 inpname = File.expand_path(inpname)
29 inpbase = File.basename(inpname)
30 inpdir = File.dirname(inpname)
31 inpbody = inpbase.sub(/\.\w*$/, "")
33 # Prepare the scratch directory in the home directory
34 scrdir = document_home.sub(/\/My Documents/, "") + "/mopac"
36 while File.exist?(scrdir) && !File.directory?(scrdir)
40 scrdir = scrdir.sub(".#{n}", ".#{n + 1}")
44 if !File.exist?(scrdir)
47 scrdir = scrdir + "/" + inpbody + "." + $$.to_s + ".0"
49 while File.exist?(scrdir)
50 scrdir = scrdir.sub(".#{n}", ".#{n + 1}")
55 # Copy the input file to the scratch directory
57 filecopy(inpname, scrdir + "/FOR005")
58 term_callback = lambda { |m, n|
59 if File.exist?("#{scrdir}/FOR006")
60 filecopy("#{scrdir}/FOR006", "#{inpdir}/#{inpbody}.out")
62 msg = "MOPAC execution on #{inpbase} "
69 msg += "failed with status #{n}."
73 msg += "\n(In directory #{inpdir})"
74 message_box(msg, hmsg, :ok, icon)
75 erase_old_logs(scrdir, "latest", 5) # Keep latest 5 logs
82 pid = mol.call_subprocess_async([mopexe], term_callback, timer_callback, "NUL", "NUL")
84 pid = call_subprocess([mopexe], "Running MOPAC", nil, "NUL", "NUL")
85 term_callback.call(nil, pid)
89 error_message_box("MOPAC failed to run.")
96 if !@mopac_settings || (com = @mopac_settings["commands"]) == nil
98 (@mopac_settings ||= Hash.new)["commands"] = com
101 str += sprintf("# %-76.76s\n", self.name)
102 str += sprintf("# %-76.76s\n", "Generated by Molby at #{Time.now.to_s}")
103 # Create geometry input
108 geom += atoms[0].element + "\n"
111 geom += sprintf("%s %.6f 1\n", atoms[1].element, r)
113 con = atoms[2].connects
116 a = calc_angle(2, 0, 1)
120 a = calc_angle(2, 1, 0)
123 geom += sprintf("%s %.6f 1 %.3f 1 %s\n", atoms[2].element, r, a, c)
130 geom += sprintf("%s %.6f 1 %.6f 1 %.6f 1\n", ap.element, ap.x, ap.y, ap.z)
133 return str + geom + "\n"
136 def cmd_create_mopac_input
139 h = Dialog.run("Create MOPAC6 Input") {
141 item(:text, :title=>"MOPAC input"),
142 item(:textview, :width=>480, :height=>200, :tag=>"mopac_commands"),
144 item_with_tag("mopac_commands")[:font] = [:fixed, 10]
145 item_with_tag("mopac_commands")[:value] = mol.create_mopac
147 # Record the command lines for next invocation
148 mop = h["mopac_commands"]
149 com = (mop.split(/[\r\n]/))[0..0].join("\n")
150 (@mopac_settings ||= Hash.new)["commands"] = com
152 basename = (self.path ? File.basename(self.path, ".*") : self.name)
153 fname = Dialog.save_panel("Export MOPAC input file:", self.dir, basename + ".mopin", "MOPAC input file (*.mopin)|*.mopin|All files|*.*")
155 open(fname, "w") { |fp|
162 # if lookup_menu("Create MOPAC6 Input...") < 0
163 # register_menu("", "")
164 # register_menu("Create MOPAC6 Input...", :cmd_create_mopac)