2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2008, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
46 #include "gromacs/legacyheaders/typedefs.h"
47 #include "gromacs/legacyheaders/macros.h"
48 #include "gromacs/utility/smalloc.h"
49 #include "gromacs/math/units.h"
50 #include "gromacs/math/vec.h"
51 #include "gromacs/legacyheaders/force.h"
52 #include "gromacs/fileio/confio.h"
53 #include "gromacs/legacyheaders/names.h"
54 #include "gromacs/legacyheaders/network.h"
55 #include "gromacs/legacyheaders/ns.h"
56 #include "gromacs/legacyheaders/nrnb.h"
57 #include "gromacs/legacyheaders/bondf.h"
58 #include "gromacs/legacyheaders/txtdump.h"
59 #include "gromacs/legacyheaders/qmmm.h"
60 #include "gromacs/utility/fatalerror.h"
62 /* ORCA interface routines */
64 void init_orca(t_QMrec *qm)
69 /* ORCA settings on the system */
70 buf = getenv("GMX_QM_ORCA_BASENAME");
73 snew(qm->orca_basename, 200);
74 sscanf(buf, "%s", qm->orca_basename);
78 gmx_fatal(FARGS, "$GMX_QM_ORCA_BASENAME is not set\n");
81 /* ORCA directory on the system */
83 buf = getenv("GMX_ORCA_PATH");
87 snew(qm->orca_dir, 200);
88 sscanf(buf, "%s", qm->orca_dir);
92 gmx_fatal(FARGS, "$GMX_ORCA_PATH not set, check manual\n");
95 fprintf(stderr, "Setting ORCA path to: %s...\n", qm->orca_dir);
96 fprintf(stderr, "ORCA initialised...\n\n");
97 /* since we append the output to the BASENAME.out file,
98 we should delete an existent old out-file here. */
99 sprintf(buf, "%s.out", qm->orca_basename);
104 void write_orca_input(t_forcerec *fr, t_QMrec *qm, t_MMrec *mm)
108 FILE *out, *pcFile, *addInputFile, *LJCoeff;
109 char *buf, *orcaInput, *addInputFilename, *LJCoeffFilename, *pcFilename, *exclInName, *exclOutName;
113 /* write the first part of the input-file */
114 snew(orcaInput, 200);
115 sprintf(orcaInput, "%s.inp", qm->orca_basename);
116 out = fopen(orcaInput, "w");
118 snew(addInputFilename, 200);
119 sprintf(addInputFilename, "%s.ORCAINFO", qm->orca_basename);
120 addInputFile = fopen(addInputFilename, "r");
122 fprintf(out, "#input-file generated by GROMACS\n");
126 fprintf(out, "!QMMMOpt TightSCF\n");
127 fprintf(out, "%s\n", "%geom TS_Search EF end");
131 fprintf(out, "!QMMMOpt TightSCF\n");
135 fprintf(out, "!EnGrad TightSCF\n");
138 /* here we include the insertion of the additional orca-input */
140 if (addInputFile != NULL)
142 while (!feof(addInputFile))
144 if (fgets(buf, 200, addInputFile) != NULL)
152 gmx_fatal(FARGS, "No information on the calculation given in %s\n", addInputFilename);
155 fclose(addInputFile);
157 if (qm->bTS || qm->bOPT)
159 /* freeze the frontier QM atoms and Link atoms. This is
160 * important only if a full QM subsystem optimization is done
161 * with a frozen MM environmeent. For dynamics, or gromacs's own
162 * optimization routines this is not important.
164 /* ORCA reads the exclusions from LJCoeffFilename.Excl,
165 * so we have to rename the file
168 for (i = 0; i < qm->nrQMatoms; i++)
170 if (qm->frontatoms[i])
174 fprintf(out, "%s\n", "%geom");
175 fprintf(out, " Constraints \n");
178 fprintf(out, " {C %d C}\n", i); /* counting from 0 */
183 fprintf(out, " end\n end\n");
185 /* make a file with information on the C6 and C12 coefficients */
186 if (QMMMrec->QMMMscheme != eQMMMschemeoniom && mm->nrMMatoms)
188 snew(exclInName, 200);
189 snew(exclOutName, 200);
190 sprintf(exclInName, "QMMMexcl.dat");
191 sprintf(exclOutName, "%s.LJ.Excl", qm->orca_basename);
192 rename(exclInName, exclOutName);
193 snew(LJCoeffFilename, 200);
194 sprintf(LJCoeffFilename, "%s.LJ", qm->orca_basename);
195 fprintf(out, "%s%s%s\n", "%LJCOEFFICIENTS \"", LJCoeffFilename, "\"");
196 /* make a file with information on the C6 and C12 coefficients */
197 LJCoeff = fopen(LJCoeffFilename, "w");
198 fprintf(LJCoeff, "%d\n", qm->nrQMatoms);
199 for (i = 0; i < qm->nrQMatoms; i++)
202 fprintf(LJCoeff, "%10.7lf %10.7lf\n", qm->c6[i], qm->c12[i]);
204 fprintf(LJCoeff, "%10.7f %10.7f\n", qm->c6[i], qm->c12[i]);
207 fprintf(LJCoeff, "%d\n", mm->nrMMatoms);
208 for (i = 0; i < mm->nrMMatoms; i++)
211 fprintf(LJCoeff, "%10.7lf %10.7lf\n", mm->c6[i], mm->c12[i]);
213 fprintf(LJCoeff, "%10.7f %10.7f\n", mm->c6[i], mm->c12[i]);
220 /* write charge and multiplicity */
221 fprintf(out, "*xyz %2d%2d\n", qm->QMcharge, qm->multiplicity);
223 /* write the QM coordinates */
224 for (i = 0; i < qm->nrQMatoms; i++)
227 if (qm->atomicnumberQM[i] == 0)
233 atomNr = qm->atomicnumberQM[i];
236 fprintf(out, "%3d %10.7lf %10.7lf %10.7lf\n",
242 fprintf(out, "%3d %10.7f %10.7f %10.7f\n",
251 /* write the MM point charge data */
252 if (QMMMrec->QMMMscheme != eQMMMschemeoniom && mm->nrMMatoms)
254 /* name of the point charge file */
255 snew(pcFilename, 200);
256 sprintf(pcFilename, "%s.pc", qm->orca_basename);
257 fprintf(out, "%s%s%s\n", "%pointcharges \"", pcFilename, "\"");
258 pcFile = fopen(pcFilename, "w");
259 fprintf(pcFile, "%d\n", mm->nrMMatoms);
260 for (i = 0; i < mm->nrMMatoms; i++)
263 fprintf(pcFile, "%8.4lf %10.7lf %10.7lf %10.7lf\n",
269 fprintf(pcFile, "%8.4f %10.7f %10.7f %10.7f\n",
276 fprintf(pcFile, "\n");
282 } /* write_orca_input */
284 real read_orca_output(rvec QMgrad[], rvec MMgrad[], t_forcerec *fr,
285 t_QMrec *qm, t_MMrec *mm)
290 buf[300], tmp[300], orca_xyzFilename[300], orca_pcgradFilename[300], orca_engradFilename[300];
294 *xyz, *pcgrad, *engrad;
299 /* in case of an optimization, the coordinates are printed in the
300 * xyz file, the energy and gradients for the QM part are stored in the engrad file
301 * and the gradients for the point charges are stored in the pc file.
304 /* we need the new xyz coordinates of the QM atoms only for separate QM-optimization
307 if (qm->bTS || qm->bOPT)
309 sprintf(orca_xyzFilename, "%s.xyz", qm->orca_basename);
310 xyz = fopen(orca_xyzFilename, "r");
311 if (fgets(buf, 300, xyz) == NULL)
313 gmx_fatal(FARGS, "Unexpected end of ORCA output");
315 if (fgets(buf, 300, xyz) == NULL)
317 gmx_fatal(FARGS, "Unexpected end of ORCA output");
319 for (i = 0; i < qm->nrQMatoms; i++)
321 if (fgets(buf, 300, xyz) == NULL)
323 gmx_fatal(FARGS, "Unexpected end of ORCA output");
326 sscanf(buf, "%s%lf%lf%lf\n",
332 sscanf(buf, "%d%f%f%f\n",
338 for (j = 0; j < DIM; j++)
340 qm->xQM[i][j] *= 0.1;
345 sprintf(orca_engradFilename, "%s.engrad", qm->orca_basename);
346 engrad = fopen(orca_engradFilename, "r");
347 /* we read the energy and the gradient for the qm-atoms from the engrad file
349 /* we can skip the first seven lines
351 for (j = 0; j < 7; j++)
353 if (fgets(buf, 300, engrad) == NULL)
355 gmx_fatal(FARGS, "Unexpected end of ORCA output");
358 /* now comes the energy
360 if (fgets(buf, 300, engrad) == NULL)
362 gmx_fatal(FARGS, "Unexpected end of ORCA output");
365 sscanf(buf, "%lf\n", &QMener);
367 sscanf(buf, "%f\n", &QMener);
369 /* we can skip the next three lines
371 for (j = 0; j < 3; j++)
373 if (fgets(buf, 300, engrad) == NULL)
375 gmx_fatal(FARGS, "Unexpected end of ORCA output");
378 /* next lines contain the gradients of the QM atoms
379 * now comes the gradient, one value per line:
380 * (atom1 x \n atom1 y \n atom1 z \n atom2 x ...
383 for (i = 0; i < 3*qm->nrQMatoms; i++)
386 if (fgets(buf, 300, engrad) == NULL)
388 gmx_fatal(FARGS, "Unexpected end of ORCA output");
393 sscanf(buf, "%lf\n", &QMgrad[k][XX]);
397 sscanf(buf, "%lf\n", &QMgrad[k][YY]);
401 sscanf(buf, "%lf\n", &QMgrad[k][ZZ]);
406 sscanf(buf, "%f\n", &QMgrad[k][XX]);
410 sscanf(buf, "%f\n", &QMgrad[k][YY]);
414 sscanf(buf, "%f\n", &QMgrad[k][ZZ]);
419 /* write the MM point charge data
421 if (QMMMrec->QMMMscheme != eQMMMschemeoniom && mm->nrMMatoms)
423 sprintf(orca_pcgradFilename, "%s.pcgrad", qm->orca_basename);
424 pcgrad = fopen(orca_pcgradFilename, "r");
426 /* we read the gradient for the mm-atoms from the pcgrad file
428 /* we can skip the first line
430 if (fgets(buf, 300, pcgrad) == NULL)
432 gmx_fatal(FARGS, "Unexpected end of ORCA output");
434 for (i = 0; i < mm->nrMMatoms; i++)
436 if (fgets(buf, 300, pcgrad) == NULL)
438 gmx_fatal(FARGS, "Unexpected end of ORCA output");
441 sscanf(buf, "%lf%lf%lf\n",
446 sscanf(buf, "%f%f%f\n",
457 void do_orca(char *orca_dir, char *basename)
460 /* make the call to the orca binary through system()
461 * The location of the binary is set through the
466 sprintf(buf, "%s/%s %s.inp >> %s.out",
471 fprintf(stderr, "Calling '%s'\n", buf);
472 if (system(buf) != 0)
474 gmx_fatal(FARGS, "Call to '%s' failed\n", buf);
478 real call_orca(t_forcerec *fr,
479 t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[])
481 /* normal orca jobs */
494 sprintf(exe, "%s", "orca");
495 snew(QMgrad, qm->nrQMatoms);
496 snew(MMgrad, mm->nrMMatoms);
498 write_orca_input(fr, qm, mm);
499 do_orca(qm->orca_dir, qm->orca_basename);
500 QMener = read_orca_output(QMgrad, MMgrad, fr, qm, mm);
501 /* put the QMMM forces in the force array and to the fshift
503 for (i = 0; i < qm->nrQMatoms; i++)
505 for (j = 0; j < DIM; j++)
507 f[i][j] = HARTREE_BOHR2MD*QMgrad[i][j];
508 fshift[i][j] = HARTREE_BOHR2MD*QMgrad[i][j];
511 for (i = 0; i < mm->nrMMatoms; i++)
513 for (j = 0; j < DIM; j++)
515 f[i+qm->nrQMatoms][j] = HARTREE_BOHR2MD*MMgrad[i][j];
516 fshift[i+qm->nrQMatoms][j] = HARTREE_BOHR2MD*MMgrad[i][j];
519 QMener = QMener*HARTREE2KJ*AVOGADRO;
525 /* end of orca sub routines */