OSDN Git Service

7f5a87e26820bc2513f834fe696ef66dd434f78c
[molby/Molby.git] / Scripts / mopac6.rb
1 #
2 #  mopac6.rb
3 #
4 #  Created by Toshi Nagata on 2013/11/01.
5 #  Copyright 2013 Toshi Nagata. All rights reserved.
6 #
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.
10
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.
15
16 class Molecule
17
18   #  Execute mopac6 (inferior copy of rungms script)
19   #  inpname is the input file
20   #  mol (optional) is the molecule from which the MOPAC input was built.
21   def Molecule.execute_mopac(inpname, mol = nil)
22
23     #  MOPAC executable
24     mopexe = "#{ResourcePath}/mopac/mopac606"
25
26     inpbase = File.basename(inpname)
27     inpdir = File.dirname(inpname)
28     inpbody = inpbase.sub(/\.\w*$/, "")
29
30     #  Prepare the scratch directory in the home directory
31     scrdir = document_home.sub(/\/My Documents/, "") + "/mopac"
32     n = 0
33     while File.exist?(scrdir) && !File.directory?(scrdir)
34       if n == 0
35         scrdir += ".1"
36       else
37         scrdir = scrdir.sub(".#{n}", ".#{n + 1}")
38       end
39       n += 1
40     end
41     if !File.exist?(scrdir)
42       Dir.mkdir(scrdir)
43     end
44     scrdir = scrdir + "/" + inpbody + "." + $$.to_s + ".0"
45     n = 0
46     while File.exist?(scrdir)
47       scrdir = scrdir.sub(".#{n}", ".#{n + 1}")
48       n += 1
49     end
50     Dir.mkdir(scrdir)
51
52         #  Copy the input file to the scratch directory
53         cwd = Dir.pwd
54         filecopy(inpname, scrdir + "/FOR005")
55         term_callback = proc { |m, n|
56             if n == 0
57                     filecopy("#{scrdir}/FOR006", "#{cwd}/#{inpdir}/#{inpbody}.out");
58             end
59                 Dir.foreach(scrdir) { |fn|
60                         next if fn == "." || fn == ".."
61                     File.delete("#{scrdir}/#{fn}")
62                 }
63                 Dir.chdir(cwd)
64                 Dir.rmdir(scrdir)
65         }
66         timer_callback = nil
67         
68         #  Execute mopac
69         Dir.chdir(scrdir)
70         if mol
71           pid = mol.call_subprocess_async(mopexe, term_callback, timer_callback, "NUL", "NUL")
72         else
73           pid = call_subprocess(mopexe, "Running MOPAC", nil, "NUL", "NUL")
74           term_callback.call(nil, pid)
75         end
76         Dir.chdir(cwd)
77         if pid < 0
78           error_message_box("MOPAC failed to run.")
79           return
80         end
81
82   end
83     
84 end
85
86 Molecule.execute_mopac("test01.inp", nil)