diff --git a/src/pystencils_autodiff/graph_datahandling.py b/src/pystencils_autodiff/graph_datahandling.py index c7dc917faf51fbf7e4ae71293c61058e5bd9d6a0..7051943bc44df79131c133277fd36fbf6dadafd7 100644 --- a/src/pystencils_autodiff/graph_datahandling.py +++ b/src/pystencils_autodiff/graph_datahandling.py @@ -177,6 +177,8 @@ class GraphDataHandling(pystencils.datahandling.SerialDataHandling): gpu=None, alignment=False, field_type=FieldType.GENERIC): + if layout is None: + layout = self.default_layout rtn = super().add_array(name, values_per_cell, @@ -188,6 +190,22 @@ class GraphDataHandling(pystencils.datahandling.SerialDataHandling): gpu, alignment, field_type) + + # Weird code happening in super class + if not hasattr(values_per_cell, '__len__'): + values_per_cell = (values_per_cell, ) + if len(values_per_cell) == 1 and values_per_cell[0] == 1: + values_per_cell = () + + rtn = self._fields[name] = pystencils.Field.create_generic(name, + self.dim, + dtype, + index_dimensions=len(values_per_cell), + layout=layout, + index_shape=values_per_cell, + field_type=field_type) + rtn.latex_name = latex_name + if cpu: self.call_queue.append(DataTransfer(self._fields[name], DataTransferKind.HOST_ALLOC)) if gpu: diff --git a/src/pystencils_autodiff/walberla.py b/src/pystencils_autodiff/walberla.py index bfab75669beee2c1d404bd163e9103453aec5539..914a65296dfdac9108cbd930a4630503e2fcde07 100644 --- a/src/pystencils_autodiff/walberla.py +++ b/src/pystencils_autodiff/walberla.py @@ -681,12 +681,10 @@ class DefineKernelObjects(JinjaCppFile): def __init__(self, block): self.sweeps = block.atoms(SweepOverAllBlocks) - self.kernels = [k.ast_dict.functor if isinstance(k.ast_dict.functor, SympyAssignment) - else SympyAssignment(k.ast_dict.functor.symbol, k.ast_dict.functor, - is_const=False, use_auto=True) - - - for k in self.sweeps] + self.kernels = sorted({k.ast_dict.functor if isinstance(k.ast_dict.functor, SympyAssignment) + else SympyAssignment(k.ast_dict.functor.symbol, k.ast_dict.functor, + is_const=False, use_auto=True) + for k in self.sweeps}, key=str) ast_dict = {'block': block, 'kernels': self.kernels, } diff --git a/tests/test_walberla.py b/tests/test_walberla.py index 7b7c264b7701103ac32d35ee54a49b066141535f..74e19b759fb02f92b6d6c70799eecb514ec1d11b 100644 --- a/tests/test_walberla.py +++ b/tests/test_walberla.py @@ -11,9 +11,9 @@ import sys from os.path import dirname, expanduser, join import numpy as np +import sympy as sp import pystencils -from lbmpy.creationfunctions import create_lb_collision_rule from pystencils.astnodes import Block, EmptyLine, SympyAssignment from pystencils.data_types import TypedSymbol, create_type from pystencils_autodiff._file_io import write_file @@ -69,6 +69,9 @@ def test_wald_wiesen_simulation(): def test_wald_wiesen_lbm(): + import pytest + pytest.importorskip('lbmpy') + from lbmpy.creationfunctions import create_lb_collision_rule sys.path.append(dirname(__file__)) with ManualCodeGenerationContext() as ctx: from test_graph_datahandling import ldc_setup @@ -94,17 +97,66 @@ def test_wald_wiesen_lbm(): file.write(v) +def test_projection(): + volume = pystencils.fields('volume: float32[3d]') + projections = pystencils.fields('projection: float32[2d]') + spacing, foo = sp.symbols('spacing, foo') + + from pystencils_walberla.special_symbols import aabb_min_vector, dx_vector, global_coord + import pystencils_reco.projection + + from sympy.matrices.dense import matrix_multiply_elementwise + volume.coordinate_transform = lambda x: aabb_min_vector + matrix_multiply_elementwise(x, dx_vector) + projections.set_coordinate_origin_to_field_center() + projections.coordinate_transform *= spacing + + from pystencils.interpolation_astnodes import TextureDeclaration + TextureDeclaration.headers = [] + projection_matrix = pystencils_reco.matrix_symbols('T', pystencils.data_types.create_type('float32'), 3, 4) + from pystencils.kernelparameters import FieldPointerSymbol, FieldStrideSymbol + + assignments = pystencils_reco.projection.forward_projection(volume, projections, projection_matrix) + assignments.main_assignments.append(pystencils.Assignment(foo, FieldPointerSymbol( + volume.name, volume.dtype, const=True) + FieldStrideSymbol(volume.name, 0) + FieldStrideSymbol(volume.name, 1))) + assignments.subs({a: b for a, b in zip(pystencils.x_vector(3), global_coord)}) + assignments.kwargs = {} + print(assignments) + kernel = assignments.compile('gpu') + pystencils.show_code(kernel) + + with ManualCodeGenerationContext() as ctx: + + dh = GraphDataHandling((300, 300, 300)) + volume, projections = dh.add_arrays('volume, projection', gpu=True) + + dh.run_kernel(kernel, simulate_only=True) + + sim = Simulation(dh, ctx, cmake_target_name='projection') + sim._debug = False + sim.write_files() + + dir = '/localhome/seitz_local/projects/walberla/apps/projection/' + os.makedirs(dir, exist_ok=True) + for k, v in ctx.files.items(): + with open(join(dir, k), 'w') as file: + file.write(v) + + def test_global_idx(): - sys.path.append(dirname(__file__)) with ManualCodeGenerationContext() as ctx: - from pystencils_walberla.special_symbols import current_global_idx, aabb_min_x + from pystencils_walberla.special_symbols import aabb_min_vector, global_coord dh = GraphDataHandling((20, 30, 40)) my_array = dh.add_array('my_array') - ast = pystencils.create_kernel([pystencils.Assignment(my_array.center, aabb_min_x + pystencils.x_)]).compile() + ast = pystencils.create_kernel([pystencils.Assignment( + my_array.center, sum(aabb_min_vector))]).compile() dh.run_kernel(ast, simulate_only=True) dh.save_fields('my_array', expanduser('~/foo')) + ast = pystencils.create_kernel([pystencils.Assignment( + my_array.center, sum(global_coord))]).compile() + dh.run_kernel(ast, simulate_only=True) + dh.save_fields('my_array', expanduser('~/foo2')) # ast = pystencils.create_kernel([pystencils.Assignment(my_array.center, sum(current_global_idx))]).compile() # dh.run_kernel(ast, simulate_only=True)