|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using BH.Engine.Adapter; |
| 24 | +using BH.Engine.LadybugTools; |
| 25 | +using BH.oM.Adapter; |
| 26 | +using BH.oM.Adapter.Commands; |
| 27 | +using BH.oM.Base; |
| 28 | +using BH.oM.Data.Requests; |
| 29 | +using BH.oM.LadybugTools; |
| 30 | +using BH.oM.Python; |
| 31 | +using BH.Engine.Python; |
| 32 | +using System; |
| 33 | +using System.Collections.Generic; |
| 34 | +using System.IO; |
| 35 | +using System.Linq; |
| 36 | +using System.Text; |
| 37 | + |
| 38 | +namespace BH.Adapter.LadybugTools |
| 39 | +{ |
| 40 | + public partial class LadybugToolsAdapter : BHoMAdapter |
| 41 | + { |
| 42 | + bool m_executeSuccess = false; |
| 43 | + public override Output<List<object>, bool> Execute(IExecuteCommand command, ActionConfig actionConfig = null) |
| 44 | + { |
| 45 | + m_executeSuccess = false; |
| 46 | + Output<List<object>, bool> output = new Output<List<object>, bool>() { Item1 = new List<object>(), Item2 = false }; |
| 47 | + |
| 48 | + List<object> temp = IRunCommand(command); |
| 49 | + |
| 50 | + output.Item1 = temp; |
| 51 | + output.Item2 = m_executeSuccess; |
| 52 | + |
| 53 | + return output; |
| 54 | + } |
| 55 | + |
| 56 | + /**************************************************/ |
| 57 | + /* Public methods - Interface */ |
| 58 | + /**************************************************/ |
| 59 | + |
| 60 | + public List<object> IRunCommand(IExecuteCommand command) |
| 61 | + { |
| 62 | + if (command == null) |
| 63 | + { |
| 64 | + BH.Engine.Base.Compute.RecordError("Please input a valid Ladybug Command to execute."); |
| 65 | + return new List<object>(); |
| 66 | + } |
| 67 | + |
| 68 | + return RunCommand(command as dynamic); |
| 69 | + } |
| 70 | + |
| 71 | + /**************************************************/ |
| 72 | + /* Private methods - Run Ladybug Command */ |
| 73 | + /**************************************************/ |
| 74 | + |
| 75 | + private List<object> RunCommand(GetMaterialCommand command) |
| 76 | + { |
| 77 | + PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true); |
| 78 | + LadybugConfig config = new LadybugConfig() |
| 79 | + { |
| 80 | + JsonFile = new FileSettings() |
| 81 | + { |
| 82 | + FileName = $"LBTBHoM_Materials_{DateTime.Now:yyyyMMdd}.json", |
| 83 | + Directory = Path.GetTempPath() |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + if (!File.Exists(config.JsonFile.GetFullFileName())) |
| 88 | + { |
| 89 | + string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "get_material.py"); |
| 90 | + |
| 91 | + string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\""; |
| 92 | + |
| 93 | + Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true); |
| 94 | + } |
| 95 | + |
| 96 | + List<object> materialObjects = Pull(new FilterRequest(), actionConfig: config).ToList(); |
| 97 | + |
| 98 | + m_executeSuccess = true; |
| 99 | + return materialObjects.Where(m => (m as IEnergyMaterialOpaque).Name.Contains(command.Filter)).ToList(); |
| 100 | + } |
| 101 | + |
| 102 | + /**************************************************/ |
| 103 | + |
| 104 | + private List<object> RunCommand(GetTypologyCommand command) |
| 105 | + { |
| 106 | + PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true); |
| 107 | + LadybugConfig config = new LadybugConfig() |
| 108 | + { |
| 109 | + JsonFile = new FileSettings() |
| 110 | + { |
| 111 | + FileName = $"LBTBHoM_Typologies_{DateTime.Now:yyyyMMdd}.json", |
| 112 | + Directory = Path.GetTempPath() |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + if (!File.Exists(config.JsonFile.GetFullFileName())) |
| 117 | + { |
| 118 | + string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "get_typology.py"); |
| 119 | + |
| 120 | + string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\""; |
| 121 | + |
| 122 | + Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true); |
| 123 | + } |
| 124 | + |
| 125 | + List<object> typologyObjects = Pull(new FilterRequest(), actionConfig: config).ToList(); |
| 126 | + |
| 127 | + m_executeSuccess = true; |
| 128 | + return typologyObjects.Where(m => (m as Typology).Name.Contains(command.Filter)).ToList(); |
| 129 | + } |
| 130 | + |
| 131 | + /**************************************************/ |
| 132 | + |
| 133 | + private List<object> RunCommand(RunSimulationCommand command) |
| 134 | + { |
| 135 | + // validation prior to passing to Python |
| 136 | + if (command.EpwFile == null) |
| 137 | + { |
| 138 | + BH.Engine.Base.Compute.RecordError($"{nameof(command.EpwFile)} input cannot be null."); |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + if (!File.Exists(command.EpwFile.GetFullFileName())) |
| 143 | + { |
| 144 | + BH.Engine.Base.Compute.RecordError($"{command.EpwFile.GetFullFileName()} does not exist."); |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + if (command.GroundMaterial == null) |
| 149 | + { |
| 150 | + BH.Engine.Base.Compute.RecordError($"{nameof(command.GroundMaterial)} input cannot be null."); |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + if (command.ShadeMaterial == null) |
| 155 | + { |
| 156 | + BH.Engine.Base.Compute.RecordError($"{nameof(command.ShadeMaterial)} input cannot be null."); |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + // construct adapter and config |
| 161 | + LadybugConfig config = new LadybugConfig() |
| 162 | + { |
| 163 | + JsonFile = new FileSettings() |
| 164 | + { |
| 165 | + FileName = $"LBTBHoM_{Guid.NewGuid()}.json", |
| 166 | + Directory = Path.GetTempPath() |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + // construct the base object and file to be passed to Python for simulation |
| 171 | + SimulationResult simulationResult = new SimulationResult() |
| 172 | + { |
| 173 | + EpwFile = Path.GetFullPath(command.EpwFile.GetFullFileName()).Replace(@"\", "/"), |
| 174 | + GroundMaterial = command.GroundMaterial, |
| 175 | + ShadeMaterial = command.ShadeMaterial, |
| 176 | + Name = Engine.LadybugTools.Compute.SimulationID(command.EpwFile.GetFullFileName(), command.GroundMaterial, command.ShadeMaterial) |
| 177 | + }; |
| 178 | + |
| 179 | + // push object to json file |
| 180 | + Push(new List<SimulationResult>() { simulationResult }, actionConfig: config); |
| 181 | + |
| 182 | + // locate the Python executable and file containing the simulation code |
| 183 | + PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true); |
| 184 | + string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "simulation_result.py"); |
| 185 | + |
| 186 | + // run the simulation |
| 187 | + string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\""; |
| 188 | + Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true); |
| 189 | + |
| 190 | + // reload from Python results |
| 191 | + List<object> simulationResultPopulated = Pull(new FilterRequest(), actionConfig: config).ToList(); |
| 192 | + |
| 193 | + // remove temporary file |
| 194 | + File.Delete(config.JsonFile.GetFullFileName()); |
| 195 | + |
| 196 | + m_executeSuccess = true; |
| 197 | + return simulationResultPopulated; |
| 198 | + } |
| 199 | + |
| 200 | + /**************************************************/ |
| 201 | + |
| 202 | + private List<object> RunCommand(RunExternalComfortCommand command) |
| 203 | + { |
| 204 | + if (command.SimulationResult == null) |
| 205 | + { |
| 206 | + BH.Engine.Base.Compute.RecordError($"{nameof(command.SimulationResult)} input cannot be null."); |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + if (command.Typology == null) |
| 211 | + { |
| 212 | + BH.Engine.Base.Compute.RecordError($"{nameof(command.Typology)} input cannot be null."); |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + LadybugConfig config = new LadybugConfig() |
| 217 | + { |
| 218 | + JsonFile = new FileSettings() |
| 219 | + { |
| 220 | + FileName = $"LBTBHoM_{Guid.NewGuid()}.json", |
| 221 | + Directory = Path.GetTempPath() |
| 222 | + } |
| 223 | + }; |
| 224 | + |
| 225 | + // construct the base object |
| 226 | + ExternalComfort externalComfort = new ExternalComfort() |
| 227 | + { |
| 228 | + SimulationResult = command.SimulationResult, |
| 229 | + Typology = command.Typology, |
| 230 | + }; |
| 231 | + |
| 232 | + // push objects to json file |
| 233 | + Push(new List<ExternalComfort>() { externalComfort }, actionConfig: config); |
| 234 | + |
| 235 | + // locate the Python executable and file containing the simulation code |
| 236 | + PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true); |
| 237 | + string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "external_comfort.py"); |
| 238 | + |
| 239 | + // run the calculation |
| 240 | + string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\""; |
| 241 | + Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true); |
| 242 | + |
| 243 | + // reload from Python results |
| 244 | + List<object> externalComfortPopulated = Pull(new FilterRequest(), actionConfig: config).ToList(); |
| 245 | + |
| 246 | + // remove temporary file |
| 247 | + File.Delete(config.JsonFile.GetFullFileName()); |
| 248 | + |
| 249 | + m_executeSuccess = true; |
| 250 | + return externalComfortPopulated; |
| 251 | + } |
| 252 | + |
| 253 | + /**************************************************/ |
| 254 | + /* Private methods - Fallback */ |
| 255 | + /**************************************************/ |
| 256 | + |
| 257 | + private List<object> RunCommand(IExecuteCommand command) |
| 258 | + { |
| 259 | + BH.Engine.Base.Compute.RecordError($"The command {command.GetType().FullName} is not valid for the LadybugTools Adapter. Please use a LadybugCommand, or use the correct adapter for the input command."); |
| 260 | + return new List<object>(); |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments