question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Diff of a ParameterFunction doesn't work when evaluated at a constant

See original GitHub issue

Describe the bug When defining a parameter as the diff of a ParameterFunction sometimes it returns 0. I think the issue is that the expression gets simplified first and, because the argument is a constant, we loose track of the differentiation variable. Then, when taking the derivative, the variable is not in the tree and therefore it returns 0.

To Reproduce Steps to reproduce the behaviour:

  1. Add a diff parameter to your model, for example
            def Uprime(sto, T):
                inputs = {"Positive particle stoichiometry": sto}
                Uprime_ref = pybamm.FunctionParameter(
                    "Positive electrode OCP [V]", inputs, diff_variable=sto
                )
                return Uprime_ref  / pot
  1. Set the positive OCV from a function (for example a linear function).
  2. Evaluate at c_0 = param.c_p_init(1), the function returns zero.
  3. Evaluate at c_0 + 0 * pybamm.t, the function now returns the right value.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
brosaplanellacommented, Nov 17, 2020

Ok, then I will implement the hacky method of the Multiplication by one, as you did with c_e.

0reactions
tinosulzercommented, Nov 17, 2020

You might need to do the adding 0 * pybamm.t for it to work. Otherwise even the multiplication will get simplified out later, e.g. 2 * pybamm.Multiplication(5, 1) would realize that the whole thing is constant and return Scalar(10)

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the difference between an argument and a parameter?
A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the...
Read more >
Functions - Advanced R. - Hadley Wickham
This function would not work without lazy evaluation because both x and y would always be evaluated, testing a > 0 even when...
Read more >
Function pass by value vs. pass by reference
I will call what you are passing in a to a function the actual parameters, and where you receive them, the parameters in...
Read more >
24. Passing Arguments | Python Tutorial
The passing of parameters and arguments in Python. Explaining the difference between call by object sharing, call by value and call by name....
Read more >
Why do I get the error "Unrecognized function or variable"?
This error occurs only when I use mphload('filename') in an exe file. When in a .m file this line works well. I thought...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found