Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Physical_Engine: Add support for GeneralMaterialTakeoff #3047

Merged
merged 12 commits into from
May 9, 2023
Merged

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .ci/Datasets/Matter_Engine/Modify/AssignTemplate.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

146 changes: 146 additions & 0 deletions Matter_Engine/Compute/AggregateGeneralMaterialTakeoff.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* 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.Text;
using System.Threading.Tasks;

using BH.oM.Physical.Constructions;
using BH.oM.Physical.Materials;

using BH.oM.Base.Attributes;
using System.ComponentModel;
using BH.oM.Physical.Elements;
using BH.oM.Geometry;
using BH.oM.Dimensional;
using BH.oM.Quantities.Attributes;
using BH.Engine.Base.Objects;
using BH.oM.Base;
using BH.Engine.Diffing;
using BH.Engine.Base;
using BH.oM.Diffing;

namespace BH.Engine.Matter
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Calculates an aggregate GeneralMaterialTakeoff from a collection of elements.")]
[Input("elements", "The elements to iterate over in generation of the VolumetricMaterialTakeoff.")]
[Input("checkForTakeoffFragment", "If true and the provided element is a BHoMObject, the incoming item is checked if it has a VolumetricMaterialTakeoff fragment attached, and if so, returns uses it as the basis for the GeneralMaterialTakeoff. If false, the GeneralMaterialTakeoff returned will be calculated, independant of fragment attached.")]
[Input("comparisonConfig", "Optional comparison config to be used for check equality of two Materials. Defaults to checking the full Material object.")]
[Output("generalMaterialTakeoff", "A GeneralMaterialTakeoff containing the unique materials across all elements.")]
public static GeneralMaterialTakeoff AggregateGeneralMaterialTakeoff(IEnumerable<IElementM> elements, bool checkForTakeoffFragment = true, BaseComparisonConfig comparisonConfig = null)
{
if (elements == null)
{
Base.Compute.RecordError($"Provided List of {nameof(IElementM)}s is null, cannot compute aggregate takeoff.");
return null;
}

if (elements.Any(x => x == null))
{
Base.Compute.RecordWarning($"At least one of the provided {nameof(IElementM)}s is null and will be filtered out when computing the {nameof(AggregateVolumetricMaterialTakeoff)}.");
elements = elements.Where(x => x != null);
}

if (!elements.Any())
{
Base.Compute.RecordWarning($"No non-null {nameof(IElementM)}s provided. Empty {nameof(GeneralMaterialTakeoff)} returned.");
return new GeneralMaterialTakeoff();
}

return AggregateGeneralMaterialTakeoff(elements.Select(x => x.IGeneralMaterialTakeoff(checkForTakeoffFragment)), comparisonConfig);
}

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

[Description("Calculates an aggregate GeneralMaterialTakeoff from a collection individual GeneralMaterialTakeoff.")]
[Input("generalMaterialTakeoffs", "The individual GeneralMaterialTakeoff to aggregate together.")]
[Input("comparisonConfig", "Optional comparison config to be used for check equality of two Materials. Defaults to checking the full Material object.")]
[Output("generalMaterialTakeoff", "A GeneralMaterialTakeoff incorporating the provided materials and qunatities from each individual GeneralMaterialTakeoff.")]
public static GeneralMaterialTakeoff AggregateGeneralMaterialTakeoff(IEnumerable<GeneralMaterialTakeoff> generalMaterialTakeoffs, BaseComparisonConfig comparisonConfig = null)
{
if (generalMaterialTakeoffs == null)
{
Base.Compute.RecordError($"Provided List of {nameof(GeneralMaterialTakeoff)}s is null, cannot compute aggregate takeoff.");
return null;
}

if (generalMaterialTakeoffs.Any(x => x == null))
{
Base.Compute.RecordWarning($"At least one of the provided {nameof(GeneralMaterialTakeoff)}s is null and will be filtered out when computing the {nameof(AggregateGeneralMaterialTakeoff)}.");
generalMaterialTakeoffs = generalMaterialTakeoffs.Where(x => x != null);
}

if (!generalMaterialTakeoffs.Any())
{
Base.Compute.RecordWarning($"No non-null {nameof(GeneralMaterialTakeoff)}s provided. Empty {nameof(GeneralMaterialTakeoff)} returned.");
return new GeneralMaterialTakeoff();
}

List<GeneralMaterialTakeoff> localMatTakeoffs = generalMaterialTakeoffs.ToList();

Dictionary<string, TakeoffItem> hashedTakeoffItems = new Dictionary<string, TakeoffItem>();

for (int j = 0; j < localMatTakeoffs.Count; j++)
{
for (int i = 0; i < localMatTakeoffs[j].MaterialTakeoffItems.Count; i++)
{
TakeoffItem current = localMatTakeoffs[j].MaterialTakeoffItems[i];
string hash = current.Material.Hash(comparisonConfig);
TakeoffItem takeoffItem;
if (hashedTakeoffItems.TryGetValue(hash, out takeoffItem))
{
takeoffItem.Mass += current.Mass;
takeoffItem.Volume += current.Volume;
takeoffItem.Area += current.Area;
takeoffItem.Length += current.Length;
takeoffItem.NumberItem += current.NumberItem;
takeoffItem.ElectricCurrent += current.ElectricCurrent;
takeoffItem.Energy += current.Energy;
takeoffItem.Power += current.Power;
takeoffItem.VolumetricFlowRate += current.VolumetricFlowRate;
}
else
takeoffItem = current;

hashedTakeoffItems[hash] = takeoffItem;
}
}


return new GeneralMaterialTakeoff { MaterialTakeoffItems = hashedTakeoffItems.Values.ToList() };
}

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

}
}



