Discretisation problems when using both algebraic and time-derivative variables #3232
-
Hi, I am trying to implement a model in pybamm, which relies on three base variables, two governed by time-derivative equations of the form dX_a/dt = div(N), where N = sum_a A_a grad(X_a) and the final variable is determined by a poisson equation div(grad(Y)) = X_a. I have attached a simplified version below with only two variables, but reflecting the shape I need. However, I need line 11 to be of the form N = -pybamm.grad(c - d / k) or N = -pybamm.grad(c) - pybamm.grad(d / k). This produces an error "pybamm.expression_tree.exceptions.ShapeError: Cannot find shape (original error: operands could not be broadcast together with shapes (21,1) (19,1) )", throwing two previous errors "AttributeError: 'Subtraction' object has no attribute '_saved_evaluate_for_shape'. Did you mean: '_evaluate_for_shape'?" and "ValueError: operands could not be broadcast together with shapes (21,1) (19,1)". I suspect the solver uses different discretisations for the variables c and d. Is there a way to force the discretisations to be equal, or another way to handle this? Thank you! codeimport pybamm model = pybamm.BaseModel() c = pybamm.Variable("Concentration", domain="negative particle") # the two variables I am solving for k = pybamm.Parameter("Constant k") N = -pybamm.grad(c) - pybamm.grad(d) / k # combine the gradients to get the flux, this line leads to issues dcdt = -pybamm.div(N) # define the rhs equation poisson = pybamm.div(pybamm.grad(d)) + c # define the algebraic equation model.rhs = {c: dcdt} # add the equation to rhs dictionary with the variable as the key #boundary conditions #initial conditions model.variables = { r = pybamm.SpatialVariable("r", domain=["negative particle"], coord_sys="cartesian") geometry = {"negative particle": {r: {"min": 0, "max": 1}}} spatial_methods = {"negative particle": pybamm.FiniteVolume()} parameter_values = pybamm.ParameterValues({ solver = pybamm.CasadiSolver() sim = pybamm.Simulation( solution = sim.solve([0, 1]) sim.plot(["Concentration", "Algebraic"]) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Your code works fine for me on the latest version of pybamm |
Beta Was this translation helpful? Give feedback.
Ah I see. The boundary conditions are applied to the variable inside the
grad
, so you have to write your model in a way that the variable in thegrad
is the one that you have boundary conditions for (e.g. by applying the chain/product rule). Off-by-two shape error means that the boundary conditions have not been correctly applied.