示例#1
0
void gauss_mask_odd

(
                       /******************************************************/
    float   sigma,     /* in     : standard deviation of Gaussian            */
    int     nm,        /* in     : data dimension in mask direktion          */
    float   hm,        /* in     : pixel size in mask direction              */
    float   precision, /* in     : cutoff at precision * sigma               */
    int     *masksize, /* out    : size of gaussian mask                     */
    float   **mask     /* out    : the mask itself                           */
                       /*          memory is allocated IN the routine !      */
                       /******************************************************/
)

/* computes odd gaussian convolution mask */

{
                       /******************************************************/
    int    i;          /* loop variables                                     */
    float  sum;        /* for summing up                                     */
                       /******************************************************/


/* calculate size of convolution mask n                                     */
/* please note that due to the symmetry of the Gaussian convolution mask    */
/* only n+1 instead of 2*n+1 value have to be stored                        */
*masksize = (int)(precision * sigma / hm) + 1;
if ((*masksize) > nm)
   {
       printf("\n gauss_mask_odd : mask too large");
       printf("\n nm   : %d",nm);
       printf("\n hm   : %f",hm);
       printf("\n size : %d",*masksize);
       printf("\n prec : %f\n\n",precision);
       exit(0);
   }

/* allocate storage for convolution vector (n+1 values) */
ALLOC_VECTOR (1, (*masksize)+1, mask);


/* calculate entries of convolution vector */
 for (i=0; i<=(*masksize); i++)
   {
     (*mask)[i] = 1 / (sigma * sqrt(2.0 * 3.1415926)) 
	 * exp (- (i * i * hm * hm) / (2.0 * sigma * sigma));
   }


/* normalise convolution vector to sum 1 */
 sum = (*mask)[0];
 for (i=1; i<=(*masksize); i++)
     sum = sum + 2.0 * (*mask)[i];
 for (i=0; i<=(*masksize); i++)
     (*mask)[i] = (*mask)[i] / sum;

 
 return;
}
示例#2
0
文件: array.c 项目: DruSatori/AEMud
/*
 * Allocate an array of size 'n'.
 */
struct vector *
allocate_array(long long nn)
{
    int i, n = nn;
    struct vector *p;

    if (nn < 0 || nn > MAX_ARRAY_SIZE)
	error("Illegal array size.\n");
    if (n == 0) {
        p = &null_vector;
	INCREF(p->ref);
	return p;
    }
    num_arrays++;
    total_array_size += sizeof (struct vector) + sizeof (struct svalue) *
	(n-1);
    p = ALLOC_VECTOR(n);
    p->ref = 1;
    p->size = n;
    for (i=0; i<n; i++)
	p->item[i] = const0;
    return p;
}
示例#3
0
main(int argc, char *argv[]){
  int i,j,k;
  int nr, n_run;
  int n,m;
  double **A, **At, **G, **Gt, **GtG, **U, **Ut, **UtU, *w, **V, norm;
  double *v;
  
  FILE *stream;
  if (argc <4){
    fprintf(stderr,"Usage: %s m n n_run <datfile>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  m = atoi(argv[1]); 
  n = atoi(argv[2]);
  n_run = atoi(argv[3]);
  if (argc == 5){
    if ((stream = fopen(argv[4], "r")) == NULL){
      fprintf(stderr, "Can't open file %s.\n", argv[4]);
      exit(EXIT_FAILURE);
    }
  } else {
    stream = stdin;
  }
  
  A = ALLOC_MATRIX(m,n);
/*   for(i=0; i<m; ++i){ */
/*     for(j=0; j<n; ++j){ */
/*       if (fscanf(stream,"%lf", A[i]+j) != 1){ */
/* 	fprintf(stderr,"Error occured in reading the data.\n"); */
/* 	exit(EXIT_FAILURE); */
/*       } */
/*     } */
/*   } */

  for(i=0; i<m; ++i){  
      for(j=0; j<n; ++j){ 
	  A[i][j] = RANDOM(-10.0,10.0);
      }
  }

  /* print A */
  U = ALLOC_MATRIX(m,n);
  COPY_VECTOR(A[0], U[0], m*n);
  for(i=0; i<m; ++i){
    for(j=0; j<n; ++j)
      printf("%8.2f ", U[i][j]);
    printf("\n");
  }

  printf("Using Gram-Schmidt\n");
  /* gram-schmidt */
  At = RECT_TRANSPOSE(A, m,n);
  Gt = ALLOC_MATRIX(n,m);
  for(i=0; i<n_run; ++i){
      COPY_VECTOR(At[0], Gt[0], n*m);
      GRAM_SCHMIDT(Gt,n,m);
  }

  printf("Orthogonal vectors are \n");
  for (i=0; i<n; ++i){
      for(j=0, norm=0.0; j<m; ++j){
	  norm += SQR(Gt[i][j]);
	  printf("%.6f ", Gt[i][j]);
      }
      printf(" %f\n",norm);
  }

  G = RECT_TRANSPOSE(Gt,n,m);
  GtG = ALLOC_MATRIX(n,n);
  printf("S=\n");
  for(i=0; i<n; ++i){
      for(j=0; j<n; ++j){
	  GtG[i][j]=0.0;
	  for(k=0; k<m; ++k)
	      GtG[i][j]+=Gt[i][k]*G[k][j];
	  printf("%8.1e ", GtG[i][j]);
      }
      printf("\n");
  }

  /* project random vector against orthogonal basis */
  v = ALLOC_VECTOR(m);
  for(i=0; i<m; ++i)
      v[i] = RANDOM(-10,10);
  PROJECT(v, m, Gt, n);
  for(i=0; i<n; ++i)
      printf("v.G[%2d] = %9.2e\n", i, DOTP(v,Gt[i],m));
}
示例#4
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);
}
示例#5
0
void conv_2d_x_sym_odd_opt

