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

Matter_Engine: Make sure no hash fragments are stored in AggregateMaterialComposition #3337

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .ci/Datasets/Structure_Engine/Query/Mass.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Matter_Engine/Compute/AggregateGeneralMaterialTakeoff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public static GeneralMaterialTakeoff AggregateGeneralMaterialTakeoff(IEnumerable
}

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

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

for (int j = 0; j < localMatTakeoffs.Count; j++)
Expand Down
65 changes: 36 additions & 29 deletions Matter_Engine/Compute/AggregateMaterialComposition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,63 +49,70 @@
/**** Public Methods ****/
/***************************************************/

[PreviousVersion("7.2", "BH.Engine.Matter.Compute.AggregateMaterialComposition(System.Collections.Generic.IEnumerable<BH.oM.Dimensional.IElementM>)")]
[Description("Calculates an aggregate MaterialComposition from a collection of elements.")]
[Input("elements", "The elements to iterate over in generation of the MaterialCombination.")]
[Output("materialComposition", "A MaterialComposition containing the unique materials across all elements and their relative proportions.")]
public static MaterialComposition AggregateMaterialComposition(IEnumerable<IElementM> elements)
public static MaterialComposition AggregateMaterialComposition(IEnumerable<IElementM> elements, BaseComparisonConfig comparisonConfig = null)

Check warning on line 56 in Matter_Engine/Compute/AggregateMaterialComposition.cs

View check run for this annotation

BHoMBot-CI / documentation-compliance

Matter_Engine/Compute/AggregateMaterialComposition.cs#L56

Input parameter requires a matching Input attribute - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsInputAttributePresent
{
return AggregateMaterialComposition(elements.Select(x => x.IMaterialComposition()), elements.Select(x => x.ISolidVolume()));
return AggregateMaterialComposition(elements.Select(x => x.IMaterialComposition()), elements.Select(x => x.ISolidVolume()), comparisonConfig);
}

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

[PreviousVersion("7.2", "BH.Engine.Matter.Compute.AggregateMaterialComposition(System.Collections.Generic.IEnumerable<BH.oM.Physical.Materials.MaterialComposition>, System.Collections.Generic.IEnumerable<System.Double>)")]
[Description("Calculates an aggregate MaterialComposition from a collection individual MaterialCompositions and their relative ratios.")]
[Input("materialCompositions", "The individual MaterialCompositions to aggregate together.")]
[Input("ratios", "The relative volumetric based ratios of each MaterialComposition. The number of ratios must match the number of MaterialCompositions.", typeof(Ratio))]
[Output("materialComposition", "A MaterialComposition incorporating the provided materials from each individual MaterialComposition and newly calculated ratios, factoring both the input ratio values and the individual Material ratios in the existing MaterialCompositions.")]
public static MaterialComposition AggregateMaterialComposition(IEnumerable<MaterialComposition> materialCompositions, IEnumerable<double> ratios)
public static MaterialComposition AggregateMaterialComposition(IEnumerable<MaterialComposition> materialCompositions, IEnumerable<double> ratios, BaseComparisonConfig comparisonConfig = null)

Check warning on line 68 in Matter_Engine/Compute/AggregateMaterialComposition.cs

View check run for this annotation

BHoMBot-CI / documentation-compliance

Matter_Engine/Compute/AggregateMaterialComposition.cs#L68

Input parameter requires a matching Input attribute - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsInputAttributePresent
{
List<Material> allMaterials = new List<Material>();
List<double> allRatios = new List<double>();
if (materialCompositions == null)
{
Base.Compute.RecordError("Cannot compute AggregateMaterialComposition for a null collection of MaterialCompositions.");
return null;
}
if (ratios == null)
{
Base.Compute.RecordError("Cannot compute AggregateMaterialComposition for a null collection of Ratios.");
return null;
}

List<MaterialComposition> localMatComps = materialCompositions.ToList();

List<double> localRatios = ratios.ToList();

if (localMatComps.Count != localRatios.Count)
{
Base.Compute.RecordError("Same number of MaterialCompositions and Ratios need to be provided to be able to compute AggregateMaterialComposition.");
return null;
}

if(localMatComps.Count == 0)
return new MaterialComposition(new List<Material>(), new List<double>());

Dictionary<string, Tuple<Material, double>> hashedMaterialRatioTuples = new Dictionary<string, Tuple<Material, double>>();

for (int j = 0; j < localMatComps.Count; j++)
{
double compositionRatio = localRatios[j];
for (int i = 0; i < localMatComps[j].Materials.Count; i++)
{
Material mat = localMatComps[j].Materials[i];
mat = BH.Engine.Diffing.Modify.SetRevisionFragment(mat);

bool existed = false;
for (int k = 0; k < allMaterials.Count; k++)
{
if (allMaterials[k].FindFragment<RevisionFragment>().Hash == mat.FindFragment<RevisionFragment>().Hash)
{
allRatios[k] += localMatComps[j].Ratios[i] * localRatios[j];
existed = true;
break;
}
}
if (!existed)
{
allMaterials.Add(mat);
allRatios.Add(localMatComps[j].Ratios[i] * localRatios[j]);
}
double matRatio = localMatComps[j].Ratios[i] * compositionRatio;
string hash = mat.Hash(comparisonConfig);
Tuple<Material, double> matVolumePair;
if (hashedMaterialRatioTuples.TryGetValue(hash, out matVolumePair))
matVolumePair = new Tuple<Material, double>(matVolumePair.Item1, matVolumePair.Item2 + matRatio);
else
matVolumePair = new Tuple<Material, double>(mat, matRatio);

hashedMaterialRatioTuples[hash] = matVolumePair;
}
}
double factor = 1 / hashedMaterialRatioTuples.Sum(x => x.Value.Item2);
return new MaterialComposition(hashedMaterialRatioTuples.Values.Select(x => x.Item1), hashedMaterialRatioTuples.Values.Select(x => x.Item2 * factor));

foreach (Material mat in allMaterials)
{
mat.RemoveFragment(typeof(RevisionFragment));
}

double factor = 1 / allRatios.Sum();
return new MaterialComposition(allMaterials, allRatios.Select(x => x * factor).ToList());
}

/***************************************************/
Expand Down
Loading