qd-cae/qd-cae-python

Problems with keyword modifying

nikorose87 opened this issue · 9 comments

Hi Codie

I was following your, tutorial established in the 15th international conference of LS-DYNA, and I want to modify the material on the keyword in order to run many simulations, so let's say that we have:

>>> kf = KeyFile("sample_snapping.k",  read_keywords=True,    parse_mesh=True,            load_includes=True)

>>> kp0 = kf['*PART'][0]
>>>print(kp0)

*PART
$#                                                                         title
CTSD                                                                  
$#     pid     secid       mid     eosid      hgid      grav    adpopt      tmid
2                  1         3         0         0         0         0         0

>>> kp0['mid'] = 2

Here the error come up:

ValueError: Can not find field: mid in comments.

If I set the command kp0.get_lines(), only the first part is shown:

['*PART',
'$# title']

Am I doing something wrong?

Thanks

Hey buddy, I tried to reproduce using the fragment above (put it into yay.key) but failed ... Do you have an isolated example or file you can share with me?

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. 
>>> from qd.cae.dyna import * 
>>> kf = KeyFile('yay.key', read_keywords=True, parse_mesh=True, load_includes=True) 
>>> kp0 = kf['*PART'][0] 
>>> print(kp0)  
*PART 
$#                                                                         title 
CTSD 
$#     pid     secid       mid     eosid      hgid      grav    adpopt      tmid 
2                  1         3         0         0         0         0         0   
>>> kp0['mid'] = 2 
>>> print(kp0)  
*PART 
$#                                                                         title 
CTSD 
$#     pid     secid       mid     eosid      hgid      grav    adpopt      tmid
2                  12                  0         0         0         0         0   
>>> kp0.get_lines() 
['', '*PART', 
'$#                                                                         title', 
'CTSD                                                                  ', 
'$#     pid     secid       mid     eosid      hgid      grav    adpopt      tmid', 
'2                  12                  0         0         0         0         0', 
'']

There you go!
codie.zip

Thanks, will try it.

However, after aligning the part wanted to modify, I could modify the text value.

However, after aligning the part wanted to modify, I could modify the text value.

Such bugs never let me sleep well ... I can reproduce now thanks. If I can't find the issue right now it might take a few days since I'm at the german dyna forum next week.

Analysis

Ok the issue is quite simple: since the mesh is parsed, the part is not stored as a string but as a real part in the mesh database. The code therefore uses the pid field and stores the rest including mid in another buffer, which is not searched/accessed when the bracket operator [] is called from python. This also explains why the part is still correct when written to the file, but you don't see the rest of the data in kw.get_lines(), since it is stored in another buffer.

Circumvention

As a circumvention you can disable mesh parsing by parse_mesh=False in the constructor. It saves time anyways if you really don't modify the mesh.

Fix

I need to think about how to fix it internally with given datastructure, I don't intend though to leave it as it is.

Thanks so much Codie!

Effectively, I set the parse_mesh=False and It worked. I will modify the data in mesh, but I will do it in separate files and later merge them.

Thanks a lot, have a nice forum.

Sry for the inconvience ... fixing it may seem simple, but the part keyword has some internal conditionals which are annoying to handle ...

Will be officially fixed in version 0.8.4. If you require a fixed version, simply clone the dev branch. Tests run through so it seems fine.