From f493de2eaf333bd49d13aa4ef588bbe662bb3b72 Mon Sep 17 00:00:00 2001 From: Dr Maurice Hendrix <52317399+MauriceHendrix@users.noreply.github.com> Date: Thu, 20 Jan 2022 16:16:21 +0000 Subject: [PATCH] performance improvement to linearity check for rush-larsen using caching (#224) * performance improvement to linearity check for rush-larsen using caching --- CITATION.cff | 2 +- RELEASE.md | 3 +++ chaste_codegen/_linearity_check.py | 5 ++++- chaste_codegen/version.txt | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CITATION.cff b/CITATION.cff index 044d9dc9f..c55f9d3b4 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -23,7 +23,7 @@ authors: given-names: "Gary R." title: "chaste-codegen" -version: 0.9.1 +version: 0.9.3 doi: 10.5281/zenodo.5517000 date-released: 2021-09-20 url: "https://github.com/ModellingWebLab/chaste-codegen" diff --git a/RELEASE.md b/RELEASE.md index 0a065db34..0cead5b78 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,3 +1,6 @@ +# Release 0.9.3 +- Performance upgrade for `--rush-larsen` using caching on linearity checking. + # Release 0.9.2 - Corrected a typo in command line argument `--skip-ingularity-fixes` renaming it to `--skip-singularity-fixes` diff --git a/chaste_codegen/_linearity_check.py b/chaste_codegen/_linearity_check.py index 44bb2aada..7aab3ca05 100644 --- a/chaste_codegen/_linearity_check.py +++ b/chaste_codegen/_linearity_check.py @@ -1,4 +1,5 @@ from enum import Enum +from functools import lru_cache from cellmlmanip.model import Variable from sympy import ( @@ -26,6 +27,7 @@ class KINDS(Enum): NONLINEAR = 3 +@lru_cache(maxsize=128) def _check_expr(expr, state_var, membrane_voltage_var, state_vars): """Check the kind of expression given (NONE, LINEAR or NONLINEAR) @@ -38,6 +40,7 @@ def _check_expr(expr, state_var, membrane_voltage_var, state_vars): :param state_vars: the state variables in the model the expression comes from :return: the kind of expr (NONE, LINEAR or NONLINEAR) """ + @lru_cache(maxsize=128) def max_kind(state_var, operands): result = KINDS.NONE for op in operands: @@ -126,7 +129,7 @@ def get_non_linear_state_vars(derivative_equations, membrane_voltage_var, state_ return set([eq.lhs.args[0] for eq in derivative_equations if isinstance(eq.lhs, Derivative) and eq.lhs.args[0] != membrane_voltage_var and - _check_expr(eq.rhs, eq.lhs.args[0], membrane_voltage_var, state_vars) != KINDS.LINEAR]) + _check_expr(eq.rhs, eq.lhs.args[0], membrane_voltage_var, tuple(state_vars)) != KINDS.LINEAR]) def subst_deriv_eqs_non_linear_vars(y_derivatives, non_linear_state_vars, membrane_voltage_var, state_vars, diff --git a/chaste_codegen/version.txt b/chaste_codegen/version.txt index 2003b639c..965065db5 100644 --- a/chaste_codegen/version.txt +++ b/chaste_codegen/version.txt @@ -1 +1 @@ -0.9.2 +0.9.3