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