Пример #1
0
        public Model CreateModelFromFile()
        {
            double density        = 1.0;
            double k              = 1.0;
            double c              = 1.0;
            var    elementFactory = new ThermalElement3DFactory(new ThermalMaterial(1, density, c, k));
            var    model          = new Model();

            model.SubdomainsDictionary[0] = new Subdomain(0);
            // Material

            char[]     delimeters = { ' ', '=', '\t' };
            Attributes?name       = null;

            String[] text = System.IO.File.ReadAllLines(Filename);

            for (int i = 0; i < text.Length; i++)
            {
                String[]      line             = text[i].Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                StringBuilder comparisonString = new StringBuilder(null, 50);
                if (line.Length == 0)
                {
                    continue;
                }
                if (line[0] == "3" & line.Length > 1)
                {
                    try
                    {
                        name = (Attributes)Enum.Parse(typeof(Attributes), line[1]);
                    }
                    catch (Exception exception)
                    {
                        name = null;
                    }
                }
                else
                {
                    for (int linePosition = 0; linePosition < line.Length; linePosition++)
                    {
                        if (line[linePosition] == "#")
                        {
                            for (int ij = linePosition + 1; ij < line.Length; ij++)
                            {
                                comparisonString.Append(line[ij]);
                            }
                            try
                            {
                                name = (Attributes)Enum.Parse(typeof(Attributes), comparisonString.ToString());
                            }
                            catch (Exception exception)
                            {
                                name = null;
                            }
                        }
                    }
                }
                switch (name)
                {
                case Attributes.sdim:
                    int NumberOfDimensions = Int32.Parse(line[0]);
                    break;

                case Attributes.numberofmeshpoints:
                    NumberOfNodes = Int32.Parse(line[0]);
                    break;

                case Attributes.Meshpointcoordinates:
                    IList <Node> nodelist = new List <Node> {
                        null
                    };
                    for (int j = 0; j < NumberOfNodes; j++)
                    {
                        i++;
                        line = text[i].Split(delimeters);
                        int    nodeGlobalID = j;
                        double x            = Double.Parse(line[2], CultureInfo.InvariantCulture);
                        double y            = Double.Parse(line[1], CultureInfo.InvariantCulture);
                        double z            = Double.Parse(line[0], CultureInfo.InvariantCulture);
                        Node   node         = new Node(nodeGlobalID, x, y, z);
                        model.NodesDictionary.Add(nodeGlobalID, node);
                        nodelist.Add(node);
                    }

                    break;

                case Attributes.tri:
                    do
                    {
                        i++;
                        line = text[i].Split(delimeters);
                    } while (line[0] == "");
                    i++;
                    line = text[i].Split(delimeters);
                    NumberOfTriElements = Int32.Parse(line[0]);
                    i++;
                    IList <Node> nodesCollection = new List <Node>();
                    for (int TriID = 0; TriID < NumberOfTriElements; TriID++)
                    {
                        i++;
                        line = text[i].Split(delimeters);

                        for (int j = 0; j < (line.Length - 1); j++)
                        {
                            nodesCollection.Add(model.NodesDictionary[Int32.Parse(line[j])]);
                        }
                    }
                    nodesCollection = nodesCollection.Distinct().ToList();
                    foreach (Node node in nodesCollection)
                    {
                        if (node.X <= 5e-4 && node.Y <= 5e-4 && node.Z <= 5e-4)
                        {
                            model.NodesDictionary[node.ID].Constraints.Add(new Constraint()
                            {
                                DOF = ThermalDof.Temperature, Amount = 10
                            });
                        }
                        else
                        {
                            model.Loads.Add(new Load()
                            {
                                Node = model.NodesDictionary[node.ID], DOF = ThermalDof.Temperature, Amount = 10
                            });
                        }
                    }
                    break;

                case Attributes.tet:
                    do
                    {
                        i++;
                        line = text[i].Split(delimeters);
                    } while (line[0] == "");
                    i++;
                    line             = text[i].Split(delimeters);
                    NumberOfElements = Int32.Parse(line[0]);
                    i++;
                    for (int TetID = 0; TetID < NumberOfElements; TetID++)
                    {
                        i++;
                        line = text[i].Split(delimeters);

                        IReadOnlyList <Node> nodes = new List <Node>
                        {
                            model.NodesDictionary[Int32.Parse(line[3])],
                            model.NodesDictionary[Int32.Parse(line[2])],
                            model.NodesDictionary[Int32.Parse(line[1])],
                            model.NodesDictionary[Int32.Parse(line[0])]
                        };
                        var Tet4    = elementFactory.CreateElement(CellType.Tet4, nodes);
                        var element = new Element();
                        element.ID          = TetID;
                        element.ElementType = Tet4;
                        foreach (Node node in nodes)
                        {
                            element.AddNode(node);
                        }
                        model.SubdomainsDictionary[0].Elements.Add(element);
                        model.ElementsDictionary.Add(TetID, element);
                    }
                    break;
                }
            }
            return(model);
        }
