Exemplo n.º 1
0
static void check_network(struct network *n)
{
	if (!n->n_beaconlen || !n->n_handshake || !n->n_ssid[0]) return;

	fix_beacon(n);

	print_network(n);

	save_network(n);
}
Exemplo n.º 2
0
static void test_wlan_info(int argc, char **argv)
{
	enum wlan_connection_state state;
	struct wlan_network sta_network;
	struct wlan_network uap_network;
	int sta_found = 0;

	if (wlan_get_connection_state(&state)) {
		wmprintf("Error: unable to get STA connection"
						" state\r\n");
	} else {
		switch (state) {
		case WLAN_CONNECTED:
			if (!wlan_get_current_network(&sta_network)) {
				wmprintf("Station connected to:\r\n");
				print_network(&sta_network);
				sta_found = 1;
			} else
				wmprintf("Station not connected\r\n");
			break;
		default:
			wmprintf("Station not connected\r\n");
			break;
		}
	}

	if (wlan_get_current_uap_network(&uap_network))
		wmprintf("uAP not started\r\n");
	else {
		/* Since uAP automatically changes the channel to the one that
		 * STA is on */
		if (sta_found == 1)
			uap_network.channel = sta_network.channel;

		if (uap_network.role == WLAN_BSS_ROLE_UAP)
			wmprintf("uAP started as:\r\n");

		print_network(&uap_network);
	}
}
Exemplo n.º 3
0
void print_network_list(void)
{
        struct wl_network_list_t* wl_network_list;
        uint8_t i;

        wl_get_network_list(&wl_network_list);

        if (wl_network_list->cnt == 0)
                printk("no nets found\n");

        for (i = 0; i < wl_network_list->cnt; i++)
                print_network(wl_network_list->net[i]);
}
Exemplo n.º 4
0
void network_generator::find_network_sol(){
	
	for(int i=0;i<10000;i++){
		network_init();
		if(network_gen()){
			cout << i << " network build done !" << endl;
		}
		//network_evolution();
		//print_network();
		//cout << "seed #1" << endl;
		//getchar();
	}
	cout << "done" << endl;
	print_network();
	
}
Exemplo n.º 5
0
static void test_wlan_list(int argc, char **argv)
{
	struct wlan_network network;
	unsigned int count;
	int i;

	if (wlan_get_network_count(&count)) {
		wmprintf("Error: unable to get number of networks\r\n");
		return;
	}

	wmprintf("%d network%s%s\r\n", count, count == 1 ? "" : "s",
		       count > 0 ? ":" : "");
	for (i = 0; i < count; i++) {
		if (wlan_get_network(i, &network) == WLAN_ERROR_NONE)
			print_network(&network);
	}
}
Exemplo n.º 6
0
void NN::write_file(string filename) //write network to file
{
  ofstream file (filename.c_str());

  if(!file.is_open())
    {
      printf("Error: Could not open file '%s'!",filename.c_str());
      return;
    }

  file << "*********************************************************\n";
  file << "This is an autogenerated file containing a neural network\n";
  file << "*********************************************************\n\n";

  file << print_network();

  file.close();
  
}
Exemplo n.º 7
0
cmd_state_t
cmd_status(int argc, char* argv[], void* ctx)
{
        struct net_cfg *ncfg = ctx;
        struct wl_network_t* net;
        uint8_t mac[WL_MAC_ADDR_LENGTH];

        printk("wl_api version " WL_API_RELEASE_NAME "\n");
        /* print mac address */
        if (wl_get_mac_addr(mac) != WL_SUCCESS) {
                printk("failed to get mac address\n");
                return CMD_DONE;
        }
        printk("hw addr: %s\n", mac2str(mac));

        /* print network info */
        net = wl_get_current_network();
        printk("link status: ");
        if (!net) {
                printk("down\n");
                return CMD_DONE;
        }
        print_network(net);

        /* print ip address */
        if (netif_is_up(netif_default))
                printk("ip addr: %s\n", ip2str(netif_default->ip_addr));
        else
                printk("ip addr: none\n");
        printk("dhcp : ");
        if (ncfg->dhcp_enabled) {
                printk("enabled\n");
        }
        else {
                printk("disabled\n");
        }


        return CMD_DONE;
}
Exemplo n.º 8
0
/**
 * Entry point for all mapping strategies
 */
