Skip to content

Commit

Permalink
#880 converted funciton args to dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottmar93 committed Mar 14, 2020
1 parent be026d7 commit 7de5ed7
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 166 deletions.
47 changes: 25 additions & 22 deletions pybamm/expression_tree/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,21 @@ class FunctionParameter(pybamm.Symbol):
name : str
name of the node
child : :class:`Symbol`
child node
inputs : dict
A dictionary with string keys and :class:`pybamm.Symbol` values representing
the function inputs.
diff_variable : :class:`pybamm.Symbol`, optional
if diff_variable is specified, the FunctionParameter node will be replaced by a
:class:`pybamm.Function` and then differentiated with respect to diff_variable.
Default is None.
description: str
A description of the function.
inputs: list
A list of strings describing the inputs.
outputs: list
A list of string describing the outputs.
"""

def __init__(
self, name, *children, diff_variable=None, inputs=None,
self, name, inputs, diff_variable=None,
):
# assign diff variable
self.diff_variable = diff_variable
children_list = list(children)
children_list = list(inputs.values())

# Turn numbers into scalars
for idx, child in enumerate(children_list):
Expand All @@ -83,16 +78,16 @@ def __init__(
auxiliary_domains=auxiliary_domains,
)

self.inputs = inputs
self.input_names = list(inputs.keys())

@property
def inputs(self):
def input_names(self):
if self._inputs:
for inp in self._inputs:
for inp in self._input_names:
print(inp)

@inputs.setter
def inputs(self, inp=None):
@input_names.setter
def inputs_names(self, inp=None):
if inp:
if inp.__class__ is list:
for i in inp:
Expand All @@ -103,7 +98,7 @@ def inputs(self, inp=None):
else:
raise TypeError("Inputs must be a provided as a list of strings")

self._inputs = inp
self._input_names = inp

def set_id(self):
"""See :meth:`pybamm.Symbol.set_id` """
Expand Down Expand Up @@ -136,19 +131,24 @@ def diff(self, variable):
""" See :meth:`pybamm.Symbol.diff()`. """
# return a new FunctionParameter, that knows it will need to be differentiated
# when the parameters are set
return FunctionParameter(
self.name, *self.orphans, diff_variable=variable, inputs=self._inputs
)
children_list = self.orphans
input_names = self._input_names

input_dict = {input_names[i]: children_list[i] for i in range(len(input_names))}

return FunctionParameter(self.name, input_dict, diff_variable=variable)

def new_copy(self):
""" See :meth:`pybamm.Symbol.new_copy()`. """
return self._function_parameter_new_copy(self.orphans)
return self._function_parameter_new_copy(self.input_names, self.orphans)

def _function_parameter_new_copy(self, children):
def _function_parameter_new_copy(self, input_names, children):
"""Returns a new copy of the function parameter.
Inputs
------
input_names : : list
A list of str of the names of the children/function inputs
children : : list
A list of the children of the function
Expand All @@ -157,8 +157,11 @@ def _function_parameter_new_copy(self, children):
: :pybamm.FunctionParameter
A new copy of the function parameter
"""

input_dict = {input_names[i]: children[i] for i in range(len(input_names))}

return FunctionParameter(
self.name, *children, diff_variable=self.diff_variable, inputs=self._inputs
self.name, input_dict, diff_variable=self.diff_variable
)

def _evaluate_for_shape(self):
Expand Down
2 changes: 1 addition & 1 deletion pybamm/parameters/electrical_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# the user may provide the typical timescale as a parameter.
timescale = pybamm.Parameter("Typical timescale [s]")
dimensional_current_with_time = pybamm.FunctionParameter(
"Current function [A]", pybamm.t * timescale, inputs=["Time [s]"]
"Current function [A]", {"Time[s]": pybamm.t * timescale}
)
dimensional_current_density_with_time = dimensional_current_with_time / (
n_electrodes_parallel * pybamm.geometric_parameters.A_cc
Expand Down
51 changes: 18 additions & 33 deletions pybamm/parameters/standard_parameters_lead_acid.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,19 @@

def D_e_dimensional(c_e, T):
"Dimensional diffusivity in electrolyte"
return pybamm.FunctionParameter(
"Electrolyte diffusivity [m2.s-1]",
c_e,
inputs=["Electrolyte concentration [mol.m-3"],
)
inputs = {"Electrolyte concentration [mol.m-3": c_e}
return pybamm.FunctionParameter("Electrolyte diffusivity [m2.s-1]", inputs)


def kappa_e_dimensional(c_e, T):
"Dimensional electrolyte conductivity"
return pybamm.FunctionParameter(
"Electrolyte conductivity [S.m-1]",
c_e,
inputs=["Electrolyte concentration [mol.m-3]"],
)
inputs = {"Electrolyte concentration [mol.m-3]": c_e}
return pybamm.FunctionParameter("Electrolyte conductivity [S.m-1]", inputs)


def chi_dimensional(c_e):
return pybamm.FunctionParameter(
"Darken thermodynamic factor",
c_e,
inputs=["Electrolyte concentration [mol.m-3]"],
)
inputs = {"Electrolyte concentration [mol.m-3]": c_e}
return pybamm.FunctionParameter("Darken thermodynamic factor", inputs)


def c_w_dimensional(c_e, c_ox=0, c_hy=0):
Expand Down Expand Up @@ -236,43 +227,37 @@ def mu_dimensional(c_e):
"""
Dimensional viscosity of electrolyte [kg.m-1.s-1].
"""
return pybamm.FunctionParameter(
"Electrolyte viscosity [kg.m-1.s-1]",
c_e,
inputs=["Electrolyte concentration [mol.m-3]"],
)
inputs = {"Electrolyte concentration [mol.m-3]": c_e}
return pybamm.FunctionParameter("Electrolyte viscosity [kg.m-1.s-1]", inputs)


def U_n_dimensional(c_e, T):
"Dimensional open-circuit voltage in the negative electrode [V]"
inputs = {"Electrolyte molar mass [mol.kg-1]": m_dimensional(c_e)}
return pybamm.FunctionParameter(
"Negative electrode open-circuit potential [V]",
m_dimensional(c_e),
inputs=["Electrolyte concentration [mol.m-3]"],
"Negative electrode open-circuit potential [V]", inputs
)


def U_p_dimensional(c_e, T):
"Dimensional open-circuit voltage in the positive electrode [V]"
inputs = {"Electrolyte molar mass [mol.kg-1]": m_dimensional(c_e)}
return pybamm.FunctionParameter(
"Positive electrode open-circuit potential [V]",
m_dimensional(c_e),
inputs=["Electrolyte concentration [mol.m-3]"],
"Positive electrode open-circuit potential [V]", inputs
)


D_e_typ = D_e_dimensional(c_e_typ, T_ref)
rho_typ = rho_dimensional(c_e_typ)
mu_typ = mu_dimensional(c_e_typ)

inputs = {"Electrolyte concentration [mol.m-3]": pybamm.Scalar(1)}
U_n_ref = pybamm.FunctionParameter(
"Negative electrode open-circuit potential [V]",
pybamm.Scalar(1),
inputs=["Electrolyte concentration [mol.m-3]"],
"Negative electrode open-circuit potential [V]", inputs
)
inputs = {"Electrolyte concentration [mol.m-3]": pybamm.Scalar(1)}
U_p_ref = pybamm.FunctionParameter(
"Positive electrode open-circuit potential [V]",
pybamm.Scalar(1),
inputs=["Electrolyte concentration [mol.m-3]"],
"Positive electrode open-circuit potential [V]", inputs
)


Expand Down Expand Up @@ -516,7 +501,7 @@ def U_p(c_e_p, T):
# 6. Input current and voltage

dimensional_current_with_time = pybamm.FunctionParameter(
"Current function [A]", pybamm.t * timescale, inputs=["Time [s]"]
"Current function [A]", {"Time [s]": pybamm.t * timescale}
)
dimensional_current_density_with_time = dimensional_current_with_time / (
n_electrodes_parallel * pybamm.geometric_parameters.A_cc
Expand Down
Loading

0 comments on commit 7de5ed7

Please sign in to comment.