Skip to content
Snippets Groups Projects
Commit 5ef2b1de authored by René Heß's avatar René Heß
Browse files

[skip ci] Big code cleanup and bugfix

parent 5f47072d
No related branches found
No related tags found
No related merge requests found
...@@ -16,8 +16,8 @@ from dune.codegen.pdelab.driver.gridfunctionspace import (main_type_trial_gfs, ...@@ -16,8 +16,8 @@ from dune.codegen.pdelab.driver.gridfunctionspace import (main_type_trial_gfs,
main_type_subgfs, main_type_subgfs,
) )
from dune.codegen.pdelab.driver.interpolate import (interpolate_vector, from dune.codegen.pdelab.driver.interpolate import (interpolate_vector,
main_name_boundary_grid_function, main_name_grid_function,
main_type_boundary_grid_function, main_type_grid_function,
) )
from dune.codegen.pdelab.driver.solve import (define_vector, from dune.codegen.pdelab.driver.solve import (define_vector,
dune_solve, dune_solve,
...@@ -38,46 +38,6 @@ def name_test_fail_variable(): ...@@ -38,46 +38,6 @@ def name_test_fail_variable():
return name return name
@preamble(section="error", kernel="main")
def main_typedef_exact_solution_grid_function(name, treepath):
# palpo TODO
solution_type = main_type_boundary_grid_function()
# solution_type = "BoundaryGridFunction"
if len(treepath) > 0:
solution_type = "Dune::TypeTree::Child<{}, {}>;".format(solution_type, ", ".join(str(t) for t in treepath))
return "using {} = {};".format(name, solution_type)
def main_type_exact_solution_grid_function(treepath):
name = "ExactSolution"
if len(treepath) > 0:
name = "{}_{}".format(name, "_".join(str(t) for t in treepath))
main_typedef_exact_solution_grid_function(name, treepath)
return name
@preamble(section="error", kernel="main")
def main_define_exact_solution_grid_function(name, treepath):
element = get_trial_element()
func = preprocess_leaf_data(element, "exact_solution")
# palpo TODO
# boundary_gf = "boundaryGridFunction"
boundary_gf = main_name_boundary_grid_function(element, func)
if len(treepath) == 0:
return "auto {} = *{};".format(name, boundary_gf)
else:
indices = ["Dune::Indices::_{}".format(str(t)) for t in treepath]
return "auto {} = child(*{}, {});".format(name, boundary_gf, ", ".join(i for i in indices))
def main_name_exact_solution_grid_function(treepath):
name = "exactSolution"
if len(treepath) > 0:
name = "{}_{}".format(name, "_".join(str(t) for t in treepath))
main_define_exact_solution_grid_function(name, treepath)
return name
def type_discrete_grid_function(treepath): def type_discrete_grid_function(treepath):
name = "DiscreteGridFunction_{}".format("_".join(str(t) for t in treepath)) name = "DiscreteGridFunction_{}".format("_".join(str(t) for t in treepath))
return name return name
...@@ -109,14 +69,7 @@ def name_discrete_grid_function(gfs, vector_name, treepath): ...@@ -109,14 +69,7 @@ def name_discrete_grid_function(gfs, vector_name, treepath):
@preamble(section="error", kernel="main") @preamble(section="error", kernel="main")
def typedef_difference_squared_adapter(name, treepath): def typedef_difference_squared_adapter(name, treepath):
# Grid function representing exact solution bgf_type = main_type_grid_function("exact_solution", treepath)
# element = get_trial_element()
# func = preprocess_leaf_data(element, "exact_solution")
# if isinstance(element, MixedElement):
# index = treepath_to_index(element, treepath)
# func = (func[index],)
# element = element.extract_component(index)[1]
bgf_type = main_type_exact_solution_grid_function(treepath)
# Discrete grid function (numerical solution) # Discrete grid function (numerical solution)
gfs = main_name_trial_subgfs(treepath) gfs = main_name_trial_subgfs(treepath)
...@@ -136,7 +89,7 @@ def type_difference_squared_adapter(treepath): ...@@ -136,7 +89,7 @@ def type_difference_squared_adapter(treepath):
@preamble(section="error", kernel="main") @preamble(section="error", kernel="main")
def define_difference_squared_adapter(name, treepath): def define_difference_squared_adapter(name, treepath):
t = type_difference_squared_adapter(treepath) t = type_difference_squared_adapter(treepath)
sol = main_name_exact_solution_grid_function(treepath) sol = main_name_grid_function("exact_solution", treepath)
vector = main_name_vector(get_form_ident()) vector = main_name_vector(get_form_ident())
gfs = main_name_trial_subgfs(treepath) gfs = main_name_trial_subgfs(treepath)
dgf = name_discrete_grid_function(gfs, vector, treepath) dgf = name_discrete_grid_function(gfs, vector, treepath)
......
...@@ -19,7 +19,7 @@ from dune.codegen.pdelab.driver.constraints import (has_dirichlet_constraints, ...@@ -19,7 +19,7 @@ from dune.codegen.pdelab.driver.constraints import (has_dirichlet_constraints,
name_constraintscontainer, name_constraintscontainer,
) )
from dune.codegen.pdelab.driver.interpolate import (interpolate_dirichlet_data, from dune.codegen.pdelab.driver.interpolate import (interpolate_dirichlet_data,
name_boundary_grid_function, name_grid_function_root,
) )
from dune.codegen.pdelab.driver.solve import (print_matrix, from dune.codegen.pdelab.driver.solve import (print_matrix,
print_residual, print_residual,
...@@ -79,8 +79,7 @@ def time_loop(): ...@@ -79,8 +79,7 @@ def time_loop():
else: else:
osm = name_onestepmethod() osm = name_onestepmethod()
if has_dirichlet_constraints(is_dirichlet): if has_dirichlet_constraints(is_dirichlet):
dirichlet = preprocess_leaf_data(element, "interpolate_expression") boundary = name_boundary_grid_function_root("interpolate_expression")
boundary = name_boundary_grid_function(element, dirichlet)
apply_call = "{}.apply(time, dt, {}, {}, {}new);".format(osm, vector, boundary, vector) apply_call = "{}.apply(time, dt, {}, {}, {}new);".format(osm, vector, boundary, vector)
else: else:
apply_call = "{}.apply(time, dt, {}, {}new);".format(osm, vector, vector) apply_call = "{}.apply(time, dt, {}, {}new);".format(osm, vector, vector)
......
...@@ -21,109 +21,155 @@ from dune.codegen.pdelab.driver.gridfunctionspace import (name_trial_gfs, ...@@ -21,109 +21,155 @@ from dune.codegen.pdelab.driver.gridfunctionspace import (name_trial_gfs,
from ufl import FiniteElement, MixedElement, TensorElement, VectorElement, TensorProductElement from ufl import FiniteElement, MixedElement, TensorElement, VectorElement, TensorProductElement
@preamble(section="vector", kernel="driver_block")
def interpolate_vector(func, gfs, name):
return "Dune::PDELab::interpolate(*{}, *{}, *{});".format(func,
gfs,
name,
)
def interpolate_dirichlet_data(name): def interpolate_dirichlet_data(name):
element = get_trial_element() element = get_trial_element()
func = preprocess_leaf_data(element, "interpolate_expression", applyZeroDefault=False) func = preprocess_leaf_data(element, "interpolate_expression", applyZeroDefault=False)
if func is not None: if func is not None:
bf = name_boundary_grid_function(element, func) bf = name_grid_function_root("interpolate_expression")
gfs = name_trial_gfs() gfs = name_trial_gfs()
interpolate_vector(bf, gfs, name) interpolate_vector(bf, gfs, name)
@preamble(section="vector", kernel="driver_block") def _grid_function_root_type(identifier):
def interpolate_vector(func, gfs, name): name_dict = {"exact_solution": "ExactSolution",
return "Dune::PDELab::interpolate(*{}, *{}, *{});".format(func, "interpolate_expression": "BoundaryGridFunction"}
gfs, return name_dict[identifier]
name,
)
def _grid_function_root_name(identifier):
name_dict = {"exact_solution": "exactSolution",
"interpolate_expression": "boundaryGridFunction"}
return name_dict[identifier]
def _get_grid_function_method_name(identifier):
name_dict = {"exact_solution": "getExactSolution",
"interpolate_expression": "getBoundaryGridFunction"}
return name_dict[identifier]
@class_member(classtag="driver_block") @class_member(classtag="driver_block")
def typedef_composite_boundary_grid_function(name, children): def typedef_composite_grid_function(name, children):
templates = ','.join('std::decay_t<decltype(*{})>'.format(c) for c in children) templates = ','.join('std::decay_t<decltype(*{})>'.format(c) for c in children)
return "using {} = Dune::PDELab::CompositeGridFunction<{}>;".format(name, templates) return "using {} = Dune::PDELab::CompositeGridFunction<{}>;".format(name, templates)
def type_composite_boundary_grid_function(children, root): def type_composite_grid_function(identifier, children, root):
if root: if root:
name = "BoundaryGridFunction" name = _grid_function_root_type(identifier)
else: else:
name = "CompositeGridFunction_{}".format('_'.join(c for c in children)) name = "CompositeGridFunction_{}".format('_'.join(c for c in children))
typedef_composite_boundary_grid_function(name, children) typedef_composite_grid_function(name, children)
return name return name
@class_member(classtag="driver_block") @class_member(classtag="driver_block")
def declare_composite_boundary_grid_function(name, children, root): def declare_composite_grid_function(identifier, name, children, root):
composite_gfs_type = type_composite_boundary_grid_function(children, root) composite_gfs_type = type_composite_grid_function(identifier, children, root)
return "std::shared_ptr<{}> {};".format(composite_gfs_type, name) return "std::shared_ptr<{}> {};".format(composite_gfs_type, name)
@preamble(section="vector", kernel="driver_block") @preamble(section="vector", kernel="driver_block")
def define_composite_boundary_grid_function(name, children, root=False): def define_composite_grid_function(identifier, name, children, root=True):
declare_composite_boundary_grid_function(name, children, root) declare_composite_grid_function(identifier, name, children, root)
composite_gfs_type = type_composite_boundary_grid_function(children, root) composite_gfs_type = type_composite_grid_function(identifier, children, root)
return "{} = std::make_shared<{}>({});".format(name, composite_gfs_type, ', '.join('*{}'.format(c) for c in children)) return "{} = std::make_shared<{}>({});".format(name, composite_gfs_type, ', '.join('*{}'.format(c) for c in children))
def _is_local(func): def function_lambda(func):
# palpo TODO assert isinstance(func, tuple)
return True func = func[0]
# return False if func is None:
# assert isinstance(func, tuple) func = 0.0
# if len(func) == 2:
# return True if isinstance(func, (int, float)):
# else: return "[&](const auto& is, const auto& xl){{ return {}; }}".format(float(func))
# try: else:
# assert len(func) == 1 from ufl.classes import Expr
# except: assert isinstance(func, Expr)
# from pudb import set_trace; set_trace() from dune.codegen.pdelab.driver.visitor import ufl_to_code
# return False return "[&](const auto& is, const auto& xl){{ {}; }}".format(ufl_to_code(func))
@class_member(classtag="driver_block") @class_member(classtag="driver_block")
def typedef_boundary_grid_function(name, local): def typedef_function(name):
range_type = type_range()
leafview_type = type_leafview()
entity = "typename {}::template Codim<0>::Entity".format(leafview_type)
coordinate = "typename {}::template Codim<0>::Entity::Geometry::LocalCoordinate".format(leafview_type)
return "using {} = std::function<{}({}, {})>;".format(name, range_type, entity, coordinate)
def type_function():
name = "FunctionExpression"
typedef_function(name)
return name
@class_member(classtag="driver_block")
def declare_function(name):
function_type = type_function()
return "std::shared_ptr<{}> {};".format(function_type, name)
@preamble(section="vector", kernel="driver_block")
def define_function(name, func):
declare_function(name)
function_type = type_function()
fct_lambda = function_lambda(func)
return "{} = std::make_shared<{}> ({});".format(name, function_type, fct_lambda)
@cached
def name_function(func):
name = get_counted_variable("functionExpression")
define_function(name, func)
return name
@class_member(classtag="driver_block")
def typedef_grid_function(name):
leafview_type = type_leafview() leafview_type = type_leafview()
range_type = type_range() range_type = type_range()
boundary_function_type = type_boundary_function(local) function_type = type_function()
# palpo TODO: 1 in the format below!
return "using {} = Dune::PDELab::LocalCallableToGridFunctionAdapter<{}, {}, {}, {}>;".format(name, return "using {} = Dune::PDELab::LocalCallableToGridFunctionAdapter<{}, {}, {}, {}>;".format(name,
leafview_type, leafview_type,
range_type, range_type,
1, 1,
boundary_function_type) function_type)
def type_boundary_grid_function(local, root): def type_grid_function(identifier, root):
# TODO: remove local stuff name = "GridFunctionLeaf"
# if local:
# name = "BoundaryGridFunctionLocal"
# else:
# name = "BoundaryGridFunctionGlobal"
if root: if root:
name = "BoundaryGridFunction" name = _grid_function_root_type(identifier)
else: typedef_grid_function(name)
name = "BoundaryGridFunctionLeaf"
typedef_boundary_grid_function(name, local)
return name return name
@class_member(classtag="driver_block") @class_member(classtag="driver_block")
def declare_boundary_grid_function(name, local, root): def declare_grid_function(identifier, name, root):
bgf_type = type_boundary_grid_function(local, root) grid_function_type = type_grid_function(identifier, root)
return "std::shared_ptr<{}> {};".format(bgf_type, name) return "std::shared_ptr<{}> {};".format(grid_function_type, name)
@preamble(section="vector", kernel="driver_block") @preamble(section="vector", kernel="driver_block")
def define_boundary_grid_function(name, func, root=False): def define_grid_function(identifier, name, func, root=True):
local = _is_local(func) declare_grid_function(identifier, name, root)
declare_boundary_grid_function(name, local, root)
gv = name_leafview() gv = name_leafview()
boundary_function = name_boundary_function(func, local) function_name = name_function(func)
bgf_type = type_boundary_grid_function(local, root) grid_function_type = type_grid_function(identifier, root)
include_file('dune/pdelab/function/callableadapter.hh', filetag='driver') include_file('dune/pdelab/function/callableadapter.hh', filetag='driver')
if is_stationary(): if is_stationary():
return "{} = std::make_shared<{}>({}, *{});".format(name, bgf_type, gv, boundary_function) return "{} = std::make_shared<{}>({}, *{});".format(name, grid_function_type, gv, function_name)
else: else:
# palpo TODO # palpo TODO
assert False assert False
...@@ -136,134 +182,87 @@ def define_boundary_grid_function(name, func, root=False): ...@@ -136,134 +182,87 @@ def define_boundary_grid_function(name, func, root=False):
) )
@cached
def name_boundary_grid_function(element, func, root=True): def name_grid_function(identifier, element, func, root=True):
assert isinstance(func, tuple) assert isinstance(func, tuple)
if isinstance(element, MixedElement): if isinstance(element, MixedElement):
k = 0 k = 0
childs = [] childs = []
for subel in element.sub_elements(): for subel in element.sub_elements():
childs.append(name_boundary_grid_function(subel, func[k:k + subel.value_size()], root=False)) childs.append(name_grid_function(identifier, subel, func[k:k + subel.value_size()], root=False))
k = k + subel.value_size() k = k + subel.value_size()
name = "_".join(childs) name = "_".join(childs)
if len(childs) == 1: if len(childs) == 1:
name = "{}_dummy".format(name) name = "{}_dummy".format(name)
if root: if root:
name = "boundary_grid_function" name = identifier
define_composite_boundary_grid_function(name, tuple(childs), root=root) define_composite_grid_function(identifier, name, tuple(childs), root=root)
else: else:
assert isinstance(element, (FiniteElement, TensorProductElement)) assert isinstance(element, (FiniteElement, TensorProductElement))
name = get_counted_variable("boundary_grid_function") name = get_counted_variable(identifier)
if root: if root:
name = "boundary_grid_function" name = identifier
define_boundary_grid_function(name, func, root=root) define_grid_function(identifier, name, func, root=root)
if root:
print("palpo 1 element: {}".format(element))
driver_block_get_boundarygridfunction(element, func, name=name)
return name return name
def boundary_lambda(func, local): def name_grid_function_root(identifier):
# palpo TODO element = get_trial_element()
assert isinstance(func, tuple) func = preprocess_leaf_data(element, identifier)
func = func[0] name = name_grid_function(identifier, element, func, root=True)
if func is None:
func = 0.0
if isinstance(func, (int, float)):
return "[&](const auto& is, const auto& x){{ return {}; }}".format(float(func))
else:
from ufl.classes import Expr
assert isinstance(func, Expr)
from dune.codegen.pdelab.driver.visitor import ufl_to_code
return "[&](const auto& is, const auto& xl){{ {}; }}".format(ufl_to_code(func))
@class_member(classtag="driver_block")
def typedef_boundary_function(name, local):
range_type = type_range()
leafview_type = type_leafview()
if not local:
coordinate = "typename {}::template Codim<0>::Entity::Geometry::GlobalCoordinate".format(leafview_type)
return "using {} = std::function<{}({})>;".format(name, range_type, coordinate)
else:
entity = "typename {}::template Codim<0>::Entity".format(leafview_type)
coordinate = "typename {}::template Codim<0>::Entity::Geometry::LocalCoordinate".format(leafview_type)
return "using {} = std::function<{}({}, {})>;".format(name, range_type, entity, coordinate)
def type_boundary_function(local):
name = "BoundaryFunction"
if not local:
name = name + "Global"
else:
name = name + "Local"
typedef_boundary_function(name, local)
return name return name
@class_member(classtag="driver_block") @preamble(section="postprocessing", kernel="main")
def declare_boundary_function(name, local): def main_typedef_grid_function(name, identifier, treepath):
bf_type = type_boundary_function(local) if len(treepath) == 0:
return "std::shared_ptr<{}> {};".format(bf_type, name) driver_block_type = type_driver_block()
gf_type = _grid_function_root_type(identifier)
type_name = "{}::{}".format(driver_block_type, gf_type)
@preamble(section="vector", kernel="driver_block") else:
def define_boundary_function(name, func, local): root_type = main_type_grid_function(identifier, ())
declare_boundary_function(name, local) type_name = "Dune::TypeTree::Child<{}, {}>;".format(root_type, ", ".join(str(t) for t in treepath))
bf_type = type_boundary_function(local) return "using {} = {};".format(name, type_name)
bf_lambda = boundary_lambda(func, local)
return "{} = std::make_shared<{}> ({});".format(name, bf_type, bf_lambda)
@cached
def name_boundary_function(func, local):
name = get_counted_variable("boundary_function")
define_boundary_function(name, func, local)
return name
@preamble(section="postprocessing", kernel="main") def main_type_grid_function(identifier, treepath):
def main_typedef_boundary_grid_function(name): name = _grid_function_root_type(identifier)
driver_block_type = type_driver_block() if len(treepath) > 0:
# palpo TODO name = "{}_{}".format(name, "_".join(str(t) for t in treepath))
local = True main_typedef_grid_function(name, identifier, treepath)
# bgf_type = type_boundary_grid_function(local, True)
bgf_type = "BoundaryGridFunction"
print("palpo name: {}".format(name))
return "using {} = {}::{};".format(name, driver_block_type, bgf_type)
def main_type_boundary_grid_function():
name = "BoundaryGridFunction"
main_typedef_boundary_grid_function(name)
return name return name
@class_member(classtag="driver_block") @class_member(classtag="driver_block")
def driver_block_get_boundarygridfunction(element, func, name=None): def driver_block_get_grid_function(identifier, name=None):
assert identifier in ["exact_solution", "interpolate_expression"]
if not name: if not name:
name = name_boundary_grid_function(element, func) name = name_grid_function_root(identifier)
# bgf_type = type_boundary_grid_function(True, True) gf_type = _grid_function_root_type(identifier)
bgf_type = "BoundaryGridFunction" method_name = _get_grid_function_method_name(identifier)
return ["std::shared_ptr<{}> getBoundaryGridFunction(){{".format(bgf_type), return ["std::shared_ptr<{}> {}(){{".format(gf_type, method_name),
" return {};".format(name), " return {};".format(name),
"}"] "}"]
@preamble(section="postprocessing", kernel="main") @preamble(section="postprocessing", kernel="main")
def main_define_boundary_grid_function(name, element, func): def main_define_grid_function(name, identifier, treepath):
driver_block_name = name_driver_block() if len(treepath) == 0:
# palpo TODO driver_block_get_grid_function(identifier)
# driver_block_get_boundarygridfunction(element, func) driver_block_name = name_driver_block()
return "auto {} = {}.getBoundaryGridFunction();".format(name, driver_block_name) method_name = _get_grid_function_method_name(identifier)
return "auto {} = {}.{}();".format(name, driver_block_name, method_name)
else:
root_name = main_name_grid_function(identifier, ())
indices = ["Dune::Indices::_{}".format(str(t)) for t in treepath]
return "auto {} = child(*{}, {});".format(name, root_name, ", ".join(i for i in indices))
@cached
def main_name_boundary_grid_function(element, func): def main_name_grid_function(identifier, treepath):
# palpo TODO func rauswerfen? name = _grid_function_root_name(identifier)
assert isinstance(func, tuple) if len(treepath) > 0:
name = "boundaryGridFunction" name = "{}_{}".format(name, "_".join(str(t) for t in treepath))
main_define_boundary_grid_function(name, element, func) main_define_grid_function(name, identifier, treepath)
return name return name
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment