Commit 28a9823b authored by Frederik Hennig's avatar Frederik Hennig
Browse files

refactored kernel_type to streaming_pattern, + code style fixes

parent a943e632
Pipeline #27058 failed with stage
in 19 minutes and 7 seconds
......@@ -10,4 +10,5 @@ __pycache__
_build
/.idea
.cache
_local_tmp
\ No newline at end of file
_local_tmp
**/.vscode
\ No newline at end of file
......@@ -11,11 +11,11 @@ class FlexibleLBMBoundaryHandling(BoundaryHandling):
object and the right one selected depending on the time step.
"""
def __init__(self, lb_method, data_handling, pdf_field_name, kernel_type='pull',
def __init__(self, lb_method, data_handling, pdf_field_name, streaming_pattern='pull',
name="boundary_handling", flag_interface=None, target='cpu', openmp=True):
self._lb_method = lb_method
self._kernel_type = kernel_type
self._two_fields_kernel = kernel_type in ['pull', 'push']
self._streaming_pattern = streaming_pattern
self._two_fields_kernel = streaming_pattern in ['pull', 'push']
self._between_timesteps = None
super(FlexibleLBMBoundaryHandling, self).__init__(data_handling, pdf_field_name, lb_method.stencil,
name, flag_interface, target, openmp)
......@@ -62,7 +62,7 @@ class FlexibleLBMBoundaryHandling(BoundaryHandling):
def _create_boundary_kernel(self, symbolic_field, symbolic_index_field, boundary_obj, between_timesteps='both'):
return create_advanced_streaming_boundary_kernel(
symbolic_field, symbolic_index_field, self._lb_method, boundary_obj,
between_timesteps=between_timesteps, kernel_type=self._kernel_type,
between_timesteps=between_timesteps, streaming_pattern=self._streaming_pattern,
target=self._target, openmp=self._openmp)
class FlexibleBoundaryInfo(object):
......@@ -93,12 +93,12 @@ class FlexibleLBMBoundaryHandling(BoundaryHandling):
def create_advanced_streaming_boundary_kernel(pdf_field, index_field, lb_method, boundary_functor,
between_timesteps='both', kernel_type='pull',
between_timesteps='both', streaming_pattern='pull',
target='cpu', openmp=True):
index_dtype = index_field.dtype.numpy_dtype.fields['dir'][0]
offsets_dtype = index_field.dtype.numpy_dtype.fields['x'][0]
indexing = AdvancedStreamingBoundaryIndexing(
pdf_field, lb_method.stencil, between_timesteps, kernel_type, index_dtype, offsets_dtype)
pdf_field, lb_method.stencil, between_timesteps, streaming_pattern, index_dtype, offsets_dtype)
f_out, f_in = indexing.proxy_fields
dir_symbol = indexing.dir_symbol
......
......@@ -5,13 +5,6 @@ import pystencils as ps
from pystencils.data_types import TypedSymbol, create_type
from pystencils.backends.cbackend import CustomCodeNode
from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor, \
StreamPushTwoFieldsAccessor, \
AAEvenTimeStepAccessor, \
AAOddTimeStepAccessor, \
EsoTwistEvenTimeStepAccessor, \
EsoTwistOddTimeStepAccessor
from lbmpy.advanced_streaming.utility import get_accessor, inverse_dir_index
from itertools import product
......@@ -39,7 +32,7 @@ class AdvancedStreamingBoundaryIndexing:
# Constructor and State
# =============================
def __init__(self, pdf_field, stencil, between_timesteps='both', kernel_type='pull',
def __init__(self, pdf_field, stencil, between_timesteps='both', streaming_pattern='pull',
index_dtype=np.int32, offsets_dtype=np.int32):
if between_timesteps not in ['both', 'odd_to_even', 'even_to_odd']:
raise ValueError(
......@@ -47,13 +40,14 @@ class AdvancedStreamingBoundaryIndexing:
+ " Must be one of 'both', 'odd_to_even' or 'even_to_odd'.",
between_timesteps)
if between_timesteps == 'both' and kernel_type in ['aa', 'esotwist']:
raise ValueError('Cannot create index arrays for both kinds of timesteps for kernel type ' + kernel_type)
if between_timesteps == 'both' and streaming_pattern in ['aa', 'esotwist']:
raise ValueError('Cannot create index arrays for both kinds of timesteps for kernel type '
+ streaming_pattern)
odd_to_even = (between_timesteps == 'odd_to_even')
even_accessor = get_accessor(kernel_type, 'even')
odd_accessor = get_accessor(kernel_type, 'odd')
even_accessor = get_accessor(streaming_pattern, 'even')
odd_accessor = get_accessor(streaming_pattern, 'odd')
if between_timesteps == 'both':
assert even_accessor == odd_accessor
......
import numpy as np
import pystencils as ps
from pystencils.slicing import make_slice, shift_slice, get_slice_before_ghost_layer
from lbmpy.stencils import get_stencil
from pystencils.slicing import shift_slice, get_slice_before_ghost_layer
from lbmpy.advanced_streaming.utility import get_accessor
......@@ -35,7 +30,7 @@ def _get_neighbour_transform(direction, ghost_layers):
return tuple(d * (ghost_layers + 1) for d in direction)
def communication_slices(lb_method, kernel_type='pull', after_timestep='both', ghost_layers=1):
def communication_slices(lb_method, streaming_pattern='pull', after_timestep='both', ghost_layers=1):
"""
Return the source and destination slices for periodicity handling or communication between
blocks. This function returns a dictionary which contains a list of tuples
......@@ -44,13 +39,13 @@ def communication_slices(lb_method, kernel_type='pull', after_timestep='both', g
adjacent block.
:param lb_method: The lattice boltzmann method to be used.
:param kernel_type: The streaming pattern.
:param streaming_pattern: The streaming pattern.
:param after_timestep: Timestep after which communication is run; either 'even', 'odd' or 'both'.
:param ghost_layers: Number of ghost layers in each direction.
"""
stencil = lb_method.stencil
write_accesses = get_accessor(kernel_type, after_timestep).write()
write_accesses = get_accessor(streaming_pattern, after_timestep).write()
all_slices = dict()
for comm_dir in stencil:
......@@ -71,4 +66,10 @@ def communication_slices(lb_method, kernel_type='pull', after_timestep='both', g
slices_for_dir.add((write_index, src_slice, dst_slice))
all_slices[comm_dir] = slices_for_dir
return all_slices
\ No newline at end of file
return all_slices
class PeriodicityHandling:
def __init__(self, lb_method, streaming_pattern, data_handling, pdf_field_name, periodicities):
pass
......@@ -7,8 +7,7 @@ from lbmpy.fieldaccess import PdfFieldAccessor, \
EsoTwistOddTimeStepAccessor
supported_kernels = ['push', 'pull', 'aa', 'esotwist']
streaming_patterns = ['push', 'pull', 'aa', 'esotwist']
timesteps = ['even', 'odd', 'both']
even_accessors = {
......@@ -25,18 +24,20 @@ odd_accessors = {
'esotwist': EsoTwistOddTimeStepAccessor
}
def get_accessor(kernel_type, timestep) -> PdfFieldAccessor:
if kernel_type not in supported_kernels:
def get_accessor(streaming_pattern, timestep) -> PdfFieldAccessor:
if streaming_pattern not in streaming_patterns:
raise ValueError(
"Invalid value of parameter 'kernel_type'.", kernel_type)
"Invalid value of parameter 'streaming_pattern'.", streaming_pattern)
if timestep not in timesteps:
raise ValueError("Invalid time step:", timestep)
if timestep == 'even':
return even_accessors[kernel_type]
return even_accessors[streaming_pattern]
else:
return odd_accessors[kernel_type]
return odd_accessors[streaming_pattern]
def inverse_dir_index(stencil, dir):
return stencil.index(tuple(-d for d in stencil[dir]))
......@@ -46,21 +47,23 @@ def inverse_dir_index(stencil, dir):
Allows to access values from a PDF array correctly depending on
the streaming pattern.
"""
class AccessPdfValues:
def __init__(self, pdf_field, stencil, kernel_type='pull', timestep='both', accessor=None):
def __init__(self, pdf_field, stencil, streaming_pattern='pull', timestep='both', accessor=None):
if accessor is None:
accessor = get_accessor(kernel_type, timestep)
accessor = get_accessor(streaming_pattern, timestep)
self.read_accs = accessor.read(pdf_field, stencil)
self.write_accs = accessor.write(pdf_field, stencil)
def write_pdf(self, pdf_arr, pos, d, value):
offsets = self.write_accs[d].offsets
pos = tuple( p + o for p, o in zip(pos, offsets) )
pos = tuple(p + o for p, o in zip(pos, offsets))
i = self.write_accs[d].index[0]
pdf_arr[pos + (i,)] = value
def read_pdf(self, pdf_arr, pos, d):
offsets = self.read_accs[d].offsets
pos = tuple( p + o for p, o in zip(pos, offsets) )
pos = tuple(p + o for p, o in zip(pos, offsets))
i = self.read_accs[d].index[0]
return pdf_arr[pos + (i,)]
......@@ -4,7 +4,6 @@ from copy import deepcopy
from lbmpy.simplificationfactory import create_simplification_strategy
from pystencils.field import Field, get_layout_of_array
from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor
from lbmpy.advanced_streaming.utility import get_accessor
......@@ -35,15 +34,15 @@ macroscopic_values_setter = pdf_initialization_assignments
def flexible_macroscopic_values_setter(lb_method, density, velocity,
pdf_field, kernel_type='pull', previous_timestep='both'):
previous_step_accessor = get_accessor(kernel_type, previous_timestep)
pdf_field, streaming_pattern='pull', previous_timestep='both'):
previous_step_accessor = get_accessor(streaming_pattern, previous_timestep)
write_accesses = previous_step_accessor.write(pdf_field, lb_method.stencil)
return pdf_initialization_assignments(lb_method, density, velocity, write_accesses)
def flexible_macroscopic_values_getter(lb_method, density, velocity,
pdf_field, kernel_type='pull', previous_timestep='both'):
previous_step_accessor = get_accessor(kernel_type, previous_timestep)
pdf_field, streaming_pattern='pull', previous_timestep='both'):
previous_step_accessor = get_accessor(streaming_pattern, previous_timestep)
write_accesses = previous_step_accessor.write(pdf_field, lb_method.stencil)
return macroscopic_values_getter(lb_method, density, velocity, write_accesses)
......@@ -51,7 +50,7 @@ def flexible_macroscopic_values_getter(lb_method, density, velocity,
def compile_macroscopic_values_getter(lb_method, output_quantities, pdf_arr=None,
ghost_layers=1, iteration_slice=None,
field_layout='numpy', target='cpu',
kernel_type='pull', previous_timestep='both'):
streaming_pattern='pull', previous_timestep='both'):
"""
Create kernel to compute macroscopic value(s) from a pdf field (e.g. density or velocity)
......@@ -110,10 +109,7 @@ def compile_macroscopic_values_getter(lb_method, output_quantities, pdf_arr=None
output_mapping[output_quantity] = output_mapping[output_quantity][0]
stencil = lb_method.stencil
# pdf_symbols = [pdf_field(i) for i in range(len(stencil))]
# Extension for any kind of streaming rule
previous_step_accessor = get_accessor(kernel_type, previous_timestep)
previous_step_accessor = get_accessor(streaming_pattern, previous_timestep)
pdf_symbols = previous_step_accessor.write(pdf_field, stencil)
eqs = cqc.output_equations_from_pdfs(pdf_symbols, output_mapping).all_assignments
......@@ -144,7 +140,7 @@ def compile_macroscopic_values_getter(lb_method, output_quantities, pdf_arr=None
def compile_macroscopic_values_setter(lb_method, quantities_to_set, pdf_arr=None,
ghost_layers=1, iteration_slice=None,
field_layout='numpy', target='cpu',
kernel_type='pull', previous_timestep='both'):
streaming_pattern='pull', previous_timestep='both'):
"""
Creates a function that sets a pdf field to specified macroscopic quantities
The returned function can be called with the pdf field to set as single argument
......@@ -197,8 +193,7 @@ def compile_macroscopic_values_setter(lb_method, quantities_to_set, pdf_arr=None
else:
eq = eq.new_without_subexpressions()
# Extension for any streaming rule
previous_step_accessor = get_accessor(kernel_type, previous_timestep)
previous_step_accessor = get_accessor(streaming_pattern, previous_timestep)
write_accesses = previous_step_accessor.write(pdf_field, lb_method.stencil)
substitutions = {sym: write_accesses[i] for i, sym in enumerate(lb_method.post_collision_pdf_symbols)}
......
......@@ -9,7 +9,7 @@ from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor, \
EsoTwistOddTimeStepAccessor
from lbmpy.advanced_streaming import FlexibleNoSlip, create_advanced_streaming_boundary_kernel
from lbmpy.advanced_streaming.utility import even_accessors, odd_accessors, supported_kernels, inverse_dir_index, AccessPdfValues
from lbmpy.advanced_streaming.utility import even_accessors, odd_accessors, streaming_patterns, inverse_dir_index, AccessPdfValues
from lbmpy.creationfunctions import create_lb_method
from lbmpy.stencils import get_stencil
......@@ -24,9 +24,9 @@ from itertools import product
import pytest
@pytest.mark.parametrize("stencil", [ 'D2Q9', 'D3Q19', 'D3Q27'])
@pytest.mark.parametrize("kernel_type", supported_kernels)
@pytest.mark.parametrize("streaming_pattern", streaming_patterns)
@pytest.mark.parametrize("timestep_type", ['odd_to_even', 'even_to_odd'])
def test_flexible_noslip_single_cell(stencil, kernel_type, timestep_type):
def test_flexible_noslip_single_cell(stencil, streaming_pattern, timestep_type):
"""
Flexible NoSlip Test
"""
......@@ -36,8 +36,8 @@ def test_flexible_noslip_single_cell(stencil, kernel_type, timestep_type):
dim = len(stencil[0])
pdf_field = ps.fields(f'pdfs({q}): [{dim}D]')
even_accessor = even_accessors[kernel_type]
odd_accessor = odd_accessors[kernel_type]
even_accessor = even_accessors[streaming_pattern]
odd_accessor = odd_accessors[streaming_pattern]
prev_accessor = (odd_accessor if timestep_type == 'odd_to_even' else even_accessor)
next_accessor = (even_accessor if timestep_type == 'odd_to_even' else odd_accessor)
......@@ -62,7 +62,7 @@ def test_flexible_noslip_single_cell(stencil, kernel_type, timestep_type):
flex_ast = create_advanced_streaming_boundary_kernel(pdf_field,
index_field, lb_method, flex_noslip,
between_timesteps=timestep_type,
kernel_type=kernel_type)
streaming_pattern=streaming_pattern)
flex_kernel = flex_ast.compile()
......
......@@ -3,16 +3,16 @@ import numpy as np
from lbmpy.creationfunctions import create_lb_method
from lbmpy.macroscopic_value_kernels import (
compile_macroscopic_values_getter, compile_macroscopic_values_setter)
from lbmpy.advanced_streaming.utility import supported_kernels
from lbmpy.advanced_streaming.utility import streaming_patterns
import pytest
@pytest.mark.parametrize('stencil', ['D2Q9', 'D3Q19'])
@pytest.mark.parametrize('force_model', ['guo', 'luo', None])
@pytest.mark.parametrize('compressible', ['compressible', False])
@pytest.mark.parametrize('kernel_type', supported_kernels)
@pytest.mark.parametrize('streaming_pattern', streaming_patterns)
@pytest.mark.parametrize('prev_timestep', ['even', 'odd'])
def test_set_get_density_velocity_with_fields(stencil, force_model, compressible, kernel_type, prev_timestep):
def test_set_get_density_velocity_with_fields(stencil, force_model, compressible, streaming_pattern, prev_timestep):
force = (0.1, 0.12, -0.17)
method = create_lb_method(stencil=stencil, force_model=force_model, method='trt',
compressible=compressible, force=force)
......@@ -31,11 +31,11 @@ def test_set_get_density_velocity_with_fields(stencil, force_model, compressible
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, kernel_type=kernel_type, previous_timestep=prev_timestep)
ghost_layers=ghost_layers, streaming_pattern=streaming_pattern, previous_timestep=prev_timestep)
setter(pdf_arr)
getter = compile_macroscopic_values_getter(method, ['density', 'velocity'], pdf_arr=pdf_arr,
ghost_layers=ghost_layers, kernel_type=kernel_type, previous_timestep=prev_timestep)
ghost_layers=ghost_layers, streaming_pattern=streaming_pattern, previous_timestep=prev_timestep)
density_output_field = np.zeros_like(density_input_field)
velocity_output_field = np.zeros_like(velocity_input_field)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment