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