Source code for Muscat.IO.AscReader

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

"""ASC file reader
"""

import numpy as np

import Muscat.Containers.ElementsDescription as ED
from Muscat.Containers.Mesh import Mesh
from Muscat.IO.ReaderBase import ReaderBase
from Muscat.Types import MuscatIndex
from Muscat.Helpers.Logger import Info

AscNumber = {}

AscNumber["2006"] = ED.Triangle_6
AscNumber["3010"] = ED.Tetrahedron_10
AscNumber["1002"] = ED.Bar_2


[docs]def ReadAsc(fileName: str = "", string: str = "", out=None, **kwargs): """Function API for reading an ASC result file Parameters ---------- fileName : str, optional name of the file to be read, by default None string : str, optional data to be read as a string instead of a file, by default None out : Mesh, optional output unstructured mesh object containing reading result, by default None Returns ------- Mesh output unstructured mesh object containing reading result """ reader = AscReader() reader.SetFileName(fileName) reader.SetStringToRead(string) return reader.Read(fileName=fileName, string=string, out=out, **kwargs)
[docs]class AscReader(ReaderBase): """ASC Reader class""" def __init__(self): super().__init__() self.commentHeader = "%" self.readFormat = "r"
[docs] def Read(self, fileName="", string="", out=None): """Function that performs the reading of an ASC file Parameters ---------- fileName : str, optional name of the file to be read, by default None string : str, optional data to be read as a string instead of a file, by default None out : Mesh, optional output unstructured mesh object containing reading result, by default None Returns ------- Mesh output unstructured mesh object containing reading result """ if fileName is not None: self.SetFileName(fileName) if string is not None: self.SetStringToRead(string) self.StartReading() if out is None: res = Mesh() else: res = out import shlex # # if fileName is not None: # string = open(fileName, 'r') # elif string is not None: # from io import StringIO # string = StringIO(string) # # res = UM.Mesh() filetointernalid = {} # filetointernalidElem = {} Info("Reading file : {} ".format(fileName)) while True: # line = string.readline() l = self.ReadCleanLine() if not l: break if l.find("BEGIN_NODES") > -1: nbNodes = int(l.split()[1]) Info("Reading " + str(nbNodes) + " Nodes") dim = int(l.split()[2]) res.nodes = np.empty((nbNodes, dim)) res.originalIDNodes = np.empty((nbNodes,), dtype=int) cpt = 0 while True: l = self.ReadCleanLine() if l.find("ED._NODES") > -1: break s = l.split() # print(s) # print(res.originalIDNodes) oid = int(s[0]) filetointernalid[oid] = cpt res.originalIDNodes[cpt] = int(s[0]) res.nodes[cpt, :] = list(map(float, s[6:])) cpt += 1 continue # pragma: no cover # unseen by coverage peephole optimization if l.find("BEGIN_ELEMENTS") > -1: nbElements = int(l.split()[1]) Info("Reading " + str(nbElements) + " Elements") # res.nodes = np.empty((nbNodes,dim)) # res.originalIDNodes= np.empty((nbNodes,)) cpt = 0 while True: l = self.ReadCleanLine() if l.find("ED._ELEMENTS") > -1: if nbElements != cpt: # pragma: no cover print("File problem!! number of elements read not equal to the total number of elemetns") print(nbElements) print(cpt) break # pragma: no cover # unseen by coverage peephole optimization s = l.split() nametype = AscNumber[s[1]] conn = [filetointernalid[x] for x in map(int, s[5:])] # for some types we need permutation if nametype == ED.Triangle_6: conn = [conn[per] for per in [0, 2, 4, 1, 3, 5]] elif nametype == ED.Tetrahedron_10: conn = [conn[per] for per in [0, 2, 4, 9, 1, 3, 5, 6, 7, 8]] elements = res.GetElementsOfType(nametype) elements.Reserve(nbElements) oid = int(s[0]) elements.AddNewElement(conn, oid) cpt += 1 for etype, data in res.elements.items(): data.Tighten() continue if l.find("BEGIN_GROUPS") > -1: nbgroups = int(l.split()[1]) Info("Reading " + str(nbgroups) + " Groups") # res.nodes = np.empty((nbNodes,dim)) # res.originalIDNodes= np.empty((nbNodes,)) cpt = 0 while True: l = self.ReadCleanLine() if l.find("ED._GROUPS") > -1: if nbgroups != (cpt): # pragma: no cover print("File problem!! number of groups read not equal to the total number of groups") print(nbgroups) print(cpt) break # pragma: no cover # unseen by coverage peephole optimization s = shlex.split(l) tagname = s[1] Info("Reading Group " + tagname) if s[2] == "1": # node group tag = res.GetNodalTag(tagname) tag.SetIds(np.array([filetointernalid[x] for x in map(int, s[7:])], dtype=MuscatIndex)) # tag.ids = np.zeros(len(s[7:]),dtype=MuscatIndex) # cpt =0 # for i in s[7:]: # tag.ids[cpt] = filetointernalid[int(i)] # cpt +=1 # else: # element group for x in range(7, len(s)): Oid = int(s[x]) # print(Oid) res.AddElementToTagUsingOriginalId(Oid, tagname) cpt += 1 continue Info("Ignoring line : '" + str(l) + "'") self.EndReading() res.PrepareForOutput() return res
from Muscat.IO.IOFactory import RegisterReaderClass RegisterReaderClass(".asc", AscReader)
[docs]def CheckIntegrity(): __checkintegritydata = """ BEGIN_NODES 6 3 1 0 0 0 0 0 295.673175860532 28.0704731415872 346.109138100075 2 0 0 0 0 0 295.105225 28.260575 345.628395 3 0 0 0 0 0 295.180501114015 25.8084581250318 344.876373186428 4 0 0 0 0 0 295.3886425 28.1693925 345.8617875 5 0 0 0 0 0 295.231426751792 27.0671220355891 345.153077365196 6 0 0 0 0 0 295.629817604236 26.9160040797623 345.45435766729 ED._NODES BEGIN_ELEMENTS 2 21 2006 0 0 0 1 2 3 4 5 6 22 3010 0 0 0 1 2 3 1 2 3 1 2 3 1 ED._ELEMENTS BEGIN_GROUPS 2 1 PointGroug 1 0 "PART_ID 2" "" "PART built in Visual-Environment" 1 2 3 2 M2D 2 0 "PART_ID 2" "" "PART built in Visual-Environment" 21 ED._GROUPS garbage """ # check from string res = ReadAsc(string=__checkintegritydata, out=Mesh()) print(res) if res.GetElementsOfType(ED.Triangle_6).originalIds[0] != 21: # pragma: no cover raise # check from file from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory newFileName = TemporaryDirectory().GetTempPath() + "AscFile" open(newFileName, "w").write(__checkintegritydata) res = ReadAsc(fileName=newFileName) print(res) if res.GetElementsOfType(ED.Triangle_6).originalIds[0] != 21: # pragma: no cover raise return "ok"
if __name__ == "__main__": print(CheckIntegrity()) # pragma: no cover