Skip to content

Commit 4d4c63e

Browse files
authored
Improve performances (#108)
1 parent f303530 commit 4d4c63e

File tree

12 files changed

+339
-44
lines changed

12 files changed

+339
-44
lines changed

.ci/run_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pathlib
44
from ansys.dpf import core
55

6-
core.disable_off_screen_rendering()
6+
core.settings.disable_off_screen_rendering()
77

88
actual_path = pathlib.Path(__file__).parent.absolute()
99
print(os.path.join(actual_path, os.path.pardir, "examples"))

ansys/dpf/core/__init__.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
if "AWP_UNIT_TEST_FILES" not in os.environ:
1515
os.environ["AWP_UNIT_TEST_FILES"] = "/mnt/ansys_inc/dpf/test_files/"
1616

17-
from ansys.dpf.core.misc import module_exists, Report
17+
from ansys.dpf.core.misc import Report
1818
from ansys.dpf.core.dpf_operator import Operator, Config
1919
from ansys.dpf.core.model import Model
2020
from ansys.dpf.core.field import Field, FieldDefinition
@@ -61,6 +61,7 @@
6161
)
6262
from ansys.dpf.core import server
6363
from ansys.dpf.core import check_version
64+
from ansys.dpf.core import settings
6465

6566
# for matplotlib
6667
# solves "QApplication: invalid style override passed, ignoring it."
@@ -91,29 +92,8 @@
9192
pass
9293

9394

94-
# Configure PyVista's ``rcParams`` for dpf
95-
if module_exists("pyvista"):
96-
import pyvista as pv
97-
98-
pv.rcParams["interactive"] = True
99-
pv.rcParams["cmap"] = "jet"
100-
pv.rcParams["font"]["family"] = "courier"
101-
pv.rcParams["title"] = "DPF"
102-
103-
10495
SERVER = None
10596

10697
_server_instances = []
10798

108-
def disable_off_screen_rendering() -> None:
109-
# enable matplotlib off_screen plotting to avoid test interruption
110-
if module_exists("matplotlib"):
111-
import matplotlib as mpl
112-
113-
mpl.use("Agg")
114-
115-
# enable off_screen plotting to avoid test interruption
116-
if module_exists("pyvista"):
117-
import pyvista as pv
118-
119-
pv.OFF_SCREEN = True
99+
settings.set_default_pyvista_config()

ansys/dpf/core/check_version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ def wrapper(self, *args, **kwargs):
173173
"""Call the original function"""
174174
server = self._server
175175
func_name = func.__name__
176-
class_name = self.__class__.__name__
177176

178177
# particular cases
179178
# scoping._set_ids case, must be checked in a particular way
180-
if func_name == "_set_ids" and class_name == "Scoping":
179+
from ansys.dpf.core import scoping
180+
if func_name == "_set_ids" and isinstance(self, scoping.Scoping):
181181
ids = args[0]
182182
size = len(ids)
183183
if size != 0:

ansys/dpf/core/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ansys.grpc.dpf import base_pb2, base_pb2_grpc
1414
from ansys.dpf.core.errors import protect_grpc
1515
from ansys.dpf.core import server as serverlib
16-
from ansys.dpf.core.misc import DEFAULT_FILE_CHUNK_SIZE
16+
from ansys.dpf.core import misc
1717
from ansys.dpf.core.common import _common_progress_bar
1818

