Example #1
0
    /// Synchronizes data stream by finding the tag sequence. Called
    /// internally on the first buffer write.
    bool Sync( ) {

       int i = 0;
       int j = 0;
       bool found = false;

       // we have to assume the user has written samples before syncing
       writeIndex_ = writeSize_;

       // search for data tag
       for ( i = 0; i < BUFFER_SIZE; ++i ) {

          if ( buffer_[i] == tags_[j] ) {

             found = true;
             readIndex_ = i;

             for ( j = 1; j < tags_.size(); ++j ) {
                if ( buffer_[i+j] != tags_[j] )
                   found = false;
             }
             break;
          }
       }

       bufferLevel_ = writeSize_ - readIndex_;
       isSynchronized_ = true;
       return found;
    }
inline void fill_tagvector(const T& in, TagVector& tags, bool& did, bool& special) {
	for (auto tag : in) {
		if (tag->type & T_NUMERICAL) {
			did = true;
		}
		else {
			if (tag->type & T_SPECIAL) {
				special = true;
			}
			tags.push_back(tag);
		}
	}
}
Example #3
0
TEST_F(ProjectFixture, DataPointTagRecords) {
  // create Analysis with one DataPoint with one Tag ("test tag")
  analysis::Problem problem("problem",analysis::VariableVector(),runmanager::Workflow());
  FileReference seed(toPath("in.osm"));
  analysis::Analysis analysis("analysis",problem,seed);
  analysis::DataPoint dataPoint = problem.createDataPoint(std::vector<QVariant>()).get();
  dataPoint.addTag("test tag");
  analysis.addDataPoint(dataPoint);

  // make sure tag records follow data points around.
  // trying to replicate issue where data point tag records get repointed to
  // file reference record with id 0.
  {
    // save analysis to a new database
    ProjectDatabase database = getCleanDatabase("DataPointTagRecords");
    AnalysisRecord analysisRecord(analysis,database);
    database.save();
    ASSERT_EQ(1u,analysisRecord.dataPointRecords().size());
    DataPointRecord dataPointRecord = analysisRecord.dataPointRecords()[0];
    ASSERT_EQ(1u,dataPointRecord.tagRecords().size());
    TagRecord tagRecord = dataPointRecord.tagRecords()[0];
    EXPECT_EQ("test tag",tagRecord.name());
    analysis::DataPoint dataPoint = dataPointRecord.dataPoint();
    ASSERT_EQ(1u,dataPoint.tags().size());
    EXPECT_EQ("test tag",dataPoint.tags()[0].name());
    analysis::Analysis analysis = analysisRecord.analysis();
    ASSERT_EQ(1u,analysis.dataPoints().size());
    dataPoint = analysis.dataPoints()[0];
    ASSERT_EQ(1u,dataPoint.tags().size());
    EXPECT_EQ("test tag",dataPoint.tags()[0].name());
  }

  NameFinder<TagRecord> testRecordFinder("test tag");
  NameFinder<TagRecord> anotherTestRecordFinder("another test");
  NameFinder<Tag> testTagFinder("test tag");
  NameFinder<Tag> anotherTestTagFinder("another test");

  {
    ProjectDatabase database = getExistingDatabase("DataPointTagRecords");
    ASSERT_EQ(1u,AnalysisRecord::getAnalysisRecords(database).size());
    AnalysisRecord analysisRecord = AnalysisRecord::getAnalysisRecords(database)[0];
    ASSERT_EQ(1u,analysisRecord.dataPointRecords().size());
    DataPointRecord dataPointRecord = analysisRecord.dataPointRecords()[0];
    ASSERT_EQ(1u,dataPointRecord.tagRecords().size());
    TagRecord tagRecord = dataPointRecord.tagRecords()[0];
    EXPECT_EQ("test tag",tagRecord.name());
    analysis::DataPoint dataPoint = dataPointRecord.dataPoint();
    ASSERT_EQ(1u,dataPoint.tags().size());
    EXPECT_EQ("test tag",dataPoint.tags()[0].name());
    analysis::Analysis analysis = analysisRecord.analysis();
    ASSERT_EQ(1u,analysis.dataPoints().size());
    dataPoint = analysis.dataPoints()[0];
    ASSERT_EQ(1u,dataPoint.tags().size());
    EXPECT_EQ("test tag",dataPoint.tags()[0].name());

    // adding a second tag and resaving the analysis
    dataPoint.addTag("another test");
    analysisRecord = AnalysisRecord(analysis,database);
    bool test = database.save();
    EXPECT_TRUE(test);
    ASSERT_EQ(1u,analysisRecord.dataPointRecords().size());
    dataPointRecord = analysisRecord.dataPointRecords()[0];
    TagRecordVector tagRecords = dataPointRecord.tagRecords();
    EXPECT_EQ(2u,tagRecords.size());
    EXPECT_FALSE(std::find_if(tagRecords.begin(),tagRecords.end(),testRecordFinder) == tagRecords.end());
    EXPECT_FALSE(std::find_if(tagRecords.begin(),tagRecords.end(),anotherTestRecordFinder) == tagRecords.end());
    dataPoint = dataPointRecord.dataPoint();
    TagVector tags = dataPoint.tags();
    EXPECT_EQ(2u,tags.size());
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),testTagFinder) == tags.end());
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),anotherTestTagFinder) == tags.end());
    analysis = analysisRecord.analysis();
    ASSERT_EQ(1u,analysis.dataPoints().size());
    dataPoint = analysis.dataPoints()[0];
    tags = dataPoint.tags();
    EXPECT_EQ(2u,tags.size());
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),testTagFinder) == tags.end());
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),anotherTestTagFinder) == tags.end());
    tagRecords = TagRecord::getTagRecords(database);
    EXPECT_EQ(2u,tagRecords.size());
    EXPECT_FALSE(std::find_if(tagRecords.begin(),tagRecords.end(),testRecordFinder) == tagRecords.end());
    EXPECT_FALSE(std::find_if(tagRecords.begin(),tagRecords.end(),anotherTestRecordFinder) == tagRecords.end());
  }

  {
    ProjectDatabase database = getExistingDatabase("DataPointTagRecords");
    ASSERT_EQ(1u,AnalysisRecord::getAnalysisRecords(database).size());
    AnalysisRecord analysisRecord = AnalysisRecord::getAnalysisRecords(database)[0];
    ASSERT_EQ(1u,analysisRecord.dataPointRecords().size());
    DataPointRecord dataPointRecord = analysisRecord.dataPointRecords()[0];
    TagRecordVector tagRecords = dataPointRecord.tagRecords();
    EXPECT_EQ(2u,tagRecords.size());
    EXPECT_FALSE(std::find_if(tagRecords.begin(),tagRecords.end(),testRecordFinder) == tagRecords.end());
    EXPECT_FALSE(std::find_if(tagRecords.begin(),tagRecords.end(),anotherTestRecordFinder) == tagRecords.end());
    dataPoint = dataPointRecord.dataPoint();
    TagVector tags = dataPoint.tags();
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),testTagFinder) == tags.end());
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),anotherTestTagFinder) == tags.end());
    analysis = analysisRecord.analysis();
    ASSERT_EQ(1u,analysis.dataPoints().size());
    dataPoint = analysis.dataPoints()[0];
    tags = dataPoint.tags();
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),testTagFinder) == tags.end());
    EXPECT_FALSE(std::find_if(tags.begin(),tags.end(),anotherTestTagFinder) == tags.end());
  }

}
openstudio::analysis::Analysis AnalysisFixture::analysis1(AnalysisState state) {
  // Create problem and analysis
  Problem problem("My Problem");

  BCLMeasure bclMeasure(resourcesPath() / toPath("utilities/BCL/Measures/SetWindowToWallRatioByFacade"));
  RubyMeasure measure(bclMeasure);

  // Measure Group
  StringVector choices;
  choices.push_back("North");
  choices.push_back("South");
  choices.push_back("East");
  choices.push_back("West");
  OSArgument facade = OSArgument::makeChoiceArgument("facade",choices);
  OSArgument arg = facade.clone();
  arg.setValue("South");
  measure.setArgument(arg);
  OSArgument wwr = OSArgument::makeDoubleArgument("wwr");
  MeasureVector measures(1u,NullMeasure());
  measures.push_back(measure.clone().cast<Measure>());
  arg = wwr.clone();
  arg.setValue(0.1);
  measures.back().cast<RubyMeasure>().setArgument(arg);
  measures.push_back(measure.clone().cast<Measure>());
  arg = wwr.clone();
  arg.setValue(0.2);
  measures.back().cast<RubyMeasure>().setArgument(arg);
  measures.push_back(measure.clone().cast<Measure>());
  arg = wwr.clone();
  arg.setValue(0.3);
  measures.back().cast<RubyMeasure>().setArgument(arg);
  problem.push(MeasureGroup("South Windows",measures));

  // Continuous Variables Attached to Arguments
  arg = facade.clone();
  arg.setValue("North");
  measure.setArgument(arg);
  arg = wwr.clone();
  RubyContinuousVariable wwrCV("Window to Wall Ratio",arg,measure);
  wwrCV.setMinimum(0.0);
  wwrCV.setMaximum(1.0);
  TriangularDistribution td(0.2,0.0,0.5);
  wwrCV.setUncertaintyDescription(td);
  problem.push(wwrCV);
  OSArgument offset = OSArgument::makeDoubleArgument("offset");
  RubyContinuousVariable offsetCV("Offset",offset,measure);
  offsetCV.setMinimum(0.0);
  offsetCV.setMaximum(1.5);
  NormalDistribution nd(0.9,0.05);
  offsetCV.setUncertaintyDescription(nd);
  problem.push(offsetCV);

  // Simulation
  problem.push(WorkItem(JobType::ModelToIdf));
  problem.push(WorkItem(JobType::EnergyPlusPreProcess));
  problem.push(WorkItem(JobType::EnergyPlus));
  problem.push(WorkItem(JobType::OpenStudioPostProcess));

  // Responses
  LinearFunction response1("Energy Use Intensity",
                           VariableVector(1u,OutputAttributeVariable("EUI","site.eui")));
  problem.pushResponse(response1);
  VariableVector vars;
  vars.push_back(OutputAttributeVariable("Heating Energy","heating.energy.gas"));
  vars.push_back(OutputAttributeVariable("Cooling Energy","cooling.energy.elec"));
  DoubleVector coeffs;
  coeffs.push_back(1.0); // approx. source factor
  coeffs.push_back(2.5); // approx. source factor
  LinearFunction response2("Approximate Source Energy",vars,coeffs);
  problem.pushResponse(response2);
  LinearFunction response3("North WWR",VariableVector(1u,wwrCV)); // input variable as output
  problem.pushResponse(response3);

  Analysis analysis("My Analysis",problem,FileReferenceType::OSM);

  if (state == PreRun) {
    // Add three DataPoints
    std::vector<QVariant> values;
    values.push_back(0);
    values.push_back(0.2);
    values.push_back(0.9);
    OptionalDataPoint dataPoint = problem.createDataPoint(values);
    analysis.addDataPoint(*dataPoint);
    values[0] = 1; values[1] = 0.21851789; values[2] = 1.1681938;
    dataPoint = problem.createDataPoint(values);
    analysis.addDataPoint(*dataPoint);
    values[0] = 2; values[1] = 0.0; values[2] = 0.581563892;
    dataPoint = problem.createDataPoint(values);
    analysis.addDataPoint(*dataPoint);
  }
  else {
    // state == PostRun
    // Add one complete DataPoint
    std::vector<QVariant> values;
    values.push_back(1);
    values.push_back(0.3);
    values.push_back(0.9);
    DoubleVector responseValues;
    responseValues.push_back(58.281967);
    responseValues.push_back(718952.281);
    responseValues.push_back(0.3);
    TagVector tags;
    tags.push_back(Tag("custom"));
    tags.push_back(Tag("faked"));
    // attributes
    AttributeVector attributes;
    attributes.push_back(Attribute("electricity.Cooling",281.281567,"kWh"));
    attributes.push_back(Attribute("electricity.Lighting",19206.291876,"kWh"));
    attributes.push_back(Attribute("electricity.Equipment",5112.125718,"kWh"));
    attributes = AttributeVector(1u,Attribute("enduses.electric",attributes));
    attributes.push_back(Attribute("eui",createQuantity(128.21689,"kBtu/ft^2").get()));
    // complete job
    // 1. get vector of work items
    std::vector<WorkItem> workItems;
    // 0
    workItems.push_back(problem.variables()[0].cast<MeasureGroup>().createWorkItem(QVariant(1),
                                                                                   toPath(rubyOpenStudioDir())));
    RubyContinuousVariable rcv = problem.variables()[1].cast<RubyContinuousVariable>();
    RubyMeasure rm = rcv.measure();
    OSArgument arg = rcv.argument().clone();
    arg.setValue(values[1].toDouble());
    rm.setArgument(arg);
    rcv = problem.variables()[2].cast<RubyContinuousVariable>();
    arg = rcv.argument().clone();
    arg.setValue(values[2].toDouble());
    rm.setArgument(arg);
    // 1
    workItems.push_back(rm.createWorkItem(toPath(rubyOpenStudioDir())));
    // 2
    workItems.push_back(WorkItem(JobType::ModelToIdf));
    // 3
    workItems.push_back(WorkItem(JobType::EnergyPlusPreProcess));
    // 4
    workItems.push_back(WorkItem(JobType::EnergyPlus));
    // 5
    workItems.push_back(WorkItem(JobType::OpenStudioPostProcess));
    // 2. step through work items and create jobs with results
    WorkItem wi = workItems[5];
    std::vector<FileInfo> inFiles = wi.files.files();
    inFiles.push_back(FileInfo("eplusout.sql",
                               DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,33,32)),
                               "",
                               toPath("myProject/fakeDataPoint/75-OpenStudioPostProcess-0/eplusout.sql")));                             
    Files inFilesObject(inFiles);
    std::vector<std::pair<ErrorType, std::string> > errors;
    errors.push_back(std::make_pair(ErrorType::Info,"Post-process completed successfully."));
    JobErrors errorsObject(OSResultValue::Success,errors);
    std::vector<FileInfo> outFiles;
    outFiles.push_back(FileInfo("report.xml",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,34,21)),
                                "",
                                toPath("myProject/fakeDataPoint/75-OpenStudioPostProcess-0/report.xml")));
    Files outFilesObject(outFiles);
    Job job = JobFactory::createJob(
          wi.type,
          wi.tools,
          wi.params,
          inFilesObject,
          std::vector<openstudio::URLSearchPath>(),
          false,
          createUUID(),
          JobState(
            DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,34,21)),
            errorsObject,
            outFilesObject,
            AdvancedStatus(),
            openstudio::path())

          ); // OpenStudioPostProcess

    Job jobLast = job;
    wi = workItems[4];
    inFiles = wi.files.files();
    inFiles.push_back(FileInfo("in.idf",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,23,05)),
                                "",
                                toPath("myProject/fakeDataPoint/74-EnergyPlus-0/in.idf")));
    inFilesObject = Files(inFiles);
    errors.clear();
    errors.push_back(std::make_pair(ErrorType::Warning,"ENERGYPLUS WARNING: ..."));
    errors.push_back(std::make_pair(ErrorType::Warning,"ENERGYPLUS WARNING: ..."));
    errors.push_back(std::make_pair(ErrorType::Warning,"ENERGYPLUS WARNING: ..."));
    errorsObject = JobErrors(OSResultValue::Success,errors);
    outFiles.clear();
    outFiles.push_back(FileInfo("eplusout.sql",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,33,32)),
                                "",
                                toPath("myProject/fakeDataPoint/74-EnergyPlus-0/eplusout.sql")));
    outFiles.push_back(FileInfo("eplusout.err",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,33,34)),
                                "",
                                toPath("myProject/fakeDataPoint/74-EnergyPlus-0/eplusout.err")));
    outFilesObject = Files(outFiles);
    job = JobFactory::createJob(
          wi.type,
          wi.tools,
          wi.params,
          inFilesObject,
          std::vector<openstudio::URLSearchPath>(),
          false,
          createUUID(),
          JobState(
            DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,33,42)),
            errorsObject,
            outFilesObject,
            AdvancedStatus(),
            openstudio::path())
          ); // EnergyPlus
    job.addChild(jobLast);

    jobLast = job;
    wi = workItems[3];
    inFiles = wi.files.files();
    inFiles.push_back(FileInfo("in.idf",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,22,30)),
                                "",
                                toPath("myProject/fakeDataPoint/73-EnergyPlusPreProcess-0/in.idf")));
    inFilesObject = Files(inFiles);
    errors.clear();
    errorsObject = JobErrors(OSResultValue::Success,errors);
    outFiles.clear();
    outFiles.push_back(FileInfo("out.idf",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,23,05)),
                                "",
                                toPath("myProject/fakeDataPoint/73-EnergyPlusPreProcess-0/out.idf")));
    outFilesObject = Files(outFiles);
    job = JobFactory::createJob(
          wi.type,
          wi.tools,
          wi.params,
          inFilesObject,
          std::vector<openstudio::URLSearchPath>(),
          false,
          createUUID(),
          JobState(
            DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,23,12)),
            errorsObject,
            outFilesObject,
            AdvancedStatus(),
            openstudio::path())
          ); // EnergyPlusPreProcess
    job.addChild(jobLast);

    jobLast = job;
    wi = workItems[2];
    inFiles = wi.files.files();
    inFiles.push_back(FileInfo("in.osm",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,22,01)),
                                "",
                                toPath("myProject/fakeDataPoint/72-ModelToIdf-0/in.osm")));
    inFilesObject = Files(inFiles);
    errors.clear();
    errors.push_back(std::make_pair(ErrorType::Info,"Did not find ScheduleTypeLimits for Schedule ..."));
    errors.push_back(std::make_pair(ErrorType::Warning,"Unexpectedly did not find a child object of a certain type, replaced with a default one."));
    errorsObject = JobErrors(OSResultValue::Success,errors);
    outFiles.clear();
    outFiles.push_back(FileInfo("out.idf",
                                DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,22,30)),
                                "",
                                toPath("myProject/fakeDataPoint/72-ModelToIdf-0/out.idf")));
    outFilesObject = Files(outFiles);
    job = JobFactory::createJob(
          wi.type,
          wi.tools,
          wi.params,
          inFilesObject,
          std::vector<openstudio::URLSearchPath>(),
          false,
          createUUID(),
          JobState(
            DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,22,32)),
            errorsObject,
            outFilesObject,
            AdvancedStatus(),
            openstudio::path())
          ); // ModelToIdf
    job.addChild(jobLast);

    jobLast = job;
    wi = workItems[1];
    errors.clear();
    errors.push_back(std::make_pair(ErrorType::InitialCondition,"Started with a window to wall ratio of ..."));
    errors.push_back(std::make_pair(ErrorType::FinalCondition,"Set the window to wall ratio ..."));
    errorsObject = JobErrors(OSResultValue::Success,errors);
    outFiles.clear();
    outFilesObject = Files(outFiles);
    wi.params.append("outdir","myProject/fakeDataPoint/");
    job = JobFactory::createJob(
          wi.type,
          wi.tools,
          wi.params,
          wi.files,
          std::vector<openstudio::URLSearchPath>(),
          false,
          createUUID(),
          JobState(
            DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,21,52)),
            errorsObject,
            outFilesObject,
            AdvancedStatus(),
            openstudio::path())
          ); // Variables 2 & 3
    job.addChild(jobLast);

    jobLast = job;
    wi = workItems[0];
    errors.clear();
    errors.push_back(std::make_pair(ErrorType::InitialCondition,"Started with a window to wall ratio of ..."));
    errors.push_back(std::make_pair(ErrorType::FinalCondition,"Set the window to wall ratio ..."));
    errorsObject = JobErrors(OSResultValue::Success,errors);
    outFiles.clear();
    outFilesObject = Files(outFiles);
    // add outdir job param
    JobParams params = wi.params;
    params.append("outdir","myProject/fakeDataPoint");
    job = JobFactory::createJob(
          wi.type,
          wi.tools,
          params,
          wi.files,
          std::vector<openstudio::URLSearchPath>(),
          false,
          createUUID(),
          JobState(
            DateTime(Date(MonthOfYear::Mar,21,2018),Time(0,8,21,10)),
            errorsObject,
            outFilesObject,
            AdvancedStatus(),
            openstudio::path())
          ); // Variable 1
    job.addChild(jobLast);

    DataPoint dataPoint(createUUID(),
                        createUUID(),
                        "fakeDataPoint",
                        "Fake Data Point",
                        "Demonstrating json serialization of complete DataPoint.",
                        problem,
                        true,
                        false,
                        true,
                        DataPointRunType::Local,
                        values,
                        responseValues,
                        toPath("myProject/fakeDataPoint/"),
                        FileReference(toPath("myProject/fakeDataPoint/71-Ruby-0/out.osm")),
                        FileReference(toPath("myProject/fakeDataPoint/72-ModelToIdf-0/out.idf")),
                        FileReference(toPath("myProject/fakeDataPoint/74-EnergyPlus-0/eplusout.sql")),
                        FileReferenceVector(1u,FileReference(toPath("myProject/fakeDataPoint/75-OpenStudioPostProcess-0/report.xml"))),
                        job,
                        std::vector<openstudio::path>(),
                        tags,
                        attributes);
    EXPECT_TRUE(analysis.addDataPoint(dataPoint));
  }

  return analysis;
}