From 4d3f4dacf522ddb0522b0a7355cf596c8003e898 Mon Sep 17 00:00:00 2001 From: Johannes Mueller Date: Wed, 9 Oct 2024 17:22:20 +0200 Subject: [PATCH] Add proper error message if unsupported elements are in the instance Signed-off-by: Johannes Mueller --- tools/odbclient/tests/test_odbclient.py | 36 ++++++++++++++++++++++--- tools/odbserver/odbserver/interface.py | 22 ++++++++++----- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/tools/odbclient/tests/test_odbclient.py b/tools/odbclient/tests/test_odbclient.py index ab6524aa..080113d3 100644 --- a/tools/odbclient/tests/test_odbclient.py +++ b/tools/odbclient/tests/test_odbclient.py @@ -50,6 +50,10 @@ def test_odbclient_instances(client): np.testing.assert_array_equal(client.instance_names(), ['PART-1-1']) +def test_odbclient_invalid_instance(client): + with pytest.raises(KeyError): + client.node_coordinates("FOO-1-1") + def test_odbclient_node_coordinates(client): expected = pd.read_csv('tests/node_coordinates.csv', index_col='node_id') pd.testing.assert_frame_equal(client.node_coordinates('PART-1-1'), expected) @@ -160,24 +164,50 @@ def test_variable_stress_integration_point(client): result.to_csv('tests/stress_integration_point.csv') pd.testing.assert_frame_equal(result, expected) + @pytest.fixture def client_history(): return odbclient.OdbClient('tests/history_output_test.odb') + def test_history_region_empty(client): assert client.history_regions("Load") == ['Assembly ASSEMBLY'] + def test_history_region_non_empty(client_history): - assert client_history.history_regions("Step-1") == ['Assembly ASSEMBLY', 'Element ASSEMBLY.1', 'Node ASSEMBLY.1', 'Node ASSEMBLY.2'] + assert client_history.history_regions("Step-1") == [ + 'Assembly ASSEMBLY', + 'Element ASSEMBLY.1', + 'Node ASSEMBLY.1', + 'Node ASSEMBLY.2', + ] + def test_history_outputs(client_history): - assert client_history.history_outputs("Step-1", 'Element ASSEMBLY.1') == ['CTF1', 'CTF2', 'CTF3', 'CTM1', 'CTM2', 'CTM3', 'CU1', 'CU2', 'CU3', 'CUR1', 'CUR2', 'CUR3'] + assert client_history.history_outputs("Step-1", 'Element ASSEMBLY.1') == [ + 'CTF1', + 'CTF2', + 'CTF3', + 'CTM1', + 'CTM2', + 'CTM3', + 'CU1', + 'CU2', + 'CU3', + 'CUR1', + 'CUR2', + 'CUR3', + ] def test_history_output_values(client_history): assert client_history.history_output_values("Step-1", 'Element ASSEMBLY.1', 'CTF1').array[1] == pytest.approx(0.09999854117631912) + def test_history_region_description(client_history): - assert client_history.history_region_description("Step-1", 'Element ASSEMBLY.1') == "Output at assembly ASSEMBLY instance ASSEMBLY element 1" + assert ( + client_history.history_region_description("Step-1", 'Element ASSEMBLY.1') + == "Output at assembly ASSEMBLY instance ASSEMBLY element 1" + ) def test_history_info(client_history): diff --git a/tools/odbserver/odbserver/interface.py b/tools/odbserver/odbserver/interface.py index b165a1f9..f1a58eb9 100644 --- a/tools/odbserver/odbserver/interface.py +++ b/tools/odbserver/odbserver/interface.py @@ -28,7 +28,6 @@ class OdbInterface: def __init__(self, odbfile): self._odb = ODB.openOdb(odbfile) self._asm = self._odb.rootAssembly - self._index_cache = {} def instance_names(self): @@ -240,11 +239,22 @@ def index_block_data(block): def _instance_or_rootasm(self, instance_name): if instance_name == b'': - return self._asm - try: - return self._asm.instances[instance_name] - except Exception as e: - return e + instance = self._asm + else: + instance = self._asm.instances[instance_name] + + element_types = {el.type for el in instance.elements} + unsupported_types = {et for et in element_types if et[0] != "C"} + if unsupported_types: + raise ValueError( + "Only continuum elements (C...) are supported at this point, sorry. " + "Please submit an issue to https://github.com/boschresearch/pylife/issues " + "if you need to support other types. " + "(Unsupported types %s found in instance %s)" % ( + ", ".join(unsupported_types), instance_name + )) + + return instance def history_regions(self, step_name): """Get history regions, which belongs to the given step.