Skip to content

Commit

Permalink
7.0 Deployment (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd authored Dec 20, 2023
2 parents 6be7b0b + 5999708 commit 8235f6f
Show file tree
Hide file tree
Showing 232 changed files with 45,071 additions and 16,311 deletions.
28 changes: 28 additions & 0 deletions .ci/BHoMBot/Nuget/BHoM.Interop.LadybugTools.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>BHoM.Interop.LadybugTools</id>
<version></version>
<authors>BHoM</authors>
<projectUrl>https://github.com/BHoM/LadybugTools_Toolkit</projectUrl>
<license type="file">licence.txt</license>
<icon>icon.png</icon>
<readme>readme.md</readme>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Interact with Ladybug Tools via BHoM</description>
<releaseNotes></releaseNotes>
<copyright></copyright>
<tags>BHoM engine aec LadyBug LadyBugTools</tags>
<title></title>
<dependencies>
<group targetFramework="netstandard2.0">
<dependency id="NETStandard.Library" version="2.0.3" />
</group>
</dependencies>
</metadata>
<files>
<file src="licence/licence.txt" target="" />
<file src="images/icon.png" target="" />
<file src="docs/readme.md" target="" />
</files>
</package>
263 changes: 263 additions & 0 deletions LadybugTools_Adapter/AdapterActions/Execute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
/*
* 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.Engine.Adapter;
using BH.Engine.LadybugTools;
using BH.oM.Adapter;
using BH.oM.Adapter.Commands;
using BH.oM.Base;
using BH.oM.Data.Requests;
using BH.oM.LadybugTools;
using BH.oM.Python;
using BH.Engine.Python;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public partial class LadybugToolsAdapter : BHoMAdapter
{
bool m_executeSuccess = false;
public override Output<List<object>, bool> Execute(IExecuteCommand command, ActionConfig actionConfig = null)
{
m_executeSuccess = false;
Output<List<object>, bool> output = new Output<List<object>, bool>() { Item1 = new List<object>(), Item2 = false };

List<object> temp = IRunCommand(command);

output.Item1 = temp;
output.Item2 = m_executeSuccess;

return output;
}

/**************************************************/
/* Public methods - Interface */
/**************************************************/

public List<object> IRunCommand(IExecuteCommand command)
{
if (command == null)
{
BH.Engine.Base.Compute.RecordError("Please input a valid Ladybug Command to execute.");
return new List<object>();
}

return RunCommand(command as dynamic);
}

/**************************************************/
/* Private methods - Run Ladybug Command */
/**************************************************/

private List<object> RunCommand(GetMaterialCommand command)
{
PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true);
LadybugConfig config = new LadybugConfig()
{
JsonFile = new FileSettings()
{
FileName = $"LBTBHoM_Materials_{DateTime.Now:yyyyMMdd}.json",
Directory = Path.GetTempPath()
}
};

if (!File.Exists(config.JsonFile.GetFullFileName()))
{
string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "get_material.py");

string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\"";

Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
}

List<object> materialObjects = Pull(new FilterRequest(), actionConfig: config).ToList();

m_executeSuccess = true;
return materialObjects.Where(m => (m as IEnergyMaterialOpaque).Name.Contains(command.Filter)).ToList();
}

/**************************************************/

private List<object> RunCommand(GetTypologyCommand command)
{
PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true);
LadybugConfig config = new LadybugConfig()
{
JsonFile = new FileSettings()
{
FileName = $"LBTBHoM_Typologies_{DateTime.Now:yyyyMMdd}.json",
Directory = Path.GetTempPath()
}
};

if (!File.Exists(config.JsonFile.GetFullFileName()))
{
string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "get_typology.py");

string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\"";

Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
}

List<object> typologyObjects = Pull(new FilterRequest(), actionConfig: config).ToList();

m_executeSuccess = true;
return typologyObjects.Where(m => (m as Typology).Name.Contains(command.Filter)).ToList();
}

/**************************************************/

