Skip to content

Commit

Permalink
Element1d displacement and extrema
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristjan Nielsen committed Nov 2, 2023
1 parent eddcb59 commit a533927
Show file tree
Hide file tree
Showing 21 changed files with 307 additions and 145 deletions.
35 changes: 24 additions & 11 deletions GsaGH/Components/5_Results/BeamDisplacements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected override void SolveInternal(IGH_DataAccess da) {
}

ReadOnlyCollection<int> elementIds = result.ElementIds(elementlist);
IBeamResultSubset<IBeamDisplacement, ResultVector6<NodeExtremaKey>> resultSet =
IElement1dResultSubset<IElement1dDisplacement, IDisplacement, ResultVector6<Element1dExtremaKey>> resultSet =
result.BeamDisplacements.ResultSubset(elementIds, positionsCount);

List<int> permutations = result.SelectedPermutationIds ?? new List<int>() {
Expand All @@ -156,29 +156,42 @@ protected override void SolveInternal(IGH_DataAccess da) {
}

if (_selectedItems[0] == ExtremaHelper.Vector6Displacements[0]) {
foreach (KeyValuePair<int, Collection<IBeamDisplacement>> kvp in resultSet.Subset) {
foreach (KeyValuePair<int, Collection<IElement1dDisplacement>> kvp in resultSet.Subset) {
foreach (int p in permutations) {
var path = new GH_Path(result.CaseId, result.SelectedPermutationIds == null ? 0 : p, kvp.Key);
outTransX.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outTransX.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.X.ToUnit(_lengthUnit))), path);
outTransY.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outTransY.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Y.ToUnit(_lengthUnit))), path);
outTransZ.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outTransZ.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Z.ToUnit(_lengthUnit))), path);
outTransXyz.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outTransXyz.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Xyz.ToUnit(_lengthUnit))), path);
outRotX.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outRotX.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Xx)), path);
outRotY.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outRotY.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Yy)), path);
outRotZ.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outRotZ.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Zz)), path);
outRotXyz.AddRange(kvp.Value[p - 1].Displacements.Values.Select(
outRotXyz.AddRange(kvp.Value[p - 1].Results.Values.Select(
r => new GH_UnitNumber(r.Xxyyzz)), path);
}
}
} else {
Element1dExtremaKey key = ExtremaHelper.ExtremaKey(resultSet, _selectedItems[0]);
IDisplacement extrema = resultSet.GetExtrema(key);
int perm = result.CaseType == CaseType.AnalysisCase ? 0 : 1;
var path = new GH_Path(result.CaseId, key.Permutation + perm, key.Id);
outTransX.Add(new GH_UnitNumber(extrema.X.ToUnit(_lengthUnit)), path);
outTransY.Add(new GH_UnitNumber(extrema.Y.ToUnit(_lengthUnit)), path);
outTransZ.Add(new GH_UnitNumber(extrema.Z.ToUnit(_lengthUnit)), path);
outTransXyz.Add(new GH_UnitNumber(extrema.Xyz.ToUnit(_lengthUnit)), path);
outRotX.Add(new GH_UnitNumber(extrema.Xx), path);
outRotY.Add(new GH_UnitNumber(extrema.Yy), path);
outRotZ.Add(new GH_UnitNumber(extrema.Zz), path);
outRotXyz.Add(new GH_UnitNumber(extrema.Xxyyzz), path);
}

PostHog.Result(result.CaseType, 1, GsaResultsValues.ResultType.Displacement);
}

Expand Down
23 changes: 23 additions & 0 deletions GsaGH/Components/5_Results/Helpers/ExtremaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,28 @@ internal static U ExtremaKey<T, U>(INodeResultSubset<T, ResultVector6<U>> result
_ => throw new System.ArgumentException("Extrema case not found"),
};
}

