From 840bac96bf34b501bf1f4393dc9cab08d0c1f6ed Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Mon, 2 Sep 2024 18:47:41 +0200 Subject: [PATCH 01/13] new mathematical operations example --- .../matrix-operations.py | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 examples/01-mathematical-operations/matrix-operations.py diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py new file mode 100644 index 0000000000..6ed618bfc5 --- /dev/null +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -0,0 +1,117 @@ +""" +.. _ref_matrix-operations: + +Matrix Operations +~~~~~~~~~~~~~~~~~ + +This example shows how to do some matrix operations, including basic mathematical operation (power, add and multiply by +a constant, add field containers and invert ) + +Here we will operate in ...? + +""" + +############################################################################### +# Import the ``ansys.dpf.core`` module, included examples file, and the ``DpfPlotter`` +# module. +from ansys.dpf import core as dpf +from ansys.dpf.core import examples +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. +# The :class:`Model ` class helps to organize access +# methods for the result by keeping track of the operators and data sources +# used by the result file. +# +# Printing the model displays this metadata: +# +# - Analysis type +# - Available results +# - Size of the mesh +# - Number of results +# +my_model = dpf.Model(examples.find_complex_rst()) +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 +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 +my_avg_stress = dpf.operators.averaging.to_elemental_fc(fields_container=my_stress,mesh=my_mesh).eval() +print(my_avg_stress[0]) +# my_mesh.plot(my_avg_stress.get_field({"time": 1, "complex": 0})) +######################################################### +# Separating tensor by component +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# If operations need to be done separetly in each tensor component, the :func:'select_component() ' +# Here, the stress tensor has 6 components by 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) + print(globals()[f'stress_{i+1}'][0]) +################################################################################ +# Mathematical operation on each field +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# Here we will do some basic mathematical operations on +# Power +# 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 +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 +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 +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 +# Compute the invert of each element of each field (1./X) +stress_6 = maths.invert_fc(fields_container=stress_6).eval() + +################################################################################ +# Reassembling the stress tensor +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# There are different methods to re-assemble the components + +# 1) With the class :class:'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() +print(assemble_1[0]) + +# 2) With the function :func:'create_tensor_field() ' + +assemble_2 = dpf.FieldsContainer() +assemble_2.labels = ['time','complex'] +for m in my_nodes_scoping.ids: + globals()[f'stress_tensor_{m}'] = dpf.fields_factory.create_tensor_field(num_entities=1, + location=dpf.locations.elemental) + globals()[f'stress_tensor_complex_{m}'] = dpf.fields_factory.create_tensor_field(num_entities=1, + location=dpf.locations.elemental) + for k in range(0,6): + globals()[f'stress_tensor_{m}'].append(data=globals()[f'stress_{k+1}'][0].get_entity_data_by_id(id=m), + scopingid=m) + globals()[f'stress_tensor_complex_{m}'].append(data=globals()[f'stress_{k + 1}'][1].get_entity_data_by_id(id=m), + scopingid=m) + assemble_2.add_field(label_space={'time': 1, 'complex': 0}, field=globals()[f'stress_tensor_{m}']) + assemble_2.add_field(label_space={'time': 1, 'complex': 1}, field=globals()[f'stress_tensor_complex_{m}']) +print(assemble_2[0]) + + + + + From 421698473a9b9a7e5561a3bdd29f032f46f9c6c5 Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Tue, 3 Sep 2024 09:42:43 +0200 Subject: [PATCH 02/13] new mathematical operations example --- .../matrix-operations.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 6ed618bfc5..06c1e55499 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -98,17 +98,17 @@ assemble_2 = dpf.FieldsContainer() assemble_2.labels = ['time','complex'] for m in my_nodes_scoping.ids: - globals()[f'stress_tensor_{m}'] = dpf.fields_factory.create_tensor_field(num_entities=1, - location=dpf.locations.elemental) - globals()[f'stress_tensor_complex_{m}'] = dpf.fields_factory.create_tensor_field(num_entities=1, - location=dpf.locations.elemental) + # globals()[f'stress_tensor_{m}'] = dpf.fields_factory.field_from_array([]) + globals()[f'stress_list_{m}']=[] for k in range(0,6): - globals()[f'stress_tensor_{m}'].append(data=globals()[f'stress_{k+1}'][0].get_entity_data_by_id(id=m), - scopingid=m) - globals()[f'stress_tensor_complex_{m}'].append(data=globals()[f'stress_{k + 1}'][1].get_entity_data_by_id(id=m), - scopingid=m) + # globals()[f'stress_tensor_{m}'].append(data=globals()[f'stress_{k+1}'][0].get_entity_data_by_id(id=m), + # scopingid=m) + # globals()[f'stress_tensor_complex_{m}'].append(data=globals()[f'stress_{k + 1}'][1].get_entity_data_by_id(id=m), + # scopingid=m) + globals()[f'stress_list_{m}'][k]=globals()[f'stress_{k+1}'][0].get_entity_data_by_id(id=m) + globals()[f'stress_tensor_{m}'] = dpf.fields_factory.create_tensor_field(num_entities=1, lo assemble_2.add_field(label_space={'time': 1, 'complex': 0}, field=globals()[f'stress_tensor_{m}']) - assemble_2.add_field(label_space={'time': 1, 'complex': 1}, field=globals()[f'stress_tensor_complex_{m}']) + # assemble_2.add_field(label_space={'time': 1, 'complex': 1}, field=globals()[f'stress_tensor_complex_{m}']) print(assemble_2[0]) From d014fa4aa6b13ff68df75762838081188d9cd34e Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Tue, 3 Sep 2024 11:21:39 +0200 Subject: [PATCH 03/13] new mathematical operations example --- .../matrix-operations.py | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 06c1e55499..ce2112dcf5 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -17,6 +17,7 @@ from ansys.dpf import core as dpf from ansys.dpf.core import examples 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. @@ -42,19 +43,20 @@ # Here we need to average the result from 'elemental_nodal' to an 'elemental' location to # facilitate the visualisation of the plot -my_avg_stress = dpf.operators.averaging.to_elemental_fc(fields_container=my_stress,mesh=my_mesh).eval() -print(my_avg_stress[0]) -# my_mesh.plot(my_avg_stress.get_field({"time": 1, "complex": 0})) +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]) + ######################################################### # Separating tensor by component # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# If operations need to be done separetly in each tensor component, the :func:'select_component() ' +# If operations need to be done separetly in each tensor component, the +# :func:'select_component()'. # Here, the stress tensor has 6 components by 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) - print(globals()[f'stress_{i+1}'][0]) + globals()[f'stress_{i + 1}'] = my_avg_stress.select_component(i) + ################################################################################ # Mathematical operation on each field # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -62,7 +64,7 @@ # Here we will do some basic mathematical operations on # Power # Compute the power operation for each elementary data -stress_1 = maths.pow_fc(fields_container=stress_1,factor=2.0).eval() +stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval() # Add constant # Each component of each field is added by 2 @@ -74,8 +76,8 @@ # Add fields containers # Each component 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() +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 # Compute the invert of each element of each field (1./X) @@ -89,29 +91,29 @@ # 1) With the class :class:'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() -print(assemble_1[0]) + xy=stress_4, yz=stress_5, xz=stress_6, + symmetrical=True).eval() +# print(assemble_1[0]) # 2) With the function :func:'create_tensor_field() ' - assemble_2 = dpf.FieldsContainer() -assemble_2.labels = ['time','complex'] -for m in my_nodes_scoping.ids: - # globals()[f'stress_tensor_{m}'] = dpf.fields_factory.field_from_array([]) - globals()[f'stress_list_{m}']=[] - for k in range(0,6): - # globals()[f'stress_tensor_{m}'].append(data=globals()[f'stress_{k+1}'][0].get_entity_data_by_id(id=m), - # scopingid=m) - # globals()[f'stress_tensor_complex_{m}'].append(data=globals()[f'stress_{k + 1}'][1].get_entity_data_by_id(id=m), - # scopingid=m) - globals()[f'stress_list_{m}'][k]=globals()[f'stress_{k+1}'][0].get_entity_data_by_id(id=m) - globals()[f'stress_tensor_{m}'] = dpf.fields_factory.create_tensor_field(num_entities=1, lo - assemble_2.add_field(label_space={'time': 1, 'complex': 0}, field=globals()[f'stress_tensor_{m}']) - # assemble_2.add_field(label_space={'time': 1, 'complex': 1}, field=globals()[f'stress_tensor_complex_{m}']) -print(assemble_2[0]) - - - - +assemble_2.labels = ["time", "complex"] +for i in range(0, 2): + loop_size = my_nodes_scoping.size + globals()[f'fields_list_{i}'] = [None] * loop_size + for m in range(loop_size): + globals()[f'stress_list_{m}{i}'] = [None] * my_avg_stress[i].component_count + for k in range(0, 6): + globals()[f'stress_list_{m}{i}'][k] = globals()[f'stress_{k + 1}'][i].get_entity_data_by_id( + id=my_nodes_scoping.ids[m]) + globals()[f'stress_tensor_{i}'] = dpf.fields_factory.create_tensor_field(num_entities=my_nodes_scoping.size, + location=dpf.locations.elemental) + globals()[f'stress_tensor_{i}'].append(data=globals()[f'stress_list_{m}{i}'], + scopingid=my_nodes_scoping.ids[m]) + globals()[f'fields_list_{i}'][m] = globals()[f'stress_tensor_{i}'] + assemble_2.add_field(label_space={'time': 1, 'complex': i}, field=globals()[f'stress_tensor_{i}']) + +assemble_3 = dpf.fields_container_factory.over_time_freq_complex_fields_container(real_fields=globals()[f'fields_list_0'], + imaginary_fields=globals()[f'fields_list_1']) +# print(assemble_2) From 06dee83b8c2a9c5808f744d0493f1d96f4b53419 Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Tue, 3 Sep 2024 11:24:40 +0200 Subject: [PATCH 04/13] new mathematical operations example --- .../matrix-operations.py | 28 ++----------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index ce2112dcf5..9cd58d323a 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -5,9 +5,7 @@ ~~~~~~~~~~~~~~~~~ This example shows how to do some matrix operations, including basic mathematical operation (power, add and multiply by -a constant, add field containers and invert ) - -Here we will operate in ...? +a constant, add field containers and invert ) and separating and assembling fields and fields containers """ @@ -93,27 +91,5 @@ 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() -# print(assemble_1[0]) - -# 2) With the function :func:'create_tensor_field() ' -assemble_2 = dpf.FieldsContainer() -assemble_2.labels = ["time", "complex"] -for i in range(0, 2): - loop_size = my_nodes_scoping.size - globals()[f'fields_list_{i}'] = [None] * loop_size - for m in range(loop_size): - globals()[f'stress_list_{m}{i}'] = [None] * my_avg_stress[i].component_count - for k in range(0, 6): - globals()[f'stress_list_{m}{i}'][k] = globals()[f'stress_{k + 1}'][i].get_entity_data_by_id( - id=my_nodes_scoping.ids[m]) - globals()[f'stress_tensor_{i}'] = dpf.fields_factory.create_tensor_field(num_entities=my_nodes_scoping.size, - location=dpf.locations.elemental) - globals()[f'stress_tensor_{i}'].append(data=globals()[f'stress_list_{m}{i}'], - scopingid=my_nodes_scoping.ids[m]) - globals()[f'fields_list_{i}'][m] = globals()[f'stress_tensor_{i}'] - assemble_2.add_field(label_space={'time': 1, 'complex': i}, field=globals()[f'stress_tensor_{i}']) - -assemble_3 = dpf.fields_container_factory.over_time_freq_complex_fields_container(real_fields=globals()[f'fields_list_0'], - imaginary_fields=globals()[f'fields_list_1']) -# print(assemble_2) +print(assemble_1, assemble_1[0]) From ca976ce9e0046b41a0c4a1f249926a93c0cc65aa Mon Sep 17 00:00:00 2001 From: Luisa Felix Salles Date: Tue, 17 Sep 2024 17:04:22 +0200 Subject: [PATCH 05/13] Update examples/01-mathematical-operations/matrix-operations.py Co-authored-by: Paul Profizi <100710998+PProfizi@users.noreply.github.com> --- examples/01-mathematical-operations/matrix-operations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 9cd58d323a..658c31081f 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -4,8 +4,8 @@ Matrix Operations ~~~~~~~~~~~~~~~~~ -This example shows how to do some matrix operations, including basic mathematical operation (power, add and multiply by -a constant, add field containers and invert ) and separating and assembling fields and fields containers +This example shows how to do some matrix operations, including basic mathematical operations (power, add and multiply by +a constant, add field containers and invert) and separating and assembling fields and fields containers. """ From cbf8bf63cbf1b4529d7aac81866ea18fdf607243 Mon Sep 17 00:00:00 2001 From: Luisa Felix Salles Date: Tue, 17 Sep 2024 17:13:16 +0200 Subject: [PATCH 06/13] Update examples/01-mathematical-operations/matrix-operations.py --- examples/01-mathematical-operations/matrix-operations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 658c31081f..3aa5ba9852 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -32,7 +32,7 @@ # my_model = dpf.Model(examples.find_complex_rst()) my_mesh = my_model.metadata.meshed_region -# print(my_model) +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 From 1d9937c24b21251b6fd0f3bfd94603302eb97385 Mon Sep 17 00:00:00 2001 From: Luisa Felix Salles Date: Thu, 26 Sep 2024 09:19:44 +0200 Subject: [PATCH 07/13] Apply suggestions from code review Co-authored-by: Paul Profizi <100710998+PProfizi@users.noreply.github.com> --- .../matrix-operations.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 3aa5ba9852..9ed1d32a76 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -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 ` class helps to organize access # methods for the result by keeping track of the operators and data sources # used by the result file. @@ -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()'. -# 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) @@ -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() @@ -87,7 +85,7 @@ # There are different methods to re-assemble the components -# 1) With the class :class:'assemble_scalars_to_matrices_fc ' +# 1) With the operator :class:'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() From e616bf0ea6d5ada8f01f9415965bb174d875cad2 Mon Sep 17 00:00:00 2001 From: Luisa Felix Salles Date: Thu, 26 Sep 2024 09:20:37 +0200 Subject: [PATCH 08/13] Apply suggestions from code review --- examples/01-mathematical-operations/matrix-operations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 9ed1d32a76..0bfbae7efd 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -17,7 +17,7 @@ import ansys.dpf.core.operators.math as maths ############################################################################### -Open an example and print the ``Model`` object +# Open an example and print the ``Model`` object # The :class:`Model ` class helps to organize access # methods for the result by keeping track of the operators and data sources # used by the result file. From 96f9357a0997518e24fb33b7e10eb11eb72c64f9 Mon Sep 17 00:00:00 2001 From: Luisa Felix Salles Date: Thu, 26 Sep 2024 09:23:17 +0200 Subject: [PATCH 09/13] Apply suggestions from code review Co-authored-by: Paul Profizi <100710998+PProfizi@users.noreply.github.com> --- examples/01-mathematical-operations/matrix-operations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 0bfbae7efd..75ed964e84 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -10,7 +10,7 @@ """ ############################################################################### -# Import the ``ansys.dpf.core`` module, included examples file, and the ``DpfPlotter`` +# Import the ``ansys.dpf.core`` module, included examples file, and the ``operators.math`` # module. from ansys.dpf import core as dpf from ansys.dpf.core import examples @@ -59,7 +59,7 @@ # Here we will do some basic mathematical operations on # Power -# Compute the power operation for each elementary data +# Raise each value of the field to power 2 stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval() # Add a constant From 4679b9bffa57813f7d826810fe0bded8498f2fb7 Mon Sep 17 00:00:00 2001 From: Luisa Felix Salles Date: Thu, 26 Sep 2024 09:29:55 +0200 Subject: [PATCH 10/13] Update examples/01-mathematical-operations/matrix-operations.py --- examples/01-mathematical-operations/matrix-operations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 75ed964e84..e17103d045 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -57,7 +57,7 @@ # Mathematical operation on each field # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Here we will do some basic mathematical operations on +# Here we will do some basic mathematical operations on each stress field # Power # Raise each value of the field to power 2 stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval() From 993ee2c843e054db7673d9a2cc3e16108dd24101 Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Mon, 21 Oct 2024 11:48:12 +0200 Subject: [PATCH 11/13] update --- .../matrix-operations.py | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index e17103d045..25c1235446 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -1,3 +1,25 @@ +# Copyright (C) 2020 - 2024 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + """ .. _ref_matrix-operations: @@ -39,7 +61,9 @@ my_stress = my_model.results.stress(mesh_scoping=my_nodes_scoping).eval() # 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() +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]) ######################################################### @@ -50,14 +74,16 @@ # :func:'select_component()'. # 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) +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) ################################################################################ # Mathematical operation on each field # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Here we will do some basic mathematical operations on each stress field +# Here we will do some basic mathematical operations on each stress field # Power # Raise each value of the field to power 2 stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval() @@ -86,8 +112,7 @@ # There are different methods to re-assemble the components # 1) With the operator :class:'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() +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() print(assemble_1, assemble_1[0]) - From c9f3aa5f4ec23a9e9b5656a9b0999001a93f2feb Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Mon, 21 Oct 2024 11:53:33 +0200 Subject: [PATCH 12/13] update --- .../01-mathematical-operations/matrix-operations.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index 25c1235446..cee08947fd 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -84,6 +84,7 @@ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Here we will do some basic mathematical operations on each stress field + # Power # Raise each value of the field to power 2 stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval() @@ -109,10 +110,11 @@ # Reassembling the stress tensor # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# There are different methods to re-assemble the components +# There are different methods to re-assemble the components, here we use the +# operator :class:'assemble_scalars_to_matrices_fc ' -# 1) With the operator :class:'assemble_scalars_to_matrices_fc ' -assemble_1 = dpf.operators.utility.assemble_scalars_to_matrices_fc( +re_assemble = 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() -print(assemble_1, assemble_1[0]) + +print(re_assemble, re_assemble[0]) From 354b8ca87c80d80b29b7f95820056c1b30c8fb35 Mon Sep 17 00:00:00 2001 From: luisaFelixSalles Date: Mon, 21 Oct 2024 15:48:58 +0200 Subject: [PATCH 13/13] not using global variable --- .../matrix-operations.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/01-mathematical-operations/matrix-operations.py b/examples/01-mathematical-operations/matrix-operations.py index cee08947fd..7ca507d4c8 100644 --- a/examples/01-mathematical-operations/matrix-operations.py +++ b/examples/01-mathematical-operations/matrix-operations.py @@ -64,8 +64,8 @@ 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) +print(my_avg_stress[0]) ######################################################### # Separating tensor by component # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -74,10 +74,13 @@ # :func:'select_component()'. # 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) +# Separating the results in different fields containers for each stress tensor component +stress_1 = my_avg_stress.select_component(0) +stress_2 = my_avg_stress.select_component(1) +stress_3 = my_avg_stress.select_component(2) +stress_4 = my_avg_stress.select_component(3) +stress_5 = my_avg_stress.select_component(4) +stress_6 = my_avg_stress.select_component(5) ################################################################################ # Mathematical operation on each field @@ -117,4 +120,5 @@ xx=stress_1, yy=stress_2, zz=stress_3, xy=stress_4, yz=stress_5, xz=stress_6, symmetrical=True ).eval() -print(re_assemble, re_assemble[0]) +print(re_assemble) +print(re_assemble[0])