Source code for Muscat.IO.CGNSReader
# -*- coding: utf-8 -*-
#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.txt', which is part of this source code package.
#
"""CGNS file reader
"""
from typing import Optional, Union
import os
import numpy as np
from Muscat.Types import MuscatFloat, MuscatIndex
from Muscat.Bridges.CGNSBridge import CGNSToMesh
from Muscat.MeshContainers.Mesh import Mesh
[docs]
def ReadCGNS(fileName, time: Optional[MuscatFloat] = None, baseNumberOrName: Union[MuscatIndex, str] = 0, zoneNumberOrName: Union[MuscatIndex, str] = 0, keepPartitionedMesh=False) -> Mesh:
"""Function API for reading a CGNS file
Parameters
----------
fileName : str
name of the file to be read
time : float, optional
not coded yet, by default None
baseNumberOrName : int or str, optional
name of the base to use, by default 0 (first)
zoneNumberOrName : int or str, optional
name of the zone to be read, by default 0 (first)
Returns
-------
Mesh
output unstructured mesh object containing reading result
"""
reader = CGNSReader()
reader.SetFileName(fileName)
reader.baseNumberOrName: Union[MuscatIndex, str] = baseNumberOrName
reader.zoneNumberOrName: Union[MuscatIndex, str] = zoneNumberOrName
reader.SetTimeToRead(time)
reader.keepPartitionedMesh = keepPartitionedMesh
res = reader.Read(fileName=fileName)
return res
[docs]
class CGNSReader:
"""CGNS Reader class"""
def __init__(self) -> None:
super().__init__()
self.fileName: str = None
self.fieldName = None
self.baseNumberOrName = None
self.zoneNumberOrName = None
self.timeToRead: MuscatFloat = -1.0
self.canHandleTemporal = False
self.keepPartitionedMesh = False
[docs]
def SetFileName(self, fileName: str) -> None:
"""Function to set fileName to read
Parameters
----------
fileName : str
name of the file to be read
"""
self.fileName = fileName
if fileName is None:
self.__path = None
else:
self.filePath = os.path.abspath(os.path.dirname(fileName)) + os.sep
[docs]
def SetTimeToRead(self, time: Optional[MuscatFloat] = None) -> None:
"""Function to set time value to read
Parameters
----------
time : float, optional
not coded yet, by default None
"""
if time is None:
self.timeToRead = 0.0
else: # pragma: no cover
raise NotImplementedError("not coded yet")
self.timeToRead = time
[docs]
def Read(self, fileName: Optional[str] = None, time: Optional[MuscatFloat] = None) -> Mesh:
"""Function that performs the reading of a CGNS result file
Parameters
----------
fileName : str
name of the file to be read
time : float, optional
not coded yet, by default None
baseNumberOrName : int or str, optional
name of the base to use, by default 0 (first)
zoneNumberOrName : int or str, optional
name of the zone to be read, by default 0 (first)
Returns
-------
Mesh
output unstructured mesh object containing reading result
"""
if fileName is not None:
self.SetFileName(fileName)
self.SetTimeToRead(time)
import CGNS.MAP as CGM
node = CGM.load(self.fileName)[0]
self.CGNSTree = node
res = CGNSToMesh(node, partitionedMesh=self.keepPartitionedMesh)
res.PrepareForOutput()
return res
from Muscat.IO.IOFactory import RegisterReaderClass
RegisterReaderClass(".cgns", CGNSReader)
[docs]
def CheckIntegrity(GUI: bool = False):
import Muscat.IO.CGNSWriter as CW
CW.CheckIntegrity()
from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory
tempdir = TemporaryDirectory.GetTempPath()
mesh = ReadCGNS(fileName=tempdir + os.sep + "toto.cgns")
print("Read mesh from cgns:", mesh)
reader = CGNSReader()
reader.SetFileName(None)
from Muscat.TestData import GetTestDataPath
print("Read mesh from cgns:", ReadCGNS(GetTestDataPath() + "CGNSExample/2TriMesh.cgns"))
return "ok"
if __name__ == "__main__":
print(CheckIntegrity(True)) # pragma: no cover