Skip to content

Commit

Permalink
Add test for extract ply wise data
Browse files Browse the repository at this point in the history
  • Loading branch information
janvonrickenbach committed Dec 8, 2023
1 parent 761a0f7 commit 9bd530f
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions tests/ply_wise_data_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from collections.abc import Sequence

from ansys.dpf.gate.common import locations
import numpy as np

from ansys.dpf.composites.composite_model import CompositeModel
from ansys.dpf.composites.constants import Sym3x3TensorComponent
from ansys.dpf.composites.ply_wise_data import ReductionStrategy, get_ply_wise_data
from tests.helper import get_basic_shell_files


def get_reduced_value(all_spot_values: Sequence[float], reduction_strategy: ReductionStrategy):
if reduction_strategy == ReductionStrategy.AVG:
return sum(all_spot_values) / len(all_spot_values)
if reduction_strategy == ReductionStrategy.MIN:
return min(all_spot_values)
if reduction_strategy == ReductionStrategy.MAX:
return max(all_spot_values)
if reduction_strategy == ReductionStrategy.BOT:
return all_spot_values[0]
if reduction_strategy == ReductionStrategy.MID:
return all_spot_values[2]
if reduction_strategy == ReductionStrategy.TOP:
return all_spot_values[1]
raise ValueError(f"Unknown reduction strategy: {reduction_strategy}")


ALL_REDUCTION_STRATEGIES = [
ReductionStrategy.AVG,
ReductionStrategy.MIN,
ReductionStrategy.MAX,
ReductionStrategy.BOT,
ReductionStrategy.MID,
ReductionStrategy.TOP,
]

ALL_TENSOR_COMPONENTS = [
Sym3x3TensorComponent.TENSOR11,
Sym3x3TensorComponent.TENSOR22,
Sym3x3TensorComponent.TENSOR22,
Sym3x3TensorComponent.TENSOR21,
Sym3x3TensorComponent.TENSOR31,
Sym3x3TensorComponent.TENSOR32,
]


def test_get_ply_wise_data(dpf_server):
files = get_basic_shell_files()

composite_model = CompositeModel(files, server=dpf_server)

stress_result_op = composite_model.core_model.results.stress()
stress_result_op.inputs.bool_rotate_to_global(False)
stress_field = stress_result_op.outputs.fields_container()[0]

def get_all_spot_values_first_element_first_node(stress_field, component=0):
all_spot_values = []
entity_data = stress_field.get_entity_data_by_id(1)[:, component]
number_of_nodes = 4
for spot_index in range(3):
all_spot_values.append(entity_data[spot_index * number_of_nodes])
return all_spot_values

first_ply = "P1L1__woven_45"

element_id = 1
first_node_index = 0

for component in ALL_TENSOR_COMPONENTS:
for reduction_strategy in ALL_REDUCTION_STRATEGIES:
all_spot_values = get_all_spot_values_first_element_first_node(
stress_field, component=component.value
)

elemental_nodal_data = get_ply_wise_data(
stress_field,
first_ply,
composite_model.get_mesh(),
component=component,
reduction_strategy=reduction_strategy,
)

assert len(elemental_nodal_data.scoping.ids) == 4
assert len(elemental_nodal_data.get_entity_data_by_id(element_id)) == 4

assert np.allclose(
elemental_nodal_data.get_entity_data_by_id(element_id)[first_node_index],
get_reduced_value(all_spot_values, reduction_strategy),
)

elemental_data = get_ply_wise_data(
stress_field,
first_ply,
composite_model.get_mesh(),
component=component,
reduction_strategy=reduction_strategy,
requested_location=locations.elemental,
)
assert len(elemental_data.scoping.ids) == 4
assert len(elemental_data.get_entity_data_by_id(element_id)) == 1

assert np.allclose(
elemental_data.get_entity_data_by_id(element_id)[first_node_index],
sum(elemental_nodal_data.get_entity_data_by_id(element_id)) / 4,
)

nodal_data = get_ply_wise_data(
stress_field,
first_ply,
composite_model.get_mesh(),
component=component,
reduction_strategy=reduction_strategy,
requested_location=locations.nodal,
)
assert len(nodal_data.scoping.ids) == 9
assert len(nodal_data.get_entity_data_by_id(element_id)) == 1

# Select node that only belongs to element 1
# no averaging needed
node_index_with_no_neighbours = 1
first_node_index_of_element_1 = (
composite_model.get_mesh().elements.connectivities_field.get_entity_data_by_id(
element_id
)[node_index_with_no_neighbours]
)
node_id_to_index = composite_model.get_mesh().nodes.mapping_id_to_index
node_id = list(node_id_to_index.keys())[
list(node_id_to_index.values()).index(first_node_index_of_element_1)
]

assert np.allclose(
nodal_data.get_entity_data_by_id(node_id)[0],
elemental_nodal_data.get_entity_data_by_id(element_id)[
node_index_with_no_neighbours
],
)

0 comments on commit 9bd530f

Please sign in to comment.