Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / kernel / add_par.c
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  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * 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 /* This file is completely threadsafe - keep it that way! */
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include <string.h>
44 #include "typedefs.h"
45 #include "smalloc.h"
46 #include "grompp.h"
47 #include "toputil.h"
48 #include "hackblock.h"
49 #include "string2.h"
50 #include "gmx_fatal.h"
51
52 static void clear_atom_list(int i0, atom_id a[])
53 {
54   int i;
55   
56   for (i=i0; i < MAXATOMLIST; i++)
57     a[i]=-1;
58 }
59
60 static void clear_force_param(int i0, real c[])
61 {
62   int i;
63   
64   for (i=i0; i < MAXFORCEPARAM; i++)
65     c[i]=NOTSET;
66 }
67
68 void add_param(t_params *ps,int ai,int aj, real *c, char *s)
69 {
70   int i;
71   
72   if ((ai < 0) || (aj < 0)) 
73     gmx_fatal(FARGS,"Trying to add impossible atoms: ai=%d, aj=%d",ai,aj);
74   pr_alloc(1,ps);
75   ps->param[ps->nr].AI=ai;
76   ps->param[ps->nr].AJ=aj;
77   clear_atom_list(2, ps->param[ps->nr].a);
78   if (c==NULL) 
79     clear_force_param(0, ps->param[ps->nr].c);
80   else
81     for(i=0; (i < MAXFORCEPARAM); i++)
82       ps->param[ps->nr].c[i]=c[i];
83   set_p_string(&(ps->param[ps->nr]),s);
84   ps->nr++;
85 }
86
87 void add_imp_param(t_params *ps,int ai,int aj,int ak,int al,real c0, real c1,
88                    char *s)
89 {
90   pr_alloc(1,ps);
91   ps->param[ps->nr].AI=ai;
92   ps->param[ps->nr].AJ=aj;
93   ps->param[ps->nr].AK=ak;
94   ps->param[ps->nr].AL=al;
95   clear_atom_list  (4, ps->param[ps->nr].a);
96   ps->param[ps->nr].C0=c0;
97   ps->param[ps->nr].C1=c1;
98   clear_force_param(2, ps->param[ps->nr].c);
99   set_p_string(&(ps->param[ps->nr]),s);
100   ps->nr++;
101 }
102
103 void add_dih_param(t_params *ps,int ai,int aj,int ak,int al,real c0, real c1,
104                    real c2,char *s)
105 {
106   pr_alloc(1,ps);
107   ps->param[ps->nr].AI=ai;
108   ps->param[ps->nr].AJ=aj;
109   ps->param[ps->nr].AK=ak;
110   ps->param[ps->nr].AL=al;
111   clear_atom_list  (4, ps->param[ps->nr].a);
112   ps->param[ps->nr].C0=c0;
113   ps->param[ps->nr].C1=c1;
114   ps->param[ps->nr].C2=c2;
115   clear_force_param(3, ps->param[ps->nr].c);
116   set_p_string(&(ps->param[ps->nr]),s);
117   ps->nr++;
118 }
119
120 void add_cmap_param(t_params *ps, int ai, int aj, int ak, int al, int am, char *s)
121 {
122         pr_alloc(1,ps);
123         ps->param[ps->nr].AI=ai;
124         ps->param[ps->nr].AJ=aj;
125         ps->param[ps->nr].AK=ak;
126         ps->param[ps->nr].AL=al;
127         ps->param[ps->nr].AM=am;
128         clear_atom_list(5,ps->param[ps->nr].a);
129         clear_force_param(0,ps->param[ps->nr].c);
130         set_p_string(&(ps->param[ps->nr]),s);
131         ps->nr++;
132 }
133
134 void add_vsite2_atoms(t_params *ps,int ai,int aj,int ak)
135 {
136   pr_alloc(1,ps);
137   ps->param[ps->nr].AI=ai;
138   ps->param[ps->nr].AJ=aj;
139   ps->param[ps->nr].AK=ak;
140   clear_atom_list  (3, ps->param[ps->nr].a);
141   clear_force_param(0, ps->param[ps->nr].c);
142   set_p_string(&(ps->param[ps->nr]),"");
143   ps->nr++;
144 }
145
146 void add_vsite2_param(t_params *ps,int ai,int aj,int ak,real c0)
147 {
148   pr_alloc(1,ps);
149   ps->param[ps->nr].AI=ai;
150   ps->param[ps->nr].AJ=aj;
151   ps->param[ps->nr].AK=ak;
152   clear_atom_list  (3, ps->param[ps->nr].a);
153   ps->param[ps->nr].C0=c0;
154   clear_force_param(1, ps->param[ps->nr].c);
155   set_p_string(&(ps->param[ps->nr]),"");
156   ps->nr++;
157 }
158
159 void add_vsite3_param(t_params *ps,int ai,int aj,int ak,int al, 
160                     real c0, real c1)
161 {
162   pr_alloc(1,ps);
163   ps->param[ps->nr].AI=ai;
164   ps->param[ps->nr].AJ=aj;
165   ps->param[ps->nr].AK=ak;
166   ps->param[ps->nr].AL=al;
167   clear_atom_list  (4, ps->param[ps->nr].a);
168   ps->param[ps->nr].C0=c0;
169   ps->param[ps->nr].C1=c1;
170   clear_force_param(2, ps->param[ps->nr].c);
171   set_p_string(&(ps->param[ps->nr]),"");
172   ps->nr++;
173 }
174
175 void add_vsite3_atoms(t_params *ps,int ai,int aj,int ak,int al, gmx_bool bSwapParity)
176 {
177   pr_alloc(1,ps);
178   ps->param[ps->nr].AI=ai;
179   ps->param[ps->nr].AJ=aj;
180   ps->param[ps->nr].AK=ak;
181   ps->param[ps->nr].AL=al;
182   clear_atom_list  (4, ps->param[ps->nr].a);
183   clear_force_param(0, ps->param[ps->nr].c);
184   if (bSwapParity)
185     ps->param[ps->nr].C1=-1;
186   set_p_string(&(ps->param[ps->nr]),"");
187   ps->nr++;
188 }
189
190 void add_vsite4_atoms(t_params *ps,int ai,int aj,int ak,int al,int am)
191 {
192   pr_alloc(1,ps);
193   ps->param[ps->nr].AI=ai;
194   ps->param[ps->nr].AJ=aj;
195   ps->param[ps->nr].AK=ak;
196   ps->param[ps->nr].AL=al;
197   ps->param[ps->nr].AM=am;
198   clear_atom_list  (5, ps->param[ps->nr].a);
199   clear_force_param(0, ps->param[ps->nr].c);
200   set_p_string(&(ps->param[ps->nr]),"");
201   ps->nr++;
202 }
203
204 int search_jtype(t_restp *rtp,char *name,gmx_bool bNterm)
205 {
206   int  niter,iter,j,k,kmax,jmax,minstrlen;
207   char *rtpname,searchname[12];
208   
209   strcpy(searchname,name);
210   
211   /* Do a best match comparison */
212   /* for protein N-terminus, allow renaming of H1, H2 and H3 to H */
213   if ( bNterm && (strlen(searchname)==2) && (searchname[0] == 'H') && 
214        ( (searchname[1] == '1') || (searchname[1] == '2') || 
215          (searchname[1] == '3') ) ) {
216     niter = 2;
217   } else {
218     niter = 1;
219   }
220   kmax=0;
221   jmax=-1;
222   for(iter=0; (iter<niter && jmax==-1); iter++) {
223     if (iter == 1) {
224       /* Try without the hydrogen number in the N-terminus */
225       searchname[1] = '\0';
226     }
227     for(j=0; (j<rtp->natom); j++) {
228       rtpname=*(rtp->atomname[j]);
229       if (gmx_strcasecmp(searchname,rtpname) == 0) {
230         jmax=j;
231         kmax=strlen(searchname);
232         break;
233       }
234       if (iter == niter - 1) {
235         minstrlen = min(strlen(searchname), strlen(rtpname));
236         for(k=0; k < minstrlen; k++) 
237           if (searchname[k] != rtpname[k])
238             break;
239         if (k > kmax) {
240           kmax=k;
241           jmax=j;
242         }
243       }
244     }
245   }
246   if (jmax == -1)
247     gmx_fatal(FARGS,"Atom %s not found in rtp database in residue %s",
248               searchname,rtp->resname);
249   if (kmax != strlen(searchname))
250     gmx_fatal(FARGS,"Atom %s not found in rtp database in residue %s, "
251               "it looks a bit like %s",
252               searchname,rtp->resname,*(rtp->atomname[jmax]));
253   return jmax;
254 }
255