Skip to content
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

Add DIST output to the KDIST and NDIST #826

Merged
merged 6 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ansys/mapdl/core/_commands/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

def parse_kdist(msg):
"""Parse the keypoint value from a keypoint message"""
finds = re.findall(NUM_PATTERN, msg)[-3:]
if len(finds) == 3:
finds = re.findall(NUM_PATTERN, msg)[-4:]
if len(finds) == 4:
return [float(val) for val in finds]


Expand Down Expand Up @@ -99,3 +99,9 @@ def parse_output_volume_area(msg):
res = re.search(r"OUTPUT (AREA|VOLUME|AREAS) =\s*([0-9]+)", msg)
if res is not None:
return int(res.group(2))

def parse_ndist(msg):
"""Parse the node value from a node message"""
finds = re.findall(NUM_PATTERN, msg)[-4:]
if len(finds) == 4:
return [float(val) for val in finds]
24 changes: 12 additions & 12 deletions ansys/mapdl/core/_commands/preproc/keypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,27 @@ def kdist(self, kp1="", kp2="", **kwargs) -> list:

APDL Command: KDIST

KDIST lists the distance between keypoints KP1 and KP2, as
well as the current coordinate system offsets from KP1 to KP2,
where the X, Y, and Z locations of KP1 are subtracted from the
X, Y, and Z locations of KP2 (respectively) to determine the
offsets. KDIST is valid in any coordinate system except
toroidal [CSYS,3].

This command is valid in any processor.

Parameters
----------
kp1
First keypoint in distance calculation.

kp2
Second keypoint in distance calculation.

Returns
-------
list
``[X, Y, Z]`` distance between two keypoints.
``[DIST, X, Y, Z]`` distance between two keypoints.

Notes
-----
KDIST lists the distance between keypoints KP1 and KP2, as
well as the current coordinate system offsets from KP1 to KP2,
where the X, Y, and Z locations of KP1 are subtracted from the
X, Y, and Z locations of KP2 (respectively) to determine the
offsets. KDIST is valid in any coordinate system except
toroidal [CSYS,3].
This command is valid in any processor.

Examples
--------
Expand All @@ -232,7 +232,7 @@ def kdist(self, kp1="", kp2="", **kwargs) -> list:
>>> knum1 = mapdl.k("", *kp1)
>>> dist = mapdl.kdist(knum0, knum1)
>>> dist
[1.0, -5.0, 13.0]
[13.96424004376894, 1.0, -5.0, 13.0]

"""
return parse.parse_kdist(self.run(f"KDIST,{kp1},{kp2}", **kwargs))
Expand Down
26 changes: 21 additions & 5 deletions ansys/mapdl/core/_commands/preproc/nodes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from ansys.mapdl.core._commands import parse

class Nodes:
def center(self, node="", node1="", node2="", node3="", radius="", **kwargs):
Expand Down Expand Up @@ -384,29 +385,44 @@ def ndist(self, nd1="", nd2="", **kwargs):
First node in distance calculation. If ND1 = P, graphical picking
is enabled and all remaining command fields are ignored (valid only
in the GUI).

nd2
Second node in distance calculation.

Returns
-------
list
``[DIST, X, Y, Z]`` distance between two nodes.

Notes
-----
NDIST lists the distance between nodes ND1 and ND2, as well as the
current coordinate system offsets from ND1 to ND2, where the X, Y, and
Z locations of ND1 are subtracted from the X, Y, and Z locations of ND2
(respectively) to determine the offsets. NDIST is valid in any
coordinate system except toroidal [CSYS,3].

NDIST returns a variable, called "_RETURN," which contains the distance
value. You can use this value for various purposes, such as the
calculation of distributed loads. In interactive mode, you can access
this command by using the Model Query Picker (Utility Menu> List>
Picked Entities), where you can also access automatic annotation
functions and display the value on your model.

This command is valid in any processor.

Examples
--------
Compute the distance between two nodes.

>>> node1 = (0, 8, -3)
>>> node2 = (13, 5, 7)
>>> node_num1 = mapdl.n("", *node1)
>>> node_num2 = mapdl.n("", *node2)
>>> node_dist = mapdl.ndist(node_num1, node_num2)
>>> node_dist
[16.673332000533065, 13.0, -3.0, 10.0]

"""
command = f"NDIST,{nd1},{nd2}"
return self.run(command, **kwargs)

return parse.parse_ndist(self.run(f"NDIST,{nd1},{nd2}", **kwargs))

def ngen(
self,
Expand Down
21 changes: 20 additions & 1 deletion tests/test_geometry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test geometry commands"""
import math

import numpy as np


Expand Down Expand Up @@ -166,7 +168,10 @@ def test_kdist(cleared, mapdl):

knum0 = mapdl.k("", *kp0)
knum1 = mapdl.k("", *kp1)
xdist, ydist, zdist = mapdl.kdist(knum0, knum1)
kpdist, xdist, ydist, zdist = mapdl.kdist(knum0, knum1)
assert kpdist == round(math.sqrt((kp1[0] - kp0[0])**2
+ (kp1[1] - kp0[1])**2
+ (kp1[2] - kp0[2])**2), 7)
assert xdist == kp1[0] - kp0[0]
assert ydist == kp1[1] - kp0[1]
assert zdist == kp1[2] - kp0[2]
Expand Down Expand Up @@ -456,3 +461,17 @@ def test_sphere(cleared, mapdl):

def test_sph5(cleared, mapdl):
assert mapdl.sph5(xedge1=1, yedge1=1, xedge2=2, yedge2=2) == 1

def test_ndist(cleared, mapdl):
node1 = (0, -5, 13)
node2 = (-10, 70, 1)

node_num1 = mapdl.n("", *node1)
node_num2 = mapdl.n("", *node2)
node_dist, node_xdist, node_ydist, node_zdist = mapdl.ndist(node_num1, node_num2)
assert node_dist == round(math.sqrt((node2[0] - node1[0]) ** 2
+ (node2[1] - node1[1]) ** 2
+ (node2[2] - node1[2]) ** 2), 7)
assert node_xdist == node2[0] - node1[0]
assert node_ydist == node2[1] - node1[1]
assert node_zdist == node2[2] - node1[2]