(
                       /******************************************************/
    int     masksize,  /* in     : size of convolution mask                  */
    float   *mask,     /* in     : convolution mask                          */
    int     nx,        /* in     : data dimension in x direction             */
    int     ny,        /* in     : data dimension in y direction             */
    int     bx,        /* in     : boundary in x direction                   */
    int     by,        /* in     : boundary in y direction                   */
    float   **f,       /* in     : original data                             */
    float   **v        /* out    : processed data                            */
                       /******************************************************/
)

/* convolution in x-direction with odd symmetric convolution mask */
/* since the values are stored in y-direction in the cache, a loop unrolling */
/* scheme is applied */

{

                            /*************************************************/
int    i, j, k,p;           /* loop variables                                */
float  *sum;                /* for summing up                                */
float  **help;              /* array of rows with suitable boundary size     */
int    tmp1,tmp2,tmp3,tmp4; /* time saver                                    */
int    UNROLL;              /* number of rows that are convolved in parallel */
int    inner_loop_max;      /* number of loops for parrallel computation     */
int    inner_loop_rest;     /* number of remaining rows                      */
                            /*************************************************/


/* set number of rows convolved in parallel */
UNROLL=32; 

/* allocate storage for that many rows */
ALLOC_MATRIX (1, nx+masksize+masksize,UNROLL, &help);

/* allocate storagy for that many results */
ALLOC_VECTOR (1, UNROLL, &sum);

/* compute number of loops required if the desired number of rows is */
/* convolved in parallel */
inner_loop_max=ny/UNROLL;

/* compute number of remaining rows that have to be convolved thereafter */
inner_loop_rest=ny-(inner_loop_max*UNROLL);

/* time saver indices */ 
tmp1=masksize-1;
tmp2=tmp1+nx;
tmp3=tmp2+1;
tmp4=tmp1-(bx-1);

/*****************************************************************************/
/* (1/2) As long as the desired number of rows can be convolved in parallel  */
/*       use loop unrolling scheme that respects cache direction             */
/*****************************************************************************/

 for (j=0; j<inner_loop_max; j++)
 {
     /* copy rows in vector array */
     for (i=bx; i<nx+bx; i++)
	 for (k=0; k<UNROLL; k++)
	 {
	     help[i+tmp4][k] = f[i][j*UNROLL+k+by];
	 }
     
     /* mirror boundaries of each of these rows */
     for (p=1; p<=masksize; p++)
	 for (k=0; k<UNROLL; k++)
	 {
	     help[masksize-p][k] = help[tmp1+p][k];
	     help[tmp2    +p][k] = help[tmp3-p][k];
	 }
     
     /* convolution step for each of these rows */
     for (i=masksize; i<=tmp2; i++)
     {
	 /* convolve different rows in parallel */
	 for (k=0; k<UNROLL; k++)
	 {	    
	     sum[k] = mask[0] * help[i][k];
	 }
	 
	 for (p=1; p<=masksize; p++)
	     for (k=0; k<UNROLL; k++)
	     {
		 sum[k] += mask[p] * (help[i+p][k] + help[i-p][k]);
	     } 
	 
	 /* write back results in parallel */
	 for (k=0; k<UNROLL; k++)	
	 {	
	     v[i-tmp4][j*UNROLL+k+by] = sum[k];     
	 }
     }
 } /* for j */

/*****************************************************************************/
/* (2/2) Convolve the remaining number of rows in parallel using the same    */
/*       loop unrolling scheme                                               */
/*****************************************************************************/

 if (inner_loop_rest>0)
 {
     /* copy rows in vector array */
     for (i=bx; i<nx+bx; i++)
	 for (k=0; k<inner_loop_rest; k++)
	 {
	     help[i+tmp4][k] = f[i][j*UNROLL+k+by];
	 }
     
     /* mirror boundaries for each of these rows */
     for (p=1; p<=masksize; p++)
	 for (k=0; k<inner_loop_rest; k++)
	 {
	     help[masksize-p][k]      = help[tmp1+p][k];
	     help[tmp2+p][k] = help[tmp3-p][k];
	 }

     /* convolution step for each of these rows */
     for (i=masksize; i<=tmp2; i++)
     {
	 /* convolve different rows in parallel */
	 for (k=0; k<inner_loop_rest; k++)
	 {	    
	     sum[k] = mask[0] * help[i][k];
	 }

	 for (p=1; p<=masksize; p++)
	     for (k=0; k<inner_loop_rest; k++)
	     {
		 sum[k] += mask[p] * (help[i+p][k] + help[i-p][k]);
	     } 

	  /* write back results in parallel */
	 for (k=0; k<inner_loop_rest; k++)
	 {	    
	     v[i-tmp4][j*UNROLL+k+by] = sum[k];
	 }
     }
 }

/* disallocate storage for the rows */
FREE_MATRIX (1, nx+masksize+masksize,UNROLL, help);

/* disallocate storage for the results */
FREE_VECTOR (1, UNROLL, sum);
 
return;
}
示例#6
0
void conv_2d_y_asym_odd_opt

