This function is altogether too long and needs to be broken up into smaller functions. This is particularly true for the HVAC translation.
| 352 | // This function is altogether too long and needs to be broken up into smaller functions. |
| 353 | // This is particularly true for the HVAC translation. |
| 354 | boost::optional<contam::IndexModel> ForwardTranslator::translateModel(model::Model model) { |
| 355 | m_logSink.setThreadId(std::this_thread::get_id()); |
| 356 | m_logSink.resetStringStream(); |
| 357 | |
| 358 | { |
| 359 | std::string s = ::openstudiocontam::embedded_files::getFileAsString(":/templates/template.prj"); |
| 360 | Reader r(s); |
| 361 | m_prjModel.read(r); |
| 362 | if (!m_prjModel.valid()) { |
| 363 | LOG(Error, "Unable to load templates"); |
| 364 | return boost::none; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // The template is a legal PRJ file, so it has one level. Not for long. |
| 369 | m_prjModel.setLevels(std::vector<Level>()); |
| 370 | |
| 371 | // Do some setup work |
| 372 | if (m_leakageDescriptor) { |
| 373 | if (!applyAirtightnessLevel(m_prjModel)) { |
| 374 | LOG(Error, "Application of airtightness level failed."); |
| 375 | return {}; |
| 376 | } |
| 377 | } else { |
| 378 | if (!applyExteriorFlowRate(m_prjModel)) { |
| 379 | LOG(Error, "Application of exterior flow rate failed."); |
| 380 | return {}; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | std::string output; |
| 385 | int nr; |
| 386 | // Set top-level model info |
| 387 | boost::optional<model::Building> building = model.getOptionalUniqueModelObject<model::Building>(); |
| 388 | std::string modelDescr = std::string("Automatically generated OpenStudio model"); |
| 389 | if (building) { |
| 390 | boost::optional<std::string> name = building->name(); |
| 391 | if (name) { |
| 392 | modelDescr = std::string("Automatically generated from \"") + name.get() + std::string("\" OpenStudio model"); |
| 393 | } |
| 394 | } |
| 395 | m_prjModel.setDesc(modelDescr); |
| 396 | // Set the simulation length to match the length of the E+ simulation |
| 397 | boost::optional<openstudio::model::RunPeriod> rp = model.runPeriod(); |
| 398 | if (rp) { |
| 399 | bool goodDates = true; |
| 400 | std::string startString; |
| 401 | std::string endString; |
| 402 | try { |
| 403 | const openstudio::Date start(rp->getBeginMonth(), rp->getBeginDayOfMonth()); |
| 404 | startString = fmt::format("{}{:02d}", start.monthOfYear().valueName(), start.dayOfMonth()); |
| 405 | const openstudio::Date end(rp->getEndMonth(), rp->getEndDayOfMonth()); |
| 406 | endString = fmt::format("{}{:02d}", end.monthOfYear().valueName(), end.dayOfMonth()); |
| 407 | m_startDateTime = boost::optional<DateTime>(DateTime(start, Time(0))); |
| 408 | m_endDateTime = boost::optional<DateTime>(DateTime(end, Time(0, 24))); |
| 409 | } catch (...) { |
| 410 | goodDates = false; |
| 411 | LOG(Error, "Failed to get usable start of end date from run period object, defaulting to steady state") |