public static void RunExample3D() { string path = @"E:\ALCoding\ALFE\topoptTest\Example3"; BESO beso = FEIO.ReadBESO(path, "beso"); beso.Initialize(); beso.Optimize(); FEIO.WriteIsovalues(path, beso); }
public static void PrintPreprocessing(BESO beso) { PrintDeviceInfo(); PrintModelInfo(beso.System); PrintMatrixInfo(beso.System.GetKG()); Console.WriteLine("------------------- Time Cost -------------------"); Console.WriteLine("Computing Ke: " + beso.System.TimeCost[0].ToString() + " ms"); Console.WriteLine("Initializing KG: " + beso.System.TimeCost[1].ToString() + " ms"); }
public static void RunChair2D() { string path = @"F:\OneDrive\OneDrive - RMIT University\Work\MyPaper\Extracting smooth design from topology optimisation results using nodal sensitivity\Numberical Experiments\2D Chair\TopologyOptimisation"; BESO beso = FEIO.ReadBESO(path, "beso"); beso.Initialize(); Console.WriteLine(beso.Model.Elements[0].Ke); beso.Optimize(); FEIO.WriteIsovalues(path, beso); }
public static void WriteIsovalues(string path, BESO beso) { string output = path + '\\' + "isovalues.txt"; StreamWriter sw = new StreamWriter(output); for (int i = 0; i < beso.isovalues.Count; i++) { sw.WriteLine(beso.isovalues[i].ToString()); } sw.Flush(); sw.Close(); sw.Dispose(); }
public static void PrintBESOInfo(BESO beso, int iter, double gse, double vf, List <double> timeCost) { Console.WriteLine("################### Step: " + iter.ToString() + " #####################"); Console.WriteLine("Compliance: " + gse.ToString()); Console.WriteLine("Volume: " + vf.ToString()); Console.WriteLine("------------------- Time Cost -------------------"); Console.WriteLine("Assembling KG: " + timeCost[0].ToString() + " ms"); Console.WriteLine("Solving: " + timeCost[1].ToString() + " ms"); Console.WriteLine("Computing Sensitivity: " + timeCost[2].ToString() + " ms"); Console.WriteLine("Fltering Sensitivity: " + timeCost[3].ToString() + " ms"); Console.WriteLine("Marking Elements: " + timeCost[4].ToString() + " ms"); Console.WriteLine("Checking Convergence: " + timeCost[5].ToString() + " ms"); Console.WriteLine(); }
/// <summary> /// Read a finite element model and the parameters of BESO topology optimization with .al format /// </summary> /// <param name="path">File path.</param> /// <returns>Return a finite element model.</returns> public static BESO ReadBESO(string path, string name) { string besoPath = path + "\\" + name + ".txt"; string projectPath = path + "\\solution"; Model model = new Model(); int dof = 0; ElementType elementType = ElementType.PixelElement; List <Node> nodes = new List <Node>(); List <Element> elements = new List <Element>(); List <Load> loads = new List <Load>(); List <Support> supports = new List <Support>(); Material material = new Material(); double ert = 0.0; double rmin = 0.0; double vf = 0.0; double p = 0; int maxIter = 0; int solver = 0; bool parallel = false; if (File.Exists(besoPath)) { StreamReader SR = new StreamReader(besoPath); #region Read FE parameters bool readFEpara = false; while (readFEpara == false) { string line = SR.ReadLine(); if (line == "FEA Parameters: ") { string[] value = SR.ReadLine().Split(':'); if (value[0] == "DOF") { dof = int.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Element Type") { string type = value[1].Split(' ')[1]; switch (type) { case "PixelElement": elementType = ElementType.PixelElement; break; case "TriangleElement": elementType = ElementType.TriangleElement; break; case "QuadElement": elementType = ElementType.QuadElement; break; case "TetrahedronElement": elementType = ElementType.TetrahedronElement; break; case "VoxelElement": elementType = ElementType.VoxelElement; break; case "HexahedronElement": elementType = ElementType.HexahedronElement; break; default: throw new Exception("Unknown element type."); } } value = SR.ReadLine().Split(':'); if (value[0] == "Node Count") { nodes = new List <Node>(int.Parse(value[1].Split(' ')[1])); } value = SR.ReadLine().Split(':'); if (value[0] == "Element Count") { elements = new List <Element>(int.Parse(value[1].Split(' ')[1])); } value = SR.ReadLine().Split(':'); if (value[0] == "Load Count") { loads = new List <Load>(int.Parse(value[1].Split(' ')[1])); } value = SR.ReadLine().Split(':'); if (value[0] == "Support Count") { supports = new List <Support>(int.Parse(value[1].Split(' ')[1])); } value = SR.ReadLine().Split(':'); if (value[0] == "Young's Modulus") { material.E = double.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Possion Rate") { material.nu = double.Parse(value[1].Split(' ')[1]); } if (value[0] == "Parallel Computing") { parallel = bool.Parse(value[1].Split(' ')[1]); } readFEpara = true; } } #endregion #region Read BESO parameters bool readBESOpara = false; while (readBESOpara == false) { string line = SR.ReadLine(); if (line == "BESO Parameters: ") { string[] value = SR.ReadLine().Split(':'); if (value[0] == "Volume Fraction") { vf = double.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Evolution Rate") { ert = double.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Filter Radius") { rmin = double.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Penalty Exponent") { p = double.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Maximum Iteration") { maxIter = int.Parse(value[1].Split(' ')[1]); } value = SR.ReadLine().Split(':'); if (value[0] == "Solver") { solver = int.Parse(value[1].Split(' ')[1]); } readBESOpara = true; } } #endregion #region Read the model while (!SR.EndOfStream) { string[] value = SR.ReadLine().Split(','); if (value[0] == "N") { nodes.Add(new Node(dof, double.Parse(value[1]), double.Parse(value[2]), double.Parse(value[3]))); } if (value[0] == "E" || value[0] == "NE") { bool nondesign = false; if (value[0] == "NE") { nondesign = true; } List <Node> elemNodes = new List <Node>(); for (int i = 1; i < value.Length; i++) { int id = int.Parse(value[i]); elemNodes.Add(nodes[id]); } switch (elementType) { case ElementType.PixelElement: elements.Add(new Pixel(elemNodes, material, nondesign)); break; case ElementType.TriangleElement: elements.Add(new Triangle(elemNodes, material, 1.0, nondesign)); break; case ElementType.QuadElement: elements.Add(new Quadrilateral(elemNodes, material, 1.0, nondesign)); break; case ElementType.TetrahedronElement: elements.Add(new Tetrahedron(elemNodes, material, nondesign)); break; case ElementType.HexahedronElement: elements.Add(new Hexahedron(elemNodes, material, nondesign)); break; case ElementType.VoxelElement: elements.Add(new Voxel(elemNodes, material, nondesign)); break; default: throw new Exception("Unknown element type."); } } if (value[0] == "L") { loads.Add(new Load(dof, int.Parse(value[1]), double.Parse(value[2]), double.Parse(value[3]), double.Parse(value[4]))); } if (value[0] == "S") { if (value[2] == "Fixed") { supports.Add(new Support(int.Parse(value[1]), SupportType.Fixed)); } } } #endregion if (elementType == ElementType.PixelElement || elementType == ElementType.QuadElement || elementType == ElementType.TriangleElement) { model = new Model(2, nodes, elements, loads, supports); } else { model = new Model(3, nodes, elements, loads, supports); } SR.Close(); SR.Dispose(); } BESO beso = new BESO(projectPath, new FESystem(model), rmin, ert, p, vf, maxIter, (Solver)solver); return(beso); }
/// <summary> /// Write a finite element model and the parameters of BESO topology optimization into a .al file. /// </summary> /// <param name="path">File path.</param> /// <param name="BESO">A finite element model for BESO topology optimization.</param> public static void WriteBESO(string path, BESO beso, int solver) { string output = path + ".txt"; StreamWriter sw = new StreamWriter(output); sw.WriteLine("%This file is created by ALFE."); Model model = beso.Model; sw.WriteLine("FEA Parameters: "); sw.WriteLine("DOF: " + model.DOF.ToString()); sw.WriteLine("Element Type: " + model.Elements[0].Type.ToString()); sw.WriteLine("Node Count: " + model.Nodes.Count.ToString()); sw.WriteLine("Element Count: " + model.Elements.Count.ToString()); sw.WriteLine("Load Count: " + model.Loads.Count.ToString()); sw.WriteLine("Support Count: " + model.Supports.Count.ToString()); sw.WriteLine("Young's Modulus: " + model.Elements[0].Material.E.ToString()); sw.WriteLine("Possion Rate: " + model.Elements[0].Material.nu.ToString()); sw.WriteLine(); sw.WriteLine("BESO Parameters: "); sw.WriteLine("Volume Fraction: " + beso.VolumeFraction.ToString()); sw.WriteLine("Evolution Rate: " + beso.EvolutionRate.ToString()); sw.WriteLine("Filter Radius: " + beso.FilterRadius.ToString()); sw.WriteLine("Penalty Exponent: " + beso.PenaltyExponent.ToString()); sw.WriteLine("Maximum Iteration: " + beso.MaximumIteration.ToString()); sw.WriteLine("Solver: " + solver.ToString()); sw.WriteLine(); sw.WriteLine("Model Info: "); foreach (var node in model.Nodes) { sw.WriteLine("N," + node.Position.X.ToString() + ',' + node.Position.Y.ToString() + ',' + node.Position.Z.ToString()); } foreach (var elem in model.Elements) { if (elem.NonDesign) { sw.Write("NE,"); } else { sw.Write("E,"); } int n = 1; foreach (var node in elem.Nodes) { sw.Write(node.ID.ToString()); if (n != elem.Nodes.Count) { sw.Write(','); } n++; } sw.Write("\n"); } foreach (var item in model.Loads) { sw.WriteLine("L," + item.NodeID.ToString() + ',' + item.ForceVector.X.ToString() + ',' + item.ForceVector.Y.ToString() + ',' + item.ForceVector.Z.ToString()); } foreach (var item in model.Supports) { sw.WriteLine("S," + item.NodeID.ToString() + ',' + item.Type.ToString()); } sw.Flush(); sw.Close(); sw.Dispose(); }