Coverage for cards/__init__.py: 77%

52 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-03-20 20:51 +0100

1import ast 

2import glob 

3import os 

4from collections import defaultdict 

5 

6from nastranio.cards.axis import * 

7from nastranio.cards.elements import * 

8from nastranio.cards.loading import * 

9from nastranio.cards.materials import * 

10from nastranio.cards.properties import * 

11from nastranio.cardslib import SimpleCard 

12from nastranio.constants import CARDS_REGISTER, ELEMENT, PROPERTY 

13 

14ELT2PROPS = None 

15PROP2ELTS = None 

16 

17 

18def collection(): 

19 """ 

20 return collection of cards as a set 

21 """ 

22 return frozenset(CARDS_REGISTER) 

23 

24 

25def associate(): 

26 """create nastranio.cards.e2c and nastranio.cards.c2e dictionnaries""" 

27 from nastranio.cards import elements, properties 

28 

29 global ELT2PROPS, PROP2ELTS 

30 ELT2PROPS = {} 

31 PROP2ELTS = defaultdict(set) 

32 property_cards = { 

33 c: card 

34 for c in collection() 

35 if ( 

36 hasattr(properties, c) and (card := getattr(properties, c)).type == PROPERTY 

37 ) 

38 } 

39 element_cards = { 

40 c: card 

41 for c in collection() 

42 if (hasattr(elements, c) and (card := getattr(elements, c)).type == ELEMENT) 

43 } 

44 _exclusive_c2p = {} 

45 for cardname in collection(): 

46 try: 

47 card = getattr(elements, cardname) 

48 except AttributeError: 

49 continue 

50 if card.type == ELEMENT: 

51 # check if we have a property by name 

52 prop_candidate = f"P{cardname[1:]}" 

53 if prop_candidate in property_cards: 

54 props = set((prop_candidate,)) 

55 _exclusive_c2p[cardname] = prop_candidate 

56 else: 

57 # get by dimension, if property card has PID field 

58 props = { 

59 _cardname 

60 for _cardname, pcard in property_cards.items() 

61 if ( 

62 pcard.DIM == card.dim 

63 and hasattr(card, "PID_FIELDNAME") 

64 and f"C{_cardname[1:]}" not in element_cards 

65 ) 

66 } 

67 ELT2PROPS[cardname] = props 

68 for prop in props: 

69 PROP2ELTS[prop].add(cardname) 

70 PROP2ELTS = dict(PROP2ELTS) 

71 

72 

73class DefaultCard(SimpleCard): 

74 """default container for unknow cards""" 

75 

76 def append_fields_list(self, fields): 

77 """fields are provided as text, without the card name""" 

78 # insert TWO dummy fields such as index in fields list match NASTRAN field 

79 fields = ["", ""] + fields 

80 # ================================================================== 

81 # read fixed fields 

82 # ================================================================== 

83 _fields = {} 

84 nb_appended = max([len(v) for v in self.carddata["main"].values()], default=0) 

85 for ix, value in enumerate(fields): 

86 header = f"field#{ix}" 

87 if header not in self.carddata["main"] and nb_appended > 0: 

88 # maybe first time this fields is appended 

89 # we need to create a list full of None 

90 self.carddata["main"][header] = [None] * nb_appended 

91 self.carddata["main"][header].append(value) 

92 _fields[ix] = header 

93 _fields = { 

94 k: v for k, v in _fields.items() if (k % 10 != 0 and (k - 1) % 10 != 0) 

95 } 

96 self.fields.update(_fields) 

97 return fields 

98 

99 

100associate()