| 1 | gezelter | 1490 |  | 
| 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, CUTOFFGROUP_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 COMPONENT: | 
| 61 |  |  | incr_block( COMPONENT_BLK ); | 
| 62 |  |  | handled = the_globals->newComponent( the_event ); | 
| 63 |  |  | break; | 
| 64 |  |  |  | 
| 65 |  |  | case ASSIGNMENT: | 
| 66 |  |  | handled = the_globals->globalAssign( the_event ); | 
| 67 |  |  | break; | 
| 68 |  |  |  | 
| 69 |  |  | case BLOCK_END: | 
| 70 |  |  | handled = the_globals->globalEnd( the_event ); | 
| 71 |  |  | break; | 
| 72 |  |  |  | 
| 73 |  |  | default: | 
| 74 |  |  | the_event->err_msg = | 
| 75 |  |  | strdup("Not a valid global event\n" ); | 
| 76 |  |  | return 0; | 
| 77 |  |  | } | 
| 78 |  |  | break; | 
| 79 |  |  |  | 
| 80 |  |  | case MOLECULE_BLK: | 
| 81 |  |  |  | 
| 82 |  |  | switch( the_event->event_type ){ | 
| 83 |  |  |  | 
| 84 |  |  | case ATOM: | 
| 85 |  |  | incr_block( ATOM_BLK ); | 
| 86 |  |  | handled = the_stamps->newAtom( the_event ); | 
| 87 |  |  | break; | 
| 88 |  |  |  | 
| 89 |  |  | case BOND: | 
| 90 |  |  | incr_block( BOND_BLK ); | 
| 91 |  |  | handled = the_stamps->newBond( the_event ); | 
| 92 |  |  | break; | 
| 93 |  |  |  | 
| 94 |  |  | case BEND: | 
| 95 |  |  | incr_block( BEND_BLK ); | 
| 96 |  |  | handled = the_stamps->newBend( the_event ); | 
| 97 |  |  | break; | 
| 98 |  |  |  | 
| 99 |  |  | case TORSION: | 
| 100 |  |  | incr_block( TORSION_BLK ); | 
| 101 |  |  | handled = the_stamps->newTorsion( the_event ); | 
| 102 |  |  | break; | 
| 103 |  |  |  | 
| 104 |  |  | case RIGIDBODY: | 
| 105 |  |  | incr_block( RIGIDBODY_BLK ); | 
| 106 |  |  | handled = the_stamps->newRigidBody( the_event ); | 
| 107 |  |  | break; | 
| 108 |  |  |  | 
| 109 |  |  | case CUTOFFGROUP: | 
| 110 |  |  | incr_block( CUTOFFGROUP_BLK ); | 
| 111 |  |  | handled = the_stamps->newCutoffGroup( 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 ASSIGNMENT: | 
| 136 |  |  | handled = the_stamps->rigidBodyAssign( the_event ); | 
| 137 |  |  | break; | 
| 138 |  |  |  | 
| 139 |  |  | case MEMBERS: | 
| 140 |  |  | handled = the_stamps->rigidBodyMembers( the_event ); | 
| 141 |  |  | break; | 
| 142 |  |  |  | 
| 143 |  |  | case BLOCK_END: | 
| 144 |  |  | decr_block(); | 
| 145 |  |  | handled = the_stamps->rigidBodyEnd( the_event ); | 
| 146 |  |  | break; | 
| 147 |  |  |  | 
| 148 |  |  | default: | 
| 149 |  |  | the_event->err_msg = | 
| 150 |  |  | strdup( "Not a valid RigidBody event\n" ); | 
| 151 |  |  | return 0; | 
| 152 |  |  | } | 
| 153 |  |  | break; | 
| 154 |  |  |  | 
| 155 |  |  | case CUTOFFGROUP_BLK: | 
| 156 |  |  |  | 
| 157 |  |  | switch( the_event->event_type ){ | 
| 158 |  |  |  | 
| 159 |  |  | case ASSIGNMENT: | 
| 160 |  |  | handled = the_stamps->cutoffGroupAssign( the_event ); | 
| 161 |  |  | break; | 
| 162 |  |  |  | 
| 163 |  |  | case MEMBERS: | 
| 164 |  |  | handled = the_stamps->cutoffGroupMembers( the_event ); | 
| 165 |  |  | break; | 
| 166 |  |  |  | 
| 167 |  |  | case BLOCK_END: | 
| 168 |  |  | decr_block(); | 
| 169 |  |  | handled = the_stamps->cutoffGroupEnd( the_event ); | 
| 170 |  |  | break; | 
| 171 |  |  |  | 
| 172 |  |  | default: | 
| 173 |  |  | the_event->err_msg = | 
| 174 |  |  | strdup( "Not a valid CutoffGroup event\n" ); | 
| 175 |  |  | return 0; | 
| 176 |  |  | } | 
| 177 |  |  | break; | 
| 178 |  |  |  | 
| 179 |  |  |  | 
| 180 |  |  | case ATOM_BLK: | 
| 181 |  |  |  | 
| 182 |  |  | switch( the_event->event_type ){ | 
| 183 |  |  |  | 
| 184 |  |  | case POSITION: | 
| 185 |  |  | handled = the_stamps->atomPosition( the_event ); | 
| 186 |  |  | break; | 
| 187 |  |  |  | 
| 188 |  |  | case ORIENTATION: | 
| 189 |  |  | handled = the_stamps->atomOrientation( the_event ); | 
| 190 |  |  | break; | 
| 191 |  |  |  | 
| 192 |  |  | case ASSIGNMENT: | 
| 193 |  |  | handled = the_stamps->atomAssign( the_event ); | 
| 194 |  |  | break; | 
| 195 |  |  |  | 
| 196 |  |  | case BLOCK_END: | 
| 197 |  |  | decr_block(); | 
| 198 |  |  | handled = the_stamps->atomEnd( the_event ); | 
| 199 |  |  | break; | 
| 200 |  |  |  | 
| 201 |  |  | default: | 
| 202 |  |  | the_event->err_msg = | 
| 203 |  |  | strdup( "Not a valid atom event\n" ); | 
| 204 |  |  | return 0; | 
| 205 |  |  | } | 
| 206 |  |  | break; | 
| 207 |  |  |  | 
| 208 |  |  | case BOND_BLK: | 
| 209 |  |  |  | 
| 210 |  |  | switch( the_event->event_type ){ | 
| 211 |  |  |  | 
| 212 |  |  | case ASSIGNMENT: | 
| 213 |  |  | handled = the_stamps->bondAssign( the_event ); | 
| 214 |  |  | break; | 
| 215 |  |  |  | 
| 216 |  |  | case MEMBERS: | 
| 217 |  |  | handled = the_stamps->bondMembers( the_event ); | 
| 218 |  |  | break; | 
| 219 |  |  |  | 
| 220 |  |  | case CONSTRAINT: | 
| 221 |  |  | handled = the_stamps->bondConstraint( the_event ); | 
| 222 |  |  | break; | 
| 223 |  |  |  | 
| 224 |  |  | case BLOCK_END: | 
| 225 |  |  | decr_block(); | 
| 226 |  |  | handled = the_stamps->bondEnd(the_event ); | 
| 227 |  |  | break; | 
| 228 |  |  |  | 
| 229 |  |  | default: | 
| 230 |  |  | the_event->err_msg = | 
| 231 |  |  | strdup( "not a valid bond event\n" ); | 
| 232 |  |  | return 0; | 
| 233 |  |  | } | 
| 234 |  |  | break; | 
| 235 |  |  |  | 
| 236 |  |  | case BEND_BLK: | 
| 237 |  |  |  | 
| 238 |  |  | switch( the_event->event_type ){ | 
| 239 |  |  |  | 
| 240 |  |  | case ASSIGNMENT: | 
| 241 |  |  | handled = the_stamps->bendAssign( the_event ); | 
| 242 |  |  | break; | 
| 243 |  |  |  | 
| 244 |  |  | case MEMBERS: | 
| 245 |  |  | handled = the_stamps->bendMembers( the_event ); | 
| 246 |  |  | break; | 
| 247 |  |  |  | 
| 248 |  |  | case CONSTRAINT: | 
| 249 |  |  | handled = the_stamps->bendConstraint( the_event ); | 
| 250 |  |  | break; | 
| 251 |  |  |  | 
| 252 |  |  | case BLOCK_END: | 
| 253 |  |  | decr_block(); | 
| 254 |  |  | handled = the_stamps->bendEnd(the_event ); | 
| 255 |  |  | break; | 
| 256 |  |  |  | 
| 257 |  |  | default: | 
| 258 |  |  | the_event->err_msg = | 
| 259 |  |  | strdup( "not a valid bend event\n" ); | 
| 260 |  |  | return 0; | 
| 261 |  |  | } | 
| 262 |  |  | break; | 
| 263 |  |  |  | 
| 264 |  |  | case TORSION_BLK: | 
| 265 |  |  |  | 
| 266 |  |  | switch( the_event->event_type ){ | 
| 267 |  |  |  | 
| 268 |  |  | case ASSIGNMENT: | 
| 269 |  |  | handled = the_stamps->torsionAssign( the_event ); | 
| 270 |  |  | break; | 
| 271 |  |  |  | 
| 272 |  |  | case MEMBERS: | 
| 273 |  |  | handled = the_stamps->torsionMembers( the_event ); | 
| 274 |  |  | break; | 
| 275 |  |  |  | 
| 276 |  |  | case CONSTRAINT: | 
| 277 |  |  | handled = the_stamps->torsionConstraint( the_event ); | 
| 278 |  |  | break; | 
| 279 |  |  |  | 
| 280 |  |  | case BLOCK_END: | 
| 281 |  |  | decr_block(); | 
| 282 |  |  | handled = the_stamps->torsionEnd(the_event ); | 
| 283 |  |  | break; | 
| 284 |  |  |  | 
| 285 |  |  | default: | 
| 286 |  |  | the_event->err_msg = | 
| 287 |  |  | strdup( "not a valid torsion event\n" ); | 
| 288 |  |  | return 0; | 
| 289 |  |  | } | 
| 290 |  |  | break; | 
| 291 |  |  |  | 
| 292 |  |  | case ZCONSTRAINT_BLK: | 
| 293 |  |  |  | 
| 294 |  |  | switch( the_event->event_type ){ | 
| 295 |  |  |  | 
| 296 |  |  | case ASSIGNMENT: | 
| 297 |  |  | handled = the_globals->zConstraintAssign( the_event ); | 
| 298 |  |  | break; | 
| 299 |  |  |  | 
| 300 |  |  | case BLOCK_END: | 
| 301 |  |  | decr_block(); | 
| 302 |  |  | handled = the_globals->zConstraintEnd( the_event ); | 
| 303 |  |  | break; | 
| 304 |  |  |  | 
| 305 |  |  | default: | 
| 306 |  |  | the_event->err_msg = | 
| 307 |  |  | strdup( "not a valid zConstraint event\n" ); | 
| 308 |  |  | return 0; | 
| 309 |  |  | } | 
| 310 |  |  | break; | 
| 311 |  |  |  | 
| 312 |  |  | case COMPONENT_BLK: | 
| 313 |  |  |  | 
| 314 |  |  | switch( the_event->event_type ){ | 
| 315 |  |  |  | 
| 316 |  |  | case ASSIGNMENT: | 
| 317 |  |  | handled = the_globals->componentAssign( the_event ); | 
| 318 |  |  | break; | 
| 319 |  |  |  | 
| 320 |  |  | case BLOCK_END: | 
| 321 |  |  | decr_block(); | 
| 322 |  |  | handled = the_globals->componentEnd(the_event ); | 
| 323 |  |  | break; | 
| 324 |  |  |  | 
| 325 |  |  | default: | 
| 326 |  |  | the_event->err_msg = | 
| 327 |  |  | strdup( "not a valid component event\n" ); | 
| 328 |  |  | return 0; | 
| 329 |  |  | } | 
| 330 |  |  | break; | 
| 331 |  |  |  | 
| 332 |  |  | default: | 
| 333 |  |  | the_event->err_msg = | 
| 334 |  |  | strdup( "event is not in a valid block type\n" ); | 
| 335 |  |  | return 0; | 
| 336 |  |  | } | 
| 337 |  |  |  | 
| 338 |  |  | return handled; | 
| 339 |  |  | } | 
| 340 |  |  |  | 
| 341 |  |  | void incr_block( block_type new_block ){ | 
| 342 |  |  |  | 
| 343 |  |  | block_stack[block_stack_ptr] = current_block; | 
| 344 |  |  | block_stack_ptr++; | 
| 345 |  |  |  | 
| 346 |  |  | if( block_stack_ptr >= MAX_NEST ){ | 
| 347 |  |  | sprintf( painCave.errMsg, | 
| 348 |  |  | "Event blocks nested too deeply\n" ); | 
| 349 |  |  | painCave.isFatal = 1; | 
| 350 |  |  | simError(); | 
| 351 |  |  |  | 
| 352 |  |  | #ifdef IS_MPI | 
| 353 |  |  | if( worldRank == 0 ) mpiInterfaceExit(); | 
| 354 |  |  | #endif //is_mpi | 
| 355 |  |  | } | 
| 356 |  |  |  | 
| 357 |  |  | else current_block = new_block; | 
| 358 |  |  | } | 
| 359 |  |  |  | 
| 360 |  |  |  | 
| 361 |  |  | void decr_block( void ){ | 
| 362 |  |  |  | 
| 363 |  |  | block_stack_ptr--; | 
| 364 |  |  |  | 
| 365 |  |  | if( block_stack_ptr < 0 ){ | 
| 366 |  |  |  | 
| 367 |  |  | sprintf( painCave.errMsg, | 
| 368 |  |  | "Too many event blocks closed\n" ); | 
| 369 |  |  | painCave.isFatal = 1; | 
| 370 |  |  | simError(); | 
| 371 |  |  |  | 
| 372 |  |  | #ifdef IS_MPI | 
| 373 |  |  | if( worldRank == 0 ) mpiInterfaceExit(); | 
| 374 |  |  | #endif //is_mpi | 
| 375 |  |  |  | 
| 376 |  |  | } | 
| 377 |  |  |  | 
| 378 |  |  | else current_block = block_stack[block_stack_ptr]; | 
| 379 |  |  | } | 
| 380 |  |  |  | 
| 381 |  |  | void set_interface_stamps( MakeStamps* ms, Globals* g ){ | 
| 382 |  |  |  | 
| 383 |  |  | the_stamps = ms; | 
| 384 |  |  | the_globals = g; | 
| 385 |  |  |  | 
| 386 |  |  | } |