(
                       /******************************************************/
    int     masksize,  /* in     : size of convolution mask                  */
    float   *mask,     /* in     : convolution mask                          */
    int     nx,        /* in     : data dimension in x direction             */
    int     ny,        /* in     : data dimension in y direction             */
    int     bx,        /* in     : boundary in x direction                   */
    int     by,        /* in     : boundary in y direction                   */
    float   **f,       /* in     : original data                             */
    float   **v        /* out    : processed data                            */
                       /******************************************************/
)

/* convolution in y-direction with odd antisymmetric convolution mask */

{
                            /*************************************************/
int    i, j, k,p;           /* loop variables                                */
float  sum;                 /* for summing up                                */
float  *help;               /* array for one column with suitable boundary   */
int    tmp1,tmp2,tmp3,tmp4; /* time saver                                    */
                            /*************************************************/


/* allocate storage for a sigle row */
ALLOC_VECTOR (1, ny+masksize+masksize, &help);

/* time saver indices */
tmp1=masksize-1;
tmp2=tmp1+ny;
tmp3=tmp2+1;
tmp4=tmp1-(by-1);

 /* for each column */
 for (i=bx; i<nx+bx; i++)
 {
     /* copy current column in this column vector */
     for (j=by; j<ny+by; j++)
	 help[j+tmp4] = f[i][j];

     /* mirror boundary of the colum vector */
     for (p=1; p<=masksize; p++)
     {
	 help[masksize-p]      = help[tmp1+p];
	 help[tmp2+p] = help[tmp3-p];
     }
     
     /* convolution step for the column vector*/
     for (j=masksize; j<=tmp2; j++)
     {
	 /* calculate convolution */
	 sum = mask[0] * help[j];
	 for (p=1; p<=masksize; p++)
	     sum += mask[p] * (help[j+p] - help[j-p]);
	 /* write back result for the current colum */
	 v[i][j-tmp4] = sum;
     }
 } /* for i */  

/* disallocate storage for a single row */
FREE_VECTOR (1, ny+masksize+masksize, help);

return;

}