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

Make use of caching system in base adapter #277

Merged
merged 5 commits into from
Jan 27, 2023
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
8 changes: 6 additions & 2 deletions GSA_Adapter/CRUD/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,24 @@ protected override bool ICreate<T>(IEnumerable<T> objects, ActionConfig actionCo

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

#if GSA_10_1
private bool CreateMaterials(IEnumerable<IMaterialFragment> materials)
{
bool success = true;
foreach (IMaterialFragment material in materials)
{
int index = m_gsaCom.GwaCommand("HIGHEST, " + Convert.ToGsaString(material.GetType())) + 1;
this.SetAdapterId(material, index);
string type = Convert.ToGsaString(material.GetType());
int index = m_gsaCom.GwaCommand("HIGHEST, " + type) + 1;
this.SetAdapterId(material, $"{type.Split('_').Last()}:{index}");
success &= ComCall(Convert.IToGsaString(material, index.ToString()));
}
return success;
}

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

#endif

private bool CreateObject(BH.oM.Base.IBHoMObject obj)
{
return ComCall(Convert.IToGsaString(obj, GetAdapterId<int>(obj).ToString()));
Expand Down
93 changes: 31 additions & 62 deletions GSA_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,7 @@ protected override IEnumerable<IBHoMObject> IRead(Type type, IList indices, Acti

public List<IMaterialFragment> ReadMaterials(List<string> ids = null)
{
return ReadMaterials(ids, false);
}

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

public List<IMaterialFragment> ReadMaterials(List<string> ids = null, bool includeStandard = false)
{
List<IMaterialFragment> materials = ReadMaterialDictionary(ids, includeStandard).Select(x => x.Value).ToList();

if (ids != null && ids.Count != 0)
materials = materials.Where(x => ids.Contains(x.GSAId().ToString())).ToList();

return materials;
}

public Dictionary<string, IMaterialFragment> ReadMaterialDictionary(List<string> ids = null, bool includeStandard = false)
{
//List<IMaterialFragment> materials = ReadMaterials(null, false);
List<IMaterialFragment> materials = new List<IMaterialFragment>();
List<string> keys = new List<string>();
Dictionary<string, IMaterialFragment> materialDictionary = new Dictionary<string, IMaterialFragment>();

#if GSA_10_1
string allProps = m_gsaCom.GwaCommand("GET_ALL, MAT_ANAL.1").ToString();
Expand All @@ -141,31 +121,33 @@ public Dictionary<string, IMaterialFragment> ReadMaterialDictionary(List<string>
string allProps = m_gsaCom.GwaCommand("GET_ALL, MAT").ToString();
#endif
string[] matArr = string.IsNullOrWhiteSpace(allProps) ? new string[0] : allProps.Split('\n');
matArr = matArr.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
matArr = matArr.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToArray();

if (ids == null)
materials = matArr.Select(x => Convert.FromGsaMaterial(x)).Where(x => x != null).ToList();
else
materials = matArr.Where(x => ids.Contains(x.Split(',')[1])).Select(x => Convert.FromGsaMaterial(x)).Where(x => x != null).ToList();

if (includeStandard)
materials.AddRange(GetStandardGsaMaterials());
return materials;
}

for (int i = 0; i < materials.Count(); i++)
{
/***************************************************/

public Dictionary<string, IMaterialFragment> ReadMaterialDictionary(List<string> ids = null, bool includeStandard = false)
{
#if GSA_10_1
string key = matArr[i].Split(("_.").ToCharArray())[1] + ":" + materials[i].GSAId();
alelom marked this conversation as resolved.
Show resolved Hide resolved
Dictionary<string, IMaterialFragment> materials = GetCachedOrReadAsDictionary<string, IMaterialFragment>(ids);
#else
string key = GetAdapterId(materials[i]).ToString();
Dictionary<string, IMaterialFragment> materials = GetCachedOrReadAsDictionary<int, IMaterialFragment>(ids?.Select(x => int.Parse(x)).ToList()).ToDictionary(x => x.Key.ToString(), x => x.Value);
#endif
if (!keys.Contains(key))
if (includeStandard)
{
foreach (IMaterialFragment standardMaterial in GetStandardGsaMaterials())
{
keys.Add(key);
materialDictionary.Add(key, materials[i]);
materials[standardMaterial.Name] = standardMaterial;
}
}

return materialDictionary;
return materials;
}

/***************************************************/
Expand All @@ -189,22 +171,16 @@ public List<Loadcase> ReadLoadCases(List<string> ids = null)

public List<Bar> ReadBars(List<string> ids = null)
{
//int[] potentialBeamRefs = GenerateIndices(ids, typeof(Bar));

//GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
//m_gsaCom.Elements(potentialBeamRefs, out gsaElements);
#if GSA_10_1
string allNodes = m_gsaCom.GwaCommand("GET_ALL, EL.4").ToString();
string allBars = m_gsaCom.GwaCommand("GET_ALL, EL.4").ToString();
#else
string allNodes = m_gsaCom.GwaCommand("GET_ALL, EL.2").ToString();
string allBars = m_gsaCom.GwaCommand("GET_ALL, EL.2").ToString();
#endif
string[] barArr = string.IsNullOrWhiteSpace(allNodes) ? new string[0] : allNodes.Split('\n');
string[] barArr = string.IsNullOrWhiteSpace(allBars) ? new string[0] : allBars.Split('\n');

List<ISectionProperty> secPropList = ReadSectionProperties();
List<Node> nodeList = ReadNodes();

Dictionary<string, ISectionProperty> secProps = secPropList.Where(x => x != null).ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, ISectionProperty> secProps = GetCachedOrReadAsDictionary<int, ISectionProperty>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

return Convert.FromGsaBars(barArr, secProps, nodes, ids);
}
Expand All @@ -224,8 +200,7 @@ public List<LoadCombination> ReadLoadCombinations(List<string> ids = null)
analList.Add(anal);
}

List<Loadcase> lCaseList = ReadLoadCases();
Dictionary<string, Loadcase> lCases = lCaseList.ToDictionary(x => x.Number.ToString());
Dictionary<int, Loadcase> lCases = GetCachedOrReadAsDictionary<int, Loadcase>();

if (ids == null)
lComabinations = analList.Select(x => Convert.FromGsaAnalTask(x, lCases)).ToList();
Expand Down Expand Up @@ -366,11 +341,8 @@ public List<FEMesh> ReadFEMesh(List<string> ids = null)
GsaElement[] gsaElements = new GsaElement[potentialMeshRefs.Length];
m_gsaCom.Elements(potentialMeshRefs, out gsaElements);

List<ISurfaceProperty> secPropList = ReadProperty2d();
List<Node> nodeList = ReadNodes();

Dictionary<string, ISurfaceProperty> props = secPropList.Where(x => x != null).ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, ISurfaceProperty> props = GetCachedOrReadAsDictionary<int, ISurfaceProperty>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

return Convert.FromGsaFEMesh(gsaElements, props, nodes);
}
Expand All @@ -392,11 +364,8 @@ public List<LinkConstraint> ReadLinkConstraint(List<string> ids = null)

public List<RigidLink> ReadRigidLink(List<string> ids = null)
{
List<LinkConstraint> constraintList = ReadLinkConstraint(null);
List<Node> nodeList = ReadNodes();

Dictionary<string, LinkConstraint> constraints = constraintList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, LinkConstraint> constraints = GetCachedOrReadAsDictionary<int, LinkConstraint>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

int[] potentialBeamRefs = GenerateIndices(ids, typeof(RigidLink));
GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
Expand All @@ -414,19 +383,14 @@ public List<RigidLink> ReadRigidLink(List<string> ids = null)

public List<Spacer> ReadSpacers(List<string> ids = null)
{
List<Node> nodeList = ReadNodes();
List<SpacerProperty> spacerProps = ReadSpacerProperties();

Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, SpacerProperty> secProps = spacerProps.Where(x => x != null).ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();
Dictionary<int, SpacerProperty> secProps = GetCachedOrReadAsDictionary<int,SpacerProperty>();

int[] potentialBeamRefs = GenerateIndices(ids, typeof(Spacer));
GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
m_gsaCom.Elements(potentialBeamRefs, out gsaElements);

return Convert.FromGsaSpacers(gsaElements, secProps, nodes);


}

/***************************************/
Expand Down Expand Up @@ -469,6 +433,7 @@ private int[] GenerateIndices(List<string> ids, Type elementType)

private List<IMaterialFragment> GetStandardGsaMaterials()
{
#if !GSA_10_1
List<IMaterialFragment> materials = new List<IMaterialFragment>();
materials.Add(new Steel() { Name = "STEEL" });
materials.Add(new Concrete() { Name = "CONC_SHORT" });
Expand All @@ -481,6 +446,10 @@ private List<IMaterialFragment> GetStandardGsaMaterials()
}

return materials;
#else
return new List<IMaterialFragment>();
#endif

}

/***************************************************/
Expand Down
14 changes: 14 additions & 0 deletions GSA_Adapter/CRUD/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
using BH.Engine.Adapter;
using BH.oM.Adapters.GSA;
using BH.Engine.Adapters.GSA;
using BH.oM.Structure.MaterialFragments;
using System.Linq;

namespace BH.Adapter.GSA
{
Expand Down Expand Up @@ -73,6 +75,18 @@ private bool Update(IEnumerable<FEMesh> objects, ActionConfig actionConfig = nul
}

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

#if GSA_10_1
private bool Update(IEnumerable<IMaterialFragment> materials, ActionConfig actionConfig = null)
{
bool success = true;
foreach (IMaterialFragment material in materials)
{
success &= ComCall(Convert.IToGsaString(material, material.MaterialId()));
}
return success;
}
#endif
}
}

Expand Down
16 changes: 8 additions & 8 deletions GSA_Adapter/Convert/FromGsa/Elements/Bar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Dictionary<string, ISectionProperty> secProps, Dictionary<string, Node> nodes)
public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Dictionary<int, ISectionProperty> secProps, Dictionary<int, Node> nodes)
{
List<Bar> barList = new List<Bar>();

Expand Down Expand Up @@ -71,8 +71,8 @@ public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Diction
}

