Framework for printing help for selections.
[gromacs/adressmacs.git] / src / gmxlib / typedefs.c
blobc6d7ca35b6da89e2a31b6c6e06092b0857e8856a
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
33 * And Hey:
34 * GROningen Mixture of Alchemy and Childrens' Stories
36 /* This file is completely threadsafe - keep it that way! */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
41 #include "smalloc.h"
42 #include "symtab.h"
43 #include "vec.h"
44 #include "pbc.h"
45 #include <string.h>
47 #ifdef GMX_THREADS
48 #include "thread_mpi.h"
49 #endif
51 /* The source code in this file should be thread-safe.
52 Please keep it that way. */
56 static bool bOverAllocDD=FALSE;
57 #ifdef GMX_THREADS
58 static tMPI_Thread_mutex_t over_alloc_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
59 #endif
62 void set_over_alloc_dd(bool set)
64 #ifdef GMX_THREADS
65 tMPI_Thread_mutex_lock(&over_alloc_mutex);
66 /* we just make sure that we don't set this at the same time.
67 We don't worry too much about reading this rarely-set variable */
68 #endif
69 bOverAllocDD = set;
70 #ifdef GMX_THREADS
71 tMPI_Thread_mutex_unlock(&over_alloc_mutex);
72 #endif
75 int over_alloc_dd(int n)
77 if (bOverAllocDD)
78 return OVER_ALLOC_FAC*n + 100;
79 else
80 return n;
83 int gmx_large_int_to_int(gmx_large_int_t step,const char *warn)
85 int i;
87 i = (int)step;
89 if (warn != NULL && (step < INT_MIN || step > INT_MAX)) {
90 fprintf(stderr,"\nWARNING during %s:\n",warn);
91 fprintf(stderr,"step value ");
92 fprintf(stderr,gmx_large_int_pfmt,step);
93 fprintf(stderr," does not fit in int, converted to %d\n\n",i);
96 return i;
99 char *gmx_step_str(gmx_large_int_t i,char *buf)
101 sprintf(buf,gmx_large_int_pfmt,i);
103 return buf;
106 void init_block(t_block *block)
108 int i;
110 block->nr = 0;
111 block->nalloc_index = 1;
112 snew(block->index,block->nalloc_index);
113 block->index[0] = 0;
116 void init_blocka(t_blocka *block)
118 int i;
120 block->nr = 0;
121 block->nra = 0;
122 block->nalloc_index = 1;
123 snew(block->index,block->nalloc_index);
124 block->index[0] = 0;
125 block->nalloc_a = 0;
126 block->a = NULL;
129 void init_atom(t_atoms *at)
131 int i;
133 at->nr = 0;
134 at->nres = 0;
135 at->atom = NULL;
136 at->resinfo = NULL;
137 at->atomname = NULL;
138 at->atomtype = NULL;
139 at->atomtypeB= NULL;
140 at->pdbinfo = NULL;
143 void init_atomtypes(t_atomtypes *at)
145 at->nr = 0;
146 at->radius = NULL;
147 at->vol = NULL;
148 at->atomnumber = NULL;
149 at->gb_radius = NULL;
150 at->S_hct = NULL;
153 void init_groups(gmx_groups_t *groups)
155 int g;
157 groups->ngrpname = 0;
158 groups->grpname = NULL;
159 for(g=0; (g<egcNR); g++) {
160 groups->grps[g].nm_ind = NULL;
161 groups->ngrpnr[g] = 0;
162 groups->grpnr[g] = NULL;
167 void init_mtop(gmx_mtop_t *mtop)
169 mtop->name = NULL;
170 mtop->nmoltype = 0;
171 mtop->moltype = NULL;
172 mtop->nmolblock = 0;
173 mtop->molblock = NULL;
174 init_groups(&mtop->groups);
175 init_block(&mtop->mols);
176 open_symtab(&mtop->symtab);
179 void init_top (t_topology *top)
181 int i;
183 top->name = NULL;
184 init_atom (&(top->atoms));
185 init_atomtypes(&(top->atomtypes));
186 init_block(&top->cgs);
187 init_block(&top->mols);
188 init_blocka(&top->excls);
189 open_symtab(&top->symtab);
192 void init_inputrec(t_inputrec *ir)
194 memset(ir,0,(size_t)sizeof(*ir));
197 void stupid_fill_block(t_block *grp,int natom,bool bOneIndexGroup)
199 int i;
201 if (bOneIndexGroup) {
202 grp->nalloc_index = 2;
203 snew(grp->index,grp->nalloc_index);
204 grp->index[0]=0;
205 grp->index[1]=natom;
206 grp->nr=1;
208 else {
209 grp->nalloc_index = natom+1;
210 snew(grp->index,grp->nalloc_index);
211 snew(grp->index,natom+1);
212 for(i=0; (i<=natom); i++)
213 grp->index[i]=i;
214 grp->nr=natom;
218 void stupid_fill_blocka(t_blocka *grp,int natom)
220 int i;
222 grp->nalloc_a = natom;
223 snew(grp->a,grp->nalloc_a);
224 for(i=0; (i<natom); i++)
225 grp->a[i]=i;
226 grp->nra=natom;
228 grp->nalloc_index = natom + 1;
229 snew(grp->index,grp->nalloc_index);
230 for(i=0; (i<=natom); i++)
231 grp->index[i]=i;
232 grp->nr=natom;
235 void copy_blocka(const t_blocka *src,t_blocka *dest)
237 int i;
239 dest->nr = src->nr;
240 dest->nalloc_index = dest->nr + 1;
241 snew(dest->index,dest->nalloc_index);
242 for(i=0; i<dest->nr+1; i++) {
243 dest->index[i] = src->index[i];
245 dest->nra = src->nra;
246 dest->nalloc_a = dest->nra + 1;
247 snew(dest->a,dest->nalloc_a);
248 for(i=0; i<dest->nra+1; i++) {
249 dest->a[i] = src->a[i];
253 void done_block(t_block *block)
255 block->nr = 0;
256 sfree(block->index);
257 block->nalloc_index = 0;
260 void done_blocka(t_blocka *block)
262 block->nr = 0;
263 block->nra = 0;
264 sfree(block->index);
265 if (block->a)
266 sfree(block->a);
267 block->nalloc_index = 0;
268 block->nalloc_a = 0;
271 void done_atom (t_atoms *at)
273 at->nr = 0;
274 at->nres = 0;
275 sfree(at->atom);
276 sfree(at->resinfo);
277 sfree(at->atomname);
280 void done_atomtypes(t_atomtypes *atype)
282 atype->nr = 0;
283 sfree(atype->radius);
284 sfree(atype->vol);
285 sfree(atype->surftens);
286 sfree(atype->gb_radius);
287 sfree(atype->S_hct);
290 void done_moltype(gmx_moltype_t *molt)
292 int f;
294 done_atom(&molt->atoms);
295 done_block(&molt->cgs);
296 done_blocka(&molt->excls);
298 for(f=0; f<F_NRE; f++) {
299 sfree(molt->ilist[f].iatoms);
300 molt->ilist[f].nalloc = 0;
304 void done_molblock(gmx_molblock_t *molb)
306 if (molb->nposres_xA > 0) {
307 molb->nposres_xA = 0;
308 free(molb->posres_xA);
310 if (molb->nposres_xB > 0) {
311 molb->nposres_xB = 0;
312 free(molb->posres_xB);
316 void done_mtop(gmx_mtop_t *mtop,bool bDoneSymtab)
318 int i;
320 if (bDoneSymtab) {
321 done_symtab(&mtop->symtab);
324 sfree(mtop->ffparams.functype);
325 sfree(mtop->ffparams.iparams);
327 for(i=0; i<mtop->nmoltype; i++) {
328 done_moltype(&mtop->moltype[i]);
330 sfree(mtop->moltype);
331 for(i=0; i<mtop->nmolblock; i++) {
332 done_molblock(&mtop->molblock[i]);
334 sfree(mtop->molblock);
335 done_block(&mtop->mols);
338 void done_top(t_topology *top)
340 int i;
342 done_atom (&(top->atoms));
344 /* For GB */
345 done_atomtypes(&(top->atomtypes));
347 done_symtab(&(top->symtab));
348 done_block(&(top->cgs));
349 done_block(&(top->mols));
350 done_blocka(&(top->excls));
353 static void done_pullgrp(t_pullgrp *pgrp)
355 sfree(pgrp->ind);
356 sfree(pgrp->ind_loc);
357 sfree(pgrp->weight);
358 sfree(pgrp->weight_loc);
361 static void done_pull(t_pull *pull)
363 int i;
365 for(i=0; i<pull->ngrp+1; i++) {
366 done_pullgrp(pull->grp);
367 done_pullgrp(pull->dyna);
371 void done_inputrec(t_inputrec *ir)
373 int m;
375 for(m=0; (m<DIM); m++) {
376 if (ir->ex[m].a) sfree(ir->ex[m].a);
377 if (ir->ex[m].phi) sfree(ir->ex[m].phi);
378 if (ir->et[m].a) sfree(ir->et[m].a);
379 if (ir->et[m].phi) sfree(ir->et[m].phi);
382 sfree(ir->opts.nrdf);
383 sfree(ir->opts.ref_t);
384 sfree(ir->opts.annealing);
385 sfree(ir->opts.anneal_npoints);
386 sfree(ir->opts.anneal_time);
387 sfree(ir->opts.anneal_temp);
388 sfree(ir->opts.tau_t);
389 sfree(ir->opts.acc);
390 sfree(ir->opts.nFreeze);
391 sfree(ir->opts.QMmethod);
392 sfree(ir->opts.QMbasis);
393 sfree(ir->opts.QMcharge);
394 sfree(ir->opts.QMmult);
395 sfree(ir->opts.bSH);
396 sfree(ir->opts.CASorbitals);
397 sfree(ir->opts.CASelectrons);
398 sfree(ir->opts.SAon);
399 sfree(ir->opts.SAoff);
400 sfree(ir->opts.SAsteps);
401 sfree(ir->opts.bOPT);
402 sfree(ir->opts.bTS);
404 if (ir->pull) {
405 done_pull(ir->pull);
406 sfree(ir->pull);
410 static void init_ekinstate(ekinstate_t *eks)
412 eks->ekinh_n = 0;
413 eks->ekinh = NULL;
414 eks->dekindl = 0;
415 eks->mvcos = 0;
418 static void init_energyhistory(energyhistory_t *enh)
420 enh->ener_ave = NULL;
421 enh->ener_sum = NULL;
422 enh->ener_sum_sim = NULL;
423 enh->nener = 0;
426 void init_gtc_state(t_state *state,int ngtc)
428 int i;
430 state->ngtc = ngtc;
431 if (state->ngtc > 0) {
432 snew(state->nosehoover_xi, state->ngtc);
433 snew(state->therm_integral,state->ngtc);
434 for(i=0; i<state->ngtc; i++) {
435 state->nosehoover_xi[i] = 0.0;
436 state->therm_integral[i] = 0.0;
438 } else {
439 state->nosehoover_xi = NULL;
440 state->therm_integral = NULL;
445 void init_state(t_state *state,int natoms,int ngtc)
447 int i;
449 state->natoms = natoms;
450 state->nrng = 0;
451 state->flags = 0;
452 state->lambda = 0;
453 clear_mat(state->box);
454 clear_mat(state->box_rel);
455 clear_mat(state->boxv);
456 clear_mat(state->pres_prev);
457 init_gtc_state(state,ngtc);
458 state->nalloc = state->natoms;
459 if (state->nalloc > 0) {
460 snew(state->x,state->nalloc);
461 snew(state->v,state->nalloc);
462 } else {
463 state->x = NULL;
464 state->v = NULL;
466 state->sd_X = NULL;
467 state->cg_p = NULL;
469 init_ekinstate(&state->ekinstate);
471 init_energyhistory(&state->enerhist);
473 state->ddp_count = 0;
474 state->ddp_count_cg_gl = 0;
475 state->cg_gl = NULL;
476 state->cg_gl_nalloc = 0;
479 void done_state(t_state *state)
481 if (state->nosehoover_xi) sfree(state->nosehoover_xi);
482 if (state->x) sfree(state->x);
483 if (state->v) sfree(state->v);
484 if (state->sd_X) sfree(state->sd_X);
485 if (state->cg_p) sfree(state->cg_p);
486 state->nalloc = 0;
487 if (state->cg_gl) sfree(state->cg_gl);
488 state->cg_gl_nalloc = 0;
491 static void do_box_rel(t_inputrec *ir,matrix box_rel,matrix b,bool bInit)
493 int d,d2;
495 for(d=YY; d<=ZZ; d++) {
496 for(d2=XX; d2<=(ir->epct==epctSEMIISOTROPIC ? YY : ZZ); d2++) {
497 /* We need to check if this box component is deformed
498 * or if deformation of another component might cause
499 * changes in this component due to box corrections.
501 if (ir->deform[d][d2] == 0 &&
502 !(d == ZZ && d2 == XX && ir->deform[d][YY] != 0 &&
503 (b[YY][d2] != 0 || ir->deform[YY][d2] != 0))) {
504 if (bInit) {
505 box_rel[d][d2] = b[d][d2]/b[XX][XX];
506 } else {
507 b[d][d2] = b[XX][XX]*box_rel[d][d2];
514 void set_box_rel(t_inputrec *ir,t_state *state)
516 /* Make sure the box obeys the restrictions before we fix the ratios */
517 correct_box(NULL,0,state->box,NULL);
519 clear_mat(state->box_rel);
521 if (PRESERVE_SHAPE(*ir))
522 do_box_rel(ir,state->box_rel,state->box,TRUE);
525 void preserve_box_shape(t_inputrec *ir,matrix box_rel,matrix b)
527 if (PRESERVE_SHAPE(*ir))
528 do_box_rel(ir,box_rel,b,FALSE);
531 void init_t_atoms(t_atoms *atoms, int natoms, bool bPdbinfo)
533 atoms->nr=natoms;
534 atoms->nres=0;
535 snew(atoms->atomname,natoms);
536 atoms->atomtype=NULL;
537 atoms->atomtypeB=NULL;
538 snew(atoms->resinfo,natoms);
539 snew(atoms->atom,natoms);
540 if (bPdbinfo)
541 snew(atoms->pdbinfo,natoms);
542 else
543 atoms->pdbinfo=NULL;
546 void t_atoms_set_resinfo(t_atoms *atoms,int atom_ind,t_symtab *symtab,
547 const char *resname,int resnr,unsigned char ic,
548 unsigned char chain)
550 t_resinfo *ri;
552 ri = &atoms->resinfo[atoms->atom[atom_ind].resind];
553 ri->name = put_symtab(symtab,resname);
554 ri->nr = resnr;
555 ri->ic = ic;
556 ri->chain = chain;
559 void free_t_atoms(t_atoms *atoms,bool bFreeNames)
561 int i;
563 if (bFreeNames) {
564 for(i=0; i<atoms->nr; i++) {
565 sfree(*atoms->atomname[i]);
566 *atoms->atomname[i]=NULL;
568 for(i=0; i<atoms->nres; i++) {
569 sfree(*atoms->resinfo[i].name);
570 *atoms->resinfo[i].name=NULL;
573 sfree(atoms->atomname);
574 /* Do we need to free atomtype and atomtypeB as well ? */
575 sfree(atoms->resinfo);
576 sfree(atoms->atom);
577 if (atoms->pdbinfo)
578 sfree(atoms->pdbinfo);
579 atoms->nr=0;
580 atoms->nres=0;
583 real max_cutoff(real cutoff1,real cutoff2)
585 if (cutoff1 == 0 || cutoff2 == 0)
587 return 0;
589 else
591 return max(cutoff1,cutoff2);