Esempio n. 1
0
        static public GridCommons ToBoSSSGrid(this Cgns cgns, int BaseNodeIndex = 0)
        {
            var b = cgns.base_t.ElementAt(BaseNodeIndex);
            int D = b.CellDimension;

            var grid = CreateBoSSSGrid(b);

            long GlobalID_cnt = 0;

            int[] NodesPerZone = b.zones.Select(delegate(Cgns.Zone_t z) {
                if (!(z is Cgns.UnstructuredZone_t))
                {
                    throw new NotSupportedException("Only unstructured Grids are supported for CGNS ot BoSSS conversion.");
                }

                Cgns.UnstructuredZone_t uz = (Cgns.UnstructuredZone_t)z;

                int Ncoords = uz.coord[0].Length;
                for (int d = 1; d < uz.coord.Length; d++)
                {
                    if (uz.coord[d].Length != Ncoords)
                    {
                        throw new ApplicationException("invalid cgns data.");
                    }
                }

                return(Ncoords);
            }).ToArray();


            // import cells
            // ============

            {
                List <Cell> Cells      = new List <Cell>();
                int         NodeOffset = 0;
                for (int iZone = 0; iZone < b.zones.Length; iZone++)
                {
                    var z = b.zones[iZone];

                    if (!(z is Cgns.UnstructuredZone_t))
                    {
                        throw new NotSupportedException("Only unstructured Grids are supported for CGNS ot BoSSS conversion.");
                    }

                    Cgns.UnstructuredZone_t uz = (Cgns.UnstructuredZone_t)z;

                    int NumberOfNodes = NodesPerZone[iZone];


                    foreach (Cgns.Elements_t elm in uz.elements)
                    {
                        if (elm.element_type.Dimension() != D)
                        {
                            continue;
                        }

                        CellType BoSSSCelType = elm.element_type.ToBoSSSCellType();

                        for (int j = 0; j < elm.ielem.GetLength(0); j++)
                        {
                            Cell cell = new Cell();
                            cell.GlobalID = GlobalID_cnt;
                            cell.Type     = BoSSSCelType;

                            int[] CgnsNodes = elm.ielem.GetRow(j);
                            foreach (int iNode in CgnsNodes)
                            {
                                if (iNode > NumberOfNodes)
                                {
                                    throw new ArgumentException("CGNS node index out of range.");
                                }
                            }
                            cell.NodeIndices = GetBoSSSConnectivityNodes(elm.element_type, CgnsNodes);
                            for (int iNode = 0; iNode < cell.NodeIndices.Length; iNode++)
                            {
                                cell.NodeIndices[iNode] += NodeOffset;
                            }

                            cell.TransformationParams = GetTransformationParams(elm.element_type, CgnsNodes, uz.coord, D);

                            GlobalID_cnt++;

                            Cells.Add(cell);
                        }
                    }

                    NodeOffset += NumberOfNodes;
                }
                grid.Cells = Cells.ToArray();
            }

            // import Bc
            // =========
            {
                List <BCElement> BcCells = new List <BCElement>();

                byte EdgeTagCnt = 0;

                int NodeOffset = 0;
                for (int iZone = 0; iZone < b.zones.Length; iZone++)
                {
                    var z = b.zones[iZone];

                    if (!(z is Cgns.UnstructuredZone_t))
                    {
                        throw new NotSupportedException("Only unstructured Grids are supported.");
                    }

                    Cgns.UnstructuredZone_t uz = (Cgns.UnstructuredZone_t)z;

                    int NumberOfNodes = NodesPerZone[iZone];

                    foreach (Cgns.BC_t bc in uz.bcs)
                    {
                        Cgns.Elements_t bc_elements = uz.elements.Single(
                            elem => (elem.Name.Equals(bc.Name)));

                        byte EdgeTag;
                        if (!(grid.EdgeTagNames.Values.Contains(bc.Name, (x, y) => x.Equals(y))))
                        {
                            EdgeTagCnt++;
                            grid.EdgeTagNames.Add(EdgeTagCnt, bc.Name);
                            EdgeTag = EdgeTagCnt;
                        }
                        else
                        {
                            var f = grid.EdgeTagNames.Single(kv => kv.Value.Equals(bc.Name));
                            EdgeTag = f.Key;
                        }

                        CellType BoSSSCelType = bc_elements.element_type.ToBoSSSCellType();

                        for (int j = 0; j < bc_elements.ielem.GetLength(0); j++)
                        {
                            BCElement cell = new BCElement();
                            cell.GlobalID = GlobalID_cnt;
                            cell.Type     = BoSSSCelType;
                            cell.EdgeTag  = EdgeTag;

                            int[] CgnsNodes = bc_elements.ielem.GetRow(j);
                            foreach (int iNode in CgnsNodes)
                            {
                                if (iNode > NumberOfNodes)
                                {
                                    throw new ArgumentException("CGNS node index out of range.");
                                }
                            }
                            cell.NodeIndices = GetBoSSSConnectivityNodes(bc_elements.element_type, CgnsNodes);
                            for (int iNode = 0; iNode < cell.NodeIndices.Length; iNode++)
                            {
                                cell.NodeIndices[iNode] += NodeOffset;
                            }

                            cell.TransformationParams = GetTransformationParams(bc_elements.element_type, CgnsNodes, uz.coord, D);

                            GlobalID_cnt++;

                            BcCells.Add(cell);
                        }
                    }

                    NodeOffset += NumberOfNodes;
                }

                if (BcCells.Count > 0)
                {
                    grid.BcCells = BcCells.ToArray();
                }
            }


            // finalize & return
            // =================

            if (b.zones.Length > 1)
            {
                grid.MergeAndCheckNodes();
            }
            grid.CompressGlobalID();
            grid.CompressNodeIndices();
            grid.CheckAndFixJacobianDeterminat();

            return(grid);
        }