diff --git a/src/ansys/mapdl/core/database/__init__.py b/src/ansys/mapdl/core/database/__init__.py index 8e465a75a40..2202f7e5d25 100644 --- a/src/ansys/mapdl/core/database/__init__.py +++ b/src/ansys/mapdl/core/database/__init__.py @@ -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 diff --git a/src/ansys/mapdl/core/database/database.py b/src/ansys/mapdl/core/database/database.py index 6ab60cb3faf..4189dfba933 100644 --- a/src/ansys/mapdl/core/database/database.py +++ b/src/ansys/mapdl/core/database/database.py @@ -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 @@ -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.""" @@ -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. @@ -380,6 +408,7 @@ def nodes(self): return self._nodes @property + @check_mapdl_db_is_alive def elems(self): """ MAPDL database element interface. diff --git a/src/ansys/mapdl/core/database/elems.py b/src/ansys/mapdl/core/database/elems.py index 24bf8960003..09e0a6e667c 100644 --- a/src/ansys/mapdl/core/database/elems.py +++ b/src/ansys/mapdl/core/database/elems.py @@ -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: @@ -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. @@ -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. @@ -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 diff --git a/src/ansys/mapdl/core/database/nodes.py b/src/ansys/mapdl/core/database/nodes.py index 5c11eedbe8d..b1d8ae0515e 100644 --- a/src/ansys/mapdl/core/database/nodes.py +++ b/src/ansys/mapdl/core/database/nodes.py @@ -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: @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/tests/test_database.py b/tests/test_database.py index a78b777b477..17ec725b799 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -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): @@ -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