test_macroscopic_value_kernels.py 3.96 KB
Newer Older
1
import numpy as np
Martin Bauer's avatar
Martin Bauer committed
2

3
from lbmpy.creationfunctions import create_lb_method
Martin Bauer's avatar
Martin Bauer committed
4
5
from lbmpy.macroscopic_value_kernels import (
    compile_macroscopic_values_getter, compile_macroscopic_values_setter)
6
7
8
9
10
11
from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor, \
    StreamPushTwoFieldsAccessor, \
    AAEvenTimeStepAccessor, \
    AAOddTimeStepAccessor, \
    EsoTwistEvenTimeStepAccessor, \
    EsoTwistOddTimeStepAccessor
12

13
14
import pytest
from itertools import product
15

16
17
18
19
20
21
22
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
58
accessors = [StreamPullTwoFieldsAccessor,
             StreamPushTwoFieldsAccessor,
             AAEvenTimeStepAccessor,
             AAOddTimeStepAccessor,
             EsoTwistEvenTimeStepAccessor,
             EsoTwistOddTimeStepAccessor]

stencils = ['D2Q9', 'D3Q19']
force_models = ['guo', 'luo', 'none']
compressibilities = [True, False]


@pytest.mark.parametrize('stencil,force_model,compressible,accessor', product(stencils, force_models, compressibilities, accessors))
def test_set_get_density_velocity_with_fields(stencil, force_model, compressible, accessor):
    force = (0.1, 0.12, -0.17)
    method = create_lb_method(stencil=stencil, force_model=force_model, method='trt',
                              compressible=compressible, force=force)
    ghost_layers = 1
    inner_size = (3, 7, 4)[:method.dim]
    total_size = tuple(s + 2 * ghost_layers for s in inner_size)
    pdf_arr = np.zeros(total_size + (len(method.stencil),))

    inner_slice = (slice(ghost_layers, -ghost_layers, 1), ) * method.dim
    density_input_field = np.zeros(total_size)
    velocity_input_field = np.zeros(total_size + (method.dim, ))
    density_input_field[inner_slice] = 1 + 0.2 * (np.random.random_sample(inner_size) - 0.5)
    velocity_input_field[inner_slice] = 0.1 * \
        (np.random.random_sample(inner_size + (method.dim, )) - 0.5)

    quantities_to_set = {'density': density_input_field, 'velocity': velocity_input_field}

    setter = compile_macroscopic_values_setter(method, pdf_arr=pdf_arr, quantities_to_set=quantities_to_set,
                                               ghost_layers=ghost_layers, previous_step_accessor=accessor)
    setter(pdf_arr)

    getter = compile_macroscopic_values_getter(method, ['density', 'velocity'], pdf_arr=pdf_arr, 
                                               ghost_layers=ghost_layers, previous_step_accessor=accessor)

    density_output_field = np.zeros_like(density_input_field)
    velocity_output_field = np.zeros_like(velocity_input_field)
    getter(pdfs=pdf_arr, density=density_output_field, velocity=velocity_output_field)
    np.testing.assert_almost_equal(density_input_field, density_output_field)
    np.testing.assert_almost_equal(velocity_input_field, velocity_output_field)
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


def test_set_get_constant_velocity():
    for stencil in ['D2Q9', 'D3Q19']:
        for force_model in ['guo', 'luo', 'none']:
            for compressible in [True, False]:
                ref_velocity = [0.01, -0.2, 0.1]

                force = (0.1, 0.12, -0.17)
                method = create_lb_method(stencil=stencil, force_model=force_model, method='trt',
                                          compressible=compressible, force=force)
                size = (1, 1, 1)[:method.dim]
                pdf_arr = np.zeros(size + (len(method.stencil),))
                setter = compile_macroscopic_values_setter(method, pdf_arr=pdf_arr,
                                                           quantities_to_set={'velocity': ref_velocity[:method.dim]}, )
                setter(pdf_arr)

                getter = compile_macroscopic_values_getter(method, ['velocity'], pdf_arr=pdf_arr)
                velocity_output_field = np.zeros(size + (method.dim, ))
                getter(pdfs=pdf_arr, velocity=velocity_output_field)
                if method.dim == 2:
                    computed_velocity = velocity_output_field[0, 0, :]
                else:
                    computed_velocity = velocity_output_field[0, 0, 0, :]

                np.testing.assert_almost_equal(np.array(ref_velocity[:method.dim]), computed_velocity)