Skip to content
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
2 changes: 1 addition & 1 deletion src/ansys/mapdl/core/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""The mapdl database module, allowing the access to the MAPDL database from Python."""

from .database import DBDef, MapdlDb # noqa: F401
from .database import DBDef, MapdlDb, check_mapdl_db_is_alive # noqa: F401
29 changes: 29 additions & 0 deletions src/ansys/mapdl/core/database/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Contains the MapdlDb classes, allowing the access to MAPDL DB from Python."""
from enum import Enum
from functools import wraps
import os
import time
from warnings import warn
Expand Down Expand Up @@ -38,6 +39,32 @@ def __exit__(self, *args, **kwargs):
self._mapdl._resume_routine()


def check_mapdl_db_is_alive(function):
"""
Decorator to check that the MAPDL.DB has started.

It works for the DB object (DBDef) and for the derived object which has "_db" attribute.
"""

@wraps(function)
def wrapper(self, *args, **kwargs):
if hasattr(self, "active"):
active = self.active
elif hasattr(self, "_db"):
active = self._db.active
else: # pragma: no cover
raise Exception("The DB object could not be found.")

if not active:
self._mapdl._log.error(
f"Please start the MAPDL DB Server to access '{function.__name__}'."
)
return None
return function(self, *args, **kwargs)

return wrapper


class DBDef(Enum): # From MAPDL ansysdef.inc include file

"""Database type definitions."""
Expand Down Expand Up @@ -311,6 +338,7 @@ def clear(self, **kwargs):
return self._mapdl.run("/CLEAR,ALL")

@property
@check_mapdl_db_is_alive
def nodes(self):
"""
MAPDL database nodes interface.
Expand Down Expand Up @@ -380,6 +408,7 @@ def nodes(self):
return self._nodes

@property
@check_mapdl_db_is_alive
def elems(self):
"""
MAPDL database element interface.
Expand Down
5 changes: 4 additions & 1 deletion src/ansys/mapdl/core/database/elems.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ansys.api.mapdl.v0 import mapdl_db_pb2

from . import DBDef, MapdlDb
from . import DBDef, MapdlDb, check_mapdl_db_is_alive


class DbElems:
Expand Down Expand Up @@ -66,6 +66,7 @@ def __str__(self):
lines.append(f" Maximum element number: {self.max_num}")
return "\n".join(lines)

@check_mapdl_db_is_alive
def first(self, ielm=0):
"""
Get the number of the first element.
Expand Down Expand Up @@ -99,6 +100,7 @@ def first(self, ielm=0):
self._itelm = ielm
return self.next()

@check_mapdl_db_is_alive
def next(self):
"""
Return the number of the next selected element.
Expand Down Expand Up @@ -154,6 +156,7 @@ def next(self):
# return self._itelm
###########################################################################

@check_mapdl_db_is_alive
def info(self, ielm, ikey):
"""
Get information about a element
Expand Down
10 changes: 9 additions & 1 deletion src/ansys/mapdl/core/database/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from numpy.lib import recfunctions

from ..common_grpc import DEFAULT_CHUNKSIZE
from .database import DBDef, MapdlDb
from .database import DBDef, MapdlDb, check_mapdl_db_is_alive


class DbNodes:
Expand Down Expand Up @@ -95,6 +95,7 @@ def _db(self):
"""Return the weakly referenced instance of db."""
return self._db_weakref()

@check_mapdl_db_is_alive
def first(self, inod=0):
"""
Return the number of the first node.
Expand Down Expand Up @@ -127,6 +128,7 @@ def first(self, inod=0):
self._itnod = inod
return self.next()

@check_mapdl_db_is_alive
def next(self):
"""
Return the number of the next selected node.
Expand Down Expand Up @@ -180,6 +182,7 @@ def next(self):
# self._itnod = result.inum
# return self._itnod

@check_mapdl_db_is_alive
def info(self, inod, ikey):
"""
Return information about a node.
Expand Down Expand Up @@ -266,6 +269,7 @@ def info(self, inod, ikey):
result = self._db._stub.NodInqr(request)
return result.ret

@check_mapdl_db_is_alive
def num(self, selected=False) -> int:
"""
Return the number of nodes, either selected or all.
Expand Down Expand Up @@ -295,6 +299,7 @@ def num(self, selected=False) -> int:
return self.info(0, DBDef.DB_NUMDEFINED.value)

@property
@check_mapdl_db_is_alive
def max_num(self) -> int:
"""
Return the maximum node number.
Expand All @@ -310,6 +315,7 @@ def max_num(self) -> int:
"""
return self.info(0, DBDef.DB_MAXDEFINED.value)

@check_mapdl_db_is_alive
def coord(self, inod):
"""
Return the location of a node.
Expand Down Expand Up @@ -345,6 +351,7 @@ def coord(self, inod):
node = self._db._stub.getNod(request)
return node.kerr, tuple(node.v)

@check_mapdl_db_is_alive
def all_asarray(self):
"""
Return all node indices, coordinates, and angles as arrays.
Expand Down Expand Up @@ -442,6 +449,7 @@ def all_asarray(self):

return ind, coord, angle

@check_mapdl_db_is_alive
def push(self, inod, x, y, z, xang=None, yang=None, zang=None):
"""
Push a single node into the DB.
Expand Down
11 changes: 9 additions & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from ansys.mapdl.core.database import DBDef, MapdlDb
from ansys.mapdl.core.misc import random_string

pytestmark = pytest.mark.skip


@pytest.fixture(scope="session")
def db(mapdl):
Expand Down Expand Up @@ -215,3 +213,12 @@ def test__channel_str(db):
assert ":" in db._channel_str
assert re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", db._channel_str)
assert re.search("\d{4,6}", db._channel_str)


def test_off_db(mapdl, db):
"""Testing that when there is no active database"""
if db.active:
db.stop()
assert not mapdl.db.active
assert mapdl.db.nodes is None
assert mapdl.db.elems is None