Skip to content

Commit b4cb809

Browse files
Use new FourCIPP type converter
1 parent 67af518 commit b4cb809

File tree

6 files changed

+34
-75
lines changed

6 files changed

+34
-75
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dynamic = ["version"]
3030

3131
[project.optional-dependencies]
3232
cubitpy = ["cubitpy@git+https://github.com/imcs-compsim/cubitpy.git@main"]
33-
fourc = ["fourcipp@git+https://github.com/4c-multiphysics/fourcipp.git@main"]
33+
fourc = ["fourcipp@git+https://github.com/davidrudlstorfer/fourcipp.git@add_type_converter"]
3434
dev = [
3535
"coverage-badge",
3636
"coverage",

src/meshpy/core/node.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,9 @@ def rotate(self, *args, **kwargs):
9696
def dump_to_list(self):
9797
"""Return a list with the legacy string representing this node."""
9898

99-
# TODO here a numpy data type is converted to a standard Python
100-
# data type. Once FourCIPP can handle non standard data types,
101-
# this should be removed.
10299
return {
103100
"id": self.i_global,
104-
"COORD": self.coordinates.tolist(),
101+
"COORD": self.coordinates,
105102
"data": {"type": "NODE"},
106103
}
107104

@@ -152,11 +149,8 @@ def dump_to_list(self):
152149
"""Return a list with the legacy string representing this control
153150
point."""
154151

155-
# TODO here a numpy data type is converted to a standard Python
156-
# data type. Once FourCIPP can handle non standard data types,
157-
# this should be removed.
158152
return {
159153
"id": self.i_global,
160-
"COORD": self.coordinates.tolist(),
154+
"COORD": self.coordinates,
161155
"data": {"type": "CP", "weight": self.weight},
162156
}

src/meshpy/core/nurbs_patch.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def dump_element_specific_section(self, input_file):
108108
"DEGREE": self.polynomial_orders[dir_manifold],
109109
"TYPE": knotvector_type,
110110
"knots": [
111-
float(knot_vector_val)
111+
knot_vector_val
112112
for knot_vector_val in self.knot_vectors[dir_manifold]
113113
],
114114
}
@@ -208,18 +208,15 @@ def get_ids_ctrlpts_surface(knot_span_u, knot_span_v):
208208
):
209209
element_cps_ids = get_ids_ctrlpts_surface(knot_span_u, knot_span_v)
210210

211-
connectivity = [int(self.nodes[i].i_global) for i in element_cps_ids]
211+
connectivity = [self.nodes[i].i_global for i in element_cps_ids]
212212

213213
num_cp_in_element = (self.polynomial_orders[0] + 1) * (
214214
self.polynomial_orders[1] + 1
215215
)
216216

