Repository: PMEAL/OpenPNM

Issue:
Allow passing scalar values as argument to built-in source term models
Something like:
```python
mod = op.models.physics.generic_source_term.standard_kinetics
k, n = 1e-4, 2
phys.add_model(
    X="pore.concentration",
    propname="pore.rxn",
    model=mod, prefactor=k, exponent=n,
    regen_mode="deferred"
)
```

Discussion:

Patch:
diff --git a/openpnm/core/_base.py b/openpnm/core/_base.py
index 3fac92c54..3152d64ab 100644
--- a/openpnm/core/_base.py
+++ b/openpnm/core/_base.py
@@ -182,6 +182,11 @@ class Base(dict):
                 raise Exception('Provided array is wrong length for ' + key)
 
     def __getitem__(self, key):
+        # If the key is a just a numerical value, the kick it directly back
+        # This allows one to do either value='pore.blah' or value=1.0
+        if isinstance(key, (int, float, bool, complex)):
+            return key
+
         element, prop = key.split('.', 1)
 
         if key in self.keys():
diff --git a/openpnm/core/_mixins.py b/openpnm/core/_mixins.py
index 83eb8704e..7fde4490d 100644
--- a/openpnm/core/_mixins.py
+++ b/openpnm/core/_mixins.py
@@ -15,6 +15,11 @@ class ParamMixin:
         self._params._value = "Values"
 
     def __getitem__(self, key):
+        # If the key is a just a numerical value, the kick it directly back
+        # This allows one to do either value='pore.blah' or value=1.0
+        if isinstance(key, (int, float, bool, complex)):
+            return key
+
         if key.startswith('param'):
             try:
                 vals = self._params[key]
diff --git a/openpnm/core/_subdomain.py b/openpnm/core/_subdomain.py
index fd2acc56f..a88362bbc 100644
--- a/openpnm/core/_subdomain.py
+++ b/openpnm/core/_subdomain.py
@@ -19,6 +19,11 @@ class Subdomain(Base, LabelMixin):
     """
 
     def __getitem__(self, key):
+        # If the key is a just a numerical value, the kick it directly back
+        # This allows one to do either value='pore.blah' or value=1.0
+        if isinstance(key, (int, float, bool, complex)):
+            return key
+
         element = key.split('.')[0]
         # Try to get vals directly first
         vals = self.get(key)
diff --git a/openpnm/network/_generic.py b/openpnm/network/_generic.py
index 1232b0e42..767ca840a 100644
--- a/openpnm/network/_generic.py
+++ b/openpnm/network/_generic.py
@@ -121,6 +121,11 @@ class GenericNetwork(ParamMixin, Base, ModelsMixin, LabelMixin):
         super().__setitem__(key, value)
 
     def __getitem__(self, key):
+        # If the key is a just a numerical value, the kick it directly back
+        # This allows one to do either value='pore.blah' or value=1.0
+        if isinstance(key, (int, float, bool, complex)):
+            return key
+
         element, prop = key.split('.', 1)
         # Deal with special keys first
         if key.split('.')[-1] == self.name:
diff --git a/openpnm/phases/_generic.py b/openpnm/phases/_generic.py
index d69ef8206..ccf2bee0a 100644
--- a/openpnm/phases/_generic.py
+++ b/openpnm/phases/_generic.py
@@ -48,6 +48,10 @@ class GenericPhase(ParamMixin, Base, ModelsMixin, LabelMixin):
         self['pore.pressure'] = 101325.0
 
     def __getitem__(self, key):
+        # If the key is a just a numerical value, the kick it directly back
+        # This allows one to do either value='pore.blah' or value=1.0
+        if isinstance(key, (int, float, bool, complex)):
+            return key
         element, prop = key.split('.', 1)
         # Deal with special keys first
         if prop == '_id':


Test patch:
diff --git a/tests/unit/core/BaseTest.py b/tests/unit/core/BaseTest.py
index c66304e21..482f323d8 100644
--- a/tests/unit/core/BaseTest.py
+++ b/tests/unit/core/BaseTest.py
@@ -699,6 +699,16 @@ class BaseTest:
         with pytest.raises(KeyError):
             _ = self.geo['pore.blah']
 
+    def test_getitem_with_numeric_value(self):
+        assert self.geo[5.0] == 5.0
+        assert self.net[44] == 44
+        assert self.phase1[True] == True
+        assert self.phase2[False] == False
+        assert self.phys1[0.1] == 0.1
+        assert self.phys2[4j] == 4j
+        a = op.algorithms.GenericTransport(network=self.net, phase=self.phase1)
+        assert a[0] == 0
+
     def test_interpolate_data(self):
         self.geo['throat.tester'] = np.linspace(0, 1.0, self.geo.network.Nt)
         self.geo['pore.tester'] = np.linspace(0, 1.0, self.geo.network.Np)