GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 10.9% 21 / 0 / 192
Functions: 8.3% 1 / 0 / 12
Branches: 6.2% 27 / 0 / 432

src/model/FileOperations.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "FileOperations.hpp"
7
8 #include "Model.hpp"
9 #include "Component.hpp"
10
11 #include "Building.hpp"
12 #include "Building_Impl.hpp"
13 #include "Facility.hpp"
14 #include "Facility_Impl.hpp"
15 #include "Site.hpp"
16 #include "Site_Impl.hpp"
17 #include "LifeCycleCostParameters.hpp"
18 #include "LifeCycleCostParameters_Impl.hpp"
19 #include "Model_Impl.hpp"
20 #include "WeatherFile.hpp"
21 #include "WeatherFile_Impl.hpp"
22 #include "SimulationControl.hpp"
23 #include "SimulationControl_Impl.hpp"
24 #include "SizingParameters.hpp"
25 #include "SizingParameters_Impl.hpp"
26 #include "Timestep.hpp"
27 #include "Timestep_Impl.hpp"
28 #include "ShadowCalculation.hpp"
29 #include "ShadowCalculation_Impl.hpp"
30 #include "HeatBalanceAlgorithm.hpp"
31 #include "HeatBalanceAlgorithm_Impl.hpp"
32 #include "RunPeriod.hpp"
33 #include "RunPeriod_Impl.hpp"
34 #include "ConstructionBase.hpp"
35 #include "ConstructionBase_Impl.hpp"
36 #include "Material.hpp"
37 #include "Material_Impl.hpp"
38 #include "BuildingStory.hpp"
39 #include "BuildingStory_Impl.hpp"
40 #include "LightingSimulationZone.hpp"
41 #include "LightingSimulationZone_Impl.hpp"
42 #include "SpaceType.hpp"
43 #include "SpaceType_Impl.hpp"
44 #include "ThermalZone.hpp"
45 #include "ThermalZone_Impl.hpp"
46 #include "BuildingUnit.hpp"
47 #include "BuildingUnit_Impl.hpp"
48 #include "RenderingColor.hpp"
49 #include "StandardsInformationConstruction.hpp"
50 #include "StandardsInformationMaterial.hpp"
51
52 #include "../utilities/filetypes/EpwFile.hpp"
53 #include "../utilities/bcl/BCLMeasure.hpp"
54 #include "../utilities/plot/ProgressBar.hpp"
55 #include "../utilities/core/Logger.hpp"
56 #include "../utilities/core/PathHelpers.hpp"
57 #include "../utilities/core/Assert.hpp"
58 #include "../utilities/core/FilesystemHelpers.hpp"
59
60 #include <OpenStudio.hxx>
61
62 namespace openstudio {
63 namespace model {
64 ✗ bool replaceDir(const openstudio::path& sourceDir, const openstudio::path& destinationDir) {
65 ✗ bool result = true;
66
67 // Path **must** exist for `canonical()` to work, `weakly_canonical()` doesn't require this
68 ✗ const auto src = openstudio::filesystem::weakly_canonical(sourceDir);
69 ✗ const auto dest = openstudio::filesystem::weakly_canonical(destinationDir);
70
71 // If both are the same canonical paths, we do nothing
72 ✗ if (src == dest) {
73 ✗ LOG_FREE(Warn, "replaceDir",
74 "Refusing to copy directory '" << toString(sourceDir) << "' only itself '" << toString(destinationDir) << "' canonical path: '"
75 << toString(src) << "'");
76 ✗ return true; // there is no error, just nothing to do
77 }
78
79 // If the source dir doesn't exist, there's a problem
80 ✗ if (!openstudio::filesystem::exists(src) || !openstudio::filesystem::is_directory(src)) {
81 ✗ LOG_FREE(Error, "replaceDir", "Source directory does not exist: " << toString(src));
82 ✗ return false;
83 }
84
85 // If the dest dir exists, remove all entries in it
86 ✗ if (openstudio::filesystem::exists(dest)) {
87 ✗ if (openstudio::filesystem::is_directory(dest)) {
88 ✗ for (openstudio::filesystem::directory_iterator end_dir_it, it(dest); it != end_dir_it; ++it) {
89 ✗ openstudio::filesystem::path path = it->path();
90 try {
91 ✗ openstudio::filesystem::remove_all(path);
92 ✗ } catch (const std::exception& e) {
93 ✗ LOG_FREE(Error, "replaceDir", "Error deleting: " << toString(path) << e.what());
94 ✗ result = false;
95 ✗ }
96 ✗ }
97 } else {
98 ✗ LOG_FREE(Error, "replaceDir", "Destination exists and is not a directory, aborting: " << toString(dest));
99 ✗ return false;
100 }
101 }
102
103 // Now we recreate it, and it must work!
104 ✗ if (!openstudio::filesystem::exists(dest)) {
105 try {
106 ✗ if (!openstudio::filesystem::create_directory(dest)) {
107 ✗ LOG_FREE(Error, "replaceDir", "Could not create destination directory: " << toString(dest));
108 ✗ return false;
109 }
110 ✗ } catch (const std::exception& e) {
111 ✗ LOG_FREE(Error, "replaceDir", "Error creating directory: " << toString(dest) << e.what());
112 ✗ return false;
113 ✗ }
114 }
115
116 // Not using PathHelper's copyDirectory because we want clear log messages
117 ✗ for (const auto& dirEnt : openstudio::filesystem::directory_iterator{src}) {
118 ✗ const auto& srcFolderPath = dirEnt.path();
119 ✗ const auto& relativeFolderPath = openstudio::filesystem::relative(srcFolderPath, src);
120
121 try {
122 // No need to pass the copy_options::overwrite_existing here, we wiped the directory before
123 ✗ openstudio::filesystem::copy(srcFolderPath, dest / relativeFolderPath, openstudio::filesystem::copy_options::recursive);
124 ✗ } catch (const std::exception& e) {
125 ✗ LOG_FREE(Error, "replaceDir",
126 "Error copying from: " << toString(srcFolderPath) << " to: " << toString(dest / relativeFolderPath) << " Description: " << e.what());
127 ✗ result = false;
128 ✗ }
129 ✗ }
130
131 ✗ return result;
132 ✗ }
133
134 ✗ bool removeDir(const openstudio::path& path) {
135 try {
136 ✗ const auto count = openstudio::filesystem::remove_all(path);
137 ✗ LOG_FREE(Debug, "removeDir", "Removed " << count << " files");
138 ✗ } catch (const std::exception& e) {
139 ✗ LOG_FREE(Error, "removeDir", "Error removing from: " << toString(path) << " Description: " << e.what());
140 ✗ return false;
141 ✗ }
142
143 ✗ return true;
144 }
145
146 ✗ openstudio::path createModelTempDir() {
147 ✗ const auto result = openstudio::filesystem::create_temporary_directory("osmodel");
148 ✗ LOG_FREE(Info, "createModelTempDir", "Created directory '" << toString(result) << "'");
149 ✗ OS_ASSERT(!result.empty());
150 ✗ return result;
151 ✗ }
152
153 ✗ bool initializeModelTempDir(const openstudio::path& osmPath, const openstudio::path& modelTempDir) {
154 ✗ bool result = true;
155
156 ✗ if (osmPath.empty() || !openstudio::filesystem::exists(osmPath)) {
157
158 ✗ LOG_FREE(Debug, "initializeModelTempDir", "OSM path '" << toString(osmPath) << "' is empty or does not exist");
159 ✗ result = false;
160
161 } else {
162 openstudio::path fileName = osmPath.filename();
163 ✗ openstudio::path destination = modelTempDir / fileName;
164 ✗ LOG_FREE(Debug, "initializeModelTempDir", "Copying '" << toString(osmPath) << "' to '" << toString(destination) << "'");
165
166 // copy osm file
167 ✗ bool test = true;
168 try {
169 ✗ openstudio::filesystem::copy_file(osmPath, destination);
170 ✗ test = true;
171 ✗ } catch (const std::exception& e) {
172 ✗ LOG_FREE(Error, "initializeModelTempDir",
173 "Could not copy osm from '" << toString(osmPath) << "' to '" << toString(destination) << "' error" << e.what());
174 ✗ test = false;
175 ✗ }
176
177 // Copy all files from existing resources dir into temp dir when opening
178 ✗ openstudio::path sourceDir = getCompanionFolder(osmPath);
179 ✗ openstudio::path destDir = modelTempDir / toPath("resources");
180 ✗ if (openstudio::filesystem::exists(sourceDir)) {
181 ✗ LOG_FREE(Debug, "initializeModelTempDir", "Copying '" << toString(sourceDir) << "' to '" << toString(destDir) << "'");
182
183 ✗ test = replaceDir(sourceDir, destDir);
184 ✗ if (!test) {
185 ✗ LOG_FREE(Error, "initializeModelTempDir", "Could not copy '" << toString(sourceDir) << "' to '" << toString(destDir) << "'");
186 ✗ result = false;
187 }
188 }
189 ✗ }
190
191 ✗ return result;
192 }
193
194 ✗ bool updateModelTempDir(openstudio::model::Model& /*model*/, const openstudio::path& modelTempDir) {
195 ✗ bool modified = false;
196
197 // Create folder layout
198 ✗ openstudio::path runpath = modelTempDir / openstudio::toPath("resources/run");
199 ✗ openstudio::path scriptspath = modelTempDir / openstudio::toPath("resources/measures");
200 ✗ openstudio::path filespath = modelTempDir / openstudio::toPath("resources/files");
201
202 ✗ if (!openstudio::filesystem::exists(runpath)) {
203 ✗ openstudio::filesystem::create_directories(runpath);
204 ✗ modified = true;
205 }
206
207 ✗ if (!openstudio::filesystem::exists(scriptspath)) {
208 ✗ openstudio::filesystem::create_directories(scriptspath);
209 ✗ modified = true;
210 }
211
212 ✗ if (!openstudio::filesystem::exists(filespath)) {
213 ✗ openstudio::filesystem::create_directories(filespath);
214 ✗ modified = true;
215 }
216
217 // weather file is fixed in OSDocument::fixWeatherFileOnOpen
218
219 ✗ return modified;
220 ✗ }
221
222 ✗ bool attachWorkflow(openstudio::model::Model& model, const openstudio::path& modelTempDir) {
223 ✗ boost::optional<WorkflowJSON> workflowJSON;
224 ✗ bool result = false;
225
226 ✗ openstudio::path oswPath = modelTempDir / openstudio::toPath("resources/workflow.osw");
227 ✗ if (boost::filesystem::exists(oswPath)) {
228 ✗ workflowJSON = WorkflowJSON::load(oswPath);
229 ✗ if (workflowJSON) {
230 ✗ result = true;
231 ✗ LOG_FREE(Debug, "attachWorkflow", "Opened existing workflow.osw file");
232 } else {
233 ✗ LOG_FREE(Error, "attachWorkflow", "Could not open existing workflow.osw file");
234 }
235 }
236
237 ✗ if (!workflowJSON) {
238 ✗ workflowJSON = WorkflowJSON();
239 ✗ workflowJSON->setOswPath(oswPath);
240 ✗ workflowJSON->save();
241 }
242 ✗ OS_ASSERT(workflowJSON);
243 ✗ model.setWorkflowJSON(*workflowJSON);
244
245 ✗ return result;
246 ✗ }
247
248 ✗ openstudio::path initializeModel(openstudio::model::Model model) {
249 ✗ return initializeModel(model, openstudio::path());
250 }
251
252 ✗ openstudio::path initializeModel(openstudio::model::Model model, const openstudio::path& savedPath) {
253 ✗ openstudio::path modelTempDir = createModelTempDir();
254
255 ✗ if (!savedPath.empty() && exists(savedPath)) {
256 ✗ initializeModelTempDir(savedPath, modelTempDir);
257 }
258
259 ✗ updateModelTempDir(model, modelTempDir);
260 ✗ attachWorkflow(model, modelTempDir);
261
262 ✗ return modelTempDir;
263 ✗ }
264
265 77 void initializeModelObjects(openstudio::model::Model model) {
266 // These objects used to be added to the model as you clicked through the App's tabs,
267 // resulting in a uncertain set of model changes. With these changes, every model will
268 // always have the following objects after opening in the app.
269
1/1
✓ Branch 1 taken 77 times.
77 openstudio::model::Building building = model.getUniqueModelObject<openstudio::model::Building>();
270
1/1
✓ Branch 1 taken 77 times.
77 auto facility = model.getUniqueModelObject<openstudio::model::Facility>();
271
1/1
✓ Branch 1 taken 77 times.
77 auto site = model.getUniqueModelObject<openstudio::model::Site>();
272
273 // from simulation tab
274 //model.getUniqueModelObject<openstudio::model::RadianceParameters>();
275
1/1
✓ Branch 1 taken 77 times.
77 model.getUniqueModelObject<openstudio::model::SimulationControl>();
276
1/1
✓ Branch 1 taken 77 times.
77 model.getUniqueModelObject<openstudio::model::SizingParameters>();
277 //model.getUniqueModelObject<openstudio::model::ProgramControl>();
278
1/1
✓ Branch 1 taken 77 times.
77 model.getUniqueModelObject<openstudio::model::Timestep>();
279 //model.getUniqueModelObject<openstudio::model::ReportingTolerances>();
280 //model.getUniqueModelObject<openstudio::model::ConvergenceLimits>();
281
1/1
✓ Branch 1 taken 77 times.
77 model.getUniqueModelObject<openstudio::model::ShadowCalculation>();
282 //model.getUniqueModelObject<openstudio::model::SurfaceConvectionAlgorithmInside>();
283 //model.getUniqueModelObject<openstudio::model::SurfaceConvectionAlgorithmOutside>();
284
1/1
✓ Branch 1 taken 77 times.
77 model.getUniqueModelObject<openstudio::model::HeatBalanceAlgorithm>();
285 //model.getUniqueModelObject<openstudio::model::ZoneAirHeatBalanceAlgorithm>();
286 //model.getUniqueModelObject<openstudio::model::ZoneAirContaminantBalance>();
287 //model.getUniqueModelObject<openstudio::model::ZoneCapacitanceMultiplierResearchSpecial>();
288
1/1
✓ Branch 1 taken 77 times.
77 model.getUniqueModelObject<openstudio::model::RunPeriod>();
289
290
1/1
✓ Branch 1 taken 77 times.
77 openstudio::model::LifeCycleCostParameters lifeCycleCostParameters = model.getUniqueModelObject<openstudio::model::LifeCycleCostParameters>();
291
292
3/3
✓ Branch 1 taken 77 times.
✓ Branch 8 taken 770 times.
✓ Branch 9 taken 77 times.
847 for (auto& object : model.objects()) {
293
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 if (object.optionalCast<model::ConstructionBase>()) {
294 ✗ object.cast<model::ConstructionBase>().standardsInformation();
295 ✗ object.cast<model::ConstructionBase>().renderingColor();
296
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 } else if (object.optionalCast<model::Material>()) {
297 ✗ object.cast<model::Material>().standardsInformation();
298
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 } else if (object.optionalCast<model::BuildingStory>()) {
299 ✗ object.cast<model::BuildingStory>().renderingColor();
300
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 } else if (object.optionalCast<model::LightingSimulationZone>()) {
301 ✗ object.cast<model::LightingSimulationZone>().renderingColor();
302
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 } else if (object.optionalCast<model::SpaceType>()) {
303 ✗ object.cast<model::SpaceType>().renderingColor();
304
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 } else if (object.optionalCast<model::ThermalZone>()) {
305 ✗ object.cast<model::ThermalZone>().renderingColor();
306
2/3
✓ Branch 1 taken 770 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 770 times.
1540 } else if (object.optionalCast<model::BuildingUnit>()) {
307 ✗ object.cast<model::BuildingUnit>().renderingColor();
308 }
309 77 }
310 77 }
311
312 ✗ bool saveModelTempDir(const openstudio::path& modelTempDir, const openstudio::path& osmPath) {
313 ✗ bool test = true;
314
315 // copy osm file
316 ✗ openstudio::path srcPath = modelTempDir / osmPath.filename();
317
318 try {
319 ✗ openstudio::filesystem::copy_file(srcPath, osmPath, openstudio::filesystem::copy_options::overwrite_existing);
320 ✗ test = true;
321 ✗ } catch (const std::exception& e) {
322 ✗ LOG_FREE(Error, "saveModelTempDir", "Could not copy osm from '" << toString(srcPath) << "' to '" << toString(osmPath) << "' error" << e.what());
323 ✗ test = false;
324 ✗ }
325
326 ✗ if (openstudio::filesystem::is_regular_file(osmPath)) {
327 // copy resources
328 ✗ openstudio::path srcDir = modelTempDir / toPath("resources");
329 // Get the companion directory
330 ✗ openstudio::path dstDir = getCompanionFolder(osmPath);
331
332 ✗ LOG_FREE(Debug, "saveModelTempDir", "Copying " << toString(srcDir) << " to " << toString(dstDir));
333
334 ✗ test = replaceDir(srcDir, dstDir);
335 ✗ if (!test) {
336 ✗ LOG_FREE(Error, "saveModelTempDir", "Could not copy '" << toString(srcDir) << "' to '" << toString(dstDir) << "'");
337 }
338 ✗ }
339
340 ✗ return test;
341 ✗ }
342
343 ✗ bool saveModel(openstudio::model::Model model, const openstudio::path& osmPath, const openstudio::path& modelTempDir) {
344 // set the workflow's path
345 ✗ openstudio::path oswPath = modelTempDir / openstudio::toPath("resources/workflow.osw");
346 ✗ boost::optional<openstudio::path> currentOswPath = model.workflowJSON().oswPath();
347 ✗ if (!currentOswPath || (oswPath != currentOswPath.get())) {
348 ✗ model.workflowJSON().setOswPath(oswPath);
349 }
350
351 // set the seed name
352 ✗ openstudio::path seedFile = toPath("..") / osmPath.filename();
353 ✗ boost::optional<openstudio::path> currentSeedFile = model.workflowJSON().seedFile();
354 ✗ if (!currentSeedFile || (seedFile != currentSeedFile.get())) {
355 ✗ model.workflowJSON().setSeedFile(seedFile);
356 }
357
358 // save the osw, do before temp dirs get copied
359 ✗ model.workflowJSON().save();
360
361 // save osm to temp directory, saveModelTempDir will copy to real location
362 ✗ openstudio::path tempModelPath = modelTempDir / osmPath.filename();
363 ✗ bool modelSaved = model.save(tempModelPath, true);
364
365 ✗ if (modelSaved) {
366 ✗ LOG_FREE(Debug, "saveModel", "Saved model to '" << toString(tempModelPath) << "'");
367 } else {
368 ✗ LOG_FREE(Error, "saveModel", "Failed to save model to '" << toString(tempModelPath) << "'");
369 }
370
371 ✗ bool tempDirSaved = saveModelTempDir(modelTempDir, osmPath);
372
373 ✗ return (modelSaved && tempDirSaved);
374 ✗ }
375
376 ✗ void removeModelTempDir(const openstudio::path& modelTempDir) {
377 ✗ removeDir(modelTempDir);
378 ✗ }
379
380 } // namespace model
381 } // namespace openstudio
382