KeyFile¶
- Ressources:
This class is for handling generic Keywords in a KeyFile. The class simply provides comfortable means to access or manipulate the lines of the Keyword. This has the advantage, that writing the Keyword will yield the exact same card including comments except for the changes.
-
class
qd.cae.dyna.
KeyFile
(filepath="", read_keywords=True, parse_mesh=True, load_includes=True, encryption_detection=0.7)¶ - Parameters
- filepathstr
path to the keyfile
- read_keywordsbool
whether to read all keywords
- parse_meshbool
whether the mesh keywords shall be parsed
- load_includesbool
load all includes within the file
- encryption_detectionfloat
detection threshold for encrypted include files. Must be between 0 and 1.
- Returns
- keyfileKeyFile
instance
- Raises
- ValueError
in case of a wrong filepath or invalid encryption threshold
- RuntimeError
if anything goes wrong during reading
Notes
If
read_keywords=True
every keyword found will be loaded and made accessible by the genericKeyword
class. If at the same timeparse_mesh=False
then also the mesh itself is treated as generic keywords.If
parse_mesh=True
, then the mesh keywords are loaded and parsed. Also the mesh specific keyword classes are used (see the keyword classes).The argument
encryption_detection
is used to skip encrypted include files. It is simply tested against the entropy of every include divided by 8 for normalization. Encrypted files usually have a very high entropy. The entropy of a file can be tested with the functionqd.cae.dyna.get_file_entropy
.Examples
>>> # load keyfile and parse the mesh >>> keyfile = KeyFile("path/to/keyfile", parse_mesh=True) >>> # get some mesh data >>> node = keyfile.get_nodeByIndex(0) >>> # check the keywords >>> keyfile.keys() ['*NODE', '*ELEMENT_SHELL', ...] >>> # get all part keywords >>> part_keywords = keyfile["*PART"]
One can also create empty keyfiles and built them.
>>> keyfile = KeyFile(parse_mesh=True) >>> kw = keyfile.add_keyword("*NODE") >>> kw.add_node(1, x=0, y=0, z=0)
Methods
add_keyword
(lines[, position])- Parameters
get_elementByIndex
(element_type, index)- Parameters
get_element_coords
(element_filter)- Parameters
get_element_energy
(element_filter)- Parameters
get_element_history_vars
(element_type)- Parameters
get_element_ids
(element_filter)- Parameters
get_element_node_ids
(element_type, n_nodes)- Parameters
get_element_plastic_strain
(element_filter)- Parameters
get_element_strain
(element_filter)- Parameters
get_element_stress
(element_filter)- Parameters
get_element_stress_mises
(element_filter)- Parameters
get_elements
([element_filter])- Parameters
- Returns
- Returns
- Returns
get_nElements
([element_filter])- Parameters
- Returns
- Returns
get_nodeByID
(id)- Parameters
get_nodeByIndex
(index)- Parameters
- Returns
- Returns
- Returns
- Returns
- Returns
get_partByID
(id)- Parameters
get_partByIndex
(index)- Parameters
get_partByName
(name)- Parameters
- Returns
keys
()- Returns
plot
(self[, export_filepath])Plot the KeyFile geometry.
plot_parts
(parts[, export_filepath])Plot a selected group of parts
remove_keyword
(name, index)- Parameters
save
()keys()
get_elementByID
get_end_keyword_position
-
__init__
(filepath="", read_keywords=True, parse_mesh=True, load_includes=True, encryption_detection=0.7)¶ KeyFile(filepath=””, read_keywords=True, parse_mesh=True, load_includes=True, encryption_detection=0.7)
- Parameters
- filepathstr
path to the keyfile
- read_keywordsbool
whether to read all keywords
- parse_meshbool
whether the mesh keywords shall be parsed
- load_includesbool
load all includes within the file
- encryption_detectionfloat
detection threshold for encrypted include files. Must be between 0 and 1.
- Returns
- keyfileKeyFile
instance
- Raises
- ValueError
in case of a wrong filepath or invalid encryption threshold
- RuntimeError
if anything goes wrong during reading
Notes
If
read_keywords=True
every keyword found will be loaded and made accessible by the genericKeyword
class. If at the same timeparse_mesh=False
then also the mesh itself is treated as generic keywords.If
parse_mesh=True
, then the mesh keywords are loaded and parsed. Also the mesh specific keyword classes are used (see the keyword classes).The argument
encryption_detection
is used to skip encrypted include files. It is simply tested against the entropy of every include divided by 8 for normalization. Encrypted files usually have a very high entropy. The entropy of a file can be tested with the functionqd.cae.dyna.get_file_entropy
.Examples
>>> # load keyfile and parse the mesh >>> keyfile = KeyFile("path/to/keyfile", parse_mesh=True) >>> # get some mesh data >>> node = keyfile.get_nodeByIndex(0) >>> # check the keywords >>> keyfile.keys() ['*NODE', '*ELEMENT_SHELL', ...] >>> # get all part keywords >>> part_keywords = keyfile["*PART"]
One can also create empty keyfiles and built them.
>>> keyfile = KeyFile(parse_mesh=True) >>> kw = keyfile.add_keyword("*NODE") >>> kw.add_node(1, x=0, y=0, z=0)
-
add_keyword
(lines, position=-1)¶ - Parameters
- linesstr or list of str
keyword definition
- positionint
where to position the keyword in the KeyFile, if negative it is appended to the end
Notes
When the KeyFile was created using
parse_mesh=True
, then if a mesh keyword is created, the corresponding classes are returned instead of a generic keyword:NodeKeyword
ElementKeyword
PartKeyword
Examples
Adding *NODE without mesh parsing:
>>> # open a keyfile >>> keyfile = KeyFile("path/to/keyfile.key") >>> # get the node keyword as generic keyword >>> kw = keyfile.add_keyword("*NODE") >>> type(kw) <class 'qd.cae.dyna_cpp.Keyword'>
Adding *NODE with mesh parsing support
>>> keyfile = KeyFile("path/to/keyfile.key", load_mesh=True) >>> kw = type(keyfile.add_keyword(["$ Comment header", >>> "*NODE"])) >>> type(kw) <class 'qd.cae.dyna_cpp.NodeKeyword'> >>> # now it's simple to add nodes >>> node = kw.add_node(id=1, x=0, y=0, z=0)
-
get_elementByIndex
(element_type, index)¶ - Parameters
- element_typeElement.type
type of the element. Must be beam, shell or solid.
- indexint or list(int)
element index or list of indexes
- Returns
- elementElement
Element(s) depending on the arguments
- Raises
- ValueError
if invalid
element_type
is given orindex
does not exist
Examples
>>> # single element >>> elem = femfile.get_elementByIndex(Element.shell, 0) >>> # multiple elements >>> list_of_shells = femfile.get_elementByID(Element.shell, [0,1,2]) >>> >>> femfile.get_elementByIndex(Element.beam, 1).get_type() type.beam >>> femfile.get_elementByIndex(Element.solid, 1).get_type() type.solid >>> femfile.get_elementByIndex(Element.tshell, 1).get_type() type.tshell
-
get_element_coords
(element_filter)¶ - Parameters
- element_filterElement.type
optional type for filtering
- Returns
- fieldnp.ndarray
coords vector of the elements (nElems x nTimesteps x 3)
Notes
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="coords") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> d3plot.get_element_coords(Element.shell).shape (4969, 32. 3)
-
get_element_energy
(element_filter)¶ - Parameters
- element_filterElement.type
optional type for filtering
- Returns
- fieldnp.ndarray
energy of the elements (nElems x nTimesteps)
Notes
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="energy mean") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> d3plot.get_element_energy(Element.shell).shape (4969, 32)
-
get_element_history_vars
(element_type)¶ - Parameters
- element_typeElement.type
type of the element
- Returns
- fieldnp.ndarray
history_vars vector of the elements (nElems x nTimesteps x nHistoryVars)
Notes
IMPORTANT: You can not query the history vars without specifying an element type. In LS-Dyna e.g. history var 7 is different between shells and beams. To prevent wrong usage and bugginess, we enforce this policy here. We didn’t mess this one up!
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="shell history 7 max") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> # Element type is required here! >>> d3plot.get_element_history_vars(Element.shell).shape (4969, 32. 10)
-
get_element_ids
(element_filter)¶ - Parameters
- element_filterElement.type
optional element type for filtering
- Returns
- element_idsnp.ndarray
Ids of the elements
Notes
If the ids for all elements are queried (
Element.none
as filter type) then the returned order is:Element.beam
Element.shell
Element.solid
Element.tshell
Examples
>>> d3plot = D3plot("path/to/d3plot") >>> # get all ids (beam, shell, solid, tshell) >>> d3plot.get_element_ids() array([4687, 4688, 4689, 4690, ...]) >>> # filter only beams >>> d3plot.get_element_ids(Element.beam) array([9545, 9546, 9547, 9511, ...])
-
get_element_node_ids
(element_type, n_nodes)¶ - Parameters
- element_typeElement.type
type of the element
- n_nodesint
number of nodes
- Returns
- element_node_idsnp.ndarray
ids of the nodes for every element (shape elems x n_nodes).
Notes
The number of nodes must be specified since elements of one type may have different number of nodes which would result in a non-uniform matrix.
Examples
>>> d3plot = D3plot("path/to/d3plot") >>> d3plot.get_element_node_ids(Element.shell, 4) array([[347, 354, 343, 344], [354, 355, 342, 343], [349, 352, 356, 348]], dtype=int32)
-
get_element_plastic_strain
(element_filter)¶ - Parameters
- element_filterElement.type
optional type for filtering
- Returns
- fieldnp.ndarray
effective plastic strain of the elements (nElems x nTimesteps)
Notes
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="plastic_strain max") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> d3plot.get_plastic_strain(Element.shell).shape (4969, 32)
-
get_element_strain
(element_filter)¶ - Parameters
- element_filterElement.type
optional type for filtering
- Returns
- fieldnp.ndarray
strain vector of the elements (nElems x nTimesteps x 6)
Notes
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="strain mean") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> d3plot.get_element_strain(Element.shell).shape (4969, 32. 6)
-
get_element_stress
(element_filter)¶ - Parameters
- element_filterElement.type
optional type for filtering
- Returns
- fieldnp.ndarray
stress vector of the elements (nElems x nTimesteps x 6)
Notes
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="stress mean") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> d3plot.get_element_stress(Element.shell).shape (4969, 32. 6)
-
get_element_stress_mises
(element_filter)¶ - Parameters
- element_filterElement.type
optional type for filtering
- Returns
- fieldnp.ndarray
mises stress of the elements (nElems x nTimesteps)
Notes
If an element does not have the respective result, 0 is set as default value.
Examples
>>> d3plot = D3plot("path/to/d3plot", read_states="stress_mises mean") >>> d3plot.get_nElements(Element.shell) 4969 >>> d3plot.get_nTimesteps() 32 >>> d3plot.get_element_stress_mises(Element.shell).shape (4969, 32)
-
get_elements
(element_filter=Element.none)¶ - Parameters
- element_filterElement.type
Optional element type filter. May be beam, shell or solid.
- Returns
- elementslist(Element)
list of Elements
- Raises
- ValueError
if invalid
element_filter
is given.
Notes
Get the elements of the femfile. One may use a filter by type.
Examples
>>> all_elements = femfile.get_elements() >>> shell_elements = femfile.get_elements(Element.shell)
-
get_filepath
()¶ - Returns
- filepathstr
Filepath of the femfile.
Examples
>>> femfile.get_filepath() "path/to/femfile"
-
get_include_dirs
()¶ - Returns
- include_dirslist of str
list of directories searched for includes. This function can be used to check which directories the library recognizes.
Notes
Gets all include directories from every *INCLUDE_PATH or *INCLUDE_PATH_RELATIVE keyword.
Examples
>>> # open a keyfile >>> keyfile = KeyFile("path/to/keyfile.key") >>> # get all includes >>> kf.get_include_dirs() ['path/to/includes', 'path/to/other/includes', ...]
-
get_includes
()¶ - Returns
- includeslist of KeyFile
list of all loaded include files
Notes
In order to load includes, the
load_includes=True
option must be used in the constructor of the KeyFile or one can use theIncludeKeyword.load
function, which every include keyword has.Examples
>>> # open a keyfile >>> keyfile = KeyFile("path/to/keyfile.key") >>> # get all includes >>> len(keyfile.get_includes()) 7
-
get_nElements
(element_filter=Element.none)¶ - Parameters
- element_filterElement.type
Optional element type filter. May be beam, shell or solid.
- Returns
- nElementsint
number of elements
- Raises
- ValueError
if invalid
element_filter
is given.
Examples
>>> femfile.get_nElements() 43156
-
get_nNodes
()¶ - Returns
- nNodesint
number of nodes in the file
Examples
>>> femfile.get_nNodes() 43145
-
get_nParts
()¶ - Returns
- nPartsint
number of parts in the database
Examples
>>> femfile.get_nParts() 7
-
get_nodeByID
(id)¶ - Parameters
- idint or list(int)
node id or list of node ids
- Returns
- nodeNode or list(Node)
requested Node(s)
- Raises
- ValueError
if
idx
does not exist.
Examples
>>> # get by single id >>> node = femfile.get_nodeByID(1) >>> # get a list of nodes at once >>> list_of_nodes = femfile.get_nodeByID( [1,2,3] )
-
get_nodeByIndex
(index)¶ - Parameters
- indexint or list(int)
internal node index or list inf indexes
- Returns
- nodeNode or list(Node)
Node(s)
- Raises
- ValueError
if
index
largerfemfile.get_nNodes()
.
Notes
The internal index starts at 0 and ends at
femfile.get_nNodes()
.Examples
>>> # single index >>> node = femfile.get_nodeByIndex(1) >>> # get a list of nodes at once >>> list_of_nodes = femfile.get_nodeByIndex( [1,2,3] )
-
get_node_acceleration
()¶ - Returns
- node_accelerationnp.ndarray
Accelration of all nodes
Examples
>>> d3plot.get_node_acceleration().shape (4915,1,3)
-
get_node_coords
()¶ - Returns
- node_coordsnp.ndarray
coordinates of all nodes
Examples
>>> d3plot.get_node_coords().shape (4915, 3)
-
get_node_ids
()¶ - Returns
- node_idsnp.ndarray
Ids of all nodes
Examples
>>> d3plot.get_node_ids().shape (100001)
-
get_node_velocity
()¶ - Returns
- node_velocitynp.ndarray
Velocity of all nodes
Examples
>>> d3plot.get_node_velocity().shape (4915,1,3)
-
get_nodes
()¶ - Returns
- nodeslist(Node)
list of node objects
Examples
>>> list_of_nodes = femfile.get_nodes()
-
get_partByID
(id)¶ - Parameters
- idint or list of int
id or ids of the part in the file
- Returns
- partsPart or list of Parts
output depending on arguments
- Raises
- ValueError
if some
id
does not exist.
Examples
>>> part = femfile.get_partByID(1)
-
get_partByIndex
(index)¶ - Parameters
- indexint or list of int
index or indexes of the part in the file
- Returns
- partsPart or list of Parts
output depending on arguments
- Raises
- ValueError
if some
index
does not exist.
Examples
>>> part = femfile.get_partByIndex(0)
-
get_partByName
(name)¶ - Parameters
- namestr
name of the part
- Returns
- partsPart
part with the given name
- Raises
- ValueError
if a part with
name
does not exist.
Examples
>>> part = femfile.get_partByName("Lower Bumper")
-
get_parts
()¶ - Returns
- partslist(Part)
list of all parts in the file
Examples
>>> list_of_all_parts = femfile.get_parts()
-
keys
()¶ - Returns
- keyword_nameslist of str
list of keyword names saved in the file
Examples
>>> keyfile = KeyFile("path/to/keyfile.key") >>> # get a list of all keyword names >>> keyfile.keys() ['*BOUNDARY_SPC_SET_ID', '*PART_CONTACT', '*NODE', ...]
-
plot
(self, export_filepath=None)¶ Plot the KeyFile geometry.
-
static
plot_parts
(parts, export_filepath=None)¶ Plot a selected group of parts
-
remove_keyword
(name, index)¶ - Parameters
- namestr
name of keyword to remove
- indexint
index of keyword in list
Notes
When using
parse_mesh=True
then all mesh keywords are using the following specific classes:NodeKeyword
ElementKeyword
PartKeyword
These mesh keywords cannot be deleted, since deletion of mesh entities is not supported yet.
Examples
>>> # open a keyfile >>> keyfile = KeyFile("path/to/keyfile.key") >>> # remove the second keyword of section shell >>> keyfile.remove_keyword("*SECTION_SHELL",1) >>> # remove all keywords node keywords >>> keyfile.remove_keyword("*NODE")
-
save
()¶ keys()
- Parameters
- filepathstr
path for the output file
- Raises
- RuntimeError
if the output file can not be written
Examples
>>> # open a keyfile >>> keyfile = KeyFile("path/to/keyfile.key") >>> # and write the identical file again >>> keyfile.save("path/to/identical_file.key")