-
Notifications
You must be signed in to change notification settings - Fork 92
Description
Describe the bug
As implemented now, when applied to more than one component, the Laplacian operator will call scalar_laplace, which itself calls grad, for each requested component individually, giving grad the component label directly.
When a component label is itself an iterable (str) of length more than one, grad will interpret it as a list of components instead, and iterate over it as if it were several variables
This leads to the gradient being computed on the wrong variables, or on variables that do not exist, causing an error.
The following code reproduces the error:
import torch
from pina import LabelTensor
from pina.operators import laplacian
d = torch.rand((20, 2))
input_data = LabelTensor( d, ['x','y'] )
output_data = LabelTensor( 2*d, ['ab','cd'] )
laplacian(output_data, input_data, components=['ab','cd'])
Expected behavior
The Laplacians of 'ab' and 'cd' are computed.
Output
Attempts to compute the gradient of 'a', which causes an error:
ValueError: 'a' is not in list
Additional context
Using version 0.2.0 of pina-mathlab.
Suggestion
One possible fix would be to change line 233 of operator.py, currently:
result[:, idx] = scalar_laplace(output_, input_, c, d).flatten()
into:
result[:, idx] = scalar_laplace(output_, input_, [c], d).flatten()
This is the line that calls scalar_laplace when laplacian is called on several components. c is the variable that iterates over component labels, never containing more than one. By replacing c by [c], the grad function called by scalar_laplace now knows without ambiguity that it should be applied on a single variable, no matter the length of its label.