lattice.py 4.04 KB
Newer Older
MischaD's avatar
MischaD committed
1
import numpy as np
MischaD's avatar
MischaD committed
2
from lbmweights.utils.mylog import logger
MischaD's avatar
MischaD committed
3
4
from .exceptions import OrderNotImplementedException, ImpossibleVelocityShellException
from .utils.utils import analyse_tensor_dimension, get_group
MischaD's avatar
MischaD committed
5
6


MischaD's avatar
MischaD committed
7
class Lattice:
MischaD's avatar
MischaD committed
8
9
10
11
12
13
14
15
16
17
18
19
    def __init__(self, dimension=2, order=4, shell_list=[1, 2, 4], seed=None):
        """
        :param dimension:
        :param order:
        :param shell_list:
        :param seed:
        """
        self._dimensions = dimension
        self._order = order
        self._shell_list = shell_list
        self._seed = seed

MischaD's avatar
MischaD committed
20
21
22
        self._tensor_space_dimension = 0
        self._possible_tensors = []

MischaD's avatar
MischaD committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    def __str__(self):
        string = "D{} - Order: {} - Shells: {}".format(
            self._dimensions, self._order, str(self._shell_list)
        )
        if self._seed:
            string += " - Seed: {}".format(self._seed)
        return string

    @classmethod
    def init_by_order(cls, dimension, order, seed=None):
        """
        :param dimension:
        :param order:
        :return:
        """
        shell_list = []
        if dimension == 2:
            if order == 4:
                shell_list = [1, 2, 4]
            elif order == 6:
                shell_list = [1, 2, 4, 8, 9]
            elif order == 8:
                shell_list = [1, 2, 4, 5, 8, 9, 10, 16]

        elif dimension == 3:
            if order == 4:
                shell_list = [1, 2, 4]

        if not shell_list:
            raise OrderNotImplementedException(
                "Cannot initialize by {} for dimension {}. Lower the order or try to come up with the shells yourself.".format(
                    order, dimension
                )
            )
        return cls(dimension=dimension, order=order, shell_list=shell_list, seed=seed)
MischaD's avatar
MischaD committed
58

MischaD's avatar
MischaD committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    @property
    def tensor_space_dimension(self):
        return np.array([len(x) for x in self._possible_tensors]).sum()

    @staticmethod
    def velocities_for_shell(dimension, shell):
        """
        :param dimension: Lattice dimension
        :param shell: Squared velocity of the shell
        :return: list of velocities with squared length equal to shell
        """
        velocities = []
        linear_lattice_size = 2 * shell + 1
        full_lattice_size = linear_lattice_size ** dimension
        for site in range(full_lattice_size):
            tmp = site
            cur_vel_squared = 0
            cur_vec = []

            for dim in range(dimension):
                coordinate = tmp % linear_lattice_size
                tmp = (tmp - coordinate) // linear_lattice_size
                shifted_coordinate = coordinate - shell
                cur_vec.append(shifted_coordinate)
                cur_vel_squared += shifted_coordinate ** 2

            if cur_vel_squared == shell:
                velocities.append(np.array(cur_vec, dtype=int))

        return velocities

    def calculate_weights(self):
        logger.debug("Entering Calculate Weights")
        logger.info(str(self))
        if self._order % 2 == 1:
            self._order -= 1
            logger.warning("Odd order for isotropy entered. This package only handles symmetric lattices, therefore"
                           "the equations for odd orders are automatically fulfilled. "
                           "Reduction of order to {}".format(self._order))

        # Do tensor dimension analysis for every order up to the desired
        for k in np.arange(2, self._order + 2, 2):
            possible_tensors = analyse_tensor_dimension(k)
            self._possible_tensors.append(possible_tensors)

        ################################################
        velocities_amount = 1
        grand_total_list = []
        subshells = []

        group = get_group(self._dimensions) # TODO test output
        for shell in range(len(self._shell_list)):
            velocities = self.velocities_for_shell(dimension=self._dimensions, shell=self._shell_list[shell])
            if len(velocities) == 0:
                raise ImpossibleVelocityShellException("No velocity sets found for velocity shell {}".format(shell))

            #getlist of subshells