internal static U ExtremaKey<T1, T2, U>(IElement1dResultSubset<T1, T2, ResultVector6<U>> resultSet, string key)
where T1 : IElement1dQuantity<T2> where T2 : IResultItem {
return key switch {
"Max Ux" => resultSet.Max.X,
"Max Uy" => resultSet.Max.Y,
"Max Uz" => resultSet.Max.Z,
"Max |U|" => resultSet.Max.Xyz,
"Max Rxx" => resultSet.Max.Xx,
"Max Ryy" => resultSet.Max.Yy,
"Max Rzz" => resultSet.Max.Zz,
"Max |R|" => resultSet.Max.Xxyyzz,
"Min Ux" => resultSet.Min.X,
"Min Uy" => resultSet.Min.Y,
"Min Uz" => resultSet.Min.Z,
"Min |U|" => resultSet.Min.Xyz,
"Min Rxx" => resultSet.Min.Xx,
"Min Ryy" => resultSet.Min.Yy,
"Min Rzz" => resultSet.Min.Zz,
"Min |R|" => resultSet.Min.Xxyyzz,
_ => throw new System.ArgumentException("Extrema case not found"),
};
}
}
}
3 changes: 2 additions & 1 deletion GsaGH/Components/5_Results/NodeDisplacements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ protected override void SolveInternal(IGH_DataAccess da) {
} else {
NodeExtremaKey key = ExtremaHelper.ExtremaKey(resultSet, _selectedItems[0]);
IDisplacement extrema = resultSet.GetExtrema(key);
var path = new GH_Path(result.CaseId, key.Permutation);
int perm = result.CaseType == CaseType.AnalysisCase ? 0 : 1;
var path = new GH_Path(result.CaseId, key.Permutation + perm);
outTransX.Add(new GH_UnitNumber(extrema.X.ToUnit(_lengthUnit)), path);
outTransY.Add(new GH_UnitNumber(extrema.Y.ToUnit(_lengthUnit)), path);
outTransZ.Add(new GH_UnitNumber(extrema.Z.ToUnit(_lengthUnit)), path);
Expand Down
6 changes: 3 additions & 3 deletions GsaGH/Parameters/5_Results/2_Results/GsaResult2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GsaGH.Parameters.Results {

public class GsaResult2 : IGsaResult {
// Caches
public IBeamResultCache<IBeamDisplacement, ResultVector6<NodeExtremaKey>> BeamDisplacements { get; private set; }
public IElement1dResultCache<IElement1dDisplacement, IDisplacement, ResultVector6<Element1dExtremaKey>> BeamDisplacements { get; private set; }
public INodeResultCache<IDisplacement, ResultVector6<NodeExtremaKey>> NodeDisplacements { get; private set; }

// Other members
Expand All @@ -25,13 +25,13 @@ internal GsaResult2(GsaResult result) {
CaseId = result.CaseId;
switch (CaseType) {
case CaseType.AnalysisCase:
BeamDisplacements = new BeamDisplacementCache(result.AnalysisCaseResult);
BeamDisplacements = new Element1dDisplacementCache(result.AnalysisCaseResult);
NodeDisplacements = new NodeDisplacementCache(result.AnalysisCaseResult);
break;

case CaseType.CombinationCase:
SelectedPermutationIds = result.SelectedPermutationIds;
BeamDisplacements = new BeamDisplacementCache(result.CombinationCaseResult);
BeamDisplacements = new Element1dDisplacementCache(result.CombinationCaseResult);
NodeDisplacements = new NodeDisplacementCache(result.CombinationCaseResult);
break;
}
Expand Down
48 changes: 0 additions & 48 deletions GsaGH/Parameters/5_Results/3_Caches/BeamResultsFactory.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
using GsaAPI;

namespace GsaGH.Parameters.Results {
public class BeamDisplacementCache
: IBeamResultCache<IBeamDisplacement, ResultVector6<NodeExtremaKey>> {
public class Element1dDisplacementCache
: IElement1dResultCache<IElement1dDisplacement, IDisplacement, ResultVector6<Element1dExtremaKey>> {
public IApiResult ApiResult { get; set; }

public ConcurrentDictionary<int, Collection<IBeamDisplacement>> Cache { get; }
= new ConcurrentDictionary<int, Collection<IBeamDisplacement>>();
public ConcurrentDictionary<int, Collection<IElement1dDisplacement>> Cache { get; }
= new ConcurrentDictionary<int, Collection<IElement1dDisplacement>>();

internal BeamDisplacementCache(AnalysisCaseResult result) {
internal Element1dDisplacementCache(AnalysisCaseResult result) {
ApiResult = new ApiResult(result);
}

internal BeamDisplacementCache(CombinationCaseResult result) {
internal Element1dDisplacementCache(CombinationCaseResult result) {
ApiResult = new ApiResult(result);
}

public IBeamResultSubset<IBeamDisplacement, ResultVector6<NodeExtremaKey>>
public IElement1dResultSubset<IElement1dDisplacement, IDisplacement, ResultVector6<Element1dExtremaKey>>
ResultSubset(ICollection<int> elementIds, int positionCount) {
var positions = Enumerable.Range(0, positionCount).Select(
i => (double)i / (positionCount - 1)).ToList();
return ResultSubset(elementIds, new ReadOnlyCollection<double>(positions));
}

public IBeamResultSubset<IBeamDisplacement, ResultVector6<NodeExtremaKey>>
public IElement1dResultSubset<IElement1dDisplacement, IDisplacement, ResultVector6<Element1dExtremaKey>>
ResultSubset(ICollection<int> elementIds, ReadOnlyCollection<double> positions) {
ConcurrentBag<int> missingIds = Cache.GetMissingKeys(elementIds);
if (missingIds.Count > 0) {
Expand All @@ -39,21 +39,21 @@ public IBeamResultSubset<IBeamDisplacement, ResultVector6<NodeExtremaKey>>
ReadOnlyDictionary<int, ReadOnlyCollection<Double6>> aCaseResults
= analysisCase.Element1dDisplacement(elementList, positions);
Parallel.ForEach(missingIds, elementId => Cache.AddOrUpdate(
elementId, BeamResultsFactory.CreateBeamDisplacements(aCaseResults[elementId], positions),
elementId, Element1dResultsFactory.CreateBeamDisplacements(aCaseResults[elementId], positions),
(key, oldValue) => oldValue.AddMissingPositions(aCaseResults[elementId], positions)));
break;

case CombinationCaseResult combinationCase:
ReadOnlyDictionary<int, ReadOnlyCollection<ReadOnlyCollection<Double6>>> cCaseResults
= combinationCase.Element1dDisplacement(elementList, positions);
Parallel.ForEach(missingIds, elementId => Cache.AddOrUpdate(
elementId, BeamResultsFactory.CreateBeamDisplacements(cCaseResults[elementId], positions),
elementId, Element1dResultsFactory.CreateBeamDisplacements(cCaseResults[elementId], positions),
(key, oldValue) => oldValue.AddMissingPositions(cCaseResults[elementId], positions)));
break;
}
}

return new BeamDisplacements(Cache.GetSubset(elementIds));
return new Element1dDisplacements(Cache.GetSubset(elementIds));
}
}
}
12 changes: 0 additions & 12 deletions GsaGH/Parameters/5_Results/3_Caches/IBeamResultCache.cs

This file was deleted.

13 changes: 13 additions & 0 deletions GsaGH/Parameters/5_Results/3_Caches/IElement1dResultCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace GsaGH.Parameters.Results {
public interface IElement1dResultCache<T1, T2, T3>
where T1 : IElement1dQuantity<T2> where T2 : IResultItem {
IApiResult ApiResult { get; }
ConcurrentDictionary<int, Collection<T1>> Cache { get; }
IElement1dResultSubset<T1, T2, T3> ResultSubset(ICollection<int> elementIds, int positionCount);
IElement1dResultSubset<T1, T2, T3> ResultSubset(ICollection<int> elementIds, ReadOnlyCollection<double> positions);
}
}
26 changes: 0 additions & 26 deletions GsaGH/Parameters/5_Results/4_Subsets/BeamDisplacements.cs

This file was deleted.

26 changes: 26 additions & 0 deletions GsaGH/Parameters/5_Results/4_Subsets/Element1dDisplacements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using OasysUnits;

namespace GsaGH.Parameters.Results {
public class Element1dDisplacements : IElement1dResultSubset<IElement1dDisplacement, IDisplacement, ResultVector6<Element1dExtremaKey>> {
public ResultVector6<Element1dExtremaKey> Max { get; private set; }
public ResultVector6<Element1dExtremaKey> Min { get; private set; }
public IList<int> Ids { get; private set; }

public ConcurrentDictionary<int, Collection<IElement1dDisplacement>> Subset { get; }
= new ConcurrentDictionary<int, Collection<IElement1dDisplacement>>();

public Element1dDisplacements(ConcurrentDictionary<int, Collection<IElement1dDisplacement>> results) {
Subset = results;
Ids = results.Keys.OrderBy(x => x).ToList();
(Max, Min) = results.BeamExtrema<IElement1dDisplacement, IDisplacement>();
}

public IDisplacement GetExtrema(Element1dExtremaKey key) {
return Subset[key.Id][key.Permutation].Results[key.Position];
}
}
}
48 changes: 48 additions & 0 deletions GsaGH/Parameters/5_Results/4_Subsets/Element1dResultsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using GsaAPI;
using System.Collections.ObjectModel;

namespace GsaGH.Parameters.Results {
public static class Element1dResultsFactory {
public static Collection<IElement1dDisplacement> CreateBeamDisplacements(
ReadOnlyCollection<Double6> results, ReadOnlyCollection<double> positions) {
return new Collection<IElement1dDisplacement> {
new Element1dDisplacement(results, positions)
};
}

public static Collection<IElement1dDisplacement> CreateBeamDisplacements(
ReadOnlyCollection<ReadOnlyCollection<Double6>> results, ReadOnlyCollection<double> positions) {
var permutations = new Collection<IElement1dDisplacement>();
foreach (ReadOnlyCollection<Double6> permutation in results) {
permutations.Add(new Element1dDisplacement(permutation, positions));
}
return permutations;
}

public static Collection<IElement1dDisplacement> AddMissingPositions(
this Collection<IElement1dDisplacement> existing,
ReadOnlyCollection<Double6> results, ReadOnlyCollection<double> positions) {
for (int i = 0; i < results.Count; i++) {
if (!existing[0].Results.ContainsKey(positions[i])) {
existing[0].Results.Add(positions[i], new Displacement(results[i]));
}
}

return existing;
}

public static Collection<IElement1dDisplacement> AddMissingPositions(
this Collection<IElement1dDisplacement> existing,
ReadOnlyCollection<ReadOnlyCollection<Double6>> results, ReadOnlyCollection<double> positions) {
for (int i = 0; i < existing.Count; i++) {
for (int j = 0; j < results.Count; j++) {
if (!existing[i].Results.ContainsKey(positions[j])) {
existing[i].Results.Add(positions[j], new Displacement(results[i][j]));
}
}
}

return existing;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
using System.Collections.ObjectModel;

namespace GsaGH.Parameters.Results {
public interface IBeamResultSubset<T1, T2> where T1 : IResultItem {
T2 Max { get; }
T2 Min { get; }
public interface IElement1dResultSubset<T1, T2, T3>
where T1 : IElement1dQuantity<T2>
where T2 : IResultItem {
T3 Max { get; }
T3 Min { get; }
IList<int> Ids { get; }
/// <summary>
/// <para> Key = Node Id
/// </para>
/// Value = Collection of results, one for each permutation. Collection will have 1 item in case of AnalysisCase
/// </summary>
ConcurrentDictionary<int, Collection<T1>> Subset { get; }
T1 GetExtrema(NodeExtremaKey key);
T2 GetExtrema(Element1dExtremaKey key);
}
}
3 changes: 1 addition & 2 deletions GsaGH/Parameters/5_Results/4_Subsets/INodeResultSubset.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace GsaGH.Parameters.Results {
Expand Down
Loading

0 comments on commit a533927

Please sign in to comment.