Skip to content

Commit c2a986b

Browse files
committed
Fix LUT copying in recursive copy
1 parent 1406641 commit c2a986b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/ansys/acp/core/_recursive_copy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import networkx as nx
2727

28+
from ._tree_objects import LookUpTable1D, LookUpTable1DColumn, LookUpTable3D, LookUpTable3DColumn
2829
from ._tree_objects._traversal import (
2930
all_linked_objects,
3031
child_objects,
@@ -187,6 +188,11 @@ def recursive_copy(
187188
continue
188189
tree_object = visited_objects[node]
189190

191+
if isinstance(tree_object, (LookUpTable1DColumn, LookUpTable3DColumn)):
192+
# handled explicitly while copying the LookUpTable object
193+
if tree_object.name == "Location":
194+
continue
195+
190196
new_tree_object = tree_object.clone()
191197

192198
# If the linked objects are also copied, replace them with the new objects.
@@ -213,8 +219,17 @@ def recursive_copy(
213219
raise KeyError(
214220
f"Parent object not found in 'parent_mapping' for object '{tree_object!r}'."
215221
) from exc
222+
216223
new_tree_object.store(parent=new_parent)
217224

225+
# NOTE: if there are more type-specific fixes needed, we may want
226+
# to implement a more generic way to handle these.
227+
# Explicit fix for LookUpTable, since the Location column needs to
228+
# be set correctly s.t. other columns may be stored.
229+
if isinstance(new_tree_object, (LookUpTable1D, LookUpTable3D)):
230+
assert isinstance(tree_object, (LookUpTable1D, LookUpTable3D))
231+
new_tree_object.columns["Location"].data = tree_object.columns["Location"].data
232+
218233
if include_linked_objects:
219234
for attr_name, edge_property_list in edge_property_lists(tree_object):
220235
new_edge_property_list = [edge.clone() for edge in edge_property_list]

tests/unittests/test_recursive_copy.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
import numpy as np
2324
import pytest
2425

2526
from ansys.acp.core import FabricWithAngle, recursive_copy
@@ -197,3 +198,32 @@ def test_missing_parent_object_raises(minimal_complete_model):
197198
msg = str(exc_info.value)
198199
assert "Parent object" in msg
199200
assert "parent_mapping" in msg
201+
202+
203+
def test_copy_lookup_table_with_columns(minimal_complete_model):
204+
"""Test copying lookup tables with columns and their data.
205+
206+
This case is special because LUT implement a check for the shape
207+
of the data in their columns, which is determined by the "Location"
208+
column.
209+
"""
210+
# GIVEN: a model which has objects with sub-attributes
211+
# (here: a lookup table with columns)
212+
model = minimal_complete_model
213+
lut = model.create_lookup_table_1d()
214+
lut.columns["Location"].data = [1.0, 2.0, 3.0]
215+
lut.create_column(name="column1", data=[4.0, 5.0, 6.0])
216+
217+
# WHEN: recursively copying the lookup table
218+
new_objects = recursive_copy(source_objects=[lut], parent_mapping=[(model, model)])
219+
220+
# THEN: the expected new objects are created, but the sub-attributes are
221+
# not explicitly copied
222+
assert len(new_objects) == 2
223+
assert {obj.id for obj in new_objects} == { # type: ignore
224+
"LookUpTable1D.2",
225+
"column1",
226+
}
227+
new_lut = model.lookup_tables_1d["LookUpTable1D.2"]
228+
assert np.allclose(new_lut.columns["Location"].data, [1.0, 2.0, 3.0])
229+
assert np.allclose(new_lut.columns["column1"].data, [4.0, 5.0, 6.0])

0 commit comments

Comments
 (0)