1 |
#include <cstdlib> |
2 |
#include <cstdio> |
3 |
|
4 |
#include "MakeStamps.hpp" |
5 |
#include "MoleculeStamp.hpp" |
6 |
#include "simError.h" |
7 |
#ifdef IS_MPI |
8 |
#include "mpiBASS.h" |
9 |
#endif // is_mpi |
10 |
|
11 |
LinkedMolStamp::~LinkedMolStamp(){ |
12 |
if( mol_stamp != NULL ) delete mol_stamp; |
13 |
if( next != NULL ) delete next; |
14 |
} |
15 |
|
16 |
void LinkedMolStamp::add( LinkedMolStamp* newbie ){ |
17 |
|
18 |
if( next != NULL ) next->add( newbie ); |
19 |
else{ |
20 |
next = newbie; |
21 |
next->setPrev( this ); |
22 |
} |
23 |
} |
24 |
|
25 |
MoleculeStamp* LinkedMolStamp::match( char* id ){ |
26 |
|
27 |
if( mol_stamp != NULL ){ |
28 |
if(!strcmp( mol_stamp->getID(), id )){ |
29 |
|
30 |
// make sure we aren't hiding somebody else with the same name |
31 |
if(next != NULL ){ |
32 |
if( next->match( id ) != NULL){ |
33 |
sprintf( painCave.errMsg, |
34 |
"Molecule Stamp Error. Two separate of declarations of " |
35 |
"%s present.\n", |
36 |
id ); |
37 |
painCave.isFatal = 1; |
38 |
simError(); |
39 |
#ifdef IS_MPI |
40 |
if( painCave.isEventLoop ){ |
41 |
if( worldRank == 0 ) mpiInterfaceExit(); |
42 |
} |
43 |
#endif //is_mpi |
44 |
} |
45 |
else return mol_stamp; |
46 |
} |
47 |
else return mol_stamp; |
48 |
} |
49 |
} |
50 |
|
51 |
if( next != NULL ) return next->match( id ); |
52 |
|
53 |
return NULL; |
54 |
} |
55 |
|
56 |
LinkedMolStamp* LinkedMolStamp::extract( char* id ){ |
57 |
|
58 |
if( mol_stamp != NULL ){ |
59 |
if(!strcmp( mol_stamp->getID(), id )){ |
60 |
|
61 |
// make sure we aren't hiding somebody else with the same name |
62 |
if(next != NULL ){ |
63 |
if( next->match( id ) != NULL){ |
64 |
sprintf( painCave.errMsg, |
65 |
"Molecule Stamp Error. Two separate of declarations of " |
66 |
"%s present.\n", |
67 |
id ); |
68 |
painCave.isFatal = 1; |
69 |
simError(); |
70 |
#ifdef IS_MPI |
71 |
if( painCave.isEventLoop ){ |
72 |
if( worldRank == 0 ) mpiInterfaceExit(); |
73 |
} |
74 |
#endif //is_mpi |
75 |
} |
76 |
} |
77 |
|
78 |
prev->setNext( next ); |
79 |
if( next != NULL ) next->setPrev( prev ); |
80 |
return this; |
81 |
} |
82 |
} |
83 |
|
84 |
if( next != NULL ) return next->extract( id ); |
85 |
|
86 |
return NULL; |
87 |
} |
88 |
|
89 |
MakeStamps::MakeStamps(){ |
90 |
|
91 |
int i; |
92 |
|
93 |
my_mols = new LinkedMolStamp*[hash_size]; |
94 |
for( i=0; i<hash_size; i++ ){ |
95 |
my_mols[i] = new LinkedMolStamp(); |
96 |
} |
97 |
|
98 |
} |
99 |
|
100 |
MakeStamps::~MakeStamps(){ |
101 |
|
102 |
int i; |
103 |
|
104 |
for( i=0; i<hash_size; i++ ){ |
105 |
if( my_mols[i] != NULL ) delete my_mols[i]; |
106 |
} |
107 |
delete[] my_mols; |
108 |
} |
109 |
|
110 |
int MakeStamps::hash( char* text ){ |
111 |
|
112 |
register unsigned short int i = 0; // loop counter |
113 |
int key = 0; // the hash key |
114 |
|
115 |
while( text[i] != '\0' ){ |
116 |
|
117 |
key = ( ( key << hash_shift ) + text[i] ) % hash_size; |
118 |
|
119 |
i++; |
120 |
} |
121 |
|
122 |
if( key < 0 ){ |
123 |
|
124 |
// if the key is less than zero, we've had an overflow error |
125 |
|
126 |
sprintf( painCave.errMsg, |
127 |
"There has been an overflow error in the MakeStamps hash key."); |
128 |
painCave.isFatal = 1; |
129 |
simError(); |
130 |
#ifdef IS_MPI |
131 |
if( painCave.isEventLoop ){ |
132 |
if( worldRank == 0 ) mpiInterfaceExit(); |
133 |
} |
134 |
#endif //is_mpi |
135 |
} |
136 |
|
137 |
return key; |
138 |
} |
139 |
|
140 |
LinkedMolStamp* MakeStamps::extractMolStamp( char* the_id ){ |
141 |
int key; |
142 |
|
143 |
key = hash( the_id ); |
144 |
if( my_mols[key] != NULL ){ |
145 |
return my_mols[key]->extract( the_id ); |
146 |
} |
147 |
|
148 |
return NULL; |
149 |
} |
150 |
|
151 |
void MakeStamps::addMolStamp( MoleculeStamp* the_stamp ){ |
152 |
|
153 |
int key; |
154 |
LinkedMolStamp* linked_mol; |
155 |
|
156 |
key = hash( the_stamp->getID() ); |
157 |
|
158 |
linked_mol = new LinkedMolStamp; |
159 |
linked_mol->setStamp( the_stamp ); |
160 |
|
161 |
my_mols[key]->add( linked_mol ); |
162 |
|
163 |
} |
164 |
|
165 |
|
166 |
int MakeStamps::newMolecule( event* the_event ){ |
167 |
|
168 |
current_mol = new MoleculeStamp; |
169 |
return 1; |
170 |
} |
171 |
|
172 |
int MakeStamps::moleculeAssign( event* the_event ){ |
173 |
|
174 |
switch( the_event->evt.asmt.asmt_type ){ |
175 |
|
176 |
case STRING: |
177 |
the_event->err_msg = current_mol->assignString( the_event->evt.asmt.lhs, |
178 |
the_event->evt.asmt.rhs.sval ); |
179 |
break; |
180 |
|
181 |
case DOUBLE: |
182 |
the_event->err_msg = current_mol->assignDouble( the_event->evt.asmt.lhs, |
183 |
the_event->evt.asmt.rhs.dval ); |
184 |
break; |
185 |
|
186 |
case INT: |
187 |
the_event->err_msg = current_mol->assignInt( the_event->evt.asmt.lhs, |
188 |
the_event->evt.asmt.rhs.ival ); |
189 |
break; |
190 |
|
191 |
default: |
192 |
the_event->err_msg = strdup( "MakeStamp error. Invalid molecule" |
193 |
" assignment type" ); |
194 |
return 0; |
195 |
break; |
196 |
} |
197 |
if( the_event->err_msg != NULL ) return 0; |
198 |
return 1; |
199 |
} |
200 |
|
201 |
int MakeStamps::moleculeEnd( event* the_event ){ |
202 |
|
203 |
the_event->err_msg = current_mol->checkMe(); |
204 |
// if err_msg is set, then something is wrong |
205 |
if( the_event->err_msg != NULL ) return 0; |
206 |
|
207 |
addMolStamp( current_mol ); |
208 |
return 1; |
209 |
} |
210 |
|
211 |
int MakeStamps::newAtom( event* the_event ){ |
212 |
|
213 |
current_atom = new AtomStamp; |
214 |
|
215 |
the_event->err_msg = current_mol->addAtom( current_atom, |
216 |
the_event->evt.blk_index ); |
217 |
if( the_event->err_msg != NULL ) return 0; |
218 |
return 1; |
219 |
} |
220 |
|
221 |
int MakeStamps::atomPosition( event* the_event ){ |
222 |
|
223 |
current_atom->setPosition( the_event->evt.pos.x, |
224 |
the_event->evt.pos.y, |
225 |
the_event->evt.pos.z ); |
226 |
return 1; |
227 |
} |
228 |
|
229 |
|
230 |
int MakeStamps::atomOrientation( event* the_event ){ |
231 |
|
232 |
current_atom->setOrientation( the_event->evt.ornt.x, |
233 |
the_event->evt.ornt.y, |
234 |
the_event->evt.ornt.z ); |
235 |
return 1; |
236 |
} |
237 |
|
238 |
int MakeStamps::atomAssign( event* the_event ){ |
239 |
|
240 |
switch( the_event->evt.asmt.asmt_type ){ |
241 |
|
242 |
case STRING: |
243 |
the_event->err_msg = |
244 |
current_atom->assignString( the_event->evt.asmt.lhs, |
245 |
the_event->evt.asmt.rhs.sval ); |
246 |
if( the_event->err_msg != NULL ) return 0; |
247 |
return 1; |
248 |
break; |
249 |
|
250 |
case DOUBLE: |
251 |
the_event->err_msg = |
252 |
current_atom->assignDouble( the_event->evt.asmt.lhs, |
253 |
the_event->evt.asmt.rhs.dval ); |
254 |
if( the_event->err_msg != NULL ) return 0; |
255 |
return 1; |
256 |
break; |
257 |
|
258 |
case INT: |
259 |
the_event->err_msg = |
260 |
current_atom->assignInt( the_event->evt.asmt.lhs, |
261 |
the_event->evt.asmt.rhs.ival ); |
262 |
if( the_event->err_msg != NULL ) return 0; |
263 |
return 1; |
264 |
break; |
265 |
|
266 |
default: |
267 |
the_event->err_msg = strdup( "MakeStamp error. Invalid atom" |
268 |
" assignment type" ); |
269 |
return 0; |
270 |
break; |
271 |
} |
272 |
return 0; |
273 |
} |
274 |
|
275 |
int MakeStamps::atomEnd( event* the_event ){ |
276 |
|
277 |
the_event->err_msg = current_atom->checkMe(); |
278 |
if( the_event->err_msg != NULL ) return 0; |
279 |
|
280 |
return 1; |
281 |
} |
282 |
|
283 |
int MakeStamps::newBond( event* the_event ){ |
284 |
|
285 |
current_bond = new BondStamp; |
286 |
|
287 |
the_event->err_msg = current_mol->addBond( current_bond, |
288 |
the_event->evt.blk_index ); |
289 |
if( the_event->err_msg != NULL ) return 0; |
290 |
|
291 |
return 1; |
292 |
} |
293 |
|
294 |
int MakeStamps::bondAssign( event* the_event ){ |
295 |
|
296 |
switch( the_event->evt.asmt.asmt_type ){ |
297 |
|
298 |
case STRING: |
299 |
current_bond->assignString( the_event->evt.asmt.lhs, |
300 |
the_event->evt.asmt.rhs.sval ); |
301 |
return 1; |
302 |
break; |
303 |
|
304 |
case DOUBLE: |
305 |
current_bond->assignDouble( the_event->evt.asmt.lhs, |
306 |
the_event->evt.asmt.rhs.dval ); |
307 |
return 1; |
308 |
break; |
309 |
|
310 |
case INT: |
311 |
current_bond->assignInt( the_event->evt.asmt.lhs, |
312 |
the_event->evt.asmt.rhs.ival ); |
313 |
return 1; |
314 |
break; |
315 |
|
316 |
default: |
317 |
the_event->err_msg = strdup( "MakeStamp error. Invalid bond" |
318 |
" assignment type" ); |
319 |
return 0; |
320 |
break; |
321 |
} |
322 |
return 0; |
323 |
} |
324 |
|
325 |
int MakeStamps::bondMember( event* the_event ){ |
326 |
|
327 |
current_bond->members( the_event->evt.mbr.a, |
328 |
the_event->evt.mbr.b ); |
329 |
return 1; |
330 |
} |
331 |
|
332 |
int MakeStamps::bondConstraint( event* the_event ){ |
333 |
|
334 |
current_bond->constrain( the_event->evt.cnstr ); |
335 |
return 1; |
336 |
} |
337 |
|
338 |
int MakeStamps::bondEnd( event* the_event ){ |
339 |
|
340 |
the_event->err_msg = current_bond->checkMe(); |
341 |
if( the_event->err_msg != NULL ) return 0; |
342 |
|
343 |
return 1; |
344 |
} |
345 |
|
346 |
int MakeStamps::newBend( event* the_event ){ |
347 |
|
348 |
current_bend = new BendStamp; |
349 |
|
350 |
the_event->err_msg = current_mol->addBend( current_bend, |
351 |
the_event->evt.blk_index ); |
352 |
if( the_event->err_msg != NULL ) return 0; |
353 |
|
354 |
return 1; |
355 |
} |
356 |
|
357 |
int MakeStamps::bendAssign( event* the_event ){ |
358 |
|
359 |
switch( the_event->evt.asmt.asmt_type ){ |
360 |
|
361 |
case STRING: |
362 |
current_bend->assignString( the_event->evt.asmt.lhs, |
363 |
the_event->evt.asmt.rhs.sval ); |
364 |
return 1; |
365 |
break; |
366 |
|
367 |
case DOUBLE: |
368 |
current_bend->assignDouble( the_event->evt.asmt.lhs, |
369 |
the_event->evt.asmt.rhs.dval ); |
370 |
return 1; |
371 |
break; |
372 |
|
373 |
case INT: |
374 |
current_bend->assignInt( the_event->evt.asmt.lhs, |
375 |
the_event->evt.asmt.rhs.ival ); |
376 |
return 1; |
377 |
break; |
378 |
|
379 |
default: |
380 |
the_event->err_msg = strdup( "MakeStamp error. Invalid bend" |
381 |
" assignment type" ); |
382 |
return 0; |
383 |
break; |
384 |
} |
385 |
return 0; |
386 |
} |
387 |
|
388 |
int MakeStamps::bendMember( event* the_event ){ |
389 |
|
390 |
current_bend->members( the_event->evt.mbr.a, |
391 |
the_event->evt.mbr.b, |
392 |
the_event->evt.mbr.c ); |
393 |
return 1; |
394 |
} |
395 |
|
396 |
int MakeStamps::bendConstraint( event* the_event ){ |
397 |
|
398 |
current_bend->constrain( the_event->evt.cnstr ); |
399 |
return 1; |
400 |
} |
401 |
|
402 |
int MakeStamps::bendEnd( event* the_event ){ |
403 |
|
404 |
the_event->err_msg = current_bend->checkMe(); |
405 |
if( the_event->err_msg != NULL ) return 0; |
406 |
|
407 |
return 1; |
408 |
} |
409 |
|
410 |
int MakeStamps::newTorsion( event* the_event ){ |
411 |
|
412 |
current_torsion = new TorsionStamp; |
413 |
|
414 |
the_event->err_msg = current_mol->addTorsion( current_torsion, |
415 |
the_event->evt.blk_index ); |
416 |
if( the_event->err_msg != NULL ) return 0; |
417 |
|
418 |
return 1; |
419 |
} |
420 |
|
421 |
int MakeStamps::torsionAssign( event* the_event ){ |
422 |
|
423 |
switch( the_event->evt.asmt.asmt_type ){ |
424 |
|
425 |
case STRING: |
426 |
current_torsion->assignString( the_event->evt.asmt.lhs, |
427 |
the_event->evt.asmt.rhs.sval ); |
428 |
return 1; |
429 |
break; |
430 |
|
431 |
case DOUBLE: |
432 |
current_torsion->assignDouble( the_event->evt.asmt.lhs, |
433 |
the_event->evt.asmt.rhs.dval ); |
434 |
return 1; |
435 |
break; |
436 |
|
437 |
case INT: |
438 |
current_torsion->assignInt( the_event->evt.asmt.lhs, |
439 |
the_event->evt.asmt.rhs.ival ); |
440 |
return 1; |
441 |
break; |
442 |
|
443 |
default: |
444 |
the_event->err_msg = strdup( "MakeStamp error. Invalid torsion" |
445 |
" assignment type" ); |
446 |
return 0; |
447 |
break; |
448 |
} |
449 |
return 0; |
450 |
} |
451 |
|
452 |
int MakeStamps::torsionMember( event* the_event ){ |
453 |
|
454 |
current_torsion->members( the_event->evt.mbr.a, |
455 |
the_event->evt.mbr.b, |
456 |
the_event->evt.mbr.c, |
457 |
the_event->evt.mbr.d ); |
458 |
return 1; |
459 |
} |
460 |
|
461 |
int MakeStamps::torsionConstraint( event* the_event ){ |
462 |
|
463 |
current_torsion->constrain( the_event->evt.cnstr ); |
464 |
return 1; |
465 |
} |
466 |
|
467 |
int MakeStamps::torsionEnd( event* the_event ){ |
468 |
|
469 |
the_event->err_msg = current_torsion->checkMe(); |
470 |
if( the_event->err_msg != NULL ) return 0; |
471 |
|
472 |
return 1; |
473 |
} |