| 1 |
|
| 2 |
#include <string.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
#include "Globals.hpp" |
| 7 |
#include "BASS_interface.h" |
| 8 |
#include "simError.h" |
| 9 |
|
| 10 |
#ifdef IS_MPI |
| 11 |
#include "mpiBASS.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
// Globals ************************************************ |
| 16 |
|
| 17 |
typedef enum { GLOBAL_BLK, MOLECULE_BLK, ATOM_BLK, BOND_BLK, BEND_BLK, |
| 18 |
TORSION_BLK, COMPONENT_BLK, ZCONSTRAINT_BLK, |
| 19 |
RIGIDBODY_BLK } block_type; |
| 20 |
|
| 21 |
block_type current_block = GLOBAL_BLK; |
| 22 |
#define MAX_NEST 20 // the max number of nested blocks |
| 23 |
block_type block_stack[MAX_NEST]; |
| 24 |
int block_stack_ptr = 0; |
| 25 |
|
| 26 |
void incr_block( block_type new_block ); |
| 27 |
void decr_block(); |
| 28 |
|
| 29 |
MakeStamps *the_stamps; |
| 30 |
Globals *the_globals; |
| 31 |
|
| 32 |
// Functions ********************************************** |
| 33 |
|
| 34 |
/* |
| 35 |
* This is the main event handler |
| 36 |
* |
| 37 |
* returns 1 if the event was handled. If the event isn't handled, the |
| 38 |
* event's err_msg is set, and 0 is returned |
| 39 |
*/ |
| 40 |
|
| 41 |
int event_handler( event* the_event ){ |
| 42 |
|
| 43 |
int handled = 0; |
| 44 |
|
| 45 |
switch( current_block ){ |
| 46 |
|
| 47 |
case GLOBAL_BLK: |
| 48 |
switch( the_event->event_type ){ |
| 49 |
|
| 50 |
case MOLECULE: |
| 51 |
incr_block( MOLECULE_BLK ); |
| 52 |
handled = the_stamps->newMolecule( the_event ); |
| 53 |
break; |
| 54 |
|
| 55 |
case ZCONSTRAINT: |
| 56 |
incr_block( ZCONSTRAINT_BLK ); |
| 57 |
handled = the_globals->newZconstraint( the_event ); |
| 58 |
break; |
| 59 |
|
| 60 |
case RIGIDBODY: |
| 61 |
incr_block( RIGIDBODY_BLK ); |
| 62 |
handled = the_stamps->newRigidBody( the_event ); |
| 63 |
break; |
| 64 |
|
| 65 |
case COMPONENT: |
| 66 |
incr_block( COMPONENT_BLK ); |
| 67 |
handled = the_globals->newComponent( the_event ); |
| 68 |
break; |
| 69 |
|
| 70 |
case ASSIGNMENT: |
| 71 |
handled = the_globals->globalAssign( the_event ); |
| 72 |
break; |
| 73 |
|
| 74 |
case BLOCK_END: |
| 75 |
handled = the_globals->globalEnd( the_event ); |
| 76 |
break; |
| 77 |
|
| 78 |
default: |
| 79 |
the_event->err_msg = |
| 80 |
strdup("Not a valid global event\n" ); |
| 81 |
return 0; |
| 82 |
} |
| 83 |
break; |
| 84 |
|
| 85 |
case MOLECULE_BLK: |
| 86 |
|
| 87 |
switch( the_event->event_type ){ |
| 88 |
|
| 89 |
case ATOM: |
| 90 |
incr_block( ATOM_BLK ); |
| 91 |
handled = the_stamps->newAtom( the_event ); |
| 92 |
break; |
| 93 |
|
| 94 |
case BOND: |
| 95 |
incr_block( BOND_BLK ); |
| 96 |
handled = the_stamps->newBond( the_event ); |
| 97 |
break; |
| 98 |
|
| 99 |
case BEND: |
| 100 |
incr_block( BEND_BLK ); |
| 101 |
handled = the_stamps->newBend( the_event ); |
| 102 |
break; |
| 103 |
|
| 104 |
case TORSION: |
| 105 |
incr_block( TORSION_BLK ); |
| 106 |
handled = the_stamps->newTorsion( the_event ); |
| 107 |
break; |
| 108 |
|
| 109 |
case RIGIDBODY: |
| 110 |
incr_block( RIGIDBODY_BLK ); |
| 111 |
handled = the_stamps->newRigidBody( the_event ); |
| 112 |
break; |
| 113 |
|
| 114 |
case ASSIGNMENT: |
| 115 |
handled = the_stamps->moleculeAssign( the_event ); |
| 116 |
break; |
| 117 |
|
| 118 |
case BLOCK_END: |
| 119 |
decr_block(); |
| 120 |
handled = the_stamps->moleculeEnd( the_event ); |
| 121 |
break; |
| 122 |
|
| 123 |
default: |
| 124 |
the_event->err_msg = |
| 125 |
strdup( "Not a valid molecule event\n" ); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
break; |
| 129 |
|
| 130 |
|
| 131 |
case RIGIDBODY_BLK: |
| 132 |
|
| 133 |
switch( the_event->event_type ){ |
| 134 |
|
| 135 |
case ATOM: |
| 136 |
incr_block( ATOM_BLK ); |
| 137 |
handled = the_stamps->newAtom( the_event ); |
| 138 |
break; |
| 139 |
|
| 140 |
case POSITION: |
| 141 |
handled = the_stamps->rigidBodyPosition( the_event ); |
| 142 |
break; |
| 143 |
|
| 144 |
case ORIENTATION: |
| 145 |
handled = the_stamps->rigidBodyOrientation( the_event ); |
| 146 |
break; |
| 147 |
|
| 148 |
case ASSIGNMENT: |
| 149 |
handled = the_stamps->rigidBodyAssign( the_event ); |
| 150 |
break; |
| 151 |
|
| 152 |
case BLOCK_END: |
| 153 |
decr_block(); |
| 154 |
handled = the_stamps->rigidBodyEnd( the_event ); |
| 155 |
break; |
| 156 |
|
| 157 |
default: |
| 158 |
the_event->err_msg = |
| 159 |
strdup( "Not a valid RigidBody event\n" ); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
break; |
| 163 |
|
| 164 |
|
| 165 |
case ATOM_BLK: |
| 166 |
|
| 167 |
switch( the_event->event_type ){ |
| 168 |
|
| 169 |
case POSITION: |
| 170 |
handled = the_stamps->atomPosition( the_event ); |
| 171 |
break; |
| 172 |
|
| 173 |
case ORIENTATION: |
| 174 |
handled = the_stamps->atomOrientation( the_event ); |
| 175 |
break; |
| 176 |
|
| 177 |
case ASSIGNMENT: |
| 178 |
handled = the_stamps->atomAssign( the_event ); |
| 179 |
break; |
| 180 |
|
| 181 |
case BLOCK_END: |
| 182 |
decr_block(); |
| 183 |
handled = the_stamps->atomEnd( the_event ); |
| 184 |
break; |
| 185 |
|
| 186 |
default: |
| 187 |
the_event->err_msg = |
| 188 |
strdup( "Not a valid atom event\n" ); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
break; |
| 192 |
|
| 193 |
case BOND_BLK: |
| 194 |
|
| 195 |
switch( the_event->event_type ){ |
| 196 |
|
| 197 |
case ASSIGNMENT: |
| 198 |
handled = the_stamps->bondAssign( the_event ); |
| 199 |
break; |
| 200 |
|
| 201 |
case MEMBER: |
| 202 |
handled = the_stamps->bondMember( the_event ); |
| 203 |
break; |
| 204 |
|
| 205 |
case CONSTRAINT: |
| 206 |
handled = the_stamps->bondConstraint( the_event ); |
| 207 |
break; |
| 208 |
|
| 209 |
case BLOCK_END: |
| 210 |
decr_block(); |
| 211 |
handled = the_stamps->bondEnd(the_event ); |
| 212 |
break; |
| 213 |
|
| 214 |
default: |
| 215 |
the_event->err_msg = |
| 216 |
strdup( "not a valid bond event\n" ); |
| 217 |
return 0; |
| 218 |
} |
| 219 |
break; |
| 220 |
|
| 221 |
case BEND_BLK: |
| 222 |
|
| 223 |
switch( the_event->event_type ){ |
| 224 |
|
| 225 |
case ASSIGNMENT: |
| 226 |
handled = the_stamps->bendAssign( the_event ); |
| 227 |
break; |
| 228 |
|
| 229 |
case MEMBER: |
| 230 |
handled = the_stamps->bendMember( the_event ); |
| 231 |
break; |
| 232 |
|
| 233 |
case CONSTRAINT: |
| 234 |
handled = the_stamps->bendConstraint( the_event ); |
| 235 |
break; |
| 236 |
|
| 237 |
case BLOCK_END: |
| 238 |
decr_block(); |
| 239 |
handled = the_stamps->bendEnd(the_event ); |
| 240 |
break; |
| 241 |
|
| 242 |
default: |
| 243 |
the_event->err_msg = |
| 244 |
strdup( "not a valid bend event\n" ); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
break; |
| 248 |
|
| 249 |
case TORSION_BLK: |
| 250 |
|
| 251 |
switch( the_event->event_type ){ |
| 252 |
|
| 253 |
case ASSIGNMENT: |
| 254 |
handled = the_stamps->torsionAssign( the_event ); |
| 255 |
break; |
| 256 |
|
| 257 |
case MEMBER: |
| 258 |
handled = the_stamps->torsionMember( the_event ); |
| 259 |
break; |
| 260 |
|
| 261 |
case CONSTRAINT: |
| 262 |
handled = the_stamps->torsionConstraint( the_event ); |
| 263 |
break; |
| 264 |
|
| 265 |
case BLOCK_END: |
| 266 |
decr_block(); |
| 267 |
handled = the_stamps->torsionEnd(the_event ); |
| 268 |
break; |
| 269 |
|
| 270 |
default: |
| 271 |
the_event->err_msg = |
| 272 |
strdup( "not a valid torsion event\n" ); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
break; |
| 276 |
|
| 277 |
case ZCONSTRAINT_BLK: |
| 278 |
|
| 279 |
switch( the_event->event_type ){ |
| 280 |
|
| 281 |
case ASSIGNMENT: |
| 282 |
handled = the_globals->zConstraintAssign( the_event ); |
| 283 |
break; |
| 284 |
|
| 285 |
case BLOCK_END: |
| 286 |
decr_block(); |
| 287 |
handled = the_globals->zConstraintEnd( the_event ); |
| 288 |
break; |
| 289 |
|
| 290 |
default: |
| 291 |
the_event->err_msg = |
| 292 |
strdup( "not a valid zConstraint event\n" ); |
| 293 |
return 0; |
| 294 |
} |
| 295 |
break; |
| 296 |
|
| 297 |
case COMPONENT_BLK: |
| 298 |
|
| 299 |
switch( the_event->event_type ){ |
| 300 |
|
| 301 |
case ASSIGNMENT: |
| 302 |
handled = the_globals->componentAssign( the_event ); |
| 303 |
break; |
| 304 |
|
| 305 |
case BLOCK_END: |
| 306 |
decr_block(); |
| 307 |
handled = the_globals->componentEnd(the_event ); |
| 308 |
break; |
| 309 |
|
| 310 |
default: |
| 311 |
the_event->err_msg = |
| 312 |
strdup( "not a valid component event\n" ); |
| 313 |
return 0; |
| 314 |
} |
| 315 |
break; |
| 316 |
|
| 317 |
default: |
| 318 |
the_event->err_msg = |
| 319 |
strdup( "event is not in a valid block type\n" ); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
return handled; |
| 324 |
} |
| 325 |
|
| 326 |
void incr_block( block_type new_block ){ |
| 327 |
|
| 328 |
block_stack[block_stack_ptr] = current_block; |
| 329 |
block_stack_ptr++; |
| 330 |
|
| 331 |
if( block_stack_ptr >= MAX_NEST ){ |
| 332 |
sprintf( painCave.errMsg, |
| 333 |
"Event blocks nested too deeply\n" ); |
| 334 |
painCave.isFatal = 1; |
| 335 |
simError(); |
| 336 |
|
| 337 |
#ifdef IS_MPI |
| 338 |
if( worldRank == 0 ) mpiInterfaceExit(); |
| 339 |
#endif //is_mpi |
| 340 |
} |
| 341 |
|
| 342 |
else current_block = new_block; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
void decr_block( void ){ |
| 347 |
|
| 348 |
block_stack_ptr--; |
| 349 |
|
| 350 |
if( block_stack_ptr < 0 ){ |
| 351 |
|
| 352 |
sprintf( painCave.errMsg, |
| 353 |
"Too many event blocks closed\n" ); |
| 354 |
painCave.isFatal = 1; |
| 355 |
simError(); |
| 356 |
|
| 357 |
#ifdef IS_MPI |
| 358 |
if( worldRank == 0 ) mpiInterfaceExit(); |
| 359 |
#endif //is_mpi |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
else current_block = block_stack[block_stack_ptr]; |
| 364 |
} |
| 365 |
|
| 366 |
void set_interface_stamps( MakeStamps* ms, Globals* g ){ |
| 367 |
|
| 368 |
the_stamps = ms; |
| 369 |
the_globals = g; |
| 370 |
} |