|
29 | 29 | import netCDF4 |
30 | 30 | from fourcipp.fourc_input import FourCInput |
31 | 31 |
|
32 | | -from cubitpy.conf import cupy |
| 32 | +from cubitpy.conf import GeometryType, cupy |
33 | 33 | from cubitpy.cubit_group import CubitGroup |
34 | 34 | from cubitpy.cubit_to_fourc_input import ( |
35 | 35 | add_exodus_geometry_section, |
@@ -421,6 +421,87 @@ def reset(self): |
421 | 421 | self.cubit.reset() |
422 | 422 | self._default_cubit_variables() |
423 | 423 |
|
| 424 | + def cmd_return(self, cmd: str, geometry_type: GeometryType, **kwargs): |
| 425 | + """Run a cubit command and return the created geometry object. |
| 426 | +
|
| 427 | + Args: |
| 428 | + cmd: The cubit command to run. |
| 429 | + geometry_type: The geometry type that should be checked for a new geometry. |
| 430 | + kwargs: Will be passed on to `cmd_return_dict`. |
| 431 | +
|
| 432 | + Returns: |
| 433 | + If a single geometry object of the given type is created, this object |
| 434 | + is returned. This function expects that a single geometry item of the given |
| 435 | + type is created, otherwise an error will be raised. For use cases, where one |
| 436 | + expects a variable amount of created items or wants to check multiple |
| 437 | + different geometry types, please refer to `cmd_return_dict`. |
| 438 | + """ |
| 439 | + geometry_dict = self.cmd_return_dict(cmd, [geometry_type], **kwargs) |
| 440 | + if len(geometry_dict[geometry_type]) == 1: |
| 441 | + return geometry_dict[geometry_type][0] |
| 442 | + else: |
| 443 | + raise ValueError( |
| 444 | + f"Expected a single created item of type {geometry_type}, but got {geometry_dict[geometry_type]}" |
| 445 | + ) |
| 446 | + |
| 447 | + def cmd_return_dict( |
| 448 | + self, |
| 449 | + cmd: str, |
| 450 | + geometry_types: list[GeometryType], |
| 451 | + *, |
| 452 | + filter_sheet_bodies: bool = True, |
| 453 | + ): |
| 454 | + """Run a cubit command and return created geometry objects. |
| 455 | +
|
| 456 | + Args: |
| 457 | + cmd: The cubit command to run. |
| 458 | + geometry_types: The geometry types that should be checked for new geometries. |
| 459 | + filter_sheet_bodies: If volumes that are sheet bodies should be ignored. |
| 460 | + Defaults to true. |
| 461 | +
|
| 462 | + Returns: |
| 463 | + A dictionary of the created geometry objects. The dictionary keys are the |
| 464 | + geometry types, the values are lists containing the respective objects. |
| 465 | + """ |
| 466 | + |
| 467 | + # Store the already existing ids for all requested geometry types. |
| 468 | + geometry_ids_before = { |
| 469 | + geometry: set(self.get_entities(geometry.get_cubit_string())) |
| 470 | + for geometry in geometry_types |
| 471 | + } |
| 472 | + |
| 473 | + # For CoreForm, we need to check that the volumes are not sheet bodies |
| 474 | + if cupy.is_coreform() and filter_sheet_bodies: |
| 475 | + if cupy.geometry.volume in geometry_ids_before: |
| 476 | + volume_ids_before = geometry_ids_before[cupy.geometry.volume] |
| 477 | + volume_ids_before_no_sheet_bodies = { |
| 478 | + id for id in volume_ids_before if not self.is_sheet_body(id) |
| 479 | + } |
| 480 | + geometry_ids_before[cupy.geometry.volume] = ( |
| 481 | + volume_ids_before_no_sheet_bodies |
| 482 | + ) |
| 483 | + |
| 484 | + # Run the command. |
| 485 | + self.cmd(cmd) |
| 486 | + |
| 487 | + # Get the objects that were created by the command. |
| 488 | + create_objects = {} |
| 489 | + for geometry, ids_before in geometry_ids_before.items(): |
| 490 | + ids_after = set(self.get_entities(geometry.get_cubit_string())) |
| 491 | + ids_new = ids_after - ids_before |
| 492 | + |
| 493 | + if ( |
| 494 | + cupy.is_coreform() |
| 495 | + and filter_sheet_bodies |
| 496 | + and geometry == cupy.geometry.volume |
| 497 | + ): |
| 498 | + ids_new = {id for id in ids_new if not self.is_sheet_body(id)} |
| 499 | + |
| 500 | + geometry_objects = self.get_items(geometry, item_ids=ids_new) |
| 501 | + create_objects[geometry] = geometry_objects |
| 502 | + |
| 503 | + return create_objects |
| 504 | + |
424 | 505 | def display_in_cubit(self, labels=[], delay=0.5, testing=False): |
425 | 506 | """Save the state to a cubit file and open cubit with that file. |
426 | 507 | Additionally labels can be displayed in cubit to simplify the mesh |
|
0 commit comments