<commit_before>import openpnm as op
import scipy as sp
from numpy.testing import assert_allclose


class AdvectionDiffusionTest:

    def setup_class(self):
        sp.random.seed(0)
        self.net = op.network.Cubic(shape=[4, 3, 1], spacing=1.0)
        self.geo = op.geometry.GenericGeometry(network=self.net,
                                               pores=self.net.Ps,
                                               throats=self.net.Ts)
        self.geo['throat.conduit_lengths.pore1'] = 0.1
        self.geo['throat.conduit_lengths.throat'] = 0.6
        self.geo['throat.conduit_lengths.pore2'] = 0.1

        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['throat.hydraulic_conductance'] = 1e-15

        self.sf = op.algorithms.StokesFlow(network=self.net, phase=self.phase)
        self.sf.set_value_BC(pores=self.net.pores('back'), values=1)
        self.sf.set_value_BC(pores=self.net.pores('front'), values=0)
        self.sf.run()

        self.phase.update(self.sf.results())

        self.ad = op.algorithms.AdvectionDiffusion(network=self.net,
                                                   phase=self.phase)
        self.ad.settings.update({"cache_A": False, "cache_b": False})
        self.ad.set_value_BC(pores=self.net.pores('back'), values=2)
        self.ad.set_value_BC(pores=self.net.pores('front'), values=0)

    def test_conductance_automatically_gets_updated_after_StokesFlow(self):
        r"""
        This test ensures that ad_dif_conductance automatically gets updated
        when calling AdvectionDiffusion.run(), otherwise, a uniform pressure
        field will be used inside ad_dif_conductance method. Alternatively,
        one can manually call regenerate_models() after running StokesFlow,
        which was painful, hence ad_dif_conductance is now treated as an
        iterative_prop, so it automatically gets updated in the process.
        """
        # Add ad_dif pore-scale model prior to running StokesFlow
        mod = op.models.physics.ad_dif_conductance.ad_dif
        self.phys.add_model(propname='throat.ad_dif_conductance',
                            model=mod, s_scheme='powerlaw')
        # Set up and run StokesFlow
        sf = op.algorithms.StokesFlow(network=self.net, phase=self.phase)
        sf.set_value_BC(pores=self.net.pores("back"), values=2)
        sf.set_value_BC(pores=self.net.pores("front"), values=0.0)
        sf.run()
        self.phase.update(sf.results())
        # Set up and run AdvectionDiffusion
        fd = op.algorithms.AdvectionDiffusion(network=self.phase, phase=self.phase)
        fd.set_value_BC(pores=self.net.pores("back"), values=1.0)
        fd.set_value_BC(pores=self.net.pores("front"), values=0.0)
        fd.run()
        # Verify that the new pressure field has been used
        c = fd["pore.concentration"]
        c_desired = [0., 0., 0.,
                     0.56164952, 0.56164952, 0.56164952,
                     0.85096674, 0.85096674, 0.85096674,
                     1., 1., 1.]
        assert_allclose(actual=c, desired=c_desired)
        self.setup_class()

    def test_powerlaw_advection_diffusion(self):
        mod = op.models.physics.ad_dif_conductance.ad_dif
        self.phys.add_model(propname='throat.ad_dif_conductance_powerlaw',
                            model=mod, s_scheme='powerlaw')
        self.phys.regenerate_models()

        self.ad.setup(conductance='throat.ad_dif_conductance_powerlaw')
        self.ad.run()
        x = [0., 0., 0.,
             0.89653, 0.89653, 0.89653,
             1.53924, 1.53924, 1.53924,
             2., 2., 2.]<commit_msg>Bugfix in recently added test for conductance as iterative prop<commit_after>import scipy as sp
import openpnm as op
from numpy.testing import assert_allclose


class AdvectionDiffusionTest:

    def setup_class(self):
        sp.random.seed(0)
        self.net = op.network.Cubic(shape=[4, 3, 1], spacing=1.0)
        self.geo = op.geometry.GenericGeometry(network=self.net,
                                               pores=self.net.Ps,
                                               throats=self.net.Ts)
        self.geo['throat.conduit_lengths.pore1'] = 0.1
        self.geo['throat.conduit_lengths.throat'] = 0.6
        self.geo['throat.conduit_lengths.pore2'] = 0.1

        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['throat.hydraulic_conductance'] = 1e-15

        self.sf = op.algorithms.StokesFlow(network=self.net, phase=self.phase)
        self.sf.set_value_BC(pores=self.net.pores('back'), values=1)
        self.sf.set_value_BC(pores=self.net.pores('front'), values=0)
        self.sf.run()

        self.phase.update(self.sf.results())

        self.ad = op.algorithms.AdvectionDiffusion(network=self.net,
                                                   phase=self.phase)
        self.ad.settings.update({"cache_A": False, "cache_b": False})
        self.ad.set_value_BC(pores=self.net.pores('back'), values=2)
        self.ad.set_value_BC(pores=self.net.pores('front'), values=0)

    def test_conductance_gets_updated_when_pressure_changes(self):
        mod = op.models.physics.ad_dif_conductance.ad_dif
        self.phase['pore.p'] = self.phase['pore.pressure'].copy()
        self.phys.add_model(propname='throat.ad_dif_conductance',
                            model=mod, s_scheme='powerlaw',
                            pore_pressure='pore.p')
        g_old = self.phys["throat.ad_dif_conductance"]
        # Manually change pressure field
        self.phase['pore.p'] = self.phase['pore.p']**0.5
        self.ad.run()
        g_updated = self.phys["throat.ad_dif_conductance"]
        # Ensure conductance values are updated
        assert g_old.mean() != g_updated.mean()
        assert_allclose(g_updated.mean(), 1.0071212e-15)

    def test_powerlaw_advection_diffusion(self):
        mod = op.models.physics.ad_dif_conductance.ad_dif
        self.phys.add_model(propname='throat.ad_dif_conductance_powerlaw',
                            model=mod, s_scheme='powerlaw')
        self.phys.regenerate_models()

        self.ad.setup(conductance='throat.ad_dif_conductance_powerlaw')
        self.ad.run()
        x = [0., 0., 0.,
             0.89653, 0.89653, 0.89653,
             1.53924, 1.53924, 1.53924,
             2., 2., 2.]
{'fraction_non_alphanumeric': 0.11834579850417949, 'fraction_numerical': 0.030942953512245196, 'mean_word_length': 2.148661126500462, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1453140', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2159, 'n_words': 329}