Node n1, n2;
nodes.TryGetValue(gsaBar.Topo[0].ToString(), out n1);
nodes.TryGetValue(gsaBar.Topo[1].ToString(), out n2);
nodes.TryGetValue(gsaBar.Topo[0], out n1);
nodes.TryGetValue(gsaBar.Topo[1], out n2);

Bar bar = new Bar { StartNode = n1, EndNode = n2 };
bar.ApplyTaggedName(gsaBar.Name);
Expand All @@ -82,7 +82,7 @@ public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Diction
bar.OrientationAngle = gsaBar.Beta;

ISectionProperty prop;
secProps.TryGetValue(gsaBar.Property.ToString(), out prop);
secProps.TryGetValue(gsaBar.Property, out prop);

bar.SectionProperty = prop;

Expand All @@ -97,7 +97,7 @@ public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Diction

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

public static List<Bar> FromGsaBars(IEnumerable<string> gsaStrings, Dictionary<string, ISectionProperty> secProps, Dictionary<string, Node> nodes, List<string> ids)
public static List<Bar> FromGsaBars(IEnumerable<string> gsaStrings, Dictionary<int, ISectionProperty> secProps, Dictionary<int, Node> nodes, List<string> ids)
{
List<Bar> barList = new List<Bar>();

Expand Down Expand Up @@ -135,15 +135,15 @@ public static List<Bar> FromGsaBars(IEnumerable<string> gsaStrings, Dictionary<s

Bar bar = new Bar()
{
StartNode = nodes[arr[7]],
EndNode = nodes[arr[8]],
StartNode = nodes[int.Parse(arr[7])],
EndNode = nodes[int.Parse(arr[8])],
FEAType = feType,
};

bar.ApplyTaggedName(arr[2]);

ISectionProperty prop;
if (secProps.TryGetValue(arr[5], out prop))
if (secProps.TryGetValue(int.Parse(arr[5]), out prop))
bar.SectionProperty = prop;

if (arr.Length > 10)
Expand Down
6 changes: 3 additions & 3 deletions GSA_Adapter/Convert/FromGsa/Elements/FEMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<FEMesh> FromGsaFEMesh(IEnumerable<GsaElement> gsaElements, Dictionary<string, ISurfaceProperty> props, Dictionary<string, Node> nodes)
public static List<FEMesh> FromGsaFEMesh(IEnumerable<GsaElement> gsaElements, Dictionary<int, ISurfaceProperty> props, Dictionary<int, Node> nodes)
{
List<FEMesh> meshList = new List<FEMesh>();

Expand All @@ -61,12 +61,12 @@ public static List<FEMesh> FromGsaFEMesh(IEnumerable<GsaElement> gsaElements, Di
FEMeshFace face = new FEMeshFace() { NodeListIndices = Enumerable.Range(0, gsaMesh.NumTopo).ToList(), OrientationAngle = gsaMesh.Beta * System.Math.PI / 180 };
face.SetAdapterId(typeof(GSAId), id);
ISurfaceProperty property;
props.TryGetValue(gsaMesh.Property.ToString(), out property);
props.TryGetValue(gsaMesh.Property, out property);

FEMesh mesh = new FEMesh()
{
Faces = new List<FEMeshFace>() { face },
Nodes = gsaMesh.Topo.Select(x => nodes[x.ToString()]).ToList(),
Nodes = gsaMesh.Topo.Select(x => nodes[x]).ToList(),
Property = property
};

Expand Down
16 changes: 8 additions & 8 deletions GSA_Adapter/Convert/FromGsa/Elements/RigidLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaElements, Dictionary<string, LinkConstraint> constraints, Dictionary<string, Node> nodes)
public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaElements, Dictionary<int, LinkConstraint> constraints, Dictionary<int, Node> nodes)
{
List<RigidLink> linkList = new List<RigidLink>();

Expand All @@ -50,17 +50,17 @@ public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaEleme
if (gsaLink.eType != 9)
continue;

RigidLink face = new RigidLink()
RigidLink link = new RigidLink()
{
PrimaryNode = nodes[gsaLink.Topo[0].ToString()],
SecondaryNodes = new List<Node> { nodes[gsaLink.Topo[1].ToString()] },
Constraint = constraints[gsaLink.Property.ToString()]
PrimaryNode = nodes[gsaLink.Topo[0]],
SecondaryNodes = new List<Node> { nodes[gsaLink.Topo[1]] },
Constraint = constraints[gsaLink.Property]
};

face.ApplyTaggedName(gsaLink.Name);
link.ApplyTaggedName(gsaLink.Name);
int id = gsaLink.Ref;
face.SetAdapterId(typeof(GSAId), id);
linkList.Add(face);
link.SetAdapterId(typeof(GSAId), id);
linkList.Add(link);

}
return linkList;
Expand Down
12 changes: 5 additions & 7 deletions GSA_Adapter/Convert/FromGsa/Elements/Spacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<Spacer> FromGsaSpacers(IEnumerable<GsaElement> gsaElements, Dictionary<string, SpacerProperty> spaProps, Dictionary<string, Node> nodes)
public static List<Spacer> FromGsaSpacers(IEnumerable<GsaElement> gsaElements, Dictionary<int, SpacerProperty> spaProps, Dictionary<int, Node> nodes)
{
List<Spacer> spacerList = new List<Spacer>();

Expand All @@ -56,16 +56,14 @@ public static List<Spacer> FromGsaSpacers(IEnumerable<GsaElement> gsaElements, D
continue;

Node n1, n2;
nodes.TryGetValue(gsaSpacer.Topo[0].ToString(), out n1);
nodes.TryGetValue(gsaSpacer.Topo[1].ToString(), out n2);
nodes.TryGetValue(gsaSpacer.Topo[0], out n1);
nodes.TryGetValue(gsaSpacer.Topo[1], out n2);

Spacer spacer = new Spacer { StartNode = n1, EndNode = n2 };
spacer.ApplyTaggedName(gsaSpacer.Name);



SpacerProperty prop;
spaProps.TryGetValue(gsaSpacer.Property.ToString(), out prop);
SpacerProperty prop;
spaProps.TryGetValue(gsaSpacer.Property, out prop);

spacer.SpacerProperty = prop;

Expand Down
4 changes: 2 additions & 2 deletions GSA_Adapter/Convert/FromGsa/Loads/LoadCombination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static LoadCombination FromGsaAnalTask(string gsaString, Dictionary<string, Loadcase> lCases)
public static LoadCombination FromGsaAnalTask(string gsaString, Dictionary<int, Loadcase> lCases)
{

if (string.IsNullOrWhiteSpace(gsaString))
Expand All @@ -61,7 +61,7 @@ public static LoadCombination FromGsaAnalTask(string gsaString, Dictionary<strin
lCaseParam[0] = "1.0";

Loadcase templCase;
if (!lCases.TryGetValue(lCaseParam[1], out templCase))
if (!lCases.TryGetValue(int.Parse(lCaseParam[1]), out templCase))
{
templCase = new Loadcase { Number = int.Parse(lCaseParam[1]), Nature = LoadNature.Other };
templCase.SetAdapterId(typeof(GSAId), templCase.Number);
Expand Down
Loading