-
Notifications
You must be signed in to change notification settings - Fork 23
Stress gradient path example #179
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b4c8c4c
stress gradient example
ayush-kumar-423 3dce702
added hemisphere.rst to examples
ayush-kumar-423 32343f9
added comments
ayush-kumar-423 2574253
newline at the end[F
ayush-kumar-423 fd2d7e0
comment correction
ayush-kumar-423 b4bc3de
comment correction
ayush-kumar-423 ad1c2cb
added hemisphere.rst to setup.py
ayush-kumar-423 b17b175
updated comments
ayush-kumar-423 4e83b9a
Merge branch 'master' into stress_gradient_path_example
ayush-kumar-423 b497454
added chart
ayush-kumar-423 f630921
Merge branch 'master' into stress_gradient_path_example
ayush-kumar-423 a775e42
implemented review suggestions
ayush-kumar-423 b580c44
Update examples/03-advanced/06-stress_gradient_path.py
ayush-kumar-423 d0b0fc2
moving rst to downloads
ayush-kumar-423 b40ce77
moving rst to downloads
ayush-kumar-423 dbf3e3a
removed path print
ayush-kumar-423 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
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,145 @@ | ||
""" | ||
.. _stress_gradient_path: | ||
|
||
Stress gradient normal to a defined node. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
This example shows how to plot a stress gradient normal to a selected node. | ||
As the example is based on creating a path along the normal, the selected node | ||
must be on the surface of the geometry. | ||
A path is created of a defined length. | ||
|
||
""" | ||
|
||
############################################################################### | ||
# First, import the DPF-Core module as ``dpf`` and import the | ||
# included examples file and ``DpfPlotter`` | ||
# | ||
import matplotlib.pyplot as plt | ||
from ansys.dpf import core as dpf | ||
from ansys.dpf.core import operators as ops | ||
from ansys.dpf.core.plotter import DpfPlotter | ||
from ansys.dpf.core import examples | ||
|
||
############################################################################### | ||
# Next, open an example and print out 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. | ||
# | ||
# Printing the model displays: | ||
# | ||
# - Analysis type | ||
# - Available results | ||
# - Size of the mesh | ||
# - Number of results | ||
# - Unit | ||
# | ||
path = examples.download_hemisphere() | ||
model = dpf.Model(path) | ||
print(model) | ||
############################################################################### | ||
# Define the `node_id` normal to which a stress gradient should be plotted. | ||
# | ||
node_id = 1928 | ||
############################################################################### | ||
# The following command prints the mesh unit | ||
# | ||
unit = model.metadata.meshed_region.unit | ||
print("Unit: %s" % unit) | ||
############################################################################### | ||
# `depth` defines the path length / depth to which the path will penetrate. | ||
# While defining `depth` make sure you use the correct mesh unit. | ||
# `delta` defines distance between consecutive points on the path. | ||
depth = 10 # in mm | ||
delta = 0.1 # in mm | ||
############################################################################### | ||
# Get the meshed region | ||
# | ||
mesh = model.metadata.meshed_region | ||
############################################################################### | ||
# Get Equivalent stress fields container. | ||
# | ||
stress_fc = model.results.stress().eqv().outputs.fields_container() | ||
############################################################################### | ||
# Define Nodal scoping. | ||
# Make sure to define ``"Nodal"`` as the requested location, important for the | ||
# `normals` operator. | ||
# | ||
nodal_scoping = dpf.Scoping(location=dpf.locations.nodal) | ||
nodal_scoping.ids = [node_id] | ||
############################################################################### | ||
# Get Skin Mesh because `normals` operator requires Shells as input. | ||
# | ||
skin_mesh = ops.mesh.skin(mesh=mesh) | ||
skin_meshed_region = skin_mesh.outputs.mesh.get_data() | ||
############################################################################### | ||
# Get normal at a node using `normals` operator. | ||
PProfizi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
normal = ops.geo.normals() | ||
normal.inputs.mesh.connect(skin_meshed_region) | ||
normal.inputs.mesh_scoping.connect(nodal_scoping) | ||
normal_vec_out_field = normal.outputs.field.get_data() | ||
############################################################################### | ||
# Normal vector is along the surface normal. We need to invert the vector | ||
# using `math.scale` operator inwards in the geometry, to get the path | ||
# direction. | ||
# | ||
normal_vec_in_field = ops.math.scale(field=normal_vec_out_field, | ||
ponderation=-1.0) | ||
normal_vec_in = normal_vec_in_field.outputs.field.get_data().data[0] | ||
############################################################################### | ||
# Get Nodal coordinates, they serve as the first point on the line. | ||
# | ||
node = mesh.nodes.node_by_id(node_id) | ||
line_fp = node.coordinates | ||
############################################################################### | ||
# Create 3D line equation. | ||
# | ||
fx = lambda t: line_fp[0] + normal_vec_in[0] * t | ||
fy = lambda t: line_fp[1] + normal_vec_in[1] * t | ||
fz = lambda t: line_fp[2] + normal_vec_in[2] * t | ||
############################################################################### | ||
# Create coordinates using 3D line equation- | ||
# | ||
coordinates = [[fx(t * delta), fy(t * delta), fz(t * delta)] for t in | ||
range(int(depth / delta))] | ||
flat_coordinates = [entry for data in coordinates for entry in data] | ||
############################################################################### | ||
# Create Field for coordinates of the path. | ||
# | ||
field_coord = dpf.fields_factory.create_3d_vector_field(len(coordinates)) | ||
field_coord.data = flat_coordinates | ||
field_coord.scoping.ids = list(range(1, len(coordinates) + 1)) | ||
############################################################################### | ||
# Let's now map results on the path. | ||
mapping_operator = ops.mapping.on_coordinates( | ||
fields_container=stress_fc, | ||
coordinates=field_coord, | ||
create_support=True, | ||
mesh=mesh) | ||
fields_mapped = mapping_operator.outputs.fields_container() | ||
############################################################################### | ||
# Here, we request the mapped field data and its mesh | ||
field_m = fields_mapped[0] | ||
mesh_m = field_m.meshed_region | ||
############################################################################### | ||
# Create stress vs length chart. | ||
# | ||
x_initial = 0.0 | ||
length = [x_initial + delta * index for index in range(len(field_m.data))] | ||
plt.plot(length, field_m.data, "r") | ||
plt.xlabel("Length (%s)" % mesh.unit) | ||
plt.ylabel("Stress (%s)" % field_m.unit) | ||
plt.show() | ||
############################################################################### | ||
# To create a plot we need to add both the meshes | ||
# `mesh_m` - mapped mesh | ||
# `mesh` - original mesh | ||
pl = DpfPlotter() | ||
pl.add_field(field_m, mesh_m) | ||
pl.add_mesh(mesh, style="surface", show_edges=True, | ||
color="w", opacity=0.3) | ||
pl.show_figure(show_axes=True, cpos=[ | ||
(62.687, 50.119, 67.247), | ||
(5.135, 6.458, -0.355), | ||
(-0.286, 0.897, -0.336)]) |
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
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.