int do_mapping(network_t *network, options_t *opt, mapping_t *mapping) {
    int ret = ORCC_OK;
    idx_t *part;
    ticks startTime, endTime;
    assert(network != NULL);
    assert(opt != NULL);
    assert(mapping != NULL);

    part = (idx_t*) malloc(sizeof(idx_t) * (network->nb_actors));

    if(check_verbosity(ORCC_VL_VERBOSE_2)) {
        print_network(network);
    }

    startTime = getticks();

    if (opt->nb_processors != 1) {
        switch (opt->strategy) {
#ifdef METIS_ENABLE
        case ORCC_MS_METIS_REC:
            ret = do_metis_recursive_partition(network, opt, part);
            break;
        case ORCC_MS_METIS_KWAY_CV:
            ret = do_metis_kway_partition(network, opt, part, METIS_OBJTYPE_CUT); /*TODO : should be METIS_OBJTYPE_VOL : Metis seem's to invert its options */
            break;
        case ORCC_MS_METIS_KWAY_EC:
            ret = do_metis_kway_partition(network, opt, part, METIS_OBJTYPE_VOL); /*TODO : should be METIS_OBJTYPE_CUT : Metis seem's to invert its options */
            break;
#endif
        case ORCC_MS_ROUND_ROBIN:
            ret = do_round_robbin_mapping(network, opt, part);
            break;
        case ORCC_MS_QM:
            ret = do_quick_mapping(network, opt, part);
            break;
        case ORCC_MS_WLB:
            ret = do_weighted_round_robin_mapping(network, opt, part);
            break;
        case ORCC_MS_COWLB:
            ret = do_weighted_round_robin_comm_mapping(network, opt, part);
            break;
        case ORCC_MS_KRWLB:
            ret = do_KLR_mapping(network, opt, part);
            break;
        default:
            break;
        }
    } else {
        int i;
        for (i = 0; i < network->nb_actors; i++) {
            part[i] = 0;
        }
    }

    endTime = getticks();

    set_mapping_from_partition(network, part, mapping);

    if(check_verbosity(ORCC_VL_VERBOSE_1)) {
        print_mapping(mapping);
        print_load_balancing(mapping);
        print_edge_cut(network);
        print_orcc_trace(ORCC_VL_VERBOSE_2, "Mapping time : %2.lf", elapsed(endTime, startTime));
    }

    free(part);
    return ret;
}
Exemplo n.º 9
0
int Network3::run_PLA(double& time, double maxTime, double sampleTime,
					  double& step, double maxStep, double stepInterval,
					  mu::Parser& stop_condition, bool print_on_stop,
					  char* prefix,
					  bool print_cdat, bool print_func, bool print_save_net, bool print_end_net,
					  bool additional_pla_output,
					  bool verbose){

	// Output files
	string outpre(prefix);
	bool print_classif = additional_pla_output;
	// ...

	// Species file (must exist)
	FILE* cdat = NULL;
	string cFile = outpre + ".cdat";
	if ((cdat = fopen(cFile.c_str(),"r"))){
		fclose(cdat);
		cdat = fopen(cFile.c_str(),"a");
	}
	else {
		cout << "Error in Network3::run_PLA(): Concentrations file \"" << cFile << "\" doesn't exist. Exiting." << endl;
		exit(1);
	}

	// Observables file (optional)
	FILE* gdat = NULL;
	string gFile = outpre + ".gdat";
	if ((gdat = fopen(gFile.c_str(),"r"))){
		fclose(gdat);
		gdat = fopen(gFile.c_str(),"a");
	}
	else{
//		cout << "Warning: Groups file \"" << gFile << "\" doesn't exist." << endl;
	}

	// Functions file (optional)
/*	FILE* fdat = NULL;
	string fFile = outpre + ".fdat";
	if ((fdat = fopen(fFile.c_str(),"r"))){
		fclose(fdat);
		fdat = fopen(fFile.c_str(),"a");
	}
	else{
//		cout << "Warning: Functions file \"" << fFile << "\" doesn't exist." << endl;
	}*/

	// PLA-specific output files
	FILE* classif = NULL;
	if (print_classif){
		if ((classif = fopen((outpre+"_classif.pla").c_str(),"r"))){
			fclose(classif);
			classif = fopen((outpre+"_classif.pla").c_str(),"a");
		}
		else{
			cout << "Error in Network3::run_PLA(): 'print_classif' flag set but classifications file \""
				 << (outpre+"_classif.pla") << "\" doesn't exist. Exiting." << endl;
			exit(1);
		}
	}
	// ...

	// Identify observables involved in functions
	vector<unsigned int> funcObs;
	for (unsigned int i=0;i < FUNCTION.size();i++){
		map<string,double*> var = FUNCTION[i]->first->p->GetUsedVar();
		for (unsigned int j=0;j < OBSERVABLE.size();j++){
			if (var.find(OBSERVABLE[j]->first->name) != var.end()){
				bool already = false;
				for (unsigned int k=0;k < funcObs.size() && !already;k++){
					if (funcObs[k] == j){
						already = true;
					}
				}
				if (!already){ // add to the list
					funcObs.push_back(j);
				}
			}
		}
	}

	// Prepare for simulation
	double nextOutputTime = time + sampleTime;
	double nextOutputStep = stepInterval;
	while (nextOutputStep <= step) nextOutputStep += stepInterval;
	bool lastOut = true;

	// Simulation loop
//	PLA_SIM->rc.forceClassifications(RxnClassifier::EXACT_STOCHASTIC);
	string print_net_message;
	while (time < maxTime && step < maxStep && !stop_condition.Eval())
	{
		// Next step
		step++;
		PLA_SIM->nextStep();

		if (PLA_SIM->tau < INFINITY && PLA_SIM->tau > -INFINITY){
			time += PLA_SIM->tau;
		}
		else break;

		// Error check
		if (PLA_SIM->tau <= 0.0){
			cout << "Error in Network3::run_PLA(): tau = " << PLA_SIM->tau << ". Shouldn't happen. Exiting." << endl;
			exit(1);
		}

		// Is it time to output?
		lastOut = false;
		if (time >= nextOutputTime || step >= nextOutputStep) // YES
		{
			// Update all observables
			for (unsigned int i=0;i < OBSERVABLE.size();i++){
				OBSERVABLE[i]->second = OBSERVABLE[i]->first->getValue();
			}
			// Update all functions
			for (unsigned int i=0;i < FUNCTION.size();i++){
				FUNCTION[i]->second = FUNCTION[i]->first->Eval();
			}
			// Output to file
			if (print_cdat) Network3::print_species_concentrations(cdat,time);
			if (gdat) Network3::print_observable_concentrations(gdat,time,print_func);
			if (print_func) Network3::print_function_values(gdat,time);
			if (print_save_net){ // Write current system state to .net file
				// Collect species populations and update network concentrations vector
				double* pops = new double[SPECIES.size()];
				for (unsigned int j=0;j < SPECIES.size();j++){
					pops[j] = SPECIES[j]->population;
				}
				set_conc_network(pops);
				delete pops;
				// Print network w/ current species populations using network::print_network()
				char buf[1000];
				sprintf(buf, "%s_save.net", prefix);
				FILE* out = fopen(buf, "w");
				print_network(out);
				fclose(out);
				print_net_message = " Wrote NET file to " + (string)buf;
//				fprintf(stdout, " Wrote NET file to %s", buf);
			}
			if (print_classif){
				fprintf(classif,"%19.12e",time);
				fprintf(classif," %19.19g",step);
				for (unsigned int v=0;v < PLA_SIM->classif.size();v++){
					fprintf(classif, " %10d", PLA_SIM->classif[v]);
				}
				fprintf(classif,"\n");
				fflush(classif);
			}
			// Output to stdout
			if (verbose){
				cout << "\t" << fixed << setprecision(6) << time << "\t" << setprecision(0) << step;
//				for (unsigned int i=0;i < OBSERVABLE.size();i++){
//					cout << "\t" << OBSERVABLE[i]->second;
//				}
				if (print_save_net){
					fprintf(stdout, "%s", print_net_message.c_str());
				}
				cout << endl;
			}
			// Get next output time and step
			if (time >= nextOutputTime) nextOutputTime += sampleTime;
			if (step >= nextOutputStep) nextOutputStep += stepInterval;
			lastOut = true;
		}
		else{ // NO
			// Only update observables that are involved in functions
			for (unsigned int i=0;i < funcObs.size();i++){
				OBSERVABLE[funcObs[i]]->second = OBSERVABLE[funcObs[i]]->first->getValue();
			}
			// Update all functions
			for (unsigned int i=0;i < FUNCTION.size();i++){
				FUNCTION[i]->second = FUNCTION[i]->first->Eval();
			}
		}
	}

	// Final output
	if (!lastOut)
	{
		// Update all observables
		for (unsigned int i=0;i < OBSERVABLE.size();i++){
			OBSERVABLE[i]->second = OBSERVABLE[i]->first->getValue();
		}
		// Update all functions
		for (unsigned int i=0;i < FUNCTION.size();i++){
			FUNCTION[i]->second = FUNCTION[i]->first->Eval();
		}
		// Output to file
		if (print_cdat) Network3::print_species_concentrations(cdat,time);
		// Don't print if stopping condition met and !print_on_stop (must print to CDAT)
		if (!(stop_condition.Eval() && !print_on_stop)){
			if (gdat) Network3::print_observable_concentrations(gdat,time,print_func);
			if (print_func) Network3::print_function_values(gdat,time);
			string print_net_message;
			if (print_save_net){ // Write current system state to .net file
				// Collect species populations and update network concentrations vector
				double pops[SPECIES.size()];
				for (unsigned int j=0;j < SPECIES.size();j++){
					pops[j] = SPECIES[j]->population;
				}
				set_conc_network(pops);
				// Print network w/ current species populations using network::print_network()
				char buf[1000];
				sprintf(buf, "%s_save.net", prefix);
				FILE* out = fopen(buf, "w");
				print_network(out);
				fclose(out);
				print_net_message = " Wrote NET file to " + (string)buf;
			}
			if (print_classif){
				fprintf(classif,"%19.12e",time);
				for (unsigned int v=0;v < PLA_SIM->classif.size();v++){
					fprintf(classif, " %10d", PLA_SIM->classif[v]);
				}
				fprintf(classif,"\n");
				fflush(classif);
			}
		}
		// Output to stdout
		if (verbose){
			cout << "\t" << fixed << setprecision(6) << time << "\t" << setprecision(0) << step;
//			for (unsigned int i=0;i < OBSERVABLE.size();i++) cout << "\t" << OBSERVABLE[i]->second;
			if (print_save_net) fprintf(stdout, "%s", print_net_message.c_str());
			cout << endl;
		}
	}

	// Messages if stopping conditions met
	if (stop_condition.Eval()){ // Stop condition satisfied
		cout << "Stopping condition " << stop_condition.GetExpr() << "met in PLA simulation." << endl;
	}
//	else if (step >= startStep + maxSteps){ // maxSteps limit reached
//		cout << "Maximum step limit (" << maxSteps << ") reached in PLA simulation." << endl;
//	}

	// If print_end_net = true, collect species populations and update network concentrations vector
	if (print_end_net){
		double pops[SPECIES.size()];
		for (unsigned int j=0;j < SPECIES.size();j++){
			pops[j] = SPECIES[j]->population;
		}
		set_conc_network(pops);
	}

	// Close files
	fclose(cdat);
	if (gdat) fclose(gdat);
//	if (fdat) fclose(fdat);
	if (classif) fclose(classif);

	// Return value
	if (time >= maxTime) return 0;
	else if (step >= maxStep) return -1;
	else return -2; // stop condition met
}
Exemplo n.º 10
0
int main(int argc, char *argv[]){
	/* Output message */
	fprintf(stdout, "run_network %s\n", RUN_NETWORK_VERSION);
	fflush(stdout);
	// Variables //
	register int i/*, j*/;
    char *netfile_name, *network_name;
    char *group_input_file_name = NULL;
    char *save_file_name;
    FILE *netfile, *conc_file, *group_file/*, *func_file*/, *out, *flux_file, *species_stats_file;
    int net_line_number, group_line_number, n_read;
    Elt_array *species, *rates/*, *parameters*/;
    Group *spec_groups = NULL;
    Rxn_array *reactions;
    int n, n_sample;
    double t_start=0.0, t, dt, atol = 1.0e-8, rtol = 1.0e-8;
    double sample_time, *sample_times = 0x0/*, *st, t1*/;
    char c, buf[1000], *outpre = NULL;
    int argleft, iarg = 1, error = 0;
    int save_file = 0;
    int check_steady_state = 0;
    int seed = -1;
    int remove_zero = 1;
    int print_flux = 0, print_end_net = 0, print_save_net = 0, enable_species_stats = 0;
    int gillespie_update_interval = 1;
    int verbose=0;
    int continuation=0;
    double *conc, *conc_last, *derivs;
    struct program_times ptimes;
//  extern char *optarg;
//  extern int optind, opterr;
    //
    // Allowed propagator types
    enum {SSA, CVODE, EULER, RKCS, PLA};
    int propagator = CVODE;
    int SOLVER = DENSE;
    int outtime = -1;
    //
    double maxSteps = INFINITY;//LONG_MAX;//-1;
    double stepInterval = INFINITY;//LONG_MAX;// -1;
    string pla_config; // No default
    bool print_cdat = true, print_func = false;
    bool additional_pla_output = false; // Print PLA-specific data (e.g., rxn classifications)
    bool print_on_stop = true; // Print to file if stopping condition met?
    string stop_string = "0";
    mu::Parser stop_condition;

    if (argc < 4) print_error();

    /* Process input options */
    while ( argv[iarg][0] == '-'  ){
    	c = argv[iarg++][1];
    	switch (c){
    	case 'a':
    		atol = atof(argv[iarg++]);
    		break;
    	case 'b':
    		if(SOLVER == DENSE) SOLVER = GMRES;
    		else SOLVER = GMRES_J;
    		break;
    	case 'c':
    		check_steady_state = 1;
    		break;
    	case 'd':
    		if (SOLVER == DENSE) SOLVER = DENSE_J;
    		else SOLVER = GMRES_J;
    		break;
    	case 'e':
    		print_end_net = 1;
    		break;
    	case 'f':
    		print_flux = 1;
    		break;
    	case 'g':
    		group_input_file_name = argv[iarg++];
    		break;
    	case 'h':
    		seed = atoi(argv[iarg++]);
    		if (seed == INT_MAX){
    			cout << "Warning in run_network: Your seed (" << seed  << ") equals INT_MAX." << endl;
    			cout << "Are you sure you didn't enter a seed larger than INT_MAX?" << endl;
    			cout << "If you did you could be getting duplicate results." << endl;
    		}
    		break;
    	case 'i':
    		t_start = atof(argv[iarg++]);
    		break;
    	case 'j':
    		enable_species_stats = 1;
    		break;
    	case 'k':
    		remove_zero = 0;
    		break;
    	case 'm':
    		propagator = SSA;
    		break;
    	case 'n':
    		print_save_net = 1;
    		break;
    	case 'o':
    		outpre = argv[iarg++];
    		break;
    	case 'p':
    		if (strcmp(argv[iarg],"ssa") == 0) propagator= SSA;
    		else if (strcmp(argv[iarg],"cvode") == 0) propagator= CVODE;
    		else if (strcmp(argv[iarg],"euler") == 0) propagator= EULER;
    		else if (strcmp(argv[iarg],"rkcs") == 0) propagator= RKCS;
    		else if (strcmp(argv[iarg],"pla") == 0){
    			propagator= PLA;
    			if (argv[iarg+1][0] != '-') pla_config = argv[++iarg];
    			else{
    				cout << "ERROR: To use the pla you must specify a simulation configuration. Please try again." << endl;
    				exit(1);
    			}
    		}
    		else{
    			fprintf(stderr, "ERROR: Unrecognized propagator type %s.\n", argv[iarg]);
    			exit(1);
    		}
    		iarg++;
    		break;
    	case 'r':
    		rtol = atof(argv[iarg++]);
    		break;
    	case 's':
    		save_file = 1;
    		break;
    	case 't':
    		atol = rtol = atof(argv[iarg++]);
    		break;
    	case 'u':
    		gillespie_update_interval = atoi(argv[iarg++]);
    		break;
    	case 'v':
    		verbose = 1;
    		break;
    	case 'x': /* continue ('extend') simulation */
    		continuation = 1;
    		break;
    	case 'z':
    		outtime = atoi(argv[iarg++]);
    		break;
    	case '?':
    		++error;
    		break;
    	case 'M':
    		if ((string)argv[iarg] == "INT_MAX") maxSteps = (double)INT_MAX;
    		else if ((string)argv[iarg] == "LONG_MAX") maxSteps = (double)LONG_MAX;
    		else if ((string)argv[iarg] == "INFINITY") maxSteps = INFINITY;//LONG_MAX;//-1L;
    		else maxSteps = floor(atof(argv[iarg])); //std::atol(argv[iarg++]);
    		if (maxSteps <= 0){
    			cout << "Warning: You set maxSteps = " << maxSteps << ". Simulation will not run." << endl;
    		}
    		iarg++;
    		break;
    	case 'I':
    		if ((string)argv[iarg] == "INT_MAX") stepInterval = (double)INT_MAX;
    		else if ((string)argv[iarg] == "LONG_MAX") stepInterval = (double)LONG_MAX;
    		else if ((string)argv[iarg] == "INFINITY") stepInterval = INFINITY;//LONG_MAX;//-1L;
    		else stepInterval = floor(atof(argv[iarg])); //std::atol(argv[iarg++]);
    		iarg++;
    		break;
    	case '-': // Process long options
//			cout << argv[iarg-1] << " ";
			string long_opt(argv[iarg-1]);
			long_opt = long_opt.substr(2); // remove '--'
			//
			// Print to .cdat
			if (long_opt == "cdat"){
				if (atoi(argv[iarg]) <= 0){
					print_cdat = false;
					cout << "Suppressing concentrations (.cdat) output" << endl;
				}
			}
			// Print to .fdat
			else if (long_opt == "fdat"){
				if (atoi(argv[iarg]) > 0){
					print_func = true;
					cout << "Activating functions output (to .gdat)" << endl;
				}
			}
			// Print additional PLA data (e.g., rxn classifications)
			else if (long_opt == "pla_output"){
				if (atoi(argv[iarg]) > 0){
					additional_pla_output = true;
				}
			}
			else if (long_opt == "stop_cond"){
				stop_string = (string)argv[iarg++];
				cout << "Stopping condition specified: " << stop_string;
				if (atoi(argv[iarg]) <= 0){
					print_on_stop = false;
					cout << " (print-on-stop disabled)";
				}
				cout << endl;
//				cout << stop_string << endl;
			}
			//...
			else{
//				cout << endl;
				cout << "Sorry, don't recognize your long option " << argv[iarg-1] << ". Please try again." << endl;
			}
			iarg++;
    		//
    		break;
    	}
    }

    /* Check input options for consistency */

    /* Check for correct number of input args */
    argleft = argc - iarg;
    if (argleft < 3) print_error();

	/* Get net file name */
	netfile_name = strdup(argv[iarg++]);

	/* Process sample times */
	if ((argleft = argc - iarg) == 2) {
		/* input is sample_time n_sample */
		sample_time = atof(argv[iarg++]);
		n_sample = (int)atof(argv[iarg++]); // Read as float and cast to int to allow for exponential format
	}
	else {
		/* input is t1 t2 ... tn */
		n_sample = argleft;
		vector<double> st;
		vector<bool> keep;
		st.push_back(t_start);
		keep.push_back(true);

		// Collect all sample times
		for (int j=0;j < n_sample;j++){
			st.push_back(atof(argv[iarg++]));
			keep.push_back(true);
		}
		if (t_start > st[st.size()-1]){ // BNG appends t_end to the sample_times array
			cout << "WARNING: t_start > t_end. Setting t_end = t_start, simulation will not run." << endl;
			st[st.size()-1] = t_start;
		}
		double t_end = st[st.size()-1];

		// Flag sample times <= t_start and >= t_end for removal
		for (unsigned int j=1;j < st.size()-1;j++){
			if (st[j] <= t_start || st[j] >= t_end){
//				cout << ": ERASE";
				keep.at(j) = false;
				n_sample--;
			}
//			cout << endl;
		}

		// Fill up sample_times array
		sample_times = ALLOC_VECTOR(n_sample+1); // t_start is the extra sample
		int k=0;
		for (unsigned int j=0;j < st.size();j++){
			if (keep.at(j)){
				sample_times[k] = st[j];
				k++;
			}
		}
		// Error check
		if (k != n_sample+1){
			cout << "Oops, something went wrong while processing sample_times." << endl;
			exit(1);
		}

		// Make sure there are at least 2 elements (t_start and t_end)
		if (n_sample < 1){
			fprintf(stderr,"ERROR: There must be at least one sample time (t_end).\n");
			exit(1);
		}

		// Check that final array is in ascending order with no negative elements
		for (i = 0; i <= n_sample; ++i) {
			if (sample_times[i] < 0.0) {
				fprintf(stderr,"ERROR: Negative sample times are not allowed.\n");
				exit(1);
			}
			if (i == 0)	continue;
//			if (sample_times[i] <= sample_times[i-1]) {
			if (sample_times[i] < sample_times[i-1]) { // Handle case where n_sample=2 and t_start=t_end
				fprintf(stderr,"ERROR: Sample times must be in ascending order.\n");
				exit(1);
			}
		}
	}

	// Initialize time
	t = t_start;

	// Find NET file
	if (!(netfile = fopen(netfile_name, "r"))) {
		fprintf(stderr, "ERROR: Couldn't open file %s.\n", netfile_name);
		exit(1);
	}

	/* Assign network_name based on netfile_name */
	network_name = strdup(netfile_name);
	chop_suffix(network_name,".net");
	if (!outpre){
		outpre = network_name;
	}

	/* Rate constants and concentration parameters should now be placed in the parameters block. */
	net_line_number = 0;
	rates = read_Elt_array(netfile, &net_line_number, (char*)"parameters", &n_read, 0x0);
	fprintf(stdout, "Read %d parameters from %s\n", n_read, netfile_name);
	rewind(netfile);
	net_line_number = 0;

    /* Read species */
    if (!(species = read_Elt_array(netfile, &net_line_number, (char*)"species", &n_read, rates))){
    	fprintf(stderr,"ERROR: Couldn't read rates array.\n");
    	exit(1);
    }
    fprintf(stdout, "Read %d species from %s\n", n_read, netfile_name);
	rewind(netfile);
	net_line_number = 0;

	/* Read optional groups */
	if (group_input_file_name){
		if (!(group_file = fopen(group_input_file_name, "r"))) {
			fprintf(stderr, "ERROR: Couldn't open file %s.\n", group_input_file_name);
			exit(1);
		}
		group_line_number = 0;
		spec_groups = read_Groups(0x0, group_file, species, &group_line_number, (char*)"groups", &n_read);
		fprintf(stdout, "Read %d group(s) from %s\n", n_read, group_input_file_name);
		fclose(group_file);
    }

	/** Ilya Korsunsky 6/2/10: Global Functions */
    map<string, double*> param_map = init_param_map(rates,spec_groups);
    map<string, int> observ_index_map = init_observ_index_map(spec_groups);
    map<string, int> param_index_map = init_param_index_map(rates);

    read_functions_array(netfile_name,rates,param_map,param_index_map,observ_index_map,&t);
    int n_func = network.functions.size();
    if (n_func > 0) n_func--; // Subtract off 'time' function
    cout << "Read " << n_func << " function(s) from " << netfile_name << endl;
	if (!rates){ // Error if the 'rates' array doesn't exist (means 0 parameters, 0 functions)
		fprintf(stderr,"ERROR: Reaction network must have parameters and/or functions defined to be used as rate laws.\n");
		exit(1);
	}

    // Create stop condition
	process_function_names(stop_string); // Remove parentheses from variable names
	vector<string> variable_names = find_variables(stop_string); // Extract variable names
	for (unsigned int i=0;i < variable_names.size();i++){
		// Error check
		if (param_map.find(variable_names[i]) == param_map.end()) {
			cout << "Error in parsing stop condition: \"" << stop_string << "\". Could not find variable "
				 << variable_names[i] << ". Exiting." << endl;
			exit(1);
		}
		// Define variable
		else {
			stop_condition.DefineVar(_T(variable_names[i]),param_map[variable_names[i]]);
		}
	}
	stop_condition.SetExpr(stop_string);

    /* Read reactions */
	if (!(reactions = read_Rxn_array(netfile,&net_line_number,&n_read,species,rates,network.is_func_map))){
		fprintf(stderr, "ERROR: No reactions in the network.\n");
		exit(1);
	}
	fprintf(stdout, "Read %d reaction(s) from %s\n", n_read, netfile_name);
	if (remove_zero) {
		remove_zero_rate_rxns(&reactions, rates);
		int n_rxn = 0;
		if (reactions){
			n_rxn = reactions->n_rxn;
		}
		fprintf(stdout, "%d reaction(s) have nonzero rate\n", n_rxn);
	}
	else{
		fprintf(stdout, "nonzero rate reactions were not removed\n"); 
	}
	/* sort_Rxn_array( reactions, rates); */
    fclose(netfile);

	/* Should add check that reactions, rates, and species are defined */
	/* Also should check that definitions don't exceed array bounds */
	if (n_sample < 1) {
		fprintf(stderr, "ERROR: n_sample < 1\n");
		exit(1);
	}

	/* Initialize reaction network */
	init_network(reactions, rates, species, spec_groups, network_name);

	// Round species populations if propagator is SSA or PLA
	if (propagator == SSA || propagator == PLA){
		for (int i=0;i < network.species->n_elt;i++) {
			network.species->elt[i]->val = floor(network.species->elt[i]->val + 0.5);
		}
	}

	/* Initialize SSA */
	if (propagator == SSA){
		init_gillespie_direct_network(gillespie_update_interval,seed);
	}

	/* Save network to file */
	if (save_file) {
		if (outpre) {
			sprintf(buf, "%s.net", outpre);
			save_file_name = strdup(buf);
		}
		else {
			save_file_name = strdup("save.net");
		}
		if ((out = fopen(save_file_name, "w"))) {
			print_network(out);
			fprintf(stdout, "Saved network to file %s.\n", save_file_name);
			fclose(out);
		}
	}
	fflush(stdout);

	/* timing for initialization */
	ptimes = t_elapsed();
	fprintf(stdout, "Initialization took %.2f CPU seconds\n", ptimes.total_cpu);

//	t = t_start;

	/* space for concentration vector */
	if (check_steady_state) {
		conc = ALLOC_VECTOR(n_species_network());
		conc_last = ALLOC_VECTOR(n_species_network());
		derivs = ALLOC_VECTOR(n_species_network());
		get_conc_network(conc_last);
	}
	outpre = chop_suffix(outpre, ".net");

	/* Initialize and print initial concentrations */
	conc_file = NULL; // Just to be safe
	conc_file = init_print_concentrations_network(outpre,continuation);
	if (!continuation) print_concentrations_network(conc_file, t);

	/* Initialize and print initial group concentrations and function values */
	group_file = NULL;
	if (spec_groups || (print_func && network.functions.size() > 0)){
		group_file = init_print_group_concentrations_network(outpre,continuation,print_func);
		if (print_func & !continuation) init_print_function_values_network(group_file);
		if (!continuation){
			print_group_concentrations_network(group_file,t,print_func);
			if (print_func) print_function_values_network(group_file,t);
		}
	}

	/* Initialize and print species stats (if enabled) */
	species_stats_file = NULL;
	if (enable_species_stats){
		species_stats_file = init_print_species_stats(outpre, continuation);
		if (!continuation) print_species_stats(species_stats_file, t);
	}

	/* Initialize flux printing (if selected) */
	flux_file = NULL;
	if (print_flux){
		flux_file = init_print_flux_network(outpre);
		int discrete = 0;
		if (propagator == SSA || propagator == PLA) discrete = 1;
		print_flux_network(flux_file,t,discrete);
	}

	fflush(stdout);

	// *** Simulate ***
	double t_end;
	if (sample_times) t_end = sample_times[n_sample];
	else t_end = t_start + (double)n_sample*sample_time;

	// PLA simulator
	if (propagator == PLA)
	{
		cout << "Accelerated stochastic simulation using PLA" << endl;

		// Initialize Network3
		Network3::init_Network3(&t,false);

		// Stop condition
		mu::Parser pla_stop_condition;
		map<string,double*> var = stop_condition.GetUsedVar();
		// Search observables
		for (unsigned int j=0;j < Network3::OBSERVABLE.size();j++){
			if (var.find(Network3::OBSERVABLE[j]->first->name) != var.end()){
//				cout << "\t" << Network3::OBSERVABLE[j]->first->name << " = " << Network3::OBSERVABLE[j]->second << endl;
				pla_stop_condition.DefineVar(Network3::OBSERVABLE[j]->first->name,&Network3::OBSERVABLE[j]->second);
			}
		}
		// Search parameters
		for (Elt* elt=network.rates->list;elt != NULL;elt=elt->next){
			if (var.find(elt->name) != var.end()){
//				cout << "\t" << "rates[" << elt->index << "] = " << elt->name << " (";
				bool func = false;
				// Is it a function?
				for (unsigned int j=0;j < network.var_parameters.size() && !func;j++){
					if (elt->index == network.var_parameters[j]){
						// YES
//						cout << "function[" << j <<"] = " << network.functions[j].GetExpr() << ")" << endl;
						func = true;
						bool found = false;
						// Which one?
						for (unsigned int k=0;k < Network3::FUNCTION.size() && !found;k++){
							if (network.functions[j].GetExpr() == Network3::FUNCTION[k]->first->GetExpr()){
								found = true;
								pla_stop_condition.DefineVar(elt->name,&Network3::FUNCTION[k]->second);
							}
						}
						// Error check
						if (!found){
							cout << "Error constructing PLA stop condition in run_network: "
								 << "Couldn't find function " << network.functions[j].GetExpr()
								 << ". Exiting." << endl;
							exit(1);
						}
					}
				}
				// NO, it's a constant
				if (!func){
//					cout << "constant)" << endl;
					pla_stop_condition.DefineConst(elt->name,elt->val);
				}
			}
		}
		// Set expression
		string expr = stop_condition.GetExpr();
		expr.erase(expr.size()-1); // Trim last character (muParser adds a null to the end)
		pla_stop_condition.SetExpr(expr);
//		cout << pla_stop_condition.GetExpr() << "= " << pla_stop_condition.Eval() << endl;

		// Initialize PLA
		Network3::init_PLA(pla_config,verbose);
		if (seed >= 0)	Network3::PLA_SIM->setSeed(seed);

		// PLA-specific output
		if (additional_pla_output){
			cout << "Activating classifications output (to _classif.pla)" << endl;
			if (!continuation){ // Print header
				FILE* outfile = NULL;
				outfile = fopen(((string)outpre+"_classif.pla").c_str(),"w");
				fprintf(outfile, "#");
				fprintf(outfile, "%18s", "time");
				fprintf(outfile, " %19s", "step");
				for (unsigned int v=0;v < Network3::REACTION.size();v++){
					fprintf(outfile," %10s",("R_"+Util::toString((int)v+1)).c_str());
				}
				fprintf(outfile,"\n");
				fclose(outfile);
			}
		}

		// Initial output to stdout
		if (verbose){
			printf("# \t time \t\t step\n");
			printf("\t %f \t 0\n",t_start);
			fflush(stdout);
		}

		// Run simulation
//		initTime = clock();
		if (!verbose) cout << "Running..." << endl;
		double step = 0;
		if (sample_times){ // Sample times
			double nextOutputStep = stepInterval;
			bool forceQuit = false;
			for (int n=1;n <= n_sample && step < maxSteps - network3::TOL && !forceQuit;n++) // t_start is the extra sample (already output)
			{
				error = Network3::run_PLA(t,sample_times[n],INFINITY,
										  step,min(nextOutputStep,maxSteps),stepInterval,
										  pla_stop_condition,print_on_stop,
										  outpre,
										  print_cdat,print_func,print_save_net,print_end_net,
										  additional_pla_output,
										  verbose);
				if (error == -1){ // stepLimit reached in propagation
					n--;
					nextOutputStep += stepInterval;
				}
				if (error == -2){ // Stop condition satisfied
					forceQuit = true;
//					cout << "\nStopping condition " << pla_stop_condition.GetExpr() << "met in "
//						 <<	"PLA simulation." << endl;
				}
			}
		}
		else{ // Sample interval
			error = Network3::run_PLA(t,t_end,sample_time,
									  step,maxSteps,stepInterval,
									  pla_stop_condition,print_on_stop,
									  outpre,
									  print_cdat,print_func,print_save_net,print_end_net,
									  additional_pla_output,
									  verbose);
		}
		if (step >= maxSteps){ // maxSteps limit reached
			cout << "Maximum step limit (" << maxSteps << ") reached in PLA simulation." << endl;
		}
		if (!verbose) cout << "Done." << endl;
//		cout << "PLA simulation took " << (clock()-initTime)/(double)CLOCKS_PER_SEC << " CPU seconds" << endl;

		// Even if .cdat printing is suppressed, must output the last step
		if (step > 0.5 && !print_cdat){ // Only print if the simulation ran (i.e., step > 0)
			string filename(outpre);
			filename += ".cdat";
			FILE* cdatFile = fopen(filename.c_str(),"a");
			Network3::print_species_concentrations(cdatFile,t);
		}

		// Print total steps to stdout
		fprintf(stdout, "TOTAL STEPS: %d\n", (int)step);

		// Clean up
		Network3::close_Network3(false);
	}
	// ODE & SSA simulators
	else{
		long int /*n_steps = 0, n_steps_last = 0,*/ n_rate_calls_last = 0, n_deriv_calls_last = 0;
		double n_steps = 0, n_steps_last = 0;//, n_rate_calls_last = 0, n_deriv_calls_last = 0;
		//
		double stepLimit = min(stepInterval,maxSteps);
		bool forceQuit = false;
		string forceQuit_message;
		double t_out = t_start;

		// Initial screen outputs
		switch (propagator) {
		case SSA:
			fprintf(stdout, "Stochastic simulation using direct Gillespie algorithm\n");
			if (verbose){
				fprintf(stdout, "%15s %8s %12s %7s %7s %10s %7s\n", "time", "n_steps", "n_rate_calls",
								 "% spec", "% rxn", "n_species", "n_rxns");
				fprintf(stdout, "%15.6f %8.0f %12d %7.3f %7.3f %10d %7d\n",
						t,
						gillespie_n_steps() - n_steps_last,
						n_rate_calls_network() - (int)n_rate_calls_last,
						100 * gillespie_frac_species_active(),
						100 * gillespie_frac_rxns_active(),
						n_species_network(),
						n_rxns_network()
						);
			}
		break;
		case CVODE:
			fprintf(stdout, "Propagating with cvode");
			if (SOLVER == GMRES) fprintf(stdout, " using GMRES\n");
			else if (SOLVER == GMRES_J) fprintf(stdout, " using GMRES with specified Jacobian multiply\n");
			else if (SOLVER == DENSE_J) fprintf(stdout, " using dense LU with specified Jacobian\n");
			else fprintf(stdout, " using dense LU\n");
			if (verbose){
				fprintf(stdout, "%15s %13s %13s\n", "time", "n_steps", "n_deriv_calls");
				fprintf(stdout, "%15.2f %13.0f %13d\n", t, n_steps, n_deriv_calls_network());
			}
		break;
		case EULER:
			fprintf(stdout,"Propagating with Euler method using fixed time step of %.15g\n",rtol);
			if (verbose){
				fprintf(stdout, "%15s %13s %13s\n", "time", "n_steps", "n_deriv_calls");
				fprintf(stdout, "%15.2f %13.0f %13d\n", t, n_steps, n_deriv_calls_network());
			}
		break;
		case RKCS:
			fprintf(stdout, "Propagating with rkcs\n");
			if (verbose){
				fprintf(stdout, "%15s %13s %13s\n", "time", "n_steps", "n_deriv_calls");
				fprintf(stdout, "%15.2f %13.0f %13d\n", t, n_steps, n_deriv_calls_network());
			}
		break;
		}
		if (verbose) fflush(stdout);

		// Initial check of stopping condition before starting propagation
		if (stop_condition.Eval()){
			cout << "Stopping condition " << stop_condition.GetExpr() << "already met prior "
				 <<	"to simulation. Quitting." << endl;
			forceQuit = true;
		}

		// Do propagation
		int n_old = 0;
		for (n = 1; n <= n_sample && t < t_end-network3::TOL && !forceQuit; ++n){
			if (n != n_old){
				if (sample_times) t_out = sample_times[n];
				else t_out += sample_time;
				n_old = n;
			}
			if (t_end < t_out) t_out = t_end; // Don't go past end time
			dt = t_out-t;
			switch (propagator){
			case SSA:
				if (gillespie_n_steps() >= stepLimit - network3::TOL){
					// Error check
					if (gillespie_n_steps() > stepLimit + network3::TOL){
						cout << "Uh oh, step limit exceeded in SSA (step limit = " << stepLimit << ", current step = "
							 << gillespie_n_steps() << "). This shouldn't happen. Exiting." << endl;
						exit(1);
					}
					// Continue
					stepLimit = min(stepLimit+stepInterval,maxSteps);
				}
				error = gillespie_direct_network(&t, dt, 0x0, 0x0, stepLimit-network3::TOL,stop_condition);
				if (verbose){
//					fprintf(stdout, "%15.6f %8ld %12d %7.3f %7.3f %10d %7d",
					fprintf(stdout, "%15.6f %8.0f %12d %7.3f %7.3f %10d %7d",
							t,
							gillespie_n_steps() - n_steps_last,
							n_rate_calls_network() - (int)n_rate_calls_last,
							100 * gillespie_frac_species_active(),
							100 * gillespie_frac_rxns_active(),
							n_species_network(),
							n_rxns_network()
							);
				}
				n_steps_last = gillespie_n_steps();
				if (error == -1) n -= 1; // stepLimit reached in propagation
				if (error == -2){ // Stop condition satisfied
					forceQuit = true;
					forceQuit_message = "Stopping condition " + stop_condition.GetExpr() +
							"met in Gillespie simulation.";
				}
				if (gillespie_n_steps() >= maxSteps - network3::TOL){ // maxSteps limit reached
					forceQuit = true;
					forceQuit_message = "Maximum step limit (" + Util::toString(maxSteps) +
							") reached in Gillespie simulation.";
				}
				break;
			case CVODE:
				if (n_steps >= stepLimit - network3::TOL){
					// Error check
					if (n_steps > stepLimit + network3::TOL){
						cout << "Uh oh, step limit exceeded in CVODE (step limit = " << stepLimit << ", current step = "
							 << n_steps << "). This shouldn't happen. Exiting." << endl;
						exit(1);
					}
					// Continue
					stepLimit = min(stepLimit+stepInterval,maxSteps);
				}
				error = propagate_cvode_network(&t, dt, &n_steps, &rtol, &atol, SOLVER, stepLimit-network3::TOL,stop_condition);
//				if (verbose) fprintf(stdout, "%15.2f %13ld %13d", t, n_steps, n_deriv_calls_network());
				if (verbose) fprintf(stdout, "%15.2f %13.0f %13d", t, n_steps, n_deriv_calls_network());
				if (error == -1) n -= 1; // stepLimit reached in propagation
				if (error == -2){ // Stop condition satisfied
					forceQuit = true;
					forceQuit_message = "Stopping condition " + stop_condition.GetExpr() +
							"met in CVODE simulation.";
				}
				if (n_steps >= maxSteps - network3::TOL){ // maxSteps limit reached
					forceQuit = true;
					forceQuit_message = "Maximum step limit (" + Util::toString(maxSteps) +
							") reached in CVODE simulation.";
				}
				break;
			case EULER:
				if (n_steps >= stepLimit - network3::TOL){
					// Error check
					if (n_steps > stepLimit + network3::TOL){
						cout << "Uh oh, step limit exceeded in EULER (step limit = " << stepLimit << ", current step = "
							 << n_steps << "). This shouldn't happen. Exiting." << endl;
						exit(1);
					}
					// Continue
					stepLimit = min(stepLimit+stepInterval,maxSteps);
				}
				error = propagate_euler_network(&t, dt, &n_steps, rtol, stepLimit-network3::TOL, stop_condition);
//				if (verbose) fprintf(stdout, "%15.2f %13ld %13d", t, n_steps, n_deriv_calls_network());
				if (verbose) fprintf(stdout, "%15.2f %13.0f %13d", t, n_steps, n_deriv_calls_network());
				if (error == -1) n -= 1; // stepLimit reached in propagation
				if (error == -2){ // Stop condition satisfied
					forceQuit = true;
					forceQuit_message = "Stopping condition " + stop_condition.GetExpr() +
							"met in EULER simulation.";
				}
				if (n_steps >= maxSteps - network3::TOL){ // maxSteps limit reached
					forceQuit = true;
					forceQuit_message = "Maximum step limit (" + Util::toString(maxSteps) +
							") reached in EULER simulation.";
				}
				break;
			case RKCS:
				if (n_steps >= stepLimit - network3::TOL){
					// Error check
					if (n_steps > stepLimit + network3::TOL){
						cout << "Uh oh, step limit exceeded in RKCS (step limit = " << stepLimit << ", current step = "
							 << n_steps << "). This shouldn't happen. Exiting." << endl;
						exit(1);
					}
					// Continue
					stepLimit = min(stepLimit+stepInterval,maxSteps);
				}
				error = propagate_rkcs_network(&t, dt, &n_steps, rtol, stepLimit-network3::TOL, stop_condition);
//				if (verbose) fprintf(stdout, "%15.2f %13ld %13d", t, n_steps, n_deriv_calls_network());
				if (verbose) fprintf(stdout, "%15.2f %13.0f %13d", t, n_steps, n_deriv_calls_network());
				if (error == -1) n -= 1; // stepLimit reached in propagation
				if (error == -2){ // Stop condition satisfied
					forceQuit = true;
					forceQuit_message = "Stopping condition " + stop_condition.GetExpr() +
							"met in RKCS simulation.";
				}
				if (n_steps >= maxSteps - network3::TOL){ // maxSteps limit reached
					forceQuit = true;
					forceQuit_message = "Maximum step limit (" + Util::toString(maxSteps) +
							") reached in RKCS simulation.";
				}
				break;
			}
			n_rate_calls_last = n_rate_calls_network();
			n_deriv_calls_last = n_deriv_calls_network();
			if (error > 0) { // error codes < 0 (e.g., stepLimit reached = -1) are not really errors
				fprintf(stderr, "Stopping due to error in integration.\n");
				exit(1);
			} // End propagation

			// Print current properties of the system
			if (print_cdat) print_concentrations_network(conc_file,t);
			// Don't print if stopping condition met and !print_on_stop (must print to CDAT)
			// NOTE: Sometimes forceQuit happens at an output step. In this case print.
			if (!(forceQuit && !print_on_stop && t < t_out-network3::TOL)){
				if (group_file) print_group_concentrations_network(group_file,t,print_func);
				if (group_file && print_func) print_function_values_network(group_file,t);
				if (enable_species_stats) print_species_stats(species_stats_file,t);
				if (print_flux){
					int discrete = 0;
					if (propagator == SSA || propagator == PLA) discrete = 1;
					print_flux_network(flux_file,t,discrete);
				}
				if (print_save_net){
					if (outpre) sprintf(buf, "%s_save.net", outpre);
					else sprintf(buf, "save.net");
					out = fopen(buf, "w");
					print_network(out);
					fclose(out);
					if (verbose) fprintf(stdout, " Wrote NET file to %s", buf);
				}
			}

			/* Check if steady state reached */
			if (check_steady_state) {
				double *a, delta, dx;
				get_conc_network(conc);
				delta = sqrt(VECTOR_DIST(conc, conc_last, n_species_network())) / n_species_network();
				fprintf(stdout, " RMS change in concentrations=%.1e.", delta);

				// Calculate derivatives
				derivs_network(t, conc, derivs);
				dx = NORM(derivs, n_species_network()) / n_species_network();
				fprintf(stdout, " |dx/dt|/dim(x)=%.1e.", dx);

				//if (delta<10*atol){
				if (dx < atol) {
					fprintf(stdout, "  Steady state reached.\n");
					break;
				}
				/* Swap conc and conc_last pointers */
				a = conc_last;
				conc_last = conc;
				conc = a;
			}

			if (verbose) printf("\n");

			if (n == outtime) {
				char buf[1000];
				FILE *outfile;
				sprintf(buf, "%s.m", outpre);
				outfile = fopen(buf, "w");
				init_sparse_matlab_file(outfile);
				sparse_jac_matlab(outfile);
				fclose(outfile);
				fprintf(stdout, "Jacobian written to %s after iteration %d\n", buf, outtime);
			}

			if (verbose) fflush(stdout);

			// Screen output if forceQuit = true
			if (forceQuit) cout << forceQuit_message << endl;

		} // end for
	} // end else

	// Final printouts
	if (t > t_start+network3::TOL && !print_cdat && propagator != PLA){ // If simulation ran t > t_start
		// Even if .cdat is suppressed, must print the last step (PLA handles this internally)
		print_concentrations_network(conc_file, t);
	}
	finish_print_concentrations_network(conc_file);
	if (group_file) finish_print_group_concentrations_network(group_file,print_func);
	if (group_file && print_func) finish_print_function_values_network(group_file);
	if (enable_species_stats) finish_print_species_stats(species_stats_file);

	// Screen outputs
	outpre = chop_suffix(outpre, ".net");
	if (propagator == SSA) fprintf(stdout, "TOTAL STEPS: %-16.0f\n", gillespie_n_steps());
	fprintf(stdout, "Time course of concentrations written to file %s.cdat.\n", outpre);
	if (n_groups_network()) fprintf(stdout, "Time course of groups written to file %s.gdat.\n", outpre);
	if (print_func && network.functions.size() > 0) fprintf(stdout, "Time course of functions written to file %s.gdat.\n", outpre);
	ptimes = t_elapsed();
	fprintf(stdout, "Propagation took %.2e CPU seconds\n", ptimes.cpu);

	/* Print final concentrations in species list */
	if (print_end_net){
		if (outpre) sprintf(buf, "%s_end.net", outpre);
		else sprintf(buf, "end.net");
		out = fopen(buf, "w");
		print_network(out);
		fclose(out);
		fprintf(stdout, "Final network file written to %s\n", buf);
	}

//	exit:
	// Clean up memory allocated for functions
	if (network.has_functions) delete[] network.rates->elt;
	if (propagator == SSA){
		// GSP.included added to GSP struct in code extension for functions
		// NOTE: GSP.included is created whether functions exist or not, so it must always be deleted
		delete_GSP_included();
	}
	// Delete sample_times if exists
	if (sample_times) free(sample_times);

	// Note that "/^Program times:/" must be last message sent from Network3 (see BNGAction.pm)
	ptimes = t_elapsed();
	fprintf(stdout, "Program times:  %.2f CPU s %.2f clock s \n", ptimes.total_cpu, ptimes.total_real);

	return (0);
}
Exemplo n.º 11
0
int benchmark(bool excess, bool defect, int num_nodes, double  average_k, int  max_degree, double  tau, double  tau2, 
	double  mixing_parameter, int  overlapping_nodes, int  overlap_membership, int  nmin, int  nmax, bool  fixed_range, double ca) {	

	
	
	
	
	double dmin=solve_dmin(max_degree, average_k, -tau);
	if (dmin==-1)
		return -1;
	
	int min_degree=int(dmin);
	
	
	double media1=integer_average(max_degree, min_degree, tau);
	double media2=integer_average(max_degree, min_degree+1, tau);
	
	if (fabs(media1-average_k)>fabs(media2-average_k))
		min_degree++;
	
	
	
	
	
	
		
	// range for the community sizes
	if (!fixed_range) {
	
		nmax=max_degree;
		nmin=max(int(min_degree), 3);
		cout<<"-----------------------------------------------------------"<<endl;
		cout<<"community size range automatically set equal to ["<<nmin<<" , "<<nmax<<"]"<<endl;
		

	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	deque <int> degree_seq ;		//  degree sequence of the nodes
	deque <double> cumulative;
	powerlaw(max_degree, min_degree, tau, cumulative);
	
	for (int i=0; i<num_nodes; i++) {
		
		int nn=lower_bound(cumulative.begin(), cumulative.end(), ran4())-cumulative.begin()+min_degree;
		degree_seq.push_back(nn);
	
	}
	
	
	
	sort(degree_seq.begin(), degree_seq.end());
		
	if(deque_int_sum(degree_seq) % 2!=0)
		degree_seq[max_element(degree_seq.begin(), degree_seq.end()) - degree_seq.begin()]--;
	
	
	deque<deque<int> >  member_matrix;
	deque<int> num_seq;
	deque<int> internal_degree_seq;
	
	// ********************************			internal_degree and membership			***************************************************

	
	

	if(internal_degree_and_membership(mixing_parameter, overlapping_nodes, overlap_membership, num_nodes, member_matrix, excess, defect, degree_seq, num_seq, internal_degree_seq, fixed_range, nmin, nmax, tau2)==-1)
		return -1;
	
	
	
	deque<set<int> > E;					// E is the adjacency matrix written in form of list of edges
	deque<deque<int> > member_list;		// row i cointains the memberships of node i
	deque<deque<int> > link_list;		// row i cointains degree of the node i respect to member_list[i][j]; there is one more number that is the external degree

	
	
	cout<<"building communities... "<<endl;
	if(build_subgraphs(E, member_matrix, member_list, link_list, internal_degree_seq, degree_seq, excess, defect)==-1)
		return -1;	
	




	cout<<"connecting communities... "<<endl;
	connect_all_the_parts(E, member_list, link_list);
	


	if(erase_links(E, member_list, excess, defect, mixing_parameter)==-1)
		return -1;

	
	
	
	if(ca!=unlikely) {
		cout<<"trying to approach an average clustering coefficient ... "<<ca<<endl;
		cclu(E, member_list, member_matrix, ca);
	}
	
	
	
	
	cout<<"recording network..."<<endl;	
	print_network(E, member_list, member_matrix, num_seq);

	
	
	
		
	return 0;
	
}
Exemplo n.º 12
0
void network_generator::network_evolution(const char **file){
	
	long double coolant_flow_rate;
	long double unit_pressure_drop = 100.0;
	
	stringstream ss;
	ss << file[channel_layer+2];
	ss >> coolant_flow_rate;
	
	if(!Is_Meaningful()){
		return ;
	}
	while(1){
		print_network();
		long double total_Q = 0;
		vector <int> network_col(101,0);
		vector < vector <int> > single_network(101,network_col);
        vector < vector < vector <int> > > network(channel_layer,single_network);
        vector <int> channel_info_col(101,-1);
        vector < vector <int> > single_channel_info(101,channel_info_col);
		vector < vector < vector <int> > > channel_info(channel_layer,single_channel_info);
		vector <long double> flowrate_col(101,0);
		vector < vector <long double> > single_flow_rate(101,flowrate_col);
		vector < vector < vector <long double> > > flow_rate(channel_layer,single_flow_rate);
		vector < vector < vector <int> > > direction(channel_layer,single_network);
		vector < matrix > matrix_a(channel_layer);
		vector < vector <node> > tempnode(channel_layer);
		vector < vector <edge_info> > edges(channel_layer);
		for (int i = 0; i < channel_layer; i++) {
			network_graph( &liquid_network[i], &tempnode[i], &edges[i]);
			cout << "network_graph done!" << endl;
			matrix_a[i].get_num_channel(&tempnode[i], &edges[i]);
			cout << "get_num_channel done!" << endl;
            matrix_a[i].initial_direction(&tempnode[i], &edges[i]);
            cout << "initial_direction done!" << endl;
            matrix_a[i].write_spice_input(&i, &tempnode[i],unit_pressure_drop);
            cout << "write_spice_input done!" << endl;
            string spice_sim = "hspice spice_";
			spice_sim += i + 48;
			spice_sim += ".txt";
			spice_sim += " > test_";
			spice_sim += i + 48;
			spice_sim += ".txt";
			cout << spice_sim << endl;
			system(spice_sim.c_str());
            matrix_a[i].read_spice_result(&i);
            cout << "read_spice_result done!" << endl;
            matrix_a[i].get_inlet_Q(&tempnode[i]);
		}
		for (int i = 0; i < channel_layer; i++) {
			total_Q += matrix_a[i].inlet_Q;
			//cout << i << " " << "total_Q " << total_Q << "\t" ;
		}
		for (int i = 0; i < channel_layer; i++) {
			cout << "\nchannel_layer " << i << endl;
			matrix_a[i].get_pressure_drop(chip.width, chip.height, chip.length, coolant_flow_rate, unit_pressure_drop, total_Q);
			matrix_a[i].fill_flow_rate(&tempnode[i] ,&edges[i],&flow_rate[i],&channel_info[i]);
			matrix_a[i].fill_direction(&tempnode[i] ,&edges[i],&direction[i]);
			matrix_a[i].write_output(&i,&liquid_network[i], &tempnode[i],&flow_rate[i],&direction[i],&channel_info[i]);
		}
		
		//cout << "file done !" << endl;
		vector < RTree<int, int, 2, float>* > edge_rtree(channel_layer);
		for( int i=0;i<channel_layer;i++ ){
			edge_rtree[i] = new RTree<int, int, 2, float>;
			int minp[2], maxp[2];
			for( int j=0;j<edges[i].size();j++ ){
				minp[0] = min(tempnode[i][edges[i][j].nodes.first].coordinate.first, tempnode[i][edges[i][j].nodes.second].coordinate.first);
				minp[1] = min(tempnode[i][edges[i][j].nodes.first].coordinate.second, tempnode[i][edges[i][j].nodes.second].coordinate.second);
				maxp[0] = max(tempnode[i][edges[i][j].nodes.first].coordinate.first, tempnode[i][edges[i][j].nodes.second].coordinate.first);
				maxp[1] = max(tempnode[i][edges[i][j].nodes.first].coordinate.second, tempnode[i][edges[i][j].nodes.second].coordinate.second);
				if(edges[i][j].HV == 'H'){
					minp[0] += 1;
					maxp[0] -= 1;
					/*cout << tempnode[i][edges[i][j].nodes.first].coordinate.first << " " << tempnode[i][edges[i][j].nodes.second].coordinate.first << endl;
					cout << tempnode[i][edges[i][j].nodes.first].coordinate.second << " " << tempnode[i][edges[i][j].nodes.second].coordinate.second << endl;
					cout << minp[0] << " " << maxp[0] << endl;
					cout << minp[1] << " " << maxp[1] << endl;*/
					//getchar();
				}
				else if(edges[i][j].HV == 'V'){
					minp[1] += 1;
					maxp[1] -= 1;
				}
				if(edges[i][j].HV != 'N'){
					edge_rtree[i]->Insert(minp, maxp, j);
				}
			}
		}
		//cout << "Rtree done !" << endl;
		/*int minp[2], maxp[2];
		minp[0] = 17;
		minp[1] = 0;
		maxp[0] = 17;
		maxp[1] = 4;
		vector <int> edge_list;
		for( int i=0;i<channel_layer;i++ ){
			edge_list.clear();
			edge_rtree[i]->Search(minp, maxp, &edge_list);
			for( int k=0;k<edge_list.size();k++ ){
				cout << tempnode[i][edges[i][edge_list[k]].nodes.first].coordinate.first << " " << tempnode[i][edges[i][edge_list[k]].nodes.first].coordinate.second << endl;
				cout << tempnode[i][edges[i][edge_list[k]].nodes.second].coordinate.first << " " << tempnode[i][edges[i][edge_list[k]].nodes.second].coordinate.second << endl;
			}
			cout << edge_list.size() << endl;
		}
		getchar();*/
		
		chdir("3d-ice/bin/");
		string simulator = "./3D-ICE-Emulator test_case_0";
		simulator += chip.case_num + 48;
		simulator += ".stk";
		for( int i=0;i<channel_layer;i++ ){
			simulator += " ../../network_";
			simulator += i+48;
			simulator += " ../../flowrate_";
			simulator += i+48;
			simulator += " ../../direction_";
			simulator += i+48;
		}
		//cout << simulator << endl;
		system(simulator.c_str());
		//getchar();
		
		chdir("../../");
		ifstream *fin = new ifstream[channel_layer+1];
		vector < vector < vector <double> > > T_map;
		double T_max = 0, T_min = 1<<30;
		pair<int, int> target;
		for( int i=0;i<channel_layer+1;i++ ){
			string file_location = "3d-ice/bin/testcase_0";
			vector < vector <double> > temp_T_map(101, vector <double>(101));
			file_location += chip.case_num+48;
			file_location += "/output_";
			file_location += i+48;
			file_location += ".txt";
			cout << file_location << endl;
			
			fin[i].open(file_location.c_str());
			if(fin[i].eof()){
				cout << "error tmap !" << endl;
				return ;
			}
			else{
				for( int k=0;k<temp_T_map.size();k++ ){
					for( int j=0;j<temp_T_map[k].size();j++ ){
						fin[i] >> temp_T_map[k][j];
						if(T_max < temp_T_map[k][j]){
							T_max = temp_T_map[k][j];
							target.first = j;
							target.second = k;
						}
						if(T_min > temp_T_map[k][j]){
							T_min = temp_T_map[k][j];
						}
					}
				}
			}
			fin[i].close();
			T_map.push_back(temp_T_map);
			print_heat_color_picture(&temp_T_map, i);
		}
		if(T_max - T_min >= chip.T_gredient){
			cout << "T_gredient fail !!!" << endl;
			cout << "T_gredient : " << chip.T_gredient << endl;
			cout << "your gragient : " << T_max - T_min << endl;
		}
		if(T_max > chip.T_max+273){
			cout << "T_max fail !!!" << endl;
			cout << "T_max : " << chip.T_max << endl;
			cout << "your T_max : " << T_max << endl;
		}
		cout << "your T_max : " << T_max << endl;
		pout(target);
		cout << endl;
		cout << "sim over !!!!!!!!!!!!!!!!!!" << endl;
		return;
		
		cout << endl;
		cout << T_max << endl;
		if(optimization_move_channel(target, &edge_rtree, &edges, &tempnode)){
			cout << "good" << endl;
		}
		else{
			cout << "bad" << endl;
		}
		getchar();
	}
	
	
}