Source code for Muscat.IO.SamcefWriter

# -*- 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.
#

"""Samcef dat (bank) file writer
"""

import numpy as np

from Muscat.IO.WriterBase import WriterBase as WriterBase
import Muscat.Containers.ElementsDescription as ED
from Muscat.Types import MuscatIndex
from Muscat.Types import MuscatFloat


MuscatToSamcef = dict()
MuscatToSamcef[ED.Bar_2] = (np.ones(2), np.arange(2), 2)
MuscatToSamcef[ED.Triangle_3] = (np.array([1, 1, 1]), np.arange(3), 3)
MuscatToSamcef[ED.Tetrahedron_4] = (np.array([1, 1, 1, 1]), np.arange(4), 3)


[docs]class DatWriter(WriterBase): """Class to write a Samcef dat (bank) file""" def __init__(self): super().__init__() def __str__(self): res = "DatWriter : \n" res += " FileName : " + str(self.fileName) + "\n" return res
[docs] def Write(self, meshObject, PointFieldsNames=None, PointFields=None, CellFieldsNames=None, CellFields=None, GridFieldsNames=None, GridFields=None): """Write mesh to file in Samcef dat format Parameters ---------- meshObject : Mesh the mesh to be written PointFieldsNames : None Not Used, by default None PointFields : None Not Used, by default None CellFieldsNames : list[str], optional name of the fields defined at the cell to write, by default None CellFields : list[np.ndarray], optional fields defined at the cell to write, by default None GridFieldsNames : None Not Used, by default None GridFields : None Not Used, by default None """ # Nodes numberOfPoints = meshObject.GetNumberOfNodes() posn = meshObject.GetPosOfNodes() self.writeText(".NOE\n") for n in range(numberOfPoints): self.filePointer.write("{} ".format(n + 1)) posn[np.newaxis, n, :].tofile(self.filePointer, sep=" ") self.writeText("\n") groupCpt = 1 groupNames = dict() # Nodes Tags if len(meshObject.nodesTags): self.writeText(".SEL\n") for tag in meshObject.nodesTags: self.writeText('GROUP {} NOM "{}" NOEUDS \n'.format(groupCpt, tag.name)) groupNames[tag.name] = groupCpt groupCpt += 1 self.writeText("I ") # tag meshObject.nodesTags[tagname] (tag.GetIds() + 1).tofile(self.filePointer, sep=" ") self.writeText("\n") # Elements self.writeText(".MAI\n") cpt = 0 # for tagname in celtags: for name, data in meshObject.elements.items(): sign, permutation, splitPoint = MuscatToSamcef[name] lconn = (1 + data.connectivity) * sign lconn = lconn[:, permutation] fp = lconn[:, :splitPoint] sp = lconn[:, splitPoint:] for n in range(data.GetNumberOfElements()): self.writeText("I {} N ".format(cpt)) fp[n, :].tofile(self.filePointer, sep=" ") if sp.shape[1] > 0: self.writeText(" 0 ") sp[n, :].tofile(self.filePointer, sep=" ") cpt += 1 self.writeText("\n") celltags = meshObject.GetNamesOfElementTags() # Nodes Tags if len(celltags): self.writeText(".SEL\n") for tagname in celltags: self.writeText('GROUP {} NOM "{}" MAILLE \n'.format(groupCpt, tagname)) groupNames[tagname] = groupCpt groupCpt += 1 self.writeText("I ") ids = meshObject.GetElementsInTag(tagname) + 1 ids.tofile(self.filePointer, sep=" ") self.writeText("\n") if "FrameX_0" in CellFieldsNames: FrameX_0 = CellFields[CellFieldsNames.index("FrameX_0")] FrameX_1 = CellFields[CellFieldsNames.index("FrameX_1")] FrameX_2 = CellFields[CellFieldsNames.index("FrameX_2")] FrameY_0 = CellFields[CellFieldsNames.index("FrameY_0")] FrameY_1 = CellFields[CellFieldsNames.index("FrameY_1")] FrameY_2 = CellFields[CellFieldsNames.index("FrameY_2")] nbelems = meshObject.GetNumberOfElements() self.writeText(".FRA\n") idx = [] for n in range(nbelems): # in Optim in the case the frame is zero if np.sum(np.abs([FrameX_0[n], FrameX_1[n], FrameX_2[n]])) == 0: # pragma: no cover continue idx.append(n + 1) self.writeText(f"I {n+1} V1 {FrameX_0[n]} {FrameX_1[n]} {FrameX_2[n]} V2 {FrameY_0[n]} {FrameY_1[n]} {FrameY_2[n]} \n") self.writeText(".AEL\n") for n in idx: self.writeText(f"I {n} FRAME {n} \n") self.writeText("RETURN\n")
from Muscat.IO.IOFactory import RegisterWriterClass RegisterWriterClass(".datt", DatWriter)
[docs]def CheckIntegrity(): import Muscat.Containers.Mesh as UM from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory tempdir = TemporaryDirectory.GetTempPath() mymesh = UM.Mesh() mymesh.nodes = np.array([[0.00000000001, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]], dtype=MuscatFloat) mymesh.originalIDNodes = np.array([1, 3, 4, 5], dtype=MuscatIndex) mymesh.nodesTags.CreateTag("FirstNode").AddToTag(0) tris = mymesh.GetElementsOfType(ED.Triangle_3) tris.AddNewElement([0, 1, 2], 0) tris.AddNewElement([2, 1, 3], 3) tris.originalIds = np.array([3, 5], dtype=MuscatIndex) Tetrahedron_4 = mymesh.GetElementsOfType(ED.Tetrahedron_4) Tetrahedron_4.AddNewElement([0, 1, 2, 3], 0) mymesh.AddElementToTagUsingOriginalId(3, "Tag1") mymesh.AddElementToTagUsingOriginalId(5, "Tag3") nbel = mymesh.GetNumberOfElements() CellFields = [np.ones(nbel), np.zeros(nbel), np.zeros(nbel), np.zeros(nbel), np.ones(nbel), np.zeros(nbel)] CellFieldsNames = ["FrameX_0", "FrameX_1", "FrameX_2", "FrameY_0", "FrameY_1", "FrameY_2"] OW = DatWriter() print(OW) OW.Open(tempdir + "Test_SamcefWriter.datt") OW.Write(mymesh, CellFieldsNames=CellFieldsNames, CellFields=CellFields) OW.Close() return "ok"
if __name__ == "__main__": print(CheckIntegrity()) # pragma: no cover