Skip to content
Snippets Groups Projects
Commit f18ff26f authored by Stephan Seitz's avatar Stephan Seitz
Browse files

First autogenerated walberla app that compiles without errors

parent d8c717db
No related branches found
No related tags found
No related merge requests found
Pipeline #21509 failed
......@@ -210,11 +210,11 @@ class JinjaCppFile(Node):
def __str__(self):
assert self.TEMPLATE, f"Template of {self.__class__} must be set"
render_dict = {k: (self._print(v)
if not isinstance(v, (pystencils.Field, pystencils.TypedSymbol)) and v is not None
if not isinstance(v, (pystencils.Field, pystencils.TypedSymbol, bool)) and v is not None
else v)
if not isinstance(v, Iterable) or isinstance(v, str)
else [(self._print(a)
if not isinstance(a, (pystencils.Field, pystencils.TypedSymbol)) and a is not None
if not isinstance(a, (pystencils.Field, pystencils.TypedSymbol, bool)) and a is not None
else a)
for a in v]
for k, v in self.ast_dict.items()}
......
import sympy as sp
import pystencils.backends.cbackend
from pystencils.kernelparameters import FieldPointerSymbol
......@@ -12,14 +14,17 @@ class FrameworkIntegrationPrinter(pystencils.backends.cbackend.CBackend):
"""
def __init__(self):
super().__init__(sympy_printer=None,
dialect='c')
super().__init__(dialect='c')
def _print(self, node):
from pystencils_autodiff.framework_integration.astnodes import JinjaCppFile
if isinstance(node, JinjaCppFile):
node.printer = self
return super()._print(node)
if isinstance(node, sp.Expr):
return self.sympy_printer._print(node)
else:
return super()._print(node)
def _print_WrapperFunction(self, node):
super_result = super()._print_KernelFunction(node)
......
This diff is collapsed.
......@@ -7,22 +7,21 @@
#pragma once
#include "lbm/communication/PdfFieldPackInfo.h"
#include "lbm/field/AddToStorage.h"
#include "lbm/field/PdfField.h"
#include "lbm/gui/Connection.h"
#include "lbm/vtk/VTKOutput.h"
{% for header in headers -%}
#include {{ header }}
{% endfor %}
namespace walberla_user {
using namespace walberla;
using LatticeModel_T = lbm::LbCodeGenerationExample_LatticeModel;
using LatticeModel_T = lbm::{{ lb_model_name }};
using Stencil_T = LatticeModel_T::Stencil;
using CommunicationStencil_T = LatticeModel_T::CommunicationStencil_T;
using lPdfField_T = bm::PdfField<LatticeModel_T>;
using flag_t = walberla::uint8_t;
using FlagField_T FlagField<flag_t>;
using CommunicationStencil_T = LatticeModel_T::CommunicationStencil;
using PdfField_T = lbm::PdfField<LatticeModel_T>;
using flag_t = {{ flag_field_type }};
using FlagField_T = FlagField<flag_t>;
using PdfField_T = lbm::PdfField<LatticeModel_T>;
typedef VectorField_T = GhostLayerField<real_t, LatticeModel_T::Stencil::D>;
typedef ScalarField_T GhostLayerField<real_t, 1>;
using VectorField_T = GhostLayerField<real_t, LatticeModel_T::Stencil::D>;
using ScalarField_T = GhostLayerField<real_t, 1>;
} // namespace walberla_user
#
# Copyright © 2020 Stephan Seitz <stephan.seitz@fau.de>
#
# Distributed under terms of the GPLv3 license.
"""
"""
from typing import Dict
import sympy as sp
from stringcase import pascalcase
import lbmpy_walberla
import pystencils
import pystencils_walberla.codegen
from pystencils.astnodes import Block, EmptyLine
from pystencils_autodiff.walberla import (
AllocateAllFields, DefinitionsHeader, InitBoundaryHandling, LbCommunicationSetup,
ResolveUndefinedSymbols, RunTimeLoop, SweepCreation, SweepOverAllBlocks, TimeLoop,
UniformBlockforestFromConfig, WalberlaMain, WalberlaModule)
class WaldUndWiesenSimulation():
def _get_sweep_class_name(prefix='Kernel'):
ctr = 0
while True:
yield f'{prefix}{ctr}'
ctr += 1
def __init__(self,
graph_data_handling,
codegen_context,
boundary_handling: pystencils.boundaries.BoundaryHandling = None,
lb_rule=None,
refinement_scaling=None):
self._data_handling = graph_data_handling
self._lb_rule = lb_rule
self._refinement_scaling = refinement_scaling
self._block_forest = UniformBlockforestFromConfig()
self.parameter_config_block = 'parameters'
self._codegen_context = codegen_context
self._boundary_handling = boundary_handling
self._lb_model_name = 'GeneratedLatticeModel'
self._flag_field_dtype = 'uint32_t'
self._kernel_class_generator = WaldUndWiesenSimulation._get_sweep_class_name()
self._with_gui = False
self._with_gui_default = False
def _create_helper_files(self) -> Dict[str, str]:
if self._lb_rule:
lbmpy_walberla.generate_lattice_model(self._codegen_context, self._lb_model_name,
self._lb_rule,
refinement_scaling=self._refinement_scaling)
if self._boundary_handling:
for bc in self.boundary_conditions:
lbmpy_walberla.generate_boundary(
self._codegen_context, pascalcase(bc.name), bc, self._lb_rule.method)
def _create_module(self):
if self._lb_rule:
lb_shape = (len(self._lb_rule.method.stencil),)
else:
lb_shape = (-1,)
self._field_allocations = field_allocations = AllocateAllFields(self._block_forest.blocks,
self._data_handling,
lb_shape,
self._lb_model_name)
if self._boundary_handling:
flag_field_id = field_allocations._cpu_allocations[
self._boundary_handling.flag_interface.flag_field_name].symbol
if self._lb_rule:
pdf_field_id = field_allocations._gpu_allocations.get(
'ldc_pdfSrc', field_allocations._cpu_allocations['ldc_pdfSrc']).symbol
else:
pdf_field_id = None
call_nodes = filter(lambda x: x, [self._graph_to_sweep(c) for c in self._data_handling.call_queue])
module = WalberlaModule(WalberlaMain(Block([
self._block_forest,
ResolveUndefinedSymbols(
Block([
field_allocations,
InitBoundaryHandling(self._block_forest.blocks, flag_field_id,
pdf_field_id, self.boundary_conditions)
if self._boundary_handling else EmptyLine(),
LbCommunicationSetup(self._lb_model_name,
pdf_field_id)
if self._lb_rule else EmptyLine(),
*call_nodes
]), self.parameter_config_block)
])))
self._codegen_context.write_file("main.cpp", str(module))
return module
def _create_defintions_header(self):
self._codegen_context.write_file("UserDefinitions.h",
str(DefinitionsHeader(self._lb_model_name, self._flag_field_dtype)))
def write_files(self):
self._create_helper_files()
self._create_module()
self._create_defintions_header()
@property
def boundary_conditions(self):
return self._boundary_handling._boundary_object_to_boundary_info.keys()
def _graph_to_sweep(self, c):
from pystencils_autodiff.graph_datahandling import KernelCall, TimeloopRun
if isinstance(c, KernelCall):
sweep_class_name = next(self._kernel_class_generator)
pystencils_walberla.codegen.generate_sweep(
self._codegen_context, sweep_class_name, c.kernel.ast)
rtn = SweepOverAllBlocks(SweepCreation(sweep_class_name, self._field_allocations,
c.kernel.ast), self._block_forest.blocks)
elif isinstance(c, TimeloopRun):
sweeps = []
for a in c.timeloop._single_step_asts:
if 'indexField' in [f.name for f in a.fields_accessed]:
continue
sweep_class_name = next(self._kernel_class_generator)
pystencils_walberla.codegen.generate_sweep(
self._codegen_context, sweep_class_name, a)
sweeps.append(SweepCreation(sweep_class_name, self._field_allocations, a))
loop = TimeLoop(self._block_forest.blocks, [], sweeps, [], sp.S(c.time_steps))
rtn = Block([loop, RunTimeLoop(self._block_forest.blocks, loop, self._with_gui, self._with_gui_default)])
else:
return None
return rtn
......@@ -6,16 +6,24 @@
"""
"""
import os
import sys
from os.path import dirname, join
import numpy as np
import sympy as sp
import lbmpy_walberla
import pystencils
from lbmpy.creationfunctions import create_lb_collision_rule, create_lbm_kernel
from pystencils.astnodes import Block, EmptyLine, SympyAssignment
from pystencils.data_types import TypedSymbol
from pystencils.data_types import TypedSymbol, create_type
from pystencils_autodiff._file_io import write_file
from pystencils_autodiff.graph_datahandling import GraphDataHandling
from pystencils_autodiff.walberla import (
DefinitionsHeader, FieldAllocation, GetParameter, PdfFieldAllocation,FlagFieldAllocation,
UniformBlockForrestFromConfig, WalberlaMain, WalberlaModule)
DefinitionsHeader, FieldAllocation, FlagFieldAllocation, GetParameter, PdfFieldAllocation,
ResolveUndefinedSymbols, UniformBlockforestFromConfig, WalberlaMain, WalberlaModule)
from pystencils_autodiff.wald_und_wiesen_simulation import WaldUndWiesenSimulation
from pystencils_walberla.cmake_integration import ManualCodeGenerationContext
def test_walberla():
......@@ -28,18 +36,18 @@ def test_walberla():
number_symbol = TypedSymbol('number', np.float32)
crazy_plus_one = TypedSymbol('crazy', np.float32)
block_forrest = UniformBlockForrestFromConfig()
block_forest = UniformBlockforestFromConfig()
block = Block([
block_forrest,
block_forest,
SympyAssignment(foo_symbol, GetParameter('parameters', foo_symbol)),
SympyAssignment(number_symbol, GetParameter('parameters', number_symbol, 1.2)),
SympyAssignment(crazy_plus_one, number_symbol + 1),
EmptyLine(),
FieldAllocation(block_forrest.blocks, x),
PdfFieldAllocation(block_forrest.blocks, pdf, 'LbModel_T'),
PdfFieldAllocation(block_forrest.blocks, pdf2, 'LbModel_T', [0, 0, 0], 1),
FlagFieldAllocation(block_forrest.blocks, flags)
FieldAllocation(block_forest.blocks, x, on_gpu=False),
PdfFieldAllocation(block_forest.blocks, pdf, 'LbModel_T', on_gpu=True),
PdfFieldAllocation(block_forest.blocks, pdf2, 'LbModel_T', [0, 0, 0], 1, on_gpu=True),
FlagFieldAllocation(block_forest.blocks, flags)
])
module = WalberlaModule(WalberlaMain(block))
......@@ -48,5 +56,56 @@ def test_walberla():
write_file('/localhome/seitz_local/projects/walberla/apps/autogen/main.cpp', code)
definitions = DefinitionsHeader(module)
definitions = DefinitionsHeader(module, 'uint8_t')
write_file('/localhome/seitz_local/projects/walberla/apps/autogen/UserDefinitions.h', str(definitions))
def test_wald_wiesen_simulation():
with ManualCodeGenerationContext() as ctx:
dh = GraphDataHandling((30, 30),
periodicity=False,
default_ghost_layers=1,
default_target='cpu')
dh.add_arrays('x, y')
dh.add_arrays('w, z', gpu=True)
sim = WaldUndWiesenSimulation(dh, ctx)
print(sim._create_module())
def test_wald_wiesen_lbm():
sys.path.append(dirname(__file__))
with ManualCodeGenerationContext() as ctx:
from test_graph_datahandling import ldc_setup
opt_params = {'target': 'gpu'}
lbm_step = ldc_setup(domain_size=(30, 30), optimization=opt_params, fixed_loop_sizes=False)
sim = WaldUndWiesenSimulation(lbm_step.data_handling,
ctx,
lbm_step.boundary_handling,
create_lb_collision_rule(lbm_step.method, optimization=opt_params))
sim.write_files()
dir = '/localhome/seitz_local/projects/walberla/apps/autogen/'
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_resolve_parameters():
sym = TypedSymbol('s', create_type('double'))
sym2 = TypedSymbol('t', create_type('double'))
block_forest = UniformBlockforestFromConfig()
module = WalberlaModule(WalberlaMain(Block([
block_forest,
ResolveUndefinedSymbols(
Block([
SympyAssignment(sym, 1 + sym2),
]), 'parameters')
])))
print(module)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment