Skip to content

Commit

Permalink
Removed auto-constructors for finicky objects (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd authored Nov 14, 2023
2 parents de50c9c + b5bc6dc commit af85473
Show file tree
Hide file tree
Showing 22 changed files with 846 additions and 253 deletions.
2 changes: 1 addition & 1 deletion LadybugTools_Engine/Compute/EPWtoCSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static string EPWtoCSV(string epwFile, bool includeAdditional = false)
{
if (epwFile == null)
{
BH.Engine.Base.Compute.RecordError("epwFile input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(epwFile)} input cannot be null.");
return null;
}

Expand Down
87 changes: 2 additions & 85 deletions LadybugTools_Engine/Compute/ExternalComfort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@

using System.ComponentModel;
using System.IO;
using System.Linq;
using System;

using BH.Engine.Geometry;
using BH.Engine.Serialiser;
using BH.oM.Base.Attributes;
using BH.oM.LadybugTools;
Expand All @@ -43,97 +41,16 @@ public static ExternalComfort ExternalComfort(SimulationResult simulationResult,
{
if (simulationResult == null)
{
BH.Engine.Base.Compute.RecordError("simulationResult input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(simulationResult)} input cannot be null.");
return null;
}

if (typology == null)
{
BH.Engine.Base.Compute.RecordError("typology input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(typology)} input cannot be null.");
return null;
}

if (typology.EvaporativeCoolingEffect.Count() != 8760)
{
BH.Engine.Base.Compute.RecordError("Typology evaporative cooling effect must be a list of values 8760 long.");
return null;
}
if(typology.EvaporativeCoolingEffect.Where(x => x < 0 || x > 1).Any())
{
BH.Engine.Base.Compute.RecordError("All Evaporative Cooling Effect values must between 0 and 1.");
return null;
}

if (typology.RadiantTemperatureAdjustment.Count() != 8760)
{
BH.Engine.Base.Compute.RecordError("Typology radiant temperature adjustment must be a list of values 8760 long.");
return null;
}

if (typology.TargetWindSpeed.Count() != 8760)
{
BH.Engine.Base.Compute.RecordError("Typology target wind speed must be a list of values 8760 long.");
return null;
}
if(typology.TargetWindSpeed.Where(x => x != null && x.Value < 0).Any())
{
BH.Engine.Base.Compute.RecordError("Typology Target Wind Speed values must be greater than or equal to 0, or null if not relevant for that hour of the year.");
return null;
}

foreach (Shelter shelter in typology.Shelters)
{
if (!BH.Engine.Geometry.Create.Polyline(shelter.Vertices).IsPlanar())
{
BH.Engine.Base.Compute.RecordError("A shelter in this Typology is not planar.");
return null;
}
if (shelter.WindPorosity.Count() != 8760)
{
BH.Engine.Base.Compute.RecordError("Shelter wind porosity must be a list of values 8760 long.");
return null;
}
if(shelter.WindPorosity.Where(x => x < 0 || x > 1).Any())
{
BH.Engine.Base.Compute.RecordError($"Shelter wind porosity must be between 0 and 1 for the Shelter with GUID {shelter.BHoM_Guid.ToString()}.");
return null;
}
if (shelter.RadiationPorosity.Count() != 8760)
{
BH.Engine.Base.Compute.RecordError("Shelter radiation porosity must be a list of values 8760 long.");
return null;
}
if(shelter.RadiationPorosity.Where(x => x < 0 || x > 1).Any())
{
BH.Engine.Base.Compute.RecordError($"Shelter Radiation Porosity must be between 0 and 1 for shelter with GUID {shelter.BHoM_Guid.ToString()}.");
return null;
}
}

if (typology.Identifier == "")
{
dynamic wsAvg;
if (typology.TargetWindSpeed.Where(x => x.HasValue).Count() == 0)
{
wsAvg = "EPW";
}
else
{
wsAvg = typology.TargetWindSpeed.Where(x => x.HasValue).Average(x => x.Value);
}

if (typology.Shelters.Count() == 0)
{
typology.Identifier = $"ec{typology.EvaporativeCoolingEffect.Average()}_ws{wsAvg}_mrt{typology.RadiantTemperatureAdjustment.Average()}";
}
else
{
typology.Identifier = $"shelters{typology.Shelters.Count()}_ec{typology.EvaporativeCoolingEffect.Average()}_ws{wsAvg}_mrt{typology.RadiantTemperatureAdjustment.Average()}";
}

Base.Compute.RecordNote($"This typology has been automatically named \"{typology.Identifier}\". This can be overriden with the 'identifier' parameter of Typology.");
}

// construct the base object
ExternalComfort externalComfort = new ExternalComfort()
{
Expand Down
2 changes: 1 addition & 1 deletion LadybugTools_Engine/Compute/GEMtoHBJSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static string GEMtoHBJSON(string gem)
{
if (gem == null)
{
BH.Engine.Base.Compute.RecordError("gem input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(gem)} input cannot be null.");
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion LadybugTools_Engine/Compute/HBJSONtoGEM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static string HBJSONtoGEM(string hbjson)
{
if (hbjson == null)
{
BH.Engine.Base.Compute.RecordError("hbjson input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(hbjson)} input cannot be null.");
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions LadybugTools_Engine/Compute/SimulationId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public static string SimulationID(string epwFile, IEnergyMaterialOpaque groundMa
{
if (epwFile == null)
{
BH.Engine.Base.Compute.RecordError("epwFile input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(epwFile)} input cannot be null.");
return null;
}

if (groundMaterial == null)
{
BH.Engine.Base.Compute.RecordError("groundMaterial input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(groundMaterial)} input cannot be null.");
return null;
}

if (shadeMaterial == null)
{
BH.Engine.Base.Compute.RecordError("shadeMaterial input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(shadeMaterial)} input cannot be null.");
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions LadybugTools_Engine/Compute/SimulationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static SimulationResult SimulationResult(string epwFile, IEnergyMaterialO
// validation prior to passing to Python
if (epwFile == null)
{
BH.Engine.Base.Compute.RecordError("epwFile input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(epwFile)} input cannot be null.");
return null;
}
if (!File.Exists(epwFile))
Expand All @@ -55,13 +55,13 @@ public static SimulationResult SimulationResult(string epwFile, IEnergyMaterialO

if (groundMaterial == null)
{
BH.Engine.Base.Compute.RecordError("groundMaterial input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(groundMaterial)} input cannot be null.");
return null;
}

if (shadeMaterial == null)
{
BH.Engine.Base.Compute.RecordError("shadeMaterial input cannot be null.");
BH.Engine.Base.Compute.RecordError($"{nameof(shadeMaterial)} input cannot be null.");
return null;
}

Expand Down
37 changes: 2 additions & 35 deletions LadybugTools_Engine/Convert/ToShelter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static Shelter ToShelter(this Panel panel, List<double> radiationPorosity
{
if (panel == null)
{
Base.Compute.RecordError("Panel is null. Shelter cannot be created.");
Base.Compute.RecordError($"{nameof(panel)} is null. Shelter cannot be created.");
return null;
}

Expand All @@ -58,43 +58,10 @@ public static Shelter ToShelter(this Panel panel, List<double> radiationPorosity
windPorosity = Enumerable.Repeat(0.0, 8760).ToList();
}

if (radiationPorosity.Count != 8760)
{
Base.Compute.RecordError("Radiation porosity list must be 8760 long.");
return null;
}
if (windPorosity.Count != 8760)
{
Base.Compute.RecordError("Wind porosity list must be 8760 long.");
return null;
}

if(radiationPorosity.Where(x => x < 0 || x > 1).Any())
{
BH.Engine.Base.Compute.RecordError("All Radiation Porosity values must be between 0-1 (inclusive).");
return null;
}
if(windPorosity.Where(x => x < 0 || x > 1).Any())
{
BH.Engine.Base.Compute.RecordError("All Wind Porosity values must be between 0-1 (inclusive).");
return null;
}

if (windPorosity.Sum() + radiationPorosity.Sum() == 0)
{
Base.Compute.RecordError("This Shelter will have no effect as it is completely transmissive.");
return null;
}

List<Point> vertices = panel.Vertices().ToList();
vertices.RemoveAt(vertices.Count - 1); // python Shelter object doesn't want a closed polyline

return new Shelter()
{
Vertices = vertices,
WindPorosity = windPorosity,
RadiationPorosity = radiationPorosity
};
return BH.Engine.LadybugTools.Create.Shelter(vertices: vertices, windPorosity: windPorosity, radiationPorosity: radiationPorosity);
}
}
}
82 changes: 82 additions & 0 deletions LadybugTools_Engine/Create/AnalysisPeriod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.Attributes;
using BH.oM.LadybugTools;
using System.Collections.Generic;
using System.ComponentModel;

namespace BH.Engine.LadybugTools
{
public static partial class Create
{
[Description("Create an AnalysisPeriod object.")]
[Input("startMonth", "The starting month of the analysis period. 1-12.")]
[Input("startDay", "The starting day of the analysis period. 1-31.")]
[Input("startHour", "The starting hour of the analysis period. 0-23.")]
[Input("endMonth", "The ending month of the analysis period. 1-12.")]
[Input("endDay", "The ending day of the analysis period. 1-31.")]
[Input("endHour", "The ending hour of the analysis period. 0-23.")]
[Input("isLeapYear", "Whether the analysis period represents a leap year.")]
[Input("timestep", "The number of timesteps per hour. One of {1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60}.")]
[Output("analysisPeriod", "An AnalysisPeriod object.")]
public static AnalysisPeriod AnalysisPeriod(int startMonth = 1, int startDay = 1, int startHour = 0, int endMonth = 12, int endDay = 31, int endHour = 23, bool isLeapYear = false, int timestep = 1)
{
if (startMonth < 1 || startMonth > 12 || endMonth < 1 || endMonth > 12)
{
BH.Engine.Base.Compute.RecordError($"{nameof(startMonth)} and {nameof(endMonth)} must be between 1 and 12.");
return null;
}

if (startDay < 1 || startDay > 31 || endDay < 1 || endDay > 31)
{
BH.Engine.Base.Compute.RecordError($"{nameof(startDay)} and {nameof(endDay)} must be between 1 and 31.");
return null;
}

if (startHour < 0 || startHour > 23 || endHour < 0 || endHour > 23)
{
BH.Engine.Base.Compute.RecordError($"{nameof(startHour)} and {nameof(endHour)} must be between 0 and 23.");
return null;
}

List<int> allowedTimesteps = new List<int>() { 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60 };
if (!allowedTimesteps.Contains(timestep))
{
BH.Engine.Base.Compute.RecordError($"{nameof(timestep)} must be one of {string.Join(", ", allowedTimesteps)}.");
return null;
}

return new AnalysisPeriod()
{
StMonth = startMonth,
StDay = startDay,
StHour = startHour,
EndMonth = endMonth,
EndDay = endDay,
EndHour = endHour,
IsLeapYear = isLeapYear,
Timestep = timestep
};
}
}
}
Loading

0 comments on commit af85473

Please sign in to comment.