67 changes: 67 additions & 0 deletions Matter_Engine/Create/GeneralMaterialTakeoff.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.Text;
using System.Threading.Tasks;

using BH.oM.Physical.Constructions;
using BH.oM.Physical.Materials;

using BH.oM.Base.Attributes;
using System.ComponentModel;
using BH.oM.Physical.Elements;
using BH.oM.Geometry;
using BH.oM.Dimensional;
using BH.oM.Quantities.Attributes;
using BH.Engine.Base;
using BH.oM.Base;

namespace BH.Engine.Matter
{
public static partial class Create
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Creates a GeneralMaterialTakeoff based on the VolumetricMaterialTakeoff. Quantities beyound Volume and Mass of the returned GeneralMaterialTakeoff will be unset.")]
[Input("volumetricMaterialTakeoff", "The VolumetricMaterialTakeoff to be used to create the GeneralMaterialTakeoff. Materials from the VolumetricMaterialTakeoff will be used with corresponing volumes as well as densities to populate mass and volume values of the general material takeoff.")]
[Output("generalMaterialTakeoff", "A GeneralMaterialTakeoff composed of the Materials in the provided VolumetricMaterialTakeoff and volumes as well as mass set.")]
public static GeneralMaterialTakeoff GeneralMaterialTakeoff(VolumetricMaterialTakeoff volumetricMaterialTakeoff)
{
if (volumetricMaterialTakeoff == null)
{
Base.Compute.RecordError($"Cannot create a {nameof(GeneralMaterialTakeoff)} from a null {nameof(VolumetricMaterialTakeoff)}.");
return null;
}

return (GeneralMaterialTakeoff)volumetricMaterialTakeoff;
}

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

}
}

22 changes: 22 additions & 0 deletions Matter_Engine/Create/MaterialComposition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ public static MaterialComposition MaterialComposition(VolumetricMaterialTakeoff

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

[Description("Creates a MaterialComposition based on the volumes normalised to 1 and materials in the provided Material.")]
[Input("generalMaterialTakeoff", "The GeneralMaterialTakeoff to be used to create the MaterialComposition. Materials from the GeneralMaterialTakeoff will be used with corresponing normalised volumes, ensuring the total of all ratios equates to 1.")]
[Output("materialComposition", "A MaterialComposition composed of the Materials in the provided GeneralMaterialTakeoff and ratios as its normalised volumes.")]
public static MaterialComposition MaterialComposition(GeneralMaterialTakeoff generalMaterialTakeoff)
{
if (generalMaterialTakeoff == null)
{
Base.Compute.RecordError($"Cannot create a {nameof(MaterialComposition)} from a null {nameof(VolumetricMaterialTakeoff)}.");
return null;
}
if (generalMaterialTakeoff.MaterialTakeoffItems == null)
{
Base.Compute.RecordError($"Cannot create a {nameof(MaterialComposition)} from a {nameof(VolumetricMaterialTakeoff)} with null {nameof(generalMaterialTakeoff.MaterialTakeoffItems)}.");
return null;
}

double totalVolume = generalMaterialTakeoff.MaterialTakeoffItems.Sum(x => x.Volume);

return new MaterialComposition(generalMaterialTakeoff.MaterialTakeoffItems.Select(x => x.Material), generalMaterialTakeoff.MaterialTakeoffItems.Select(x => x.Volume / totalVolume));
}

/***************************************************/
}
}

