inpKeyword¶
Module Contents¶
This module implements the inpKeyword class, which will parse and store information from an Abaqus keyword
block. The module also contains a couple functions outside the class, but which are closely associated with the class.
- inpKeyword.createParamDictFromString(parameterString, ps=True, useDecimal=True)¶
This function will create a
csidfrom a string representing a keyword block’s parameters.Use
formatParameterOutput()to write a string from the parameter dictionary.- Parameters:
parameterString (str) – A string that contains the parameter information for a keyword block. This should include neither the keyword name nor the comma following the keyword name.
ps (bool) – Short for Preserve Spaces. If True,
inpString,inpDecimal, andinpIntclasses will be used to preserve the exact spacing of the parent string when the items are parsed. Defaults to True.useDecimal (bool) – If True, all numbers that would evaluate to
floatwill instead beinpDecimalorDecimal. Defaults to True.
- Returns:
A case and space insensitive dictionary containing the parameter names (keys) and values.
- Return type:
Examples
This shows the default behavior of the function.
>>> from inpKeyword import createParamDictFromString >>> createParamDictFromString("change number=1000, pole") csid(csiKeyString('change number'): inpInt('1000'), csiKeyString(' pole'): '')
If ps and useDecimal are both set to False, the items in parameterString will be parsed as
str,int, andfloatinstead ofinpString,inpInt, andinpDecimal:>>> createParamDictFromString("change number=1000, pole", ps=False, useDecimal=False) csid(csiKeyString('change number'): 1000, csiKeyString(' pole'): '')
- inpKeyword.findElementNodeNum(t, lines)¶
Returns the number of nodes needed to define an element in the given keyword block.
The element type is determined from
block.parameter['type']. The number of nodes is then retrieved frominpKeyword._elementTypeDictionary, if it is defined, or it is estimated from the element data.For variable node elements, this function looks up the valid number of nodes to define the element. Then, it keeps reading data lines until it finds a line which does not end with a comma. If the number of nodes is in the set of valid node numbers for the element type (
numNodesSet), this function returns an integer. If the number of nodes is not in the set, anElementIncorrectNodeNumErroris raised.For undefined element type labels, this function will read datalines until it finds one which does not end with a comma. It will return the number of nodes it thinks the element has. For accurate results with user or sub-structure elements, the user should add an entry to
_elementTypeDictionarybefore callingparse().This function will only be called once per *ELEMENT keyword block, so it assumes all elements in the keyword block are defined with the same number of nodes.
- Returns:
The number of nodes required for the element type. None: If the keyword block is not an ELEMENT keyword block.
- Return type:
- Raises:
Examples
If the element type is defined in
~inpKeyword._elementTypeDictionary, this function simply looks up the appropriate entry:>>> from inpKeyword import findElementNodeNum >>> lines = ['1, 22, 2, 10, 3, 45, 25, 33, 26', ... '2, 5, 4, 15, 23, 28, 27, 38, 46'] >>> findElementNodeNum('C3D8R', lines) 8
If the element can have a variable number of nodes, this function will read from lines until it finds a line which ends without a comma. It then verifies that the number of nodes is found in
numNodesSetof the element type entry:>>> lines = ['101,101,102,103,104,105,106,107,108,', ... '109,110,111,112,113,114,115,' ... '201,202,203', ... '**', ... '102,103,102,117,106,105,116,108,122,', ... '118,111,121,119,115,114,120,', ... '202,204,205'] >>> findElementNodeNum('c3d15v', lines) 18
If the datalines are not formatted properly, this function will not be able to find the proper number of nodes and will raise an
ElementIncorrectNodeNumError:>>> lines = ['101,101,102,103,104,105,106,107,108,', ... '109,110,111,112,113,114,115,' ... '201,202,203,', ... '**', ... '102,103,102,117,106,105,116,108,122,', ... '118,111,121,119,115,114,120,', ... '202,204,205'] >>> findElementNodeNum('c3d15v', lines) Traceback (most recent call last): ... inpRWErrors.ElementIncorrectNodeNumError: ERROR! An element of type c3d15v must have between 15 and 18 nodes.
For undefined element types (such as sub-structure or user elements), this function will keep reading datalines until one which does not end with a comma is encountered:
>>> lines = ['1, 1451,1452,1453,1454,1455,1456,1457,1483,1484,1485,1486,1487,', ... '1488,1489,1596,1597,1629,1630,1652,1653,1681,1682,1704,1705,', ... '1735,1736,1758,1759,2380,2381,2382,2383,2384,2385,2386,2412,', ... '2413,2414,2415,2416,2417,2418,2525,2526,2558,2559,2581,2582,', ... '2610,2611,2633,2634,2664,2665,2687,2688,7507,7508,7530,7531,', ... '7551,7552,7574,7575,7973,7974,7996,7997,8017,8018,8040,8041'] >>> findElementNodeNum('Z1', lines) WARNING! Element type 'Z1' is not well documented. It looks like this element type needs 72 nodes to define the element. If this is incorrect, please specify the 'numNodes' attribute by running inp._elementTypeDictionary['Z1'] = elType(name='Z1', numNodes=NUM) 72
- inpKeyword.formatStringFromParamDict(parameters, joinPS=',', rmtrailing0=False)¶
Creates a string from the Abaqus keyword
parameterin valid keyword line formatting.The dictionary should be a one-level structure. The output will be of the form key=item. If item is ‘’, then only key will appear in the output. key-item pairs are separated with joinPS, which defaults to ‘,’.
- Parameters:
- Returns:
str
Examples
This shows the creation of a parameter dictionary, modifying one of the values in the dictionary, and creating a string from the dictionary:
>>> from inpKeyword import * >>> d = createParamDictFromString('INC=2000, NLGEOM, UNSYMM=YES') >>> d['inc'] = int(d['inc'] / 2) >>> formatStringFromParamDict(d) 'INC=1000, NLGEOM, UNSYMM=YES'
- inpKeyword._insertComments(data, comments)¶
This function will insert each item in comments to the appropriate location in data.
data should be a list of strings from the data from an
inpKeyword, while comments should beinpKeyword.comments.The user should not call this function in most circumstances. The
_formatDataOutput()and_formatDataLabelDict()functions are meant to call this function.- Parameters:
- Returns:
- Return type:
Examples
We’ll create an
inpKeywordwhich contains a comment line:>>> from inpKeyword import inpKeyword >>> block = inpKeyword('''*ELEMENT, TYPE=C3D4 ... 1, 1, 2, 3, 4 ... **2, 5, 6, 7, 8 ... 3, 9, 10, 11, 12''')
If we print
dataandcomments, we can verify the data and comment lines are stored separately:>>> print(block.data) { 1: 1, 1, 2, 3, 4 3: 3, 9, 10, 11, 12 } >>> print(block.comments) [[1, '**2, 5, 6, 7, 8']]
Finally, calling
formatData()will automatically call the appropriate sub-function for formatting the datalines and comments. The sub-function will automatically call_insertComments()to place the comment strings to their original location:>>> block.formatData() ['\n1, 1, 2, 3, 4', '\n**2, 5, 6, 7, 8', '\n3, 9, 10, 11, 12']
Since the position of the comment lines is stored absolutely, if we delete a data line, the comment will likely not end up in the same relative position:
>>> del block.data[1] >>> block.formatData() ['\n3, 9, 10, 11, 12', '\n**2, 5, 6, 7, 8']
- class inpKeyword.inpKeyword(inputString='', name='', parameter=None, data=None, path='', suboptions=None, comments=None, createSuboptions=True, **inpRWargs)¶
This class creates a blank
inpKeywordobject.The
inpKeywordobject is almost identical in the structure to that created by the inpParser module, except data containers are mostly mutable instead of tuples.Using print on an instance of this object will create a string of the
inpKeywordin valid Abaqus formatting. This could potentially print a huge amount of data, so the user should have some idea what theinpKeywordinstance holds before printing it. This applies to any operation that will trigger__str__().This will print all information corresponding to the
inpKeywordblock, but not any information fromsuboptions. This will includedatathat was read and/or will be written to a sub-input file as specified by the INPUT parameter.- __init__(inputString='', name='', parameter=None, data=None, path='', suboptions=None, comments=None, createSuboptions=True, **inpRWargs)¶
Initializes the
inpKeyword.There are three methods to initialize
inpKeyword. The first is to specify no input arguments to create a blankinpKeyword. The second is to specify any or all of name, parameter, data, path, suboptions, and comments directly; if using this option, each of the items will need to be formatted properly prior to insertion. The third option is to specify inputString, which is a string consisting of the entirety of the Abaqus keyword block in proper Abaqus input file syntax. If inputString is specified, the __init__ process will handle parsing inputString and populating the appropriate attributes, thus sparing the user this task. The user can also pass additional arguments to this function to control certain formatting options. See the inpRWargs entry in the Args section for more details.The most important attributes of
inpKeywordare the following:name,parameter,data,comments,path,suboptionsUsers might need to be aware of these attributes (especially if creating keywords from scratch), but they should not often need to access them directly:
_inpItemsToUpdate,_data,_subdata,_dataParsedThe most important user functions are the following:
parseKWData(),formatData(),formatKeywordLine(),formatParameterOutput(),writeKWandSuboptions(),_setMiscInpKeywordAttrs()- Parameters:
inputString (str) – The string corresponding to the entirety of the keyword block. This must be one string, not a sequence of strings. Defaults to ‘’.
name (str) – The keyword name. Defaults to ‘’.
parameter (csid) – Stores the parameters and their values. Defaults to an empty
csid.data (list) – A list of lists to hold dataline information. Each sublist corresponds to one dataline. Defaults to [].
path (str) – Stores the path to access the keyword block in
keywords. Defaults to ‘’.suboptions (list) – Stores the sub blocks to the
inpKeywordobject iforganize= True. Defaults toinpKeywordSequence.comments (list) – Stores the comments of the
inpKeyword. Defaults to [].createSuboptions (bool) – If True, will set the
suboptionsattribute to a blankinpKeywordSequenceinstance. If False,suboptionswill be None. Defaults to True.inpRWargs (dict) –
Should contain attributes from
inpRWthat are needed for proper formatting and organization of the parsed data.parse()will pass the appropriate values to this function automatically.inpKeywordcan be used without any of these attributes specified, in which case default values that allow a standaloneinpKeywordwill be used. To be consistent with the rest of the parsed keyword blocks, you should pass theinpKeywordArgsdictionary to this function as followsinpKeyword(arg1, arg2, argn, **inp.inpKeywordArgs)
Examples
Here’s an example of creating an
inpKeywordby specifying the inputString argument:>>> from inpKeyword import inpKeyword >>> blocka = inpKeyword('''*Node, nset=Nset-1 ... 1, 0.0, 0.0, 0.0 ... **Comment ... 2, 1.0, 0.0, 0.0''') >>> blocka inpKeyword(name='Node', parameter=csid(csiKeyString(' nset'): inpString('Nset-1', False)), data=csid(inpInt('1'): Node(label=inpInt('1'), data=[inpInt('1'), inpDecimal(' 0.0'), inpDecimal(' 0.0'), inpDecimal(' 0.0')], elements=[]), inpInt('2'): Node(label=inpInt('2'), data=[inpInt('2'), inpDecimal(' 1.0'), inpDecimal(' 0.0'), inpDecimal(' 0.0')], elements=[])), path='', comments=[[1, '**Comment']], suboptions=inpKeywordSequence(num child keywords: 0, num descendant keywords: 0, path: .suboptions)) >>> str(blocka) '\n*Node, nset=Nset-1\n1, 0.0, 0.0, 0.0\n**Comment\n2, 1.0, 0.0, 0.0'
We can create the equivalent block if we forgo the inputString argument and instead specify the name, parameter, data, and comments arguments, although this will be more work for the user in many cases. Example:
>>> from inpKeyword import createParamDictFromString >>> from mesh import * >>> name = 'Node' >>> parameter = createParamDictFromString(' nset=Nset-1') >>> node1 = Node('1, 0.0, 0.0, 0.0') >>> node2 = Node('2, 1.0, 0.0, 0.0') >>> data = Mesh([[i.label, i] for i in [node1, node2]]) >>> comments = [[1, '**Comment']] >>> blockb = inpKeyword(name=name, parameter=parameter, data=data, comments=comments) >>> str(blocka) == str(blockb) True
- preserveSpacing¶
See
inpRW.preserveSpacing. Should be passed from theinpRWinstance via inpRWargs. Defaults to True
- useDecimal¶
See
inpRW.useDecimal. Should be passed from theinpRWinstance via inpRWargs. Defaults to True
- _ParamInInp¶
See
inpRW._ParamInInp. Should be passed from theinpRWinstance via inpRWargs. Defaults to False
- fastElementParsing¶
See
inpRW.fastElementParsing. Should be passed from theinpRWinstance via inpRWargs. Defaults to True
- _joinPS¶
See
inpRW._joinPS. Should be passed from theinpRWinstance via inpRWargs. Defaults to ‘,’
- parseSubFiles¶
See
inpRW.parseSubFiles. Should be passed from theinpRWinstance via inpRWargs. Defaults to False
- inputFolder¶
See
inpRW.inputFolder. Should be passed from theinpRWinstance via inpRWargs. Defaults to ‘’
- rmtrailing0¶
See
inpRW.rmtrailing0. Should be passed from theinpRWinstance via inpRWargs. Defaults to False
- _addSpace¶
See
inpRW._addSpace. Should be passed from theinpRWinstance via inpRWargs. Defaults to ‘’
- _debug¶
See
inpRW._debug. Should be passed from theinpRWinstance via inpRWargs. Defaults to False
- delayParsingDataKws¶
See
inpRW.delayParsingDataKws. Should be passed from theinpRWinstance via inpRWargs. Defaults to set()
- _subinps¶
Contains each sub
inpRWinstance associated for this block. Only populated for *INCLUDE and *MANIFEST.- Type:
- _firstItem¶
See
inpRW._firstItem. Defaults to False
- _leadstr¶
Stores any whitespace characters that occur on a keyword line prior to the * symbol. Defaults to None
- Type:
- _baseStep¶
Stores the base step for *STEP keyword blocks.
- Type:
- _namedRefs¶
See
inpRW.namedRefs. Defaults to csid()
- _inpItemsToUpdate¶
Contains the items from the
inpKeywordblock that need to be set in theinpRWinstance. Defaults to {‘namedRefs’: self._namedRefs}- Type:
- _data¶
A sub-instance of
inpKeywordthat contains thedataandcommentsof the keyword block that are in the main input file if the keyword block is set to read data from a sub-file using the INPUT parameter. Does not exist by default.- Type:
- _subdata¶
A sub-instance of
inpKeywordthat contains thedataandcommentsof the keyword block that are in the sub input file if the keyword block is set to read data from a sub-file using the INPUT parameter. Does not exist by default.- Type:
- _nd¶
A
Meshinstance which will hold node labels and the elements connected to them. Only exists for *ELEMENT keyword blocks.- Type:
- inputString¶
The unparsed string representing the entirety of the keyword block. Will be the value of the inputString parameter for
__init__(). Defaults to ‘’- Type:
- name¶
The name of the keyword block (i.e. “ELEMENT” for a *ELEMENT keyword block). Defaults to ‘’
- Type:
- data¶
Stores the parsed data. Each item in
datawill be a list containing the parsed entries from one dataline. If the keyword block is *NODE or *ELEMENT,datawill instead be aMeshinstance. If the keyword block specifies data in a sub-input file via the INPUT parameter,datawill first list the data from the main input file, and then the data from the sub-input file. Defaults to []
- _dataParsed¶
Indicates if the data for the keyword block has been parsed. Will be set to True by
parseKWData()if the keyword’s data was parsed. Defaults to False- Type:
- path¶
Stores a string representation of the path to the
inpKeywordobject throughkeywords. Defaults to ‘’- Type:
- suboptions¶
Stores the sub blocks to the
inpKeywordobject iforganize= True. The suboptions for each block will be set during the block organization loop ofparse(). Defaults toinpKeywordSequence- Type:
- comments¶
Stores the commented lines with the keyword block (i.e. any line that starts with “**” following the keyword line, but before the next keyword line. Each item in comments will be of the form [ind, line] where ind is the index of the line in
dataand line is the string of the comment line. If the keyword block specifies data in a sub-input file via the INPUT parameter,commentswill first list the comments from the main input file, and then the comments from the sub-input file. Defaults to []- Type:
- lines¶
Stores the input strings for the keyword block; these will have been split on new line characters. This is normally deleted when the block is parsed, but it will include the unparsed strings representing the datalines if the data was not parsed.
- Type:
- formatData(includeSubData=True)¶
This function maps the actual formatting function used by a given keyword block to a consistent name (formatData).
If the inputString or name parameters are specified, formatData will actually be an attribute mapping to the appropriate function for formatting the data of the keyword block. This function exists solely to create the mapping to the real function and call the real function. This function will not be used in most cases.
- Parameters:
includeSubData (bool) – If True, data from
_subdatawill also be included in the output. Defaults to True.- Returns:
A list with a string for each line in
data.- Return type:
Examples
This example shows creating a keyword block by populating the individual fields:
>>> from inpKeyword import inpKeyword, createParamDictFromString >>> name = 'Nset' >>> parameters = createParamDictFromString('nset= "Nset 2"') >>> data = [[1,2,3,4,5]] >>> block = inpKeyword(name=name, parameter=parameters, data=data) >>> block.formatData() ['\n1,2,3,4,5']
Any method which populates the
dataattribute will automatically setformatData()to the appropriate sub-function. We can see this for the previous keyword block:>>> block.formatData.__name__ '_formatDataOutput'
formatData is mapped to
_formatDataLabelDict()for *NODE and *ELEMENT keyword blocks:>>> block2 = inpKeyword('*ELEMENT, elset=test, type=C3D8R\n1, 1, 2, 3, 4, 5, 6, 7, 8') >>> block2.formatData.__name__ '_formatDataLabelDict'
The only case where this is not set automatically is if we create a blank
inpKeywordinstance and then populate it. In this case,formatData()will not be remapped until it has been called once:>>> block3 = inpKeyword() >>> block3name = 'ELSET' >>> block3parameter = createParamDictFromString(' elset=test_elset') >>> block3data = [[1, 2, 3, 4, 5, 6, 7, 8]] >>> block3.name = block3name >>> block3.parameter = block3parameter >>> block3.data = block3data >>> block3.formatData.__name__ 'formatData'
Now calling
formatData()picks the correct sub-function based on the element name and remaps the formatData name from this function to the sub-function:>>> block3.formatData() ['\n1,2,3,4,5,6,7,8'] >>> block3.formatData.__name__ '_formatDataOutput'
- formatKeywordLine()¶
Creates a printable string for the keyword line(s) of the
inpKeywordinstance.Keyword lines include the keyword name and the parameters, but not the datalines. If the
inpKeywordinstance originally had multiple keyword lines, this function will also write out multiple keyword lines.- Returns:
A string representing the keyword line(s).
- Return type:
Examples
First we create a sample keyword block, and then call the function:
>>> from inpKeyword import inpKeyword >>> block = inpKeyword('''*Nset, nset= "Nset 2" ... 1, 2, 3, 4, 5''') >>> block.formatKeywordLine() '\n*Nset, nset= "Nset 2"'
This will also handle cases where the keywordline is split across multiple lines:
>>> block = inpKeyword('''*Element, type=C3D8, ... elset= Elset-1 ... 1, 1, 2, 3, 4, 5, 6, 7, 8''') >>> block.formatKeywordLine() '\n*Element, type=C3D8,\nelset= Elset-1'
If
nameis not set, this function simply returns a blank string:>>> block = inpKeyword() >>> block.formatKeywordLine() ''
- formatParameterOutput()¶
Turns the parameter dictionary into a printable string.
- Returns:
A string representation of the parameter dictionary.
- Return type:
Examples
You can use this function if you need to create a string just from the parameters. The parameters will be written in their original order:
>>> from inpKeyword import inpKeyword >>> block = inpKeyword('''*STEP, INC=2000, NLGEOM, UNSYMM=YES ... STEP 3 - EARTHQUAKE''') >>> block.formatParameterOutput() ' INC=2000, NLGEOM, UNSYMM=YES'
- parseKWData(parseDelayed=False)¶
This function calls
_parseData()to parse keyword block data, and adds the results of that function call todata. It is called during__init__()if the inputString parameter is not ‘’.This function will not parse the keyword block data if
nameis indelayParsingDataKwsand parseDelayed = False (the default). This will allow the user to avoid costly parsing operations if they only need access to the organization of the keywords. If the data is not parsed, it will be stored as a list of strings in thelinesattribute, and this list will be included when a string of the entire keyword block is produced.Note
You should only place keyword block names in
delayParsingDataKwsif you know for certain you will not need to access the data in those keyword blocks.inpRWcurrently does not include a mechanism to automatically parse the data of keyword blocks after theinpKeywordhas been initialized. This will likely be enhanced in the future.If
nameis in_dataKWsand the keyword includes the INPUT parameter,_parseData()will delete thedataattribute and create_dataand_subdata, which are sub-instances ofinpKeyword. The data and comments from the main input file will be stored in_data. IfparseSubFiles= True, this function will also read the data and comments from the sub-file (whose name is specified by the value of the INPUT parameter) and store them to_subdata. The user should rarely need to directly access_dataand_subdata. Ifdataorcommentsare not set,__getattr__()will retrieve the information from_dataand_subdataautomatically.- Parameters:
parseDelayed (bool) – If True, will parse data for any blocks which were set to parse on a later loop. This is controlled by
parse()anddelayParsingDataKws. Defaults to False.- Returns:
None if
datashould not be populated at this time, else no return.- Return type:
Examples
In the most basic case, we parse a simple keyword block, which calls this function automatically:
>>> from inpKeyword import inpKeyword >>> block1 = inpKeyword(r'''*NSET,NSET=MIDPLANE ... 204,303,402,214,315,416 ... 1902,2003,1916,2015 ... **''') >>> block1.data [[inpInt('204'), inpInt('303'), inpInt('402'), inpInt('214'), inpInt('315'), inpInt('416')], [inpInt('1902'), inpInt('2003'), inpInt('1916'), inpInt('2015')]]
If a keyword block has some data and/or comments in the main input file, but some data in a separate file specified by the INPUT parameter, the information from each file will be stored in separate
_dataand_subdataattributes (these keyword names are store inconfig._dataKWs). The user can still simply usedatato access the combined information; the information from the main input file will always be reported first. When the input file is written, any information in_subdatawill be written to the file specified by the INPUT parameter. Example:>>> from config import _slash as sl >>> block2 = inpKeyword(f'''*NODE,NSET=HANDLE,INPUT=sample_input_files{sl}inpKeyword{sl}tennis_rig1.inp ... ** ... **''', parseSubFiles=True) >>> print(block2) *NODE,NSET=HANDLE,INPUT=sample_input_files...inpKeyword...tennis_rig1.inp ** ** 50001, 0.54, -8.425, 0.25 50002, 0.54, -9.80625, 0.25 50003, 0.54, -11.1875, 0.25
We can verify the information is stored in separate containers:
>>> print(block2._data) ** ** >>> print(block2._subdata.data) { 50001: 50001, 0.54, -8.425, 0.25 50002: 50002, 0.54, -9.80625, 0.25 50003: 50003, 0.54, -11.1875, 0.25 }
Of course, if the INPUT parameter is not part of the keyword block,
_dataand_subdatawon’t be created:>>> block3 = inpKeyword('''*NODE ... ** Bottom curve. ... 104, -2.700,-6.625,0. ... 109, 0.000,-8.500,0. ... 114, 2.700,-6.625,0.''') >>> hasattr(block3, '_data') False
If you won’t need to access the data of a keyword type and wish to avoid parsing all blocks with that name, you can include that name in
delayParsingDataKws. You would typically set this at theinpRW, but it’s shown here for illustration. This will save some processing time, especially for keyword blocks with a lot of data.>>> block4 = inpKeyword('''*NODE ... ** Bottom curve. ... 104, -2.700,-6.625,0. ... 109, 0.000,-8.500,0. ... 114, 2.700,-6.625,0.''', delayParsingDataKws={'node'}) >>> block4.data csid()
The data is still stored as a list of unparsed strings in the
linesattribute, so it will still be included when a string of theinpKeywordblock is produced.>>> print(block4) *NODE ** Bottom curve. 104, -2.700,-6.625,0. 109, 0.000,-8.500,0. 114, 2.700,-6.625,0.
Todo
Replace return None with raise a custom exception type.
- printKWandSuboptions(includeSubData=False)¶
This function will print the list of strings generated by by
writeKWandSuboptions().- Parameters:
includeSubData (bool) – If True, will include
dataread from a sub file in the output string list. Defaults to False.
Examples
We’ll first parse an input file:
>>> import inpRW >>> from config import _slash as sl >>> inp = inpRW.inpRW(f'sample_input_files{sl}inpKeyword{sl}mcooot3vlp.inp') >>> inp.parse() Splitting string of input file from ... into strings representing each keyword block. Parsing 36 keyword blocks using 1 cores. ... Updating keyword blocks' paths Searching for *STEP locations Finished parsing mcooot3vlp.inp time to parse = ...
Next, we’ll find the *STEP keyword containing the *TEMPERATURE block and call
printKWandSuboptions()on it:>>> for tempblock in inp.kwg['temperature']: # Each kwg entry is a set, so we must iterate through it to select the only TEMPERATURE block ... break >>> step = inp.getParentBlock(tempblock, parentKWName='step') >>> step.printKWandSuboptions() *STEP,INC=10 STEP 3 - REST OF NONLINEAR *STATIC,DIRECT 1.,10. *BOUNDARY 7,3,,-.005 5,3,,-.005 6,3,,-.005 8,3,,-.005 *TEMPERATURE,INPUT=mcooot3vlp_temp.inp *END STEP
If we activate the includeSubData parameter, the contents of sub-input files will be shown in the output:
>>> step.printKWandSuboptions(includeSubData=True) *STEP,INC=10 STEP 3 - REST OF NONLINEAR *STATIC,DIRECT 1.,10. *BOUNDARY 7,3,,-.005 5,3,,-.005 6,3,,-.005 8,3,,-.005 *TEMPERATURE,INPUT=mcooot3vlp_temp.inp 1,20. 2,20. 3,20. 4,20. 5,20. 6,20. 7,20. 8,20. *END STEP
- writeKWandSuboptions(firstItem=False, includeSubData=False)¶
This function will create a list of strings representing each line of the
inpKeyword, and all blocks insuboptions, except forsuboptionsof *INCLUDE or *MANIFEST blocks. If includeSubData is True, the data read from sub files will also be included.The first character of the output will be a new line character, unless firstItem = True. This prevents adding an extra new line character to the start of the text block or input file.
- Parameters:
- Returns:
A list of strings.
- Return type:
Examples
We’ll first parse an input file:
>>> import inpRW >>> from config import _slash as sl >>> inp = inpRW.inpRW(f'sample_input_files{sl}inpKeyword{sl}mcooot3vlp.inp') >>> inp.parse() Splitting string of input file from ... into strings representing each keyword block. Parsing 36 keyword blocks using 1 cores. ... Updating keyword blocks' paths Searching for *STEP locations Finished parsing mcooot3vlp.inp time to parse = ...
Next, we’ll find the *STEP keyword containing the *TEMPERATURE block and call
printKWandSuboptions()on it:>>> for tempblock in inp.kwg['temperature']: # Each kwg entry is a set, so we must iterate through it to select the only TEMPERATURE block ... break >>> step = inp.getParentBlock(tempblock, parentKWName='step') >>> step.writeKWandSuboptions() ['\n*STEP,INC=10', '\nSTEP 3 - REST OF NONLINEAR', '\n*STATIC,DIRECT', '\n1.,10.', '\n*BOUNDARY', '\n7,3,,-.005', '\n5,3,,-.005', '\n6,3,,-.005', '\n8,3,,-.005', '\n*TEMPERATURE,INPUT=mcooot3vlp_temp.inp', '\n*END STEP']
firstItem = True will prevent a newline character from being added to the first output item:
>>> step.writeKWandSuboptions(firstItem=True) ['*STEP,INC=10', '\nSTEP 3 - REST OF NONLINEAR', '\n*STATIC,DIRECT', '\n1.,10.', '\n*BOUNDARY', '\n7,3,,-.005', '\n5,3,,-.005', '\n6,3,,-.005', '\n8,3,,-.005', '\n*TEMPERATURE,INPUT=mcooot3vlp_temp.inp', '\n*END STEP']
If we activate the includeSubData parameter, the contents of sub-input files will be shown in the output:
>>> step.writeKWandSuboptions(includeSubData=True) ['\n*STEP,INC=10', '\nSTEP 3 - REST OF NONLINEAR', '\n*STATIC,DIRECT', '\n1.,10.', '\n*BOUNDARY', '\n7,3,,-.005', '\n5,3,,-.005', '\n6,3,,-.005', '\n8,3,,-.005', '\n*TEMPERATURE,INPUT=mcooot3vlp_temp.inp', '\n1,20.', '\n2,20.', '\n3,20.', '\n4,20.', '\n5,20.', '\n6,20.', '\n7,20.', '\n8,20.', '\n', '\n*END STEP']
- _cloneKW(attributes=None)¶
_cloneKW(attributes=None)
Creates a new keyword object which is an identical copy of self.
- Parameters:
attributes (list) – A list indicating the attributes of self to copy to the new keyword object. If None, all attributes will be copied. Valid attributes are ‘name’, ‘parameter’, ‘data’, ‘path’, ‘suboptions’, and ‘comments’. Defaults to None.
- Returns:
inpKeyword
Todo
This function needs to be reworked. There are more robust ways to implement it. Use with caution.
- _formatDataOutput(includeSubData=True)¶
Creates a list of printable strings for the datalines of the
inpKeywordinstance.New line characters will be prepended to the string for each line, unless the
inpKeywordinstance has no keyword line and this is the first block in the input file.- Parameters:
includeSubData (bool) – If True, the
dataandcommentsin the_subdata(i.e. data lines read from a sub file) will be included in the output. They will be included after thedataandcommentsfrom the main input file. Defaults to True.- Returns:
A list of strings, with each item corresponding to a dataline.
- Return type:
Examples
In the most basic case, we parse a simple keyword block and then call this function:
>>> from inpKeyword import inpKeyword >>> block1 = inpKeyword(r'''*NSET,NSET=MIDPLANE ... 204,303,402,214,315,416 ... 1902,2003,1916,2015 ... **''') >>> block1._formatDataOutput() ['\n204,303,402,214,315,416', '\n1902,2003,1916,2015', '\n**']
This function will also format data and comments from sub files if includeSubData = True (the default):
>>> from config import _slash as sl >>> block2 = inpKeyword(fr'''*TEMPERATURE,INPUT=sample_input_files{sl}inpKeyword{sl}mcooot3vlp_temp.inp ... **''', parseSubFiles=True) >>> block2._formatDataOutput() ['\n**', '\n1,20.', '\n2,20.', '\n3,20.', '\n4,20.', '\n5,20.', '\n6,20.', '\n7,20.', '\n8,20.', '\n']
If we call it with includeSubData = False, this will return just the information from the main block:
>>> block2._formatDataOutput(includeSubData=False) ['\n**']
- _formatDataLabelDict(includeSubData=True)¶
_formatDataLabelNodeDict(includeSubData=True)
This function will create strings for the datalines for *NODE and *ELEMENT keyword blocks, which have their
datastored in a dictionary-like construct.New line characters will be prepended to the string for each line, unless the
inpKeywordinstance has no keyword line and this is the first block in the input file.- Parameters:
includeSubData (bool) – If True, the
dataandcommentsin the_subdata(i.e. data lines read from a sub file) will be included in the output. They will be included after thedataandcommentsfrom the main input file. Defaults to True.- Returns:
A list of strings, with each item corresponding to a dataline.
- Return type:
Examples
In the most basic case, we parse a simple keyword block and then call this function:
>>> from inpKeyword import inpKeyword >>> block1 = inpKeyword('''*NODE ... ** Bottom curve. ... 104, -2.700,-6.625,0. ... 109, 0.000,-8.500,0. ... 114, 2.700,-6.625,0.''') >>> block1._formatDataLabelDict() ['\n** Bottom curve.', '\n 104, -2.700,-6.625,0.', '\n 109, 0.000,-8.500,0.', '\n 114, 2.700,-6.625,0.']
This function will also format data and comments from sub files if includeSubData = True (the default):
>>> from config import _slash as sl >>> block2 = inpKeyword(fr'''*NODE,NSET=HANDLE,INPUT=sample_input_files{sl}inpKeyword{sl}tennis_rig1.inp ... ** ... **''', parseSubFiles=True) >>> block2._formatDataLabelDict() ['\n**', '\n**', '\n 50001, 0.54, -8.425, 0.25', '\n 50002, 0.54, -9.80625, 0.25', '\n 50003, 0.54, -11.1875, 0.25']
If we call it with includeSubData = False, this will return just the information from the main block:
>>> block2._formatDataLabelDict(includeSubData=False) ['\n**', '\n**']
- _handleComment(line, ind)¶
Appends a list of [
int,str] tocommentsof the keyword block. Theintrepresents the position of the comment in the data, and thestris the unparsed string of the commented line.
- _handleCommentSub(line, ind)¶
Appends a list of [
int,str] tocommentsof the _data keyword block. Theintrepresents the position of the comment in the data, and thestris the unparsed
- _parseData()¶
This function maps the actual parsing function used by a given keyword block to a consistent name (_parseData).
It will be overridden by
_parseKWLine(). In case_parseKWLine()is not called (for example, the attributes of the keyword block are populated manually instead of parsed from a string), this function will overwrite itself with the call to the proper parsing function based onname.
- _parseDataElement()¶
Parses the data in *ELEMENT blocks.
This function will parse the data in the *ELEMENT block. It will also add some information to
_nd. Comments are left as strings and added to the appropriatecommentssection.This function will read a dataline and check if the number of nodes in that dataline matches the element type’s specification. If not, the next dataline will be read until the appropriate number of nodes have been found.
This function has a fast mode which runs if
fastElementParsing= True and the number of nodes to define the element is less than or equal to 10. This assumes the element definition is not split across multiple lines. This should cover the most common Abaqus element types, as long as the pre-processor has not written the element data across more than 1 line.
- _parseDataGeneral()¶
Parses datalines without any special considerations.
This method parses any type of data. It splits the data lines and tries to turn each data item into their non-text type using
eval2(). This function will split each data line on the “,” symbol andeval2()each item individually. All comments are stored incomments, or in the comments attribute if_data, if appropriate.The other _parseData* functions will work similarly to
_parseDataGeneral()(i.e. check if the dataline is a comment, split the dataline on commas to separate items,eval2(item)). The other functions will either take a shortcut ineval2()by specifying the expected data type or will perform some additional action(s) while looping through the data.- Returns:
A list of lists containing the data for the keyword block.
- Return type:
- _parseDataHeading()¶
Parses the datalines of *HEADING keyword blocks.
Special method to parse the *HEADING data lines. Simply returns the raw string as a list of lines without operating on the text.
- Returns:
A list of strings containing the data for the keyword block.
- Return type:
- _parseDataNode()¶
Parses the datalines of *NODE keyword blocks.
This is a special method for parsing *NODE keyword blocks with data lines that must be exclusively integers or floats. All comments are stored in the appropriate
commentssection.
- _parseDataParameter()¶
Parses the datalines of *PARAMETER keyword blocks.
This function parses the datalines of *PARAMETER keyword blocks. In addition to filling
dataof the keyword block, this function will execute all the parameter functions and store the results inpdusing the parameter name as the key. All comments are stored incomments.- Returns:
A list of lists containing the data for the keyword block.
- Return type:
- _parseDataRebarLayer()¶
Parses the datalines of *REBAR LAYER keyword blocks.
This function parses the datalines of *REBAR LAYER keyword blocks. In addition to filling
dataof the keyword block, this function will add the rebar layer names to_namedRefs. All comments are stored incomments.- Returns:
A list of lists containing the data for the keyword block.
- Return type:
- _parseDataSet()¶
Parses the *NSET and *ELSET keyword blocks.
This is a special method for parsing *NSET and *ELSET keyword blocks. For keyword blocks with 100 or more datalines, all items are assumed to be integers (i.e. labels). If items are not labels (i.e. set names), they will be treated as strings. This should increase the performance of the code for very large set keyword blocks, while hurting performance for sets defined by referencing existing sets (which should not have many datalines). For blocks with less than 100 datalines,
_parseDataGeneral()will be called on lines, as this will avoid raising Exceptions ineval2()if a data item is not an integer. All comments are stored incomments.- Returns:
A list of lists containing the data for the keyword block.
- Return type:
- _parseDataSurface()¶
parseDataSurface()
Parses *SURFACE keyword blocks.
This is a special method for parsing *SURFACE keyword blocks. It provides a fast method for TYPE=ELEMENT and TYPE=NODE, which should have much more data then the other types. It has an optimized path if len(lines) > 100. Otherwise, it simply calls
_parseDataGeneral()on the data. All comments are stored incomments.- Returns:
A list of lists containing the data for the keyword block.
- Return type:
- _parseInputString(inputString='', parseKwLine=True)¶
This function will split the
inputStringattribute on new lines, and will identify the keyword line (calling subfunctions to handle the tasks). It will populate thenameandparameterattributes, and it will return a list of strings corresponding to the data lines.If
_debugis not True and parseKwLine is True, this function will setinputStringto ‘’ upon completion as a means to reduce memory consumption.- Parameters:
inputString (str) – A single string (with new line characters) composing the entirety of the keyword block. If inputString is not specified (thus ‘’), this function will parse
inputString. Defaults to ‘’.parseKwLine (bool) – If True, this function will parse the keyword line. Setting this to False will simply split the string into separate lines (used mainly for keyword blocks with no keyword line, which
inpKeyworddoes internally to house data read from sub files). Defaults to True.
- Returns:
A list of strings corresponding to the datalines. The keyword line(s) will not be output if parseKwLine = True.
- Return type:
- _parseKWLine(line)¶
Populates
nameandparameterfrom the keyword line string. This function will also set_parseData()andformatData()based onname.This function is meant to be called by
_parseInputString().- Parameters:
line (str) – A string containing the entirety of the keyword line, but only a single line.
_parseInputString()will handle parameters on subsequent lines for keyword lines which span multiple lines.
- _parseSubData(subName, parentKwLine='')¶
Populates the
dataattributed of a keyword block by reading from subName.This function is meant to be called as part of
_parseData(), as that function will handle some organizational operations. See that function for more details.This function will only be called if
parseSubFilesis True.- Parameters:
subName (str) – A string representing the sub-file from which to read. This should taken from the INPUT parameter of the parent
inpKeywordblock.parentKwLine (str) – A string representing the keyword line of the parent
inpKeywordblock. This is only used to print some information, so it is not strictly necessary. Defaults to ‘’.
- __weakref__¶
list of weak references to the object (if defined)
- _populateRebarLayerNamedRefs()¶
_populateRebarLayerNamedRefs()
This function will populate
_namedRefswith the *REBAR LAYER names and a reference to thisinpKeyword.This function should only be called if you directly specify the
dataattribute of the block, and not if you created the block by parsing inputString. The parsing method will automatically perform the same operation.
- _setMiscInpKeywordAttrs()¶
_setMiscInpKeywordAttrs(parentInp)
This function should be called after the
nameandparameterattributes have been set. It will set_parseData(),formatData(), and set and populate the appropriate items for_inpItemsToUpdate.This function is called by
_parseKWLine(), and thus automatically ifinpKeywordis instanced with the inputString parameter specified. It should be called manually if theinpKeywordis created without parsing inputString.
- _yieldInpRWItemsToUpdate()¶
_yieldInpRWItemsToUpdate():
Yields the items in
_inpItemsToUpdate.The items to yield are set according to the keyword
namein_parseKWLine(). They will be populated as part of the parsing function. These items are needed by theinpRW.inpRWfor further operations, and will be adding when theinpKeywordblock is inserted into theinpKeywordSequence.- Yields:
dict
- __getattr__(name)¶
__getattr__(name)
Overrides the default behavior for cases where
dataandcommentsare not defined (i.e. when the keyword block reads data from a sub file).See
__getattr__()for more information.- Parameters:
name (str) – The name of the attribute to retrieve.
- Returns:
- Return type:
- Raises:
AttributeError – Will be raised if
inpKeyworddoes not have attribute name, unless name is ‘data’ or ‘comments’.
- __str__(includeSubData=True)¶
Produces a string representation from the object in valid Abaqus formatting. All data of this
inpKeywordinstance will be fully expanded.The includeSubData parameter defaults to True and cannot be accessed by the user when using built-in Python functions like
strorprint(). You must specifically callblock.__str__(includeSubData=False)if you need to modify this parameter.writeBlocks()sets this to False to prevent writing the_subdatawith the main keyword block; the_subdatawill instead be written to the proper subfile.- Parameters:
includeSubData (bool) – If True, the string of this
inpKeywordwill include all sub data. Defaults to True.- Returns:
str
- __repr__(expand=False)¶
Produces a string representation of the
inpKeywordobject.By default, this will not expand
suboptionsor the entirety ofdataif len(data)>10. Set expand = True to display the entirety ofdataand allsuboptionsand theirsuboptions. Be aware that the block could contain an enormous amount of information.- Parameters:
expand (bool) – If True, print the entire contents of the
inpKeyword. Defaults to False.- Returns:
str