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


MischaD's avatar
MischaD committed
8
class Lattice:
MischaD's avatar
MischaD committed
9
10
11
12
13
14
15
16
17
18
19
20
    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
21
22
        self._tensor_space_dimension = 0
        self._possible_tensors = []
MischaD's avatar
MischaD committed
23
24
        self._velocities = 0
        self._shells = 0
MischaD's avatar
MischaD committed
25

MischaD's avatar
MischaD committed
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
58
59
60
    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
61

MischaD's avatar
MischaD committed
62
63
64
65
    @property
    def tensor_space_dimension(self):
        return np.array([len(x) for x in self._possible_tensors]).sum()

MischaD's avatar
MischaD committed
66
67
68
69
70
71
72
73
    @property
    def shells(self):
        self._shells

    @property
    def velocities(self):
        self._velocities

MischaD's avatar
MischaD committed
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
116
117
    @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 = []
MischaD's avatar
MischaD committed
118
        total_subshells = []
MischaD's avatar
MischaD committed
119
120
121
122
123

        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:
MischaD's avatar
MischaD committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                raise ImpossibleVelocityShellException("No velocity sets found for velocity shell {}.".format(shell))

            subshells = get_subshells(velocities, group)
            total_subshells.append(len(subshells))
            if len(subshells) > 1:
                # TODO l.119
                raise ReducibleShellException("Shell {} is reducible")
            grand_total_list.extend(subshells)
            velocities_amount += len(velocities)

        self._velocities = velocities_amount
        self._shells = len(total_subshells) + 1
        logger.info("Velocities: {} Shells: {}".format(self._velocities, self._shells))

        logger.info("Non zero shells: ")
        for i, shell in enumerate(grand_total_list):
            number_of_velocities = len(shell)
            type = tuple(np.sort(abs(shell[0])))
            logger.info("Shell number {} with c_i^2 = {} and {} velocities of type {}".format(i+1, (shell[0]**2).sum(), number_of_velocities, type))

        random.seed(self._seed)
MischaD's avatar
MischaD committed
145