Пример #1
0
        private void GenerateGeometryFile(string path, List <Surface> surfaces, MeshProperties props)
        {
            int count = 0;

            using (TextWriter tw = File.CreateText(path))
            {
                WriteComment(tw, "NODES");
                Dictionary <Vector <double>, int> nodes = surfaces
                                                          .SelectMany(s => s.Nodes)
                                                          .Distinct()
                                                          .ToDictionary(node => node, node => count++);
                foreach (Vector <double> p in nodes.Keys)
                {
                    WriteLine(tw, GetDefinitionString("Point", nodes[p], new List <string>
                    {
                        Convert.ToString(p[0], CultureInfo.InvariantCulture),
                        Convert.ToString(p[1], CultureInfo.InvariantCulture),
                        Convert.ToString(p[2], CultureInfo.InvariantCulture),
                        Convert.ToString(props.Size, CultureInfo.InvariantCulture)
                    }));
                }

                count = 0;
                WriteComment(tw, "LINES");
                Dictionary <Line, int> lines = surfaces
                                               .SelectMany(s => s.Lines)
                                               .ToDictionary(node => node, node => count++);
                foreach (Line line in lines.Keys)
                {
                    WriteLine(tw, GetDefinitionString("Line", lines[line], new List <string>
                    {
                        Convert.ToString(nodes[line.StartNode], CultureInfo.InvariantCulture),
                        Convert.ToString(nodes[line.EndNode], CultureInfo.InvariantCulture)
                    }));
                }

                WriteComment(tw, "SURFACES");
                count = 0;
                foreach (Surface surface in surfaces)
                {
                    List <string> lineTags = new List <string> {
                        Convert.ToString(count, CultureInfo.InvariantCulture)
                    };
                    WriteLine(tw, GetDefinitionString("Line Loop", count, surface.Lines.Select(l => Convert.ToString(lines[l], CultureInfo.InvariantCulture))));
                    WriteLine(tw, GetDefinitionString("Plane Surface", count, lineTags));
                    WriteLine(tw, GetDefinitionString("Physical Surface", count, lineTags));
                    if (props.Type == MeshType.Quadrangular)
                    {
                        WriteLine(tw, GetCommandString("Recombine Surface", lineTags));
                    }
                    count++;
                }
                tw.Flush();
                tw.Close();
            }
        }
Пример #2
0
        // METHODS
        public Mesh GenerateMesh(IPhysicalModel model, MeshProperties props)
        {
            Mesh mesh = null;

            FileInfo fileInfo = new FileInfo(gmshExecutionPath);

            if (fileInfo.Exists)
            {
                DateTime now          = DateTime.Now;
                string   prefix       = $"{now.Year}-{now.Month}-{now.Day}--{now.Hour}-{now.Minute}-{now.Second}-{now.Millisecond}";
                string   geometryFile = Path.Combine(fileInfo.DirectoryName, $"{prefix}.geo");
                string   meshFile     = Path.Combine(fileInfo.DirectoryName, $"{prefix}.msh");
                GenerateGeometryFile(geometryFile, model.GetSurfaces(), props);
                ExecuteGmsh(geometryFile, meshFile);
                mesh = ReadMeshFile(meshFile);
            }

            return(mesh);
        }