Exemplo n.º 1
0
int input_data(void)
{
    string input, test;
    int    i;

    if (instr == NULL) {        /* initialization only at first entry */
        input = getparam("in");             /* get filename */
        instr = stropen(input, "r");        /* open file */
        test = getparam("test");            /* get name for testfile */
        if (*test != 0)                     /* if testfile provided: */
	    tststr = stropen(test, "r");    /* ... open it */
    }

    if (tststr==NULL) {                     /* if no testfile, data from in */
        get_history(instr);
        i = read_snapshot(&massdata, &nmass, instr); /* read mass coord data */
        if (i==0) return 0;
	testdata = massdata;			/* use mass data for tests */
	ntest = nmass;
    } else {                                /* else data from test */
        if (massdata==NULL) {                   /* on first pass read masses */
            get_history(instr);            
            i=read_snapshot(&massdata, &nmass, instr);
            if (i==0) return 0;
        }
	get_history(tststr);                    /* read (next) testdata */
	i=read_snapshot(&testdata, &ntest, tststr);
        if (i==0) return(0);
    }
    return 1;
}
Exemplo n.º 2
0
void repository::extractor::update(const entry &package,
                                   const boost::filesystem::path &destination) {
  try {
    BUNSAN_LOG_DEBUG << "Starting \"" << package << "\" " << __func__;
    const boost::filesystem::path meta =
        destination / m_config.installation.meta;
    boost::optional<snapshot> snapshot_;
    if (boost::filesystem::is_regular_file(meta)) {
      try {
        snapshot_ = read_snapshot(meta);
      } catch (std::exception &) {
        BUNSAN_LOG_WARNING << "Unable to read snapshot from " << meta
                           << ", falling back to outdated";
      }
    }
    if (!snapshot_ ||
        *snapshot_ != cache_().read_installation_snapshot(package)) {
      BUNSAN_LOG_DEBUG << "\"" << package << "\" installation at "
                       << destination << " is outdated, updating...";
      install(package, destination);
    } else {
      boost::filesystem::last_write_time(meta, std::time(nullptr));
    }
  } catch (std::exception &) {
    BOOST_THROW_EXCEPTION(extractor_update_error()
                          << extractor_update_error::package(package)
                          << extractor_update_error::destination(destination)
                          << enable_nested_current());
  }
}
Exemplo n.º 3
0
snapshot repository::cache::read_installation_snapshot(const entry &package) {
  try {
    return read_snapshot(installation_snapshot_path(package));
  } catch (std::exception &) {
    BOOST_THROW_EXCEPTION(cache_read_installation_snapshot_error()
                          << cache_read_installation_snapshot_error::package(
                                 package) << enable_nested_current());
  }
}
Exemplo n.º 4
0
bool repository::cache::build_outdated(const entry &package,
                                       const snapshot &snapshot_) {
  try {
    const boost::filesystem::path snp = build_snapshot_path(package);
    const boost::filesystem::path build = build_archive_path(package);
    if (!boost::filesystem::exists(snp) || !boost::filesystem::exists(build))
      return true;
    return snapshot_ != read_snapshot(snp);
  } catch (std::exception &) {
    BOOST_THROW_EXCEPTION(cache_build_outdated_error()
                          << cache_build_outdated_error::package(package)
                          << enable_nested_current());
  }
}
Exemplo n.º 5
0
int main(int argc, char **argv){

	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**            =          HALOGEN V0.5           =                **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                            let there be dark  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	if (argc!=2){
		fprintf(stderr,"usage: %s inputfile\n",argv[0]);	
		return -1;
	}

	float Lbox, mpart, *x, *y, *z, *vx,*vy,*vz,*hx, *hy, *hz, *hvx,*hvy,*hvz,*hR, om_m;	
	char inname[256];
	long Npart, Nhalos, **ListOfParticles, *NPartPerCell;
	float *HaloMass, rho;
	double *MassLeft;
	float *dens;


	strcpy(inname,argv[1]);

#ifdef VERB
	fprintf(stderr,"#def VERB\n");
#endif 
#ifdef DEBUG
	fprintf(stderr,"#def DEBUG \n");
#endif
#ifdef ULTRADEBUG
	fprintf(stderr,"#def ULTRADEBUG \n");
#endif
#ifdef ONLYBIG
	fprintf(stderr,"#def ONLYBIG\n");
#endif
#ifdef NO_EXCLUSION
	fprintf(stderr,"#def NO_EXCLUSION\n");
#endif
#ifdef NO_MASS_CONSERVATION
	fprintf(stderr,"#def NO_MASS_CONSERVATION\n");
#endif
#ifdef MASS_OF_PARTS
	fprintf(stderr,"#def MASS_OF_PARTS\n");
#endif
#ifdef RANKED
	fprintf(stderr,"#def RANKED\n");
#endif
#ifdef NDENS
	fprintf(stderr,"#def NDENS\n");
#endif


	fprintf(stderr,"\nReading input file...\n");
	if (read_input_file(inname)<0)
		return -1;
	fprintf(stderr,"... file read correctly!\n");


	fprintf(stderr,"Reading Gadget file(s)...\n");
	if (read_snapshot(Snapshot, format, LUNIT, MUNIT, SWP, LGADGET, DGADGET,Nlin,&x, &y, &z, &vx, &vy, &vz, &Npart, &mpart, &Lbox, &om_m,&ListOfParticles,&NPartPerCell,&MassLeft,&dens)==0)
		fprintf(stderr,"Gadget file(s) correctly read!\n");
	else {
		fprintf(stderr,"error: Something went wrong reading the gadget file %s\n",inname);
		return -1;
	}
	
	#ifdef VERB
	fprintf(stderr,"\n\tCheck: Npart=%ld, mpart=%e, Lbox=%f\n",Npart,mpart,Lbox);
	fprintf(stderr,"\tx[0]= %f, y[0]= %f, z[0]= %f\n",(x)[0],(y)[0],(z)[0]);
	fprintf(stderr,"\t      ...\n");
	fprintf(stderr,"\tx[%ld]= %f, y[%ld]= %f, z[%ld]= %f\n\n",Npart-1,(x)[Npart-1],Npart-1,(y)[Npart-1],Npart-1,(z)[Npart-1]);
	#endif
	
	if (seed<0){
		seed = time(NULL);
		fprintf(stderr,"Seed used: %ld\n",seed);
	}

	//Generate the halo masses from the mass function
	fprintf(stderr,"Generating Halo Masses...\n");
	Nhalos = populate_mass_function(MassFunctionFile,Mmin,Lbox,&HaloMass,seed);
	if (Nhalos<0)
		fprintf(stderr,"error: Couldnt create HaloMass array\n");	
	fprintf(stderr,"...Halo Masses Generated\n");

	//Allocalte memory for the halo XYZR, and MassLeft vector
	hx = (float *) calloc(Nhalos,sizeof(float));
	hy = (float *) calloc(Nhalos,sizeof(float));
	hz = (float *) calloc(Nhalos,sizeof(float));
	hvx = (float *) calloc(Nhalos,sizeof(float));
	hvy = (float *) calloc(Nhalos,sizeof(float));
	hvz = (float *) calloc(Nhalos,sizeof(float));
	hR = (float *) calloc(Nhalos,sizeof(float));
	
	//MassLeft = (double *) calloc(Nlin*Nlin*Nlin,sizeof(double));
	
	//density at the boundary of a halo
	if (strcmp(rho_ref,"crit")==0)
		rho = OVD*rho_crit;
	else 
		rho = OVD*rho_crit*om_m;


	//place the halos
	fprintf(stderr,"Placing halos down...\n");
	if (place_halos(Nhalos,HaloMass, Nlin, Npart, x, y, z, vx,vy,vz,Lbox, rho,seed,mpart, alpha, Malpha, Nalpha, hx, hy, hz, hvx,hvy,hvz, hR,ListOfParticles,NPartPerCell,MassLeft,dens)==0)
		fprintf(stderr,"...halos placed correctly\n");
	else {
		fprintf(stderr,"Problem placing halos\n");
		return -1;
	}

	//writting output	
	fprintf(stderr,"Writing Halo catalogue...\n");
	write_halogen_cat(OutputFile,hx,hy,hz,hvx,hvy,hvz,HaloMass,hR,Nhalos);
	fprintf(stderr,"...halo catalogue written in %s\n",OutputFile);
	
	free(hx);free(hy);free(hz);free(hR);
	free(alpha); free(Malpha);


	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                        ... and there were dark matter haloes  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	return 0;	
}
Exemplo n.º 6
0
int main(int argc, char **argv){

	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**            =         HALOGEN V0.7.3          =                **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                            let there be dark  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	if (argc!=2){
		fprintf(stderr,"usage: %s inputfile\n",argv[0]);	
		return -1;
	}

	float Lbox, mpart, *x, *y, *z, *vx,*vy,*vz,*hx, *hy, *hz, *hvx,*hvy,*hvz,*hR, om_m;	
	char inname[256];
	long Npart, Nhalos, **ListOfParticles, *NPartPerCell;
	float *HaloMass, rho;
	double *MassLeft;
	float *dens;
	long i;

	strcpy(inname,argv[1]);

#ifdef VERB
	fprintf(stderr,"#def VERB\n");
#endif 
#ifdef DEBUG
	fprintf(stderr,"#def DEBUG \n");
#endif
#ifdef ULTRADEBUG
	fprintf(stderr,"#def ULTRADEBUG \n");
#endif
#ifdef ONLYBIG
	fprintf(stderr,"#def ONLYBIG\n");
#endif
#ifdef NO_EXCLUSION
	fprintf(stderr,"#def NO_EXCLUSION\n");
#endif
#ifdef NO_MASS_CONSERVATION
	fprintf(stderr,"#def NO_MASS_CONSERVATION\n");
#endif
#ifdef MASS_OF_PARTS
	fprintf(stderr,"#def MASS_OF_PARTS\n");
#endif
#ifdef RANKED
	fprintf(stderr,"#def RANKED\n");
#endif
#ifdef NDENS
	fprintf(stderr,"#def NDENS\n");
#endif
#ifdef NGP
	fprintf(stderr,"#def NGP\n");
#endif


	fprintf(stderr,"\nReading input file...\n");
	if (read_input_file(inname)<0)
		return -1;
	fprintf(stderr,"... file read correctly!\n");


	fprintf(stderr,"Reading Gadget file(s)...\n");
	if (read_snapshot(Snapshot, format, LUNIT, MUNIT, SWP, LGADGET, DGADGET,Nlin,nthreads,&x, &y, &z, &vx, &vy, &vz, &Npart, &mpart, &Lbox, &om_m,&ListOfParticles,&NPartPerCell,&MassLeft,&dens)==0)
		fprintf(stderr,"Gadget file(s) correctly read!\n");
	else {
		fprintf(stderr,"error: Something went wrong reading the gadget file %s\n",inname);
		return -1;
	}
	
	#ifdef VERB
	fprintf(stderr,"\n\tCheck: Npart=%ld, mpart=%e, Lbox=%f\n",Npart,mpart,Lbox);
	fprintf(stderr,"\tx[0]= %f, y[0]= %f, z[0]= %f\n",(x)[0],(y)[0],(z)[0]);
	fprintf(stderr,"\t      ...\n");
	fprintf(stderr,"\tx[%ld]= %f, y[%ld]= %f, z[%ld]= %f\n\n",Npart-1,(x)[Npart-1],Npart-1,(y)[Npart-1],Npart-1,(z)[Npart-1]);
	#endif
	
	//TEMPORARY OUTPUT FOR TESTING AGAINST 0.6.7
	for(i=0;i<10;i++){
		fprintf(stderr,"ListOfParticles[%ld][0] = %ld, NPartPerCell[%ld] = %ld\n",i,ListOfParticles[i][0],i,NPartPerCell[i]);
	}

	if (seed<0){
		seed = time(NULL);
		fprintf(stderr,"Seed used: %ld\n",seed);
	}

	//Generate the halo masses from the mass function
	fprintf(stderr,"Generating Halo Masses...\n");
	Nhalos = populate_mass_function(MassFunctionFile,Mmin,Lbox,&HaloMass,seed,nthreads);
	if (Nhalos<0)
		fprintf(stderr,"error: Couldnt create HaloMass array\n");	
	fprintf(stderr,"...Halo Masses Generated\n");

	// TEMPORARY OUTPUT FOR TESTING AGAINST 0.6.7
	for(i=0;i<10;i++){
		fprintf(stderr,"HaloMass[%ld] = %f\n",i,HaloMass[i]);
	}
	for(i=Nhalos-10;i<Nhalos;i++){
		fprintf(stderr,"HaloMass[%ld] = %f\n",i,HaloMass[i]);
	}

	//Allocalte memory for the halo XYZR, and MassLeft vector
	hx = (float *) calloc(Nhalos,sizeof(float));
	hy = (float *) calloc(Nhalos,sizeof(float));
	hz = (float *) calloc(Nhalos,sizeof(float));
	hvx = (float *) calloc(Nhalos,sizeof(float));
	hvy = (float *) calloc(Nhalos,sizeof(float));
	hvz = (float *) calloc(Nhalos,sizeof(float));
	hR = (float *) calloc(Nhalos,sizeof(float));
	
	//MassLeft = (double *) calloc(Nlin*Nlin*Nlin,sizeof(double));
	
	//density at the boundary of a halo
	if (strcmp(rho_ref,"crit")==0)
		rho = OVD*rho_crit;
	else 
		rho = OVD*rho_crit*om_m;

	//place the halos
	fprintf(stderr,"Placing halos down...\n");
	
	// Check the M-alpha vector against produced halos.
	fprintf(stderr,"MALPHA, HALOMASS: %e, %e", Malpha[Nalpha-1],HaloMass[Nhalos-1]);
	if( Malpha[Nalpha-1] > HaloMass[Nhalos-1]){
		if (Malpha[Nalpha-1] < 1.2*HaloMass[Nhalos-1]) {
			Malpha[Nalpha-1] = HaloMass[Nhalos-1]/1.01;
		}
		else{
			fprintf(stderr,"ERROR: LOWEST M(alpha) LARGER THAN LOWEST MASS: %e < %e",Malpha[Nalpha-1],HaloMass[Nhalos-1]);
			exit(0);
		}
	}
	fprintf(stderr,"MALPHA, HALOMASS: %e, %e", Malpha[Nalpha-1],HaloMass[Nhalos-1]);

	fprintf(stderr,"\n");
	if (place_halos(Nhalos,HaloMass, Nlin, Npart, x, y, z, vx,vy,vz,Lbox, rho,seed,mpart, nthreads,alpha, betavec, Malpha, Nalpha,recalc_frac,hx, hy, hz, hvx,hvy,hvz, hR,ListOfParticles,NPartPerCell,MassLeft,dens)==0){
		fprintf(stderr,"...halos placed correctly\n");
	}


	/*
	int place_halos(long Nend, float *HaloMass, long Nlin, long NTotPart, float *PartX,
                float *PartY, float *PartZ, float *PartVX, float *PartVY, float *PartVZ,
                float L, float rho_ref, long seed, float mp, int nthreads, double *alpha, double *beta, double *Malpha,
                long Nalpha,int Nrecalc, float *HaloX, float *HaloY, float *HaloZ, float *HaloVX,
                float *HaloVY, float *HaloVZ,float *HaloR,long **ListOfPart,
                long *NPartPerCell,double *RemMass, float *dens){
*/


	else {
		fprintf(stderr,"Problem placing halos\n");
		return -1;
	}
	fprintf(stderr,"\n");

	for(i=0;i<10;i++){
		fprintf(stderr,"(hx, hy, hz)[%ld] = (%f, %f, %f)",i,hx[i],hy[i],hz[i]);
	}
	for(i=Nhalos-10;i<Nhalos;i++){
		fprintf(stderr,"(hx, hy, hz)[%ld] = (%f, %f, %f)",i,hx[i],hy[i],hz[i]);
	}
	//writting output	
	fprintf(stderr,"Writing Halo catalogue...\n");
	write_halogen_cat(OutputFile,hx,hy,hz,hvx,hvy,hvz,HaloMass,hR,Nhalos);
	fprintf(stderr,"...halo catalogue written in %s\n",OutputFile);
	
	free(hx);free(hy);free(hz);free(hR);
	free(alpha); free(Malpha);


	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                        ... and there were dark matter haloes  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	return 0;	
}
Exemplo n.º 7
0
int main(int argc, char **argv){

	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**            =           HALOGEN V0.3          =                **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                            let there be dark  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	if (argc!=2){
		fprintf(stderr,"usage: %s inputfile\n",argv[0]);	
		return -1;
	}

	float Lbox, mpart, *x, *y, *z, *hx, *hy, *hz, *hR, om_m;	
	char inname[256];
	long Npart, Nhalos;
	float *HaloMass, rho;

	strcpy(inname,argv[1]);

#ifdef VERB
	fprintf(stderr,"#def VERB\n");
#endif 
#ifdef DEBUG
	fprintf(stderr,"#def DEBUG \n");
#endif
#ifdef ULTRADEBUG
	fprintf(stderr,"#def ULTRADEBUG \n");
#endif
#ifdef ONLYBIG
	fprintf(stderr,"#def ONLYBIG\n");
#endif


	fprintf(stderr,"\nReading input file...\n");
	if (read_input_file(inname)<0)
		return -1;
	fprintf(stderr,"... file read correctly!\n");


	fprintf(stderr,"Reading Gadget file(s)...\n");
	if (read_snapshot(Snapshot, format, LUNIT, MUNIT, SWP, LGADGET, DGADGET,&x, &y, &z, &Npart, &mpart, &Lbox, &om_m)==0)
		fprintf(stderr,"Gadget file(s) correctly read!\n");
	else {
		fprintf(stderr,"error: Something went wrong reading the gadget file %s\n",inname);
		return -1;
	}
	
	#ifdef VERB
	fprintf(stderr,"\n\tCheck: Npart=%ld, mpart=%e, Lbox=%f\n",Npart,mpart,Lbox);
	fprintf(stderr,"\tx[0]= %f, y[0]= %f, z[0]= %f\n",x[0],y[0],z[0]);
	fprintf(stderr,"\t      ...\n");
	fprintf(stderr,"\tx[%ld]= %f, y[%ld]= %f, z[%ld]= %f\n\n",Npart-1,x[Npart-1],Npart-1,y[Npart-1],Npart-1,z[Npart-1]);
	#endif

	//Generate the halo masses from the mass function
	fprintf(stderr,"Generating Halo Masses...\n");
	Nhalos = populate_mass_function(MassFunctionFile,Mmin*mpart,Lbox,&HaloMass);
	if (Nhalos<0)
		fprintf(stderr,"error: Couldnt create HaloMass array\n");	
	fprintf(stderr,"...Halo Masses Generated\n");

	//Allocalte memory for the halo XYZ and R
	hx = (float *) calloc(Nhalos,sizeof(float));
	hy = (float *) calloc(Nhalos,sizeof(float));
	hz = (float *) calloc(Nhalos,sizeof(float));
	hR = (float *) calloc(Nhalos,sizeof(float));

	
	if (strcmp(rho_ref,"crit")==0)
		rho = OVD*rho_crit;
	else 
		rho = OVD*rho_crit*om_m;

	//place the halos
	fprintf(stderr,"Placing halos down...\n");
	if (place_halos(Nhalos, HaloMass, Nlin, Npart, x, y, z, Lbox, rho,-1,mpart, alpha, Malpha, Nalpha, hx, hy, hz,hR)==0)
		fprintf(stderr,"\n...Halos placed!\n");
	else
		fprintf(stderr,"error in placing the halos\n");
	
	//writting output	
	fprintf(stderr,"Writing Halo catalogue...\n");
	write_halogen_cat(OutputFile,hx,hy,hz,HaloMass,hR,Nhalos);
	fprintf(stderr,"...halo catalogue written in %s\n",OutputFile);
	
	free(hx);free(hy);free(hz);free(hR);
	free(alpha); free(Malpha);


	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                        ... and there were dark matter haloes  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	return 0;	
}
Exemplo n.º 8
0
Arquivo: fit.c Projeto: savila/HALOGEN
int main(int argc, char **argv){

	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**            =         HALOGEN FIT V0.5        =                **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                                                               **\n");
	fprintf(stderr,"**                           let there be knowledge of the dark  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	if (argc!=2){
		fprintf(stderr,"usage: %s inputfile\n",argv[0]);	
		return -1;
	}

	char inname[256];
	float *hx, *hy, *hz, *hvx, *hvy, *hvz, *hR;
	strcpy(inname,argv[1]);
	double dr;
	double **halogen_2pcf, **halogen_err;
	long nend;
	unsigned long long *dd;
	long i,j,ii;

#ifdef VERB
	fprintf(stderr,"#def VERB\n");
#endif 
#ifdef DEBUG
	fprintf(stderr,"#def DEBUG \n");
#endif
#ifdef ULTRADEBUG
	fprintf(stderr,"#def ULTRADEBUG \n");
#endif
#ifdef ONLYBIG
	fprintf(stderr,"#def ONLYBIG\n");
#endif
#ifdef NO_EXCLUSION
	fprintf(stderr,"#def NO_EXCLUSION\n");
#endif
#ifdef NO_MASS_CONSERVATION
	fprintf(stderr,"#def NO_MASS_CONSERVATION\n");
#endif
#ifdef MASS_OF_PARTS
	fprintf(stderr,"#def MASS_OF_PARTS\n");
#endif
#ifdef RANKED
	fprintf(stderr,"#def RANKED\n");
#endif
#ifdef NDENS
	fprintf(stderr,"#def NDENS\n");
#endif
#ifdef NO_PROB_PARALEL
	fprintf(stderr,"#def NO_PROB_PARALEL\n");
#endif
#ifdef NO_PAR_FIT
	fprintf(stderr,"#def NO_PAR_FIT\n");
#endif
#ifdef MASS_CUTS_FIT
	fprintf(stderr,"#def MASS_CUTS_FIT\n");
#endif
#ifdef BETA0
	fprintf(stderr,"#def BETA0\n");
#endif

	fprintf(stderr,"\nReading input file...\n");
	if (read_input_file(inname)<0)
		return -1;
	fprintf(stderr,"... file read correctly!\n");

	//SETUP OUTPUT FILES
	strcpy(OutputNbody2PCF,OutputDir);
	strcpy(OutputHalogen2PCF,OutputDir);
	strcpy(OutputHalogenErr,OutputDir);
	strcpy(OutputAlphaM,OutputDir);
	strcpy(OutputExampleCat,OutputDir);

	strcat(OutputNbody2PCF,"/nbody.2pcf");
	strcat(OutputHalogen2PCF,"/halogen.2pcf");
	strcat(OutputHalogenErr,"/halogen.err");
	strcat(OutputAlphaM,"/M-alpha.txt");
	strcat(OutputExampleCat,"/example.halos");

	fprintf(stderr,"Reading Gadget file(s)...\n");
	if (read_snapshot(Snapshot, format, LUNIT, MUNIT, SWP, LGADGET, DGADGET,Nlin,&x, &y, &z, &vx, &vy, &vz, &Npart, &mpart, &Lbox, &om_m,&ListOfParticles,&NPartPerCell)==0)
		fprintf(stderr,"...Gadget file(s) correctly read!\n");
	else {
		fprintf(stderr,"error: Something went wrong reading the gadget file %s\n",inname);
		return -1;
	}
	


	#ifdef VERB
	fprintf(stderr,"\n\tCheck: Npart=%ld, mpart=%e, Lbox=%f\n",Npart,mpart,Lbox);
	fprintf(stderr,"\tx[0]= %f, y[0]= %f, z[0]= %f\n",(x)[0],(y)[0],(z)[0]);
	fprintf(stderr,"\t\tvx[0]= %f, vy[0]= %f, vz[0]= %f\n",(vx)[0],(vy)[0],(vz)[0]);
	fprintf(stderr,"\t      ...\n");
	fprintf(stderr,"\tx[%ld]= %f, y[%ld]= %f, z[%ld]= %f\n",Npart-1,(x)[Npart-1],Npart-1,(y)[Npart-1],Npart-1,(z)[Npart-1]);
	fprintf(stderr,"\t\tvx[%ld]= %f, vy[%ld]= %f, vz[%ld]= %f\n\n",Npart-1,(vx)[Npart-1],Npart-1,(vy)[Npart-1],Npart-1,(vz)[Npart-1]);
	#endif
	
	seed = time(NULL);

	//Generate the halo masses from the mass function
	fprintf(stderr,"Generating Halo Masses...\n");
	Nhalos = populate_mass_function(MassFunctionFile,Mmin,Lbox,&HaloMass,seed,160);
	if (Nhalos<0){
		fprintf(stderr,"error: Couldnt create HaloMass array\n");	
		return -1;
	}
	fprintf(stderr,"...Halo Masses Generated\n");

	// GET THE ACTUAL NUMBER OF R VALUES IN CORR (LINEARLY SPACED)
	dr = (maxr - minr) / nr;
	total_nr = (int) ceil(maxr / dr) + 2;
	maxr += 2* dr;

	//density at the boundary of a halo
	if (strcmp(rho_ref,"crit")==0)
		rho = OVD*rho_crit;
	else 
		rho = OVD*rho_crit*om_m;

	Nmin = Lbox*Lbox*Lbox*Dmin;
	Nmax = Lbox*Lbox*Lbox*Dmax;
#ifdef VERB
	fprintf(stderr,"Generating Mass Bins...\n");
#endif
	Nalpha = get_mass_bins(HaloMass,Nhalos,Nmax,Nmin);
#ifdef VERB
	fprintf(stderr,"... generated mass bins\n");
	fprintf(stderr,"Nmin=%d, Nmax=%d, Nmassbins=%d (=Nalphas).\n",Nmin,Nmax,Nalpha);
	fprintf(stderr,"Getting NBODY 2PCF...\n");
#endif
	


	float *nbx, *nby, *nbz, *nbvx, *nbvy, *nbvz, *nbm,*fvel;
	long nb_n;

	// Import the Nbody halos
	nb_n = read_nbody(&nbx,&nby,&nbz,&nbvx,&nbvy,&nbvz,&nbm);
	fprintf(stderr,"READ IT IN\n");
	
	

	// Get the Nbody 2PCF
	get_nbody_2pcf(nbx,nby,nbz,nbm,nb_n);

	free(nbx);
	free(nby);
	free(nbz);
	free(nbm);

	betavec = (double *)calloc(Nalpha,sizeof(double));
	alphavec = (double *)calloc(Nalpha,sizeof(double));
	for (i=0;i<Nalpha;i++){
		betavec[i]=1.0;
	}
	// DO THE FIT
	if(find_best_alpha()==0)
		fprintf(stderr, "... done fitting.\n");
	else {
		fprintf(stderr,"Problem fitting\n");
		return -1;
	}

	hx = (float *) calloc(Nhalos,sizeof(float));
	hy = (float *) calloc(Nhalos,sizeof(float));
	hz = (float *) calloc(Nhalos,sizeof(float));
	hvx = (float *) calloc(Nhalos,sizeof(float));
	hvy = (float *) calloc(Nhalos,sizeof(float));
	hvz = (float *) calloc(Nhalos,sizeof(float));
	hR = (float *) calloc(Nhalos,sizeof(float));


	seed++;

	// Do a final placement with the correct alphavec
	place_halos(Nhalos,HaloMass, Nlin, Npart, x, y, z, vx,vy,vz,Lbox,
				rho,seed,
				mpart,nthreads, alphavec, betavec, mcuts, Nalpha, recalc_frac,hx, hy, hz,
				hvx,hvy,hvz, hR,ListOfParticles,NPartPerCell);
		
	fprintf(stderr,"\tComputing velocity bias fvel...\n");
	fvel = compute_fvel(hvx,hvy,hvz,nbvx,nbvy,nbvz,nbm);	
	free(nbvx);
	free(nbvy);
	free(nbvz);
	#ifdef VERB
	for (i=0;i<Nalpha;i++){
		fprintf(stderr,"alpha[%ld]=%f beta[%ld]=%f fvel=%f\n",i,alphavec[i],i,betavec[i], fvel[i]);
	}
	#endif
	fprintf(stderr,"\t...done!\n");

	// ALLOCATE the halogen_2pcf array
	halogen_2pcf = (double **) calloc(Nalpha,sizeof(double *));
	halogen_err = (double **) calloc(Nalpha,sizeof(double *));

	nend = 0;
	dd = (unsigned long long *) calloc(total_nr,sizeof(unsigned long long));
	for(ii=0;ii<Nalpha;ii++){
		(halogen_2pcf)[ii] = (double *) calloc(total_nr,sizeof(double));
		(halogen_err)[ii] = (double *) calloc(total_nr,sizeof(double));
		while(HaloMass[nend]>mcuts[ii] && nend<Nhalos){
			nend++;
		}
		if(nend==Nhalos){
			fprintf(stderr,"ERROR: HALOMASSES DON'T REACH MALPHA_MIN\n");
			return -1;
		}
		// Do the correlation
		correlate(nend, Lbox,hx,hy,hz,halogen_2pcf[ii],halogen_err[ii], dd,total_nr,maxr,160);
	}

	// OUTPUT
	FILE* tpcfile;
	FILE* errfile;
	tpcfile = fopen(OutputHalogen2PCF,"w");
	errfile = fopen(OutputHalogenErr,"w");
	if(tpcfile==NULL){
		fprintf(stderr,"UNABLE TO OPEN NBODY 2PCF FILE\n");
		exit(1);
	}
	if(errfile==NULL){
		fprintf(stderr,"UNABLE TO OPEN NBODY 2PCF FILE\n");
		exit(1);
	}

	fprintf(stderr,"OPENED TPC AND ERR\n");
	for(i=0;i<total_nr;i++){
		fprintf(tpcfile,"%e\t",(i+0.5)*dr);
		fprintf(errfile,"%e\t",(i+0.5)*dr);
		for(j=0;j<Nalpha;j++){
			fprintf(tpcfile,"%e\t",halogen_2pcf[j][i]);
			fprintf(errfile,"%e\t",halogen_err[j][i]);
		}
		fprintf(stderr,"\n");
		fprintf(tpcfile,"\n");
		fprintf(errfile,"\n");
	}
	fclose(tpcfile);
	fclose(errfile);

	fprintf(stderr,"outputted tpc and errfile\n");
	FILE* alphafile;
	alphafile = fopen(OutputAlphaM,"w");
	for(i=0;i<Nalpha;i++){
		fprintf(alphafile,"%e\t%e\t%e\n",mcuts[i],alphavec[i],fvel[i]);
	}
	fclose(alphafile);
	for(ii=0;ii<Nalpha;ii++){
		free((halogen_2pcf)[ii]);
		free((halogen_err)[ii]);
	}
	// TBD: change this for a loop correcting velocities or change it on the fly while writing
	place_halos(Nhalos,HaloMass, Nlin, Npart, x, y, z, vx,vy,vz,Lbox,
				rho,seed,
				mpart,nthreads, alphavec, betavec, mcuts, Nalpha, recalc_frac,hx, hy, hz,
				hvx,hvy,hvz, hR,ListOfParticles,NPartPerCell);	
	fprintf(stderr,"Writing an example halo catalog to %s\n",OutputExampleCat);
	write_halogen_cat(OutputExampleCat, hx, hy, hz, hvx, hvy,hvz, HaloMass, hR,Nhalos,fvel,mcuts);
	fprintf(stderr,"...done!\n");
	free(halogen_2pcf);
	free(halogen_err);
	free(dd);	
	free(hvx);
	free(hvy);
	free(hvz);
	free(hx);	
	free(hy);	
	free(hz);	
	free(hR);	


	fprintf(stderr,"\n*******************************************************************\n");
	fprintf(stderr,"**                                     ... the fit is complete.  **\n");
	fprintf(stderr,"*******************************************************************\n\n");

	return 0;
}