FlowAroundSphereCodeGen.py 4.54 KB
Newer Older
Markus Holzer's avatar
Markus Holzer committed
1
from pystencils import Target
2
from pystencils.field import fields
Markus Holzer's avatar
Markus Holzer committed
3
from lbmpy import LBMConfig, LBMOptimisation, LBStencil, Method, Stencil
4

5
from lbmpy.advanced_streaming.utility import get_timesteps
6
from lbmpy.macroscopic_value_kernels import macroscopic_values_setter
7
from lbmpy.creationfunctions import create_lb_collision_rule
8
9
10
11
12
from lbmpy.boundaries import NoSlip, UBB, ExtrapolationOutflow

from pystencils_walberla import CodeGeneration, generate_sweep, generate_info_header

from lbmpy_walberla.additional_data_handler import UBBAdditionalDataHandler, OutflowAdditionalDataHandler
Markus Holzer's avatar
Markus Holzer committed
13
from lbmpy_walberla import generate_lb_pack_info
14
15
16
17
18
from lbmpy_walberla import generate_alternating_lbm_sweep, generate_alternating_lbm_boundary

import sympy as sp

with CodeGeneration() as ctx:
19
    data_type = "float64" if ctx.double_accuracy else "float32"
Markus Holzer's avatar
Markus Holzer committed
20
21
22
    stencil = LBStencil(Stencil.D3Q27)
    q = stencil.Q
    dim = stencil.D
23
24
25
26
27
28
29
30
31
32
33
34
35
    streaming_pattern = 'esotwist'
    timesteps = get_timesteps(streaming_pattern)

    pdfs, velocity_field, density_field = fields(f"pdfs({q}), velocity({dim}), density(1) : {data_type}[{dim}D]",
                                                 layout='fzyx')
    omega = sp.Symbol("omega")
    u_max = sp.Symbol("u_max")

    output = {
        'density': density_field,
        'velocity': velocity_field
    }

Markus Holzer's avatar
Markus Holzer committed
36
37
    lbm_config = LBMConfig(stencil=stencil, method=Method.CUMULANT, compressible=True,
                           relaxation_rate=omega, galilean_correction=True,
Markus Holzer's avatar
Markus Holzer committed
38
39
40
41
42
                           field_name='pdfs', streaming_pattern=streaming_pattern, output=output)

    lbm_optimisation = LBMOptimisation(symbolic_field=pdfs, cse_global=False, cse_pdfs=False)

    collision_rule = create_lb_collision_rule(lbm_config=lbm_config, lbm_optimisation=lbm_optimisation)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    lb_method = collision_rule.method

    # getter & setter
    setter_assignments = macroscopic_values_setter(lb_method, velocity=velocity_field.center_vector,
                                                   pdfs=pdfs, density=1.0,
                                                   streaming_pattern=streaming_pattern,
                                                   previous_timestep=timesteps[0])

    # opt = {'instruction_set': 'sse', 'assume_aligned': True, 'nontemporal': False, 'assume_inner_stride_one': True}

    stencil_typedefs = {'Stencil_T': stencil}
    field_typedefs = {'PdfField_T': pdfs,
                      'VelocityField_T': velocity_field,
                      'ScalarField_T': density_field}

58
    if ctx.cuda:
Markus Holzer's avatar
Markus Holzer committed
59
        target = Target.GPU
60
    else:
Markus Holzer's avatar
Markus Holzer committed
61
        target = Target.CPU
62
63
64

    # sweeps
    generate_alternating_lbm_sweep(ctx, 'FlowAroundSphereCodeGen_LbSweep',
Markus Holzer's avatar
Markus Holzer committed
65
66
                                   collision_rule, lbm_config=lbm_config, lbm_optimisation=lbm_optimisation,
                                   target=target)
67
68
69
    generate_sweep(ctx, 'FlowAroundSphereCodeGen_MacroSetter', setter_assignments, target=target)

    # boundaries
Markus Holzer's avatar
Markus Holzer committed
70
    ubb = UBB(lambda *args: None, dim=dim, data_type=data_type)
71
    ubb_data_handler = UBBAdditionalDataHandler(stencil, ubb)
Markus Holzer's avatar
Markus Holzer committed
72
    outflow = ExtrapolationOutflow(stencil[4], lb_method, streaming_pattern=streaming_pattern, data_type=data_type)
73
74
75
76
    outflow_data_handler = OutflowAdditionalDataHandler(stencil, outflow, target=target)

    generate_alternating_lbm_boundary(ctx, 'FlowAroundSphereCodeGen_UBB', ubb, lb_method,
                                      target=target, streaming_pattern=streaming_pattern,
77
78
                                      additional_data_handler=ubb_data_handler,
                                      layout='fzyx')
79
80

    generate_alternating_lbm_boundary(ctx, 'FlowAroundSphereCodeGen_NoSlip', NoSlip(), lb_method,
81
82
                                      target=target, streaming_pattern=streaming_pattern,
                                      layout='fzyx')
83
84
85

    generate_alternating_lbm_boundary(ctx, 'FlowAroundSphereCodeGen_Outflow', outflow, lb_method,
                                      target=target, streaming_pattern=streaming_pattern,
86
87
                                      additional_data_handler=outflow_data_handler,
                                      layout='fzyx')
88
89
90
91
92
93
94
95

    # communication
    generate_lb_pack_info(ctx, 'FlowAroundSphereCodeGen_PackInfo', stencil, pdfs,
                          streaming_pattern=streaming_pattern, always_generate_separate_classes=True, target=target)

    # Info header containing correct template definitions for stencil and field
    generate_info_header(ctx, 'FlowAroundSphereCodeGen_InfoHeader',
                         stencil_typedefs=stencil_typedefs, field_typedefs=field_typedefs)