1 |
#include <stdlib.h> |
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
#include <iostream> |
5 |
|
6 |
#include "MoleculeStamp.hpp" |
7 |
|
8 |
char MoleculeStamp::errMsg[500]; |
9 |
|
10 |
MoleculeStamp::MoleculeStamp(){ |
11 |
|
12 |
n_atoms = 0; |
13 |
n_bonds = 0; |
14 |
n_bends = 0; |
15 |
n_torsions = 0; |
16 |
n_rigidbodies = 0; |
17 |
|
18 |
unhandled = NULL; |
19 |
atoms = NULL; |
20 |
bonds = NULL; |
21 |
bends = NULL; |
22 |
torsions = NULL; |
23 |
rigidBodies = NULL; |
24 |
|
25 |
have_name = 0; |
26 |
have_atoms = 0; |
27 |
have_bonds = 0; |
28 |
have_bends = 0; |
29 |
have_torsions = 0; |
30 |
have_rigidbodies = 0; |
31 |
|
32 |
} |
33 |
|
34 |
MoleculeStamp::~MoleculeStamp(){ |
35 |
int i; |
36 |
|
37 |
if( unhandled != NULL) delete unhandled; |
38 |
|
39 |
if( rigidBodies != NULL ) { |
40 |
for( i=0; i<n_rigidbodies; i++ ) delete rigidBodies[i]; |
41 |
} |
42 |
|
43 |
if( atoms != NULL ){ |
44 |
for( i=0; i<n_atoms; i++ ) delete atoms[i]; |
45 |
} |
46 |
|
47 |
if( bonds != NULL ){ |
48 |
for( i=0; i<n_bonds; i++ ) delete bonds[i]; |
49 |
} |
50 |
|
51 |
if( bends != NULL ){ |
52 |
for( i=0; i<n_bends; i++ ) delete bends[i]; |
53 |
} |
54 |
|
55 |
if( torsions != NULL ){ |
56 |
for( i=0; i<n_torsions; i++ ) delete torsions[i]; |
57 |
} |
58 |
|
59 |
} |
60 |
|
61 |
char* MoleculeStamp::assignString( char* lhs, char* rhs ){ |
62 |
|
63 |
if( !strcmp( lhs, "name" ) ){ |
64 |
strcpy( name, rhs ); |
65 |
have_name = 1; |
66 |
} |
67 |
else{ |
68 |
if( unhandled == NULL ) unhandled = new LinkedAssign( lhs, rhs ); |
69 |
else unhandled->add( lhs, rhs ); |
70 |
have_extras = 1; |
71 |
} |
72 |
return NULL; |
73 |
} |
74 |
|
75 |
char* MoleculeStamp::assignDouble( char* lhs, double rhs ){ |
76 |
int i; |
77 |
|
78 |
if( !strcmp( lhs, "nAtoms" ) ){ |
79 |
n_atoms = (int)rhs; |
80 |
|
81 |
if( have_atoms ){ |
82 |
sprintf( errMsg, |
83 |
"MoleculeStamp error, n_atoms already declared" |
84 |
" for molecule: %s\n", |
85 |
name); |
86 |
return strdup( errMsg ); |
87 |
} |
88 |
have_atoms = 1; |
89 |
atoms = new AtomStamp*[n_atoms]; |
90 |
for( i=0; i<n_atoms; i++ ) atoms[i] = NULL; |
91 |
} |
92 |
|
93 |
else if( !strcmp( lhs, "nBonds" ) ){ |
94 |
n_bonds = (int)rhs; |
95 |
|
96 |
if( have_bonds ){ |
97 |
sprintf( errMsg, |
98 |
"MoleculeStamp error, n_bonds already declared for" |
99 |
" molecule: %s\n", |
100 |
name); |
101 |
return strdup( errMsg ); |
102 |
} |
103 |
have_bonds = 1; |
104 |
bonds = new BondStamp*[n_bonds]; |
105 |
for( i=0; i<n_bonds; i++ ) bonds[i] = NULL; |
106 |
} |
107 |
|
108 |
else if( !strcmp( lhs, "nBends" ) ){ |
109 |
n_bends = (int)rhs; |
110 |
|
111 |
if( have_bends ){ |
112 |
sprintf( errMsg, |
113 |
"MoleculeStamp error, n_bends already declared for" |
114 |
" molecule: %s\n", |
115 |
name); |
116 |
return strdup( errMsg ); |
117 |
} |
118 |
have_bends = 1; |
119 |
bends = new BendStamp*[n_bends]; |
120 |
for( i=0; i<n_bends; i++ ) bends[i] = NULL; |
121 |
} |
122 |
|
123 |
else if( !strcmp( lhs, "nTorsions" ) ){ |
124 |
n_torsions = (int)rhs; |
125 |
|
126 |
if( have_torsions ){ |
127 |
sprintf( errMsg, |
128 |
"MoleculeStamp error, n_torsions already declared for" |
129 |
" molecule: %s\n", |
130 |
name ); |
131 |
return strdup( errMsg ); |
132 |
} |
133 |
have_torsions = 1; |
134 |
torsions = new TorsionStamp*[n_torsions]; |
135 |
for( i=0; i<n_torsions; i++ ) torsions[i] = NULL; |
136 |
} |
137 |
|
138 |
else if( !strcmp( lhs, "nRigidBodies" ) ){ |
139 |
n_rigidbodies = (int)rhs; |
140 |
|
141 |
if( have_rigidbodies ){ |
142 |
sprintf( errMsg, |
143 |
"MoleculeStamp error, n_rigidbodies already declared for" |
144 |
" molecule: %s\n", |
145 |
name ); |
146 |
return strdup( errMsg ); |
147 |
} |
148 |
have_rigidbodies = 1; |
149 |
rigidBodies = new RigidBodyStamp*[n_rigidbodies]; |
150 |
for( i=0; i<n_rigidbodies; i++ ) rigidBodies[i] = NULL; |
151 |
} |
152 |
|
153 |
else{ |
154 |
if( unhandled == NULL ) unhandled = new LinkedAssign( lhs, rhs ); |
155 |
else unhandled->add( lhs, rhs ); |
156 |
have_extras = 1; |
157 |
} |
158 |
return NULL; |
159 |
} |
160 |
|
161 |
char* MoleculeStamp::assignInt( char* lhs, int rhs ){ |
162 |
int i; |
163 |
|
164 |
if( !strcmp( lhs, "nAtoms" ) ){ |
165 |
n_atoms = rhs; |
166 |
|
167 |
if( have_atoms ){ |
168 |
sprintf( errMsg, |
169 |
"MoleculeStamp error, n_atoms already declared for" |
170 |
" molecule: %s\n", |
171 |
name); |
172 |
return strdup( errMsg ); |
173 |
} |
174 |
have_atoms = 1; |
175 |
atoms = new AtomStamp*[n_atoms]; |
176 |
for( i=0; i<n_atoms; i++ ) atoms[i] = NULL; |
177 |
} |
178 |
|
179 |
else if( !strcmp( lhs, "nBonds" ) ){ |
180 |
n_bonds = rhs; |
181 |
|
182 |
if( have_bonds ){ |
183 |
sprintf( errMsg, |
184 |
"MoleculeStamp error, n_bonds already declared for" |
185 |
" molecule: %s\n", |
186 |
name); |
187 |
return strdup( errMsg ); |
188 |
} |
189 |
have_bonds = 1; |
190 |
bonds = new BondStamp*[n_bonds]; |
191 |
for( i=0; i<n_bonds; i++ ) bonds[i] = NULL; |
192 |
} |
193 |
|
194 |
else if( !strcmp( lhs, "nBends" ) ){ |
195 |
n_bends = rhs; |
196 |
|
197 |
if( have_bends ){ |
198 |
sprintf( errMsg, |
199 |
"MoleculeStamp error, n_bends already declared for" |
200 |
" molecule: %s\n", |
201 |
name ); |
202 |
return strdup( errMsg ); |
203 |
} |
204 |
have_bends = 1; |
205 |
bends = new BendStamp*[n_bends]; |
206 |
for( i=0; i<n_bends; i++ ) bends[i] = NULL; |
207 |
} |
208 |
|
209 |
else if( !strcmp( lhs, "nTorsions" ) ){ |
210 |
n_torsions = rhs; |
211 |
|
212 |
if( have_torsions ){ |
213 |
sprintf( errMsg, |
214 |
"MoleculeStamp error, n_torsions already declared for" |
215 |
" molecule: %s\n", |
216 |
name); |
217 |
return strdup( errMsg ); |
218 |
} |
219 |
have_torsions = 1; |
220 |
torsions = new TorsionStamp*[n_torsions]; |
221 |
for( i=0; i<n_torsions; i++ ) torsions[i] = NULL; |
222 |
} |
223 |
|
224 |
else if( !strcmp( lhs, "nRigidBodies" ) ){ |
225 |
n_rigidbodies = rhs; |
226 |
|
227 |
if( have_rigidbodies ){ |
228 |
sprintf( errMsg, |
229 |
"RigidBodyStamp error, n_rigidbodies already declared for" |
230 |
" molecule: %s\n", |
231 |
name); |
232 |
return strdup( errMsg ); |
233 |
} |
234 |
have_rigidbodies = 1; |
235 |
rigidBodies = new RigidBodyStamp*[n_rigidbodies]; |
236 |
for( i=0; i<n_rigidbodies; i++ ) rigidBodies[i] = NULL; |
237 |
} |
238 |
else{ |
239 |
if( unhandled == NULL ) unhandled = new LinkedAssign( lhs, rhs ); |
240 |
else unhandled->add( lhs, rhs ); |
241 |
have_extras = 1; |
242 |
} |
243 |
return NULL; |
244 |
} |
245 |
|
246 |
char* MoleculeStamp::addAtom( AtomStamp* the_atom, int atomIndex ){ |
247 |
|
248 |
if( have_atoms && atomIndex < n_atoms ) atoms[atomIndex] = the_atom; |
249 |
else{ |
250 |
if( have_atoms ){ |
251 |
sprintf( errMsg, "MoleculeStamp error, %d out of nAtoms range", |
252 |
atomIndex ); |
253 |
return strdup( errMsg ); |
254 |
} |
255 |
else return strdup("MoleculeStamp error, nAtoms not given before" |
256 |
" first atom declaration." ); |
257 |
} |
258 |
|
259 |
return NULL; |
260 |
} |
261 |
|
262 |
char* MoleculeStamp::addRigidBody( RigidBodyStamp* the_rigidbody, |
263 |
int rigidBodyIndex ){ |
264 |
|
265 |
if( have_rigidbodies && rigidBodyIndex < n_rigidbodies ) |
266 |
rigidBodies[rigidBodyIndex] = the_rigidbody; |
267 |
else{ |
268 |
if( have_rigidbodies ){ |
269 |
sprintf( errMsg, "MoleculeStamp error, %d out of nRigidBodies range", |
270 |
rigidBodyIndex ); |
271 |
return strdup( errMsg ); |
272 |
} |
273 |
else return strdup("MoleculeStamp error, nRigidBodies not given before" |
274 |
" first rigidBody declaration." ); |
275 |
} |
276 |
|
277 |
return NULL; |
278 |
} |
279 |
|
280 |
char* MoleculeStamp::addBond( BondStamp* the_bond, int bondIndex ){ |
281 |
|
282 |
|
283 |
if( have_bonds && bondIndex < n_bonds ) bonds[bondIndex] = the_bond; |
284 |
else{ |
285 |
if( have_bonds ){ |
286 |
sprintf( errMsg, "MoleculeStamp error, %d out of nBonds range", |
287 |
bondIndex ); |
288 |
return strdup( errMsg ); |
289 |
} |
290 |
else return strdup("MoleculeStamp error, nBonds not given before" |
291 |
"first bond declaration." ); |
292 |
} |
293 |
|
294 |
return NULL; |
295 |
} |
296 |
|
297 |
char* MoleculeStamp::addBend( BendStamp* the_bend, int bendIndex ){ |
298 |
|
299 |
|
300 |
if( have_bends && bendIndex < n_bends ) bends[bendIndex] = the_bend; |
301 |
else{ |
302 |
if( have_bends ){ |
303 |
sprintf( errMsg, "MoleculeStamp error, %d out of nBends range", |
304 |
bendIndex ); |
305 |
return strdup( errMsg ); |
306 |
} |
307 |
else return strdup("MoleculeStamp error, nBends not given before" |
308 |
"first bend declaration." ); |
309 |
} |
310 |
|
311 |
return NULL; |
312 |
} |
313 |
|
314 |
char* MoleculeStamp::addTorsion( TorsionStamp* the_torsion, int torsionIndex ){ |
315 |
|
316 |
|
317 |
if( have_torsions && torsionIndex < n_torsions ) |
318 |
torsions[torsionIndex] = the_torsion; |
319 |
else{ |
320 |
if( have_torsions ){ |
321 |
sprintf( errMsg, "MoleculeStamp error, %d out of nTorsions range", |
322 |
torsionIndex ); |
323 |
return strdup( errMsg ); |
324 |
} |
325 |
else return strdup("MoleculeStamp error, nTorsions not given before" |
326 |
"first torsion declaration." ); |
327 |
} |
328 |
|
329 |
return NULL; |
330 |
} |
331 |
|
332 |
char* MoleculeStamp::checkMe( void ){ |
333 |
|
334 |
int i; |
335 |
short int no_atom, no_rigidbody; |
336 |
|
337 |
// Fix for Rigid Bodies!!! Molecules may not have any atoms that |
338 |
// they know about. They might have only rigid bodies (which then |
339 |
// know about the atoms! |
340 |
|
341 |
|
342 |
if( !have_name ) return strdup( "MoleculeStamp error. Molecule's name" |
343 |
" was not given.\n" ); |
344 |
|
345 |
if( !have_rigidbodies && !have_atoms ){ |
346 |
return strdup( "MoleculeStamp error. Molecule contains no atoms or RigidBodies." ); |
347 |
} |
348 |
|
349 |
no_rigidbody = 0; |
350 |
for( i=0; i<n_rigidbodies; i++ ){ |
351 |
if( rigidBodies[i] == NULL ) no_rigidbody = 1; |
352 |
} |
353 |
|
354 |
if( no_rigidbody ){ |
355 |
sprintf( errMsg, |
356 |
"MoleculeStamp error. Not all of the RigidBodies were" |
357 |
" declared in molecule \"%s\".\n", name ); |
358 |
return strdup( errMsg ); |
359 |
} |
360 |
|
361 |
no_atom = 0; |
362 |
for( i=0; i<n_atoms; i++ ){ |
363 |
if( atoms[i] == NULL ) no_atom = 1; |
364 |
} |
365 |
|
366 |
if( no_atom ){ |
367 |
sprintf( errMsg, |
368 |
"MoleculeStamp error. Not all of the atoms were" |
369 |
" declared in molecule \"%s\".\n", name ); |
370 |
return strdup( errMsg ); |
371 |
} |
372 |
|
373 |
return NULL; |
374 |
} |
375 |
|
376 |
|
377 |
int MoleculeStamp::getTotAtoms() { |
378 |
int total_atoms; |
379 |
int i; |
380 |
|
381 |
total_atoms = n_atoms; |
382 |
|
383 |
if( rigidBodies != NULL ) { |
384 |
for( i=0; i<n_rigidbodies; i++ ) |
385 |
total_atoms += rigidBodies[i]->getNAtoms(); |
386 |
} |
387 |
|
388 |
return total_atoms; |
389 |
} |
390 |
|