Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Profizi <[email protected]>
  • Loading branch information
luisaFelixSalles and PProfizi committed Sep 26, 2024
1 parent d62c54c commit 1d9937c
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions examples/01-mathematical-operations/matrix-operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import ansys.dpf.core.operators.math as maths

###############################################################################
# Open an example and print the ``Model`` object. Here a result file from a crankshaft
# under load simulation is used.
Open an example and print the ``Model`` object
# The :class:`Model <ansys.dpf.core.model.Model>` class helps to organize access
# methods for the result by keeping track of the operators and data sources
# used by the result file.
Expand All @@ -34,23 +33,22 @@
my_mesh = my_model.metadata.meshed_region
print(my_model)
###############################################################################
# Get the stress tensor and define it's scoping. Here, only three nodes will be take into account to facilitate the
# results visualisation
# Get the stress tensor and define its scoping. Only three nodes will be taken into account to facilitate the
# visualization.
my_nodes_scoping = dpf.Scoping(ids=[38, 37, 36], location=dpf.locations.elemental)
my_stress = my_model.results.stress(mesh_scoping=my_nodes_scoping).eval()

# Here we need to average the result from 'elemental_nodal' to an 'elemental' location to
# facilitate the visualisation of the plot
# We need to average the result from 'elemental_nodal' to an 'elemental' location to plot it.
my_avg_stress = dpf.operators.averaging.to_elemental_fc(fields_container=my_stress, mesh=my_mesh).eval()
# print(my_avg_stress, my_avg_stress[0])
print(my_avg_stress, my_avg_stress[0])

#########################################################
# Separating tensor by component
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# If operations need to be done separetly in each tensor component, the
# If operations need to be done separately in each tensor component, use
# :func:'select_component()<ansys.dpf.core.fields_container.FieldsContainer.select_component>'.
# Here, the stress tensor has 6 components by elementary data (symmetrical tensor XX,YY,ZZ,XY,YZ,XZ).
# Here, the stress tensor has 6 components per elementary data (symmetrical tensor XX,YY,ZZ,XY,YZ,XZ).

for i in range(0, 6): # Separating the results in different fields containers for each stress tensor component
globals()[f'stress_{i + 1}'] = my_avg_stress.select_component(i)
Expand All @@ -64,20 +62,20 @@
# Compute the power operation for each elementary data
stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval()

# Add constant
# Each component of each field is added by 2
# Add a constant
# Add 2 to each value in the field
stress_2 = maths.add_constant_fc(fields_container=stress_2, ponderation=2.0).eval()

# Multiply by a constant
# Each component of each field is multiplied by 3
# Multiply each value in the field by 3
stress_3 = maths.scale_fc(fields_container=stress_3, ponderation=3.0).eval()

# Add fields containers
# Each component of each field is added by the correspondent component of the others fields
# Each value of each field is added by the correspondent component of the others fields
stress_4 = maths.add_fc(fields_container1=stress_4, fields_container2=stress_5).eval()
stress_5 = maths.add_fc(fields_container1=stress_5, fields_container2=stress_6).eval()

#Invert
# Invert
# Compute the invert of each element of each field (1./X)
stress_6 = maths.invert_fc(fields_container=stress_6).eval()

Expand All @@ -87,7 +85,7 @@

# There are different methods to re-assemble the components

# 1) With the class :class:'assemble_scalars_to_matrices_fc <ansys.dpf.core.operators.utility.assemble_scalars_to_matrices_fc.assemble_scalars_to_matrices_fc>'
# 1) With the operator :class:'assemble_scalars_to_matrices_fc <ansys.dpf.core.operators.utility.assemble_scalars_to_matrices_fc.assemble_scalars_to_matrices_fc>'
assemble_1 = dpf.operators.utility.assemble_scalars_to_matrices_fc(xx=stress_1, yy=stress_2, zz=stress_3,
xy=stress_4, yz=stress_5, xz=stress_6,
symmetrical=True).eval()
Expand Down

0 comments on commit 1d9937c

Please sign in to comment.