1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Acknowledgement of the program authors must be made in any |
10 |
* publication of scientific results based in part on use of the |
11 |
* program. An acceptable form of acknowledgement is citation of |
12 |
* the article in which the program was described (Matthew |
13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
* |
18 |
* 2. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in the |
23 |
* documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* This software is provided "AS IS," without a warranty of any |
27 |
* kind. All express or implied conditions, representations and |
28 |
* warranties, including any implied warranty of merchantability, |
29 |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
* be liable for any damages suffered by licensee as a result of |
32 |
* using, modifying or distributing the software or its |
33 |
* derivatives. In no event will the University of Notre Dame or its |
34 |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
* direct, indirect, special, consequential, incidental or punitive |
36 |
* damages, however caused and regardless of the theory of liability, |
37 |
* arising out of the use of or inability to use software, even if the |
38 |
* University of Notre Dame has been advised of the possibility of |
39 |
* such damages. |
40 |
*/ |
41 |
|
42 |
#include <string.h> |
43 |
#include <stdio.h> |
44 |
#include <stdlib.h> |
45 |
|
46 |
#include "io/Globals.hpp" |
47 |
#include "io/parse_me.h" |
48 |
#include "utils/simError.h" |
49 |
|
50 |
#ifdef IS_MPI |
51 |
#include "io/mpiBASS.h" |
52 |
#endif |
53 |
|
54 |
|
55 |
// Globals ************************************************ |
56 |
|
57 |
typedef enum { GLOBAL_BLK, MOLECULE_BLK, ATOM_BLK, BOND_BLK, BEND_BLK, |
58 |
TORSION_BLK, COMPONENT_BLK, ZCONSTRAINT_BLK, |
59 |
RIGIDBODY_BLK, CUTOFFGROUP_BLK } block_type; |
60 |
|
61 |
block_type current_block = GLOBAL_BLK; |
62 |
#define MAX_NEST 20 // the max number of nested blocks |
63 |
block_type block_stack[MAX_NEST]; |
64 |
int block_stack_ptr = 0; |
65 |
|
66 |
void incr_block( block_type new_block ); |
67 |
void decr_block(); |
68 |
|
69 |
MakeStamps *the_stamps; |
70 |
Globals *the_simParams; |
71 |
|
72 |
// Functions ********************************************** |
73 |
|
74 |
/* |
75 |
* This is the main event handler |
76 |
* |
77 |
* returns 1 if the event was handled. If the event isn't handled, the |
78 |
* event's err_msg is set, and 0 is returned |
79 |
*/ |
80 |
|
81 |
int event_handler( event* the_event ){ |
82 |
|
83 |
int handled = 0; |
84 |
|
85 |
switch( current_block ){ |
86 |
|
87 |
case GLOBAL_BLK: |
88 |
switch( the_event->event_type ){ |
89 |
|
90 |
case MOLECULE: |
91 |
incr_block( MOLECULE_BLK ); |
92 |
handled = the_stamps->newMolecule( the_event ); |
93 |
break; |
94 |
|
95 |
case ZCONSTRAINT: |
96 |
incr_block( ZCONSTRAINT_BLK ); |
97 |
handled = the_simParams->newZconstraint( the_event ); |
98 |
break; |
99 |
|
100 |
case COMPONENT: |
101 |
incr_block( COMPONENT_BLK ); |
102 |
handled = the_simParams->newComponent( the_event ); |
103 |
break; |
104 |
|
105 |
case ASSIGNMENT: |
106 |
handled = the_simParams->globalAssign( the_event ); |
107 |
break; |
108 |
|
109 |
case BLOCK_END: |
110 |
handled = the_simParams->globalEnd( the_event ); |
111 |
break; |
112 |
|
113 |
default: |
114 |
the_event->err_msg = |
115 |
strdup("Not a valid global event\n" ); |
116 |
return 0; |
117 |
} |
118 |
break; |
119 |
|
120 |
case MOLECULE_BLK: |
121 |
|
122 |
switch( the_event->event_type ){ |
123 |
|
124 |
case ATOM: |
125 |
incr_block( ATOM_BLK ); |
126 |
handled = the_stamps->newAtom( the_event ); |
127 |
break; |
128 |
|
129 |
case BOND: |
130 |
incr_block( BOND_BLK ); |
131 |
handled = the_stamps->newBond( the_event ); |
132 |
break; |
133 |
|
134 |
case BEND: |
135 |
incr_block( BEND_BLK ); |
136 |
handled = the_stamps->newBend( the_event ); |
137 |
break; |
138 |
|
139 |
case TORSION: |
140 |
incr_block( TORSION_BLK ); |
141 |
handled = the_stamps->newTorsion( the_event ); |
142 |
break; |
143 |
|
144 |
case RIGIDBODY: |
145 |
incr_block( RIGIDBODY_BLK ); |
146 |
handled = the_stamps->newRigidBody( the_event ); |
147 |
break; |
148 |
|
149 |
case CUTOFFGROUP: |
150 |
incr_block( CUTOFFGROUP_BLK ); |
151 |
handled = the_stamps->newCutoffGroup( the_event ); |
152 |
break; |
153 |
|
154 |
case ASSIGNMENT: |
155 |
handled = the_stamps->moleculeAssign( the_event ); |
156 |
break; |
157 |
|
158 |
case BLOCK_END: |
159 |
decr_block(); |
160 |
handled = the_stamps->moleculeEnd( the_event ); |
161 |
break; |
162 |
|
163 |
default: |
164 |
the_event->err_msg = |
165 |
strdup( "Not a valid molecule event\n" ); |
166 |
return 0; |
167 |
} |
168 |
break; |
169 |
|
170 |
|
171 |
case RIGIDBODY_BLK: |
172 |
|
173 |
switch( the_event->event_type ){ |
174 |
|
175 |
case ASSIGNMENT: |
176 |
handled = the_stamps->rigidBodyAssign( the_event ); |
177 |
break; |
178 |
|
179 |
case MEMBERS: |
180 |
handled = the_stamps->rigidBodyMembers( the_event ); |
181 |
break; |
182 |
|
183 |
case BLOCK_END: |
184 |
decr_block(); |
185 |
handled = the_stamps->rigidBodyEnd( the_event ); |
186 |
break; |
187 |
|
188 |
default: |
189 |
the_event->err_msg = |
190 |
strdup( "Not a valid RigidBody event\n" ); |
191 |
return 0; |
192 |
} |
193 |
break; |
194 |
|
195 |
case CUTOFFGROUP_BLK: |
196 |
|
197 |
switch( the_event->event_type ){ |
198 |
|
199 |
case ASSIGNMENT: |
200 |
handled = the_stamps->cutoffGroupAssign( the_event ); |
201 |
break; |
202 |
|
203 |
case MEMBERS: |
204 |
handled = the_stamps->cutoffGroupMembers( the_event ); |
205 |
break; |
206 |
|
207 |
case BLOCK_END: |
208 |
decr_block(); |
209 |
handled = the_stamps->cutoffGroupEnd( the_event ); |
210 |
break; |
211 |
|
212 |
default: |
213 |
the_event->err_msg = |
214 |
strdup( "Not a valid CutoffGroup event\n" ); |
215 |
return 0; |
216 |
} |
217 |
break; |
218 |
|
219 |
|
220 |
case ATOM_BLK: |
221 |
|
222 |
switch( the_event->event_type ){ |
223 |
|
224 |
case POSITION: |
225 |
handled = the_stamps->atomPosition( the_event ); |
226 |
break; |
227 |
|
228 |
case ORIENTATION: |
229 |
handled = the_stamps->atomOrientation( the_event ); |
230 |
break; |
231 |
|
232 |
case ASSIGNMENT: |
233 |
handled = the_stamps->atomAssign( the_event ); |
234 |
break; |
235 |
|
236 |
case BLOCK_END: |
237 |
decr_block(); |
238 |
handled = the_stamps->atomEnd( the_event ); |
239 |
break; |
240 |
|
241 |
default: |
242 |
the_event->err_msg = |
243 |
strdup( "Not a valid atom event\n" ); |
244 |
return 0; |
245 |
} |
246 |
break; |
247 |
|
248 |
case BOND_BLK: |
249 |
|
250 |
switch( the_event->event_type ){ |
251 |
|
252 |
case ASSIGNMENT: |
253 |
handled = the_stamps->bondAssign( the_event ); |
254 |
break; |
255 |
|
256 |
case MEMBERS: |
257 |
handled = the_stamps->bondMembers( the_event ); |
258 |
break; |
259 |
|
260 |
case CONSTRAINT: |
261 |
handled = the_stamps->bondConstraint( the_event ); |
262 |
break; |
263 |
|
264 |
case BLOCK_END: |
265 |
decr_block(); |
266 |
handled = the_stamps->bondEnd(the_event ); |
267 |
break; |
268 |
|
269 |
default: |
270 |
the_event->err_msg = |
271 |
strdup( "not a valid bond event\n" ); |
272 |
return 0; |
273 |
} |
274 |
break; |
275 |
|
276 |
case BEND_BLK: |
277 |
|
278 |
switch( the_event->event_type ){ |
279 |
|
280 |
case ASSIGNMENT: |
281 |
handled = the_stamps->bendAssign( the_event ); |
282 |
break; |
283 |
|
284 |
case MEMBERS: |
285 |
handled = the_stamps->bendMembers( the_event ); |
286 |
break; |
287 |
|
288 |
case CONSTRAINT: |
289 |
handled = the_stamps->bendConstraint( the_event ); |
290 |
break; |
291 |
|
292 |
case BLOCK_END: |
293 |
decr_block(); |
294 |
handled = the_stamps->bendEnd(the_event ); |
295 |
break; |
296 |
|
297 |
default: |
298 |
the_event->err_msg = |
299 |
strdup( "not a valid bend event\n" ); |
300 |
return 0; |
301 |
} |
302 |
break; |
303 |
|
304 |
case TORSION_BLK: |
305 |
|
306 |
switch( the_event->event_type ){ |
307 |
|
308 |
case ASSIGNMENT: |
309 |
handled = the_stamps->torsionAssign( the_event ); |
310 |
break; |
311 |
|
312 |
case MEMBERS: |
313 |
handled = the_stamps->torsionMembers( the_event ); |
314 |
break; |
315 |
|
316 |
case CONSTRAINT: |
317 |
handled = the_stamps->torsionConstraint( the_event ); |
318 |
break; |
319 |
|
320 |
case BLOCK_END: |
321 |
decr_block(); |
322 |
handled = the_stamps->torsionEnd(the_event ); |
323 |
break; |
324 |
|
325 |
default: |
326 |
the_event->err_msg = |
327 |
strdup( "not a valid torsion event\n" ); |
328 |
return 0; |
329 |
} |
330 |
break; |
331 |
|
332 |
case ZCONSTRAINT_BLK: |
333 |
|
334 |
switch( the_event->event_type ){ |
335 |
|
336 |
case ASSIGNMENT: |
337 |
handled = the_simParams->zConstraintAssign( the_event ); |
338 |
break; |
339 |
|
340 |
case BLOCK_END: |
341 |
decr_block(); |
342 |
handled = the_simParams->zConstraintEnd( the_event ); |
343 |
break; |
344 |
|
345 |
default: |
346 |
the_event->err_msg = |
347 |
strdup( "not a valid zConstraint event\n" ); |
348 |
return 0; |
349 |
} |
350 |
break; |
351 |
|
352 |
case COMPONENT_BLK: |
353 |
|
354 |
switch( the_event->event_type ){ |
355 |
|
356 |
case ASSIGNMENT: |
357 |
handled = the_simParams->componentAssign( the_event ); |
358 |
break; |
359 |
|
360 |
case BLOCK_END: |
361 |
decr_block(); |
362 |
handled = the_simParams->componentEnd(the_event ); |
363 |
break; |
364 |
|
365 |
default: |
366 |
the_event->err_msg = |
367 |
strdup( "not a valid component event\n" ); |
368 |
return 0; |
369 |
} |
370 |
break; |
371 |
|
372 |
default: |
373 |
the_event->err_msg = |
374 |
strdup( "event is not in a valid block type\n" ); |
375 |
return 0; |
376 |
} |
377 |
|
378 |
return handled; |
379 |
} |
380 |
|
381 |
void incr_block( block_type new_block ){ |
382 |
|
383 |
block_stack[block_stack_ptr] = current_block; |
384 |
block_stack_ptr++; |
385 |
|
386 |
if( block_stack_ptr >= MAX_NEST ){ |
387 |
sprintf( painCave.errMsg, |
388 |
"Event blocks nested too deeply\n" ); |
389 |
painCave.isFatal = 1; |
390 |
simError(); |
391 |
|
392 |
#ifdef IS_MPI |
393 |
if( worldRank == 0 ) mpiInterfaceExit(); |
394 |
#endif //is_mpi |
395 |
} |
396 |
|
397 |
else current_block = new_block; |
398 |
} |
399 |
|
400 |
|
401 |
void decr_block( void ){ |
402 |
|
403 |
block_stack_ptr--; |
404 |
|
405 |
if( block_stack_ptr < 0 ){ |
406 |
|
407 |
sprintf( painCave.errMsg, |
408 |
"Too many event blocks closed\n" ); |
409 |
painCave.isFatal = 1; |
410 |
simError(); |
411 |
|
412 |
#ifdef IS_MPI |
413 |
if( worldRank == 0 ) mpiInterfaceExit(); |
414 |
#endif //is_mpi |
415 |
|
416 |
} |
417 |
|
418 |
else current_block = block_stack[block_stack_ptr]; |
419 |
} |
420 |
|
421 |
void set_interface_stamps( MakeStamps* ms, Globals* g ){ |
422 |
|
423 |
the_stamps = ms; |
424 |
the_simParams = g; |
425 |
|
426 |
} |