Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
extensions = [
"jupyter_sphinx",
"notfound.extension", # for the not found page.
"numpydoc",
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"numpydoc",
"sphinx.ext.coverage",
"sphinx.ext.intersphinx",
"sphinx_copybutton",
Expand Down
2 changes: 1 addition & 1 deletion doc/source/links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.. _grpc: https://grpc.io/
.. _numpy_docs: https://numpy.org/doc/stable/
.. _precommit: https://pre-commit.com/
.. _scipy_docs: https://docs.scipy.org/doc/scipy/reference/
.. _scipy_docs: https://docs.scipy.org/doc/scipy/

.. #Other projects
.. _dpf_post_docs: https://post.docs.pyansys.com/
Expand Down
6 changes: 3 additions & 3 deletions examples/eigen_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
# get the stiff (``k``) and mass (``m``) matrices from the FULL file.

out = mm._mapdl.input(vmfiles["vm153"])

k = mm.stiff(fname="PRSMEMB.full")
m = mm.mass(fname="PRSMEMB.full")
fullfile = mm._mapdl.jobname + ".full"
k = mm.stiff(fname=fullfile)
m = mm.mass(fname=fullfile)


###############################################################################
Expand Down
3 changes: 2 additions & 1 deletion examples/scipy_sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
# get the stiff (``k``) matrix from the FULL file.

out = mm._mapdl.input(vmfiles["vm153"])
k = mm.stiff(fname="PRSMEMB.full")
fullfile = mm._mapdl.jobname + ".full"
k = mm.stiff(fname=fullfile)

################################################################################
# Copy AnsMath sparse matrix to SciPy CSR matrix and plot
Expand Down
8 changes: 4 additions & 4 deletions examples/solve_sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# After a solve command, the ``FULL`` file contains the assembled stiffness
# matrix, mass matrix, and load vector.
#
out = mm._mapdl.input(vmfiles["vm153"])
out = mm._mapdl.input(vmfiles["vm152"])

###############################################################################
# List files in current directory
Expand All @@ -47,7 +47,8 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Print the dimensions of the sparse matrix.

k = mm.stiff(fname="PRSMEMB.full", name="K")
fullfile = mm._mapdl.jobname + ".full"
k = mm.stiff(fname=fullfile, name="K")
k

################################################################################
Expand All @@ -74,8 +75,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Extract the load vector from the FULL file and print the norm of this
# vector.

b = mm.rhs(fname="PRSMEMB.full", name="B")
b = mm.rhs(fname=fullfile, name="B")
b.norm()

###############################################################################
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = [
"pyansys-tools-versioning>=0.3.3",
"numpy>=1.14.0",
"importlib-metadata>=4.0,<5; python_version<='3.8'",
"scipy>=1.3.0", # for sparse (consider optional?)
]

[project.optional-dependencies]
Expand Down Expand Up @@ -84,7 +85,7 @@ default_section = "THIRDPARTY"
src_paths = ["doc", "src", "tests"]

[tool.coverage.run]
source = ["src"]
source = ["ansys/pyansys-math"]

[tool.coverage.report]
show_missing = true
Expand Down
24 changes: 15 additions & 9 deletions src/ansys/math/core/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

from ansys.api.mapdl.v0 import ansys_kernel_pb2 as anskernel
from ansys.api.mapdl.v0 import mapdl_pb2 as pb_types
from ansys.mapdl.core import VERSION_MAP, launch_mapdl
from ansys.mapdl.core.common_grpc import (
ANSYS_VALUE_TYPE,
DEFAULT_CHUNKSIZE,
DEFAULT_FILE_CHUNK_SIZE,
)
from ansys.mapdl.core.errors import ANSYSDataTypeError, protect_grpc
from ansys.mapdl.core.launcher import launch_mapdl
from ansys.mapdl.core.errors import (
ANSYSDataTypeError,
MapdlRuntimeError,
VersionError,
protect_grpc,
)
from ansys.mapdl.core.misc import load_file
from ansys.mapdl.core.parameters import interp_star_status
from ansys.tools.versioning import requires_version
from ansys.tools.versioning.exceptions import VersionError
from ansys.tools.versioning.utils import server_meets_version
import numpy as np

Expand Down Expand Up @@ -165,7 +169,7 @@ def status(self):
"""
print(self._status)

def vec(self, size=0, dtype=np.double, init="zeros", name=None, asarray=False):
def vec(self, size=0, dtype=np.double, init=None, name=None, asarray=False):
"""Create a vector.

Parameters
Expand Down Expand Up @@ -209,7 +213,7 @@ def vec(self, size=0, dtype=np.double, init="zeros", name=None, asarray=False):

return vec

def mat(self, nrow=1, ncol=1, dtype=np.double, init="zeros", name=None, asarray=False):
def mat(self, nrow=1, ncol=1, dtype=np.double, init=None, name=None, asarray=False):
"""Create a matrix.

Parameters
Expand Down Expand Up @@ -252,7 +256,9 @@ def mat(self, nrow=1, ncol=1, dtype=np.double, init="zeros", name=None, asarray=
mat.rand()
elif init == "ones":
mat.ones()
elif init != "zeros":
elif init == "zeros" or init is None:
mat.zeros()
elif init is not None:
raise ValueError(f"Invalid initialization method '{init}'.")
else:
info = self._mapdl._data_info(name)
Expand Down Expand Up @@ -1171,7 +1177,7 @@ def _set_mat(self, mname, arr, sym=False, dtype=None, chunk_size=DEFAULT_CHUNKSI
else: # must be dense matrix
self._send_dense(mname, arr, dtype, chunk_size)

@requires_version((0, 4, 0))
@requires_version((0, 4, 0), VERSION_MAP)
def _send_dense(self, mname, arr, dtype, chunk_size):
"""Send a dense NumPy array/matrix to MAPDL."""
if dtype is not None:
Expand Down Expand Up @@ -1417,10 +1423,10 @@ def __init__(self, id_, mapdl, dtype=np.double, init=None):

@property
def size(self):
"""Number of items in the vector."""
"""Number of items in this vector."""
sz = self._mapdl.scalar_param(f"{self.id}_DIM")
if sz is None:
raise RuntimeError("This vector has been deleted within MAPDL.")
raise MapdlRuntimeError("This vector has been deleted within MAPDL.")
return int(sz)

def __repr__(self):
Expand Down
46 changes: 31 additions & 15 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import re

from ansys.mapdl.core.errors import ANSYSDataTypeError
from ansys.mapdl.core.errors import ANSYSDataTypeError, MapdlRuntimeError
from ansys.mapdl.core.launcher import get_start_instance
from ansys.mapdl.core.misc import random_string
from ansys.tools.versioning.exceptions import VersionError
Expand Down Expand Up @@ -107,13 +107,11 @@ def test_vec(mm):
assert isinstance(arr, np.ndarray)


def test_vec_from_name(mm):
vec0 = mm.vec(10)
@pytest.mark.parametrize("vecval", [np.zeros(10), np.array([1.0, 2.0, 8.0, 5.0, 6.2]), np.ones(3)])
def test_vec_from_name(mm, vecval):
vec0 = mm.set_vec(vecval)
vec1 = mm.vec(name=vec0.id)
assert np.allclose(vec0, vec1)

vec1 = mm.vec(name=vec0.id, asarray=True)
assert isinstance(vec1, np.ndarray)
assert np.allclose(vecval, vec1.asarray())


def test_vec__mul__(mm):
Expand Down Expand Up @@ -328,17 +326,35 @@ def test_load_matrix_from_file_incorrect_name(mm, cube_solve):
mm.load_matrix_from_file(name=1245)


def test_mat_from_name(mm):
def test_mat_asarray(mm):
mat0 = mm.mat(10, 10, asarray=True)
mat1 = mm.mat(10, 10)
assert np.allclose(mat0, mat1.asarray())


def test_mat_from_name_mapdl(mm):
mat0 = mm.mat(10, 10, init="ones") # The test has to be done with a
# value other than the default one.
mat1 = mm.mat(name=mat0.id)
assert np.allclose(mat0, mat1)


def test_mat_asarray(mm):
mat0 = mm.mat(10, 10, asarray=True)
mat1 = mm.mat(10, 10)
assert np.allclose(mat0, mat1.asarray())
@pytest.mark.parametrize(
"matval",
[
np.zeros((5, 5)),
np.array([[1.0, 2.0, 8.0, 5.0, 6.2], [5.1, 3.8, 8.2, 4.5, 2.0]]),
np.ones((3, 4)),
],
)
def test_mat_from_name_dense(mm, matval):
if not server_meets_version(mm._server_version, (0, 4, 0)):
with pytest.raises(VersionError):
mat0 = mm.matrix(matval)
else:
mat0 = mm.matrix(matval)
mat1 = mm.mat(name=mat0.id)
assert np.allclose(matval, mat1.asarray())


def test_mat_from_name_sparse(mm):
Expand Down Expand Up @@ -621,7 +637,7 @@ def test_invalid_sparse_name(mm):
def test_free(mm):
my_mat = mm.ones(10)
mm.free()
with pytest.raises(RuntimeError, match="This vector has been deleted"):
with pytest.raises(MapdlRuntimeError, match="This vector has been deleted"):
my_mat.size


Expand Down Expand Up @@ -687,7 +703,7 @@ def test_factorize_inplace_arg(mm):
def test_mult(mapdl, mm):
rand_ = np.random.rand(100, 100)

if not server_meets_version(mapdl._server_version, (0, 4, 0)):
if not server_meets_version(mm._server_version, (0, 4, 0)):
with pytest.raises(VersionError):
AA = mm.matrix(rand_, name="AA")

Expand All @@ -708,7 +724,7 @@ def test__parm(mm):
mat = sparse.random(sz, sz, density=0.05, format="csr")

rand_ = np.random.rand(100, 100)
if not server_meets_version(mm._mapdl._server_version, (0, 4, 0)):
if not server_meets_version(mm._server_version, (0, 4, 0)):
with pytest.raises(VersionError):
AA = mm.matrix(rand_, name="AA")

Expand Down