private List<object> RunCommand(RunSimulationCommand command)
{
// validation prior to passing to Python
if (command.EpwFile == null)
{
BH.Engine.Base.Compute.RecordError($"{nameof(command.EpwFile)} input cannot be null.");
return null;
}

if (!File.Exists(command.EpwFile.GetFullFileName()))
{
BH.Engine.Base.Compute.RecordError($"{command.EpwFile.GetFullFileName()} does not exist.");
return null;
}

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

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

// construct adapter and config
LadybugConfig config = new LadybugConfig()
{
JsonFile = new FileSettings()
{
FileName = $"LBTBHoM_{Guid.NewGuid()}.json",
Directory = Path.GetTempPath()
}
};

// construct the base object and file to be passed to Python for simulation
SimulationResult simulationResult = new SimulationResult()
{
EpwFile = Path.GetFullPath(command.EpwFile.GetFullFileName()).Replace(@"\", "/"),
GroundMaterial = command.GroundMaterial,
ShadeMaterial = command.ShadeMaterial,
Name = Engine.LadybugTools.Compute.SimulationID(command.EpwFile.GetFullFileName(), command.GroundMaterial, command.ShadeMaterial)
};

// push object to json file
Push(new List<SimulationResult>() { simulationResult }, actionConfig: config);

// locate the Python executable and file containing the simulation code
PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true);
string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "simulation_result.py");

// run the simulation
string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\"";
Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);

// reload from Python results
List<object> simulationResultPopulated = Pull(new FilterRequest(), actionConfig: config).ToList();

// remove temporary file
File.Delete(config.JsonFile.GetFullFileName());

m_executeSuccess = true;
return simulationResultPopulated;
}

/**************************************************/

private List<object> RunCommand(RunExternalComfortCommand command)
{
if (command.SimulationResult == null)
{
BH.Engine.Base.Compute.RecordError($"{nameof(command.SimulationResult)} input cannot be null.");
return null;
}

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

LadybugConfig config = new LadybugConfig()
{
JsonFile = new FileSettings()
{
FileName = $"LBTBHoM_{Guid.NewGuid()}.json",
Directory = Path.GetTempPath()
}
};

// construct the base object
ExternalComfort externalComfort = new ExternalComfort()
{
SimulationResult = command.SimulationResult,
Typology = command.Typology,
};

// push objects to json file
Push(new List<ExternalComfort>() { externalComfort }, actionConfig: config);

// locate the Python executable and file containing the simulation code
PythonEnvironment env = Engine.LadybugTools.Compute.InstallPythonEnv_LBT(true);
string script = Path.Combine(Engine.Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "external_comfort.py");

// run the calculation
string cmdCommand = $"{env.Executable} {script} -j \"{config.JsonFile.GetFullFileName()}\"";
Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);

// reload from Python results
List<object> externalComfortPopulated = Pull(new FilterRequest(), actionConfig: config).ToList();

// remove temporary file
File.Delete(config.JsonFile.GetFullFileName());

m_executeSuccess = true;
return externalComfortPopulated;
}

/**************************************************/
/* Private methods - Fallback */
/**************************************************/

private List<object> RunCommand(IExecuteCommand command)
{
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.");
return new List<object>();
}
}
}
72 changes: 72 additions & 0 deletions LadybugTools_Adapter/AdapterActions/Pull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.Engine.Adapter;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.Data.Requests;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public partial class LadybugToolsAdapter : BHoMAdapter
{
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
{
if (actionConfig == null)
{
BH.Engine.Base.Compute.RecordError("Please provide a valid LadybugConfig ActionConfig.");
return new List<IBHoMObject>();
}

LadybugConfig config = actionConfig as LadybugConfig;
if (config == null)
{
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.");
return new List<IBHoMObject>();
}

if (config.JsonFile == null)
{
BH.Engine.Base.Compute.RecordError("Please provide a valid JsonFile FileSettings object.");
return new List<IBHoMObject>();
}

if (!System.IO.File.Exists(config.JsonFile.GetFullFileName()))
{
BH.Engine.Base.Compute.RecordError($"The file at {config.JsonFile.GetFullFileName()} does not exist to pull from.");
return new List<IBHoMObject>();
}

if (request != null)
{
FilterRequest filterRequest = request as FilterRequest;
return Read(filterRequest.Type, actionConfig: config);
}
else
return Read(null, config);
}
}
}
Loading

0 comments on commit 8235f6f

Please sign in to comment.