# -*- 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.#""" LSDyna file reader. Documentation of the format: http://ftp.lstc.com/anonymous/outgoing/jday/manuals/DRAFT_Vol_I.pdf"""fromMuscat.IO.IOFactoryimportRegisterReaderClassimportnumpyasnpfromMuscat.TypesimportMuscatIndex,MuscatFloatimportMuscat.Containers.ElementsDescriptionasEDfromMuscat.Containers.MeshimportMeshfromMuscat.IO.ReaderBaseimportReaderBasefromMuscat.Helpers.LoggerimportDebug,InfoLSDynaNumber={4:ED.Tetrahedron_4}# 4-point tetraedron
[docs]defReadLSDyna(fileName:str="",string:str="",out=None,printNotRead=True):"""Function API for reading a LSDyna mesh 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 printNotRead : bool, optional if True, prints in console the lines dot understood by the reader, by default True Returns ------- Mesh output unstructured mesh object containing reading result """reader=LSDynaReader()reader.SetFileName(fileName)reader.SetStringToRead(string)returnreader.Read(fileName=fileName,string=string,out=out,printNotRead=printNotRead)
[docs]defListToNumber(list):"""hack where the element type is defined by inspecting the number of identical columns at the end of the connectivity list. """l=len(list)val=list[-1]foriinrange(l-2,-1,-1):iflist[i]!=val:breakreturni+2
[docs]defRead(self,fileName=None,string=None,out=None,printNotRead=True):"""Function that performs the reading of a LSDyna mesh 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 printNotRead : bool, optional if True, prints in console the lines dot understood by the reader, by default True Returns ------- Mesh output unstructured mesh object containing reading result """iffileNameisnotNone:self.SetFileName(fileName)ifstringisnotNone:self.SetStringToRead(string)self.StartReading()ifoutisNone:res=Mesh()else:res=outfiletointernalid={}oidToElementContainer={}oidToLocalElementNumber={}l=self.ReadCleanLine()originalIds=[]nodes=[]nodeSetCounter=0whileTrue:# premature EOFiflisNone:print("ERROR premature EOF: please check the integrity of your .k file")# pragma: no coverbreak# pragma: no cover# if len(l) == 0: l = string.readline().strip('\n').lstrip().rstrip(); continueifl.find("*ELEMENT")>-1:whileTrue:l=self.ReadCleanLine()ifl.find("*")>-1:breaks=LineToListNoQuote(l)n=ListToNumber(s[2:])try:nametype=LSDynaNumber[n]exceptKeyError:# pragma: no coverraiseRuntimeError("Elements with "+str(n)+"vertices not compatible with reader")conn=[xforxinmap(int,s[2:6])]elements=res.GetElementsOfType(nametype)oid=int(s[0])cpt=elements.AddNewElement(conn,oid)oidToElementContainer[oid]=elementsoidToLocalElementNumber[oid]=cptelTag="canonical:"+s[1]elements.tags.CreateTag(elTag,False).AddToTag(cpt-1)continue# pragma: no cover # unseen by coverage peephole optimizationifl.find("*NODE")>-1:dim=3s=Nonecpt=0whileTrue:l=self.ReadCleanLine()ifl.find("*")>-1:breaks=LineToListNoQuote(l)oid=int(s[0])filetointernalid[oid]=cptoriginalIds.append(oid)nodes.append(list(map(float,s[1:dim+1])))cpt+=1continue# pragma: no cover # unseen by coverage peephole optimizationifl.find("*SET_NODE_LIST")>-1:tag=res.GetNodalTag(str(nodeSetCounter))nodeSetCounter+=1self.ReadCleanLine()whileTrue:l=self.ReadCleanLine()ifl.find("*")>-1:breaks=np.array(l.split(),dtype=int)foroidins[s>0]:tag.AddToTag(int(oid))continue# pragma: no cover # unseen by coverage peephole optimizationifl.find("*ED.")>-1:Debug("End file")break# case not treatedifprintNotRead==True:Info("line starting with <<"+l+">> not considered in the reader")l=self.ReadCleanLine()continueself.EndReading()res.nodes=np.array(nodes,dtype=MuscatFloat)res.nodes.shape=(cpt,dim)res.originalIDNodes=np.array(originalIds,dtype=MuscatIndex)defupdateIDsFunc(x):returnfiletointernalid[x]fortaginres.nodesTags:tag.SetIds(np.vectorize(updateIDsFunc)(tag.GetIds()))for_,datainres.elements.items():data.Tighten()data.connectivity=np.vectorize(updateIDsFunc)(data.connectivity)res.PrepareForOutput()returnres