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

set T_init as a function in the x domain #3151

Closed
wants to merge 0 commits into from

Conversation

tommaull
Copy link
Contributor

@tommaull tommaull commented Jul 14, 2023

Description

Changed T_init to functional parameter in the x domain

Fixes #3131

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python run-tests.py --all
  • The documentation builds: $ python run-tests.py --doctest

You can run unit and doctests together at once, using $ python run-tests.py --quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@tommaull
Copy link
Contributor Author

tommaull commented Jul 14, 2023

Lots of test errors, opening PR for visibility only.

@tommaull
Copy link
Contributor Author

So I'm getting an error that I don't fully understand within the symbol.py, called through the equation tree. There appears to be math operation where two different domains are being subtracted. (Current collector and particle).

@brosaplanella
Copy link
Member

My guess is that the errors are caused because now T_init is a function of x but this has not been changed in the other scripts, so you need to pass the additional argument to all instances of T_init.

@valentinsulzer
Copy link
Member

You'll also need to make sure that you set pybamm.x_average(T_init(x)) as the initial condition for the lumped thermal model, and maybe do some broadcasting for the pouch models.

@tommaull
Copy link
Contributor Author

OK, so got some more stuff working, but now it seems like there's some confusion around the domain variable between the submodels and the base model.

Error trace:

File c:\users\tom.maull\desktop\pybammdev\pybamm\pybamm\models\submodels\thermal\lumped.py:24, in Lumped.init(self, param, domain, options)
23 def init(self, param, domain, options=None):
---> 24 super().init(param, domain, options=options)
25 pybamm.citations.register("Timms2021")

File c:\users\tom.maull\desktop\pybammdev\pybamm\pybamm\models\submodels\thermal\base_thermal.py:23, in BaseThermal.init(self, param, domain, options)
21 def init(self, param, domain, options=None):
22 super().init(param, options=options)
---> 23 self.domain = domain

File c:\users\tom.maull\desktop\pybammdev\pybamm\pybamm\models\submodels\base_submodel.py:123, in BaseSubModel.domain(self, domain)
120 @domain.setter
121 def domain(self, domain):
122 if domain is not None:
--> 123 domain = domain.lower()
124 ok_domain_list = ["negative", "separator", "positive", None]
125 if domain in ok_domain_list:

@tommaull
Copy link
Contributor Author

image

@tommaull tommaull marked this pull request as ready for review August 10, 2023 13:00
@tommaull
Copy link
Contributor Author

Hey is anyone able to take a look at this one?

Comment on lines 44 to 49
x = pybamm.SpatialVariable(
f"x_{domain[0]}",
domain=[f"{domain} electrode"],
auxiliary_domains={"secondary": "current collector"},
coord_sys="cartesian",
)
Copy link
Member

Choose a reason for hiding this comment

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

I think the issue with the tests is here as the domain is inherited from the for loop and it is not a string.

Copy link
Contributor

Choose a reason for hiding this comment

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

this should be the "whole cell" x, i.e.

whole_cell = ["negative electrode", "separator", "positive electrode"]
x = pybamm.SpatialVariable(
    "x",
    domain=whole_cell,
    auxiliary_domains={"secondary": "current collector"},
    coord_sys="cartesian",
)

or, even better, just do pybamm.standard_spatial_vars.x which will fetch the same defn from standard_spatial_vars.py

@brosaplanella
Copy link
Member

Hi @tommaull! Let me know if you have any questions about the comments above (not sure I was very clear haha).

@tommaull
Copy link
Contributor Author

tommaull commented Oct 4, 2023

Hi @brosaplanella and @rtimms, here's the latest. I've definitely bitten off more than I should have here. Appreciate your help.

for domain in self.domain_params.values():
domain._set_parameters()
for domain, params in self.domain_params.items():
params._domain = domain # Set _domain attribute
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is needed, since T is defined across all domains, not by component

auxiliary_domains={"secondary": "current collector"},
coord_sys="cartesian",
)
# Domain parameters
Copy link
Contributor

Choose a reason for hiding this comment

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

now this can go, for the same reasons as above

# )

self.T_init = pybamm.FunctionParameter(
f"{domain}Initial temperature in {domain} electrode [mol.m-3]",
Copy link
Contributor

Choose a reason for hiding this comment

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

just call it "Initial temperature [K]"

@@ -67,4 +67,4 @@ def set_rhs(self, variables):

def set_initial_conditions(self, variables):
T_vol_av = variables["Volume-averaged cell temperature [K]"]
self.initial_conditions = {T_vol_av: self.param.T_init}
self.initial_conditions = {T_vol_av: pybamm.x_average(self.param.T_init)}
Copy link
Contributor

Choose a reason for hiding this comment

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

will also need to do this in the "x-lumped" models for pouch cell (in the folder submodels/thermal/pouch_cell/)

@rtimms
Copy link
Contributor

rtimms commented Oct 5, 2023

thanks @tommaull it's getting there - just some confusion around the cell temperature being set on the "whole cell" domain instead of separately for negative electrode/separator/positive electrode

@tommaull
Copy link
Contributor Author

Failing tests where non-lumped models are used due to mismatch between eqn and var domains.
image

Seems to be because the variable is set for Negative electrode temperature [K], Separator temperature [K], Positive electrode temperature [K] but the equation just has initial temp.
image

@@ -28,17 +31,39 @@ def __init__(self):

def _set_parameters(self):
"""Defines the dimensional parameters"""
for domain in self.domain_params.values():
domain._set_parameters()
for domain, params in self.domain_params.items():
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should just be

        for domain in self.domain_params.values():
            domain._set_parameters()

@rtimms
Copy link
Contributor

rtimms commented Nov 27, 2023

For the x-full model you need to update the boundary conditions to be

    def set_initial_conditions(self, variables):
        T = variables["Cell temperature [K]"]
        T_cn = variables["Negative current collector temperature [K]"]
        T_cp = variables["Positive current collector temperature [K]"]
        T_init = self.param.T_init
        self.initial_conditions = {
            T_cn: pybamm.boundary_value(T_init, "left"),
            T: T_init,
            T_cp: pybamm.boundary_value(T_init, "right"),
        }

so that the current collector initial temperatures are T_init at the left and right (since T_init is the temperature in the domain neg/sep/pos.

It looks like you'll also need to update the BasicSPM and Basic DFN models since they are written assuming a uniform temperature.

@tommaull
Copy link
Contributor Author

tommaull commented Dec 5, 2023

image
So I've been trying to suss out the reason for the domain mismatch, and it seems to be, at least partially, because the x_average or Xaverage functions only return a value in the secondary domain. I'm not sure I understand why this occurs?

@valentinsulzer
Copy link
Member

domain can be thought of as "the primary spatial dimension along which this variable changes" which is why XAverage has this behavior. T(x,y,z,t) changes primarily along the through-cell (x) dimension, then secondarily along the current collector (y,z) dimension(s), its x-average is T_av(y,z,t) which changes primarily along the current collector dimension. Depending on the geometry the "current collector" dimension might just be 0D. Variables that depend only on time have no spatial dimension hence no domain.

If you want a variable with primary domains matching the original variable, you can re-broadcast

pybamm.PrimaryBroadcast(T_av, ["negative electrode", "separator", "positive electrode"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assign a spacial temperature vector as initial temperatrue field for DFN model
4 participants