1from typing import Tuple, Union 

2 

3from .datahandling_interface import DataHandling 

4from .serial_datahandling import SerialDataHandling 

5 

6try: 

7 # noinspection PyPep8Naming 

8 import waLBerla as wlb 

9 if wlb.cpp_available: 

10 from pystencils.datahandling.parallel_datahandling import ParallelDataHandling 

11 else: 

12 wlb = None 

13except ImportError: 

14 wlb = None 

15 ParallelDataHandling = None 

16 

17 

18def create_data_handling(domain_size: Tuple[int, ...], 

19 periodicity: Union[bool, Tuple[bool, ...]] = False, 

20 default_layout: str = 'SoA', 

21 default_target: str = 'cpu', 

22 parallel: bool = False, 

23 default_ghost_layers: int = 1, 

24 opencl_queue=None) -> DataHandling: 

25 """Creates a data handling instance. 

26 

27 Args: 

28 domain_size: size of the rectangular domain 

29 periodicity: either True, False for full or no periodicity or a tuple of booleans indicating periodicity 

30 for each coordinate 

31 default_layout: default array layout, that is used if not explicitly specified in 'add_array' 

32 default_target: either 'cpu' or 'gpu' 

33 parallel: if True a parallel domain is created using walberla - each MPI process gets a part of the domain 

34 default_ghost_layers: default number of ghost layers if not overwritten in 'add_array' 

35 """ 

36 if parallel: 36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never true

37 assert not opencl_queue, "OpenCL is only supported for SerialDataHandling" 

38 if wlb is None: 

39 raise ValueError("Cannot create parallel data handling because walberla module is not available") 

40 

41 if periodicity is False or periodicity is None: 

42 periodicity = (0, 0, 0) 

43 elif periodicity is True: 

44 periodicity = (1, 1, 1) 

45 else: 

46 periodicity = tuple(int(bool(x)) for x in periodicity) 

47 if len(periodicity) == 2: 

48 periodicity += (1,) 

49 

50 if len(domain_size) == 2: 

51 dim = 2 

52 domain_size = (domain_size[0], domain_size[1], 1) 

53 else: 

54 dim = 3 

55 

56 # noinspection PyArgumentList 

57 block_storage = wlb.createUniformBlockGrid(cells=domain_size, periodic=periodicity) 

58 return ParallelDataHandling(blocks=block_storage, dim=dim, default_target=default_target, 

59 default_layout=default_layout, default_ghost_layers=default_ghost_layers) 

60 else: 

61 return SerialDataHandling(domain_size, 

62 periodicity=periodicity, 

63 default_target=default_target, 

64 default_layout=default_layout, 

65 default_ghost_layers=default_ghost_layers, 

66 opencl_queue=opencl_queue) 

67 

68 

69__all__ = ['create_data_handling']