generated from BHoM/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapter: added remaining object serialisation (#152)
- Loading branch information
Showing
19 changed files
with
748 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* This file is part of the Buildings and Habitats object Model (BHoM) | ||
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. | ||
* | ||
* Each contributor holds copyright over their respective contributions. | ||
* The project versioning (Git) records all such contribution source information. | ||
* | ||
* | ||
* The BHoM is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3.0 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The BHoM 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using BH.oM.LadybugTools; | ||
using BH.oM.Geometry; | ||
|
||
namespace BH.Adapter.LadybugTools | ||
{ | ||
public static partial class Convert | ||
{ | ||
public static Point ToPoint(Dictionary<string, object> oldObject) | ||
{ | ||
double x = 0.0; | ||
double y = 0.0; | ||
double z = 0.0; | ||
|
||
try | ||
{ | ||
x = (double)oldObject["x"]; | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading x of the Point. returning x as default ({x}).\n The error: {ex}"); | ||
} | ||
|
||
try | ||
{ | ||
y = (double)oldObject["y"]; | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading y of the Point. returning y as default ({y}).\n The error: {ex}"); | ||
} | ||
|
||
try | ||
{ | ||
z = (double)oldObject["z"]; | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading z of the Point. returning z as default ({z}).\n The error: {ex}"); | ||
} | ||
|
||
return new Point() | ||
{ | ||
X = x, | ||
Y = y, | ||
Z = z | ||
}; | ||
} | ||
|
||
public static string FromPoint(Point point) | ||
{ | ||
string type = @"""Point3D"""; | ||
string xyz = $@"""x"" : {point.X}, ""y"" : {point.Y}, ""z"" : {point.Z}"; | ||
return @"{""type"" : " + type + ", " + xyz + "}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* This file is part of the Buildings and Habitats object Model (BHoM) | ||
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. | ||
* | ||
* Each contributor holds copyright over their respective contributions. | ||
* The project versioning (Git) records all such contribution source information. | ||
* | ||
* | ||
* The BHoM is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3.0 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The BHoM 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using BH.Engine.Serialiser; | ||
using BH.oM.Base; | ||
using BH.oM.Geometry; | ||
using BH.oM.LadybugTools; | ||
|
||
namespace BH.Adapter.LadybugTools | ||
{ | ||
public static partial class Convert | ||
{ | ||
public static oM.LadybugTools.Shelter ToShelter(Dictionary<string, object> oldObject) | ||
{ | ||
List<Point> points = new List<Point>(); | ||
List<double> radiationPorosity = Enumerable.Repeat(0.0, 8760).ToList(); | ||
List<double> windPorosity = Enumerable.Repeat(0.0, 8760).ToList(); | ||
if (oldObject.ContainsKey("vertices")) | ||
{ | ||
foreach (var vertex in oldObject["vertices"] as List<object>) | ||
{ | ||
Dictionary<string, object> point; | ||
if (vertex.GetType() == typeof(CustomObject)) | ||
{ | ||
point = (vertex as CustomObject).CustomData; | ||
} | ||
else | ||
{ | ||
point = vertex as Dictionary<string, object>; | ||
} | ||
|
||
points.Add(ToPoint(point)); | ||
} | ||
} | ||
|
||
try | ||
{ | ||
List<double> values = new List<double>(); | ||
foreach (object value in oldObject["radiation_porosity"] as List<object>) | ||
{ | ||
values.Add(double.Parse(value.ToString())); | ||
} | ||
radiationPorosity = values; | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the radiation porosity of the Shelter. returning as default (List of 0s of length 8760).\n The error: {ex}"); | ||
} | ||
|
||
try | ||
{ | ||
List<double> values = new List<double>(); | ||
foreach (object value in oldObject["wind_porosity"] as List<object>) | ||
{ | ||
values.Add(double.Parse(value.ToString())); | ||
} | ||
windPorosity = values; | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the wind porosity of the Shelter. returning as default (List of 0s of length 8760).\n The error: {ex}"); | ||
} | ||
|
||
return new oM.LadybugTools.Shelter() | ||
{ | ||
Vertices = points, | ||
RadiationPorosity = radiationPorosity, | ||
WindPorosity = windPorosity | ||
|
||
}; | ||
} | ||
|
||
public static string FromShelter(oM.LadybugTools.Shelter shelter) | ||
{ | ||
string radiationPorosity = $@"""radiation_porosity"": [{string.Join(", ", shelter.RadiationPorosity)}]"; | ||
string windPorosity = $@"""wind_porosity"": [{string.Join(", ", shelter.WindPorosity)}]"; | ||
|
||
List<string> points = new List<string>(); | ||
foreach (Point point in shelter.Vertices) | ||
{ | ||
points.Add(FromPoint(point)); | ||
} | ||
string vertices = $@"""vertices"": [{string.Join(", ", points)}]"; | ||
|
||
return @"{ ""type"": ""Shelter""," + vertices + ", " + radiationPorosity + ", " + windPorosity + "}"; | ||
} | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
LadybugTools_Adapter/Convert/Simulation/ExternalComfort.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* This file is part of the Buildings and Habitats object Model (BHoM) | ||
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. | ||
* | ||
* Each contributor holds copyright over their respective contributions. | ||
* The project versioning (Git) records all such contribution source information. | ||
* | ||
* | ||
* The BHoM is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3.0 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The BHoM 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. | ||
*/ | ||
|
||
using BH.oM.Base; | ||
using BH.oM.LadybugTools; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace BH.Adapter.LadybugTools | ||
{ | ||
public static partial class Convert | ||
{ | ||
public static ExternalComfort ToExternalComfort(Dictionary<string, object> oldObject) | ||
{ | ||
SimulationResult simulationResult = new SimulationResult(); | ||
Typology typology = new Typology(); | ||
List<HourlyContinuousCollection> simulatedProperties = new List<HourlyContinuousCollection>(); | ||
|
||
List<string> properties = new List<string>() | ||
{ | ||
"dry_bulb_temperature", | ||
"relative_humidity", | ||
"wind_speed", | ||
"mean_radiant_temperature", | ||
"universal_thermal_climate_index" | ||
}; | ||
|
||
try | ||
{ | ||
if (oldObject["simulation_result"].GetType() == typeof(CustomObject)) | ||
oldObject["simulation_result"] = ((CustomObject)oldObject["simulation_result"]).CustomData; | ||
simulationResult = ToSimulationResult(oldObject["simulation_result"] as Dictionary<string, object>); | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the SimulationResult of the ExternalComfort. Returning a default SimulationResult. \n The error: {ex}"); | ||
} | ||
|
||
try | ||
{ | ||
if (oldObject["typology"].GetType() == typeof(CustomObject)) | ||
oldObject["typology"] = ((CustomObject)oldObject["typology"]).CustomData; | ||
typology = ToTypology(oldObject["typology"] as Dictionary<string, object>); | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the Typology of the ExternalComfort. Returning a default Typology. \n The error: {ex}"); | ||
} | ||
|
||
foreach (string property in properties) | ||
{ | ||
if (oldObject.ContainsKey(property)) | ||
{ | ||
try | ||
{ | ||
if (oldObject[property].GetType() == typeof(CustomObject)) | ||
oldObject[property] = ((CustomObject)oldObject[property]).CustomData; | ||
simulatedProperties.Add(ToHourlyContinuousCollection(oldObject[property] as Dictionary<string, object>)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"An error occurred while parsing the collection {property} of the ExternalComfort. Returning an empty collection in its place.\n The error: {ex}"); | ||
simulatedProperties.Add(new HourlyContinuousCollection() { Values = Enumerable.Repeat<string>(null, 8760).ToList() }); | ||
} | ||
} | ||
else | ||
{ | ||
BH.Engine.Base.Compute.RecordError($"The incoming json for ExternalComfort does not contain the key: {property}. Returning an empty collection in its place."); | ||
simulatedProperties.Add(new HourlyContinuousCollection() { Values = Enumerable.Repeat<string>(null, 8760).ToList() }); | ||
} | ||
} | ||
|
||
return new ExternalComfort() | ||
{ | ||
SimulationResult = simulationResult, | ||
Typology = typology, | ||
DryBulbTemperature = simulatedProperties[0], | ||
RelativeHumidity = simulatedProperties[1], | ||
WindSpeed = simulatedProperties[2], | ||
MeanRadiantTemperature = simulatedProperties[3], | ||
UniversalThermalClimateIndex = simulatedProperties[4] | ||
}; | ||
} | ||
|
||
public static string FromExternalComfort(ExternalComfort externalComfort) | ||
{ | ||
string type = "\"type\": \"ExternalComfort\", "; | ||
string simulationResult = $"\"simulation_result\": {FromSimulationResult(externalComfort.SimulationResult)}, "; | ||
string typology = $"\"typology\": {FromTypology(externalComfort.Typology)}"; | ||
List<string> properties = new List<string>(); | ||
|
||
if (externalComfort.DryBulbTemperature != null) | ||
properties.Add("\"dry_bulb_temperature\": " + FromHourlyContinuousCollection(externalComfort.DryBulbTemperature)); | ||
|
||
if (externalComfort.RelativeHumidity != null) | ||
properties.Add("\"relative_humidity\": " + FromHourlyContinuousCollection(externalComfort.RelativeHumidity)); | ||
|
||
if (externalComfort.WindSpeed != null) | ||
properties.Add("\"wind_speed\": " + FromHourlyContinuousCollection(externalComfort.WindSpeed)); | ||
|
||
if (externalComfort.MeanRadiantTemperature != null) | ||
properties.Add("\"mean_radiant_temperature\": " + FromHourlyContinuousCollection(externalComfort.MeanRadiantTemperature)); | ||
|
||
if (externalComfort.UniversalThermalClimateIndex != null) | ||
properties.Add("\"universal_thermal_climate_index\": " + FromHourlyContinuousCollection(externalComfort.UniversalThermalClimateIndex)); | ||
|
||
if (properties.Count > 0) | ||
properties[0] = ", " + properties[0]; | ||
string simulatedProperties = string.Join(", ", properties); | ||
return "{" + type + simulationResult + typology + simulatedProperties + "}"; | ||
} | ||
} | ||
} |
Oops, something went wrong.