From 49cf40a348c0ba1f4e3aff6ff1d05d7a74b2bae1 Mon Sep 17 00:00:00 2001
From: "Frank T. Bergmann" <fbergman@caltech.edu>
Date: Thu, 19 May 2022 15:41:30 +0200
Subject: [PATCH] - fix crash when ending eqn on species

---
 basico/model_info.py             | 2 +-
 tests/test_model_manipulation.py | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/basico/model_info.py b/basico/model_info.py
index 8abf68a..396a648 100644
--- a/basico/model_info.py
+++ b/basico/model_info.py
@@ -3711,7 +3711,7 @@ def _tokenize_eqn(eqn):
                 var = eqn[i + 1:pos]
                 var_is_species = True
                 i = pos + 1
-                c_0 = eqn[i]
+                c_0 = eqn[i] if i  < num_chars else None
                 c_1 = eqn[i + 1] if i + 1 < num_chars else None
 
                 if c_0 == '_' and c_1 == '0':
diff --git a/tests/test_model_manipulation.py b/tests/test_model_manipulation.py
index 02ba993..cfe4bf0 100644
--- a/tests/test_model_manipulation.py
+++ b/tests/test_model_manipulation.py
@@ -106,9 +106,10 @@ def test_add_ode(self):
             'd[CEX]/dt=(k_2 [E][PGME])/K_s -([E]/((k_4 [7-ADCA])/K_n +(k_5 [7-ADCA])/K_n +(k_6 [PGME])/K_si +k3))((k_2 [PGME])/K_s   +(k_4b [CEX])/K_p )(k_3+(k_5 [7-ADCA])/K_n )')
         basico.add_equation(
             '[E]=([EA]_0 exp(-k_d t))/(1+([PGME])/K_s + ((k_2 [PGME])/(K_s ((k_4 [7-ADCA])/K_n +(k_5 [7-ADCA])/K_n +(k_6 [PGME])/K_si +k3) ))(1+([7-ADCA])/K_n +([PGME])/K_si )+([CEX])/K_p +([PG])/K_p2i )')
-
+        basico.add_equation('d[S1]/dt=-r_s1*w_1*[B]')
         species = basico.get_species()
         self.assertEqual(species.loc['PG'].type, 'ode')
+        self.assertEqual(species.loc['S1'].type, 'ode')
         self.assertEqual(species.loc['PG'].expression, '[E] / ( Values[k_4] * [7-ADCA] / Values[K_n] + Values[k_5] * [7-ADCA] / Values[K_n] + Values[k_6] * [PGME] / Values[K_si] + Values[k3] ) * ( Values[k_2] * [PGME] / Values[K_s] + Values[k_4b] * [CEX] / Values[K_p] ) * ( Values[k_3] + Values[k_5] * [7-ADCA] / Values[K_n] + Values[k_6] * [PGME] / Values[K_si] )')
         self.assertEqual(species.loc['E'].type, 'assignment')
         self.assertEqual(species.loc['E'].expression, '[EA]_0 * exp ( - Values[k_d] * Time ) / ( 1 + [PGME] / Values[K_s] + Values[k_2] * [PGME] / ( Values[K_s] * ( Values[k_4] * [7-ADCA] / Values[K_n] + Values[k_5] * [7-ADCA] / Values[K_n] + Values[k_6] * [PGME] / Values[K_si] + Values[k3] ) ) * ( 1 + [7-ADCA] / Values[K_n] + [PGME] / Values[K_si] ) + [CEX] / Values[K_p] + [PG] / Values[K_p2i] )')