From d97f72f3a114a91edb92c36a53df7eb97cdc3c83 Mon Sep 17 00:00:00 2001 From: Antoine Karcher <128720408+ansys-akarcher@users.noreply.github.com> Date: Fri, 27 Oct 2023 18:03:13 +0200 Subject: [PATCH] Don't create LocalField when using InProcess (#1243) * do not copy if field is in process * use _server to discriminate InProcess/grpc use * fix condition for inprocess discrimination * Added unit test * mock enter and exit for with statement * Update tests * Update tests * Update tests * update test * Updated docstrings * Updated test for codecov * Update src/ansys/dpf/core/field_base.py Co-authored-by: Paul Profizi <100710998+PProfizi@users.noreply.github.com> --------- Co-authored-by: paul.profizi Co-authored-by: Paul Profizi <100710998+PProfizi@users.noreply.github.com> --- src/ansys/dpf/core/field.py | 30 ++++++++------ src/ansys/dpf/core/field_base.py | 8 +++- tests/test_field.py | 69 ++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/src/ansys/dpf/core/field.py b/src/ansys/dpf/core/field.py index 8ed2d23f3d..3ca076a9ad 100644 --- a/src/ansys/dpf/core/field.py +++ b/src/ansys/dpf/core/field.py @@ -232,15 +232,19 @@ def as_local_field(self): ... for i in range(1,num_entities+1): ... f.append([[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]],i) ... f.get_entity_data(i-1),[[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]] - (array([[0.1, 0.2, 0.3], - [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) - (array([[0.2, 0.4, 0.6], - [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]]) - (array([[0.3, 0.6, 0.9], - [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]]) + (DPFArray([[0.1, 0.2, 0.3], + [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) + (DPFArray([[0.2, 0.4, 0.6], + [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]]) + (DPFArray([[0.3, 0.6, 0.9], + [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]]) """ # noqa: E501 - return _LocalField(self) + # Do not copy data if using InProcess server + if self._server.client is not None: + return _LocalField(self) + else: + return self @property def location(self): @@ -860,12 +864,12 @@ class _LocalField(_LocalFieldBase, Field): ... for i in range(1,num_entities+1): ... f.append(np.array([[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]]),i) ... f.get_entity_data(i-1),[[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]] - (array([[0.1, 0.2, 0.3], - [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) - (array([[0.2, 0.4, 0.6], - [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]]) - (array([[0.3, 0.6, 0.9], - [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]]) + (DPFArray([[0.1, 0.2, 0.3], + [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) + (DPFArray([[0.2, 0.4, 0.6], + [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]]) + (DPFArray([[0.3, 0.6, 0.9], + [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]]) """ # noqa: E501 diff --git a/src/ansys/dpf/core/field_base.py b/src/ansys/dpf/core/field_base.py index 711ad09c15..05d9612314 100644 --- a/src/ansys/dpf/core/field_base.py +++ b/src/ansys/dpf/core/field_base.py @@ -494,6 +494,12 @@ def _get_data(self, np_array=True): def _set_data(self, data): pass + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, tb): + pass + class _LocalFieldBase(_FieldBase): """Caches the internal data of the field so that it can be modified locally. @@ -612,7 +618,7 @@ def get_entity_data_by_id(self, id): >>> stress_op = model.results.stress() >>> fields_container = stress_op.outputs.fields_container() >>> with fields_container[0].as_local_field() as f: - ... for id in f.scoping_ids: + ... for id in f.scoping.ids: ... if id < 2: ... print(f.get_entity_data_by_id(id)) [[-5.83890625e+03 -1.04498969e+05 -5.83890625e+03 2.10637354e+03 diff --git a/tests/test_field.py b/tests/test_field.py index a212f479a8..a6d426a3b6 100644 --- a/tests/test_field.py +++ b/tests/test_field.py @@ -543,14 +543,14 @@ def test_set_support_mesh(simple_bar): assert mesh_to_check_2.elements.n_elements == 0 -def test_local_field_append(): +def test_local_field_append(server_type_remote_process): num_entities = 400 - field_to_local = dpf.core.fields_factory.create_3d_vector_field(num_entities) + field_to_local = dpf.core.fields_factory.create_3d_vector_field(num_entities, server=server_type_remote_process) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): f.append([0.1 * i, 0.2 * i, 0.3 * i], i) - assert f._is_set == True - field = dpf.core.fields_factory.create_3d_vector_field(num_entities) + assert f._is_set is True + field = dpf.core.fields_factory.create_3d_vector_field(num_entities, server=server_type_remote_process) for i in range(1, num_entities + 1): field.append([0.1 * i, 0.2 * i, 0.3 * i], i) @@ -559,15 +559,15 @@ def test_local_field_append(): assert len(field_to_local._data_pointer) == 0 -def test_local_elemental_nodal_field_append(): +def test_local_elemental_nodal_field_append(server_type_remote_process): num_entities = 100 field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): f.append([[0.1 * i, 0.2 * i, 0.3 * i], [0.1 * i, 0.2 * i, 0.3 * i]], i) - field = dpf.core.fields_factory.create_3d_vector_field(num_entities) + field = dpf.core.fields_factory.create_3d_vector_field(num_entities, server=server_type_remote_process) for i in range(1, num_entities + 1): field.append([[0.1 * i, 0.2 * i, 0.3 * i], [0.1 * i, 0.2 * i, 0.3 * i]], i) @@ -577,7 +577,7 @@ def test_local_elemental_nodal_field_append(): # flat data field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): @@ -588,14 +588,14 @@ def test_local_elemental_nodal_field_append(): assert len(field_to_local._data_pointer) == num_entities -def test_local_array_field_append(): +def test_local_array_field_append(server_type_remote_process): num_entities = 400 - field_to_local = dpf.core.fields_factory.create_3d_vector_field(num_entities) + field_to_local = dpf.core.fields_factory.create_3d_vector_field(num_entities, server=server_type_remote_process) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): f.append(np.array([0.1 * i, 0.2 * i, 0.3 * i]), i) assert f._is_set is True - field = dpf.core.fields_factory.create_3d_vector_field(num_entities) + field = dpf.core.fields_factory.create_3d_vector_field(num_entities, server=server_type_remote_process) for i in range(1, num_entities + 1): field.append(np.array([0.1 * i, 0.2 * i, 0.3 * i]), i) @@ -604,15 +604,15 @@ def test_local_array_field_append(): assert len(field_to_local._data_pointer) == 0 -def test_local_elemental_nodal_array_field_append(): +def test_local_elemental_nodal_array_field_append(server_type_remote_process): num_entities = 100 field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): f.append(np.array([[0.1 * i, 0.2 * i, 0.3 * i], [0.1 * i, 0.2 * i, 0.3 * i]]), i) - field = dpf.core.fields_factory.create_3d_vector_field(num_entities) + field = dpf.core.fields_factory.create_3d_vector_field(num_entities, server=server_type_remote_process) for i in range(1, num_entities + 1): field.append(np.array([[0.1 * i, 0.2 * i, 0.3 * i], [0.1 * i, 0.2 * i, 0.3 * i]]), i) @@ -622,7 +622,7 @@ def test_local_elemental_nodal_array_field_append(): # flat data field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): @@ -633,10 +633,10 @@ def test_local_elemental_nodal_array_field_append(): assert len(field_to_local._data_pointer) == num_entities -def test_local_get_entity_data(): +def test_local_get_entity_data(server_type_remote_process): num_entities = 100 field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): @@ -653,10 +653,10 @@ def test_local_get_entity_data(): assert hasattr(f, "_is_set") is False -def test_local_elemental_nodal_get_entity_data(): +def test_local_elemental_nodal_get_entity_data(server_type_remote_process): num_entities = 100 field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: for i in range(1, num_entities + 1): @@ -685,22 +685,22 @@ def test_local_elemental_nodal_get_entity_data(): assert hasattr(f, "_is_set") is False -def test_auto_delete_field_local(): +def test_auto_delete_field_local(server_type_remote_process): num_entities = 1 field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) field_to_local.append([3.0, 4.0, 5.0], 1) - fc = dpf.core.fields_container_factory.over_time_freq_fields_container([field_to_local]) + fc = dpf.core.fields_container_factory.over_time_freq_fields_container([field_to_local], server=server_type_remote_process) field_to_local = None with fc[0].as_local_field() as f: assert np.allclose(f.get_entity_data(0), [3.0, 4.0, 5.0]) -def test_auto_delete_field_local2(): +def test_auto_delete_field_local2(server_type_remote_process): num_entities = 1 field_to_local = dpf.core.fields_factory.create_3d_vector_field( - num_entities, location=dpf.core.locations.elemental_nodal + num_entities, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) f = field_to_local.as_local_field() f.append([3.0, 4.0, 5.0], 1) @@ -709,9 +709,9 @@ def test_auto_delete_field_local2(): assert np.allclose(f.get_entity_data(0), [3.0, 4.0, 5.0]) -def test_get_set_data_local_field(): +def test_get_set_data_local_field(server_type_remote_process): field_to_local = dpf.core.fields_factory.create_3d_vector_field( - 2, location=dpf.core.locations.elemental_nodal + 2, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: f.data = [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]] @@ -730,9 +730,9 @@ def test_get_set_data_local_field(): assert np.allclose(field_to_local.data, [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) -def test_get_set_data_elemental_nodal_local_field(): +def test_get_set_data_elemental_nodal_local_field(server_type_remote_process): field_to_local = dpf.core.fields_factory.create_3d_vector_field( - 2, location=dpf.core.locations.elemental_nodal + 2, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: f.data = [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3], [0.1, 0.2, 0.3], [0.1, 0.2, 0.4]] @@ -798,13 +798,13 @@ def test_get_set_data_elemental_nodal_local_field(): assert np.allclose(field_to_local.get_entity_data(1), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.4]]) -def test_get_set_scoping_local_field(): +def test_get_set_scoping_local_field(server_type_remote_process): field_to_local = dpf.core.fields_factory.create_3d_vector_field( - 2, location=dpf.core.locations.elemental_nodal + 2, location=dpf.core.locations.elemental_nodal, server=server_type_remote_process ) with field_to_local.as_local_field() as f: f.data = [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]] - f.scoping = dpf.core.Scoping(ids=[3, 4]) + f.scoping = dpf.core.Scoping(ids=[3, 4], server=server_type_remote_process) assert np.allclose(f.data, [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) assert np.allclose(f.scoping_ids, [3, 4]) assert np.allclose(f.scoping.ids, [3, 4]) @@ -1274,3 +1274,10 @@ def test_dot_operator_field(): out = add.outputs.field() assert np.allclose(out.scoping.ids, [1, 2]) assert np.allclose(out.data, -field.data) + +def test_field_no_inprocess_localfield(server_in_process, allkindofcomplexity): + model = dpf.core.Model(allkindofcomplexity, server=server_in_process) + field = model.results.stress().outputs.fields_container()[0] + + with field.as_local_field() as local_field: + assert field == local_field \ No newline at end of file