public static Model GetRandomFrameModel(int n = 50) { var buf = new Model(); var nodes = new Node[n]; for (var i = 0; i < n; i++) { nodes[i] = new Node(GetRandomNumber(0, 10), GetRandomNumber(0, 10), GetRandomNumber(0, 10)); nodes[i].Constraints = GetRandomConstraint(); } buf.Nodes.Add(nodes); for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { if (i == j) { continue; } var elm = new FrameElement2Node(nodes[i], nodes[j]); elm.A = GetRandomNumber(); elm.Iy = GetRandomNumber(); elm.Iz = GetRandomNumber(); elm.J = GetRandomNumber(); elm.E = GetRandomNumber(); elm.G = GetRandomNumber(); var ld = new UniformLoad1D(GetRandomNumber(100, 1000), LoadDirection.Z, CoordinationSystem.Global); elm.Loads.Add(ld); buf.Elements.Add(elm); } } for (int i = 0; i < n; i++) { nodes[i].Loads.Add(new NodalLoad(GetRandomForce(1000, 10000))); } return(buf); }
public static void AddRandomiseLoading(Model mdl, bool addNodalLoads, bool addElementLoads, params LoadCase[] cases) { if (addNodalLoads) { foreach (var nde in mdl.Nodes) { foreach (var cse in cases) { nde.Loads.Add(new NodalLoad(RandomStuff.GetRandomForce(-1000, 1000), cse)); } } } if (addElementLoads) { foreach (var elm in mdl.Elements) { foreach (var cse in cases) { var uniformLoad = new UniformLoad1D(rnd.GetRandomNumber(-1000, 1000), LoadDirection.X, CoordinationSystem.Global, cse); var l = (elm.Nodes[0].Location - elm.Nodes[1].Location).Length; var concenstratedLoad = new ConcentratedLoad1D(rnd.GetRandomForce(-1000, 1000), rnd.GetRandomNumber(0, l), CoordinationSystem.Global, cse); elm.Loads.Add(uniformLoad); elm.Loads.Add(concenstratedLoad); } } } }
/// <summary> /// Run an analysis on the specified model /// </summary> /// <param name="model"></param> /// <param name="alertLog"></param> /// <returns></returns> public ModelResults AnalyseModel(Model.Model model, AnalysisCaseCollection cases, AlertLog alertLog = null) { alertLog?.RaiseAlert("BLD", "Building analysis model..."); var results = new ModelResults(); // Create model: var feModel = new BFE.Model(); // Create nodes: var nodesMap = new Dictionary <Guid, BFE.Node>(); foreach (var node in model.Nodes) { var bfeNode = ToBFE.Convert(node); feModel.Nodes.Add(bfeNode); nodesMap.Add(node.GUID, bfeNode); } // Create materials: /*var materialsMap = new Dictionary<Guid, BFE.Materials.BaseMaterial>(); * foreach (var material in model.Materials) * { * var bfeMat = ToBFE.Convert(material); * materialsMap.Add(material.GUID, bfeMat); * }*/ // Create sections: /*foreach (var family in model.Families) * { * if (family is SectionFamily) * { * SectionFamily section = (SectionFamily)family; * //TODO? * } * }*/ // Create elements: var elementMap = new Dictionary <Guid, BFE.FrameElement2Node>(); foreach (var element in model.Elements) { if (element is LinearElement) { var linEl = (LinearElement)element; if (linEl.StartNode != null && linEl.EndNode != null && linEl.Family != null) { var el = new BFE.FrameElement2Node( nodesMap[linEl.StartNode.GUID], nodesMap[linEl.EndNode.GUID]); //TODO: Releases //TODO: Orientation //TODO: Offsets if (linEl.Family.Profile?.Perimeter != null) { var profile = ToBFE.Convert(linEl.Family.Profile.Perimeter); el.Geometry = profile; el.UseOverridedProperties = false; //TODO: Hollow sections } el.E = linEl.Family.GetPrimaryMaterial()?.GetE(Geometry.Direction.X) ?? 210e9; feModel.Elements.Add(el); elementMap.Add(element.GUID, el); } else { alertLog.RaiseAlert("INCOMPLETE DATA", linEl, "Incomplete data. Will be excluded from analysis."); } } } //Loading time! // TODO: Limit to specific load cases? foreach (var load in model.Loads) { if (load is LinearElementLoad) { var lEL = (LinearElementLoad)load; if (lEL.IsMoment) { alertLog.RaiseAlert("MOMENTS UNSUPPORTED", load, "Moment line loads are not supported.", AlertLevel.Error); } else { // TODO: Set load case var bLoad = new BFE.UniformLoad1D(lEL.Value, ToBFE.Convert(lEL.Direction), ToBFE.Convert(lEL.Axes)); // TODO: Generalise var elements = lEL.AppliedTo.Items; foreach (var el in elements) { elementMap[el.GUID].Loads.Add(bLoad); } } } else if (load is NodeLoad) { var nL = (NodeLoad)load; } else { alertLog.RaiseAlert("LOAD TYPE UNSUPPORTED", load, "Load type is not supported.", AlertLevel.Error); } } alertLog?.RaiseAlert("BLD", "Analysis model built."); alertLog.RaiseAlert("SLV", "Solving..."); feModel.Solve(); alertLog.RaiseAlert("SLV", "Solved."); foreach (var kvp in nodesMap) { var disp = kvp.Value.GetNodalDisplacement(); var nR = new NodeResults(); var cNR = new CaseNodeResults(); //nR.Add(cNR); } /*foreach (var element in model.Elements) * { * bNS = nodesMap[element] * }*/ return(results); }
public static Model ReadFromXml(Stream stream) { var doc = new XmlDocument(); doc.Load(stream); var buf = new Model(); var nodesList = new List <Node>(); var nodesDictionary = new Dictionary <string, Node>(); #region nodes var nodes = doc.SelectNodes("Model/Nodes/Node"); if (nodes == null) { throw new Exception("Model/Nodes/Node element not found"); } foreach (XmlElement node in nodes) { var x = node.GetAttributeValue <double>("X"); var y = node.GetAttributeValue <double>("Y"); var z = node.GetAttributeValue <double>("Z"); #region fixity var dxFix = node.GetAttributeValue <bool>("DxFix"); var dyFix = node.GetAttributeValue <bool>("DyFix"); var dzFix = node.GetAttributeValue <bool>("DzFix"); var rxFix = node.GetAttributeValue <bool>("RxFix"); var ryFix = node.GetAttributeValue <bool>("RyFix"); var rzFix = node.GetAttributeValue <bool>("RzFix"); var lbl = node.GetAttributeValue("Label"); var nde = new Node(x.Value, y.Value, z.Value); var cns = Constraint.Released; if (dxFix.HasValue) { cns.DX = dxFix.Value ? DofConstraint.Fixed : DofConstraint.Released; } if (dyFix.HasValue) { cns.DY = dyFix.Value ? DofConstraint.Fixed : DofConstraint.Released; } if (dzFix.HasValue) { cns.DZ = dzFix.Value ? DofConstraint.Fixed : DofConstraint.Released; } if (rxFix.HasValue) { cns.RX = rxFix.Value ? DofConstraint.Fixed : DofConstraint.Released; } if (ryFix.HasValue) { cns.RY = ryFix.Value ? DofConstraint.Fixed : DofConstraint.Released; } if (rzFix.HasValue) { cns.RZ = rzFix.Value ? DofConstraint.Fixed : DofConstraint.Released; } nde.Constraints = cns; #endregion #region settlement var dxSl = node.GetAttributeValue <double>("DxStl"); var dySl = node.GetAttributeValue <double>("DxStl"); var dzSl = node.GetAttributeValue <double>("DxStl"); var rxSl = node.GetAttributeValue <double>("DxStl"); var rySl = node.GetAttributeValue <double>("DxStl"); var rzSl = node.GetAttributeValue <double>("DxStl"); var settl = new Displacement(); if (dxSl.HasValue) { settl.DX = dxSl.Value; } if (dySl.HasValue) { settl.DY = dySl.Value; } if (dzSl.HasValue) { settl.DZ = dzSl.Value; } if (rxSl.HasValue) { settl.RX = rxSl.Value; } if (rySl.HasValue) { settl.RY = rySl.Value; } if (rzSl.HasValue) { settl.RZ = rzSl.Value; } nde.Settlements = settl; #endregion var tag = node.GetAttributeValue("Tag"); if (tag != null) { nde.Tag = tag; } if (lbl != null) { nde.Label = lbl; if (nodesDictionary.ContainsKey(lbl)) { throw new Exception("duplicated label for nodes: " + lbl); } nodesDictionary[lbl] = nde; } nodesList.Add(nde); } #endregion var elementList = new List <Element>(); var elementDictionary = new Dictionary <string, Element>(); var frmElms = doc.SelectNodes("Model/Elements/FrameElement2Node"); #region frame elements if (frmElms != null) { foreach (XmlElement node in frmElms) { var elm = new FrameElement2Node(); var n1Index = node.GetAttributeValue <int>("Node1Index"); var n2Index = node.GetAttributeValue <int>("Node2Index"); var n1Label = node.GetAttributeValue("Node1Label"); var n2Label = node.GetAttributeValue("Node2Label"); var iy = node.GetAttributeValue <double>("Iy"); var iz = node.GetAttributeValue <double>("Iz"); var j = node.GetAttributeValue <double>("J"); var a = node.GetAttributeValue <double>("A"); var ay = node.GetAttributeValue <double>("Ay"); var az = node.GetAttributeValue <double>("Az"); var density = node.GetAttributeValue <double>("Density"); var hing1 = node.GetAttributeValue <bool>("HingedAtStart"); var hing2 = node.GetAttributeValue <bool>("HingedAtEnd"); var webRot = node.GetAttributeValue <double>("WebRoation"); var label = node.GetAttributeValue("Label"); var tag = node.GetAttributeValue("Tag"); var shearEffect = node.GetAttributeValue <bool>("ConsiderShearEffect"); if (n1Index == null && n1Label == null) { throw new Exception("Neither Node1Index and Node1Label are defined"); } if (n2Index == null && n2Label == null) { throw new Exception("Neither Node2Index and Node2Label are defined"); } elm.Iy = iy.Value; elm.Iz = iz.Value; elm.J = j.Value; elm.A = a.Value; if (label != null) { elm.Label = label; elementDictionary[label] = elm; } if (tag != null) { elm.Tag = tag; } if (ay.HasValue) { elm.Ay = ay.Value; } if (az.HasValue) { elm.Az = az.Value; } if (hing1.HasValue) { elm.HingedAtStart = hing1.Value; } if (hing2.HasValue) { elm.HingedAtEnd = hing2.Value; } elm.MassDensity = density.Value; elm.UseOverridedProperties = true; if (shearEffect.HasValue) { elm.ConsiderShearDeformation = shearEffect.Value; } if (webRot.HasValue) { elm.WebRotation = webRot.Value; } if (n1Index.HasValue) { elm.StartNode = nodesList[n1Index.Value]; } else { if (n1Label == null) { throw new Exception(); } if (!nodesDictionary.ContainsKey(n1Label)) { throw new Exception("node not exists, label: " + n1Label); } elm.StartNode = nodesDictionary[n1Label]; } if (n2Index.HasValue) { elm.EndNode = nodesList[n2Index.Value]; } else { if (n2Label == null) { throw new Exception(); } if (!nodesDictionary.ContainsKey(n2Label)) { throw new Exception("node not exists, label: " + n1Label); } elm.EndNode = nodesDictionary[n2Label]; } elementList.Add(elm); } } #endregion var rgdElms = doc.SelectNodes("Model/Elements/RigidElement"); #region rigid elements foreach (XmlElement node in rgdElms) { var rElm = new RigidElement(); var innerNodes = node.SelectNodes("/Node"); var label = node.GetAttributeValue("Label"); if (label != null) { rElm.Label = label; } var tag = node.GetAttributeValue("Tag"); if (tag != null) { rElm.Tag = tag; } foreach (XmlElement innerNode in innerNodes) { var num = innerNode.GetAttributeValue <int>("Index"); var lbl = innerNode.GetAttributeValue("Label"); var isCenter = innerNode.GetAttributeValue <bool>("IsCenter"); if (lbl == null && !num.HasValue) { continue; } Node targetNode; if (num.HasValue) { rElm.Nodes.Add(targetNode = nodesList[num.Value]); } else { if (!nodesDictionary.ContainsKey(lbl)) { throw new Exception("Invalid node label: " + lbl); } rElm.Nodes.Add(targetNode = nodesDictionary[lbl]); } if (isCenter.HasValue) { if (isCenter.Value) { rElm.CentralNode = targetNode; } } } var useForAllLoads = node.GetAttributeValue <bool>("UseForAllLoads"); if (useForAllLoads.HasValue) { rElm.UseForAllLoads = useForAllLoads.Value; } var appliedLoadCases = node.SelectNodes("RigidElement/AppliedLoadCases/LoadCase"); #region cases foreach (XmlElement item in appliedLoadCases) { var name = item.GetAttributeValue("Name"); var type = item.GetAttributeValue("Type"); if (name == null || type == null) { throw new Exception("RigidElement/AppliedLoadCases/LoadCase should have both Type and Title"); } LoadType loadTp; if (type.IsNumber()) { loadTp = (LoadType)int.Parse(type); } else { loadTp = (LoadType)Enum.Parse(typeof(LoadType), type); } var lcase = new LoadCase(name, loadTp); rElm.AppliedLoadCases.Add(lcase); } #endregion var appliedLoadTypes = node.SelectNodes("RigidElement/AppliedLoadTypes/LoadType"); #region types foreach (XmlElement item in appliedLoadTypes) { var type = item.GetAttributeValue("Type"); if (type == null) { throw new Exception("RigidElement/AppliedLoadCases/LoadCase should have both Type and Title"); } LoadType loadTp; if (type.IsNumber()) { loadTp = (LoadType)int.Parse(type); } else { loadTp = (LoadType)Enum.Parse(typeof(LoadType), type); } rElm.AppliedLoadTypes.Add(loadTp); } #endregion } #endregion #region loads { //reading loads var loadSets = doc.SelectNodes("Model/LoadSet"); foreach (XmlElement xelm in loadSets) { var loadCaseType = xelm.GetAttributeValue("LoadCaseType"); var loadCaseName = xelm.GetAttributeValue("LoadCaseName"); var loads = xelm.SelectNodes("/"); foreach (XmlElement loadElm in loads) { if (loadElm.Name == "NodalLoad") { #region nodal load var nodeIndex = xelm.GetAttributeValue <int>("NodeIndex"); var nodeLabel = xelm.GetAttributeValue("NodeLabel"); var fx = xelm.GetAttributeValue <double>("Fx"); var fy = xelm.GetAttributeValue <double>("Fy"); var fz = xelm.GetAttributeValue <double>("Fz"); var mx = xelm.GetAttributeValue <double>("Mx"); var my = xelm.GetAttributeValue <double>("My"); var mz = xelm.GetAttributeValue <double>("Mz"); var load = new NodalLoad(); var force = new Force(); force.Fx = fx ?? 0; force.Fy = fy ?? 0; force.Fz = fz ?? 0; force.Mx = mx ?? 0; force.My = my ?? 0; force.Mz = mz ?? 0; load.Case = new LoadCase(loadCaseName, GetLoadType(loadCaseType)); load.Force = force; if (nodeIndex.HasValue) { nodesList[nodeIndex.Value].Loads.Add(load); } else if (nodeLabel != null) { nodesDictionary[nodeLabel].Loads.Add(load); } else { throw new Exception("neither node index and label are defined"); } #endregion } else if (loadElm.Name == "UniformLoad") { #region uniform load var elmIndex = xelm.GetAttributeValue <int>("NodeIndex"); var elmLabel = xelm.GetAttributeValue("NodeLabel"); var magnitude = xelm.GetAttributeValue <double>("Magnitude"); var dir = xelm.GetAttributeValue("Direction"); var sys = xelm.GetAttributeValue("CoordinationSystem"); var load = new UniformLoad1D(); load.Magnitude = magnitude ?? magnitude.Value; load.Case = new LoadCase(loadCaseName, GetLoadType(loadCaseType)); load.Direction = (LoadDirection)Enum.Parse(typeof(LoadDirection), dir); load.CoordinationSystem = (CoordinationSystem)Enum.Parse(typeof(CoordinationSystem), sys); if (elmIndex.HasValue) { elementList[elmIndex.Value].Loads.Add(load); } else if (elmLabel != null) { elementDictionary[elmLabel].Loads.Add(load); } else { throw new Exception("neither node index and label are defined"); } #endregion } else { throw new Exception(string.Format("Element Type {0} not supported!", loadElm.Name)); } } } } #endregion throw new NotImplementedException(); }