OSDN Git Service

Start implementing JANPA integration with Psi4
[molby/Molby.git] / Scripts / gamess.rb
index 560082a..66978ab 100755 (executable)
@@ -1553,6 +1553,7 @@ class Molecule
       fp.print "#  Generated by Molby at #{now}\n"
       fp.print "import sys\n"
       fp.print "import re\n"
+      fp.print "base = re.sub('\\.\\w*$', '', sys.argv[1])  #  Basename of the input file\n"
       fp.print "molecule mol {\n"
       charge = Integer(hash["charge"])
       mult = Integer(hash["mult"])
@@ -1584,7 +1585,17 @@ class Molecule
       end
       runtype = hash['runtype'].downcase
       fp.print "energy, wfn = #{runtype}('scf', #{options})\n"
-      fp.print "wfn.write_molden(re.sub('\\.\\w*$', '.molden', sys.argv[1]))\n"
+      fp.print "wfn.write_molden(f'{base}.molden')\n"
+      if hash['run_junpa'] != 0
+        fp.print "\n"
+        fp.print "#  Interface for JANPA\n"
+        fp.print "#  cf. https://sourceforge.net/p/janpa/wiki/psi4Examples/\n"
+        fp.print "d = wfn.Da().to_array()\n"
+        fp.print "s = wfn.S().to_array()\n"
+        fp.print "c = wfn.Ca().to_array()\n"
+        fp.print "occs = c.T.dot(s.dot(d).dot(s).dot(c))\n"
+        fp.print "molden(wfn, f'{base}.da.molden', density_a = psi4.core.Matrix.from_array(occs))\n"
+      end
     end
     if fname == nil
       s = fp.buffer
@@ -1596,6 +1607,42 @@ class Molecule
     end
   end
 
+  #  Execute JUNPA
+  #  inppath is the input file minus extention
+  #  mol is the molecule (may be nil)
+  def Molecule.execute_janpa(inppath, mol, spherical)
+    #    nbo_desc =   #  JANPA
+    janpa_dir = "#{ResourcePath}/JANPA"
+    status = 0
+    if spherical
+      cmd1 = "java -jar #{janpa_dir}/molden2molden.jar -NormalizeBF -i #{inppath}.da.molden -o #{inppath}.in.molden >#{inppath}.janpa.log"
+    else
+    cmd1 = "java -jar #{janpa_dir}/molden2molden.jar -frompsi4v1mo -NormalizeBF -cart2pure -i #{inppath}.da.molden -o #{inppath}.in.molden >#{inppath}.janpa.log"
+    end
+    flag = system(cmd1)
+    if flag
+      cmd2 = "java -jar #{janpa_dir}/janpa.jar -i #{inppath}.in.molden"
+      ["nao", "pnao", "aho", "lho", "lpo", "clpo"].each { |type|
+        if (get_global_settings("psi4.#{type}").to_i != 0)
+          cmd2 += " -#{type.upcase}_Molden_File #{inppath}.#{type.upcase}.molden"
+        end
+      }
+      cmd2 += " >>#{inppath}.janpa.log"
+      flag = system(cmd2)
+    end
+    if flag
+      if mol
+        #  import JANPA log and molden output
+        #  Files: inppath.japa.log, inppath.{NAO,PNAO,AHO,LHO,LPO,CLPO}.molden
+        mol.sub_load_janpa_log(inppath)
+      end
+    else
+      status = $?.exitstatus
+      message_box("Execution of #{procname} failed with status #{status}.", "JANPA Failed")
+    end
+    return status
+  end
+  
   #  Execute Psi4
   #  inpname is the input file
   #  mol (optional) is the molecule from which the Psi4 input was built.
@@ -1623,6 +1670,7 @@ class Molecule
     hf_type = nil
     nalpha = nil
     nbeta = nil
+    spherical = false
 
     outfile = inpdir + "/" + inpbody + ".out"
     if File.exists?(outfile)
