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

Centre stencil #17

Merged
merged 31 commits into from
Jul 10, 2024
Merged

Centre stencil #17

merged 31 commits into from
Jul 10, 2024

Conversation

ZoeLeibowitz
Copy link
Owner

@ZoeLeibowitz ZoeLeibowitz commented Jul 1, 2024

Add function to extract the centre stencil from an equation.

the input equation (first argument).

NOTE: At the point of entry, the time derivatives are likely evaluated, but
not the spatial derivatives. This necessitates evaluating 'eqn'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cannot pre-evaluate an expression -- thus destroying its (e.g.) IndexDerivative objects -- to obtain this information... assuming I'm reading this docstring correctly. You'd be hurting compilation elsewhere.

Can you add an example of what you're trying to accomplish here? Also, what's the use case?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looks like this only locally evaluate it to extract the middle part so it shouldn't impact the rest of the compiler.
evaluate can be a little bit expensive though might wanna add a TODO that needs to be improved somehow

@@ -7,6 +7,7 @@
from devito.petsc.types import PETScArray, LinearSolveExpr, MatVecEq, RHSEq

from sympy import simplify
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: line break between sympy/numpy/etc imports and Devito imports. Also group Devito imports

b, F_target = separate_eqn(eq, target)

# Args were updated so need to update target to enable uxreplace on F_target
new_target = {func for func in retrieve_functions(F_target) if
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: f rather than func might be tidier

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also == or is?


# Args were updated so need to update target to enable uxreplace on F_target
new_target = {func for func in retrieve_functions(F_target) if
func.function == target.function}.pop()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be safer to do this in two steps? i.e.

new_target = {f for f in retrieve_functions(F_target) if f.function == target.function}
assert len(new_target) == 1  # Sanity check: one target expected
new_target = new_target.pop()

That way if you ever end up with multiple targets for whatever reason, it will error, rather than failing cryptically

@@ -66,8 +75,9 @@ def separate_eqn(eqn, target):
zeroed_eqn = Eq(eqn.lhs - eqn.rhs, 0)
tmp = eval_time_derivatives(zeroed_eqn.lhs)
b = remove_target(tmp, target)
F_target = simplify(tmp - b)

# Is this ok? Is there another way of using simplify
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use devito.finite_differences.differentiable.diffify to turn sp.Add etc into dv.Add

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need simplify, what's wrong with just tmp - b ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases this does not work and it evaluates to -term + term , but simplify evaluates it to 0



@extract_centre.register(Mul)
def _(expr, target):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be consolidated with the Add/EvalDerivative dispatch? Seems like the only difference is the if not a.has(target)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so



@skipif('petsc')
def test_centre_stencil():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done with pytest.mark.parametrize rather than hardcoding a bunch of values in a single test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to test something like (f * g.dx).dy to mimic div(grad) eqs.

@@ -105,3 +115,52 @@ def _(expr, target):
@remove_target.register(Derivative)
def _(expr, target):
return 0 if expr.has(target) else expr


def centre_stencil(expr, target):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems redundant?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just haven't used it yet but I will need the functionality

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will do more stuff later?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function won't do more, it will be used elsewhere

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry I see what you mean, I removed the expr.evaluate so yes it is now redundant

if not a.has(target):
args.append(a)
else:
a1 = centre_stencil(a, target)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just args.append(centre_stencil(a, target))

@ZoeLeibowitz ZoeLeibowitz merged commit 321b874 into separate-eqn Jul 10, 2024
1 check passed
@ZoeLeibowitz ZoeLeibowitz deleted the centre-stencil branch July 10, 2024 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants