import openpnm as op
import scipy as sp
import pytest


class GenericTransportTest:

    def setup_class(self):
        self.net = op.network.Cubic(shape=[9, 9, 9])
        self.geo = op.geometry.GenericGeometry(network=self.net,
                                               pores=self.net.Ps,
                                               throats=self.net.Ts)
        self.phase = op.phases.GenericPhase(network=self.net)
        self.phys = op.physics.GenericPhysics(network=self.net,
                                              phase=self.phase,
                                              geometry=self.geo)
        self.phys['throat.diffusive_conductance'] = 1e-15
        self.phys['pore.A'] = -1e-10
        self.phys['pore.k'] = 2
        mod = op.models.physics.generic_source_term.standard_kinetics
        self.phys.add_model(propname='pore.reaction',
                            model=mod,
                            prefactor='pore.A',
                            exponent='pore.k',
                            quantity='pore.concentration',
                            regen_mode='normal')

    def test_one_value_one_source(self):
        rt = op.algorithms.ReactiveTransport(network=self.net,
                                             phase=self.phase)
        rt.setup(rxn_tolerance=1e-05, max_iter=5000,
                 relaxation_source=1, relaxation_quantity=1)
        rt.settings.update({'conductance': 'throat.diffusive_conductance',
                            'quantity': 'pore.concentration'})
        rt.set_source(pores=self.net.pores('bottom'), propname='pore.reaction')
        rt.set_value_BC(pores=self.net.pores('top'), values=1.0)
        rt.run()
        x = [0.0011, 0.1260, 0.2508, 0.3757,
             0.5006, 0.6254, 0.7503, 0.8751, 1.0]
        y = sp.unique(sp.around(rt['pore.concentration'], decimals=4))
        assert sp.all(x == y)

    def test_source_over_BCs(self):
        rt = op.algorithms.ReactiveTransport(network=self.net,
                                             phase=self.phase)
        rt.settings.update({'conductance': 'throat.diffusive_conductance',
                            'quantity': 'pore.concentration'})
        rt.set_value_BC(pores=self.net.pores('left'), values=1.0)
        rt.set_value_BC(pores=self.net.pores('right'), values=0.5)
        with pytest.raises(Exception):
            rt.set_source(pores=self.net.Ps, propname='pore.reaction')

    def test_BCs_over_source(self):
        rt = op.algorithms.ReactiveTransport(network=self.net,
                                             phase=self.phase)
        rt.settings.update({'conductance': 'throat.diffusive_conductance',
                            'quantity': 'pore.concentration'})
        rt.set_source(pores=self.net.pores('left'), propname='pore.reaction')
        with pytest.raises(Exception):
            rt.set_value_BC(pores=self.net.pores('left'), values=1.0)

    def teardown_class(self):
        ws = op.Workspace()
        ws.clear()


if __name__ == '__main__':

    t = GenericTransportTest()
    t.setup_class()
    for item in t.__dir__():
        if item.startswith('test'):
            print('running test: '+item)
            t.__getattribute__(item)()
    self = t

@@ -1,12 +1,26 @@
+import pytest%0A
 import openp
@@ -32,40 +32,49 @@
  op%0A
-import scipy as sp%0Aimport pytest
+from numpy.testing import assert_allclose
 %0A%0A%0Ac
@@ -78,23 +78,24 @@
 %0A%0Aclass 
-Generic
+Reactive
 Transpor
@@ -729,17 +729,17 @@
  = -1e-1
-0
+5
 %0A       
@@ -771,19 +771,28 @@
         
-mod
+std_kinetics
  = op.mo
@@ -899,46 +899,27 @@
 on',
-%0A                            model=mod
+ model=std_kinetics
 ,%0A  
@@ -967,36 +967,8 @@
 .A',
-%0A                           
  exp
@@ -1086,14 +1086,16 @@
 de='
-normal
+deferred
 ')%0A%0A
@@ -1299,10 +1299,10 @@
 =1e-
+1
 0
-5
 , ma
@@ -1350,16 +1350,18 @@
 source=1
+.0
 , relaxa
@@ -1375,16 +1375,18 @@
 antity=1
+.0
 )%0A      
@@ -1691,127 +1691,51 @@
     
-x
+c_mean_desired
  = 
-%5B
 0.
-0011, 0.1260, 0.2508, 0.3757,%0A             0.5006, 0.6254, 0.7503, 0.8751, 1.0%5D%0A        y = sp.unique(sp.around(
+648268%0A        c_mean = 
 rt%5B'
@@ -1758,21 +1758,14 @@
 on'%5D
-, decimals=4)
+.mean(
 )%0A  
@@ -1780,22 +1780,51 @@
 sert
- sp.all(x == y
+_allclose(c_mean, c_mean_desired, rtol=1e-6
 )%0A%0A 
@@ -2335,19 +2335,67 @@
 elf.net.
-Ps,
+pores(%5B%22left%22, %22right%22%5D),%0A                         
  propnam
@@ -2902,16 +2902,553 @@
 s=1.0)%0A%0A
+    def test_source_over_source(self):%0A        rt = op.algorithms.ReactiveTransport(network=self.net,%0A                                             phase=self.phase)%0A        rt.settings.update(%7B'conductance': 'throat.diffusive_conductance',%0A                            'quantity': 'pore.concentration'%7D)%0A        rt.set_source(pores=self.net.pores('left'), propname='pore.reaction')%0A        with pytest.raises(Exception):%0A            rt.set_source(pores=self.net.pores(%22left%22),%0A                          propname='pore.another_reaction')%0A%0A
     def 
@@ -3554,23 +3554,24 @@
     t = 
-Generic
+Reactive
 Transpor

Add test for check for duplicate source term in same locs + minor refac.