Split txtdump.*, part 1
[gromacs.git] / src / gromacs / mdlib / qm_orca.cpp
blobb0cf841c9ff063e7ce52ac18d56e870e3a39a5a4
1 /*
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,2015, 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.
37 #include "gmxpre.h"
39 #include <math.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #include "gromacs/fileio/confio.h"
45 #include "gromacs/gmxlib/network.h"
46 #include "gromacs/gmxlib/nrnb.h"
47 #include "gromacs/math/units.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/mdlib/force.h"
50 #include "gromacs/mdlib/ns.h"
51 #include "gromacs/mdlib/qmmm.h"
52 #include "gromacs/mdtypes/md_enums.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/smalloc.h"
56 /* ORCA interface routines */
58 void init_orca(t_QMrec *qm)
60 char *buf;
61 snew(buf, 200);
63 /* ORCA settings on the system */
64 buf = getenv("GMX_QM_ORCA_BASENAME");
65 if (buf)
67 snew(qm->orca_basename, 200);
68 sscanf(buf, "%s", qm->orca_basename);
70 else
72 gmx_fatal(FARGS, "$GMX_QM_ORCA_BASENAME is not set\n");
75 /* ORCA directory on the system */
76 snew(buf, 200);
77 buf = getenv("GMX_ORCA_PATH");
79 if (buf)
81 snew(qm->orca_dir, 200);
82 sscanf(buf, "%s", qm->orca_dir);
84 else
86 gmx_fatal(FARGS, "$GMX_ORCA_PATH not set, check manual\n");
89 fprintf(stderr, "Setting ORCA path to: %s...\n", qm->orca_dir);
90 fprintf(stderr, "ORCA initialised...\n\n");
91 /* since we append the output to the BASENAME.out file,
92 we should delete an existent old out-file here. */
93 sprintf(buf, "%s.out", qm->orca_basename);
94 remove(buf);
98 void write_orca_input(t_forcerec *fr, t_QMrec *qm, t_MMrec *mm)
100 int i;
101 t_QMMMrec *QMMMrec;
102 FILE *out, *pcFile, *addInputFile, *LJCoeff;
103 char *buf, *orcaInput, *addInputFilename, *LJCoeffFilename, *pcFilename, *exclInName, *exclOutName;
105 QMMMrec = fr->qr;
107 /* write the first part of the input-file */
108 snew(orcaInput, 200);
109 sprintf(orcaInput, "%s.inp", qm->orca_basename);
110 out = fopen(orcaInput, "w");
112 snew(addInputFilename, 200);
113 sprintf(addInputFilename, "%s.ORCAINFO", qm->orca_basename);
114 addInputFile = fopen(addInputFilename, "r");
116 fprintf(out, "#input-file generated by GROMACS\n");
118 if (qm->bTS)
120 fprintf(out, "!QMMMOpt TightSCF\n");
121 fprintf(out, "%s\n", "%geom TS_Search EF end");
123 else if (qm->bOPT)
125 fprintf(out, "!QMMMOpt TightSCF\n");
127 else
129 fprintf(out, "!EnGrad TightSCF\n");
132 /* here we include the insertion of the additional orca-input */
133 snew(buf, 200);
134 if (addInputFile != NULL)
136 while (!feof(addInputFile))
138 if (fgets(buf, 200, addInputFile) != NULL)
140 fputs(buf, out);
144 else
146 gmx_fatal(FARGS, "No information on the calculation given in %s\n", addInputFilename);
149 fclose(addInputFile);
151 if (qm->bTS || qm->bOPT)
153 /* freeze the frontier QM atoms and Link atoms. This is
154 * important only if a full QM subsystem optimization is done
155 * with a frozen MM environmeent. For dynamics, or gromacs's own
156 * optimization routines this is not important.
158 /* ORCA reads the exclusions from LJCoeffFilename.Excl,
159 * so we have to rename the file
161 int didStart = 0;
162 for (i = 0; i < qm->nrQMatoms; i++)
164 if (qm->frontatoms[i])
166 if (!didStart)
168 fprintf(out, "%s\n", "%geom");
169 fprintf(out, " Constraints \n");
170 didStart = 1;
172 fprintf(out, " {C %d C}\n", i); /* counting from 0 */
175 if (didStart)
177 fprintf(out, " end\n end\n");
179 /* make a file with information on the C6 and C12 coefficients */
180 if (QMMMrec->QMMMscheme != eQMMMschemeoniom && mm->nrMMatoms)
182 snew(exclInName, 200);
183 snew(exclOutName, 200);
184 sprintf(exclInName, "QMMMexcl.dat");
185 sprintf(exclOutName, "%s.LJ.Excl", qm->orca_basename);
186 rename(exclInName, exclOutName);
187 snew(LJCoeffFilename, 200);
188 sprintf(LJCoeffFilename, "%s.LJ", qm->orca_basename);
189 fprintf(out, "%s%s%s\n", "%LJCOEFFICIENTS \"", LJCoeffFilename, "\"");
190 /* make a file with information on the C6 and C12 coefficients */
191 LJCoeff = fopen(LJCoeffFilename, "w");
192 fprintf(LJCoeff, "%d\n", qm->nrQMatoms);
193 for (i = 0; i < qm->nrQMatoms; i++)
195 #ifdef GMX_DOUBLE
196 fprintf(LJCoeff, "%10.7lf %10.7lf\n", qm->c6[i], qm->c12[i]);
197 #else
198 fprintf(LJCoeff, "%10.7f %10.7f\n", qm->c6[i], qm->c12[i]);
199 #endif
201 fprintf(LJCoeff, "%d\n", mm->nrMMatoms);
202 for (i = 0; i < mm->nrMMatoms; i++)
204 #ifdef GMX_DOUBLE
205 fprintf(LJCoeff, "%10.7lf %10.7lf\n", mm->c6[i], mm->c12[i]);
206 #else
207 fprintf(LJCoeff, "%10.7f %10.7f\n", mm->c6[i], mm->c12[i]);
208 #endif
210 fclose(LJCoeff);
214 /* write charge and multiplicity */
215 fprintf(out, "*xyz %2d%2d\n", qm->QMcharge, qm->multiplicity);
217 /* write the QM coordinates */
218 for (i = 0; i < qm->nrQMatoms; i++)
220 int atomNr;
221 if (qm->atomicnumberQM[i] == 0)
223 atomNr = 1;
225 else
227 atomNr = qm->atomicnumberQM[i];
229 #ifdef GMX_DOUBLE
230 fprintf(out, "%3d %10.7lf %10.7lf %10.7lf\n",
231 atomNr,
232 qm->xQM[i][XX]/0.1,
233 qm->xQM[i][YY]/0.1,
234 qm->xQM[i][ZZ]/0.1);
235 #else
236 fprintf(out, "%3d %10.7f %10.7f %10.7f\n",
237 atomNr,
238 qm->xQM[i][XX]/0.1,
239 qm->xQM[i][YY]/0.1,
240 qm->xQM[i][ZZ]/0.1);
241 #endif
243 fprintf(out, "*\n");
245 /* write the MM point charge data */
246 if (QMMMrec->QMMMscheme != eQMMMschemeoniom && mm->nrMMatoms)
248 /* name of the point charge file */
249 snew(pcFilename, 200);
250 sprintf(pcFilename, "%s.pc", qm->orca_basename);
251 fprintf(out, "%s%s%s\n", "%pointcharges \"", pcFilename, "\"");
252 pcFile = fopen(pcFilename, "w");
253 fprintf(pcFile, "%d\n", mm->nrMMatoms);
254 for (i = 0; i < mm->nrMMatoms; i++)
256 #ifdef GMX_DOUBLE
257 fprintf(pcFile, "%8.4lf %10.7lf %10.7lf %10.7lf\n",
258 mm->MMcharges[i],
259 mm->xMM[i][XX]/0.1,
260 mm->xMM[i][YY]/0.1,
261 mm->xMM[i][ZZ]/0.1);
262 #else
263 fprintf(pcFile, "%8.4f %10.7f %10.7f %10.7f\n",
264 mm->MMcharges[i],
265 mm->xMM[i][XX]/0.1,
266 mm->xMM[i][YY]/0.1,
267 mm->xMM[i][ZZ]/0.1);
268 #endif
270 fprintf(pcFile, "\n");
271 fclose(pcFile);
273 fprintf(out, "\n");
275 fclose(out);
276 } /* write_orca_input */
278 real read_orca_output(rvec QMgrad[], rvec MMgrad[], t_forcerec *fr,
279 t_QMrec *qm, t_MMrec *mm)
282 i, j, atnum;
283 char
284 buf[300], orca_xyzFilename[300], orca_pcgradFilename[300], orca_engradFilename[300];
285 real
286 QMener;
287 FILE
288 *xyz, *pcgrad, *engrad;
289 int k;
290 t_QMMMrec
291 *QMMMrec;
292 QMMMrec = fr->qr;
293 /* in case of an optimization, the coordinates are printed in the
294 * xyz file, the energy and gradients for the QM part are stored in the engrad file
295 * and the gradients for the point charges are stored in the pc file.
298 /* we need the new xyz coordinates of the QM atoms only for separate QM-optimization
301 if (qm->bTS || qm->bOPT)
303 sprintf(orca_xyzFilename, "%s.xyz", qm->orca_basename);
304 xyz = fopen(orca_xyzFilename, "r");
305 if (fgets(buf, 300, xyz) == NULL)
307 gmx_fatal(FARGS, "Unexpected end of ORCA output");
309 if (fgets(buf, 300, xyz) == NULL)
311 gmx_fatal(FARGS, "Unexpected end of ORCA output");
313 for (i = 0; i < qm->nrQMatoms; i++)
315 if (fgets(buf, 300, xyz) == NULL)
317 gmx_fatal(FARGS, "Unexpected end of ORCA output");
319 #ifdef GMX_DOUBLE
320 sscanf(buf, "%d%lf%lf%lf\n",
321 &atnum,
322 &qm->xQM[i][XX],
323 &qm->xQM[i][YY],
324 &qm->xQM[i][ZZ]);
325 #else
326 sscanf(buf, "%d%f%f%f\n",
327 &atnum,
328 &qm->xQM[i][XX],
329 &qm->xQM[i][YY],
330 &qm->xQM[i][ZZ]);
331 #endif
332 for (j = 0; j < DIM; j++)
334 qm->xQM[i][j] *= 0.1;
337 fclose(xyz);
339 sprintf(orca_engradFilename, "%s.engrad", qm->orca_basename);
340 engrad = fopen(orca_engradFilename, "r");
341 /* we read the energy and the gradient for the qm-atoms from the engrad file
343 /* we can skip the first seven lines
345 for (j = 0; j < 7; j++)
347 if (fgets(buf, 300, engrad) == NULL)
349 gmx_fatal(FARGS, "Unexpected end of ORCA output");
352 /* now comes the energy
354 if (fgets(buf, 300, engrad) == NULL)
356 gmx_fatal(FARGS, "Unexpected end of ORCA output");
358 #ifdef GMX_DOUBLE
359 sscanf(buf, "%lf\n", &QMener);
360 #else
361 sscanf(buf, "%f\n", &QMener);
362 #endif
363 /* we can skip the next three lines
365 for (j = 0; j < 3; j++)
367 if (fgets(buf, 300, engrad) == NULL)
369 gmx_fatal(FARGS, "Unexpected end of ORCA output");
372 /* next lines contain the gradients of the QM atoms
373 * now comes the gradient, one value per line:
374 * (atom1 x \n atom1 y \n atom1 z \n atom2 x ...
377 for (i = 0; i < 3*qm->nrQMatoms; i++)
379 k = i/3;
380 if (fgets(buf, 300, engrad) == NULL)
382 gmx_fatal(FARGS, "Unexpected end of ORCA output");
384 #ifdef GMX_DOUBLE
385 if (i%3 == 0)
387 sscanf(buf, "%lf\n", &QMgrad[k][XX]);
389 else if (i%3 == 1)
391 sscanf(buf, "%lf\n", &QMgrad[k][YY]);
393 else if (i%3 == 2)
395 sscanf(buf, "%lf\n", &QMgrad[k][ZZ]);
397 #else
398 if (i%3 == 0)
400 sscanf(buf, "%f\n", &QMgrad[k][XX]);
402 else if (i%3 == 1)
404 sscanf(buf, "%f\n", &QMgrad[k][YY]);
406 else if (i%3 == 2)
408 sscanf(buf, "%f\n", &QMgrad[k][ZZ]);
410 #endif
412 fclose(engrad);
413 /* write the MM point charge data
415 if (QMMMrec->QMMMscheme != eQMMMschemeoniom && mm->nrMMatoms)
417 sprintf(orca_pcgradFilename, "%s.pcgrad", qm->orca_basename);
418 pcgrad = fopen(orca_pcgradFilename, "r");
420 /* we read the gradient for the mm-atoms from the pcgrad file
422 /* we can skip the first line
424 if (fgets(buf, 300, pcgrad) == NULL)
426 gmx_fatal(FARGS, "Unexpected end of ORCA output");
428 for (i = 0; i < mm->nrMMatoms; i++)
430 if (fgets(buf, 300, pcgrad) == NULL)
432 gmx_fatal(FARGS, "Unexpected end of ORCA output");
434 #ifdef GMX_DOUBLE
435 sscanf(buf, "%lf%lf%lf\n",
436 &MMgrad[i][XX],
437 &MMgrad[i][YY],
438 &MMgrad[i][ZZ]);
439 #else
440 sscanf(buf, "%f%f%f\n",
441 &MMgrad[i][XX],
442 &MMgrad[i][YY],
443 &MMgrad[i][ZZ]);
444 #endif
446 fclose(pcgrad);
448 return(QMener);
451 void do_orca(char *orca_dir, char *basename)
454 /* make the call to the orca binary through system()
455 * The location of the binary is set through the
456 * environment.
458 char
459 buf[100];
460 sprintf(buf, "%s/%s %s.inp >> %s.out",
461 orca_dir,
462 "orca",
463 basename,
464 basename);
465 fprintf(stderr, "Calling '%s'\n", buf);
466 if (system(buf) != 0)
468 gmx_fatal(FARGS, "Call to '%s' failed\n", buf);
472 real call_orca(t_forcerec *fr,
473 t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[])
475 /* normal orca jobs */
476 static int
477 step = 0;
479 i, j;
480 real
481 QMener;
482 rvec
483 *QMgrad, *MMgrad;
484 char
485 *exe;
487 snew(exe, 30);
488 sprintf(exe, "%s", "orca");
489 snew(QMgrad, qm->nrQMatoms);
490 snew(MMgrad, mm->nrMMatoms);
492 write_orca_input(fr, qm, mm);
493 do_orca(qm->orca_dir, qm->orca_basename);
494 QMener = read_orca_output(QMgrad, MMgrad, fr, qm, mm);
495 /* put the QMMM forces in the force array and to the fshift
497 for (i = 0; i < qm->nrQMatoms; i++)
499 for (j = 0; j < DIM; j++)
501 f[i][j] = HARTREE_BOHR2MD*QMgrad[i][j];
502 fshift[i][j] = HARTREE_BOHR2MD*QMgrad[i][j];
505 for (i = 0; i < mm->nrMMatoms; i++)
507 for (j = 0; j < DIM; j++)
509 f[i+qm->nrQMatoms][j] = HARTREE_BOHR2MD*MMgrad[i][j];
510 fshift[i+qm->nrQMatoms][j] = HARTREE_BOHR2MD*MMgrad[i][j];
513 QMener = QMener*HARTREE2KJ*AVOGADRO;
514 step++;
515 free(exe);
516 return(QMener);
517 } /* call_orca */
519 /* end of orca sub routines */