Skip to content

Commit 8235f6f

Browse files
author
Fraser Greenroyd
authored
7.0 Deployment (#159)
2 parents 6be7b0b + 5999708 commit 8235f6f

232 files changed

Lines changed: 45071 additions & 16311 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package>
3+
<metadata>
4+
<id>BHoM.Interop.LadybugTools</id>
5+
<version></version>
6+
<authors>BHoM</authors>
7+
<projectUrl>https://github.com/BHoM/LadybugTools_Toolkit</projectUrl>
8+
<license type="file">licence.txt</license>
9+
<icon>icon.png</icon>
10+
<readme>readme.md</readme>
11+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
12+
<description>Interact with Ladybug Tools via BHoM</description>
13+
<releaseNotes></releaseNotes>
14+
<copyright></copyright>
15+
<tags>BHoM engine aec LadyBug LadyBugTools</tags>
16+
<title></title>
17+
<dependencies>
18+
<group targetFramework="netstandard2.0">
19+
<dependency id="NETStandard.Library" version="2.0.3" />
20+
</group>
21+
</dependencies>
22+
</metadata>
23+
<files>
24+
<file src="licence/licence.txt" target="" />
25+
<file src="images/icon.png" target="" />
26+
<file src="docs/readme.md" target="" />
27+
</files>
28+
</package>
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.oM.Adapter;
25+
using BH.oM.Base;
26+
using BH.oM.Data.Requests;
27+
using BH.oM.LadybugTools;
28+
using System;
29+
using System.Collections.Generic;
30+
using System.Text;
31+
32+
namespace BH.Adapter.LadybugTools
33+
{
34+
public partial class LadybugToolsAdapter : BHoMAdapter
35+
{
36+
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
37+
{
38+
if (actionConfig == null)
39+
{
40+
BH.Engine.Base.Compute.RecordError("Please provide a valid LadybugConfig ActionConfig.");
41+
return new List<IBHoMObject>();
42+
}
43+
44+
LadybugConfig config = actionConfig as LadybugConfig;
45+
if (config == null)
46+
{
47+
BH.Engine.Base.Compute.RecordError($"The type of actionConfig provided: {actionConfig.GetType().FullName} is not valid for this adapter. Please provide a valid LadybugConfig actionConfig.");
48+
return new List<IBHoMObject>();
49+
}
50+
51+
if (config.JsonFile == null)
52+
{
53+
BH.Engine.Base.Compute.RecordError("Please provide a valid JsonFile FileSettings object.");
54+
return new List<IBHoMObject>();
55+
}
56+
57+
if (!System.IO.File.Exists(config.JsonFile.GetFullFileName()))
58+
{
59+
BH.Engine.Base.Compute.RecordError($"The file at {config.JsonFile.GetFullFileName()} does not exist to pull from.");
60+
return new List<IBHoMObject>();
61+
}
62+
63+
if (request != null)
64+
{
65+
FilterRequest filterRequest = request as FilterRequest;
66+
return Read(filterRequest.Type, actionConfig: config);
67+
}
68+
else
69+
return Read(null, config);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)