forked from niemasd/GEMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemfc_nrm.c
436 lines (409 loc) · 14.8 KB
/
gemfc_nrm.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "nrm.h"
#include "common.h"
#include "para.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
/*
* main function of GEMF in C language
* Futing Fan
* Kansas State University
* Updates by Niema Moshiri (UC San Diego)
* Last Modified: March 2018
* Copyright (c) 2016, Futing Fan. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted
*/
/*
* main
*/
int _LOGLVL_;
void init_graph(Graph* graph, int echo);
void del_graph(Graph* graph);
void del_transition(Transition* tran);
void del_status(Status* sts);
void del_run(Run* run);
void load_graph(FILE* fil_para, Graph* graph);
void pre_init_graph(FILE* fil_para, Graph* graph);
void init_para(FILE* fil_para, Graph* graph, Transition* tran, Status* sts, Run* run, int echo);
void initi_status(FILE* fil_para, Graph* graph, Status* sts, int echo);
int main(int argc,char* argv[] ) {
FILE* fil_para= NULL;
int ret;
int echo= 1;
Graph graph;
Transition tran;
Status sts;
Run run;
_LOGLVL_= 0;
if( argc< 2){
fil_para= fopen( "para.txt", "r");
if( fil_para== NULL){
printf("cann't open default config file[para.txt], please check again\n");
return -1;
}
}
else{
fil_para= fopen( argv[1], "r");
if( fil_para== NULL){
printf(" para file [%s] read error\n", argv[1]);
return -1;
}
if( argc>2){
if( !strcmp(argv[2], "DEBUG")){
_LOGLVL_= 2;
}
else if( !strcmp(argv[2], "TRACE")){
_LOGLVL_= 1;
}
}
}
//initialize running conditions
init_para(fil_para, &graph, &tran, &sts, &run, echo);
//pre initialize graph, basically all kinds of sizes
pre_init_graph(fil_para, &graph);
//initialize graph, memory allocation
init_graph(&graph, echo);
sts._node_s= graph._s;
sts._node_e= graph._e;
initi_status( fil_para, &graph, &sts, echo);
//dump_status(&sts);
load_graph(fil_para, &graph);
//run simulation
ret= nrm( &graph, &tran, &sts, &run);
if( ret){
printf("simulation error[%d]\n", ret);
}
else{
printf("\nsimulation success!\n");
}
//clean up
fclose(fil_para);
del_graph(&graph);
del_transition(&tran);
del_status(&sts);
del_run(&run);
return 0;
}
void init_graph(Graph* graph, int echo){
size_t memo_size, layer;
LOG(1, __FILE__, __LINE__, "Init graph begin\n");
graph->edge= NULL;
graph->edge_w= NULL;
graph->index= NULL;
if( graph->weighted){
graph->edge_w= (Edge_w**)malloc(sizeof(Edge_w*)*graph->L);
if( graph->edge_w== NULL){
printf("Memory allocation failure for network list, size[%zu]\n", sizeof(Edge_w*)*graph->L);
exit( - 1);
}
for( layer= 0; layer< graph->L; layer++){
memo_size= ((size_t)(2 - graph->directed))*(graph->E[layer]);
if( memo_size< graph->E[layer]){
printf(" Arithematic overflow layer[%zu], size [%d]*[%zu]\n", layer, 2 - graph->directed, graph->E[layer]);
exit( - 1);
}
graph->edge_w[layer]= (Edge_w*)malloc(sizeof(Edge_w)*memo_size);
if( graph->edge_w[layer]== NULL){
printf("Memory allocation failure for layer[%zu], size[%zu]\n", layer, sizeof(Edge_w)*memo_size);
exit( - 1);
}
}
}
else{
graph->edge= (Edge**)malloc(sizeof(Edge*)*graph->L);
if( graph->edge== NULL){
printf("Memory allocation failure for network list, size[%zu]\n", sizeof(Edge*)*graph->L);
exit( - 1);
}
for( layer= 0; layer< graph->L; layer++){
memo_size= ((size_t)(2 - graph->directed))*(graph->E[layer]);
if( memo_size< graph->E[layer]){
printf(" Arithematic overflow layer[%zu], size [%d]*[%zu]\n", layer, 2 - graph->directed, graph->E[layer]);
exit( - 1);
}
graph->edge[layer]= (Edge*)malloc(sizeof(Edge)*memo_size);
if( graph->edge[layer]== NULL){
printf("Memory allocation failure for layer[%zu], size[%zu]\n", layer, sizeof(Edge)*memo_size);
exit( - 1);
}
}
}
LOG(1, __FILE__, __LINE__, "Init graph end\n");
if(echo){
print_graph_size(graph);
}
}
void del_graph(Graph* graph){
int layer;
if( graph->edge!= NULL){
for( layer= 0; layer< graph->L; layer++){
if( graph->edge[layer]!= NULL){
free(graph->edge[layer]);
}
}
graph->edge= NULL;
}
if( graph->edge_w!= NULL){
for( layer= 0; layer< graph->L; layer++){
if( graph->edge_w[layer]!= NULL){
free(graph->edge_w[layer]);
}
}
graph->edge_w= NULL;
}
if( graph->E!= NULL){
free( graph->E);
graph->E= NULL;
}
if( graph->index!= NULL){
for( layer= 0; layer< graph->L; layer++){
if( graph->index[layer]!= NULL){
free(graph->index[layer]);
}
}
free( graph->index);
}
}
void del_transition(Transition* tran){
size_t i, layer;
if( tran->nodal_trn!= NULL){
for( i= tran->_s; i< tran->M+ tran->_s; i++){
if( tran->nodal_trn[i]!= NULL){
free( tran->nodal_trn[i]);
}
}
free( tran->nodal_trn);
}
if( tran->edge_trn!= NULL){
for( layer= 0; layer< tran->L; layer++){
if( tran->edge_trn[layer]!= NULL){
for( i= tran->_s; i< tran->M+ tran->_s; i++){
if( tran->edge_trn[layer][i]!= NULL){
free( tran->edge_trn[layer][i]);
}
}
free( tran->edge_trn[layer]);
}
}
free( tran->edge_trn);
}
if( tran->inducer_lst!= NULL){
free( tran->inducer_lst);
}
}
void del_status(Status* sts){
if( sts->init_lst!= NULL){
free( sts->init_lst);
}
if( sts->init_cnt!= NULL){
free( sts->init_cnt);
}
}
void del_run(Run* run){
if( run->out_file!= NULL) free (run->out_file);
}
void load_graph(FILE* fil_para, Graph* graph){
printf("Reading network...\n");
FILE* fil_dat= NULL;
char* fil_nam= NULL;
NINT tr, i, j;
int ret;
size_t li, layer;
double t0= gettimenow();
//read in network matrix [i j weight]
locate_section( fil_para, "[DATA_FILE]");
fil_nam= (char*)malloc(sizeof(char)*MAX_LINE_LEN);
for(layer=0; layer< graph->L; layer++){
LOG(2, __FILE__, __LINE__, "Read layer[%d]\n", layer+ 1);
fget_next_item( fil_para, fil_nam, MAX_LINE_LEN);
fil_dat= fopen( fil_nam, "r");
if( fil_dat== NULL){
printf("Read file[%s] error\n", fil_nam);
exit( -1);
}
//skip comment lines on top
skip_top_comment( fil_dat, '#');
//weighted
if( graph->weighted){
LOG(2, __FILE__, __LINE__, "Read weighted network\n");
for ( li= 0; li< graph->E[layer]; li++) {
if (!(li % 10000000)&& li) {
time_print("[", gettimenow() - t0, " ]\t");
printf("layer[%zu] ", layer+ 1);
kilobit_print("[ ", (LONG)li, "/");
kilobit_print("", (LONG)graph->E[layer], " ] edges get\n");
}
ret= fscanf( fil_dat, fmt_n fmt_n " %lf", &i, &j, &(graph->edge_w[layer][li].w));
if( ret != 3){
printf("Error! Expecting 3 columns, getting %d\n", ret);
exit(-1);
}
if( i< graph->_s || i> graph->_e){
printf("node["fmt_n"of layer[%zu]edge[%zu] out of range["fmt_n"/"fmt_n"]\n", i, layer, li, graph->_s, graph->_e);
}
if( j< graph->_s || j> graph->_e){
printf("node["fmt_n"of layer[%zu]edge[%zu] out of range["fmt_n"/"fmt_n"]\n", j, layer, li, graph->_s, graph->_e);
}
graph->edge_w[layer][li].i= i;
graph->edge_w[layer][li].j= j;
if( !graph->directed){
graph->edge_w[layer][graph->E[layer]+ li]= graph->edge_w[layer][li];
tr= graph->edge_w[layer][graph->E[layer]+ li].i;
graph->edge_w[layer][graph->E[layer]+ li].i= graph->edge_w[layer][graph->E[layer]+ li].j;
graph->edge_w[layer][graph->E[layer]+ li].j= tr;
}
}
}
//unweighted
else{
LOG(2, __FILE__, __LINE__, "Read unweighted network\n");
for ( li= 0; li< graph->E[layer]; li++) {
if (!(li % 10000000)&& li) {
time_print("[", gettimenow() - t0, " ]\t");
printf("layer[%zu] ", layer+ 1);
kilobit_print("[ ", (LONG)li, "/");
kilobit_print("", (LONG)graph->E[layer], " ] edges get\n");
}
ret= fscanf( fil_dat, fmt_n fmt_n , &i, &j);
if( ret != 2){
printf("Error! Expecting 2 columns, getting %d\n", ret);
exit(-1);
}
if( i< graph->_s || i> graph->_e){
printf("node["fmt_n"of layer[%zu]edge[%zu] out of range["fmt_n"/"fmt_n"]\n", i, layer, li, graph->_s, graph->_e);
}
if( j< graph->_s || j> graph->_e){
printf("node["fmt_n"of layer[%zu]edge[%zu] out of range["fmt_n"/"fmt_n"]\n", j, layer, li, graph->_s, graph->_e);
}
graph->edge[layer][li].i= i;
graph->edge[layer][li].j= j;
if( !graph->directed){
graph->edge[layer][graph->E[layer]+ li]= graph->edge[layer][li];
tr= graph->edge[layer][graph->E[layer]+ li].i;
graph->edge[layer][graph->E[layer]+ li].i= graph->edge[layer][graph->E[layer]+ li].j;
graph->edge[layer][graph->E[layer]+ li].j= tr;
}
}
}
time_print("[", gettimenow() - t0, " ]\t");
printf("layer[%zu] ", layer+ 1);
kilobit_print("[ ", (LONG)li, "/");
kilobit_print("", (LONG)graph->E[layer], " ] edges get\n");
if( !graph->directed){
graph->E[layer]+= graph->E[layer];
}
fclose(fil_dat);
}
time_print( "initial time cost[ ", gettimenow() - t0, " ]\n");
free(fil_nam);
}
void pre_init_graph(FILE* fil_para, Graph* graph){
LINE str;
size_t layer;
LONG val;
//scan all network files, analysis metrics
if( item_count( fil_para, "[NETWORK_INFO]")< (int)(2+ graph->L )){
//missing NETWORK_INFO section or section incomplete, analysis from all network file
printf("Analysis network info automaticly\n");
locate_section( fil_para, "[DATA_FILE]");
if( analysis_network( fil_para, graph)< 0)
exit(-1);
}
else{
locate_section( fil_para, "[NETWORK_INFO]");
fget_next_item( fil_para, str, MAX_LINE_LEN);
sscanf( str, "%d", &graph->weighted);
fget_next_item( fil_para, str, MAX_LINE_LEN);
sscanf( str, fmt_n" %lld", &graph->_s, &val);
check_int_range( val);
graph->V= (NINT)val - graph->_s+ 1;
graph->_e= graph->_s+ graph->V;
graph->E= (size_t*)malloc(sizeof(size_t)*graph->L);
if( graph->E== NULL){
printf("Memory allocation failure for edges number list, size[%zu]\n", sizeof(size_t)*graph->L);
exit( - 1);
}
for( layer= 0; layer< graph->L; layer++){
fget_next_item( fil_para, str, MAX_LINE_LEN);
sscanf( str, "%zu", &graph->E[layer]);
printf(" layer[%zu],", layer);
kilobit_print(" [", (LONG)graph->E[layer], " ]edges\n");
}
}
}
void init_para(FILE* fil_para, Graph* graph, Transition* tran, Status* sts, Run* run, int echo){
//scan input file, analysis key parameter
int ret;
ret= item_count( fil_para, "[DATA_FILE]");
if( ret<= 0){
printf("wrong [DATA_FILE] config\n");
exit( -1);
}
graph->L= (size_t)ret;
tran->L= graph->L;
tran->M= (size_t)item_count( fil_para, "[NODAL_TRAN_MATRIX]");
sts->M= tran->M;
printf("[compartment number]\t[%zu]\n", tran->M);
printf("[layer number]\t\t[%zu]\n", tran->L);
//read in status begin num
sts->_s= (size_t)getValInt( fil_para, "[STATUS_BEGIN]", echo);
tran->_s= sts->_s;
//read in directed flag
if( getValInt( fil_para, "[DIRECTED]", echo)> 0){
graph->directed= 1;
}
else{
graph->directed= 0;
}
//read in output file names
run->out_file= getValStr( fil_para, "[OUT_FILE]", MAX_LINE_LEN, echo);
//read in max_time
run->max_time= getValDbl( fil_para, "[MAX_TIME]", echo);
//read in max events
run->max_events= (size_t)getValInt( fil_para, "[MAX_EVENTS]", echo);
//read in run times
run->sim_rounds = (size_t)getValInt( fil_para, "[SIM_ROUNDS]", echo);
//read in sample size
run->interval_num = (size_t)getValInt( fil_para, "[INTERVAL_NUM]", echo);
//read in random number seed
sts->random_seed = (size_t)getValInt( fil_para, "[RANDOM_SEED]", echo);
//print inducer for signle simulation if presented and non zero
run->show_inducer= 0;
if( item_count( fil_para, "[SHOW_INDUCER]")> 0){
run->show_inducer = strcmp(getValStr( fil_para, "[SHOW_INDUCER]", MAX_LINE_LEN, echo), "0");
}
//read in inducer list
tran->inducer_lst= getValSize_tLst( fil_para, "[INDUCER_LIST]", graph->L, echo);
//read in nodal transition rate matrix
tran->nodal_trn= getValMatrix( fil_para, "[NODAL_TRAN_MATRIX]", tran->M, tran->_s, MAX_LINE_LEN, echo);
//read in edge based rate matrix
tran->edge_trn= getValMatrixLst( fil_para, "[EDGED_TRAN_MATRIX]", tran->M, tran->L, tran->_s, MAX_LINE_LEN, echo);
}
void initi_status(FILE* fil_para, Graph* graph, Status* sts, int echo){
char *fil_nam= NULL;
FILE* fil_sts= NULL;
size_t i;
//read in status file
LOG(2, __FILE__, __LINE__, "Read in status file\n");
fil_nam= getValStr( fil_para, "[STATUS_FILE]", MAX_LINE_LEN, echo);
fil_sts= fopen( fil_nam, "r");
if( fil_sts== NULL){
printf("Read file[%s] error\n", fil_nam);
exit( -1);
}
if( initial_con( fil_sts, graph, sts)< 0){
printf("initial status failed\n");
exit(-1);
}
printf("[initial population]\t[");
for( i= sts->_s; i< sts->M+ sts->_s; i++){
kilobit_print(" ", (LONG)sts->init_cnt[i], " ");
}
printf("]\n");
fclose(fil_sts);
LOG(2, __FILE__, __LINE__, "Read in status file success\n");
if( fil_nam!= NULL) free (fil_nam);
}