|
| 1 | +using BriefFiniteElementNet.Elements; |
| 2 | +using BriefFiniteElementNet.Materials; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows.Controls; |
| 10 | + |
| 11 | +namespace BriefFiniteElementNet.Validation |
| 12 | +{ |
| 13 | + public class AbaqusInputFileReader |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Method that reads an Abaqus input file and returns a BFE model with the same nodes and elements |
| 17 | + /// </summary> |
| 18 | + /// <param name="path">Path to an input file</param> |
| 19 | + /// <returns>A BFE model</returns> |
| 20 | + public static Model AbaqusInputToBFE(string pathToInputFile) |
| 21 | + { |
| 22 | + var buf = new Model(); |
| 23 | + |
| 24 | + //splitting char |
| 25 | + char delimiter = ','; |
| 26 | + Node node; |
| 27 | + TetrahedronElement element; |
| 28 | + |
| 29 | + //list for the node- and elementsets |
| 30 | + List<NodeSet> nodeSets = new List<NodeSet>(); |
| 31 | + List<ElementSet> elementSets = new List<ElementSet>(); |
| 32 | + |
| 33 | + using (StreamReader sr = File.OpenText(pathToInputFile)) |
| 34 | + { |
| 35 | + string[] split; |
| 36 | + selectedInputVariable selectedVar = selectedInputVariable.Nodes; |
| 37 | + string input = sr.ReadLine(); |
| 38 | + while (input != null) |
| 39 | + { |
| 40 | + split = input.Split(delimiter); |
| 41 | + if (split[0].Contains('*')) |
| 42 | + { |
| 43 | + switch (split[0]) |
| 44 | + { |
| 45 | + case "*Node": |
| 46 | + selectedVar = selectedInputVariable.Nodes; |
| 47 | + break; |
| 48 | + case "*Element": |
| 49 | + selectedVar = selectedInputVariable.Elements; |
| 50 | + break; |
| 51 | + case "*Nset": |
| 52 | + selectedVar = selectedInputVariable.NodeSet; |
| 53 | + nodeSets.Add(new NodeSet() { Name = split[1].Replace("nset=", "") }); |
| 54 | + break; |
| 55 | + case "*Elset": |
| 56 | + selectedVar = selectedInputVariable.ElementSet; |
| 57 | + elementSets.Add(new ElementSet() { Name = split[1].Replace("elset=", "") }); |
| 58 | + break; |
| 59 | + case "*Cload": |
| 60 | + selectedVar = selectedInputVariable.CLoad; |
| 61 | + break; |
| 62 | + case "*Boundary": |
| 63 | + selectedVar = selectedInputVariable.BC; |
| 64 | + break; |
| 65 | + default: |
| 66 | + selectedVar = selectedInputVariable.Other; |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + switch (selectedVar) |
| 73 | + { |
| 74 | + case selectedInputVariable.Nodes: |
| 75 | + { |
| 76 | + node = ReadNode(input, delimiter); |
| 77 | + if (node != null) |
| 78 | + { |
| 79 | + //tetrahedron element - > fix rotation |
| 80 | + node.Constraints = Constraints.RotationFixed; |
| 81 | + buf.Nodes.Add(node); |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
| 85 | + case selectedInputVariable.Elements: |
| 86 | + { |
| 87 | + element = ReadTetraElement(input, delimiter, buf.Nodes); |
| 88 | + if (element != null) |
| 89 | + { |
| 90 | + element.Material = UniformIsotropicMaterial.CreateFromYoungPoisson(210e9, 0.25); |
| 91 | + element.FixNodeOrder(); |
| 92 | + buf.Elements.Add(element); |
| 93 | + } |
| 94 | + break; |
| 95 | + } |
| 96 | + case selectedInputVariable.NodeSet: |
| 97 | + { |
| 98 | + for (int i = 0; i < split.Count(); i++) |
| 99 | + { |
| 100 | + nodeSets[nodeSets.Count - 1].Nodes.Add(Convert.ToInt32(split[i])); |
| 101 | + } |
| 102 | + break; |
| 103 | + } |
| 104 | + case selectedInputVariable.ElementSet: |
| 105 | + { |
| 106 | + for (int i = 0; i < split.Count(); i++) |
| 107 | + { |
| 108 | + elementSets[elementSets.Count - 1].Elements.Add(Convert.ToInt32(split[i])); |
| 109 | + } |
| 110 | + break; |
| 111 | + } |
| 112 | + case selectedInputVariable.CLoad: |
| 113 | + { |
| 114 | + NodeSet set = nodeSets.Where(x => x.Name.Replace(" ", "") == split[0].Replace(" ", "")).FirstOrDefault(); |
| 115 | + if (set != null) |
| 116 | + { |
| 117 | + //determine the magnitude of the load |
| 118 | + var load = new BriefFiniteElementNet.NodalLoad(); |
| 119 | + var frc = new Force(); |
| 120 | + if (Convert.ToInt32(split[1]) == 1) |
| 121 | + { |
| 122 | + frc.Fx = Convert.ToDouble(split[2]); |
| 123 | + load.Force = frc; |
| 124 | + } |
| 125 | + else if (Convert.ToInt32(split[1]) == 2) |
| 126 | + { |
| 127 | + frc.Fy = Convert.ToDouble(split[2]); |
| 128 | + load.Force = frc; |
| 129 | + } |
| 130 | + else if (Convert.ToInt32(split[1]) == 3) |
| 131 | + { |
| 132 | + frc.Fz = Convert.ToDouble(split[2]); |
| 133 | + load.Force = frc; |
| 134 | + } |
| 135 | + //add the load to the nodes |
| 136 | + foreach (var nodeLabel in set.Nodes) |
| 137 | + { |
| 138 | + buf.Nodes[nodeLabel - 1].Loads.Add(load); |
| 139 | + } |
| 140 | + } |
| 141 | + break; |
| 142 | + } |
| 143 | + case selectedInputVariable.BC: |
| 144 | + { |
| 145 | + NodeSet set = nodeSets.Where(x => x.Name.Replace(" ", "") == split[0].Replace(" ", "")).FirstOrDefault(); |
| 146 | + if (set != null) |
| 147 | + { |
| 148 | + //add the load to the nodes |
| 149 | + foreach (var nodeLabel in set.Nodes) |
| 150 | + { |
| 151 | + buf.Nodes[nodeLabel - 1].Constraints = Constraints.Fixed; |
| 152 | + } |
| 153 | + } |
| 154 | + break; |
| 155 | + } |
| 156 | + default: |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + input = sr.ReadLine(); |
| 161 | + } |
| 162 | + } |
| 163 | + return buf; |
| 164 | + } |
| 165 | + /// <summary> |
| 166 | + /// Reading node |
| 167 | + /// </summary> |
| 168 | + /// <param name="nodeline">A line from an Abaqus file</param> |
| 169 | + /// <param name="delimiter">The separator char</param> |
| 170 | + /// <returns>A node for the BFE model</returns> |
| 171 | + private static Node ReadNode(string nodeline, char delimiter) |
| 172 | + { |
| 173 | + //variables needed for later |
| 174 | + string[] split; |
| 175 | + Node node = null; |
| 176 | + double X, Y, Z; |
| 177 | + int nodeNr; |
| 178 | + |
| 179 | + try |
| 180 | + { |
| 181 | + split = nodeline.Split(delimiter); |
| 182 | + |
| 183 | + nodeNr = Convert.ToInt32(split[0]); |
| 184 | + X = Convert.ToDouble(split[1]); |
| 185 | + Y = Convert.ToDouble(split[2]); |
| 186 | + Z = Convert.ToDouble(split[3]); |
| 187 | + node = new Node(X, Y, Z) { Label = "n" + nodeNr.ToString() }; |
| 188 | + } |
| 189 | + catch (Exception) |
| 190 | + { |
| 191 | + throw new Exception("Something went wrong with reading the nodes! Error with line: " + nodeline); |
| 192 | + } |
| 193 | + return node; |
| 194 | + } |
| 195 | + /// <summary> |
| 196 | + /// Reads an element from an Abaqus input file |
| 197 | + /// </summary> |
| 198 | + /// <param name="elementline">A line with the element props</param> |
| 199 | + /// <param name="delimiter">The separator char</param> |
| 200 | + /// <param name="nodes">A list of nodes - starts at 0 -> = node-1</param> |
| 201 | + /// <returns>An element</returns> |
| 202 | + private static TetrahedronElement ReadTetraElement(string elementline, char delimiter, NodeCollection nodes) |
| 203 | + { |
| 204 | + TetrahedronElement elm = new TetrahedronElement(); |
| 205 | + string[] split; |
| 206 | + int elementNr, nodeNr1, nodeNr2, nodeNr3, nodeNr4; |
| 207 | + try |
| 208 | + { |
| 209 | + split = elementline.Split(delimiter); |
| 210 | + |
| 211 | + elementNr = Convert.ToInt32(split[0]); |
| 212 | + //subtract 1. The nodes are numbered from 1-> end and are stored as 0-> end-1 |
| 213 | + elm.Nodes[0] = nodes[Convert.ToInt32(split[1]) - 1]; |
| 214 | + elm.Nodes[1] = nodes[Convert.ToInt32(split[2]) - 1]; |
| 215 | + elm.Nodes[2] = nodes[Convert.ToInt32(split[3]) - 1]; |
| 216 | + elm.Nodes[3] = nodes[Convert.ToInt32(split[4]) - 1]; |
| 217 | + elm.label = elementNr.ToString(); |
| 218 | + } |
| 219 | + catch (Exception) |
| 220 | + { |
| 221 | + throw new Exception("Something went wrong with reading the nodes! Error with line: " + elementline); |
| 222 | + } |
| 223 | + |
| 224 | + return elm; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + /// <summary> |
| 230 | + /// A nodeset |
| 231 | + /// </summary> |
| 232 | + public class NodeSet |
| 233 | + { |
| 234 | + /// <summary> |
| 235 | + /// Name of the set |
| 236 | + /// </summary> |
| 237 | + public string Name { get; set; } |
| 238 | + /// <summary> |
| 239 | + /// List of node labels |
| 240 | + /// </summary> |
| 241 | + public List<int> Nodes { get; set; } = new List<int>(); |
| 242 | + } |
| 243 | + /// <summary> |
| 244 | + /// Element set |
| 245 | + /// </summary> |
| 246 | + public class ElementSet |
| 247 | + { |
| 248 | + /// <summary> |
| 249 | + /// Name of the set |
| 250 | + /// </summary> |
| 251 | + public string Name { get; set; } |
| 252 | + /// <summary> |
| 253 | + /// List of element labels |
| 254 | + /// </summary> |
| 255 | + public List<int> Elements { get; set; } = new List<int>(); |
| 256 | + } |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// Enum for reading data |
| 260 | + /// </summary> |
| 261 | + public enum selectedInputVariable |
| 262 | + { |
| 263 | + Nodes = 1, |
| 264 | + Elements = 2, |
| 265 | + NodeSet = 3, |
| 266 | + ElementSet = 4, |
| 267 | + CLoad = 5, |
| 268 | + BC = 6, |
| 269 | + Other = 7, |
| 270 | + } |
| 271 | +} |
0 commit comments