TEST_F(AnalysisDriverFixture,SimpleProject_EditProblemWithTwoWorkflows) {
  // Written to reproduce Bug 1189
  {
    // Create PAT project with an IDF measure
    SimpleProject project = getCleanPATProject("SimpleProject_EditProblemWithTwoWorkflows");

    Problem problem = project.analysis().problem();
    OptionalInt index = problem.getWorkflowStepIndexByJobType(JobType::EnergyPlusPreProcess);
    ASSERT_TRUE(index);
    MeasureGroup dv("Idf Measure",MeasureVector(1u,NullMeasure()));
    problem.insert(*index,dv);
    BOOST_FOREACH(const BCLMeasure& measure,BCLMeasure::patApplicationMeasures()) {
      if (measure.inputFileType() == FileReferenceType::IDF) {
        RubyMeasure measure(measure);
        dv.push(measure);
        break;
      }
    }

    project.save();

    ProjectDatabase database = project.projectDatabase();
    RunManager runManager = project.runManager();
    WorkflowRecordVector workflowRecords = WorkflowRecord::getWorkflowRecords(database);
    EXPECT_EQ(2u,workflowRecords.size());
    BOOST_FOREACH(const WorkflowRecord& wr,workflowRecords) {
      EXPECT_NO_THROW(runManager.loadWorkflow(wr.runManagerWorkflowKey()));
    }
  }
Exemple #2
0
TEST_F(RunManagerTestFixture, Workflow_PersistDeleteByName)
{
  RunManager rm;
  
  Workflow wf("null->energyplus", "mywfname");
  rm.persistWorkflow(wf);

  Workflow wf2 = rm.loadWorkflowByName("mywfname");
  EXPECT_EQ(wf2.getName(), "mywfname");
  
  Job j = wf2.create();
  EXPECT_EQ(j.jobType(), JobType::Null);
  EXPECT_EQ(j.children().size(), 1u);

  rm.deleteWorkflowByName("mywfname");

  EXPECT_THROW(rm.loadWorkflowByName("mywfname"), std::runtime_error);
}
void populateJobs(RunManager &rm, bool bulkadd, const openstudio::path &t_model, int number)
{
  std::vector<openstudio::runmanager::Job> jobs;

  openstudio::runmanager::ConfigOptions co;
  co.fastFindEnergyPlus();

  for (int i = 0; i < number; ++i)
  {
    boost::timer t;
    Workflow wf("Null->Null->ModelToIdf->ExpandObjects->EnergyPlusPreProcess->EnergyPlus->EnergyPlusPostProcess");

    
    // keep filepaths short enough for Windows
    JobParams params;
    std::stringstream ss;
    ss << i;
    params.append("jobnumber", ss.str());
    params.append(JobParam("flatoutdir"));

    wf.add(co.getTools());
    wf.add(params);

    openstudio::runmanager::Job j = wf.create(openstudio::toPath("ClearJobsPerformanceTest"), t_model);
    LOG_FREE(Info, "RunManagerTiming", "Time to create job: " << t.elapsed());

    if (!bulkadd)
    {
      rm.enqueue(j, true);
    } else {
      jobs.push_back(j);
    }

    LOG_FREE(Info, "RunManagerTiming", "Total Time to crate Job: " << t.elapsed());
  }

  if (bulkadd)
  {
    rm.enqueue(jobs, true);
  }
}
void populateJobs(RunManager &rm)
{
  for (int i = 1; i < 31; ++i)
  {
    Workflow wf;
    for (int j = 0; j < i; ++j)
    {
      wf.addJob(JobType::Null);
    }
    // keep filepaths short enough for Windows
    wf.addParam(runmanager::JobParam("flatoutdir"));
    rm.enqueue(wf.create(openstudio::toPath("ClearJobsPerformanceTest")), true);
  }
}
TEST_F(RunManagerTestFixture, JobCreatePerformanceTest)
{
  int number = 50;

  double oldway = 0;

  openstudio::path model = openstudio::toPath("ExampleModel.osm");

  openstudio::model::exampleModel().save(model);

  {
    RunManager rm;
    rm.setPaused(true);
    boost::timer t;

    populateJobs(rm, false, model, number);
    ASSERT_EQ(number*7, static_cast<int>(rm.getJobs().size()));

    oldway = t.elapsed();
  }


  double newway = 0;

  {
    RunManager rm;
    rm.setPaused(true);

    boost::timer t;
    populateJobs(rm, true, model, number);
    ASSERT_EQ(number *7, static_cast<int>(rm.getJobs().size()));

    newway = t.elapsed();
  }

  // Assert that new way is at least 2x faster than old way
  ASSERT_LT(newway, oldway);

  LOG(Info, "Oldway: " << oldway << " Newway: " << newway);
}
Exemple #6
0
/**
 * The main function.
 */
int main(int argc, char **argv){
	int seed = 3041; //default seed

	//number of calls for numeric integration and number of events for phsp integration
	unsigned int mcPrecision = 500000;
	Logging log("log-compareTreeAmp.txt",boost::log::trivial::debug); //initialize logging

	//initialize kinematics of decay
	Physics::DPKinematics::DalitzKinematics::createInstance("D0","K_S0","K-","K+");//setup kinematics
	//initialize random generator
	std::shared_ptr<Generator> gen =
			std::shared_ptr<Generator>(
					new Physics::DPKinematics::RootGenerator(seed)
			);

	RunManager run;
	run.setGenerator(gen);
	//======================= DATA =============================
	unsigned int numEvents = 2000;//data size to be generated

	//empty file: run generation before fit
	std::shared_ptr<Data> inputData(
			new DataReader::RootReader::RootReader()
	);

	//======================= EFFICIENCY =============================
	std::shared_ptr<Efficiency> eff(new UnitEfficiency());

	//======================= AMPLITUDE =============================
	//true amplitude model
	std::string trueModelFile = "test/CompareTreeAmp-model.xml";

	boost::property_tree::ptree pt;
	read_xml(
			trueModelFile, pt, boost::property_tree::xml_parser::trim_whitespace
	);

	auto a = new Physics::AmplitudeSum::AmpSumIntensity(
			"amp", normStyle::one, eff, mcPrecision
	);
	a->Configure(pt);
	std::shared_ptr<Amplitude> trueAmp(a);

	//fit amplitude model
	std::string fitModelFile = trueModelFile;
	boost::property_tree::ptree pt2;
	read_xml(fitModelFile, pt2, boost::property_tree::xml_parser::trim_whitespace);
	auto fitAmpPtr = new Physics::AmplitudeSum::AmpSumIntensity(
			"fitAmp", normStyle::one, eff, mcPrecision
	);
	fitAmpPtr->Configure(pt2);
	auto fitAmpTreePtr = new Physics::AmplitudeSum::AmpSumIntensity(
			"fitAmpTree", normStyle::one, eff, mcPrecision
	);
	fitAmpTreePtr->Configure(pt2);
	//	AmplitudeSetup ini(fitModelFile);//put start parameters here
	//	AmplitudeSetup iniTree(fitModelFile);//put start parameters here
	//	AmpSumIntensity* fitAmpPtr = new AmpSumIntensity(ini, normStyle::one, eff, mcPrecision);
	//	AmpSumIntensity* fitAmpTreePtr = new AmpSumIntensity(iniTree, normStyle::one, eff, mcPrecision);
	std::shared_ptr<Amplitude> fitAmp(fitAmpPtr);
	std::shared_ptr<Amplitude> fitAmpTree(fitAmpTreePtr);

	run.setAmplitude(trueAmp);//set true model here for generation

	//empty phsp sample
	std::shared_ptr<Data> toyPhspData(
			new DataReader::RootReader::RootReader()
	);

	run.setPhspSample(toyPhspData);
	if( !toyPhspData->getNEvents() ) {
		run.generatePhsp(mcPrecision);
	}
	toyPhspData->setEfficiency(eff);

	run.setData(inputData);
	if( !inputData->getNEvents() ) {
		run.generate(numEvents);
	}

	//======================= PARAMETERS =============================
	ParameterList fitPar;
	fitAmp->FillParameterList(fitPar);
	ParameterList fitParTree;
	fitAmpTree->FillParameterList(fitParTree);
	ParameterList truePar;
	trueAmp->FillParameterList(truePar); //true values

	//	fitParTree.GetDoubleParameter("g1_a_0")->FixParameter(1);
	//	fitPar.GetDoubleParameter("g1_a_0")->FixParameter(1);

	for(unsigned int i=0; i<fitParTree.GetNDouble(); i++)
		fitParTree.GetDoubleParameter(i)->SetError(.1);
	for(unsigned int i=0; i<fitPar.GetNDouble(); i++)
		fitPar.GetDoubleParameter(i)->SetError(.1);

	fitAmpTree->UpdateParameters(fitParTree);
	fitAmp->UpdateParameters(fitPar);
	fitAmp->to_str();

	BOOST_LOG_TRIVIAL(info)<<"Entries in data file: "<<inputData->getNEvents();
	BOOST_LOG_TRIVIAL(info)<<"True model file: "<<trueModelFile ;
	BOOST_LOG_TRIVIAL(info)<<"Fit model file: "<<fitModelFile ;

	//======================= TREE FIT =============================
	std::shared_ptr<Optimizer::ControlParameter> esti(Estimator::MinLogLH::MinLogLH::createInstance(
			fitAmpTree, inputData, toyPhspData));
	Estimator::MinLogLH::MinLogLH* minLog = dynamic_cast<Estimator::MinLogLH::MinLogLH*>(&*(esti->Instance()));
	minLog->setUseFunctionTree(1);
	std::shared_ptr<FunctionTree> physicsTree = minLog->getTree();
	BOOST_LOG_TRIVIAL(debug) << physicsTree->head()->to_str(20);
	double initialLHTree = esti->controlParameter(fitParTree);
	std::shared_ptr<Optimizer::Optimizer> optiTree(
			new Optimizer::Minuit2::MinuitIF(esti, fitParTree)
	);
	run.setOptimizer(optiTree);

	//======================= Compare tree and amplitude =============================
	std::shared_ptr<AmpRelBreitWignerRes> phiRes =
			std::dynamic_pointer_cast<AmpRelBreitWignerRes>(
					fitAmpPtr->GetResonance("phi(1020)")
			);
	double phimag = fitAmpPtr->GetResonance("phi(1020)")->GetMagnitude();
	double phiphase = fitAmpPtr->GetResonance("phi(1020)")->GetPhase();
	std::complex<double> phiCoeff( phimag*cos(phiphase), phimag*sin(phiphase) );

	std::shared_ptr<AmpFlatteRes> a0Res =
			std::dynamic_pointer_cast<AmpFlatteRes>(
					fitAmpPtr->GetResonance("a_0(980)0")
			);
	double a0mag = fitAmpPtr->GetResonance("a_0(980)0")->GetMagnitude();
	double a0phase = fitAmpPtr->GetResonance("a_0(980)0")->GetPhase();
	std::complex<double> a0Coeff( a0mag*cos(a0phase), a0mag*sin(a0phase) );

	std::shared_ptr<AmpFlatteRes> aplusRes =
			std::dynamic_pointer_cast<AmpFlatteRes>(
					fitAmpPtr->GetResonance("a_0(980)+")
			);
	double aplusmag = fitAmpPtr->GetResonance("a_0(980)+")->GetMagnitude();
	double aplusphase = fitAmpPtr->GetResonance("a_0(980)+")->GetPhase();
	std::complex<double> aplusCoeff(
			aplusmag*cos(aplusphase),
			aplusmag*sin(aplusphase)
	);

	dataPoint point(inputData->getEvent(0)); //first data point in sample
	ParameterList intens = fitAmpPtr->intensity(point);

	/*the tree contains multiple node with the same names. We search for node 'Intens' first and
	 * afterwards for the resonance.*/
	std::shared_ptr<TreeNode> intensNode;
	MultiDouble* intensValue;
	intensNode = physicsTree->head()->getChildNode("AmpSq");
	if(intensNode)
		intensValue = dynamic_cast<MultiDouble*>( &*(intensNode->getValue()) );
	else {
		BOOST_LOG_TRIVIAL(error)<<"Failed to get Node 'AmpSq'!";
		throw;
	}

	BOOST_LOG_TRIVIAL(info) <<" Total integral of phi(1020) "<<phiRes->GetTotalIntegral();
	std::cout<<std::setprecision(8)<<std::endl;
	BOOST_LOG_TRIVIAL(info) <<"===========================================";
	BOOST_LOG_TRIVIAL(info) <<"Compare values: (use first event of data sample) TREE/AMPLITUDE";
	BOOST_LOG_TRIVIAL(info) <<"===========================================";
	BOOST_LOG_TRIVIAL(info) <<"Intensity: "<<intensValue->GetValue(0)
											<<"/"<<*intens.GetDoubleParameter(0)
											<<" = "<<intensValue->GetValue(0) / *intens.GetDoubleParameter(0);
	double norm_tree = physicsTree->head()->getChildSingleValue("N").real();
	double norm_amp = 1/fitAmp->GetIntegral();
	BOOST_LOG_TRIVIAL(info)<<" Norm: "<<norm_tree <<"/"<<norm_amp
			<<" = "<<norm_tree/norm_amp;
	BOOST_LOG_TRIVIAL(info) <<"================= phi(1020) ==========================";
	BOOST_LOG_TRIVIAL(info) <<"Reso_phi(1020): "<<intensNode->getChildSingleValue("Reso_phi(1020)")
											<<"/"<<phiRes->Evaluate(point);
	BOOST_LOG_TRIVIAL(info) <<"N_phi(1020): "<<intensNode->getChildSingleValue("N_phi(1020)").real()
											<<"/"<<phiRes->GetNormalization()
											<<" = "<<intensNode->getChildSingleValue("N_phi(1020)").real()/phiRes->GetNormalization();
	BOOST_LOG_TRIVIAL(info) <<"RelBW_phi(1020): "<<intensNode->getChildSingleValue("RelBW_phi(1020)")
											<<"/"<<phiRes->EvaluateAmp(point);
	BOOST_LOG_TRIVIAL(info) <<"AngD_phi(1020): "<<intensNode->getChildSingleValue("AngD_phi(1020)").real()
											<<"/"<<phiRes->EvaluateWignerD(point);
	BOOST_LOG_TRIVIAL(info) <<"================= a_0(980)0 ==========================";
	BOOST_LOG_TRIVIAL(info) <<"Reso_a_0(980)0: "<<intensNode->getChildSingleValue("Reso_a_0(980)0")
											<<"/"<<a0Res->Evaluate(point);
	BOOST_LOG_TRIVIAL(info) <<"N_a_0(980)0: "<<intensNode->getChildSingleValue("N_a_0(980)0").real()
											<<"/"<<a0Res->GetNormalization();
	BOOST_LOG_TRIVIAL(info) <<"FlatteRes_a_0(980)0: "<<intensNode->getChildSingleValue("FlatteRes_a_0(980)0")
											<<"/"<<a0Res->EvaluateAmp(point);
	BOOST_LOG_TRIVIAL(info) <<"================= a_0(980)+ ==========================";
	BOOST_LOG_TRIVIAL(info) <<"Reso_a_0(980)+: "<<intensNode->getChildSingleValue("Reso_a_0(980)+")
											<<"/"<<aplusRes->Evaluate(point);
	BOOST_LOG_TRIVIAL(info) <<"N_a_0(980)+: "<<intensNode->getChildSingleValue("N_a_0(980)+").real()
											<<"/"<<aplusRes->GetNormalization();
	BOOST_LOG_TRIVIAL(info) <<"FlatteRes_a_0(980)+: "<<intensNode->getChildSingleValue("FlatteRes_a_0(980)+")
											<<"/"<<aplusRes->EvaluateAmp(point);
	BOOST_LOG_TRIVIAL(info) <<"===========================================";
	std::shared_ptr<FitResult> resultTree = run.startFit(fitParTree);
	double finalLHTree = resultTree->getResult();
	resultTree->setTrueParameters(truePar);//set true parameters

	//======================= AMPLITUDE FIT =============================
	esti->resetInstance();
	esti = std::shared_ptr<Optimizer::ControlParameter>(
			Estimator::MinLogLH::MinLogLH::createInstance(
					fitAmp, inputData, toyPhspData)
	);
	double initialLH = esti->controlParameter(fitPar);

	std::cout<<std::setprecision(20);
	BOOST_LOG_TRIVIAL(info) <<"Initial likelihood: "<<initialLHTree<< "/"<<initialLH
			<< " Deviation = "<<initialLHTree-initialLH;

	std::shared_ptr<Optimizer::Optimizer> opti(
			new Optimizer::Minuit2::MinuitIF(esti, fitPar)
	);
	run.setOptimizer(opti);

	std::shared_ptr<FitResult> result= run.startFit(fitPar);
	double finalLH = result->getResult();
	result->setTrueParameters(truePar);//set true parameters

	//======================= OUTPUT =============================
	BOOST_LOG_TRIVIAL(info) <<"TREE fit result:";
	resultTree->print("P");
	BOOST_LOG_TRIVIAL(info) <<"AMPLITUDE fit result:";
	result->print("P");
	BOOST_LOG_TRIVIAL(info) <<"Comparison TREE/AMPLITUDE:";
	BOOST_LOG_TRIVIAL(info) <<"Timings[s]: "<<resultTree->getTime()<<"/"<<result->getTime()
											<<"="<<resultTree->getTime()/result->getTime();
	BOOST_LOG_TRIVIAL(info) <<"Initial likelihood: "<<initialLHTree<< "/"<<initialLH
			<< " Deviation = "<<initialLHTree/initialLH;
	BOOST_LOG_TRIVIAL(info) <<"Final likelihood: "<<finalLHTree<< "/"<<finalLH
			<< " Deviation = "<<finalLHTree/finalLH;


	BOOST_LOG_TRIVIAL(info) << "FINISHED!";
	return 0;
}
TEST_F(RunManagerTestFixture, ClearJobsPerformanceTest)
{
  openstudio::Application::instance().application(false);
  double oldway = 0;

  {
    RunManager rm;
    populateJobs(rm);

    ASSERT_EQ(465, static_cast<int>(rm.getJobs().size()));

    boost::timer t;

    for (openstudio::runmanager::Job job : rm.getJobs()) {
      rm.remove(job);
    }

    oldway = t.elapsed();

    ASSERT_TRUE(rm.getJobs().empty());
    ASSERT_FALSE(rm.workPending());
  }


  double newway = 0;

  {
    RunManager rm;
    populateJobs(rm);

    ASSERT_EQ(465, static_cast<int>(rm.getJobs().size()));

    boost::timer t;

    rm.clearJobs();

    newway = t.elapsed();

    ASSERT_TRUE(rm.getJobs().empty());
    ASSERT_FALSE(rm.workPending());
  }

  // Assert that new way is at least 2x faster than old way
  ASSERT_LT(newway * 2, oldway);

  LOG(Info, "Oldway: " << oldway << " Newway: " << newway);
}
TEST_F(AnalysisDriverFixture,RuntimeBehavior_StopCustomAnalysis) {
  // Tests for stopping time < 20s.

  // RETRIEVE PROBLEM
  Problem problem = retrieveProblem("UserScriptContinuous",true,false);

  // DEFINE SEED
  Model model = model::exampleModel();
  openstudio::path p = toPath("./example.osm");
  model.save(p,true);
  FileReference seedModel(p);

  // CREATE ANALYSIS
  Analysis analysis("Stop Custom Analysis",
                    problem,
                    seedModel);
  // generate 100 random points
  boost::mt19937 mt;
  typedef boost::uniform_real<> dist_type;
  typedef boost::variate_generator<boost::mt19937&, dist_type > gen_type;
  InputVariableVector variables = problem.variables();
  ContinuousVariable cvar = variables[0].cast<ContinuousVariable>();
  gen_type generator0(mt,dist_type(cvar.minimum().get(),cvar.maximum().get()));
  cvar = variables[1].cast<ContinuousVariable>();
  gen_type generator1(mt,dist_type(cvar.minimum().get(),cvar.maximum().get()));
  cvar = variables[2].cast<ContinuousVariable>();
  gen_type generator2(mt,dist_type(cvar.minimum().get(),cvar.maximum().get()));

  for (int i = 0, n = 100; i < n; ++i) {
    std::vector<QVariant> values;
    double value = generator0();
    values.push_back(value);
    value = generator1();
    values.push_back(value);
    value = generator2();
    values.push_back(value);
    OptionalDataPoint dataPoint = problem.createDataPoint(values);
    ASSERT_TRUE(dataPoint);
    ASSERT_TRUE(analysis.addDataPoint(*dataPoint));
  }

  // RUN ANALYSIS
  ProjectDatabase database = getCleanDatabase("StopCustomAnalysis");
  AnalysisDriver analysisDriver(database);
  AnalysisRunOptions runOptions = standardRunOptions(analysisDriver.database().path().parent_path());
  runOptions.setQueueSize(2);
  StopWatcher watcher(analysisDriver);
  watcher.watch(analysis.uuid());
  CurrentAnalysis currentAnalysis = analysisDriver.run(analysis,runOptions);
  EXPECT_EQ(2,currentAnalysis.numQueuedJobs());
  EXPECT_EQ(0,currentAnalysis.numQueuedDakotaJobs());
  EXPECT_EQ(100,currentAnalysis.totalNumJobsInOSIteration());
  EXPECT_EQ(0,currentAnalysis.numCompletedJobsInOSIteration());
  analysisDriver.waitForFinished();
  EXPECT_FALSE(analysisDriver.isRunning());
  EXPECT_GE(watcher.nComplete(),watcher.stopNum());
  EXPECT_LE(watcher.stoppingTime(),openstudio::Time(0,0,0,20));

  // check conditions afterward
  RunManager runManager = analysisDriver.database().runManager();
  EXPECT_FALSE(runManager.workPending());
  BOOST_FOREACH(const Job& job,runManager.getJobs()) {
    EXPECT_FALSE(job.running());
    EXPECT_FALSE(job.treeRunning());
  }
  EXPECT_TRUE(currentAnalysis.numCompletedJobsInOSIteration() > 0);
  EXPECT_TRUE(currentAnalysis.analysis().dataPointsToQueue().size() > 0u);
  EXPECT_TRUE(currentAnalysis.analysis().dataPointsToQueue().size() < 100u);
  EXPECT_EQ(0u,analysisDriver.currentAnalyses().size());
}