Skip to content

Commit

Permalink
Don't create LocalField when using InProcess (#1243)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: paul.profizi <[email protected]>
Co-authored-by: Paul Profizi <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2023
1 parent da3a0ea commit d97f72f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 45 deletions.
30 changes: 17 additions & 13 deletions src/ansys/dpf/core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/ansys/dpf/core/field_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
69 changes: 38 additions & 31 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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]]
Expand All @@ -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]]
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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

0 comments on commit d97f72f

Please sign in to comment.