-
Notifications
You must be signed in to change notification settings - Fork 17
/
Micro.h
315 lines (226 loc) · 8.11 KB
/
Micro.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/////////////////////////////////////////////////////////////////////////////////////////
//
// gro is protected by the UW OPEN SOURCE LICENSE, which is summarized here.
// Please see the file LICENSE.txt for the complete license.
//
// THE SOFTWARE (AS DEFINED BELOW) AND HARDWARE DESIGNS (AS DEFINED BELOW) IS PROVIDED
// UNDER THE TERMS OF THIS OPEN SOURCE LICENSE (“LICENSE”). THE SOFTWARE IS PROTECTED
// BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THIS SOFTWARE OTHER THAN AS
// AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
//
// BY EXERCISING ANY RIGHTS TO THE SOFTWARE AND/OR HARDWARE PROVIDED HERE, YOU ACCEPT AND
// AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
// CONSIDERED A CONTRACT, THE UNIVERSITY OF WASHINGTON (“UW”) GRANTS YOU THE RIGHTS
// CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
//
// TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
//
//
#ifndef _MICRO_H_
#define _MICRO_H_
#ifndef NOGUI
#include "GroPainter.h"
#endif
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <list>
#include <vector>
#include <string>
#include <map>
#include <stdint.h>
#include "ccl.h"
#include "Cell.h"
#include "Defines.h"
#include "Utility.h"
#ifndef NOGUI
#include "Theme.h"
#endif
//void set_throttle ( bool val );
void register_gro_functions ( void );
////////////////////////////////////////////////////////////////////////////////
// Signal
//
class Signal {
public:
Signal ( cpVect p, cpVect q, int nx, int ny, float kdi, float kde );
~Signal ( void );
inline int row ( float x ) { return ( (int) floor ( (x-minp.x)/dw ) ); }
inline int col ( float y ) { return ( (int) floor ( (y-minp.y)/dh ) ); }
inline float max ( float a, float b ) { return a<b?b:a; }
void set ( int i, int j, float c );
void set ( float x, float y, float c );
void set_rect ( float x1, float y1, float x2, float y2, float c );
void inc ( int i, int j, float c );
void inc ( float x, float y, float c );
void dec ( int i, int j, float c );
void dec ( float x, float y, float c );
float get ( int i, int j );
float get ( float x, float y );
void zero ( void );
void integrate ( float dt );
inline int get_numx ( void ) { return numx; }
inline int get_numy ( void ) { return numy; }
inline cpVect get_minp ( void ) { return minp; }
inline cpVect get_maxp ( void ) { return maxp; }
inline float get_dw ( void ) { return dw; }
inline float get_dh ( void ) { return dh; }
inline float get_val ( int i, int j ) { return sig[i][j]; }
inline std::vector< std::vector<float> > * get_signal_matrix ( void ) {
return &sig;
}
private:
int numx, numy;
cpVect minp, maxp;
float w, h, dw, dh;
float kdiff, kdeg;
std::vector< std::vector<float> > sig, dsig;
};
class Reaction {
public:
Reaction(float k);
void add_reactant ( int i );
void add_product ( int i );
void integrate ( std::vector<Signal *> * signal_list, int i, int j, float dt );
private:
std::vector<int> reactants, products;
float rate;
};
////////////////////////////////////////////////////////////////////////////////
// Program
//
class MicroProgram {
public:
MicroProgram ( void ) {}
virtual void init ( World * ) {}
virtual void update ( World *, Cell * ) {}
virtual Value * eval ( World * , Cell * , Expr * ) { return NULL; }
virtual void world_update ( World * ) {}
virtual void destroy ( World * ) {}
virtual std::string name ( void ) const { return "Untitled Program"; }
private:
};
////////////////////////////////////////////////////////////////////////////////
// Messages
//
class MessageHandler {
public:
MessageHandler ( void );
~MessageHandler ( void );
void add_message ( int i, const char * str );
void clear_messages ( int i ); // clears a quadrant
void clear_messages_all ( void ); // clears all quadrants
#ifndef NOGUI
void render ( GroPainter * painter );
#endif
private:
std::list<std::string> message_buffer[4];
};
////////////////////////////////////////////////////////////////////////////////
// World
//
struct Barrier {
float x1, y1, x2, y2;
};
class GroThread;
class World {
public:
#ifdef NOGUI
World ( void );
#else
World ( GroThread * ct );
#endif
~World ( void );
void set_program ( MicroProgram * p ) { prog = p; }
MicroProgram * get_program ( void ) { return prog; }
void init ();
void restart ( void );
void update ();
#ifndef NOGUI
void render ( GroPainter * painter );
#endif
void add_cell ( Cell * c );
void add_signal ( Signal * s );
void add_reaction ( Reaction r ) { reaction_list.push_back(r); }
float get_signal_value ( Cell * c, int i );
void emit_signal ( Cell * c, int i, float ds );
void absorb_signal ( Cell * c, int i, float ds );
std::vector< std::vector<float> > * get_signal_matrix ( int i );
int num_signals ( void ) { return signal_list.size(); }
inline void set_signal ( int i, float x, float y, float c ) { signal_list[i]->set(x,y,c); }
inline void set_signal_rect ( int i, float x1, float y1, float x2, float y2, float c ) { signal_list[i]->set_rect(x1,y1,x2,y2,c); }
inline cpSpace * get_space ( void ) { return space; }
void set_chemostat_mode ( bool mode ) { chemostat_mode = mode; }
void toggle_chemostat_mode ( void ) { chemostat_mode = chemostat_mode ? false : true; }
bool get_chemostat_mode ( void ) { return chemostat_mode; }
cpVect chemostat_flow ( float, float y, float mag );
void print ( void );
void set_print_rate ( int r ) { print_rate = r; }
bool snapshot ( const char * path );
void set_movie_rate ( int r ) { movie_rate = r; }
void histogram ( float x, float y, float width, float height, int channel );
void scatter ( float x, float y, float width, float height, int channel1, int channel2 );
float get_time ( void ) { return t; }
int get_pop_size ( void ) { return population->size(); }
void message ( int i, std::string str ) { message_handler.add_message ( i, str.c_str() ); }
void clear_messages ( int i ) { message_handler.clear_messages(i); }
void select_cells ( int x1, int y1, int x2, int y2 );
void deselect_all_cells ( void );
inline void set_sim_dt ( float x ) { parameters["dt"] = x; }
inline float get_sim_dt ( void ) { return parameters["dt"]; }
inline void set_chip_dt ( float x ) { chip_dt = x; }
inline float get_chip_dt ( void ) { return chip_dt; }
inline void set_param ( std::string str, float val ) { parameters[str] = val;
//
//for( std::map<std::string,float>::iterator ii = parameters.begin(); ii != parameters.end(); ++ii ) {
// printf ( "%s -> %f\n", (*ii).first.c_str(), (*ii).second );
//}
}
inline bool initialized (void ) { return program_initialized; }
// note that if the parameter doesn't exist, the [] operator will insert it
// with a default value, which I assume would be 0.0 for a float.
inline float get_param ( std::string str ) { return parameters[str]; }
inline std::map<std::string,float> get_param_map ( void ) { return parameters; }
Value * map_to_cells ( Expr * e );
void set_theme ( Value * v );
void set_zoom ( float z ) { zoom = z; };
#ifndef NOGUI
Theme * get_theme ( void ) { return &theme; }
#endif
void emit_message ( std::string str, bool clear = false );
void set_stop_flag ( bool f ) { stop_flag = f; }
bool get_stop_flag ( void ) { bool b = stop_flag; stop_flag = false; return b; }
std::vector<FILE *> fileio_list;
void dump ( FILE * fp );
void add_barrier ( float x1, float y1, float x2, float y2 );
private:
cpSpace * space;
std::list<Cell *> * population;
std::vector<Signal *> signal_list;
std::vector<Reaction> reaction_list;
MicroProgram * prog;
bool chemostat_mode;
int next_id;
int step;
int print_rate, movie_rate;
float t;
float max_val;
bool program_initialized;
std::string gro_message;
MessageHandler message_handler;
float zoom;
std::list<Barrier> * barriers;
#ifndef NOGUI
Theme theme;
#endif
float chip_dt;
std::map<std::string,float> parameters;
#ifndef NOGUI
GroThread * calling_thread;
#endif
bool stop_flag;
};
#endif