| Bsoft 2.0.5 Bernard's software package

model_path.cpp
Go to the documentation of this file.
1
9 #include "model_util.h"
10 #include "model_transform.h"
11 #include "model_select.h"
12 #include "model_compare.h"
13 #include "mol_transform.h"
14 #include "mol_compare.h"
15 #include "mol_util.h"
16 #include "symmetry.h"
17 #include "Matrix3.h"
18 #include "random_numbers.h"
19 #include "linked_list.h"
20 #include "utilities.h"
21
22 // Declaration of global variables
23 extern int verbose; // Level of output to the screen
24
25
26
36 Matrix model_shortest_path(Bmodel* model)
37 {
38 Matrix mat = model_adjacency_matrix(model);
39
40 int h, i, j, k, ncomp;
41 double s = 0;
42 Bcomponent* comp;
43 for ( ncomp = 0, comp = model->comp; comp; comp = comp->next ) ncomp++;
44
45 if ( verbose & VERB_FULL )
46 cout << "Cycle: Coverage" << endl;
47 for ( h=2; h<10 && s<ncomp*(ncomp-1); h++ ) {
48 for ( i=0; i<ncomp; i++ ) {
49 for ( j=0; j<ncomp; j++ ) {
50 if ( i!=j && mat[i][j] && mat[i][j]<h ) {
51 for ( k=0; k<ncomp; k++ ) {
52 s = mat[i][j] + mat[k][j];
53 if ( i!=k && j!=k && mat[k][j] && s==h && mat[k][i] == 0 )
54 mat[k][i] = s;
55 }
56 }
57 }
58 }
59 for ( i=0, s=0; i<ncomp; i++ )
60 for ( j=0; j<ncomp; j++ ) if ( mat[i][j] ) s++;
61 if ( verbose & VERB_FULL )
62 cout << h << ":\t" << s << endl;
63 }
64
65 // if ( verbose & VERB_FULL )
66 // print_matrix(ncomp, mat);
67
68 return mat;
69 }
70
82 double model_wiener_index(Bmodel* model)
83 {
84 int i, j, n, ncomp;
85 double wi, wia;
86 Matrix mat;
87 Bmodel* mp;
88 Bcomponent* comp;
89
90 if ( verbose & VERB_RESULT )
91 cout << "Model\tWienerIndex" << endl;
92 for ( n=0, wia=0, mp = model; mp; mp = mp->next, n++ ) {
93 for ( ncomp = 0, comp = mp->comp; comp; comp = comp->next ) ncomp++;
94 if ( ncomp ) {
95 mat = model_shortest_path(mp);
96 for ( i=0, wi=0; i<ncomp; i++ )
97 for ( j=0; j<ncomp; j++ ) wi += (long) mat[i][j];
98 wi /= 2;
99 wia += wi;
100 }
101 if ( verbose & VERB_RESULT )
102 cout << mp->id << tab << wi << endl;
103 }
104
105 if ( n ) wia /= n;
106
107 if ( verbose & VERB_RESULT )
108 cout << "Avg:\t" << wia << endl << endl;
109
110 return wia;
111 }
112
113 int comp_link_check(Bcomponent* comp, Bcomponent* comp2)
114 {
115 int j;
116
117 for ( j=0; j<MAXLINK && comp->link[j]; j++ )
118 if ( comp->link[j] == comp2 ) return 1;
119
120 return 0;
121 }
122
123 int comp_link_check(Bmodel* model, Bstring& id1, Bstring& id2)
124 {
125 Bcomponent* comp, *comp2;
126
127 for ( comp = model->comp; comp && comp->id != id1; comp = comp->next ) ;
128
129 for ( comp2 = model->comp; comp2 && comp2->id != id2; comp2 = comp2->next ) ;
130
131 return comp_link_check(comp, comp2);
132 }
133
134 Blink* model_link_add(Bmodel* model, Bstring& id1, Bstring& id2)
135 {
136 Bcomponent* comp, *comp2;
137
138 for ( comp = model->comp; comp && comp->id != id1; comp = comp->next ) ;
139
140 for ( comp2 = model->comp; comp2 && comp2->id != id2; comp2 = comp2->next ) ;
141
142 return link_add(&model->link, comp, comp2);
143 }
144
145 int model_check_path(Bmodel* model, Bcomponent** path)
146 {
147 int same(0), i, n;
148 Bmodel* modpath;
149 Bcomponent* comp;
150
151 for ( n=0, comp = model->comp; comp; comp = comp->next ) n++;
152
153 for ( modpath = model->next; modpath && same<n; modpath = modpath->next ) {
154 for ( same=0, i=1; i<n; i++ )
155 if ( comp_link_check(modpath, path[i-1]->id, path[i]->id) ) same++;
156 if ( comp_link_check(modpath, path[0]->id, path[n-1]->id) ) same++;
157 }
158
159 if ( same < n ) return 1;
160
161 return 0;
162 }
163
164 double model_add_path(Bmodel* model, Bcomponent** path)
165 {
166 int i, n;
167 double len(0);
168 Bcomponent* comp;
169 Blink* link;
170
171 for ( n=0, comp = model->comp; comp; comp = comp->next ) n++;
172
173 Bmodel* modpath = model_copy(model);
174
175 model_link_list_kill(modpath);
176 modpath->link = NULL;
177
178 for ( i=1; i<n; i++ ) {
179 link = model_link_add(modpath, path[i-1]->id, path[i]->id);
180 len += link->length;
181 }
182 link = model_link_add(modpath, path[0]->id, path[n-1]->id);
183 len += link->length;
184
185 for ( ; model->next; model = model->next ) ;
186 model->next = modpath;
187
188 return len;
189 }
190
191 int comp_pick_next(int i, int n, Bcomponent* comp, Bcomponent** path, Bmodel* model)
192 {
193 path[i] = comp;
194
195 int j, k, use;
196 double len;
197 Bcomponent* comp_link;
198
199 for ( j=0; j<MAXLINK && comp->link[j]; j++ ) {
200 comp_link = comp->link[j];
201 for ( use=1, k=0; k<i && use; k++ ) // Check if already in path
202 if ( comp_link == path[k] ) use = 0;
203 if ( use ) // Add to path and check next
204 comp_pick_next(i+1, n, comp_link, path, model);
205 else {
206 if ( i+2 > n && comp_link_check(path[0], path[i]) ) {
207 // Check if path already exists
208 use = model_check_path(model, path);
209 // If not, add as a new model
210 if ( use ) {
211 len = model_add_path(model, path);
212 if ( verbose ) {
213 cout << i+1 << ":";
214 for ( k=0; k<=i; k++ ) cout << " " << path[k]->id;
215 cout << " " << path[0]->id;
216 cout << " " << len << endl;
217 }
218 }
219 }
220 }
221 }
222
223 return i;
224 }
225
239 Bmodel* model_hamiltonian_cycle(Bmodel* model)
240 {
241 int i, n, m;
242 Bcomponent* comp;
243 Blink* link;
244
245 for ( n=0, comp = model->comp; comp; comp = comp->next ) n++;
246 for ( m=0, link = model->link; link; link = link->next ) m++;
247
248 if ( verbose ) {
249 cout << "Finding Hamiltonian cycles:" << endl;
250 cout << "Components: " << n << endl;
251 cout << "Starting links: " << m << endl;
252 cout << "Paths:" << endl;
253 }
254
255 Bcomponent** path = new Bcomponent*[n];
256 for ( i=0; i<n; i++ ) path[i] = NULL;
257
258 comp_pick_next(0, n, model->comp, path, model);
259
260 delete[] path;
261
262 Bmodel* modpath = model->next;
263 model->next = NULL;
264
265 if ( verbose )
266 cout << endl;
267
268 return modpath;
269 }
270
MAXLINK
#define MAXLINK
Definition: rwmodel.h:14
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Blink
Definition: rwmodel.h:82
verbose
int verbose
Bmodel::id
Bstring id
Definition: rwmodel.h:121
symmetry.h
Header file for general symmetry functions.
utilities.h
Header file for general utilities.
model_util.h
Library routines used for model processing.
comp_pick_next
int comp_pick_next(int i, int n, Bcomponent *comp, Bcomponent **path, Bmodel *model)
Definition: model_path.cpp:191
link_add
Blink * link_add(Blink **link, Bcomponent *comp1, Bcomponent *comp2, double length, double radius)
Adds a component link to a linked list.
Definition: rwmodel.cpp:315
mol_transform.h
Library routines used for atomic coordinate transformations.
model_copy
Bmodel * model_copy(Bmodel *model)
Copies a model.
Definition: rwmodel.cpp:685
Matrix3.h
3x3 Matrix manipulation functions
comp_link_check
int comp_link_check(Bcomponent *comp, Bcomponent *comp2)
Definition: model_path.cpp:113
mol_util.h
Library routines used for atomic coordinates.
model_link_add
Blink * model_link_add(Bmodel *model, Bstring &id1, Bstring &id2)
Definition: model_path.cpp:134
linked_list.h
Header file for generalized linked list functions.
Blink::length
float length
Definition: rwmodel.h:86
Blink::next
Blink * next
Definition: rwmodel.h:83
model_add_path
double model_add_path(Bmodel *model, Bcomponent **path)
Definition: model_path.cpp:164
use
const char * use[]
Definition: b3v.cpp:19
model_hamiltonian_cycle
Bmodel * model_hamiltonian_cycle(Bmodel *model)
Calculates the Hamiltonian cycles in a model.
Definition: model_path.cpp:239
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:105
model
Bmodel * model
Definition: bshow.cpp:32
random_numbers.h
Header file for functions for creating random images.
Matrix
Definition: Matrix.h:21
Bstring
Definition: Bstring.h:29
Bcomponent::link
Bcomponent * link[MAXLINK]
Definition: rwmodel.h:55
Bmodel::link
Blink * link
Definition: rwmodel.h:133
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
model_adjacency_matrix
Matrix model_adjacency_matrix(Bmodel *model)
Constructs the adjacency matrix for a model.
Definition: model_compare.cpp:152
model_check_path
int model_check_path(Bmodel *model, Bcomponent **path)
Definition: model_path.cpp:145
Bmodel
Definition: rwmodel.h:119
model_link_list_kill
int model_link_list_kill(Bmodel *model)
Deallocates memory for a list of component links.
Definition: rwmodel.cpp:950
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
model_select.h
Header file for reading and writing atomic model files.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
model_transform.h
Library routines used for model transformation.
model_compare.h
Functions to compare models and components.
model_shortest_path
Matrix model_shortest_path(Bmodel *model)
Calculates the shortest path between every pair of vertices.
Definition: model_path.cpp:36
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
mol_compare.h
Library routines used to compare sets of atomic coordinates.
model_wiener_index
double model_wiener_index(Bmodel *model)
Calculates the Wiener index.
Definition: model_path.cpp:82
Bcomponent
Definition: rwmodel.h:51