File: mkconn.c

package info (click to toggle)
viewmol 2.3-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,424 kB
  • ctags: 2,239
  • sloc: ansic: 29,098; sh: 909; makefile: 513; python: 238
file content (411 lines) | stat: -rw-r--r-- 13,829 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*******************************************************************************
*                                                                              *
*                                   Viewmol                                    *
*                                                                              *
*                               M K C O N N . C                                *
*                                                                              *
*                 Copyright (c) Joerg-R. Hill, December 2000                   *
*                                                                              *
********************************************************************************
*
* $Id: mkconn.c,v 1.5 2000/12/10 15:11:48 jrh Exp $
* $Log: mkconn.c,v $
* Revision 1.5  2000/12/10 15:11:48  jrh
* Release 2.3
*
* Revision 1.4  1999/05/24 01:26:32  jrh
* Release 2.2.1
*
* Revision 1.3  1999/02/07 21:52:40  jrh
* Release 2.2
*
* Revision 1.2  1998/01/26 00:48:40  jrh
* Release 2.1
*
* Revision 1.1  1996/12/10  18:42:09  jrh
* Initial revision
*
*/
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<X11/Intrinsic.h>
#include<Xm/Xm.h>
#include "viewmol.h"
#include "dialog.h"

extern struct MOLECULE *molecules;
extern struct WINDOW windows[];
extern double amplitude, forceScale, hbondThreshold;
extern int nmolecule, ne;
extern int debug, bondType;
extern Widget topShell;

void setWindowSize(double);

extern struct SAVE *saveGeometry(struct MOLECULE *);
extern double bondLength(struct MOLECULE *, int, int);
extern void GetMessageBoxButton(Widget, XtPointer, caddr_t);
extern char *getStringResource(Widget, char *);
extern void *getmem(size_t, size_t);
extern void *expmem(void *, size_t, size_t);
extern void fremem(void **);
extern void bondOrder(struct MOLECULE *, struct BOND *, int *, int);
extern void findConjugation(struct MOLECULE *, struct BOND *, int *, int, int);
extern void calculateShift(struct MOLECULE *, struct BOND *, int *, int, int,
				   int);
extern void pixelToWorld(int, double *, double *);
extern void redraw(int);