217-
# TODO here a numpy data type is converted to a standard Python
218-
# data type. Once FourCIPP can handle non standard data types,
219-
# this should be removed.
220217
patch_elements.append(
221218
{
222-
"id": int(self.i_global + j),
219+
"id": self.i_global + j,
223220
"cell": {
224221
"type": f"NURBS{num_cp_in_element}",
225222
"connectivity": connectivity,
@@ -308,22 +305,17 @@ def get_ids_ctrlpts_volume(knot_span_u, knot_span_v, knot_span_w):
308305
knot_span_u, knot_span_v, knot_span_w
309306
)
310307

311-
connectivity = [
312-
int(self.nodes[i].i_global) for i in element_cps_ids
313-
]
308+
connectivity = [self.nodes[i].i_global for i in element_cps_ids]
314309

315310
num_cp_in_element = (
316311
(self.polynomial_orders[0] + 1)
317312
* (self.polynomial_orders[1] + 1)
318313
* (self.polynomial_orders[2] + 1)
319314
)
320315

321-
# TODO here a numpy data type is converted to a standard Python
322-
# data type. Once FourCIPP can handle non standard data types,
323-
# this should be removed.
324316
patch_elements.append(
325317
{
326-
"id": int(self.i_global + increment_ele),
318+
"id": self.i_global + increment_ele,
327319
"cell": {
328320
"type": f"NURBS{num_cp_in_element}",
329321
"connectivity": connectivity,

src/meshpy/four_c/element_beam.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,17 @@ def dump_to_list(self):
5151
# Check the material.
5252
self._check_material()
5353

54-
# TODO here a numpy data type is converted to a standard Python
55-
# data type. Once FourCIPP can handle non standard data types,
56-
# this should be removed.
5754
return {
5855
"id": self.i_global,
5956
"cell": {
6057
"type": "HERM2LINE3",
61-
"connectivity": [int(self.nodes[i].i_global) for i in [0, 2, 1]],
58+
"connectivity": [self.nodes[i].i_global for i in [0, 2, 1]],
6259
},
6360
"data": {
6461
"type": "BEAM3R",
6562
"MAT": self.material.i_global,
6663
"TRIADS": [
67-
float(item)
64+
item
6865
for i in [0, 2, 1]
6966
for item in self.nodes[i].rotation.get_rotation_vector()
7067
],
@@ -89,20 +86,17 @@ def dump_to_list(self):
8986
# Check the material.
9087
self._check_material()
9188

92-
# TODO here a numpy data type is converted to a standard Python
93-
# data type. Once FourCIPP can handle non standard data types,
94-
# this should be removed.
9589
return {
9690
"id": self.i_global,
9791
"cell": {
9892
"type": "LINE2",
99-
"connectivity": [int(self.nodes[i].i_global) for i in [0, 1]],
93+
"connectivity": [self.nodes[i].i_global for i in [0, 1]],
10094
},
10195
"data": {
10296
"type": "BEAM3R",
10397
"MAT": self.material.i_global,
10498
"TRIADS": [
105-
float(item)
99+
item
106100
for i in [0, 1]
107101
for item in self.nodes[i].rotation.get_rotation_vector()
108102
],
@@ -141,22 +135,19 @@ def dump_to_list(self):
141135
# Check the material.
142136
self._check_material()
143137

144-
# TODO here a numpy data type is converted to a standard Python
145-
# data type. Once FourCIPP can handle non standard data types,
146-
# this should be removed.
147138
return {
148139
"id": self.i_global,
149140
"cell": {
150141
"type": "LINE3",
151-
"connectivity": [int(self.nodes[i].i_global) for i in [0, 2, 1]],
142+
"connectivity": [self.nodes[i].i_global for i in [0, 2, 1]],
152143
},
153144
"data": {
154145
"type": "BEAM3K",
155146
"WK": 1 if self.weak else 0,
156147
"ROTVEC": 1 if self.rotvec else 0,
157148
"MAT": self.material.i_global,
158149
"TRIADS": [
159-
float(item)
150+
item
160151
for i in [0, 2, 1]
161152
for item in self.nodes[i].rotation.get_rotation_vector()
162153
],
@@ -210,14 +201,11 @@ def dump_to_list(self):
210201
"The rotations do not match the direction of the Euler Bernoulli beam!"
211202
)
212203

213-
# TODO here a numpy data type is converted to a standard Python
214-
# data type. Once FourCIPP can handle non standard data types,
215-
# this should be removed.
216204
return {
217205
"id": self.i_global,
218206
"cell": {
219207
"type": "LINE2",
220-
"connectivity": [int(self.nodes[i].i_global) for i in [0, 1]],
208+
"connectivity": [self.nodes[i].i_global for i in [0, 1]],
221209
},
222210
"data": {
223211
"type": "BEAM3EB",

src/meshpy/four_c/input_file.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,17 @@ class InputFile(_FourCInput):
114114
def __init__(self, sections=None):
115115
"""Initialize the input file."""
116116

117+
super().__init__(sections=sections)
118+
117119
# Contents of NOX xml file.
118120
self.nox_xml_contents = ""
119121

120-
super().__init__(sections=sections)
122+
# Register converters to directly convert non-primitive types
123+
# to native Python types via the FourCIPP type converter.
124+
self.type_converter.register_numpy_types()
125+
self.type_converter.register_type(
126+
_Function, lambda converter, obj: obj.i_global
127+
)
121128

122129
def add(self, object_to_add, **kwargs):
123130
"""Add a mesh or a dictionary to the input file.
@@ -131,7 +138,7 @@ def add(self, object_to_add, **kwargs):
131138
self.add_mesh_to_input_file(mesh=object_to_add, **kwargs)
132139

133140
else:
134-
super().combine_sections(object_to_add, **kwargs)
141+
super().combine_sections(object_to_add)
135142

136143
def dump(
137144
self,
@@ -336,36 +343,10 @@ def _dump_mesh_items(section_name, data_list):
336343
elif hasattr(item, "dump_to_list"):
337344
list.append(item.dump_to_list())
338345
elif isinstance(item, _BoundaryCondition):
339-
# Here we need to convert the function objects to their
340-
# global index.
341-
342-
def convert_function_field(key, value):
343-
"""Convert function objects in boundary condititions to
344-
their global index.
345-
346-
TODO improve this approach
347-
"""
348-
349-
if key != "FUNCT":
350-
return value
351-
352-
if isinstance(value, _List):
353-
return [
354-
v.i_global if isinstance(v, _Function) else v
355-
for v in value
356-
]
357-
if isinstance(value, _Function):
358-
return value.i_global
359-
else:
360-
return value
361-
362346
list.append(
363347
{
364348
"E": item.geometry_set.i_global,
365-
**{
366-
key: convert_function_field(key, value)
367-
for key, value in item.data.items()
368-
},
349+
**item.data,
369350
}
370351
)
371352

@@ -374,11 +355,15 @@ def convert_function_field(key, value):
374355
else:
375356
raise TypeError(f"Could not dump {item}")
376357

358+
# If section already exists, retrieve from input file and
359+
# add newly. We always need to go through fourcipp to convert
360+
# the data types correctly.
377361
if section_name in self.sections:
378-
# If the section already exists, append the new data to it.
379-
self.sections[section_name].extend(list)
380-
else:
381-
self.add({section_name: list})
362+
existing_entries = self.pop(section_name)
363+
existing_entries.extend(list)
364+
list = existing_entries
365+
366+
self.add({section_name: list})
382367

383368
# Add sets from couplings and boundary conditions to a temp container.
384369
mesh.unlink_nodes()

tests/test_four_c_simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def test_four_c_simulation_dirichlet_boundary_to_neumann_boundary_with_all_value
920920
"VAL": [
921921
0,
922922
0,
923-
float(0.25 * np.sin(node.coordinates[0] * np.pi)),
923+
0.25 * np.sin(node.coordinates[0] * np.pi),
924924
0,
925925
0,
926926
0,

0 commit comments

Comments
 (0)