1919
LOG = logging.getLogger(__name__)
@@ -716,7 +716,7 @@ def __file_chunk_yielder(self, file_path, to_server_file_path, use_tmp_dir=False
716716
i = 0
717717
with open(file_path, "rb") as f:
718718
while True:
719-
piece = f.read(DEFAULT_FILE_CHUNK_SIZE)
719+
piece = f.read(misc.DEFAULT_FILE_CHUNK_SIZE)
720720
if len(piece) == 0:
721721
break
722722
request.data.data = piece

ansys/dpf/core/field_base.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,13 @@ def __cache_data__(self):
532532
self._data_copy = super().data_as_list
533533
self._num_entities_reserved = len(self._data_copy)
534534
self._data_pointer_copy = super()._data_pointer_as_list
535-
self._scoping_ids_copy = super().scoping.ids
536-
self._num_entities = len(self._scoping_ids_copy)
535+
self._scoping_copy = super().scoping.as_local_scoping()
537536
self._has_data_pointer = len(self._data_pointer_copy) > 0
538537

538+
@property
539+
def _num_entities(self):
540+
return len(self._scoping_copy)
541+
539542
@property
540543
def size(self):
541544
"""Length of the data vector.
@@ -589,7 +592,7 @@ def get_entity_data(self, index):
589592
if index > self._num_entities:
590593
raise ValueError(
591594
f"Requested scoping {index} is greater than the number of "
592-
f"available indices {len(self._scoping_ids_copy)}"
595+
f"available indices {len(self._scoping_copy)}"
593596
)
594597
if self._has_data_pointer:
595598
first_index = self._data_pointer_copy[index]
@@ -649,7 +652,7 @@ def get_entity_data_by_id(self, id):
649652
7.69014221e+02 4.90502930e+02]]
650653
651654
"""
652-
index = self._scoping_ids_copy.index(id)
655+
index = self._scoping_copy.index(id)
653656
if index < 0:
654657
raise ValueError(f"The id {id} doesn't exist in the scoping")
655658
return self.get_entity_data(index)
@@ -687,12 +690,11 @@ def append(self, data, scopingid):
687690
data = np.array(data).flatten().tolist()
688691

689692
data_size = len(self._data_copy)
690-
self._scoping_ids_copy.append(scopingid)
693+
self._scoping_copy.append(scopingid)
691694
if len(self._data_pointer_copy) > 0:
692695
self._data_pointer_copy.append(data_size)
693696

694697
self._data_copy.extend(data)
695-
self._num_entities += 1
696698
if self._has_data_pointer == False:
697699
if isinstance(data, (np.ndarray, np.generic)):
698700
data_size = data.size
@@ -849,27 +851,46 @@ def scoping_ids(self):
849851
list
850852
List of integers representing the scoping IDs of the field.
851853
"""
852-
return self._scoping_ids_copy
854+
return self._scoping_copy.ids
853855

854856
@scoping_ids.setter
855857
def scoping_ids(self, data):
856-
self._scoping_ids_copy = data
857-
self._num_entities = len(data)
858+
self._scoping_copy.ids = data
859+
860+
@property
861+
def scoping(self):
862+
"""Scoping specifying where the data is.
863+
864+
Each entity data is on a given scoping ID.
865+
866+
Returns
867+
-------
868+
scoping : :class:`ansys.dpf.core.scoping.Scoping`
869+
"""
870+
return self._scoping_copy
871+
872+
@scoping.setter
873+
def scoping(self, data):
874+
self._scoping_copy = data
858875

859876
def release_data(self):
860877
"""Release the data."""
861878
super()._set_data(self._data_copy)
862879
super()._set_data_pointer(self._data_pointer_copy)
863-
super().scoping.ids = self._scoping_ids_copy
880+
self._scoping_copy.release_data()
864881

865882
def __enter__(self):
866883
return self
867884

868885
def __exit__(self, type, value, tb):
869886
if tb is None:
887+
self._is_exited = True
870888
self.release_data()
871889
else:
872890
print(tb)
873891

874892
def __del__(self):
893+
if not hasattr(self, "_is_exited") or not self._is_exited:
894+
self._is_exited = True
895+
self.release_data()
875896
pass

ansys/dpf/core/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pkgutil import iter_modules
66

77

8-
DEFAULT_FILE_CHUNK_SIZE = 65536
8+
DEFAULT_FILE_CHUNK_SIZE = 524288
99

1010
# ANSYS CPython Workbench environment may not have scooby installed.
1111
try:

0 commit comments

Comments
 (0)