int makeConnectivity(struct MOLECULE *mol, int setWindow, int saveCoord)
{
  Dimension width, height;
  double mass, r1, r2, box, xpix, ypix;
  int *connectivity, maxBondsPerAtom=8, uc=mol->existsUnitCell, found, nbonds;
  size_t mnb=20;
  register int i, j, k, l;

  mol->transx=0.0;
  mol->transy=0.0;
  mol->transz=0.0;
  mass=0.0;
  for (i=0; i<mol->na; i++)
  {
    mol->transx+=mol->atoms[i].mass*mol->atoms[i].x;
    mol->transy+=mol->atoms[i].mass*mol->atoms[i].y;
    mol->transz+=mol->atoms[i].mass*mol->atoms[i].z;
    mass+=mol->atoms[i].mass;
  }
  mol->transx=-mol->transx/mass;
  mol->transy=-mol->transy/mass;
  mol->transz=-mol->transz/mass;
  if (debug == 1)
    printf("Shifting molecule by %f %f %f.\n", mol->transx, mol->transy,
           mol->transz);
  for (i=0; i<mol->na; i++)
  {
    mol->atoms[i].x+=mol->transx;
    mol->atoms[i].y+=mol->transy;
    mol->atoms[i].z+=mol->transz;
  }
  if (saveCoord)
  {
    if (mol->coord != NULL) fremem((void **)&(mol->coord));
    mol->coord=saveGeometry(mol);
  }

  repeat:
  connectivity=(int *)getmem((size_t)(maxBondsPerAtom*mol->na), sizeof(int));
  box=0.0;
  for (i=0; i<mol->na; i++)
  {
    mol->atoms[i].nbonds=0;
    mol->atoms[i].hbonds=0;
    if (strncmp(mol->atoms[i].name, "Uc", 2))
    {
      for (j=0; j<mol->na; j++)
      {
        if (i != j && strncmp(mol->atoms[j].name, "Uc", 2))
        {
          r1=bondLength(mol, i, j);
          r2=mol->atoms[i].radScale*mol->atoms[i].rad
	      +mol->atoms[j].radScale*mol->atoms[j].rad;
          box=box > r1+r2 ? box : r1+r2;
          if (r1 < r2)
          {
            mol->atoms[i].nbonds++;
            if (mol->atoms[i].nbonds > maxBondsPerAtom)
            {
              maxBondsPerAtom+=5;
              fremem((void **)&connectivity);
              goto repeat;
            }
            connectivity[maxBondsPerAtom*i+mol->atoms[i].nbonds-1]=j;
          }
          else
          {
            if (((!strcmp(mol->atoms[i].name, "H") &&
                   strcmp(mol->atoms[j].name, "H") &&
                   strcmp(mol->atoms[j].name, "C")) ||
                 (!strcmp(mol->atoms[j].name, "H") &&
                   strcmp(mol->atoms[i].name, "H") &&
                   strcmp(mol->atoms[i].name, "C"))) && r1 < hbondThreshold)
            {
              mol->atoms[i].nbonds++;
              mol->atoms[i].hbonds++;
              if (mol->atoms[i].nbonds > maxBondsPerAtom)
              {
                maxBondsPerAtom+=5;
                fremem((void **)&connectivity);
                goto repeat;
              }
              connectivity[maxBondsPerAtom*i+mol->atoms[i].nbonds-1]=(-j);
            }
          }
        }
      } 
    }
    else
    {
      uc=i;
      mol->atoms[i].nbonds=3;
    }
  }
  if (box == 0.0) box=1.3*sqrt(3.)*mol->atoms[0].rad;
  box*=1.05/sqrt(3.);
  XtVaGetValues(windows[VIEWER].widget, XtNwidth, &width, XtNheight, &height, NULL);
  if (setWindow) setWindowSize(box);
  if (mol->bondShift == 0.0)
  {
    pixelToWorld(VIEWER, &xpix, &ypix);
    mol->bondShift=((double)width*xpix+(double)height*ypix)*0.01;
  }

/* Fill the bonds structure, but check whether user has deleted a particular
   bond. If an atom is only bond to one other atom assign bond order
   immediately since there can be no dependance on any other atom. */

  if (mol->bonds) fremem((void **)&(mol->bonds));
  mol->bonds=(struct BOND *)getmem(mnb, sizeof(struct BOND));
  mol->nb=0;
  for (i=0; i<mol->na; i++)
  {
    nbonds=mol->atoms[i].nbonds;
    for (j=0; j<nbonds; j++)
    {
      k=connectivity[maxBondsPerAtom*i+j];
      if (i < abs(k))
      {
        found=FALSE;
        for (l=0; l<mol->nbDeleted; l++)
        {
          if ((mol->deletedBonds[2*l] == i && mol->deletedBonds[2*l+1] == abs(k)) ||
              (mol->deletedBonds[2*l+1] == i && mol->deletedBonds[2*l] == abs(k)))
          {
            found=TRUE;
            break;
          }
        }
        if (!found)
        {
          mol->bonds[mol->nb].first=i;
          mol->bonds[mol->nb].second=abs(k);
          if (bondType != SINGLE_BONDS)
          {
            if (mol->atoms[i].nbonds == 1 || mol->atoms[k].nbonds == 1)
              bondOrder(mol, &(mol->bonds[mol->nb]), connectivity,
                        maxBondsPerAtom);
            else
              mol->bonds[mol->nb].order=0;
          }
          else
            mol->bonds[mol->nb].order=1;
          if (k < 0) mol->bonds[mol->nb].order=(-1);
          mol->bonds[mol->nb++].frac=mol->atoms[i].rad/bondLength(mol, i, abs(k));
          if (mol->nb >= mnb)
          {
            mnb+=20;
            mol->bonds=(struct BOND *)expmem((void *)mol->bonds, mnb,
                                  sizeof(struct BOND));
          }
        }
      }
    }
  }

/* Correct nbonds if bonds have been deleted. This cannot be done before since
   nbonds is used as indicator for the number of entries in connectivity. */

  for (i=0; i<mol->nbDeleted; i++)
  {
    mol->atoms[mol->deletedBonds[2*i]].nbonds--;
    mol->atoms[mol->deletedBonds[2*i+1]].nbonds--; 
  }

/* Add user defined bonds to the bonds array, but check whether that bond
   was already assigned by the previous loop. */

  for (i=0; i<mol->nbAdded; i++)
  {
    found=FALSE;
    for (j=0; j<mol->nb; j++)
    {
      if ((mol->addedBonds[3*i] == mol->bonds[j].first &&
           mol->addedBonds[3*i+1] == mol->bonds[j].second) ||
          (mol->addedBonds[3*i] == mol->bonds[j].second &&
           mol->addedBonds[3*i+1] == mol->bonds[j].first))
      {
        found=TRUE;
        mol->bonds[j].order=mol->addedBonds[3*i+2];
        if (mol->bonds[j].order > 1)
          calculateShift(mol, &mol->bonds[j], connectivity, maxBondsPerAtom,
                         mol->bonds[j].first, mol->bonds[j].second);
        break;
      }
    }
    if (!found)
    {
      mol->atoms[mol->addedBonds[3*i]].nbonds++;
      mol->atoms[mol->addedBonds[3*i+1]].nbonds++;
      mol->bonds[mol->nb].first=mol->addedBonds[3*i];
      mol->bonds[mol->nb].second=mol->addedBonds[3*i+1];
      mol->bonds[mol->nb].order=mol->addedBonds[3*i+2];
      if (mol->addedBonds[3*i+2] > 1)
        calculateShift(mol, &mol->bonds[mol->nb], connectivity, maxBondsPerAtom,
                       mol->addedBonds[3*i], mol->addedBonds[3*i+1]);
      mol->bonds[mol->nb++].frac=mol->atoms[mol->addedBonds[3*i]].rad
                                /bondLength(mol, mol->addedBonds[3*i],
                                mol->addedBonds[3*i+1]);
      if (mol->nb >= mnb)
      {
        mnb+=20;
        mol->bonds=(struct BOND *)expmem((void *)mol->bonds, mnb,
                                         sizeof(struct BOND));
      }
    }
  }

/* Assign bond orders for the remaining bonds */

  for (i=0; i<mol->nb; i++)
  {
    if (mol->bonds[i].order == 0)
    {
      if (bondType != SINGLE_BONDS)
        bondOrder(mol, &mol->bonds[i], connectivity, maxBondsPerAtom);
      else
        mol->bonds[i].order=1;
    }
  }

/* Check if there are conjugated double bonds */

  if (bondType == CONJUGATION) findConjugation(mol, mol->bonds,
                                 connectivity, maxBondsPerAtom, mol->nb);

  fremem((void **)&connectivity);
  if (mol->nb > 0)
    mol->bonds=(struct BOND *)expmem((void *)mol->bonds,
                          mol->nb, sizeof(struct BOND));

/* Add unit cell edges as "bonds" */

  if (uc)
  {
    mol->bonds=(struct BOND *)expmem((void *)mol->bonds, mol->nb+12, sizeof(struct BOND));
    mol->bonds[mol->nb].first=uc-7;
    mol->bonds[mol->nb++].second=uc-6;
    mol->bonds[mol->nb].first=uc-7;
    mol->bonds[mol->nb++].second=uc-5;
    mol->bonds[mol->nb].first=uc-7;
    mol->bonds[mol->nb++].second=uc-4;
    mol->bonds[mol->nb].first=uc-6;
    mol->bonds[mol->nb++].second=uc-3;
    mol->bonds[mol->nb].first=uc-6;
    mol->bonds[mol->nb++].second=uc;
    mol->bonds[mol->nb].first=uc-5;
    mol->bonds[mol->nb++].second=uc-1;
    mol->bonds[mol->nb].first=uc-5;
    mol->bonds[mol->nb++].second=uc;
    mol->bonds[mol->nb].first=uc-4;
    mol->bonds[mol->nb++].second=uc-3;
    mol->bonds[mol->nb].first=uc-4;
    mol->bonds[mol->nb++].second=uc-1;
    mol->bonds[mol->nb].first=uc-3;
    mol->bonds[mol->nb++].second=uc-2;
    mol->bonds[mol->nb].first=uc-2;
    mol->bonds[mol->nb++].second=uc-1;
    mol->bonds[mol->nb].first=uc-2;
    mol->bonds[mol->nb++].second=uc;
    for (i=mol->nb-12; i<mol->nb; i++)
    {
      mol->bonds[i].order=1;
      mol->bonds[i].frac=0.5;
    }
  }
  if (debug == 1)
  {
    printf("Bonds:\n");
    for (i=0; i<mol->nb; i++)
    {
      printf("%d: %s(%d)-%s(%d) = %d, frac = %f", i, mol->atoms[mol->bonds[i].first].name,
             mol->bonds[i].first+1, mol->atoms[mol->bonds[i].second].name,
             mol->bonds[i].second+1, mol->bonds[i].order, mol->bonds[i].frac);
      if (abs(mol->bonds[i].order) != 1)
        printf(", multiple bond shift %f %f %f\n", mol->bonds[i].x,
               mol->bonds[i].y, mol->bonds[i].z);
      else
        printf("\n");
    }
  }
  return(TRUE);
}