@@ -1731,6 +1779,8 @@ class Molecule
             nalpha = Integer($1)
           elsif line =~ /^ *Nbeta *= *(\d+)/
             nbeta = Integer($1)
+          elsif line =~ /^ *Spherical Harmonics\?: *(\w+)/
+            spherical = ($1 == "true")
           end
         end
         if next_index > 0
@@ -1748,6 +1798,7 @@ class Molecule
 
     #  Terminate callback
     term_callback = lambda { |m, n|
+      do_janpa = false
       begin
         msg = "Psi4 execution of #{inpbase} "
         hmsg = "Psi4 "
@@ -1793,6 +1844,9 @@ class Molecule
               $stderr.write("#{e.backtrace.inspect}\n")
             end
           end
+          if (get_global_settings("psi4.run_janpa").to_i == 1)
+            do_janpa = true
+          end
         elsif n == -1
           #  The child process actually did not start
           #  Restore the old file if outbackfile is not nil
@@ -1803,6 +1857,9 @@ class Molecule
         if outbackfile && File.exists?(outbackfile)
           File.delete(outbackfile)
         end
+        if do_janpa
+          Molecule.execute_janpa(inpdir + "/" + inpbody, mol, spherical)
+        end
       rescue => e
         $stderr.write("#{e.message}\n")
         $stderr.write("#{e.backtrace.inspect}\n")
@@ -1853,7 +1910,7 @@ class Molecule
     dfttype_desc = ["B3LYP"]
     runtype_desc = ["Energy", "Optimize"]
     scftype_desc = ["RHF", "ROHF", "UHF"]
-#   nbo_desc = ["nao", "nbo", "nho", "nlmo", "pnao", "pnbo", "pnho", "pnlmo"]
+    nbo_desc = ["nao", "pnao", "aho", "lho", "lpo", "clpo"]  #  JANPA
     user_input = Hash.new
     defaults = {"scftype"=>0, "runtype"=>0, "move_to_com"=>0, "do_reorient"=>0,
       "use_symmetry"=>0,  "charge"=>"0", "mult"=>"1",
@@ -1956,22 +2013,21 @@ class Molecule
         #item(:line),
         #-1, -1, -1,
         #  ------
-        #item(:checkbox, :title=>"Include NBO instructions", :tag=>"include_nbo",
-        #    :action=>lambda { |it|
-        #      flag = (it[:value] != 0)
-        #      nbos.each { |nbo| set_attr(nbo, :enabled=>flag) }
-        #    }),
-        #-1, -1, -1,
+        item(:checkbox, :title=>"Run JANPA after Psi4", :tag=>"run_janpa",
+            :action=>lambda { |it|
+              flag = (it[:value] != 0)
+              nbo_desc.each { |nbo| set_attr(nbo, :enabled=>flag) }
+            }),
+        -1, -1, -1,
         #  ------
-        #item(:checkbox, :title=>"NAO", :tag=>"nao"),
-        #item(:checkbox, :title=>"NBO", :tag=>"nbo"),
-        #item(:checkbox, :title=>"NHO", :tag=>"nho"),
-        #item(:checkbox, :title=>"NLMO", :tag=>"nlmo"),
+        item(:checkbox, :title=>"NAO", :tag=>"nao"),
+        item(:checkbox, :title=>"PNAO", :tag=>"pnao"),
+        item(:checkbox, :title=>"AHO", :tag=>"aho"),
+        item(:checkbox, :title=>"LHO", :tag=>"lho"),
         #  ------
-        #item(:checkbox, :title=>"PNAO", :tag=>"pnao"),
-        #item(:checkbox, :title=>"PNBO", :tag=>"pnbo"),
-        #item(:checkbox, :title=>"PNHO", :tag=>"pnho"),
-        #item(:checkbox, :title=>"PNLMO", :tag=>"pnlmo"),
+        item(:checkbox, :title=>"LPO", :tag=>"lpo"),
+        item(:checkbox, :title=>"CLPO", :tag=>"clpo"),
+        -1, -1,
         #  ------
         item(:line),
         -1, -1, -1,