github.com/rmera/gochem@v0.7.1/qm/qm_test.go (about)

     1  /*
     2   * qm_test.go, part of gochem.
     3   *
     4   *
     5   * Copyright 2012 Raul Mera <rmera{at}chemDOThelsinkiDOTfi>
     6   *
     7   * This program is free software; you can redistribute it and/or modify
     8   * it under the terms of the GNU Lesser General Public License as
     9   * published by the Free Software Foundation; either version 2.1 of the
    10   * License, or (at your option) any later version.
    11   *
    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.
    16   *
    17   * You should have received a copy of the GNU Lesser General
    18   * Public License along with this program.  If not, see
    19   * <http://www.gnu.org/licenses/>.
    20   *
    21   *
    22   * Gochem is developed at the laboratory for instruction in Swedish, Department of Chemistry,
    23   * University of Helsinki, Finland.
    24   *
    25   *
    26   */
    27  
    28  package qm
    29  
    30  import (
    31  	"fmt"
    32  	"os"
    33  	"strings"
    34  	"testing"
    35  
    36  	chem "github.com/rmera/gochem"
    37  	v3 "github.com/rmera/gochem/v3"
    38  )
    39  
    40  //TestQM tests the QM functionality. It prepares input for ORCA and MOPAC
    41  //In the case of MOPAC it reads a previously prepared output and gets the energy.
    42  func TestQM(Te *testing.T) {
    43  	mol, err := chem.XYZFileRead("../test/water.xyz")
    44  	if err != nil {
    45  		Te.Error(err)
    46  
    47  	}
    48  	if err := mol.Corrupted(); err != nil {
    49  		Te.Error(err)
    50  	}
    51  	fmt.Println(mol.Coords[0], len(mol.Coords), "LOS JUIMOS CTM", err)
    52  	//	mol.Del(mol.Len() - 1)
    53  	mol.SetCharge(0)
    54  	mol.SetMulti(1)
    55  	calc := new(Calc)
    56  	calc.SetDefaults()
    57  	calc.SCFTightness = 2 //very demanding
    58  	calc.Job = &Job{Opti: true}
    59  	//calc.Job.Opti=true
    60  	calc.Method = "TPSS"
    61  	calc.Dielectric = 4
    62  	calc.Basis = "def2-SVP"
    63  	calc.HighBasis = "def2-TZVP"
    64  	calc.Grid = 4
    65  	calc.Memory = 1000
    66  	//	calc.HBAtoms = []int{3, 10, 12}
    67  	//	calc.HBElements = []string{"Cu", "Zn"}
    68  	calc.CConstraints = []int{0, 2}
    69  	calc.OldMO = true
    70  	orca := NewOrcaHandle()
    71  	orca.SetnCPU(1) /////////////////////
    72  	orca.SetWorkDir("orca")
    73  	atoms := mol.Coords[0] //v3.Zeros(mol.Len())
    74  	//	mol.Next(atoms)
    75  	original_dir, _ := os.Getwd() //will check in a few lines
    76  	if err = os.Chdir("../test"); err != nil {
    77  		Te.Error(err)
    78  	}
    79  	_ = orca.BuildInput(atoms, mol, calc)
    80  	//Now anothertest with HF-3c
    81  	calc.HBAtoms = nil
    82  	calc.HBElements = nil
    83  	calc.RI = false
    84  	calc.Grid = -1
    85  	calc.Dielectric = 0
    86  	calc.Method = "HF-3c"
    87  	orca.SetName("HF3c")
    88  	orca.SetnCPU(8)
    89  	//	fmt.Println(mol.Coords[0], "vieja")
    90  	_ = orca.BuildInput(atoms, mol, calc)
    91  	path, _ := os.Getwd()
    92  	//	if err:=orca.Run(false); err!=nil{
    93  	//			Te.Error(err.Error())
    94  	//		}
    95  	fmt.Println(path)
    96  	//Now a MOPAC optimization with the same configuration.
    97  	mopac := NewMopacHandle()
    98  	mopac.SetWorkDir("mopac")
    99  	mopac.BuildInput(atoms, mol, calc)
   100  	mopaccommand := os.Getenv("MOPAC_LICENSE") + "/MOPAC2016.exe"
   101  	mopac.SetCommand(mopaccommand)
   102  	fmt.Println("command", mopaccommand)
   103  	/*
   104  		if err := mopac.Run(true); err != nil {
   105  			if strings.Contains(err.Error(), "no such file") {
   106  				fmt.Println("Error", err.Error(), (" Will continue."))
   107  			} else {
   108  				Te.Error(err.Error())
   109  			}
   110  		}
   111  		energy, err := mopac.Energy()
   112  		if err != nil {
   113  			if err.Error() == "Probable problem in calculation" {
   114  				fmt.Println(err.Error())
   115  			} else {
   116  				Te.Error(err)
   117  			}
   118  		}
   119  		geometry, err := mopac.OptimizedGeometry(mol)
   120  		if err != nil {
   121  			if err.Error() == "Probable problem in calculation" {
   122  				fmt.Println(err.Error())
   123  			} else {
   124  				Te.Error(err)
   125  			}
   126  		}
   127  		mol.Coords[0] = geometry
   128  		fmt.Println(energy)
   129  		if err := chem.XYZFileWrite("mopac.xyz", mol.Coords[0], mol); err != nil {
   130  			Te.Error(err)
   131  		}
   132  		//Took away this because it takes too long to run :-)
   133  		/*	if err=orca.Run(true); err!=nil{
   134  			Te.Error(err)
   135  			}
   136  	*/
   137  	if err = os.Chdir(original_dir); err != nil {
   138  		Te.Error(err)
   139  	}
   140  	fmt.Println("end mopac and orca test!")
   141  }
   142  
   143  //TestTurbo tests the QM functionality. It prepares input for Turbomole
   144  //Notice that 2 TM inputs cannot be in the same directory. Notice that TMHandle
   145  //supports ECPs
   146  func TestTurbo(Te *testing.T) {
   147  	fmt.Println("Turbomole TEST y wea!")
   148  	mol, err := chem.XYZFileRead("../test/ethanol.xyz")
   149  	original_dir, _ := os.Getwd() //will check in a few lines
   150  	os.Chdir("../test")
   151  	defer os.Chdir(original_dir)
   152  	if err != nil {
   153  		Te.Error(err)
   154  	}
   155  	if err := mol.Corrupted(); err != nil {
   156  		Te.Error(err)
   157  	}
   158  	mol.SetCharge(0)
   159  	mol.SetMulti(1)
   160  	calc := new(Calc)
   161  	calc.CartesianOpt = true
   162  	calc.SCFConvHelp = 1 //very demanding
   163  	calc.Memory = 1000
   164  	//Not advised
   165  	//	calc.ECP = "ecp-10-mdf"
   166  	//	calc.ECPElements = []string{"O"}
   167  	calc.Grid = 4
   168  	calc.Job = &Job{Opti: true}
   169  	calc.Method = "BP86"
   170  	calc.Dielectric = 4
   171  	calc.Basis = "def2-SVP"
   172  	calc.HighBasis = "def2-TZVP"
   173  	calc.HBElements = []string{"C"}
   174  	calc.RI = true
   175  	calc.Dispersion = "D3"
   176  	calc.CConstraints = []int{0, 3}
   177  	tm := NewTMHandle()
   178  	atoms := mol.Coords[0]
   179  	//if err = os.Chdir("./test"); err != nil {
   180  	//	Te.Error(err)
   181  	//}
   182  	//	tm.SetDryRun(true) //I don't have TM installed.
   183  	if err := tm.BuildInput(atoms, mol, calc); err != nil {
   184  		Te.Error(err)
   185  	}
   186  	/*
   187  
   188  		if err := tm.Run(true); err != nil {
   189  			Te.Error(err)
   190  		}
   191  		energy, err := tm.Energy()
   192  		if err != nil {
   193  			Te.Error(err)
   194  		}
   195  		fmt.Println("energy", energy)
   196  		geo, err := tm.OptimizedGeometry(mol)
   197  		if err != nil {
   198  			Te.Error(err)
   199  		}
   200  		fmt.Println("GEO", geo)
   201  		chem.XYZFileWrite("optiethanol.xyz", geo, mol)
   202  		fmt.Println("end TurboTest!")
   203  	*/
   204  	//	os.Chdir(original_dir)
   205  }
   206  
   207  func TestFermions(Te *testing.T) {
   208  	mol, err := chem.XYZFileRead("../test/ethanol.xyz")
   209  	if err != nil {
   210  		Te.Error(err)
   211  	}
   212  	if err := mol.Corrupted(); err != nil {
   213  		Te.Error(err)
   214  	}
   215  	mol.SetCharge(0)
   216  	mol.SetMulti(1)
   217  	calc := new(Calc)
   218  	calc.Job = &Job{Opti: true}
   219  	calc.Method = "BLYP"
   220  	calc.Dielectric = 4
   221  	calc.Basis = "def2-SVP"
   222  	calc.Grid = 4
   223  	calc.Dispersion = "D3"
   224  	calc.CConstraints = []int{0, 10, 20}
   225  	cs := NewFermionsHandle()
   226  	cs.SetWorkDir("fermions")
   227  	cs.SetName("gochemF")
   228  	atoms := mol.Coords[0]
   229  	original_dir, _ := os.Getwd() //will check in a few lines
   230  	if err = os.Chdir("../test"); err != nil {
   231  		Te.Error(err)
   232  	}
   233  	err = cs.BuildInput(atoms, mol, calc)
   234  	defer os.Chdir(original_dir)
   235  	//	E, err := cs.Energy()
   236  	//	if err != nil {
   237  	//		Te.Error(err)
   238  	//	}
   239  	//	fmt.Println("Final energy:", E, "kcal/mol")
   240  	//	ngeo,err:=cs.OptimizedGeometry(mol)
   241  	//	if err!=nil{
   242  	//		fmt.Println("Error with the geometry?: ", err.Error())
   243  	//	}
   244  	//	chem.XYZFileWrite("LastGeoFermions.xyz",ngeo,mol)
   245  	fmt.Println("Passed FermiONs++ test!")
   246  }
   247  
   248  func qderror_handler(err error, Te *testing.T) {
   249  	if err != nil {
   250  		if strings.Contains("NonFatal", err.Error()) {
   251  			fmt.Println("Non fatal error: ", err.Error())
   252  		} else {
   253  			Te.Error(err)
   254  		}
   255  	}
   256  }
   257  
   258  func TestNWChem(Te *testing.T) {
   259  	mol, err := chem.XYZFileRead("../test/water.xyz")
   260  	if err != nil {
   261  		Te.Error(err)
   262  	}
   263  	if err := mol.Corrupted(); err != nil {
   264  		Te.Error(err)
   265  	}
   266  	fmt.Println(mol.Coords[0], len(mol.Coords), "Quiere quedar leyenda, compadre?", err)
   267  	mol.SetCharge(0)
   268  	mol.SetMulti(1)
   269  	calc := new(Calc)
   270  	calc.SCFTightness = 1 //quite tight
   271  	calc.SCFConvHelp = 1
   272  	calc.Job = &Job{Opti: true}
   273  	calc.Method = "TPSS"
   274  	calc.Dielectric = 4
   275  	calc.Basis = "def2-SVP"
   276  	calc.HighBasis = "def2-TZVP"
   277  	calc.Grid = 4
   278  	calc.Memory = 1000
   279  	calc.HBAtoms = []int{2}
   280  	calc.HBElements = []string{"O"}
   281  	calc.CConstraints = []int{1}
   282  	calc.SetDefaults()
   283  	nw := NewNWChemHandle()
   284  	orca := NewOrcaHandle()
   285  	nw.SetName("gochem")
   286  	nw.SetnCPU(1)
   287  	nw.SetCommand("/usr/lib64/openmpi/bin/nwchem_openmpi")
   288  	orca.SetName("gochemII")
   289  	nw.SetWorkDir("../test/nwchem")
   290  	orca.SetWorkDir("../test/nwchem")
   291  	atoms := mol.Coords[0] //v3.Zeros(mol.Len())
   292  	//	mol.Next(atoms)
   293  	//	if err = os.Chdir("../test"); err != nil {
   294  	//		Te.Error(err)
   295  	//	}
   296  	err = nw.BuildInput(atoms, mol, calc)
   297  	if err != nil {
   298  		Te.Error(err)
   299  	}
   300  	/*
   301  		//nw.Run(true)
   302  		_ = orca.BuildInput(atoms, mol, calc)
   303  		//The files are already in ./test.
   304  		os.Chdir("../test")
   305  		defer os.Chdir("../qm")
   306  		energy, err := nw.Energy()
   307  		if err != nil {
   308  			Te.Error(err)
   309  		}
   310  		fmt.Println("NWChem Energy: ", energy)
   311  		newg, err := nw.OptimizedGeometry(mol)
   312  		if err != nil {
   313  			Te.Error(err)
   314  		}
   315  		chem.XYZFileWrite("optiNW.xyz", newg, mol)
   316  	*/
   317  }
   318  
   319  func TestXtb(Te *testing.T) {
   320  	mol, err := chem.XYZFileRead("../test/ethanol.xyz")
   321  	if err != nil {
   322  		Te.Error(err)
   323  	}
   324  	if err := mol.Corrupted(); err != nil {
   325  		Te.Error(err)
   326  	}
   327  	fmt.Println(mol.Coords[0], len(mol.Coords), "Quiere quedar XTB leyenda, compadre?", err)
   328  	mol.SetCharge(0)
   329  	mol.SetMulti(1)
   330  	calc := new(Calc)
   331  	calc.Job = &Job{Opti: true}
   332  	calc.CConstraints = []int{2, 4}
   333  	//no support for constraints yet
   334  	calc.Method = "gfn2"
   335  	calc.Dielectric = 4
   336  	xtb := NewXTBHandle()
   337  	xtb.SetName("XTBgochem")
   338  	xtb.SetWorkDir("xtb")
   339  	atoms := v3.Zeros(mol.Len())
   340  	mol.Next(atoms)
   341  	if err = os.Chdir("../test"); err != nil {
   342  		Te.Error(err)
   343  	}
   344  	err = xtb.BuildInput(atoms, mol, calc)
   345  	if err != nil {
   346  		Te.Error(err)
   347  	}
   348  	if err := xtb.Run(true); err != nil {
   349  		Te.Error(err)
   350  	}
   351  	os.Chdir("../test")
   352  	defer os.Chdir("../qm")
   353  	energy, err := xtb.Energy()
   354  	if err != nil {
   355  		Te.Error(err)
   356  	}
   357  	fmt.Println("XTB Energy: ", energy)
   358  	newg, err := xtb.OptimizedGeometry(mol)
   359  	if err != nil {
   360  		Te.Error(err)
   361  	}
   362  	chem.XYZFileWrite("optiXTB.xyz", newg, mol)
   363  
   364  }