Source code for ufl.algorithms.remove_complex_nodes

# -*- coding: utf-8 -*-
"""Algorithm for removing conj, real, and imag nodes
from a form for when the user is in 'real mode'"""

from ufl.corealg.multifunction import MultiFunction
from ufl.constantvalue import ComplexValue
from ufl.algorithms.map_integrands import map_integrand_dags
from ufl.log import error


[docs]class ComplexNodeRemoval(MultiFunction): """Replaces complex operator nodes with their children""" expr = MultiFunction.reuse_if_untouched
[docs] def conj(self, o, a): return a
[docs] def real(self, o, a): return a
[docs] def imag(self, o, a): error("Unexpected imag in real expression.")
[docs] def terminal(self, t, *ops): if isinstance(t, ComplexValue): error('Unexpected complex value in real expression.') else: return t
[docs]def remove_complex_nodes(expr): """Replaces complex operator nodes with their children. This is called during compute_form_data if the compiler wishes to compile real-valued forms. In essence this strips all trace of complex support from the preprocessed form. """ return map_integrand_dags(ComplexNodeRemoval(), expr)