void setBondType(Widget widget, caddr_t which, XmToggleButtonCallbackStruct *data)
{
  register int i;

  if (data->set)
  {
    bondType=(int)which;
    for (i=0; i<nmolecule; i++)
      makeConnectivity(&molecules[i], FALSE, FALSE);
    redraw(VIEWER);
  }
}

void setWindowSize(double box)
{
  Dimension width, height;
  double r;

  amplitude=forceScale=box;
  windows[VIEWER].left=(-box);
  windows[VIEWER].right=box;
  windows[VIEWER].bottom=(-box);
  windows[VIEWER].top=box;
  windows[VIEWER].near=(-box);
  windows[VIEWER].far=box;

  XtVaGetValues(windows[VIEWER].widget, XtNwidth, &width, XtNheight, &height, NULL);
  if (width > height)
  {
    r=((double)width/(double)height-1.0)*windows[VIEWER].near;
    windows[VIEWER].left=windows[VIEWER].near+r;
    windows[VIEWER].right=windows[VIEWER].far-r;
    windows[VIEWER].bottom=windows[VIEWER].near;
    windows[VIEWER].top=windows[VIEWER].far;
  }
  else
  {
    r=((double)height/(double)width-1.0)*windows[VIEWER].near;             
    windows[VIEWER].left=windows[VIEWER].near;                               
    windows[VIEWER].right=windows[VIEWER].far;
    windows[VIEWER].bottom=windows[VIEWER].near+r;                         
    windows[VIEWER].top=windows[VIEWER].far-r;
  }
}
/*
void deleteBond(struct MOLECULE *mol, int i, int j)
{
  register int i;

  for (i=0; i<mol->nb; i++)
  {
    if ((molecules[imol].bonds[i].first == clicked[0] && molecules[imol].bonds[i].second == clicked[1])
      ||(molecules[imol].bonds[i].first == clicked[1] && molecules[imol].bonds[i].second == clicked[0]))
    {
      for (j=i+1; j<molecules[imol].nb; j++)
      {
                                      molecules[imol].bonds[j-1].first=molecules[imol].bonds[j].first;
                                      molecules[imol].bonds[j-1].second=molecules[imol].bonds[j].second;
                                      molecules[imol].bonds[j-1].order=molecules[imol].bonds[j].order;
                                      molecules[imol].bonds[j-1].frac=molecules[imol].bonds[j].frac;
                                      molecules[imol].bonds[j-1].x=molecules[imol].bonds[j].x;
                                      molecules[imol].bonds[j-1].y=molecules[imol].bonds[j].y;
                                      molecules[imol].bonds[j-1].z=molecules[imol].bonds[j].z;
                                    }
                                    molecules[imol].nb--;
                                    molecules[imol].bonds=(struct BOND *)expmem((void *)molecules[imol].bonds,
                                                            molecules[imol].nb, sizeof(struct BOND));
                                  } */