Skip to content

Commit

Permalink
cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristjan Nielsen committed Oct 22, 2023
1 parent 41f04c2 commit b80c07c
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,26 @@ namespace GsaGH.Parameters.Results {
public class AnalysisCaseNodeDisplacementCache : INodeResultCache<IDisplacement> {
public IApiResult ApiResult { get; set; }

Check warning on line 10 in GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs#L10

Added line #L10 was not covered by tests

// API Node results (will not be needed after GSA-7517)
internal Dictionary<string, ReadOnlyDictionary<int, NodeResult>> AnalysisCaseNodeResults { get; set; }
= new Dictionary<string, ReadOnlyDictionary<int, NodeResult>>();

public IDictionary<string, IResultSubset<IDisplacement>> Cache { get; set; }
= new Dictionary<string, IResultSubset<IDisplacement>>();
public ConcurrentDictionary<int, ICollection<IDisplacement>> Cache { get; }
= new ConcurrentDictionary<int, ICollection<IDisplacement>>();

Check warning on line 13 in GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs#L12-L13

Added lines #L12 - L13 were not covered by tests

internal AnalysisCaseNodeDisplacementCache(AnalysisCaseResult result) {
ApiResult = new AnalysisCaseApiResult(result);
}

Check warning on line 17 in GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs#L15-L17

Added lines #L15 - L17 were not covered by tests

public IResultSubset<IDisplacement> ResultSubset(string nodelist) {
if (nodelist.ToLower() == "all" || nodelist == string.Empty) {
nodelist = "All";
}

if (!Cache.ContainsKey(nodelist)) {
if (!AnalysisCaseNodeResults.ContainsKey(nodelist)) {
AnalysisCaseNodeResults.Add(nodelist, ((AnalysisCaseResult)ApiResult.Result).NodeResults(nodelist));
}

Cache.Add(nodelist, new GsaNodeDisplacements(AnalysisCaseNodeResults[nodelist]));
public IResultSubset<IDisplacement> ResultSubset(ICollection<int> nodeIds) {
ConcurrentBag<int> missingIds = Cache.GetMissingKeys(nodeIds);

Check warning on line 20 in GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs#L20

Added line #L20 was not covered by tests
if (missingIds.Count > 0) {
string nodelist = string.Join(" ", missingIds);
ReadOnlyDictionary<int, NodeResult> apiAnalysisCaseResults =
((AnalysisCaseResult)ApiResult.Result).NodeResults(nodelist);
Parallel.ForEach(missingIds, nodeId => {
var res = new GsaDisplacementQuantity(apiAnalysisCaseResults[nodeId].Displacement);
Cache.TryAdd(nodeId, new Collection<IDisplacement>() { res });
});

Check warning on line 28 in GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs#L22-L28

Added lines #L22 - L28 were not covered by tests
}

return Cache[nodelist];
return new GsaNodeDisplacements(Cache.GetSubset(nodeIds));

Check warning on line 31 in GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/AnalysisCaseNodeDisplacementCache.cs#L31

Added line #L31 was not covered by tests
}
}
}
28 changes: 28 additions & 0 deletions GsaGH/Parameters/5_Results/Caches/CacheUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace GsaGH.Parameters.Results {
public static class CacheUtility {
public static ConcurrentBag<int> GetMissingKeys<T>(
this IDictionary<int, T> existing, ICollection<int> newKeys) {
var missingIds = new ConcurrentBag<int>();
Parallel.ForEach(newKeys, key => {

Check warning on line 12 in GsaGH/Parameters/5_Results/Caches/CacheUtility.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CacheUtility.cs#L11-L12

Added lines #L11 - L12 were not covered by tests
if (!existing.ContainsKey(key)) {
missingIds.Add(key);
}
});

Check warning on line 16 in GsaGH/Parameters/5_Results/Caches/CacheUtility.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CacheUtility.cs#L14-L16

Added lines #L14 - L16 were not covered by tests

return missingIds;

Check warning on line 18 in GsaGH/Parameters/5_Results/Caches/CacheUtility.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CacheUtility.cs#L18

Added line #L18 was not covered by tests
}

public static ConcurrentDictionary<int, T> GetSubset<T>(
this IDictionary<int, T> dictionary, ICollection<int> keys) {
var subset = new ConcurrentDictionary<int, T>();
Parallel.ForEach(keys, key => subset.TryAdd(key, dictionary[key]));
return subset;

Check warning on line 25 in GsaGH/Parameters/5_Results/Caches/CacheUtility.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CacheUtility.cs#L23-L25

Added lines #L23 - L25 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,30 @@ namespace GsaGH.Parameters.Results {
public class CombinationCaseNodeDisplacementCache : INodeResultCache<IDisplacement> {
public IApiResult ApiResult { get; set; }

Check warning on line 10 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L10

Added line #L10 was not covered by tests

// API Node results (will not be needed after GSA-7517)
internal Dictionary<string, ReadOnlyDictionary<int, ReadOnlyCollection<NodeResult>>>
CombinationCaseNodeResults { get; set; }
= new Dictionary<string, ReadOnlyDictionary<int, ReadOnlyCollection<NodeResult>>>();

public IDictionary<string, IResultSubset<IDisplacement>> Cache { get; set; }
= new Dictionary<string, IResultSubset<IDisplacement>>();
public ConcurrentDictionary<int, ICollection<IDisplacement>> Cache { get; set; }
= new ConcurrentDictionary<int, ICollection<IDisplacement>>();

Check warning on line 13 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L12-L13

Added lines #L12 - L13 were not covered by tests

internal CombinationCaseNodeDisplacementCache(CombinationCaseResult result) {
ApiResult = new CombinationCaseApiResult(result);
}

Check warning on line 17 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L15-L17

Added lines #L15 - L17 were not covered by tests

public IResultSubset<IDisplacement> ResultSubset(string nodelist) {
if (nodelist.ToLower() == "all" || nodelist == string.Empty) {
nodelist = "All";
}

if (!Cache.ContainsKey(nodelist)) {
if (!CombinationCaseNodeResults.ContainsKey(nodelist)) {
CombinationCaseNodeResults.Add(nodelist, ((CombinationCaseResult)ApiResult.Result).NodeResults(nodelist));
}

Cache.Add(nodelist, new GsaNodeDisplacements(CombinationCaseNodeResults[nodelist]));
public IResultSubset<IDisplacement> ResultSubset(ICollection<int> nodeIds) {
ConcurrentBag<int> missingIds = Cache.GetMissingKeys(nodeIds);

Check warning on line 20 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L20

Added line #L20 was not covered by tests
if (missingIds.Count > 0) {
string nodelist = string.Join(" ", missingIds);
ReadOnlyDictionary<int, ReadOnlyCollection<NodeResult>> apiCombinationCaseResults =
((CombinationCaseResult)ApiResult.Result).NodeResults(nodelist);
Parallel.ForEach(missingIds, nodeId => {
var permutationResults = new Collection<IDisplacement>();

Check warning on line 26 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L22-L26

Added lines #L22 - L26 were not covered by tests
foreach (NodeResult permutationResult in apiCombinationCaseResults[nodeId]) {
permutationResults.Add(new GsaDisplacementQuantity(permutationResult.Displacement));
}

Cache.TryAdd(nodeId, permutationResults);
});

Check warning on line 32 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L28-L32

Added lines #L28 - L32 were not covered by tests
}

return Cache[nodelist];
return new GsaNodeDisplacements(Cache.GetSubset(nodeIds));

Check warning on line 35 in GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Caches/CombinationCaseNodeDisplacementCache.cs#L35

Added line #L35 was not covered by tests
}
}
}
7 changes: 4 additions & 3 deletions GsaGH/Parameters/5_Results/Caches/INodeResultCache.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace GsaGH.Parameters.Results {
public interface INodeResultCache<T> where T : IResultQuantitySet {
public IApiResult ApiResult { get; set; }
public IDictionary<string, IResultSubset<T>> Cache { get;}
public IResultSubset<T> ResultSubset(string list);
public ConcurrentDictionary<int, ICollection<T>> Cache { get; }
public IResultSubset<T> ResultSubset(ICollection<int> list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ internal GsaDisplacementQuantity(Double6 result) {
X = new Length(result.X, LengthUnit.Meter);
Y = new Length(result.Y, LengthUnit.Meter);
Z = new Length(result.Z, LengthUnit.Meter);
Xyz = ResultUtility.PythagoreanQuadruple(X, Y, Z);
Xyz = QuantityUtility.PythagoreanQuadruple(X, Y, Z);
Xx = CreateAngle(result.XX);
Yy = CreateAngle(result.YY);
Zz = CreateAngle(result.ZZ);
Xxyyzz = ResultUtility.PythagoreanQuadruple(Xx, Yy, Zz);
Xxyyzz = QuantityUtility.PythagoreanQuadruple(Xx, Yy, Zz);
}

Check warning on line 41 in GsaGH/Parameters/5_Results/Quantities/GsaDisplacementQuantity.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Quantities/GsaDisplacementQuantity.cs#L32-L41

Added lines #L32 - L41 were not covered by tests

private Angle CreateAngle(double val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Linq;

namespace GsaGH.Parameters.Results {
public static class ResultUtility {
public static IDisplacement GetMax(this ICollection<Collection<IDisplacement>> c) {
public static class QuantityUtility {
public static IDisplacement GetMax(this ICollection<ICollection<IDisplacement>> c) {
return new GsaDisplacementQuantity(
c.Select(c => c.Select(p => p.X).Max()).Max(),
c.Select(c => c.Select(p => p.Y).Max()).Max(),
Expand All @@ -20,7 +20,7 @@ public static IDisplacement GetMax(this ICollection<Collection<IDisplacement>> c
c.Select(c => c.Select(p => p.Xxyyzz).Max()).Max());

Check warning on line 20 in GsaGH/Parameters/5_Results/Quantities/QuantityUtility.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Quantities/QuantityUtility.cs#L12-L20

Added lines #L12 - L20 were not covered by tests
}

public static IDisplacement GetMin(this ICollection<Collection<IDisplacement>> c) {
public static IDisplacement GetMin(this ICollection<ICollection<IDisplacement>> c) {
return new GsaDisplacementQuantity(
c.Select(c => c.Select(p => p.X).Min()).Min(),
c.Select(c => c.Select(p => p.Y).Min()).Min(),
Expand Down
28 changes: 4 additions & 24 deletions GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,14 @@ public class GsaNodeDisplacements : IResultSubset<IDisplacement> {
public List<int> Ids => Results.Keys.OrderBy(x => x).ToList();

Check warning on line 12 in GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs#L10-L12

Added lines #L10 - L12 were not covered by tests

/// <summary>
/// Combination Case Node Displacement Result VALUES Dictionary
/// Append to this dictionary to chache results
/// key = nodeId
/// value = Collection of permutations(permutationsResults) ei Collection will have 1 item in case of AnalysisCase
/// </summary>
public ConcurrentDictionary<int, Collection<IDisplacement>> Results { get; }
= new ConcurrentDictionary<int, Collection<IDisplacement>>();
public ConcurrentDictionary<int, ICollection<IDisplacement>> Results { get; }
= new ConcurrentDictionary<int, ICollection<IDisplacement>>();

Check warning on line 19 in GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs#L18-L19

Added lines #L18 - L19 were not covered by tests

internal GsaNodeDisplacements(ReadOnlyDictionary<int, NodeResult> apiAnalysisCaseResults) {
Parallel.ForEach(apiAnalysisCaseResults.Keys, nodeId => {
var res = new GsaDisplacementQuantity(apiAnalysisCaseResults[nodeId].Displacement);
Results.TryAdd(nodeId, new Collection<IDisplacement>() { res });
});

Max = Results.Values.GetMax();
Min = Results.Values.GetMin();
}

internal GsaNodeDisplacements(ReadOnlyDictionary<int, ReadOnlyCollection<NodeResult>> apiCombinationCaseResults) {
Parallel.ForEach(apiCombinationCaseResults.Keys, nodeId => {
var permutationResults = new Collection<IDisplacement>();
foreach (NodeResult permutationResult in apiCombinationCaseResults[nodeId]) {
permutationResults.Add(new GsaDisplacementQuantity(permutationResult.Displacement));
}

Results.TryAdd(nodeId, permutationResults);
});

internal GsaNodeDisplacements(ConcurrentDictionary<int, ICollection<IDisplacement>> results) {
Results = results;
Max = Results.Values.GetMax();
Min = Results.Values.GetMin();
}

Check warning on line 25 in GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/ResultSubsets/GsaNodeDisplacements.cs#L21-L25

Added lines #L21 - L25 were not covered by tests
Expand Down
3 changes: 1 addition & 2 deletions GsaGH/Parameters/5_Results/ResultSubsets/IResultSubset.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace GsaGH.Parameters.Results {
public interface IResultSubset<T> where T : IResultQuantitySet {
public T Max { get; }
public T Min { get; }
public List<int> Ids { get; }
public ConcurrentDictionary<int, Collection<T>> Results { get; }
public ConcurrentDictionary<int, ICollection<T>> Results { get; }
}
}
10 changes: 7 additions & 3 deletions GsaGH/Parameters/5_Results/Results/GsaResult2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ internal Dictionary<string, ReadOnlyDictionary<int, ReadOnlyCollection<NodeResul
public List<int> SelectedPermutationIds { get; set; }
public CaseType CaseType { get; set; }

Check warning on line 31 in GsaGH/Parameters/5_Results/Results/GsaResult2.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Results/GsaResult2.cs#L30-L31

Added lines #L30 - L31 were not covered by tests

public GsaResult2() { }

internal GsaResult2(GsaModel model, AnalysisCaseResult result, int caseId) {
Model = model;
AnalysisCaseResult = result;
Expand Down Expand Up @@ -72,7 +70,13 @@ public override string ToString() {
}

internal GsaNodeDisplacements NodeDisplacementValues(string nodelist) {
return (GsaNodeDisplacements)NodeDisplacements.ResultSubset(nodelist);
var entityList = new EntityList() {
Definition = nodelist,
Type = GsaAPI.EntityType.Node,
Name = "tmp"
};
ReadOnlyCollection<int> nodeIds = Model.Model.ExpandList(entityList);
return (GsaNodeDisplacements)NodeDisplacements.ResultSubset(nodeIds);

Check warning on line 79 in GsaGH/Parameters/5_Results/Results/GsaResult2.cs

View check run for this annotation

Codecov / codecov/patch

GsaGH/Parameters/5_Results/Results/GsaResult2.cs#L73-L79

Added lines #L73 - L79 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void GsaNodeDisplacementsValuesFromAnalysisCaseTest(GsaNodeDisplacementCo
// Assert result values
int i = 0;
foreach (int id in resultSet.Ids) {
Collection<IDisplacement> displacementQuantity = resultSet.Results[id];
var displacementQuantity = (Collection<IDisplacement>)resultSet.Results[id];

// for analysis case results we expect only one value in the collection
Assert.Single(displacementQuantity);
Expand Down Expand Up @@ -191,7 +191,7 @@ public void GsaNodeDisplacementsValuesFromCombinationCaseTest(GsaNodeDisplacemen
// Assert result values
int i = 0;
foreach (int id in resultSet.Ids) {
Collection<IDisplacement> displacementQuantity = resultSet.Results[id];
var displacementQuantity = (Collection<IDisplacement>)resultSet.Results[id];

// for C4 case results we expect two permutations in the collection
Assert.Equal(2, displacementQuantity.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void PythagoreanQuadrupleTest(double a, double b, double c, Enum unit) {
IQuantity x = Quantity.From(a, unit);
IQuantity y = Quantity.From(b, unit);
IQuantity z = Quantity.From(c, unit);
IQuantity quantity = ResultUtility.PythagoreanQuadruple(x, y, z);
IQuantity quantity = QuantityUtility.PythagoreanQuadruple(x, y, z);

double pyth = Math.Sqrt((a * a) + (b * b) + (c * c));
IQuantity expected = Quantity.From(pyth, unit);
Expand Down

0 comments on commit b80c07c

Please sign in to comment.