543a0f41135fc173604f8325950a2025100a5ed0
[alexxy/gromacs.git] / src / gromacs / topology / idef.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2018 by the GROMACS development team.
7  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "idef.h"
41
42 #include <cstdio>
43
44 #include "gromacs/topology/forcefieldparameters.h"
45 #include "gromacs/topology/ifunc.h"
46 #include "gromacs/utility/fatalerror.h"
47 #include "gromacs/utility/smalloc.h"
48 #include "gromacs/utility/stringstream.h"
49 #include "gromacs/utility/textwriter.h"
50 #include "gromacs/utility/txtdump.h"
51
52 static void printHarmonicInteraction(gmx::TextWriter* writer,
53                                      const t_iparams& iparams,
54                                      const char*      r,
55                                      const char*      kr)
56 {
57     writer->writeLineFormatted("%sA=%12.5e, %sA=%12.5e, %sB=%12.5e, %sB=%12.5e",
58                                r,
59                                iparams.harmonic.rA,
60                                kr,
61                                iparams.harmonic.krA,
62                                r,
63                                iparams.harmonic.rB,
64                                kr,
65                                iparams.harmonic.krB);
66 }
67
68 void pr_iparams(FILE* fp, t_functype ftype, const t_iparams& iparams)
69 {
70     gmx::StringOutputStream stream;
71     {
72         gmx::TextWriter writer(&stream);
73         printInteractionParameters(&writer, ftype, iparams);
74     }
75     fputs(stream.toString().c_str(), fp);
76 }
77
78 void printInteractionParameters(gmx::TextWriter* writer, t_functype ftype, const t_iparams& iparams)
79 {
80     switch (ftype)
81     {
82         case F_ANGLES:
83         case F_G96ANGLES: printHarmonicInteraction(writer, iparams, "th", "ct"); break;
84         case F_CROSS_BOND_BONDS:
85             writer->writeLineFormatted("r1e=%15.8e, r2e=%15.8e, krr=%15.8e",
86                                        iparams.cross_bb.r1e,
87                                        iparams.cross_bb.r2e,
88                                        iparams.cross_bb.krr);
89             break;
90         case F_CROSS_BOND_ANGLES:
91             writer->writeLineFormatted("r1e=%15.8e, r1e=%15.8e, r3e=%15.8e, krt=%15.8e",
92                                        iparams.cross_ba.r1e,
93                                        iparams.cross_ba.r2e,
94                                        iparams.cross_ba.r3e,
95                                        iparams.cross_ba.krt);
96             break;
97         case F_LINEAR_ANGLES:
98             writer->writeLineFormatted("klinA=%15.8e, aA=%15.8e, klinB=%15.8e, aB=%15.8e",
99                                        iparams.linangle.klinA,
100                                        iparams.linangle.aA,
101                                        iparams.linangle.klinB,
102                                        iparams.linangle.aB);
103             break;
104         case F_UREY_BRADLEY:
105             writer->writeLineFormatted(
106                     "thetaA=%15.8e, kthetaA=%15.8e, r13A=%15.8e, kUBA=%15.8e, thetaB=%15.8e, "
107                     "kthetaB=%15.8e, r13B=%15.8e, kUBB=%15.8e",
108                     iparams.u_b.thetaA,
109                     iparams.u_b.kthetaA,
110                     iparams.u_b.r13A,
111                     iparams.u_b.kUBA,
112                     iparams.u_b.thetaB,
113                     iparams.u_b.kthetaB,
114                     iparams.u_b.r13B,
115                     iparams.u_b.kUBB);
116             break;
117         case F_QUARTIC_ANGLES:
118             writer->writeStringFormatted("theta=%15.8e", iparams.qangle.theta);
119             for (int i = 0; i < 5; i++)
120             {
121                 writer->writeStringFormatted(", c%c=%15.8e", '0' + i, iparams.qangle.c[i]);
122             }
123             writer->ensureLineBreak();
124             break;
125         case F_BHAM:
126             writer->writeLineFormatted(
127                     "a=%15.8e, b=%15.8e, c=%15.8e", iparams.bham.a, iparams.bham.b, iparams.bham.c);
128             break;
129         case F_BONDS:
130         case F_G96BONDS:
131         case F_HARMONIC: printHarmonicInteraction(writer, iparams, "b0", "cb"); break;
132         case F_IDIHS: printHarmonicInteraction(writer, iparams, "xi", "cx"); break;
133         case F_MORSE:
134             writer->writeLineFormatted(
135                     "b0A=%15.8e, cbA=%15.8e, betaA=%15.8e, b0B=%15.8e, cbB=%15.8e, betaB=%15.8e",
136                     iparams.morse.b0A,
137                     iparams.morse.cbA,
138                     iparams.morse.betaA,
139                     iparams.morse.b0B,
140                     iparams.morse.cbB,
141                     iparams.morse.betaB);
142             break;
143         case F_CUBICBONDS:
144             writer->writeLineFormatted("b0=%15.8e, kb=%15.8e, kcub=%15.8e",
145                                        iparams.cubic.b0,
146                                        iparams.cubic.kb,
147                                        iparams.cubic.kcub);
148             break;
149         case F_CONNBONDS: writer->ensureEmptyLine(); break;
150         case F_FENEBONDS:
151             writer->writeLineFormatted("bm=%15.8e, kb=%15.8e", iparams.fene.bm, iparams.fene.kb);
152             break;
153         case F_RESTRBONDS:
154             writer->writeLineFormatted(
155                     "lowA=%15.8e, up1A=%15.8e, up2A=%15.8e, kA=%15.8e, lowB=%15.8e, up1B=%15.8e, "
156                     "up2B=%15.8e, kB=%15.8e,",
157                     iparams.restraint.lowA,
158                     iparams.restraint.up1A,
159                     iparams.restraint.up2A,
160                     iparams.restraint.kA,
161                     iparams.restraint.lowB,
162                     iparams.restraint.up1B,
163                     iparams.restraint.up2B,
164                     iparams.restraint.kB);
165             break;
166         case F_TABBONDS:
167         case F_TABBONDSNC:
168         case F_TABANGLES:
169         case F_TABDIHS:
170             writer->writeLineFormatted(
171                     "tab=%d, kA=%15.8e, kB=%15.8e", iparams.tab.table, iparams.tab.kA, iparams.tab.kB);
172             break;
173         case F_POLARIZATION:
174             writer->writeLineFormatted("alpha=%15.8e", iparams.polarize.alpha);
175             break;
176         case F_ANHARM_POL:
177             writer->writeLineFormatted("alpha=%15.8e drcut=%15.8e khyp=%15.8e",
178                                        iparams.anharm_polarize.alpha,
179                                        iparams.anharm_polarize.drcut,
180                                        iparams.anharm_polarize.khyp);
181             break;
182         case F_THOLE_POL:
183             writer->writeLineFormatted("a=%15.8e, alpha1=%15.8e, alpha2=%15.8e",
184                                        iparams.thole.a,
185                                        iparams.thole.alpha1,
186                                        iparams.thole.alpha2);
187             break;
188         case F_WATER_POL:
189             writer->writeLineFormatted(
190                     "al_x=%15.8e, al_y=%15.8e, al_z=%15.8e, rOH=%9.6f, rHH=%9.6f, rOD=%9.6f",
191                     iparams.wpol.al_x,
192                     iparams.wpol.al_y,
193                     iparams.wpol.al_z,
194                     iparams.wpol.rOH,
195                     iparams.wpol.rHH,
196                     iparams.wpol.rOD);
197             break;
198         case F_LJ:
199             writer->writeLineFormatted("c6=%15.8e, c12=%15.8e", iparams.lj.c6, iparams.lj.c12);
200             break;
201         case F_LJ14:
202             writer->writeLineFormatted("c6A=%15.8e, c12A=%15.8e, c6B=%15.8e, c12B=%15.8e",
203                                        iparams.lj14.c6A,
204                                        iparams.lj14.c12A,
205                                        iparams.lj14.c6B,
206                                        iparams.lj14.c12B);
207             break;
208         case F_LJC14_Q:
209             writer->writeLineFormatted("fqq=%15.8e, qi=%15.8e, qj=%15.8e, c6=%15.8e, c12=%15.8e",
210                                        iparams.ljc14.fqq,
211                                        iparams.ljc14.qi,
212                                        iparams.ljc14.qj,
213                                        iparams.ljc14.c6,
214                                        iparams.ljc14.c12);
215             break;
216         case F_LJC_PAIRS_NB:
217             writer->writeLineFormatted("qi=%15.8e, qj=%15.8e, c6=%15.8e, c12=%15.8e",
218                                        iparams.ljcnb.qi,
219                                        iparams.ljcnb.qj,
220                                        iparams.ljcnb.c6,
221                                        iparams.ljcnb.c12);
222             break;
223         case F_PDIHS:
224         case F_PIDIHS:
225         case F_ANGRES:
226         case F_ANGRESZ:
227             writer->writeLineFormatted("phiA=%15.8e, cpA=%15.8e, phiB=%15.8e, cpB=%15.8e, mult=%d",
228                                        iparams.pdihs.phiA,
229                                        iparams.pdihs.cpA,
230                                        iparams.pdihs.phiB,
231                                        iparams.pdihs.cpB,
232                                        iparams.pdihs.mult);
233             break;
234         case F_DISRES:
235             writer->writeLineFormatted(
236                     "label=%4d, type=%1d, low=%15.8e, up1=%15.8e, up2=%15.8e, fac=%15.8e)",
237                     iparams.disres.label,
238                     iparams.disres.type,
239                     iparams.disres.low,
240                     iparams.disres.up1,
241                     iparams.disres.up2,
242                     iparams.disres.kfac);
243             break;
244         case F_ORIRES:
245             writer->writeLineFormatted(
246                     "ex=%4d, label=%d, power=%4d, c=%15.8e, obs=%15.8e, kfac=%15.8e)",
247                     iparams.orires.ex,
248                     iparams.orires.label,
249                     iparams.orires.power,
250                     iparams.orires.c,
251                     iparams.orires.obs,
252                     iparams.orires.kfac);
253             break;
254         case F_DIHRES:
255             writer->writeLineFormatted(
256                     "phiA=%15.8e, dphiA=%15.8e, kfacA=%15.8e, phiB=%15.8e, dphiB=%15.8e, "
257                     "kfacB=%15.8e",
258                     iparams.dihres.phiA,
259                     iparams.dihres.dphiA,
260                     iparams.dihres.kfacA,
261                     iparams.dihres.phiB,
262                     iparams.dihres.dphiB,
263                     iparams.dihres.kfacB);
264             break;
265         case F_POSRES:
266             writer->writeLineFormatted(
267                     "pos0A=(%15.8e,%15.8e,%15.8e), fcA=(%15.8e,%15.8e,%15.8e), "
268                     "pos0B=(%15.8e,%15.8e,%15.8e), fcB=(%15.8e,%15.8e,%15.8e)",
269                     iparams.posres.pos0A[XX],
270                     iparams.posres.pos0A[YY],
271                     iparams.posres.pos0A[ZZ],
272                     iparams.posres.fcA[XX],
273                     iparams.posres.fcA[YY],
274                     iparams.posres.fcA[ZZ],
275                     iparams.posres.pos0B[XX],
276                     iparams.posres.pos0B[YY],
277                     iparams.posres.pos0B[ZZ],
278                     iparams.posres.fcB[XX],
279                     iparams.posres.fcB[YY],
280                     iparams.posres.fcB[ZZ]);
281             break;
282         case F_FBPOSRES:
283             writer->writeLineFormatted(
284                     "pos0=(%15.8e,%15.8e,%15.8e), geometry=%d, r=%15.8e, k=%15.8e",
285                     iparams.fbposres.pos0[XX],
286                     iparams.fbposres.pos0[YY],
287                     iparams.fbposres.pos0[ZZ],
288                     iparams.fbposres.geom,
289                     iparams.fbposres.r,
290                     iparams.fbposres.k);
291             break;
292         case F_RBDIHS:
293             for (int i = 0; i < NR_RBDIHS; i++)
294             {
295                 writer->writeStringFormatted(
296                         "%srbcA[%d]=%15.8e", i == 0 ? "" : ", ", i, iparams.rbdihs.rbcA[i]);
297             }
298             writer->ensureLineBreak();
299             for (int i = 0; i < NR_RBDIHS; i++)
300             {
301                 writer->writeStringFormatted(
302                         "%srbcB[%d]=%15.8e", i == 0 ? "" : ", ", i, iparams.rbdihs.rbcB[i]);
303             }
304             writer->ensureLineBreak();
305             break;
306         case F_FOURDIHS:
307         {
308             /* Use the OPLS -> Ryckaert-Bellemans formula backwards to get
309              * the OPLS potential constants back.
310              */
311             const real* rbcA = iparams.rbdihs.rbcA;
312             const real* rbcB = iparams.rbdihs.rbcB;
313             real        VA[4], VB[4];
314
315             VA[3] = -0.25 * rbcA[4];
316             VA[2] = -0.5 * rbcA[3];
317             VA[1] = 4.0 * VA[3] - rbcA[2];
318             VA[0] = 3.0 * VA[2] - 2.0 * rbcA[1];
319
320             VB[3] = -0.25 * rbcB[4];
321             VB[2] = -0.5 * rbcB[3];
322             VB[1] = 4.0 * VB[3] - rbcB[2];
323             VB[0] = 3.0 * VB[2] - 2.0 * rbcB[1];
324
325             for (int i = 0; i < NR_FOURDIHS; i++)
326             {
327                 writer->writeStringFormatted("%sFourA[%d]=%15.8e", i == 0 ? "" : ", ", i, VA[i]);
328             }
329             writer->ensureLineBreak();
330             for (int i = 0; i < NR_FOURDIHS; i++)
331             {
332                 writer->writeStringFormatted("%sFourB[%d]=%15.8e", i == 0 ? "" : ", ", i, VB[i]);
333             }
334             writer->ensureLineBreak();
335             break;
336         }
337
338         case F_CONSTR:
339         case F_CONSTRNC:
340             writer->writeLineFormatted("dA=%15.8e, dB=%15.8e", iparams.constr.dA, iparams.constr.dB);
341             break;
342         case F_SETTLE:
343             writer->writeLineFormatted("doh=%15.8e, dhh=%15.8e", iparams.settle.doh, iparams.settle.dhh);
344             break;
345         case F_VSITE1: writer->ensureEmptyLine(); break;
346         case F_VSITE2: writer->writeLineFormatted("a=%15.8e", iparams.vsite.a); break;
347         case F_VSITE3:
348         case F_VSITE3FD:
349         case F_VSITE3FAD:
350             writer->writeLineFormatted("a=%15.8e, b=%15.8e", iparams.vsite.a, iparams.vsite.b);
351             break;
352         case F_VSITE3OUT:
353         case F_VSITE4FD:
354         case F_VSITE4FDN:
355             writer->writeLineFormatted(
356                     "a=%15.8e, b=%15.8e, c=%15.8e", iparams.vsite.a, iparams.vsite.b, iparams.vsite.c);
357             break;
358         case F_VSITEN:
359             writer->writeLineFormatted("n=%2d, a=%15.8e", iparams.vsiten.n, iparams.vsiten.a);
360             break;
361         case F_GB12_NOLONGERUSED:
362         case F_GB13_NOLONGERUSED:
363         case F_GB14_NOLONGERUSED:
364             // These could only be generated by grompp, not written in
365             // a .top file. Now that implicit solvent is not
366             // supported, they can't be generated, and the values are
367             // ignored if read from an old .tpr file. So there is
368             // nothing to print.
369             break;
370         case F_CMAP:
371             writer->writeLineFormatted("cmapA=%1d, cmapB=%1d", iparams.cmap.cmapA, iparams.cmap.cmapB);
372             break;
373         case F_RESTRANGLES: printHarmonicInteraction(writer, iparams, "ktheta", "costheta0"); break;
374         case F_RESTRDIHS:
375             writer->writeLineFormatted("phiA=%15.8e, cpA=%15.8e", iparams.pdihs.phiA, iparams.pdihs.cpA);
376             break;
377         case F_CBTDIHS:
378             writer->writeLineFormatted("kphi=%15.8e", iparams.cbtdihs.cbtcA[0]);
379             for (int i = 1; i < NR_CBTDIHS; i++)
380             {
381                 writer->writeStringFormatted(", cbtcA[%d]=%15.8e", i - 1, iparams.cbtdihs.cbtcA[i]);
382             }
383             writer->ensureLineBreak();
384             break;
385         default:
386             gmx_fatal(FARGS,
387                       "unknown function type %d (%s) in %s line %d",
388                       ftype,
389                       interaction_function[ftype].name,
390                       __FILE__,
391                       __LINE__);
392     }
393 }
394
395 template<typename T>
396 static void printIlist(FILE*             fp,
397                        int               indent,
398                        const char*       title,
399                        const t_functype* functype,
400                        const T&          ilist,
401                        gmx_bool          bShowNumbers,
402                        gmx_bool          bShowParameters,
403                        const t_iparams*  iparams)
404 {
405     indent = pr_title(fp, indent, title);
406     pr_indent(fp, indent);
407     fprintf(fp, "nr: %d\n", ilist.size());
408     if (!ilist.empty())
409     {
410         pr_indent(fp, indent);
411         fprintf(fp, "iatoms:\n");
412         int j = 0;
413         for (int i = 0; i < ilist.size();)
414         {
415             pr_indent(fp, indent + INDENT);
416             const int type  = ilist.iatoms[i];
417             const int ftype = functype[type];
418             if (bShowNumbers)
419             {
420                 fprintf(fp, "%d type=%d ", j, type);
421             }
422             j++;
423             printf("(%s)", interaction_function[ftype].name);
424             for (int k = 0; k < interaction_function[ftype].nratoms; k++)
425             {
426                 fprintf(fp, " %3d", ilist.iatoms[i + 1 + k]);
427             }
428             if (bShowParameters)
429             {
430                 fprintf(fp, "  ");
431                 pr_iparams(fp, ftype, iparams[type]);
432             }
433             fprintf(fp, "\n");
434             i += 1 + interaction_function[ftype].nratoms;
435         }
436     }
437 }
438
439 void pr_ilist(FILE*                  fp,
440               int                    indent,
441               const char*            title,
442               const t_functype*      functype,
443               const InteractionList& ilist,
444               gmx_bool               bShowNumbers,
445               gmx_bool               bShowParameters,
446               const t_iparams*       iparams)
447 {
448     printIlist(fp, indent, title, functype, ilist, bShowNumbers, bShowParameters, iparams);
449 }
450
451 void pr_idef(FILE* fp, int indent, const char* title, const t_idef* idef, gmx_bool bShowNumbers, gmx_bool bShowParameters)
452 {
453     if (available(fp, idef, indent, title))
454     {
455         indent = pr_title(fp, indent, title);
456         pr_indent(fp, indent);
457         fprintf(fp, "atnr=%d\n", idef->atnr);
458         pr_indent(fp, indent);
459         fprintf(fp, "ntypes=%d\n", idef->ntypes);
460         for (int i = 0; i < idef->ntypes; i++)
461         {
462             pr_indent(fp, indent + INDENT);
463             fprintf(fp,
464                     "functype[%d]=%s, ",
465                     bShowNumbers ? i : -1,
466                     interaction_function[idef->functype[i]].name);
467             pr_iparams(fp, idef->functype[i], idef->iparams[i]);
468         }
469         pr_real(fp, indent, "fudgeQQ", idef->fudgeQQ);
470
471         for (int j = 0; (j < F_NRE); j++)
472         {
473             printIlist(fp,
474                        indent,
475                        interaction_function[j].longname,
476                        idef->functype,
477                        idef->il[j],
478                        bShowNumbers,
479                        bShowParameters,
480                        idef->iparams);
481         }
482     }
483 }
484
485 void init_idef(t_idef* idef)
486 {
487     idef->ntypes           = 0;
488     idef->atnr             = 0;
489     idef->functype         = nullptr;
490     idef->iparams          = nullptr;
491     idef->fudgeQQ          = 0.0;
492     idef->iparams_posres   = nullptr;
493     idef->iparams_fbposres = nullptr;
494     for (int f = 0; f < F_NRE; ++f)
495     {
496         idef->il[f].iatoms = nullptr;
497         idef->il[f].nalloc = 0;
498         idef->il[f].nr     = 0;
499     }
500 }
501
502 InteractionDefinitions::InteractionDefinitions(const gmx_ffparams_t& ffparams) :
503     iparams(ffparams.iparams), functype(ffparams.functype), cmap_grid(ffparams.cmap_grid)
504 {
505 }
506
507 void InteractionDefinitions::clear()
508 {
509     /* Clear the counts */
510     for (auto& ilist : il)
511     {
512         ilist.clear();
513     }
514     iparams_posres.clear();
515     iparams_fbposres.clear();
516 }
517
518 void done_idef(t_idef* idef)
519 {
520     sfree(idef->functype);
521     sfree(idef->iparams);
522     sfree(idef->iparams_posres);
523     sfree(idef->iparams_fbposres);
524     for (int f = 0; f < F_NRE; ++f)
525     {
526         sfree(idef->il[f].iatoms);
527     }
528
529     init_idef(idef);
530 }