Пример #2
0
        private static Model CreateModel()
        {
            var model = new Model();

            // Subdomains
            model.SubdomainsDictionary.Add(0, new Subdomain(subdomainID));

            // Material
            double density = 1.0;
            double k       = 1.0;
            double c       = 1.0;

            // Nodes
            int numNodes = 27;
            var nodes    = new Node[numNodes];

            nodes[0]  = new Node(id: 0, x: 2.0, y: 2.0, z: 2.0);
            nodes[1]  = new Node(id: 1, x: 2.0, y: 1.0, z: 2.0);
            nodes[2]  = new Node(id: 2, x: 2.0, y: 0.0, z: 2.0);
            nodes[3]  = new Node(id: 3, x: 2.0, y: 2.0, z: 1.0);
            nodes[4]  = new Node(id: 4, x: 2.0, y: 1.0, z: 1.0);
            nodes[5]  = new Node(id: 5, x: 2.0, y: 0.0, z: 1.0);
            nodes[6]  = new Node(id: 6, x: 2.0, y: 2.0, z: 0.0);
            nodes[7]  = new Node(id: 7, x: 2.0, y: 1.0, z: 0.0);
            nodes[8]  = new Node(id: 8, x: 2.0, y: 0.0, z: 0.0);
            nodes[9]  = new Node(id: 9, x: 1.0, y: 2.0, z: 2.0);
            nodes[10] = new Node(id: 10, x: 1.0, y: 1.0, z: 2.0);
            nodes[11] = new Node(id: 11, x: 1.0, y: 0.0, z: 2.0);
            nodes[12] = new Node(id: 12, x: 1.0, y: 2.0, z: 1.0);
            nodes[13] = new Node(id: 13, x: 1.0, y: 1.0, z: 1.0);
            nodes[14] = new Node(id: 14, x: 1.0, y: 0.0, z: 1.0);
            nodes[15] = new Node(id: 15, x: 1.0, y: 2.0, z: 0.0);
            nodes[16] = new Node(id: 16, x: 1.0, y: 1.0, z: 0.0);
            nodes[17] = new Node(id: 17, x: 1.0, y: 0.0, z: 0.0);
            nodes[18] = new Node(id: 18, x: 0.0, y: 2.0, z: 2.0);
            nodes[19] = new Node(id: 19, x: 0.0, y: 1.0, z: 2.0);
            nodes[20] = new Node(id: 20, x: 0.0, y: 0.0, z: 2.0);
            nodes[21] = new Node(id: 21, x: 0.0, y: 2.0, z: 1.0);
            nodes[22] = new Node(id: 22, x: 0.0, y: 1.0, z: 1.0);
            nodes[23] = new Node(id: 23, x: 0.0, y: 0.0, z: 1.0);
            nodes[24] = new Node(id: 24, x: 0.0, y: 2.0, z: 0.0);
            nodes[25] = new Node(id: 25, x: 0.0, y: 1.0, z: 0.0);
            nodes[26] = new Node(id: 26, x: 0.0, y: 0.0, z: 0.0);

            for (int i = 0; i < numNodes; ++i)
            {
                model.NodesDictionary[i] = nodes[i];
            }

            // Elements
            int numElements    = 8;
            var elementFactory = new ThermalElement3DFactory(new ThermalMaterial(density, c, k));
            var elementNodes   = new List <Node> [8];

            elementNodes[0] = new List <Node>()
            {
                nodes[13], nodes[4], nodes[3], nodes[12], nodes[10], nodes[1], nodes[0], nodes[9]
            };
            elementNodes[1] = new List <Node>()
            {
                nodes[14], nodes[5], nodes[4], nodes[13], nodes[11], nodes[2], nodes[1], nodes[10]
            };
            elementNodes[2] = new List <Node>()
            {
                nodes[16], nodes[7], nodes[6], nodes[15], nodes[13], nodes[4], nodes[3], nodes[12]
            };
            elementNodes[3] = new List <Node>()
            {
                nodes[17], nodes[8], nodes[7], nodes[16], nodes[14], nodes[5], nodes[4], nodes[13]
            };
            elementNodes[4] = new List <Node>()
            {
                nodes[22], nodes[13], nodes[12], nodes[21], nodes[19], nodes[10], nodes[9], nodes[18]
            };
            elementNodes[5] = new List <Node>()
            {
                nodes[23], nodes[14], nodes[13], nodes[22], nodes[20], nodes[11], nodes[10], nodes[19]
            };
            elementNodes[6] = new List <Node>()
            {
                nodes[25], nodes[16], nodes[15], nodes[24], nodes[22], nodes[13], nodes[12], nodes[21]
            };
            elementNodes[7] = new List <Node>()
            {
                nodes[26], nodes[17], nodes[16], nodes[25], nodes[23], nodes[14], nodes[13], nodes[22]
            };

            var nodeReordering = new GMeshElementLocalNodeOrdering();
            var rearrangeNodes = elementNodes.Select(x => nodeReordering.ReorderNodes(x, CellType.Hexa8)).ToArray();;

            for (int i = 0; i < numElements; ++i)
            {
                var elementWrapper = new Element()
                {
                    ID = i, ElementType = elementFactory.CreateElement(CellType.Hexa8, rearrangeNodes[i])
                };
                foreach (var node in elementNodes[i])
                {
                    elementWrapper.AddNode(node);
                }
                model.ElementsDictionary[i] = elementWrapper;
                model.SubdomainsDictionary[subdomainID].Elements.Add(elementWrapper);
            }

            // Dirichlet BC
            model.NodesDictionary[0].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[1].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[2].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[9].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[10].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[11].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[18].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[19].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });
            model.NodesDictionary[20].Constraints.Add(new Constraint()
            {
                DOF = ThermalDof.Temperature, Amount = 100.0
            });

            // Neumann BC
            double q = 100;

            model.Loads.Add(new Load()
            {
                Amount = q, Node = model.NodesDictionary[6], DOF = ThermalDof.Temperature
            });
            model.Loads.Add(new Load()
            {
                Amount = q, Node = model.NodesDictionary[24], DOF = ThermalDof.Temperature
            });

            return(model);
        }