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, MEMBER_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 ASSIGNMENT: |
110 |
handled = the_stamps->moleculeAssign( the_event ); |
111 |
break; |
112 |
|
113 |
case BLOCK_END: |
114 |
decr_block(); |
115 |
handled = the_stamps->moleculeEnd( the_event ); |
116 |
break; |
117 |
|
118 |
default: |
119 |
the_event->err_msg = |
120 |
strdup( "Not a valid molecule event\n" ); |
121 |
return 0; |
122 |
} |
123 |
break; |
124 |
|
125 |
|
126 |
case RIGIDBODY_BLK: |
127 |
|
128 |
switch( the_event->event_type ){ |
129 |
|
130 |
case ASSIGNMENT: |
131 |
handled = the_stamps->rigidBodyAssign( the_event ); |
132 |
break; |
133 |
|
134 |
case MEMBER: |
135 |
incr_block( MEMBER_BLK ); |
136 |
handled = the_stamps->rigidBodyMember( the_event ); |
137 |
break; |
138 |
|
139 |
case BLOCK_END: |
140 |
decr_block(); |
141 |
handled = the_stamps->rigidBodyEnd( the_event ); |
142 |
break; |
143 |
|
144 |
default: |
145 |
the_event->err_msg = |
146 |
strdup( "Not a valid RigidBody event\n" ); |
147 |
return 0; |
148 |
} |
149 |
break; |
150 |
|
151 |
|
152 |
case ATOM_BLK: |
153 |
|
154 |
switch( the_event->event_type ){ |
155 |
|
156 |
case POSITION: |
157 |
handled = the_stamps->atomPosition( the_event ); |
158 |
break; |
159 |
|
160 |
case ORIENTATION: |
161 |
handled = the_stamps->atomOrientation( the_event ); |
162 |
break; |
163 |
|
164 |
case ASSIGNMENT: |
165 |
handled = the_stamps->atomAssign( the_event ); |
166 |
break; |
167 |
|
168 |
case BLOCK_END: |
169 |
decr_block(); |
170 |
handled = the_stamps->atomEnd( the_event ); |
171 |
break; |
172 |
|
173 |
default: |
174 |
the_event->err_msg = |
175 |
strdup( "Not a valid atom event\n" ); |
176 |
return 0; |
177 |
} |
178 |
break; |
179 |
|
180 |
case BOND_BLK: |
181 |
|
182 |
switch( the_event->event_type ){ |
183 |
|
184 |
case ASSIGNMENT: |
185 |
handled = the_stamps->bondAssign( the_event ); |
186 |
break; |
187 |
|
188 |
case MEMBERS: |
189 |
handled = the_stamps->bondMembers( the_event ); |
190 |
break; |
191 |
|
192 |
case CONSTRAINT: |
193 |
handled = the_stamps->bondConstraint( the_event ); |
194 |
break; |
195 |
|
196 |
case BLOCK_END: |
197 |
decr_block(); |
198 |
handled = the_stamps->bondEnd(the_event ); |
199 |
break; |
200 |
|
201 |
default: |
202 |
the_event->err_msg = |
203 |
strdup( "not a valid bond event\n" ); |
204 |
return 0; |
205 |
} |
206 |
break; |
207 |
|
208 |
case BEND_BLK: |
209 |
|
210 |
switch( the_event->event_type ){ |
211 |
|
212 |
case ASSIGNMENT: |
213 |
handled = the_stamps->bendAssign( the_event ); |
214 |
break; |
215 |
|
216 |
case MEMBERS: |
217 |
handled = the_stamps->bendMembers( the_event ); |
218 |
break; |
219 |
|
220 |
case CONSTRAINT: |
221 |
handled = the_stamps->bendConstraint( the_event ); |
222 |
break; |
223 |
|
224 |
case BLOCK_END: |
225 |
decr_block(); |
226 |
handled = the_stamps->bendEnd(the_event ); |
227 |
break; |
228 |
|
229 |
default: |
230 |
the_event->err_msg = |
231 |
strdup( "not a valid bend event\n" ); |
232 |
return 0; |
233 |
} |
234 |
break; |
235 |
|
236 |
case TORSION_BLK: |
237 |
|
238 |
switch( the_event->event_type ){ |
239 |
|
240 |
case ASSIGNMENT: |
241 |
handled = the_stamps->torsionAssign( the_event ); |
242 |
break; |
243 |
|
244 |
case MEMBERS: |
245 |
handled = the_stamps->torsionMembers( the_event ); |
246 |
break; |
247 |
|
248 |
case CONSTRAINT: |
249 |
handled = the_stamps->torsionConstraint( the_event ); |
250 |
break; |
251 |
|
252 |
case BLOCK_END: |
253 |
decr_block(); |
254 |
handled = the_stamps->torsionEnd(the_event ); |
255 |
break; |
256 |
|
257 |
default: |
258 |
the_event->err_msg = |
259 |
strdup( "not a valid torsion event\n" ); |
260 |
return 0; |
261 |
} |
262 |
break; |
263 |
|
264 |
case MEMBER_BLK: |
265 |
|
266 |
switch( the_event->event_type ){ |
267 |
|
268 |
case ASSIGNMENT: |
269 |
handled = the_stamps->memberAssign( the_event ); |
270 |
break; |
271 |
|
272 |
case BLOCK_END: |
273 |
decr_block(); |
274 |
handled = the_stamps->memberEnd(the_event ); |
275 |
break; |
276 |
|
277 |
default: |
278 |
the_event->err_msg = |
279 |
strdup( "not a valid member event\n" ); |
280 |
return 0; |
281 |
} |
282 |
break; |
283 |
|
284 |
case ZCONSTRAINT_BLK: |
285 |
|
286 |
switch( the_event->event_type ){ |
287 |
|
288 |
case ASSIGNMENT: |
289 |
handled = the_globals->zConstraintAssign( the_event ); |
290 |
break; |
291 |
|
292 |
case BLOCK_END: |
293 |
decr_block(); |
294 |
handled = the_globals->zConstraintEnd( the_event ); |
295 |
break; |
296 |
|
297 |
default: |
298 |
the_event->err_msg = |
299 |
strdup( "not a valid zConstraint event\n" ); |
300 |
return 0; |
301 |
} |
302 |
break; |
303 |
|
304 |
case COMPONENT_BLK: |
305 |
|
306 |
switch( the_event->event_type ){ |
307 |
|
308 |
case ASSIGNMENT: |
309 |
handled = the_globals->componentAssign( the_event ); |
310 |
break; |
311 |
|
312 |
case BLOCK_END: |
313 |
decr_block(); |
314 |
handled = the_globals->componentEnd(the_event ); |
315 |
break; |
316 |
|
317 |
default: |
318 |
the_event->err_msg = |
319 |
strdup( "not a valid component event\n" ); |
320 |
return 0; |
321 |
} |
322 |
break; |
323 |
|
324 |
default: |
325 |
the_event->err_msg = |
326 |
strdup( "event is not in a valid block type\n" ); |
327 |
return 0; |
328 |
} |
329 |
|
330 |
return handled; |
331 |
} |
332 |
|
333 |
void incr_block( block_type new_block ){ |
334 |
|
335 |
block_stack[block_stack_ptr] = current_block; |
336 |
block_stack_ptr++; |
337 |
|
338 |
if( block_stack_ptr >= MAX_NEST ){ |
339 |
sprintf( painCave.errMsg, |
340 |
"Event blocks nested too deeply\n" ); |
341 |
painCave.isFatal = 1; |
342 |
simError(); |
343 |
|
344 |
#ifdef IS_MPI |
345 |
if( worldRank == 0 ) mpiInterfaceExit(); |
346 |
#endif //is_mpi |
347 |
} |
348 |
|
349 |
else current_block = new_block; |
350 |
} |
351 |
|
352 |
|
353 |
void decr_block( void ){ |
354 |
|
355 |
block_stack_ptr--; |
356 |
|
357 |
if( block_stack_ptr < 0 ){ |
358 |
|
359 |
sprintf( painCave.errMsg, |
360 |
"Too many event blocks closed\n" ); |
361 |
painCave.isFatal = 1; |
362 |
simError(); |
363 |
|
364 |
#ifdef IS_MPI |
365 |
if( worldRank == 0 ) mpiInterfaceExit(); |
366 |
#endif //is_mpi |
367 |
|
368 |
} |
369 |
|
370 |
else current_block = block_stack[block_stack_ptr]; |
371 |
} |
372 |
|
373 |
void set_interface_stamps( MakeStamps* ms, Globals* g ){ |
374 |
|
375 |
the_stamps = ms; |
376 |
the_globals = g; |
377 |
} |