Exemple #1
0
void GetParams(int argc, char* argv[], struct DATA *params){

  	// Get the parameter file:
    IniReader inifile;
    // If the user specified a param file at runtime
    //  then use that, else, use the default, params.ini
    //   (User specifies via ./EXE my_param.par)
    
    if (argc > 1)
        inifile.read(argv[1]);
    else
        inifile.read("params.ini");


	// The numbers shown are the "default" values,
	// if nothing can be found in the params.ini file.
	
    params->h = inifile.getiniDouble("h",0.1);
    params->ht = inifile.getiniDouble("ht",0.01);
    params->derivsaccuracy = int(inifile.getiniDouble("derivsaccuracy",2));
    
    params->imax = int(inifile.getiniDouble("imax",10));
    params->cmax = int(inifile.getiniDouble("cmax",2));
    params->ntimsteps = int(inifile.getiniDouble("ntimsteps",100));
  
	params->pottype = int(inifile.getiniDouble("pottype",1));
	params->inittype = int(inifile.getiniDouble("inittype",1));
	params->evoltype = int(inifile.getiniDouble("evoltype",0));
	params->field_lap_type = int(inifile.getiniDouble("field_lap_type",0));
	params->eomtype = int(inifile.getiniDouble("eomtype",0));
	
	params->screenfreq = int(inifile.getiniDouble("screenfreq",10));
	params->filefreq = int(inifile.getiniDouble("filefreq",10));
	params->thistfreq = int(inifile.getiniDouble("thistfreq",1));
	params->potparam1 = inifile.getiniDouble("potparam1",0.0);
	params->initparam1 = inifile.getiniDouble("initparam1",0.0);
	
	params->OutDir = inifile.getiniString("OutDir","output/");
	params->RunID = inifile.getiniString("RunID","run_01");
	
	params->PoissSolnMethod = int(inifile.getiniDouble("PoissSolnMethod",1));
	params->PoissSolnRelaxMethod = int(inifile.getiniDouble("PoissSolnRelaxMethod",1));
	params->PoissSourceType = int(inifile.getiniDouble("PoissSourceType",1));
	params->PoissAccuracy =  inifile.getiniDouble("PoissAccuracy",1);
	params->PossSolveFreq =  inifile.getiniDouble("PossSolveFreq",1);
	
	params->H0 = inifile.getiniDouble("H0",1.0);
	params->hbar = inifile.getiniDouble("hbar",1.0);
	
	
	// Make sure output directory exists.
	// If it dosnt, this function will create it & tell you
	// it got created
    checkdirexists(params->OutDir);
	
	params->flag = 0;
	
	
} // END GetParams()
Exemple #2
0
// Our program entry point
// This entry point is just a wrapper around the evolution routines.
// It sets up the input parameters as well as the output file, and otherwise just calls the routines to run the evolution.
int main(int argc, char* argv[]) {

    int result; // Just a number for return values

    //******************//
    // Input parameters //
    //******************//

	// Read the input parameters file, named "params.ini" by default.
	// If there is a command line argument, assume that it is the filename for the parameters file.
	IniReader inifile;
	if (argc > 1)
		inifile.read(argv[1]);
	else
		inifile.read("params.ini");

	// Values in the ini file can be set using the following command:
    // inifile.setparam("desiredh", "Cosmology", 1);
	// First parameter is the key name, the second is the section name, and the third is the value, either an integer, string or double

    // Set up the cosmological parameters
    Parameters myParams(inifile);

    // Figure out if we're going to postprocess or not
    bool dopostprocess = inifile.getiniBool("postprocess", false, "Single");

	// Turn off post-processing for now
	if(dopostprocess)
		dopostprocess = false;

    //**************//
    // Output class //
    //**************//

	/*

    // Set up the filenames to output
    string outputdir = inifile.getiniString("logdir", "logs", "Function");
    string basename = inifile.getiniString("runname", "run", "Function");
    string postname = inifile.getiniString("postname", "d", "Function"); // Only used if dopostprocess is true

    // Go and find our appropriate file name (using 4 digit numbers as the default)
    string outputname = getfilename(outputdir, basename, postname, inifile.getiniInt("numberpad", 4, "Function"), dopostprocess);

    // Set up the output class
    std::string parsestring = inifile.getiniString("outputclass", "BasicDump", "Single");
    Output *myOutput;
    if (parsestring == "BasicDump")
        myOutput = new BasicDump(dopostprocess, outputname, postname);
    else
        myOutput = new BasicDump(dopostprocess, outputname, postname);    // BasicDump is the default

    // Check that output is a go
    if (!myOutput->filesready()) {
        // End gracefully if not
        cout << "Unable to open files for output." << endl;
        delete myOutput;
        return -1;
    }
*/
	Print2Memory myOutput;
    // Load SN1a data
    // Not so much here in a single run, but when doing multiple runs, it makes sense to store the SN1a data in memory
    // rather than reading the file each time it's needed.
    // Here, we load up the SN1a data (if postprocessing is true)
    vector<vector<double> > SN1adata;
    if (dopostprocess) {
        string sn1afile = inifile.getiniString("union21", "SCPUnion2.1_mu_vs_z.txt", "Function");
        if (loadSN1adata(sn1afile, SN1adata) != 0) {
            // Could not find data file
            myOutput.printlog("Warning: cannot find Union2.1 SN1a data file.");
            cout << "Warning: cannot find Union2.1 SN1a data file." << endl;
        }
    }


    //*******************//
    // Do the evolution! //
    //*******************//

    // Print some stuff to the screen
    cout << "Beginning evolution." << endl;
    //cout << "Outputting to " << outputname << endl;

    // Start timing!
    boost::timer::cpu_timer myTimer;

	// parameter name to be dialed
	string paramname = "phi0"; 
	// Section name of dialed parameter
	string paramsection = "Cosmology"; 
	// Value of the parameter which will be returned as the sequestering-compatible parameter
	double param_seq;
	// Start value of the parameter to be dialed
	double paramstart = inifile.getiniDouble("paramstart", 0.0, "Seq");
	double paramend = inifile.getiniDouble("paramend", 2.5, "Seq");
	// Required accuracy of the sequestering constraint
	int maxSteps = inifile.getiniInt("maxSteps", 2000, "Seq");
	double DeltaParam = ( paramend - paramstart) / maxSteps;
	
	int Steps = 0;
	int NRflag = 0;
	double param = paramstart;
	double histIntRicciScalar;

	vector<double> results;
	/*
	ofstream dumpconv;
	dumpconv.open( inifile.getiniString("dumpdir", "testconv", "Seq") + "/" + inifile.getiniString("dumpfilename", "test.dat", "Seq") );
	string p1_name = "mass";
	string p1_section = "Quintessence";
	string p2_name = "Omegakh2";
	string p2_section = "Cosmology";
	double p1_min = inifile.getiniDouble("p1_min", 0.0, "Seq");
	double p1_max = inifile.getiniDouble("p1_max", 0.0, "Seq");
	double delta_p1 = (p1_max - p1_min)/inifile.getiniDouble("p1_n", 10.0, "Seq");
	double p2_min = inifile.getiniDouble("p2_min", 0.0, "Seq");
	double p2_max = inifile.getiniDouble("p2_max", 0.0, "Seq");
	double delta_p2 = (p2_max - p2_min)/inifile.getiniDouble("p2_n", 10.0, "Seq");		 
	std::cout << p1_name << " = " << p1_min << " " << p1_max << " " << delta_p1 << "(logspace)" << std::endl;
	std::cout << p2_name << " = " << p2_min << " " << p2_max << " " << delta_p2 << std::endl;
	
	for(double p1 = p1_min; p1 < p1_max; p1 += delta_p1){
		inifile.setparam(p1_name,p1_section,pow(10.0,p1));
		for(double p2 = p2_min; p2 < p2_max; p2 += delta_p2){
			inifile.setparam("evtoend","Seq",true);
			inifile.setparam(p2_name,p2_section,p2);
			Parameters myParams_1(inifile);
			results = doEvolution(inifile, myParams_1, myOutput, SN1adata, dopostprocess);
			// Now do a run to get the likelihood
			inifile.setparam("evtoend","Seq",false);
			Parameters myParams_2(inifile);
			doEvolution(inifile, myParams_2, myOutput, SN1adata, true);
			dumpconv << p1 << " " << p2 << " ";
			for(int n = 0; n < results.size(); n++)
				dumpconv << results[n] << " ";
			dumpconv << myOutput.getvalue("combinationchi", 0.0);
			dumpconv << std::endl;
		}
		dumpconv << std::endl;
	}
	*/
	
	
	// HOPEFULLY, A ROUTINE TO FIND A SEQUESTERING SOLUTION VIA SOME MCMC-ISH METHOD.
	
	
	
	ofstream dumpconv;
	dumpconv.open( inifile.getiniString("dumpdir", "testconv", "Seq") + "/" + inifile.getiniString("dumpfilename", "test.dat", "Seq") );
	
	double HIRl_p, HIRl_c = 1000, Omk_p, tmax, w0, amax;
	double HIRl_t = inifile.getiniDouble("HIRt", 0.1, "Seq");

	double p1_min = inifile.getiniDouble("p1_min", 0.0, "Seq");
	double p1_max = inifile.getiniDouble("p1_max", 0.0, "Seq");
	string p1_name = "mass";
	string p1_section = "Quintessence";
	double delta_p1 = (p1_max - p1_min)/inifile.getiniDouble("p1_n", 10.0, "Seq");
	double p2_min = inifile.getiniDouble("p2_min", 0.0, "Seq");
	double p2_max = inifile.getiniDouble("p2_max", 0.0, "Seq");
	double p2_sigma = (p2_max - p2_min) / 100.0;
	double Omk = p2_min;
	double tarmfrac, a_now;
	for(double p1 = p1_min; p1 < p1_max; p1 += delta_p1){
		
		inifile.setparam(p1_name,p1_section,pow(10.0,p1));
		Omk = p2_min + (p2_max - p2_min) * 0.5 + NormalRand() * p2_sigma;
		HIRl_c = 1000;
		while(true){
			
			// Pick a new Omk that is inside the prior range
			while(true){
				Omk_p = Omk + NormalRand() * p2_sigma;
				if(Omk_p <= p2_max && Omk_p >= p1_min)
					break;
			}

			// Now that we have a sensible choice of Omk,
			// run the evolver...
			inifile.setparam("evtoend","Seq",true);
			inifile.setparam("Omegakh2","Cosmology",Omk_p);
			Parameters myParams_1(inifile);
			results = doEvolution(inifile, myParams_1, myOutput, SN1adata, false);
			// ... and pull out the value of <R>
			HIRl_p = log10(abs(results[1]));
			
			// If <R> for this choice of Omk is smaller than the previous one,
			// then keep it.
			if(HIRl_p < HIRl_c){
				Omk = Omk_p;
				HIRl_c = HIRl_p;
			}
	
			// If <R> is smaller than te desired threshold "error",
			//	then we have found a sequestering solution.	
			if(HIRl_c < HIRl_t){
				amax = results[4];
				w0 = results[5];
				tmax = results[6];
				tarmfrac = results[7];
				a_now = results[8];
				break;
			}
		}
		inifile.setparam("evtoend","Seq",false);
		Parameters myParams_2(inifile);
		doEvolution(inifile, myParams_2, myOutput, SN1adata, true);
		dumpconv << p1 << " " <<  Omk << " " << HIRl_c << " ";
		dumpconv << exp(-0.5 * myOutput.getvalue("combinationchi", 0.0) ) << " ";
		dumpconv << amax << " " << tmax << " " << w0 << " " << tarmfrac << " " << a_now << endl;
		/*
		1: mass
		2: Omk
		3: <R>
		4: L
		5: amax
		6: tmax
		7: w0
		
		*/
	}
	
	
	
	dumpconv.close();
	if(NRflag == 0)
		std::cout << "histIntRicciScalar = " << histIntRicciScalar << endl;	
	
    // Stop timing
    myTimer.stop();
	result = (int)results[0];
	
    // Interpret the result of the evolution
    if (result == 0) {
        // Success! Print a nice message
        myOutput.printfinish(myTimer.elapsed().wall / 1e6);
        cout << setprecision(4) << "Evolution complete in " << myTimer.elapsed().wall / 1e6 << " milliseconds." << endl;
    }
    else if (result == -1) {
        // Initialization error
        cout << "Error initializing model; terminating." << endl;
    }
    else if (result == 1) {
        // Integration error
        cout << "Integration error; terminating." << endl;
    }
    else if (result == 2) {
        // NAN error
        cout << "NAN error; terminating." << endl;
    }
    else if (result == 3) {
        // Did not get t a = 1 error
        cout << "Did not evolve to a = 1 within appropriate conformal time; terminating." << endl;
    }
    else if (result == 4) {
        // Invalid state error
        cout << "Invalid state reached; terminating." << endl;
    }
    else if (result == 5) {
        // Invalid state error
        cout << "Error integrating distance measures; terminating." << endl;
    }


    //**********//
    // Clean up //
    //**********//

    // No memory leaks!
//    delete *myOutput;

	// Exit gracefully
	return 0;
}
Exemple #3
0
// Our program entry point
int main(int argc, char* argv[]) {

	// Read the params.ini file
	// If there is a command line argument, assume that it is a different file than params.ini
	IniReader inifile;
	if (argc > 1)
		inifile.read(argv[1]);
	else
		inifile.read("params.ini");

	// Set up the integrator
	Integrator *myIntegrator = new Integrator;

	// Set up the equations of motion/model class
//	Model *myModel = new Quintessence();
	Model *myModel = new QuintessenceH();
//	Model *myModel = new LambdaCDM();

	// Set up the parameters - OmegaM, Tgamma, OmegaK, z_init, h (of H_0 = h * 100 km/s/Mpc) and the model
	Parameters *myParams = new Parameters(inifile.getiniDouble("Omegam", 0.3, "general"),
			                              inifile.getiniDouble("Tgamma", 2.72548, "general"), 0.01, 1.0e4, 0.7);

	// Load the model and parameters into a class to pass into the integration routine
	IntParams *myIntParams = new IntParams(*myParams, *myModel);

	// Set up the initial conditions
	// We use conformal time for our time coordinate. To make things easier, we start at tau = 0.
	double starttime = 0.0;
	// We don't want to integrate forever; specify an endtime in conformal time. I've chosen 100 here; this should
	// be chosen to correspond to about twice as long as you'd except the integration to take.
	// Note that we'll actually be checking to stop integration when the energy fraction of matter is roughly correct (a >= 1)
	double endtime = 10.0;
	// The initial conditions is specified by values for the initial a, \phi, and \dot{\phi}
	// where the overdot is a derivative with respect to conformal time.
	// Rather than setting a, it's simpler to set a redshift to begin at, and compute a from there, noting that 1 + z = a_{today}/a
	// and a_{today} = 1 by choice. This choice is specified in myParams.
	// \phi and \dot{\phi} will need to be set based on the model
	double phi0 = 0.01;
	double phidot0 = 0.0;

	// The data array stores a, \phi, and \dot{phi} through the evolution. The fourth parameter is an initial value of \dot{a}/a,
	// which may be necessary in some models.
	double data[4] = { 1.0 / (1.0 + myParams->z0()), phi0, phidot0, 0.0 };

	// Set up the filenames to output
	string outputdir = "logs";
	string basename = "run";
	// Go and find our appropriate file name (using 4 digit numbers)
	string outputname = getfilename(outputdir, basename, 4);

	// Set up the output class
	Output *myOutput = new BasicDump(outputname);
	// Check that output is a go
	if (!myOutput->filesready()) {
		cerr << "Unable to open files for output." << endl;
		return -1;
	}

	// Set up the consistency check class
	// Consistency *myChecker = new Consistency();
	Consistency *myChecker = new SimpleCheck();

	// Allow the model to initialize itself
	int initresult = myModel->init(data, starttime, *myParams);
	if (initresult != 0) {
		cerr << "Unable to initialize model." << endl;
		return -1;
	}

	// Get the output class to write out information on the run
	myOutput->printinfo(data, *myIntParams);

	// Allow the model class to write out any information on the run
	myOutput->printlog(myModel->description());

	// Start timing!
	boost::timer::cpu_timer myTimer;

	// Do the evolution!
	int result = BeginEvolution(*myIntegrator, *myIntParams, data, starttime, endtime, *myOutput, *myChecker);

	// Print a goodbye message, using time in milliseconds
	myTimer.stop();
	myOutput->printfinish(myTimer.elapsed().wall / 1e6);

	// Clean up
	delete myChecker;
	delete myOutput;
	delete myIntParams;
	delete myParams;
	delete myModel;
	delete myIntegrator;

	// Exit gracefully with result from integration
	return result;
}
Exemple #4
0
// Our program entry point
int main(int argc, char* argv[]) {

    // Start timing!
    boost::timer::cpu_timer myTimer;

    //******************//
    // Input parameters //
    //******************//

    // Read the input parameters file, named "params.ini" by default.
    // If there is a command line argument, assume that it is the filename for the parameters file.
    IniReader inifile;
    string paramsfile;
    if (argc > 1)
        paramsfile = argv[1];
    else
        paramsfile = "params.ini";
    inifile.read(paramsfile.c_str());

    //**************//
    // Declarations //
    //**************//

    // Print2Memory is used for MCMC outputting
    Print2Memory myOutput;

    // Seed random number generator
    RNGtool.seed(time(NULL));

    // Output directories and filenames
    string chaindir = inifile.getiniString("chaindir", "chains", "MCMC");
    string chainsubdir = inifile.getiniString("chainsubdir", "run1", "MCMC");
    string outputdir = chaindir + "/" + chainsubdir;
    string basename = inifile.getiniString("chainprefix", "chain", "MCMC");
    string priorsfile = inifile.getiniString("priorsfile", "priors.txt", "MCMC");

    // If the priors file doesn't exist, abort
    // Make sure that the directories exist
    if (!boost::filesystem::exists(priorsfile)) {
        cout << "Priors file (" << priorsfile << ") does not exist. Aborting." << endl;
        return -1;
    }

    // Make sure that the directories exist
    if (!boost::filesystem::exists(chaindir + "/")) {
        // Directory doesn't exist. Make it.
        boost::filesystem::create_directory(chaindir);
        std::cout << "Creating directory " << chaindir << "/" << std::endl;
    }
    if (!boost::filesystem::exists(outputdir + "/")) {
        // Directory doesn't exist. Make it.
        boost::filesystem::create_directory(outputdir);
        std::cout << "Creating directory " << outputdir << "/" << std::endl;
    }


    // Copy the params.ini and priors file being used into the subdir
    if (boost::filesystem::exists(paramsfile.c_str())) {
        boost::filesystem::copy_file(paramsfile, outputdir + "/params.ini", boost::filesystem::copy_option::overwrite_if_exists);
        boost::filesystem::copy_file(priorsfile, outputdir + "/priors.txt", boost::filesystem::copy_option::overwrite_if_exists);
    }

    // MCMC parameters
    // Number of MCMC steps to burn
    int MCMCburninsteps = inifile.getiniInt("MCMCburninsteps", 1000, "MCMC");
    // Number of MCMC steps to take
    int MCMCnumsteps = inifile.getiniInt("MCMCnumsteps", 40000, "MCMC");
    // Number of MCMC chains
    int MCMCnumchains = inifile.getiniInt("numchains", 5, "MCMC");

    // Various variables
    int MCMCstep, MCMCaccept_counter;
    double lower, upper;
    bool usingSN1a;

    // Variables for holding the current and proposed likelihoods, and their ratio
    double L_current, L_proposed, LikelihoodRatio;

    // Progressbar stuff
    bool showprogress = inifile.getiniBool("progress", true, "MCMC");
    float progress = 0.0;
    int barcount = 0;


    //****************//
    // Initialization //
    //****************//

    // Get the priors
	ifstream priorsin;
    string line;
	priorsin.open(priorsfile.c_str());
	vector<PARAMPRIORS> spriors;
	string dummy;
	if(priorsin){
		while(!priorsin.eof()){
		    // Extract the line
		    getline(priorsin, line);
		    // Check whether the line is a comment
		    if (line[0] != '#' && line.length() > 0) {
		        if (line[0] == 'L' && line[1] == ' ') {
		            // Log parameter
                    // Convert the string into a stringstream for extraction
                    stringstream stream(line);
                    PARAMPRIORS temp;
                    stream >> dummy >> temp.section >> temp.name >> temp.lower >> temp.upper >> temp.sigma;
                    temp.logparam = true;
                    // Make sure that the upper and lower bounds are positive
                    if (temp.upper < 0 or temp.lower < 0) {
                        cout << "Error: bounds for " << temp.name << " must be positive in order to investigate parameter in log space. Terminating." << endl;
                        return -1;
                    }
                    spriors.push_back(temp);
		        } else {
                    // Convert the string into a stringstream for extraction
                    stringstream stream(line);
                    PARAMPRIORS temp;
                    stream >> temp.section >> temp.name >> temp.lower >> temp.upper >> temp.sigma;
                    temp.logparam = false;
                    spriors.push_back(temp);
		        }
		    }
		}