-
Notifications
You must be signed in to change notification settings - Fork 25
Added Mathematical operations doc page #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5887944
Added Mathematical operations doc page
ansys-akarcher c61125a
Moved scoping related code to the end
ansys-akarcher bfc7284
minor page formatting, style check
ansys-akarcher 41f9c69
Apply suggestions from code review
ansys-akarcher 6a97618
comments-related suggestions from review
ansys-akarcher 4c147e7
Merge branch 'master' into examples/add_math_ops
ansys-akarcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# noqa: D400 | ||
""" | ||
.. _ref_math_operators_example: | ||
|
||
Mathematical Operations | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
DPF provides operators implementing mathematical operations, | ||
ranging from addition and multiplication to FFT and QR solving | ||
|
||
For a complete list, see :ref:`ref_dpf_operators_reference`, under the math section. | ||
|
||
""" | ||
|
||
# Import the necessary modules | ||
import ansys.dpf.core as dpf | ||
|
||
############################################################################### | ||
# Addition | ||
# ~~~~~~~~ | ||
|
||
# Initialize Fields | ||
num_entities = 2 | ||
field1 = dpf.Field(nentities=2) | ||
field2 = dpf.Field(nentities=2) | ||
|
||
# By default Fields contain 3d vectors | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# So with 3 entities we need 9 values | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field1.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] | ||
field2.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] | ||
|
||
field1.scoping.ids = range(num_entities) | ||
field2.scoping.ids = range(num_entities) | ||
############################################################################### | ||
# Once the fields are ready we can instantiate an operator | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
add_op = dpf.operators.math.add(field1, field2) | ||
# Alternatively: | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# add_op = dpf.Operator("add") | ||
# add_op.connect(0, field1) | ||
# add_op.connect(1, field2) | ||
|
||
############################################################################### | ||
# Finally we use eval() to compute and retrieve the result | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field3 = add_op.eval() | ||
|
||
# = [[2. 4. 6.] [8. 10. 12.]] | ||
print(field3.data) | ||
|
||
############################################################################### | ||
# Dot product | ||
# ~~~~~~~~~~~ | ||
dot_op = dpf.operators.math.generalized_inner_product(field1, field2) | ||
|
||
# (1. * 1.) + (2. * 2.) + (3. * 3.) = 14. | ||
# (4. * 4.) + (5. * 5.) + (6. * 6.) = 77. | ||
field3 = dot_op.eval() | ||
print(field3.data) | ||
|
||
|
||
############################################################################### | ||
# Power | ||
# ~~~~~ | ||
field = dpf.Field(nentities=1) | ||
field1.data = [1.0, 2.0, 3.0] | ||
field1.scoping.ids = [1] | ||
|
||
pow_op = dpf.operators.math.pow(field1, 3.0) | ||
|
||
# [1. 8. 27.] | ||
field3 = pow_op.eval() | ||
print(field3.data) | ||
|
||
|
||
############################################################################### | ||
# L2 norm | ||
# ~~~~~~~ | ||
field1.data = [16.0, -8.0, 2.0] | ||
norm_op = dpf.operators.math.norm(field1) | ||
|
||
# [ 18. ] | ||
field3 = norm_op.eval() | ||
print(field3.data) | ||
|
||
|
||
############################################################################### | ||
# Accumulate | ||
# ~~~~~~~~~~ | ||
# First we define fields, by default fields represent 3d vectors, | ||
# so one elementary data is a 3d vector | ||
# but the optional ponderation field is a field which takes one value per entity, | ||
# so we need to change its dimensionality (1d) | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
num_entities = 3 | ||
input_field = dpf.Field(nentities=num_entities) | ||
ponderation_field = dpf.Field(num_entities) | ||
ponderation_field.dimensionality = dpf.Dimensionality([1]) | ||
|
||
input_field.scoping.ids = range(num_entities) | ||
ponderation_field.scoping.ids = range(num_entities) | ||
|
||
############################################################################### | ||
# Fill fields with data | ||
# 9 values because there are 3 entities | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input_field.data = [-2.0, 2.0, 4.0, -5.0, 0.5, 1.0, 7.0, 3.0, -3.0] | ||
############################################################################### | ||
# 3 weights, one per entity | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ponderation_field.data = [0.5, 2.0, 0.5] | ||
|
||
############################################################################### | ||
# Retrieve the result | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
acc = dpf.operators.math.accumulate(fieldA=input_field, ponderation=ponderation_field) | ||
output_field = acc.outputs.field() | ||
|
||
# (-2.0 * 0.5) + (-5.0 * 2.0) + (7.0 * 0.5) = -7.5 | ||
# (2.0 * 0.5) + (0.5 * 2.0) + (3.0 * 0.5) = 3.5 | ||
# (4.0 * 0.5) + (1.0 * 2.0) + (-3.0 * 0.5) = 2.5 | ||
print(output_field.data) | ||
|
||
############################################################################### | ||
# With scoping | ||
# ~~~~~~~ | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field1.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] | ||
field2.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] | ||
|
||
############################################################################### | ||
# Next, we need to provide information about the scoping. | ||
# DPF needs to know what are the IDs of the data we just provided, | ||
# so that it can apply an operator on a subset of the original data. | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# Here by providing these integers we only select the data with an ID in common, | ||
# thus we are selecting the third elementary data of the first field, | ||
# and the first elementary data of the second field, | ||
# Other elementary data won't be taken into account when using an operator which needs 2 operands | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field1.scoping.ids = [1, 2, 3] | ||
field2.scoping.ids = [3, 4, 5] | ||
|
||
add_op = dpf.operators.math.add(field1, field2) | ||
field3 = add_op.eval() | ||
|
||
# only the third entity was changed, | ||
# because it is the only operator for which 2 operands were provided | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(field3.data) | ||
# [[8. 10. 12.]] | ||
print(field3.get_entity_data_by_id(3)) | ||
|
||
############################################################################### | ||
# Dot product | ||
|
||
dot_op = dpf.operators.math.generalized_inner_product(field1, field2) | ||
|
||
# We obtain zeros for IDs where there could not be 2 operands | ||
ansys-akarcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# (7. * 1.) + (8. * 2.) + (9. * 3.) = 50. | ||
# [0. 0. 50. 0. 0.] | ||
field3 = dot_op.eval() | ||
print(field3.data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.