Coverage for readers/gmsh/ui_versions/user_input_v1.py: 21%

1 import json

2 from collections import defaultdict

3

4 import nastranio.cards as nio_cards

5 from nastranio.cards import PROP2ELTS

6 from nastranio.constants import BOUNDARY

7

8 DEFAULT_USERINPUT_TPL = {

9 "format": 1,

10 "exec": {"SOL": "SESTATIC"},

11 "params": {

12 "PRGPST": "YES",

13 "POST": -1,

14 "OGEOM": "NO",

15 "AUTOSPC": "YES",

16 "K6ROT": 100.0,

17 "GRDPNT": 0,

18 "SKIPMGG": "YES",

19 "CHKGRDS": "NO",

20 },

21 "cases": {

22 "default": {

23 "id": -1,

24 "TITLE": "LCID1",

25 "ECHO": "NONE",

26 "DISPLACEMENT(PLOT)": "ALL",

27 "$ SPC": 1,

28 "$ LOAD": 1,

29 }

30 },

31 "attributes": {

32 "materials": {"1": ["MAT1", {"E": 7e6, "NU": 0.33, "RHO": 5000.5}]},

33 "properties": {

34 1: [

35 "PBAR",

36 {"MID": 1, "A": 50.0, "I1": 18.0, "I2": 18.0, "J": 0.1, "I12": 0.0},

37 ],

38 2: [

39 "PBUSH",

40 {

41 "K": "K",

42 "K1": 1e4,

43 "K2": 1e4,

44 "K3": 1e4,

45 "K4": 1e4,

46 "K5": 1e4,

47 "K6": 1e4,

48 },

49 ],

50 3: ["PSHELL", {"MID1": 1, "T": 3.0, "MID2": 1, "MID3": 1}],

51 },

52 "affectations": {},

53 "boundaries": {},

54 "loading": {},

55 },

56}

57

58 DEFAULT_BOUNDARY_INPUT = ["SPC1", {"C": "123456"}]

59 DEFAULT_LOADING_INPUT = [

60 "FORCE",

61 {

62 "CID": 0,

63 "F": 1.0,

64 "N1": 1.0,

65 "N2": 0.0,

66 "N3": 0.0,

67 "as_sum": False,

68 "exclusive": False,

69 },

70]

71

72

73 class UserInput:

74 def load_parameters(self, parameters):

75 self.parameters = parameters

76 self.attributes = parameters["attributes"]

77

78 def to_json(self, **kwargs):

79 return json.dumps(self.parameters, **kwargs)

80

81 def get_card_data(self, cardname, params, **kwargs):

82 card = getattr(nio_cards, cardname)

83 params = params.copy()

84 if "XID" in kwargs:

85 kwargs[card.XID_FIELDNAME] = kwargs.pop("XID")

86 params.update(kwargs)

87 fields_info = card.fields_info()

88 mandatory = set(fields_info["mandatory"])

89 optional = fields_info["optional"]

90 all = mandatory | optional

91 unknown = set(params) - all

92 missing = mandatory - set(params)

93 repeated = fields_info.get("repeated", ())

94 ok_params = {k: v for k, v in params.items() if k in all}

95 ret = {

96 "card": card,

97 "params": ok_params,

98 }

99 if hasattr(card, "LOADING_TYPE") and card.LOADING_TYPE is not None:

100 ret["over"] = [card.LOADING_TYPE]

101 if card.type == BOUNDARY:

102 ret["over"] = ["nodes"]

103 if missing:

104 ret["missing"] = missing

105 if repeated:

106 ret["repeated"] = repeated

107 if unknown:

108 ret["unknown"] = {k: v for k, v in params.items() if k in unknown}

109 return ret

110

111 def get_materials(self):

112 ret = {}

113 for mid, (cardname, params) in self.attributes["materials"].items():

114 ret[mid] = self.get_card_data(cardname, params, XID=mid)

115 return ret

116

117 def get_properties(self):

118 ret = {}

119 for pid, (cardname, params) in self.attributes["properties"].items():

120 ret[pid] = self.get_card_data(cardname, params, XID=pid)

121 return ret

122

123 def get_loading(self):

124 ret = defaultdict(list)

125 for sid, data in self.attributes["loading"].items():

126 for grpname, (cardname, params) in data.items():

127 _data = self.get_card_data(cardname, params, XID=sid)

128 if grpname != "*":

129 _data["over"].append(grpname)

130 ret[sid].append(_data)

131 return dict(ret)

132

133 def get_boundaries(self):

134 ret = defaultdict(list)

135 for sid, data in self.attributes["boundaries"].items():

136 for grpname, (cardname, params) in data.items():

137 _data = self.get_card_data(cardname, params, XID=sid)

138 if grpname != "*":

139 _data["over"].append(grpname)

140 ret[sid].append(_data)

141 return dict(ret)

142

143 def get_affectations(self):

144 ret = {}

145 props = self.get_properties()

146 for grpname, params in self.attributes["affectations"].items():

147 prop_card = props[params["PID"]]["card"]

148 cards_choice = PROP2ELTS[prop_card.__name__]

149 card_by_type = {}

150 for cardname in cards_choice:

151 gmsh_elem_type = getattr(nio_cards, cardname).gmsh_eltype

152 _data = self.get_card_data(cardname, params)

153 _data["over"] = ["elements", grpname]

154 card_by_type[gmsh_elem_type] = _data

155 ret[grpname] = card_by_type

156 return ret