-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnrgyModel.cpp
1384 lines (1213 loc) · 50.3 KB
/
EnrgyModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Topologic software library.
// Copyright(C) 2019, Cardiff University and University College London
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "EnergyModel.h"
#include "EnergySimulation.h"
using namespace System::Diagnostics;
using namespace System::IO;
using namespace System::Linq;
namespace TopologicEnergy
{
bool EnergyModel::CreateIdfFile(OpenStudio::Model^ osModel, String^ idfPathName)
{
if (idfPathName == nullptr)
{
throw gcnew Exception("The input idfPathName must not be null.");
}
OpenStudio::EnergyPlusForwardTranslator^ osForwardTranslator = gcnew OpenStudio::EnergyPlusForwardTranslator();
OpenStudio::Workspace^ osWorkspace = osForwardTranslator->translateModel(osModel);
OpenStudio::IdfFile^ osIdfFile = osWorkspace->toIdfFile();
OpenStudio::Path^ osIdfPath = OpenStudio::OpenStudioUtilitiesCore::toPath(idfPathName);
bool idfSaveCondition = osIdfFile->save(osIdfPath);
return idfSaveCondition;
}
EnergyModel^ EnergyModel::ByCellComplex(
CellComplex^ building,
Cluster^ shadingSurfaces,
IList<double>^ floorLevels,
String^ buildingName,
String^ buildingType,
String^ defaultSpaceType,
double northAxis,
Nullable<double> glazingRatio,
double coolingTemp,
double heatingTemp,
String^ weatherFilePath,
String^ designDayFilePath,
String^ openStudioTemplatePath
)
{
IList<double>^ floorLevelList = (IList<double>^) floorLevels;
numOfApertures = 0;
numOfAppliedApertures = 0;
CellComplex^ buildingCopy = building->Copy<CellComplex^>();
// Create an OpenStudio model from the template, EPW, and DDY
OpenStudio::Model^ osModel = GetModelFromTemplate(openStudioTemplatePath, weatherFilePath, designDayFilePath);
double buildingHeight = Enumerable::Max(floorLevels);
int numFloors = floorLevelList->Count - 1;
OpenStudio::Building^ osBuilding = ComputeBuilding(osModel, buildingName, buildingType, buildingHeight, numFloors, northAxis, defaultSpaceType);
IList<Cell^>^ pBuildingCells = buildingCopy->Cells;
// Create OpenStudio spaces
OpenStudio::SpaceVector^ osSpaceVector = gcnew OpenStudio::SpaceVector();
Autodesk::DesignScript::Geometry::Vector^ dynamoZAxis = Autodesk::DesignScript::Geometry::Vector::ZAxis();
for each(Cell^ buildingCell in pBuildingCells)
{
int spaceNumber = 1;
OpenStudio::Space^ osSpace = AddSpace(
spaceNumber,
buildingCell,
buildingCopy,
osModel,
dynamoZAxis,
buildingHeight,
floorLevels,
glazingRatio,
heatingTemp,
coolingTemp
);
Dictionary<String^, Object^>^ attributes = gcnew Dictionary<String^, Object^>();
attributes->Add("Name", osSpace->nameString());
buildingCell->AddAttributesNoCopy(attributes);
OpenStudio::SpaceVector::SpaceVectorEnumerator^ spaceEnumerator = osSpaceVector->GetEnumerator();
while (spaceEnumerator->MoveNext())
{
OpenStudio::Space^ osExistingSpace = spaceEnumerator->Current;
osSpace->matchSurfaces(osExistingSpace);
}
osSpaceVector->Add(osSpace);
}
delete dynamoZAxis;
// Create shading surfaces
if (shadingSurfaces != nullptr)
{
OpenStudio::ShadingSurfaceGroup^ osShadingGroup = gcnew OpenStudio::ShadingSurfaceGroup(osModel);
IList<Face^>^ contextFaces = shadingSurfaces->Faces;
int faceIndex = 1;
for each(Face^ contextFace in contextFaces)
{
AddShadingSurfaces(contextFace, osModel, osShadingGroup, faceIndex++);
}
}
osModel->purgeUnusedResourceObjects();
return gcnew EnergyModel(osModel, osBuilding, pBuildingCells, shadingSurfaces, osSpaceVector);
}
bool EnergyModel::ExportToOSM(EnergyModel ^ energyModel, String^ filePath)
{
if (filePath == nullptr)
{
throw gcnew Exception("The input filePath must not be null.");
}
return SaveModel(energyModel->m_osModel, filePath);
}
void EnergyModel::ProcessOsModel(
OpenStudio::Model^ osModel,
double tolerance,
OpenStudio::Building^% osBuilding,
IList<Cell^>^% buildingCells,
Topologic::Cluster^% shadingFaces,
OpenStudio::SpaceVector^% osSpaceVector)
{
osBuilding = osModel->getBuilding(); // 2
osSpaceVector = osModel->getSpaces(); // 3
OpenStudio::SpaceVector::SpaceVectorEnumerator^ spaceEnumerator = osSpaceVector->GetEnumerator();
List<OpenStudio::Space^>^ osSpaces = gcnew List<OpenStudio::Space^>();
while (spaceEnumerator->MoveNext())
{
OpenStudio::Space^ osSpace = spaceEnumerator->Current;
osSpaces->Add(osSpace);
}
// 4. Get shading surfaces as a cluster
OpenStudio::ShadingSurfaceVector^ osShadingSurfaceVector = osModel->getShadingSurfaces(); //4
OpenStudio::ShadingSurfaceVector::ShadingSurfaceVectorEnumerator^ shadingSurfaceEnumerator = osShadingSurfaceVector->GetEnumerator();
List<Topologic::Topology^>^ shadingFaceList = gcnew List<Topologic::Topology^>();
while (shadingSurfaceEnumerator->MoveNext())
{
OpenStudio::ShadingSurface^ osShadingSurface = shadingSurfaceEnumerator->Current;
Face^ shadingFace = FaceByOsSurface(osShadingSurface);
shadingFaceList->Add(shadingFace);
}
if (shadingFaceList->Count > 0)
{
shadingFaces = Topologic::Cluster::ByTopologies(shadingFaceList);
}
// 5. Get building spaces as a CellComplex
OpenStudio::SpaceVector::SpaceVectorEnumerator^ osSpaceEnumerator = osSpaceVector->GetEnumerator();
List<Topologic::Cell^>^ cellList = gcnew List<Topologic::Cell^>();
while (osSpaceEnumerator->MoveNext())
{
OpenStudio::Space^ osSpace = osSpaceEnumerator->Current;
OpenStudio::SurfaceVector^ osSurfaces = osSpace->surfaces;
OpenStudio::Transformation^ osTransformation = osSpace->transformation();
OpenStudio::Vector3d^ osTranslation = osTransformation->translation();
OpenStudio::Matrix^ osMatrix = osTransformation->rotationMatrix();
double rotation11 = osMatrix->__getitem__(0, 0);
double rotation12 = osMatrix->__getitem__(0, 1);
double rotation13 = osMatrix->__getitem__(0, 2);
double rotation21 = osMatrix->__getitem__(1, 0);
double rotation22 = osMatrix->__getitem__(1, 1);
double rotation23 = osMatrix->__getitem__(1, 2);
double rotation31 = osMatrix->__getitem__(2, 0);
double rotation32 = osMatrix->__getitem__(2, 1);
double rotation33 = osMatrix->__getitem__(2, 2);
OpenStudio::SurfaceVector::SurfaceVectorEnumerator^ osSurfaceEnumerator = osSurfaces->GetEnumerator();
List<Topologic::Face^>^ faceList = gcnew List<Topologic::Face^>();
while (osSurfaceEnumerator->MoveNext())
{
OpenStudio::Surface^ osSurface = osSurfaceEnumerator->Current;
Face^ face = FaceByOsSurface(osSurface);
// Subsurfaces
OpenStudio::SubSurfaceVector^ osSubSurfaces = osSurface->subSurfaces();
if (osSubSurfaces->Count > 0)
{
OpenStudio::SubSurfaceVector::SubSurfaceVectorEnumerator^ osSubSurfaceEnumerator = osSubSurfaces->GetEnumerator();
List<Topologic::Topology^>^ faceApertureList = gcnew List<Topologic::Topology^>();
while (osSubSurfaceEnumerator->MoveNext())
{
OpenStudio::SubSurface^ osSubSurface = osSubSurfaceEnumerator->Current;
Face^ subFace = FaceByOsSurface(osSubSurface);
faceApertureList->Add(subFace);
}
Topologic::Topology^ topologyWithApertures = face->AddApertures(faceApertureList);
try {
Face^ faceWithApertures = safe_cast<Topologic::Face^>(topologyWithApertures);
faceList->Add(faceWithApertures);
}
catch (Exception^)
{
throw gcnew Exception("Error converting a topology with apertures to a face.");
}
}
else // no subsurfaces
{
faceList->Add(face);
}
}
Cell^ cell = Cell::ByFaces(faceList, tolerance);
Topologic::Topology^ transformedTopology = Topologic::Utilities::TopologyUtility::Transform(cell,
osTranslation->x(), osTranslation->y(), osTranslation->z(),
rotation11, rotation12, rotation13,
rotation21, rotation22, rotation23,
rotation31, rotation32, rotation33);
Cell^ transformedCell = safe_cast<Topologic::Cell^>(transformedTopology);
cellList->Add(transformedCell);
}
buildingCells = gcnew List<Topologic::Cell^>();
if (cellList->Count == 1)
{
((List<Topologic::Cell^>^)buildingCells)->Add(cellList[0]);
}
else
{
CellComplex^ cellComplex = CellComplex::ByCells(cellList);
buildingCells = cellComplex->Cells;
}
}
EnergyModel ^ EnergyModel::ByImportedOSM(String ^ filePath, double tolerance)
{
if (filePath == nullptr)
{
throw gcnew Exception("The input filePath must not be null.");
}
if (tolerance <= 0.0)
{
throw gcnew Exception("The tolerance must have a positive value.");
}
OpenStudio::Path^ osOsmFile = OpenStudio::OpenStudioUtilitiesCore::toPath(filePath);
// Create an abstract model
OpenStudio::OptionalModel^ osOptionalModel = OpenStudio::Model::load(osOsmFile);
if (osOptionalModel->isNull())
{
return nullptr;
}
OpenStudio::Model^ osModel = osOptionalModel->get(); //1
if (osModel == nullptr)
{
return nullptr;
}
OpenStudio::Building^ osBuilding = nullptr;
List<Cell^>^ buildingCells = nullptr;
Cluster^ shadingFaces = nullptr;
OpenStudio::SpaceVector^ osSpaceVector = nullptr;
ProcessOsModel(osModel, tolerance, osBuilding, buildingCells, shadingFaces, osSpaceVector);
EnergyModel^ energyModel = gcnew EnergyModel(osModel, osBuilding, buildingCells, shadingFaces, osSpaceVector);
return energyModel;
}
bool EnergyModel::SaveModel(OpenStudio::Model^ osModel, String^ osmPathName)
{
if (osmPathName == nullptr)
{
throw gcnew Exception("The input osmPathName must not be null.");
}
// Purge unused resources
//osModel->purgeUnusedResourceObjects();
// Create a path string
OpenStudio::Path^ osPath = OpenStudio::OpenStudioUtilitiesCore::toPath(osmPathName);
bool osCondition = osModel->save(osPath, true);
return osCondition;
}
OpenStudio::Model^ EnergyModel::GetModelFromTemplate(String^ osmTemplatePath, String^ epwWeatherPath, String^ ddyPath)
{
if (osmTemplatePath == nullptr)
{
throw gcnew Exception("The input osmTemplatePath must not be null.");
}
if (epwWeatherPath == nullptr)
{
throw gcnew Exception("The input epwWeatherPath must not be null.");
}
if (ddyPath == nullptr)
{
throw gcnew Exception("The input ddyPath must not be null.");
}
if (osmTemplatePath == nullptr)
{
throw gcnew Exception("The input osmTemplatePath must not be null.");
}
if (!File::Exists(osmTemplatePath))
{
throw gcnew FileNotFoundException("OSM file not found.");
}
if (!File::Exists(epwWeatherPath))
{
throw gcnew FileNotFoundException("EPW file not found.");
}
if (!File::Exists(ddyPath))
{
throw gcnew FileNotFoundException("DDY file not found.");
}
OpenStudio::Path^ osTemplatePath = OpenStudio::OpenStudioUtilitiesCore::toPath(osmTemplatePath);
// Create an abstract model
OpenStudio::OptionalModel^ osOptionalModel = OpenStudio::Model::load(osTemplatePath);
OpenStudio::Model^ osModel = osOptionalModel->__ref__();
// Read an EPW weather file
OpenStudio::Path^ osEPWPath = OpenStudio::OpenStudioUtilitiesCore::toPath(epwWeatherPath);
OpenStudio::EpwFile^ osEPWFile = gcnew OpenStudio::EpwFile(osEPWPath);
OpenStudio::WeatherFile^ osWeatherFile = osModel->getWeatherFile();
OpenStudio::WeatherFile::setWeatherFile(osModel, osEPWFile);
// Read an DDY design days files
OpenStudio::Path^ osDDYPath = OpenStudio::OpenStudioUtilitiesCore::toPath(ddyPath);
OpenStudio::EnergyPlusReverseTranslator^ osTranslator = gcnew OpenStudio::EnergyPlusReverseTranslator();
OpenStudio::OptionalModel^ tempModel01 = osTranslator->loadModel(osDDYPath);
OpenStudio::Model^ tempModel02 = tempModel01->__ref__();
OpenStudio::DesignDayVector^ designDays = tempModel02->getDesignDays();
OpenStudio::DesignDayVector::DesignDayVectorEnumerator^ designDaysEnumerator = designDays->GetEnumerator();
while (designDaysEnumerator->MoveNext())
{
OpenStudio::DesignDay^ aDesignDay = designDaysEnumerator->Current;
OpenStudio::IdfObject^ anIdfObject = aDesignDay->idfObject();
osModel->addObject(anIdfObject);
}
return osModel;
}
OpenStudio::ThermalZone^ EnergyModel::CreateThermalZone(OpenStudio::Model^ model, OpenStudio::Space^ space, double ceilingHeight, double heatingTemp, double coolingTemp)
{
// Create a thermal zone for the space
OpenStudio::ThermalZone^ osThermalZone = gcnew OpenStudio::ThermalZone(model);
osThermalZone->setName(space->name()->get() + "_THERMAL_ZONE");
String^ name = osThermalZone->name()->get();
osThermalZone->setUseIdealAirLoads(true);
osThermalZone->setCeilingHeight(ceilingHeight);
osThermalZone->setVolume(space->volume());
// Assign Thermal Zone to space
// aSpace.setThermalZone(osThermalZone);//Not available in C#
OpenStudio::UUID^ tzHandle = osThermalZone->handle();
int location = 10;
space->setPointer(location, tzHandle);
OpenStudio::ScheduleConstant^ heatingScheduleConstant = gcnew OpenStudio::ScheduleConstant(model);
heatingScheduleConstant->setValue(heatingTemp);
OpenStudio::ScheduleConstant^ coolingScheduleConstant = gcnew OpenStudio::ScheduleConstant(model);
coolingScheduleConstant->setValue(coolingTemp);
// Create a Thermostat
OpenStudio::ThermostatSetpointDualSetpoint^ osThermostat = gcnew OpenStudio::ThermostatSetpointDualSetpoint(model);
// Set Heating and Cooling Schedules on the Thermostat
osThermostat->setHeatingSetpointTemperatureSchedule(heatingScheduleConstant);
osThermostat->setCoolingSetpointTemperatureSchedule(coolingScheduleConstant);
// Assign Thermostat to the Thermal Zone
osThermalZone->setThermostatSetpointDualSetpoint(osThermostat);
return osThermalZone;
}
OpenStudio::BuildingStory^ EnergyModel::AddBuildingStory(OpenStudio::Model^ model, int floorNumber)
{
OpenStudio::BuildingStory^ osBuildingStory = gcnew OpenStudio::BuildingStory(model);
osBuildingStory->setName("STORY_" + floorNumber);
osBuildingStory->setDefaultConstructionSet(getDefaultConstructionSet(model));
osBuildingStory->setDefaultScheduleSet(getDefaultScheduleSet(model));
return osBuildingStory;
}
OpenStudio::SubSurface ^ EnergyModel::CreateSubSurface(IList<Topologic::Vertex^>^ vertices, OpenStudio::Model^ osModel)
{
OpenStudio::Point3dVector^ osWindowFacePoints = gcnew OpenStudio::Point3dVector();
for each(Vertex^ vertex in vertices)
{
OpenStudio::Point3d^ osPoint = gcnew OpenStudio::Point3d(
vertex->X,
vertex->Y,
vertex->Z);
osWindowFacePoints->Add(osPoint);
}
OpenStudio::SubSurface^ osWindowSubSurface = gcnew OpenStudio::SubSurface(osWindowFacePoints, osModel);
return osWindowSubSurface;
}
OpenStudio::Building^ EnergyModel::ComputeBuilding(
OpenStudio::Model^ osModel,
String^ buildingName,
String^ buildingType,
double buildingHeight,
int numFloors,
double northAxis,
String^ spaceType)
{
OpenStudio::Building^ osBuilding = osModel->getBuilding();
osBuilding->setStandardsNumberOfStories(numFloors);
osBuilding->setDefaultConstructionSet(getDefaultConstructionSet(osModel));
osBuilding->setDefaultScheduleSet(getDefaultScheduleSet(osModel));
osBuilding->setName(buildingName);
osBuilding->setStandardsBuildingType(buildingType);
double floorToFloorHeight = (double)buildingHeight / (double)numFloors;
osBuilding->setNominalFloortoFloorHeight(floorToFloorHeight);
// Get all space types and find the one that matches
OpenStudio::SpaceTypeVector^ spaceTypes = osModel->getSpaceTypes();
OpenStudio::SpaceTypeVector::SpaceTypeVectorEnumerator^ spaceTypesEnumerator = spaceTypes->GetEnumerator();
while (spaceTypesEnumerator->MoveNext())
{
OpenStudio::SpaceType^ aSpaceType = spaceTypesEnumerator->Current;
String^ spaceTypeName = aSpaceType->name()->__str__();
if (spaceTypeName == spaceType)
{
osBuilding->setSpaceType(aSpaceType);
}
}
buildingStories = CreateBuildingStories(osModel, numFloors);
osBuilding->setNorthAxis(northAxis);
return osBuilding;
}
IList<OpenStudio::BuildingStory^>^ EnergyModel::CreateBuildingStories(OpenStudio::Model^ osModel, int numFloors)
{
List<OpenStudio::BuildingStory^>^ osBuildingStories = gcnew List<OpenStudio::BuildingStory^>();
for (int i = 0; i < numFloors; i++)
{
osBuildingStories->Add(AddBuildingStory(osModel, (i + 1)));
}
return osBuildingStories;
}
OpenStudio::SqlFile^ EnergyModel::CreateSqlFile(OpenStudio::Model ^ osModel, String^ sqlFilePath)
{
if (sqlFilePath == nullptr)
{
throw gcnew Exception("The input sqlFilePath must not be null.");
}
OpenStudio::Path^ osSqlFilePath = OpenStudio::OpenStudioUtilitiesCore::toPath(sqlFilePath);
OpenStudio::SqlFile^ osSqlFile = gcnew OpenStudio::SqlFile(osSqlFilePath);
if (osSqlFile == nullptr)
{
throw gcnew Exception("Fails to create an SQL output file");
}
bool isSuccessful = osModel->setSqlFile(osSqlFile);
if (!isSuccessful)
{
throw gcnew Exception("Fails to create an SQL output file");
}
return osSqlFile;
}
Topologic::Face ^ EnergyModel::FaceByOsSurface(OpenStudio::PlanarSurface^ osPlanarSurface)
{
OpenStudio::Point3dVector^ osVertices = osPlanarSurface->vertices();
OpenStudio::Point3dVector::Point3dVectorEnumerator^ osPointEnumerator = osVertices->GetEnumerator();
List<Topologic::Vertex^>^ vertices = gcnew List<Topologic::Vertex^>();
List<int>^ indices = gcnew List<int>();
int index = 0;
while (osPointEnumerator->MoveNext())
{
OpenStudio::Point3d^ osVertex = osPointEnumerator->Current;
vertices->Add(Topologic::Vertex::ByCoordinates(osVertex->x(), osVertex->y(), osVertex->z()));
indices->Add(index);
++index;
}
if (vertices->Count < 3)
{
throw gcnew Exception("Invalid surface is found.");
}
indices->Add(0); // close the wire
IList<IList<int>^>^ vertexIndices = gcnew List<IList<int>^>();
((IList<IList<int>^>^)vertexIndices)->Add(indices);
IList<Topologic::Topology^>^ topologies = (IList<Topologic::Topology^>^)Topologic::Topology::ByVerticesIndices(vertices, vertexIndices);
if (topologies == nullptr || topologies->Count == 0)
{
throw gcnew Exception("Error creating a topology from a surface.");
}
Topologic::Face^ face = nullptr;
try {
face = safe_cast<Topologic::Face^>(topologies[0]);
}
catch (Exception^)
{
throw gcnew Exception("Error converting a topology to a face.");
}
return face;
}
double EnergyModel::DoubleValueFromQuery(OpenStudio::SqlFile^ sqlFile, String^ EPReportName, String^ EPReportForString, String^ EPTableName, String^ EPColumnName, String^ EPRowName, String^ EPUnits)
{
double doubleValue = 0.0;
String^ query = "SELECT Value FROM tabulardatawithstrings WHERE ReportName='" + EPReportName + "' AND ReportForString='" + EPReportForString + "' AND TableName = '" + EPTableName + "' AND RowName = '" + EPRowName + "' AND ColumnName= '" + EPColumnName + "' AND Units='" + EPUnits + "'";
OpenStudio::OptionalDouble^ osDoubleValue = sqlFile->execAndReturnFirstDouble(query);
if (osDoubleValue->is_initialized())
{
doubleValue = osDoubleValue->get();
}
else
{
throw gcnew Exception("Fails to get a double value from the SQL file.");
}
return doubleValue;
}
String^ EnergyModel::StringValueFromQuery(OpenStudio::SqlFile^ sqlFile, String^ EPReportName, String^ EPReportForString, String^ EPTableName, String^ EPColumnName, String^ EPRowName, String^ EPUnits)
{
String^ query = "SELECT Value FROM tabulardatawithstrings WHERE ReportName='" + EPReportName + "' AND ReportForString='" + EPReportForString + "' AND TableName = '" + EPTableName + "' AND RowName = '" + EPRowName + "' AND ColumnName= '" + EPColumnName + "' AND Units='" + EPUnits + "'";
return sqlFile->execAndReturnFirstString(query)->get();
}
int EnergyModel::IntValueFromQuery(OpenStudio::SqlFile^ sqlFile, String^ EPReportName, String^ EPReportForString, String^ EPTableName, String^ EPColumnName, String^ EPRowName, String^ EPUnits)
{
String^ query = "SELECT Value FROM tabulardatawithstrings WHERE ReportName='" + EPReportName + "' AND ReportForString='" + EPReportForString + "' AND TableName = '" + EPTableName + "' AND RowName = '" + EPRowName + "' AND ColumnName= '" + EPColumnName + "' AND Units='" + EPUnits + "'";
return sqlFile->execAndReturnFirstInt(query)->get();
}
bool EnergyModel::Export(EnergyModel ^ energyModel, String ^ openStudioOutputDirectory, String ^% oswPath)
{
// Add timestamp to the output file name
String^ openStudioOutputTimeStampPath = Path::GetDirectoryName(openStudioOutputDirectory + "\\") + "\\" +
Path::GetFileNameWithoutExtension(energyModel->BuildingName) +
".osm";
// Save model to an OSM file
bool saveCondition = SaveModel(energyModel->m_osModel, openStudioOutputTimeStampPath);
if (!saveCondition)
{
return false;
}
OpenStudio::WorkflowJSON^ workflow = gcnew OpenStudio::WorkflowJSON();
try {
String^ osmFilename = Path::GetFileNameWithoutExtension(openStudioOutputTimeStampPath);
String^ osmDirectory = Path::GetDirectoryName(openStudioOutputTimeStampPath);
oswPath = osmDirectory + "\\" + osmFilename + ".osw";
OpenStudio::Path^ osOswPath = OpenStudio::OpenStudioUtilitiesCore::toPath(oswPath);
workflow->setSeedFile(OpenStudio::OpenStudioUtilitiesCore::toPath(openStudioOutputTimeStampPath));
workflow->setWeatherFile(gcnew OpenStudio::Path());
workflow->saveAs(osOswPath);
return true;
}
catch (...)
{
return false;
}
}
OpenStudio::DefaultScheduleSet^ EnergyModel::getDefaultScheduleSet(OpenStudio::Model^ model)
{
// Get list of default schedule sets
OpenStudio::DefaultScheduleSetVector^ defaultScheduleSets = model->getDefaultScheduleSets();
OpenStudio::DefaultScheduleSetVector::DefaultScheduleSetVectorEnumerator^ defSchedEnum = defaultScheduleSets->GetEnumerator();
defSchedEnum->MoveNext();
defaultScheduleSet = defSchedEnum->Current;
return defaultScheduleSet;
}
OpenStudio::DefaultConstructionSet^ EnergyModel::getDefaultConstructionSet(OpenStudio::Model ^ model)
{
// Get list of default construction sets
OpenStudio::DefaultConstructionSetVector^ defaultConstructionSets = model->getDefaultConstructionSets();
// Get the first item and use as the default construction set
OpenStudio::DefaultConstructionSetVector::DefaultConstructionSetVectorEnumerator^ defConEnum = defaultConstructionSets->GetEnumerator();
defConEnum->MoveNext();
defaultConstructionSet = defConEnum->Current;
return defaultConstructionSet;
}
OpenStudio::Space^ EnergyModel::AddSpace(
int spaceNumber,
Cell^ cell,
CellComplex^ cellComplex,
OpenStudio::Model^ osModel,
Autodesk::DesignScript::Geometry::Vector^ upVector,
double buildingHeight,
IList<double>^ floorLevels,
Nullable<double> glazingRatio,
double heatingTemp,
double coolingTemp)
{
OpenStudio::Space^ osSpace = gcnew OpenStudio::Space(osModel);
int storyNumber = StoryNumber(cell, buildingHeight, floorLevels);
OpenStudio::BuildingStory^ buildingStory = ((IList< OpenStudio::BuildingStory^>^)buildingStories)[storyNumber];
osSpace->setName(buildingStory->name()->get() + "_SPACE_" + spaceNumber.ToString());
osSpace->setBuildingStory(buildingStory);
osSpace->setDefaultConstructionSet(getDefaultConstructionSet(osModel));
osSpace->setDefaultScheduleSet(getDefaultScheduleSet(osModel));
IList<Face^>^ faces = (IList<Face^>^)cell->Faces;
List<OpenStudio::Point3dVector^>^ facePointsList = gcnew List<OpenStudio::Point3dVector^>();
for each(Face^ face in faces)
{
OpenStudio::Point3dVector^ facePoints = GetFacePoints(face);
facePointsList->Add(facePoints);
}
for (int i = 0; i < faces->Count; ++i)
{
AddSurface(i + 1, faces[i], cell, cellComplex, facePointsList[i], osSpace, osModel, upVector, glazingRatio);
}
// Get all space types
OpenStudio::SpaceTypeVector^ osSpaceTypes = osModel->getSpaceTypes();
OpenStudio::SpaceTypeVector::SpaceTypeVectorEnumerator^ osSpaceTypesEnumerator = osSpaceTypes->GetEnumerator();
int spaceTypeCount = osSpaceTypes->Count;
while (osSpaceTypesEnumerator->MoveNext())
{
OpenStudio::SpaceType^ osSpaceType = osSpaceTypesEnumerator->Current;
OpenStudio::OptionalString^ osSpaceTypeOptionalString = osSpaceType->name();
String^ spaceTypeName = osSpaceTypeOptionalString->__str__();
if (spaceTypeName == "ASHRAE 189::1-2009 ClimateZone 4-8 MediumOffice")
{
osSpace->setSpaceType(osSpaceType);
}
}
IList<double>^ minMax = (IList<double>^)Topologic::Utilities::CellUtility::GetMinMax(cell);
double minZ = minMax[4];
double maxZ = minMax[5];
double ceilingHeight = Math::Abs(maxZ - minZ);
OpenStudio::ThermalZone^ thermalZone = CreateThermalZone(osModel, osSpace, ceilingHeight, heatingTemp, coolingTemp);
return osSpace;
}
void EnergyModel::AddShadingSurfaces(Cell^ buildingCell, OpenStudio::Model^ osModel)
{
OpenStudio::ShadingSurfaceGroup^ osShadingGroup = gcnew OpenStudio::ShadingSurfaceGroup(osModel);
IList<Face^>^ faceList = buildingCell->Faces;
int faceIndex = 1;
for each(Face^ face in faceList)
{
IList<Vertex^>^ vertices = face->Vertices;
OpenStudio::Point3dVector^ facePoints = gcnew OpenStudio::Point3dVector();
for each(Vertex^ aVertex in vertices)
{
OpenStudio::Point3d^ aPoint = gcnew OpenStudio::Point3d(aVertex->X, aVertex->Y, aVertex->Z);
facePoints->Add(aPoint);
}
OpenStudio::ShadingSurface^ aShadingSurface = gcnew OpenStudio::ShadingSurface(facePoints, osModel);
String^ surfaceName = buildingCell->ToString() + "_SHADINGSURFACE_" + (faceIndex.ToString());
aShadingSurface->setName(surfaceName);
aShadingSurface->setShadingSurfaceGroup(osShadingGroup);
++faceIndex;
}
}
void EnergyModel::AddShadingSurfaces(Face ^ buildingFace, OpenStudio::Model ^ osModel, OpenStudio::ShadingSurfaceGroup^ osShadingGroup, int faceIndex)
{
IList<Vertex^>^ vertices = buildingFace->Vertices;
OpenStudio::Point3dVector^ facePoints = gcnew OpenStudio::Point3dVector();
for each(Vertex^ aVertex in vertices)
{
OpenStudio::Point3d^ aPoint = gcnew OpenStudio::Point3d(aVertex->X, aVertex->Y, aVertex->Z);
facePoints->Add(aPoint);
}
OpenStudio::ShadingSurface^ aShadingSurface = gcnew OpenStudio::ShadingSurface(facePoints, osModel);
String^ surfaceName = "SHADINGSURFACE_" + (faceIndex.ToString());
aShadingSurface->setName(surfaceName);
aShadingSurface->setShadingSurfaceGroup(osShadingGroup);
}
OpenStudio::Surface^ EnergyModel::AddSurface(
int surfaceNumber,
Face^ buildingFace,
Cell^ buildingSpace,
CellComplex^ cellComplex,
OpenStudio::Point3dVector^ osFacePoints,
OpenStudio::Space^ osSpace,
OpenStudio::Model^ osModel,
Autodesk::DesignScript::Geometry::Vector^ upVector,
[Autodesk::DesignScript::Runtime::DefaultArgument("null")] Nullable<double> glazingRatio)
{
OpenStudio::Construction^ osInteriorCeilingType = nullptr;
OpenStudio::Construction^ osExteriorRoofType = nullptr;
OpenStudio::Construction^ osInteriorFloorType = nullptr;
OpenStudio::Construction^ osInteriorWallType = nullptr;
OpenStudio::Construction^ osExteriorDoorType = nullptr;
OpenStudio::Construction^ osExteriorWallType = nullptr;
OpenStudio::Construction^ osExteriorWindowType = nullptr;
int subsurfaceCounter = 1;
OpenStudio::ConstructionVector^ osConstructionTypes = osModel->getConstructions();
OpenStudio::ConstructionVector::ConstructionVectorEnumerator^ osConstructionTypesEnumerator =
osConstructionTypes->GetEnumerator();
int constructionTypeCount = osConstructionTypes->Count;
while (osConstructionTypesEnumerator->MoveNext())
{
OpenStudio::Construction^ osConstruction = osConstructionTypesEnumerator->Current;
OpenStudio::OptionalString^ osConstructionTypeOptionalString = osConstruction->name();
String^ constructionTypeName = osConstructionTypeOptionalString->__str__();
if (constructionTypeName->Equals("000 Interior Ceiling"))
{
osInteriorCeilingType = osConstruction;
}
else if (constructionTypeName->Equals("000 Interior Floor"))
{
osInteriorFloorType = osConstruction;
}
else if (constructionTypeName->Equals("000 Interior Wall"))
{
osInteriorWallType = osConstruction;
}
else if (constructionTypeName->Equals("ASHRAE 189.1-2009 ExtWindow ClimateZone 4-5"))
{
osExteriorWindowType = osConstruction;
}
else if (constructionTypeName->Equals("000 Exterior Door"))
{
osExteriorDoorType = osConstruction;
}
else if (constructionTypeName->Equals("ASHRAE 189.1-2009 ExtRoof IEAD ClimateZone 2-5"))
{
osExteriorRoofType = osConstruction;
}
else if (constructionTypeName->Equals("ASHRAE 189.1-2009 ExtWall SteelFrame ClimateZone 4-8"))
{
osExteriorWallType = osConstruction;
}
} // while (osConstructionTypesEnumerator.MoveNext())
int adjCount = AdjacentCellCount(buildingFace);
//HACK
/*if (adjCount > 1)
{
osFacePoints->Reverse();
}*/
OpenStudio::Surface^ osSurface = gcnew OpenStudio::Surface(osFacePoints, osModel);
osSurface->setSpace(osSpace);
OpenStudio::OptionalString^ osSpaceOptionalString = osSpace->name();
String^ spaceName = osSpace->name()->get();
String^ surfaceName = osSpace->name()->get() + "_SURFACE_" + surfaceNumber.ToString();
bool isUnderground = IsUnderground(buildingFace);
FaceType faceType = CalculateFaceType(buildingFace, osFacePoints, buildingSpace, upVector);
osSurface->setName(surfaceName);
if ((faceType == FACE_ROOFCEILING) && (adjCount > 1))
{
osSurface->setOutsideBoundaryCondition("Surface");
osSurface->setSurfaceType("RoofCeiling");
osSurface->setConstruction(osInteriorCeilingType);
osSurface->setSunExposure("NoSun");
osSurface->setWindExposure("NoWind");
}
else if ((faceType == FACE_ROOFCEILING) && (adjCount < 2) && (!isUnderground))
{
OpenStudio::Vector3d^ pSurfaceNormal = osSurface->outwardNormal();
double x = pSurfaceNormal->x();
double y = pSurfaceNormal->y();
double z = pSurfaceNormal->z();
pSurfaceNormal->normalize();
OpenStudio::Vector3d^ upVector = gcnew OpenStudio::Vector3d(0, 0, 1.0);
// If the normal does not point downward, flip it. Use dot product.
double dotProduct = pSurfaceNormal->dot(upVector);
if (dotProduct < 0.98)
{
OpenStudio::Point3dVector^ surfaceVertices = osSurface->vertices();
surfaceVertices->Reverse();
osSurface->setVertices(surfaceVertices);
}
osSurface->setOutsideBoundaryCondition("Outdoors");
osSurface->setSurfaceType("RoofCeiling");
osSurface->setConstruction(osExteriorRoofType);
osSurface->setSunExposure("SunExposed");
osSurface->setWindExposure("WindExposed");
}
else if ((faceType == FACE_ROOFCEILING) && (adjCount < 2) && isUnderground)
{
OpenStudio::Vector3d^ pSurfaceNormal = osSurface->outwardNormal();
double x = pSurfaceNormal->x();
double y = pSurfaceNormal->y();
double z = pSurfaceNormal->z();
pSurfaceNormal->normalize();
OpenStudio::Vector3d^ upVector = gcnew OpenStudio::Vector3d(0, 0, 1.0);
// If the normal does not point downward, flip it. Use dot product.
double dotProduct = pSurfaceNormal->dot(upVector);
if (dotProduct < 0.98)
{
OpenStudio::Point3dVector^ surfaceVertices = osSurface->vertices();
surfaceVertices->Reverse();
osSurface->setVertices(surfaceVertices);
}
osSurface->setOutsideBoundaryCondition("Ground");
osSurface->setSurfaceType("RoofCeiling");
osSurface->setConstruction(osExteriorRoofType);
osSurface->setSunExposure("NoSun");
osSurface->setWindExposure("NoWind");
}
else if ((faceType == FACE_FLOOR) && (adjCount > 1))
{
osSurface->setOutsideBoundaryCondition("Surface");
osSurface->setSurfaceType("Floor");
osSurface->setConstruction(osInteriorFloorType);
osSurface->setSunExposure("NoSun");
osSurface->setWindExposure("NoWind");
}
else if ((faceType == FACE_FLOOR) && (adjCount < 2))
{
OpenStudio::Vector3d^ pSurfaceNormal = osSurface->outwardNormal();
double x = pSurfaceNormal->x();
double y = pSurfaceNormal->y();
double z = pSurfaceNormal->z();
pSurfaceNormal->normalize();
OpenStudio::Vector3d^ downwardVector = gcnew OpenStudio::Vector3d(0, 0, -1.0);
// If the normal does not point downward, flip it. Use dot product.
double dotProduct = pSurfaceNormal->dot(downwardVector);
if (dotProduct < 0.98)
{
OpenStudio::Point3dVector^ surfaceVertices = osSurface->vertices();
surfaceVertices->Reverse();
osSurface->setVertices(surfaceVertices);
}
osSurface->setOutsideBoundaryCondition("Ground");
osSurface->setSurfaceType("Floor");
osSurface->setConstruction(osExteriorWallType);
osSurface->setSunExposure("NoSun");
osSurface->setWindExposure("NoWind");
}
else if ((faceType == FACE_WALL) && (adjCount > 1)) // internal wall
{
osSurface->setOutsideBoundaryCondition("Surface");
osSurface->setSurfaceType("Wall");
osSurface->setConstruction(osInteriorWallType);
osSurface->setSunExposure("NoSun");
osSurface->setWindExposure("NoWind");
}
else if ((faceType == FACE_WALL) && (adjCount < 2) && isUnderground) // external wall underground
{
osSurface->setOutsideBoundaryCondition("Ground");
osSurface->setSurfaceType("Wall");
osSurface->setConstruction(osExteriorWallType);
osSurface->setSunExposure("NoSun");
osSurface->setWindExposure("NoWind");
}
else if ((faceType == FACE_WALL) && (adjCount < 2) && (!isUnderground)) // external wall overground
{
osSurface->setOutsideBoundaryCondition("Outdoors");
osSurface->setSurfaceType("Wall");
osSurface->setConstruction(osExteriorWallType);
osSurface->setSunExposure("SunExposed");
osSurface->setWindExposure("WindExposed");
if (glazingRatio.HasValue)
{
if (glazingRatio.Value < 0.0 || glazingRatio.Value > 1.0)
{
throw gcnew Exception("The glazing ratio must be between 0.0 and 1.0 (both inclusive).");
}
else if (glazingRatio.Value > 0.0 && glazingRatio.Value <= 1.0)
{
// Triangulate the Windows
IList<Vertex^>^ scaledVertices = (IList<Vertex^>^)ScaleFaceVertices(buildingFace, glazingRatio.Value);
for (int i = 0; i < scaledVertices->Count - 2; ++i)
{
List<Vertex^>^ triangleVertices = gcnew List<Vertex^>();
triangleVertices->Add(scaledVertices[0]);
triangleVertices->Add(scaledVertices[i + 1]);
triangleVertices->Add(scaledVertices[i + 2]);
IList<Vertex^>^ scaledTriangleVertices = ScaleVertices(triangleVertices, 0.999);
OpenStudio::Point3dVector^ osWindowFacePoints = gcnew OpenStudio::Point3dVector();
for each (Vertex^ scaledTriangleVertex in scaledTriangleVertices)
{
OpenStudio::Point3d^ osPoint = gcnew OpenStudio::Point3d(
scaledTriangleVertex->X,
scaledTriangleVertex->Y,
scaledTriangleVertex->Z);
osWindowFacePoints->Add(osPoint);
}
OpenStudio::SubSurface^ osWindowSubSurface = gcnew OpenStudio::SubSurface(osWindowFacePoints, osModel);
double dotProduct = osWindowSubSurface->outwardNormal()->dot(osSurface->outwardNormal());
if (dotProduct < -0.99) // flipped
{
osWindowFacePoints->Reverse();
osWindowSubSurface->remove();
osWindowSubSurface = gcnew OpenStudio::SubSurface(osWindowFacePoints, osModel); //CreateSubSurface(pApertureVertices, osModel);
}
else if (dotProduct > -0.99 && dotProduct < 0.99)
{
throw gcnew Exception("There is a non-coplanar subsurface.");
}
osWindowSubSurface->setSubSurfaceType("FixedWindow");
osWindowSubSurface->setSurface(osSurface);
osWindowSubSurface->setName(osSurface->name()->get() + "_SUBSURFACE_" + subsurfaceCounter.ToString());
subsurfaceCounter++;
} // for (int i = 0; i < scaledVertices->Count - 2; ++i)
}
}
else // glazingRatio is null
{
// Use the surface apertures
IList<Topologic::Topology^>^ pContents = buildingFace->Contents;
for each(Topologic::Topology^ pContent in pContents)
{
Aperture^ pAperture = dynamic_cast<Aperture^>(pContent);
if (pAperture == nullptr)
{
continue;
}
Face^ pFaceAperture = dynamic_cast<Face^>(pAperture->Topology);
if (pAperture == nullptr)
{
continue;
}
// skip small triangles
double area = Topologic::Utilities::FaceUtility::Area(pFaceAperture);
if (area <= 0.1)
{
continue;
}
Wire^ pApertureWire = pFaceAperture->ExternalBoundary;
List<Vertex^>^ pApertureVertices = (List<Vertex^>^)pApertureWire->Vertices;
//OpenStudio::SubSurface^ osWindowSubSurface = gcnew OpenStudio::SubSurface(osWindowFacePoints, osModel);
OpenStudio::SubSurface^ osWindowSubSurface = CreateSubSurface(pApertureVertices, osModel);
double dotProduct = osWindowSubSurface->outwardNormal()->dot(osSurface->outwardNormal());
if (dotProduct < -0.99) // flipped
{
pApertureVertices->Reverse();
osWindowSubSurface->remove();
osWindowSubSurface = CreateSubSurface(pApertureVertices, osModel);
}
else if (dotProduct > -0.99 && dotProduct < 0.99)
{
throw gcnew Exception("There is a non-coplanar subsurface.");
}
numOfApertures++;
double grossSubsurfaceArea = osWindowSubSurface->grossArea();
double netSubsurfaceArea = osWindowSubSurface->netArea();
double grossSurfaceArea = osSurface->grossArea();
double netSurfaceArea = osSurface->netArea();
if (grossSubsurfaceArea > 0.1)
{
osWindowSubSurface->setSubSurfaceType("FixedWindow");