Expand Down
16 changes: 16 additions & 0 deletions Matter_Engine/Create/VolumetricMaterialTakeoff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ public static VolumetricMaterialTakeoff VolumetricMaterialTakeoff(MaterialCompos
}

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

[Description("Creates a VolumetricMaterialTakeoff based on the materials in the provided GeneralMaterialTakeoff, collection only volumes.")]
[Input("generalMaterialTakeoff", "The GeneralMaterialTakeoff to be used to create the VolumetricMaterialTakeoff. Materials and volumes of the GeneralMaterialTakeoff will be assigned to the takeoff.")]
[Output("volumetricMaterialTakeoff", "A VolumetricMaterialTakeoff composed of the Materials adn Volumes in the provided GeneralMaterialTakeoff.")]
public static VolumetricMaterialTakeoff VolumetricMaterialTakeoff(GeneralMaterialTakeoff generalMaterialTakeoff)
{
if (generalMaterialTakeoff == null)
{
Base.Compute.RecordError($"Cannot create a {nameof(VolumetricMaterialTakeoff)} from a null {nameof(GeneralMaterialTakeoff)}.");
return null;
}

return new VolumetricMaterialTakeoff(generalMaterialTakeoff.MaterialTakeoffItems.Select(x => x.Material), generalMaterialTakeoff.MaterialTakeoffItems.Select(x => x.Volume));
}

/***************************************************/
}
}

49 changes: 49 additions & 0 deletions Matter_Engine/Modify/AssignTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,55 @@ public static VolumetricMaterialTakeoff AssignTemplate(this VolumetricMaterialTa
}

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

[Description("Maps a set of materials in the MaterialCompositions to a set of provided transdiciplinary materials.\n" +
"First atempts to match the name of the provided materials to the transdiciplinary material maps.\n" +
"If no name match is found, atempts to instead find a material with as many matching MaterialProperties (based on type and name) as possible.\n" +
"If a unique match is found based on one of the above matching methods, all Properties from the transdiciplinary material is applied to the material to be matched.")]
[Input("generalMaterialTakeoff", "The GeneralMaterialTakeoff to Modify. Materials in the GeneralMaterialTakeoff will be evaluated based on the name and properties.")]
[Input("templateMaterials", "The template materials to match to and assign properties from onto the model materials. Should generally have unique names. Names of material as well as material properties will be used to map to the materials to be modified.")]
[Input("prioritiseTemplate", "Controls if main material or map material should be prioritised when conflicting information is found on both in terms of Density and/or Properties. If true, map is prioritised, if false, main material is prioritised.")]
[Input("uniquePerNamespace", "If true, the method is checking for similarity of MaterialProperties on the materials and found matching material map based on namespace. If false, this check is instead done on exact type.")]
[Output("generalMaterialTakeoff", "MaterialComposition with Materials with modified list of properties. Materials for which no unique match could be found are unaffected.")]
public static GeneralMaterialTakeoff AssignTemplate(this GeneralMaterialTakeoff generalMaterialTakeoff, IEnumerable<Material> templateMaterials, bool prioritiseTemplate = true, bool uniquePerNamespace = true)
{
if (generalMaterialTakeoff == null)
return null;

if (templateMaterials == null || !templateMaterials.Any())
{
Base.Compute.RecordWarning($"No {nameof(templateMaterials)} provied. Unmapped {nameof(GeneralMaterialTakeoff)}s returned.");
return generalMaterialTakeoff;
}

List<Material> materials = generalMaterialTakeoff.MaterialTakeoffItems.Select(x => x.Material).AssignTemplate(templateMaterials, prioritiseTemplate, uniquePerNamespace).ToList();

List<TakeoffItem> takeoffItems = new List<TakeoffItem>();

for (int i = 0; i < materials.Count; i++)
{
TakeoffItem unMapped = generalMaterialTakeoff.MaterialTakeoffItems[i];
TakeoffItem item = new TakeoffItem
{
Material = materials[i],
Volume = unMapped.Volume,
Mass = unMapped.Mass,
Area = unMapped.Area,
Length = unMapped.Length,
ElectricCurrent = unMapped.ElectricCurrent,
Energy = unMapped.Energy,
NumberItem = unMapped.NumberItem,
Power = unMapped.Power,
VolumetricFlowRate = unMapped.VolumetricFlowRate,
};
takeoffItems.Add(item);
}


return new GeneralMaterialTakeoff { MaterialTakeoffItems = takeoffItems };
}

/***************************************************/
}
}

Expand Down
Loading