Skip to content

Commit

Permalink
return full array for nodal solution (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
akaszynski authored Mar 7, 2023
1 parent 029d87c commit 9cf59cf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
16 changes: 8 additions & 8 deletions ansys/mapdl/reader/cyclic_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def plot_nodal_thermal_strain(
sel_type_all=True,
add_text=True,
overlay_wireframe=False,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal thermal strain.
Expand Down Expand Up @@ -819,7 +819,7 @@ def plot_nodal_elastic_strain(
sel_type_all=True,
add_text=True,
overlay_wireframe=False,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal elastic strain.
Expand Down Expand Up @@ -966,7 +966,7 @@ def plot_nodal_plastic_strain(
sel_type_all=True,
add_text=True,
overlay_wireframe=False,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal plastic strain.
Expand Down Expand Up @@ -1134,7 +1134,7 @@ def plot_nodal_solution(
overlay_wireframe=False,
add_text=True,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot the nodal solution (generally displacement).
Expand Down Expand Up @@ -1249,7 +1249,7 @@ def plot_nodal_stress(
overlay_wireframe=False,
add_text=True,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal stress of a given component
Expand Down Expand Up @@ -1359,7 +1359,7 @@ def plot_principal_nodal_stress(
sel_type_all=True,
add_text=True,
overlay_wireframe=False,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot the nodal principal stress.
Expand Down Expand Up @@ -1507,7 +1507,7 @@ def plot_nodal_temperature(
add_text=True,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot the nodal temperature.
Expand Down Expand Up @@ -1813,7 +1813,7 @@ def _plot_cyclic_point_scalars(
element_components=None,
sel_type_all=True,
phase=None,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot point scalars on active mesh.
Expand Down
40 changes: 26 additions & 14 deletions ansys/mapdl/reader/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def plot_nodal_solution(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plots the nodal solution.
Expand Down Expand Up @@ -1589,31 +1589,43 @@ def _nodal_solution_result(
mask = np.zeros(self.grid.n_points, bool)
for node_component in nodes:
if node_component not in self.grid.point_data:
raise ValueError(f"Invalid node component {nodes}")
raise ValueError(f"Invalid node component '{nodes}'.")
mask = np.logical_or(
mask, self.grid.point_data[node_component].view(bool)
)
else:
raise TypeError(
f"Invalid type for {nodes}. Expected Sequence of " "str or int"
f"Invalid type for {nodes}. Expected Sequence of str or int."
)
else:
raise TypeError(
f"Invalid type for {nodes}. Expected Sequence of " "str or int"
f"Invalid type for {nodes}. Expected Sequence of str or int."
)

# mask is for global nodes, need for potentially subselectd nodes
submask = np.in1d(nnum, self.grid.point_data["ansys_node_num"][mask])
nnum, result = nnum[submask], result[submask]

# always return the full array
if nnum.size < self._neqv.size:
# repopulate full array
nnum_full = self._neqv[self._sidx]
mask = np.in1d(nnum_full, nnum, assume_unique=True)
result_full = np.empty(
(nnum_full.size, result.shape[1]), dtype=result.dtype
)
result_full[:] = np.nan
result_full[mask] = result
return nnum_full, result_full

return nnum, result

@wraps(nodal_solution)
def nodal_displacement(self, *args, **kwargs):
"""wraps plot_nodal_solution"""
if self._is_thermal:
raise AttributeError(
"Thermal solution does not contain nodal " "displacement results"
"Thermal solution does not contain nodal displacement results."
)
return self.nodal_solution(*args, **kwargs)

Expand Down Expand Up @@ -2772,7 +2784,7 @@ def plot_principal_nodal_stress(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot the principal stress.
Expand Down Expand Up @@ -2893,7 +2905,7 @@ def _plot_point_scalars(
element_components=None,
sel_type_all=True,
movie_filename=None,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
progress_bar=True,
**kwargs,
):
Expand Down Expand Up @@ -3378,7 +3390,7 @@ def plot_nodal_stress(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plots the stresses at each node in the solution.
Expand Down Expand Up @@ -4310,7 +4322,7 @@ def plot_cylindrical_nodal_stress(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal_stress in the cylindrical coordinate system.
Expand Down Expand Up @@ -4401,7 +4413,7 @@ def plot_nodal_temperature(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal temperature
Expand Down Expand Up @@ -4536,7 +4548,7 @@ def plot_nodal_thermal_strain(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal component thermal strains.
Expand Down Expand Up @@ -4676,7 +4688,7 @@ def plot_nodal_elastic_strain(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal elastic strain.
Expand Down Expand Up @@ -4811,7 +4823,7 @@ def plot_nodal_plastic_strain(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal component plastic strain.
Expand Down Expand Up @@ -4895,7 +4907,7 @@ def _plot_nodal_result(
node_components=None,
element_components=None,
sel_type_all=True,
treat_nan_as_zero=True,
treat_nan_as_zero=False,
**kwargs,
):
"""Plot nodal result"""
Expand Down
13 changes: 9 additions & 4 deletions tests/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ def test_sparse_nodal_solution():
def test_is16():
npz_rst = np.load(is16_known_result)
nnum, data = is16.nodal_solution(0)
# remove NAN data
mask = ~np.isnan(data[:, 0])
nnum = nnum[mask]
data = data[mask]

assert np.allclose(data, npz_rst["data"], atol=1e-6)
assert np.allclose(nnum, npz_rst["nnum"])

Expand Down Expand Up @@ -458,7 +463,7 @@ def test_nnum_of_interest(nnum_of_interest):
@pytest.mark.parametrize("nodes", [range(11, 50), "NCOMP2", ("NCOMP2", "NODE_COMP")])
def test_nodes_subselection(hex_rst, nodes):
nnum_sel, data_sel = hex_rst.nodal_solution(0, nodes=nodes)
nnum, data = hex_rst.nodal_solution(0, nodes=nodes)
nnum, data = hex_rst.nodal_solution(0)

grid_nnum = hex_rst.grid.point_data["ansys_node_num"]
if isinstance(nodes, str):
Expand All @@ -472,10 +477,10 @@ def test_nodes_subselection(hex_rst, nodes):
else:
nnum_of_interest = nodes

mask = np.in1d(nnum, nnum_of_interest)
mask = ~np.isnan(data_sel[:, 0])

assert np.allclose(nnum[mask], nnum_sel)
assert np.allclose(data[mask], data_sel, equal_nan=True)
assert np.allclose(nnum[mask], nnum_sel[mask])
assert np.allclose(data[mask], data_sel[mask], equal_nan=True)


def test_materials(materials_281_rst):
Expand Down

0 comments on commit 9cf59cf

Please sign in to comment.