Skip to content

Commit

Permalink
#899 rob comments
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 26, 2020
2 parents 0e8fdde + 998b2c6 commit f47b4b8
Show file tree
Hide file tree
Showing 28 changed files with 216 additions and 162 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

## Bug fixes

- Fixed bug raised if function returns a scalar ([#919](https://github.com/pybamm-team/PyBaMM/pull/919))
- Fixed event handling in `ScipySolver` ([#905](https://github.com/pybamm-team/PyBaMM/pull/905))
- Made input handling clearer in solvers ([#905](https://github.com/pybamm-team/PyBaMM/pull/905))
- Updated Getting started notebook 2 ([#903](https://github.com/pybamm-team/PyBaMM/pull/903))
Expand All @@ -28,7 +29,7 @@

## Breaking changes

- Changed keyword argument `u` for inputs (when evaluating an object) to `params` ([#905](https://github.com/pybamm-team/PyBaMM/pull/905))
- Changed keyword argument `u` for inputs (when evaluating an object) to `inputs` ([#905](https://github.com/pybamm-team/PyBaMM/pull/905))
- Removed "set external temperature" and "set external potential" options. Use "external submodels" option instead ([#862](https://github.com/pybamm-team/PyBaMM/pull/862))

# [v0.2.0](https://github.com/pybamm-team/PyBaMM/tree/v0.2.0) - 2020-02-26
Expand Down
46 changes: 46 additions & 0 deletions CODE-OF-CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# PyBaMM Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
4 changes: 2 additions & 2 deletions examples/notebooks/parameter-values.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@
"d = pybamm.InputParameter(\"d\")\n",
"expr = 2 + d\n",
"expr_eval = parameter_values.process_symbol(expr)\n",
"print(\"with d = {}, {} = {}\".format(3, expr_eval, expr_eval.evaluate(params={\"d\": 3})))\n",
"print(\"with d = {}, {} = {}\".format(5, expr_eval, expr_eval.evaluate(params={\"d\": 5})))"
"print(\"with d = {}, {} = {}\".format(3, expr_eval, expr_eval.evaluate(inputs={\"d\": 3})))\n",
"print(\"with d = {}, {} = {}\".format(5, expr_eval, expr_eval.evaluate(inputs={\"d\": 5})))"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,19 +1019,19 @@ def check_initial_conditions(self, model):
# Individual
for var, eqn in model.initial_conditions.items():
assert isinstance(
eqn.evaluate(t=0, params="shape test"), np.ndarray
eqn.evaluate(t=0, inputs="shape test"), np.ndarray
), pybamm.ModelError(
"""
initial_conditions must be numpy array after discretisation but they are
{} for variable '{}'.
""".format(
type(eqn.evaluate(t=0, params="shape test")), var
type(eqn.evaluate(t=0, inputs="shape test")), var
)
)
# Concatenated
assert (
type(
model.concatenated_initial_conditions.evaluate(t=0, params="shape test")
model.concatenated_initial_conditions.evaluate(t=0, inputs="shape test")
)
is np.ndarray
), pybamm.ModelError(
Expand Down
2 changes: 1 addition & 1 deletion pybamm/expression_tree/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ def new_copy(self):
self.entries_string,
)

def _base_evaluate(self, t=None, y=None, y_dot=None, params=None):
def _base_evaluate(self, t=None, y=None, y_dot=None, inputs=None):
""" See :meth:`pybamm.Symbol._base_evaluate()`. """
return self._entries
10 changes: 5 additions & 5 deletions pybamm/expression_tree/binary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,23 @@ def _binary_new_copy(self, left, right):
"Default behaviour for new_copy"
return self.__class__(left, right)

def evaluate(self, t=None, y=None, y_dot=None, params=None, known_evals=None):
def evaluate(self, t=None, y=None, y_dot=None, inputs=None, known_evals=None):
""" See :meth:`pybamm.Symbol.evaluate()`. """
if known_evals is not None:
id = self.id
try:
return known_evals[id], known_evals
except KeyError:
left, known_evals = self.left.evaluate(t, y, y_dot, params, known_evals)
left, known_evals = self.left.evaluate(t, y, y_dot, inputs, known_evals)
right, known_evals = self.right.evaluate(
t, y, y_dot, params, known_evals
t, y, y_dot, inputs, known_evals
)
value = self._binary_evaluate(left, right)
known_evals[id] = value
return value, known_evals
else:
left = self.left.evaluate(t, y, y_dot, params)
right = self.right.evaluate(t, y, y_dot, params)
left = self.left.evaluate(t, y, y_dot, inputs)
right = self.right.evaluate(t, y, y_dot, inputs)
return self._binary_evaluate(left, right)

def _evaluate_for_shape(self):
Expand Down
6 changes: 3 additions & 3 deletions pybamm/expression_tree/concatenations.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ def _concatenation_evaluate(self, children_eval):
else:
return self.concatenation_function(children_eval)

def evaluate(self, t=None, y=None, y_dot=None, params=None, known_evals=None):
def evaluate(self, t=None, y=None, y_dot=None, inputs=None, known_evals=None):
""" See :meth:`pybamm.Symbol.evaluate()`. """
children = self.cached_children
if known_evals is not None:
if self.id not in known_evals:
children_eval = [None] * len(children)
for idx, child in enumerate(children):
children_eval[idx], known_evals = child.evaluate(
t, y, y_dot, params, known_evals
t, y, y_dot, inputs, known_evals
)
known_evals[self.id] = self._concatenation_evaluate(children_eval)
return known_evals[self.id], known_evals
else:
children_eval = [None] * len(children)
for idx, child in enumerate(children):
children_eval[idx] = child.evaluate(t, y, y_dot, params)
children_eval[idx] = child.evaluate(t, y, y_dot, inputs)
return self._concatenation_evaluate(children_eval)

def new_copy(self):
Expand Down
6 changes: 3 additions & 3 deletions pybamm/expression_tree/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@ def _function_jac(self, children_jacs):

return jacobian

def evaluate(self, t=None, y=None, y_dot=None, params=None, known_evals=None):
def evaluate(self, t=None, y=None, y_dot=None, inputs=None, known_evals=None):
""" See :meth:`pybamm.Symbol.evaluate()`. """
if known_evals is not None:
if self.id not in known_evals:
evaluated_children = [None] * len(self.children)
for i, child in enumerate(self.children):
evaluated_children[i], known_evals = child.evaluate(
t, y, y_dot, params, known_evals=known_evals
t, y, y_dot, inputs, known_evals=known_evals
)
known_evals[self.id] = self._function_evaluate(evaluated_children)
return known_evals[self.id], known_evals
else:
evaluated_children = [
child.evaluate(t, y, y_dot, params) for child in self.children
child.evaluate(t, y, y_dot, inputs) for child in self.children
]
return self._function_evaluate(evaluated_children)

Expand Down
2 changes: 1 addition & 1 deletion pybamm/expression_tree/independent_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def new_copy(self):
""" See :meth:`pybamm.Symbol.new_copy()`. """
return Time()

def _base_evaluate(self, t=None, y=None, y_dot=None, params=None):
def _base_evaluate(self, t=None, y=None, y_dot=None, inputs=None):
""" See :meth:`pybamm.Symbol._base_evaluate()`. """
if t is None:
raise ValueError("t must be provided")
Expand Down
16 changes: 8 additions & 8 deletions pybamm/expression_tree/input_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ def _jac(self, variable):
""" See :meth:`pybamm.Symbol._jac()`. """
return pybamm.Scalar(0)

def _base_evaluate(self, t=None, y=None, y_dot=None, params=None):
# u should be a dictionary
def _base_evaluate(self, t=None, y=None, y_dot=None, inputs=None):
# inputs should be a dictionary
# convert 'None' to empty dictionary for more informative error
if params is None:
params = {}
if not isinstance(params, dict):
if inputs is None:
inputs = {}
if not isinstance(inputs, dict):
# if the special input "shape test" is passed, just return 1
if params == "shape test":
if inputs == "shape test":
return 1
raise TypeError("inputs u should be a dictionary")
raise TypeError("inputs should be a dictionary")
try:
return params[self.name]
return inputs[self.name]
# raise more informative error if can't find name in dict
except KeyError:
raise KeyError("Input parameter '{}' not found".format(self.name))
26 changes: 13 additions & 13 deletions pybamm/expression_tree/operations/convert_to_casadi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, casadi_symbols=None):

pybamm.citations.register("Andersson2019")

def convert(self, symbol, t, y, y_dot, params):
def convert(self, symbol, t, y, y_dot, inputs):
"""
This function recurses down the tree, converting the PyBaMM expression tree to
a CasADi expression tree
Expand All @@ -28,8 +28,8 @@ def convert(self, symbol, t, y, y_dot, params):
A casadi symbol representing state vectors
y_dot : :class:`casadi.MX`
A casadi symbol representing time derivatives of state vectors
params : dict
A dictionary of casadi symbols representing inputs
inputs : dict
A dictionary of casadi symbols representing parameters
Returns
-------
Expand All @@ -39,14 +39,14 @@ def convert(self, symbol, t, y, y_dot, params):
try:
return self._casadi_symbols[symbol.id]
except KeyError:
# Change u to empty dictionary if it's None
params = params or {}
casadi_symbol = self._convert(symbol, t, y, y_dot, params)
# Change inputs to empty dictionary if it's None
inputs = inputs or {}
casadi_symbol = self._convert(symbol, t, y, y_dot, inputs)
self._casadi_symbols[symbol.id] = casadi_symbol

return casadi_symbol

def _convert(self, symbol, t, y, y_dot, params):
def _convert(self, symbol, t, y, y_dot, inputs):
""" See :meth:`CasadiConverter.convert()`. """
if isinstance(
symbol,
Expand All @@ -58,7 +58,7 @@ def _convert(self, symbol, t, y, y_dot, params):
pybamm.ExternalVariable,
),
):
return casadi.MX(symbol.evaluate(t, y, y_dot, params))
return casadi.MX(symbol.evaluate(t, y, y_dot, inputs))

elif isinstance(symbol, pybamm.StateVector):
if y is None:
Expand All @@ -73,8 +73,8 @@ def _convert(self, symbol, t, y, y_dot, params):
elif isinstance(symbol, pybamm.BinaryOperator):
left, right = symbol.children
# process children
converted_left = self.convert(left, t, y, y_dot, params)
converted_right = self.convert(right, t, y, y_dot, params)
converted_left = self.convert(left, t, y, y_dot, inputs)
converted_right = self.convert(right, t, y, y_dot, inputs)

if isinstance(symbol, pybamm.Minimum):
return casadi.fmin(converted_left, converted_right)
Expand All @@ -85,14 +85,14 @@ def _convert(self, symbol, t, y, y_dot, params):
return symbol._binary_evaluate(converted_left, converted_right)

elif isinstance(symbol, pybamm.UnaryOperator):
converted_child = self.convert(symbol.child, t, y, y_dot, params)
converted_child = self.convert(symbol.child, t, y, y_dot, inputs)
if isinstance(symbol, pybamm.AbsoluteValue):
return casadi.fabs(converted_child)
return symbol._unary_evaluate(converted_child)

elif isinstance(symbol, pybamm.Function):
converted_children = [
self.convert(child, t, y, y_dot, params) for child in symbol.children
self.convert(child, t, y, y_dot, inputs) for child in symbol.children
]
# Special functions
if symbol.function == np.min:
Expand Down Expand Up @@ -123,7 +123,7 @@ def _convert(self, symbol, t, y, y_dot, params):
return symbol._function_evaluate(converted_children)
elif isinstance(symbol, pybamm.Concatenation):
converted_children = [
self.convert(child, t, y, y_dot, params) for child in symbol.children
self.convert(child, t, y, y_dot, inputs) for child in symbol.children
]
if isinstance(symbol, (pybamm.NumpyConcatenation, pybamm.SparseStack)):
return casadi.vertcat(*converted_children)
Expand Down
4 changes: 2 additions & 2 deletions pybamm/expression_tree/operations/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def find_symbols(symbol, constant_symbols, variable_symbols):
symbol_str = "t"

elif isinstance(symbol, pybamm.InputParameter):
symbol_str = "params['{}']".format(symbol.name)
symbol_str = "inputs['{}']".format(symbol.name)

else:
raise NotImplementedError(
Expand Down Expand Up @@ -269,7 +269,7 @@ def __init__(self, symbol):
self._result_var, "return" + self._result_var, "eval"
)

def evaluate(self, t=None, y=None, y_dot=None, params=None, known_evals=None):
def evaluate(self, t=None, y=None, y_dot=None, inputs=None, known_evals=None):
"""
Acts as a drop-in replacement for :func:`pybamm.Symbol.evaluate`
"""
Expand Down
2 changes: 1 addition & 1 deletion pybamm/expression_tree/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def set_id(self):
(self.__class__, self.name) + tuple(self.domain) + tuple(str(self._value))
)

def _base_evaluate(self, t=None, y=None, y_dot=None, params=None):
def _base_evaluate(self, t=None, y=None, y_dot=None, inputs=None):
""" See :meth:`pybamm.Symbol._base_evaluate()`. """
return self._value

Expand Down
4 changes: 2 additions & 2 deletions pybamm/expression_tree/state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def __init__(
auxiliary_domains=auxiliary_domains,
evaluation_array=evaluation_array)

def _base_evaluate(self, t=None, y=None, y_dot=None, params=None):
def _base_evaluate(self, t=None, y=None, y_dot=None, inputs=None):
""" See :meth:`pybamm.Symbol._base_evaluate()`. """
if y is None:
raise TypeError("StateVector cannot evaluate input 'y=None'")
Expand Down Expand Up @@ -294,7 +294,7 @@ def __init__(
auxiliary_domains=auxiliary_domains,
evaluation_array=evaluation_array)

def _base_evaluate(self, t=None, y=None, y_dot=None, params=None):
def _base_evaluate(self, t=None, y=None, y_dot=None, inputs=None):
""" See :meth:`pybamm.Symbol._base_evaluate()`. """
if y_dot is None:
raise TypeError("StateVectorDot cannot evaluate input 'y_dot=None'")
Expand Down
Loading

0 comments on commit f47b4b8

Please sign in to comment.