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

[Bug]: Upwind/downwind schemes are not working properly #3526

Closed
brosaplanella opened this issue Nov 16, 2023 · 3 comments · Fixed by #3979
Closed

[Bug]: Upwind/downwind schemes are not working properly #3526

brosaplanella opened this issue Nov 16, 2023 · 3 comments · Fixed by #3979
Labels
bug Something isn't working difficulty: easy A good issue for someone new. Can be done in a few hours priority: low No existing plans to resolve

Comments

@brosaplanella
Copy link
Member

brosaplanella commented Nov 16, 2023

PyBaMM Version

develop

Python Version

all

Describe the bug

Checking the problem solved in finite-volumes.ipynb it does not correspond with what one would expect from an upwind scheme.

See example below.

Steps to Reproduce

The example below compares a toy problem with its analytical solution. Note that for coarse meshes there is some disagreement but it converges if you refine the mesh. Commenting the lines that fix the system retrieves the previous behaviour which disagrees with the analytical solution.

import pybamm


model = pybamm.BaseModel()
x = pybamm.SpatialVariable("x", domain="domain", coord_sys="cartesian")
u = pybamm.Variable("u", domain="domain")
u_an = x + (pybamm.t - x) * ((x - pybamm.t) > 0)
v = pybamm.PrimaryBroadcastToEdges(1, ["domain"])
rhs = - pybamm.div(pybamm.upwind(u) * v) + 1
model.boundary_conditions = {
    u: {
        "left": (pybamm.Scalar(0), "Dirichlet"),
    }
}
model.rhs = {u: rhs}
model.initial_conditions = {u: pybamm.Scalar(0)}
model.variables = {"u": u, "x": x, "analytical": u_an}
geometry = {"domain": {x: {"min": pybamm.Scalar(0), "max": pybamm.Scalar(1)}}}
submesh_types = {"domain": pybamm.Uniform1DSubMesh}
N = 5
var_pts = {x: N}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
spatial_methods = {"domain": pybamm.FiniteVolume()}
disc = pybamm.Discretisation(mesh, spatial_methods)
model_disc = disc.process_model(model, inplace=False)

# Fix the entries of the discretised system (uncomment to get original behaviour)
for i in range(N - 1):
    model_disc.rhs[u].children[1].children[0].entries[i + 1, i] = 0
    model_disc.rhs[u].children[1].children[0].entries[i + 1, i + 1] = -N
    model_disc.rhs[u].children[1].children[0].entries[i + 1, i + 2] = N
model_disc.rhs[u].children[1].children[0].entries[0, 0] = - 2 * N
model_disc.rhs[u].children[1].children[0].entries[0, 1] = 2 * N


print(model_disc.rhs[u].children[1].children[0].entries.toarray())
solver = pybamm.CasadiSolver()
solution =solver.solve(model_disc, [0, 10])

plot = pybamm.QuickPlot(solution,[["u", "analytical"]])
plot.dynamic_plot()

Relevant log output

No response

@brosaplanella brosaplanella added bug Something isn't working difficulty: easy A good issue for someone new. Can be done in a few hours priority: low No existing plans to resolve labels Nov 16, 2023
brosaplanella added a commit that referenced this issue Nov 16, 2023
@brosaplanella
Copy link
Member Author

The current approach creates an "upwind matrix" that converts the standard divergence matrix to the upwind divergence. However, the standard divergence matrix is singular (or at least for the problem I tested above) so I don't think such transformation exists.

I think a better approach would be to allow a keyword argument when calling pybamm.div that allows specifying upwind/downwind (or other methods in the future) so different matrices are assembled (similar on how different matrices are assembled for different coordinate systems).

@brosaplanella
Copy link
Member Author

brosaplanella commented Nov 16, 2023

I have been playing with it and I can think of 2 possible ways of implementing this, and it boils down to how we conceptualise the model. The entry point for both would be to allow an extra keyword method.

  1. Define UpwindDivergence and DownwindDivergence operator
    The divergence function would contain an if statement that defines a different symbol depending on the value of method. Then the spatial method should have separate methods for upwind and downwind. This would enable defining these methods as NotImplemented in the base class so it throws an error if the operator is used with a spatial method that doesn't support it.

  2. Keep Divergence operator and have the if statement in the spatial method
    It has the advantage of being more generalisable and applicable to other operators, but not sure if there are any other cases we might want to implement that would benefit from this. I am thinking, for example, of a TVD scheme, but not sure if that's something we want to implement to a given operator or is a whole new spatial method. It would also require a lot of checks in the spatial methods to ensure if a wrong method is passed we throw an error.

I am not sure which way to go. I am tempted to say that, unless we can think of a good near-future feature that would benefit from approach 2, let's use 1 for now as it is cleaner and less prone to errors.

What do you think @tinosulzer @rtimms @martinjrobins?

@valentinsulzer
Copy link
Member

It's been too long since I thought hard about spatial methods so just do whatever you think is simplest and safest. Does adding method require a user input or is there a sensible default?

brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 10, 2024
brosaplanella added a commit that referenced this issue Apr 11, 2024
brosaplanella added a commit that referenced this issue Apr 12, 2024
* #3526 initialise branch

* #3256 add UpwindDivergence and DownwindDivergence

* work in progress for fixing upwind scheme

* #3526 add missing argument docstring

* #3526 fix upwind/downwind methods

* style: pre-commit fixes

* #3526 get finite volume notebook from develop

* 3526 add temporary test

* ruff

* style: pre-commit fixes

* #3526 remove unused variable

* #3526 clarify wording

* #3526 fix tests

* #3526 add to CHANGELOG

* #3526 update coverage

* #3526 add integration test

* style: pre-commit fixes

* #3526 remove example for debugging

* Remove print for debug

Co-authored-by: Eric G. Kratz <[email protected]>

* #3526 add second test

* #3526 generalised example but did not add test as was hard to know what to measure

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <[email protected]>
Co-authored-by: Valentin Sulzer <[email protected]>
js1tr3 pushed a commit to js1tr3/PyBaMM that referenced this issue Aug 12, 2024
* pybamm-team#3526 initialise branch

* pybamm-team#3256 add UpwindDivergence and DownwindDivergence

* work in progress for fixing upwind scheme

* pybamm-team#3526 add missing argument docstring

* pybamm-team#3526 fix upwind/downwind methods

* style: pre-commit fixes

* pybamm-team#3526 get finite volume notebook from develop

* 3526 add temporary test

* ruff

* style: pre-commit fixes

* pybamm-team#3526 remove unused variable

* pybamm-team#3526 clarify wording

* pybamm-team#3526 fix tests

* pybamm-team#3526 add to CHANGELOG

* pybamm-team#3526 update coverage

* pybamm-team#3526 add integration test

* style: pre-commit fixes

* pybamm-team#3526 remove example for debugging

* Remove print for debug

Co-authored-by: Eric G. Kratz <[email protected]>

* pybamm-team#3526 add second test

* pybamm-team#3526 generalised example but did not add test as was hard to know what to measure

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <[email protected]>
Co-authored-by: Valentin Sulzer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working difficulty: easy A good issue for someone new. Can be done in a few hours priority: low No existing plans to resolve
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants