Source code for Muscat.IO.CGNSWriter
# -*- 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 writer
"""
import os
import numpy as np
from Muscat.MeshContainers.Mesh import Mesh
from Muscat.Bridges.CGNSBridge import MeshToCGNS
from Muscat.IO.WriterBase import WriterBase as WriterBase
from Muscat.IO.IOFactory import RegisterWriterClass
import CGNS.MAP as CGM
[docs]
class CGNSWriter(WriterBase):
"""Class to writes a CGNS file on disk"""
def __init__(self):
super().__init__()
self.canHandleTemporal = True
self.canHandleAppend = False
def __str__(self):
res = "CGNSWriter"
return res
[docs]
def Write(self, mesh: Mesh, fileName=None, outputPyTree=None, PointFields=None, PointFieldsNames=None, CellFieldsNames=None, CellFields=None, GridFieldsNames=None, GridFields=None, updateZone=False):
"""Function to write a CGNS File on disk
Parameters
----------
mesh : Mesh
support of the data to be written
fileName : str
filename of the file to be read
outpuPyTree : list
existing pyTree in which the data structure in mesh will be appended
updateZone : bool or str, only with outputPyTree
if False: data are added to a new CGNS base
if True: all zones pre existing in outPyTree base are updated (replace all structured zones with one unstructured zone, or update unique zone)
if zoneName: only zone with zoneName is updated (for unstructured multi zones computation)
"""
from copy import deepcopy
# make a
outputMesh = mesh.View()
if PointFields is not None and PointFieldsNames is not None:
outputMesh.nodeFields = {k: v for k, v in zip(PointFieldsNames, PointFields)}
if CellFields is not None and CellFieldsNames is not None:
outputMesh.elemFields = {k: v for k, v in zip(CellFieldsNames, CellFields)}
newPyTree = MeshToCGNS(outputMesh, deepcopy(outputPyTree), updateZone=updateZone)
if fileName is None:
fileName = self.fileName
CGM.save(fileName, newPyTree)
[docs]
def CheckIntegrity(GUI: bool = False):
from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory
tempdir = TemporaryDirectory.GetTempPath()
import Muscat.TestData as MuscatTestData
import Muscat.IO.UtReader as UR
reader = UR.UtReader()
reader.SetFileName(MuscatTestData.GetTestDataPath() + "UtExample/cube.ut")
reader.ReadMetaData()
reader.atIntegrationPoints = False
myMesh = reader.Read()
myMesh.nodeFields["Nodes arange"] = np.arange(myMesh.GetNumberOfNodes(), dtype=float)
myMesh.elemFields["Element arange"] = np.arange(myMesh.GetNumberOfElements(), dtype=float)
from Muscat.MeshContainers.Tags import Tags
##################################
# EXEMPLE SYNTAXE DU WRITER
# import Muscat.IO.CGNSWriter as CW
# CgW = CW.CGNSWriter()
CgW = CGNSWriter()
CgW.Write(mesh=myMesh, fileName=tempdir + os.sep + "toto.cgns")
totoC = np.random.rand(myMesh.GetNumberOfElements())
totoV = np.random.rand(myMesh.GetNumberOfNodes())
CgW.Write(mesh=myMesh, fileName=tempdir + os.sep + "toto.cgns", CellFieldsNames=["toto"], CellFields=[totoC], PointFieldsNames=["toto"], PointFields=[totoV])
##################################
return "ok"
RegisterWriterClass(".cgns", CGNSWriter)
if __name__ == "__main__":
print(CheckIntegrity(GUI=True)) # pragma: no cover