SteveDoyle2 / pyNastran

A Python-based interface tool for Nastran's file formats
Other
384 stars 147 forks source link

PLOAD2 does not support more than 6 elements with non-consecutive ids #760

Closed fmamitrotta closed 6 months ago

fmamitrotta commented 6 months ago

Summary

The implementation of the PLOAD2 card in pyNastran supports either a maximum of 6 elements with non-consecutive ids or unlimited elements with consecutive ids. This does not reflect the behavior of Nastran's PLOAD2 card, which supports an unlimited number of elements with non-consecutive ids.

I do understand that when having a large number of elements assigned to a PLOAD2 card it would be a good practice to have consecutive ids and use the THRU keyword instead of writing a large number of ids in the bdf file. However, it's not always practical to control the numbering of the element ids in medium to large finite element models. Sometimes (like in my case) you have a number of adjacent elements that don't have consecutive ids, but you might still want to apply a pressure load over those elements.

For this reason it would be good to change pyNastran's implementation in order to reflect Nastran's behavior.

Proposed solution

My proposal is that pyNastran should only distinguish between the case where the user has provided a list of consecutive element ids, and in that case use the word THRU when writing the bdf file, and the case where the user has provided a list of non-consecutive ids, and in that case just write the plain list of element ids in the bdf file.

I would change the add_card method of the PLOAD2 class in the following way.

@classmethod
    def add_card(cls, card, comment: str=''):
        """
        Adds a PLOAD2 card from ``BDF.add_card(...)``

        Parameters
        ----------
        card : BDFCard()
            a BDFCard object
        comment : str; default=''
            a comment for the card

        """
        sid = integer(card, 1, 'sid')
        pressure = double(card, 2, 'p')

        # If keyword THRU is used assign first and last element id and check that number of fields is equal to 6
        if integer_string_or_blank(card, 4, 'THRU') == 'THRU':
            e1 = integer(card, 3, 'Element1')
            e2 = integer(card, 5, 'Element1')
            eids = [i for i in range(e1, e2 + 1)]
            assert len(card) == 6, f'len(PLOAD2 card) = {len(card):d}\ncard={card}'
        # Otherwise just assign the list of element ids
        else:
            eids = fields(integer, card, 'eid', i=3, j=len(card))
        return PLOAD2(sid, pressure, eids, comment=comment)

And the raw_fields method in the following way.

def raw_fields(self) -> list:
    list_fields = ['PLOAD2', self.sid, self.pressure]
    eids = self.element_ids
    # Convert list of element ids to numpy array if needed
    if isinstance(eids, list):
        eids = np.array(eids)
    # Sort array of element ids and calculate the differences between consecutive elements
    eids.sort()
    diffs = np.diff(eids)
    # If all differences are equal to 1 use keyword THRU, otherwise just append the list of element ids to the list of fields
    if np.all(diffs == 1):
        list_fields += [eids[0], 'THRU', eids[-1]]
    else:
        list_fields += eids.tolist()
    return list_fields

Versions

platform.python_version() --> 3.9.16 pyNastran.__version__ --> 1.4.0+dev.no.checksum.error (this commit)

SteveDoyle2 commented 6 months ago

Ahh...MSC changed things to be difficult. In the 2005r2 QRG, it's got a max of 6 elements. In 2018, it supports unlimited.

NX/Simcenter supports only 6 elements per card.

fmamitrotta commented 6 months ago

Oh wow, didn't think about this possibility!

What do you reckon? Should there be a check on the Nastran version then?

SteveDoyle2 commented 6 months ago

I'm tempted to just ignore that issue (and use your fix) and just leave that to the user with a note in the add_card method. It's a pain to access the model in the card init. MSC and NX are not that different and with enough time, Siemens will add it.

Lemme think about it more.

fmamitrotta commented 6 months ago

I see your point and I think it makes sense in this case!

No worries, I've implemented a temporary fix in my local version for the moment.

Happy to open a pull request if and when you are definitely conviced of the solution.

SteveDoyle2 commented 6 months ago

I think temporary is fine. Can you make a pull request? Just add a note to the init, add_card, and raw_fields in the PLOAD2 class and the add_pload2 method in the bdf_interface/add_card.py file.

fmamitrotta commented 6 months ago

Pull request made with the notes that you requested 😊

763

fmamitrotta commented 6 months ago

Fixed with #763