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