1 /*****
2 This file is part of the Babel Program
3 Copyright (C) 1992-96 W. Patrick Walters and Matthew T. Stahl
4 All Rights Reserved
5 All Rights Reserved
6 All Rights Reserved
7 All Rights Reserved
8 
9 For more information please contact :
10 
11 babel@mercury.aichem.arizona.edu
12 --------------------------------------------------------------------------------
13 
14 FILE : renum.c
15 AUTHOR(S) : Pat Walters
16 DATE : 1-95
17 PURPOSE : routines to renumber a structure in order to produce a more reasonable
18 Z-matrix
19 
20 ******/
21 
22 #include "bbltyp.h"
23 
renumber(ums_type * mol)24 ums_type *renumber(ums_type *mol)
25 {
26   vect_type v;
27 
28   center_at_origin(mol,&v);
29   find_dist_from_origin(mol);
30   sort_by_dist_to_origin(mol);
31   mol = build_new_ums(mol,Atoms);
32   return(mol);
33 }
34 
find_dist_from_origin(ums_type * mol)35 void find_dist_from_origin(ums_type *mol)
36 {
37   int i;
38   coord_type origin;
39 
40   origin.x = 0.0;
41   origin.y = 0.0;
42   origin.z = 0.0;
43 
44   for (i = 1; i <= Atoms; i++)
45   {
46     Double(i) = distance(Point(i),origin);
47   }
48 }
49 
sort_by_dist_to_origin(ums_type * mol)50 void sort_by_dist_to_origin(ums_type *mol)
51 {
52   int i,j;
53   temp_atom_rec *temp;
54 
55   temp = (temp_atom_rec *)malloc(Atoms * sizeof(temp_atom_rec));
56 
57   if (!temp)
58     fatal_error("Error allocating memory in sort_by_dist_to_origin");
59 
60   for (i = 0; i < Atoms; i++)
61   {
62     j = i + 1;
63     temp[i].x = X(j);
64     temp[i].y = Y(j);
65     temp[i].z = Z(j);
66     temp[i].num = j;
67     temp[i].dist = Double(j);
68   }
69 
70   qsort(temp,Atoms,sizeof(temp_atom_rec),QSORT_PROTO sort_by_dist);
71 
72   for (i = 0; i < Atoms; i++)
73   {
74     printf("%d %10.3f%10.3f%10.3f - %10.3f\n",
75 	   temp[i].num,temp[i].x,temp[i].y,temp[i].z,temp[i].dist);
76   }
77 
78   j = 1;
79   for (i = 0; i < Atoms; i++)
80   {
81     if (Type(temp[i].num)[0] != 'H')
82     {
83       Redo(temp[i].num) = j;
84       j++;
85     }
86   }
87   for (i = 0; i < Atoms; i++)
88   {
89     if (Type(temp[i].num)[0] == 'H')
90     {
91       Redo(temp[i].num) = j;
92       j++;
93     }
94   }
95 }
96 
97 
sort_by_dist(temp_atom_rec * a,temp_atom_rec * b)98 int sort_by_dist(temp_atom_rec *a, temp_atom_rec *b)
99 {
100   if (a->dist > b->dist)
101     return(1);
102   if (a->dist < b->dist)
103     return(-1);
104   return(0);
105 }
106 
107 
108