Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compat with pre-dataclass expressions #248

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytential/symbolic/mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def rec_int_g_arguments(mapper, expr):
name: mapper.rec(arg) for name, arg in expr.kernel_arguments.items()
}

changed = (
changed = not (
inducer marked this conversation as resolved.
Show resolved Hide resolved
all(d is orig for d, orig in zip(densities, expr.densities, strict=True))
and all(
arg is orig for arg, orig in zip(
Expand Down
60 changes: 53 additions & 7 deletions pytential/symbolic/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
THE SOFTWARE.
"""

from collections.abc import Iterable
from dataclasses import field
from warnings import warn
from functools import partial
from typing import Any, Union, Literal
from typing import Any, Literal, Union

import numpy as np

Expand Down Expand Up @@ -520,7 +521,9 @@ class NodeCoordinateComponent(DiscretizationProperty):
"""The axis index this node coordinate represents, i.e. 0 for $x$, etc."""

# FIXME: this is added for backwards compatibility with pre-dataclass expressions
def __init__(self, ambient_axis: int, dofdesc: DOFDescriptorLike) -> None:
def __init__(self,
ambient_axis: int,
dofdesc: DOFDescriptorLike | None = None) -> None:
object.__setattr__(self, "ambient_axis", ambient_axis)
super().__init__(dofdesc) # type: ignore[arg-type]

Expand Down Expand Up @@ -578,7 +581,7 @@ def make_op(operand_i):
def __init__(self,
ref_axes: tuple[tuple[int, int], ...],
operand: Expression,
dofdesc: DOFDescriptorLike) -> None:
dofdesc: DOFDescriptorLike | None = None) -> None:
object.__setattr__(self, "ref_axes", ref_axes)
object.__setattr__(self, "operand", operand)
super().__init__(dofdesc) # type: ignore[arg-type]
Expand Down Expand Up @@ -1236,7 +1239,9 @@ class SingleScalarOperandExpressionWithWhere(Expression):

operand: Operand
"""An expression or an array on which to apply the operation."""
dofdesc: DOFDescriptor

# pylint: disable-next=invalid-field-call
dofdesc: DOFDescriptor = field(default_factory=lambda: DEFAULT_DOFDESC)
"""The descriptor for the geometry where the *operand* is defined."""

def __new__(cls,
Expand Down Expand Up @@ -1347,9 +1352,13 @@ class IterativeInverse(Expression):
"""The right-hand side variable used in the linear solve."""
variable_name: str
"""The name of the variable to solve for."""
extra_vars: dict[str, Variable]

# pylint: disable-next=invalid-field-call
extra_vars: dict[str, Variable] = field(default_factory=dict)
"""A dictionary of additional variables required to define the operator."""
dofdesc: DOFDescriptor

# pylint: disable-next=invalid-field-call
dofdesc: DOFDescriptor = field(default_factory=lambda: DEFAULT_DOFDESC)
"""A descriptor for the geometry on which the solution is defined."""

def __post_init__(self) -> None:
Expand Down Expand Up @@ -1450,7 +1459,7 @@ def hashable_kernel_args(kernel_arguments):
return tuple(hashable_args)


@expr_dataclass(hash=False)
@expr_dataclass(init=False, hash=False)
class IntG(Expression):
r"""
.. math::
Expand Down Expand Up @@ -1528,6 +1537,43 @@ class IntG(Expression):
them.
"""

def __init__(
self,
target_kernel: Kernel,
source_kernels: Iterable[Kernel],
densities: Iterable[Expression],
qbx_forced_limit: QBXForcedLimit,
source: DOFDescriptorLike | None = None,
target: DOFDescriptorLike | None = None,
kernel_arguments: dict[str, Any] | None = None,
**kwargs: Any
) -> None:
if kernel_arguments is None:
kernel_arguments = {}

if kwargs:
warn(f"Passing named '**kwargs' to {type(self).__name__!r} is "
"deprecated and will result in an error in 2025. Use the "
"'kernel_arguments' argument instead.",
DeprecationWarning, stacklevel=2)

kernel_arguments = kernel_arguments.copy()
for name, value in kwargs.items():
if name in kernel_arguments:
raise ValueError(f"'{name}' already set in 'kernel_arguments'")

kernel_arguments[name] = value

object.__setattr__(self, "target_kernel", target_kernel)
object.__setattr__(self, "source_kernels", source_kernels)
object.__setattr__(self, "densities", densities)
object.__setattr__(self, "qbx_forced_limit", qbx_forced_limit)
object.__setattr__(self, "source", source)
object.__setattr__(self, "target", target)
object.__setattr__(self, "kernel_arguments", kernel_arguments)

self.__post_init__()

def __post_init__(self) -> None:
if self.qbx_forced_limit not in {-1, +1, -2, +2, "avg", None}:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion test/test_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def test_mapper_int_g_term_collector(op_name, k=0):
raise ValueError(f"unknown operator name: {op_name}")

from pytential.symbolic.mappers import flatten
assert expr_only_intgs == flatten(expected_expr)
assert flatten(expr_only_intgs) == flatten(expected_expr)

# }}}

Expand Down
Loading