

def set_source_term(self, source_name=None, pores=None, x0=None, tol=None, maxiter=None, mode='merge'):
    "\n        Apply source terms to specified pores\n\n        Parameters\n        ----------\n        source_name : string\n          Specifies the name of source term from a Physics object to apply.\n        pores : array_like\n          The pores where the boundary conditions should be applied\n        x0 : array_like, optional\n          By sending guess values for the quantity, the method calculates the\n          source terms and stores them in the algorithm\n        tol : float, optional\n          Tolerance for the iterative method. (if maxiter>0)\n        maxiter : integer, optional\n          Maximum number of iterations for this source term. Iteration will\n          stop after maxiter steps.\n        mode : string, optional\n          Controls how the source terms should be applied.\n          Options are:\n                - 'merge': Inserts specified values, leaving existing values \\\n                  elsewhere.\n                - 'overwrite': Inserts specified values, clearing all other \\\n                  values.\n                - 'remove': Removes boundary conditions from specified \\\n                  locations.\n                - 'update': Allows to insert specified values to new \\\n                  locations, updating existing ones.\n\n        Notes\n        -----\n        Difference between 'merge' and 'update' modes: in the merge, a new\n        value cannot be applied to a pore with existing one, but in the\n        'update' it is possible.\n\n        "
    if (mode not in ['merge', 'overwrite', 'remove', 'update']):
        raise Exception(((('The mode (' + mode) + ') cannot be applied to ') + 'the set_source_term!'))
    if (pores is not None):
        pores = sp.array(pores, ndmin=1)
    if (source_name is not None):
        s_group = sp.array(source_name, ndmin=1)
        for source_name in s_group:
            source_name = ('pore.' + source_name.split('.')[(- 1)])
            prop = source_name.split('.')[(- 1)]
            try:
                self._phase[source_name]
            except KeyError:
                Exception(((((('The attached phase in the algorithm ' + self.name) + ', does not have the source ') + 'property ') + source_name) + ' in its physics!'))
            except ValueError:
                pass
            if (mode == 'remove'):
                s_mode = ['linear', 'nonlinear']
                if (pores is None):
                    try:
                        del self[('pore.source_' + prop)]
                    except KeyError:
                        pass
                    for s in s_mode:
                        try:
                            del self[((('pore.source_' + s) + '_s1_') + prop)]
                        except KeyError:
                            pass
                        try:
                            del self[((('pore.source_' + s) + '_s2_') + prop)]
                        except KeyError:
                            pass
                else:
                    try:
                        self[('pore.source_' + prop)][pores] = False
                    except KeyError:
                        pass
                    for s in s_mode:
                        try:
                            self[((('pore.source_' + s) + '_s1_') + prop)][pores] = sp.nan
                        except KeyError:
                            pass
                        try:
                            self[((('pore.source_' + s) + '_s2_') + prop)][pores] = sp.nan
                        except KeyError:
                            pass
            else:
                if ('pore.source_tol' not in self.props()):
                    self['pore.source_tol'] = (sp.ones((self.Np,), dtype=float) * sp.nan)
                if ('pore.source_maxiter' not in self.props()):
                    maxiter_arr = (sp.ones((self.Np,), dtype=float) * sp.nan)
                    self['pore.source_maxiter'] = maxiter_arr
                if (x0 is None):
                    x0 = 0
                self._guess = x0
                if (maxiter is None):
                    maxiter = int(100)
                    source_mode = 'nonlinear'
                else:
                    try:
                        maxiter = int(maxiter)
                    except (ValueError, TypeError):
                        raise Exception(('input for maxiter cannot be ' + 'converted to integer!'))
                    if (maxiter > 0):
                        source_mode = 'nonlinear'
                    elif (maxiter == 0):
                        source_mode = 'linear'
                if (tol is None):
                    tol = 1e-05
                else:
                    try:
                        tol = float(tol)
                    except (ValueError, TypeError):
                        raise Exception(('input for tol cannot be ' + 'converted to float!'))
                if ((('pore.source_' + prop) not in self.labels()) or (mode == 'overwrite')):
                    self[('pore.source_' + prop)] = sp.zeros((self.Np,), dtype=bool)
                    temp_arr = (sp.ones((self.Np,), dtype=float) * sp.nan)
                    self[((('pore.source_' + source_mode) + '_s1_') + prop)] = temp_arr
                    self[((('pore.source_' + matching_physics) + '_s2_') + prop)] = temp_arr
                matching_physics = [phys for phys in self._phase._physics if (source_name in phys.models.keys())]
                for phys in matching_physics:
                    x = phys.models[source_name]['x']
                    return_rate = phys.models[source_name]['return_rate']
                    regen_mode = phys.models[source_name]['regen_mode']
                    phys.models[source_name]['x'] = x0
                    phys.models[source_name]['return_rate'] = False
                    phys.models[source_name]['regen_mode'] = 'normal'
                    s_regen = phys.models[source_name].run()
                    phys.models[source_name]['x'] = x
                    phys.models[source_name]['return_rate'] = return_rate
                    phys.models[source_name]['regen_mode'] = regen_mode
                    map_pores = phys.map_pores()
                    loc = pores[sp.in1d(pores, map_pores)]
                    if (mode == 'merge'):
                        try:
                            spore = self.pores(('source_' + prop))
                            if (sp.sum(sp.in1d(loc, spore)) > 0):
                                raise Exception('Because of the existing source term, the method cannot apply new source terms with the merge mode to the specified pores.')
                        except KeyError:
                            pass
                    self[('pore.source_' + prop)][loc] = True
                    map_pores_loc = sp.in1d(map_pores, pores)
                    self[((('pore.source_' + source_mode) + '_s1_') + prop)][loc] = s_regen[:, 0][map_pores_loc]
                    self[((('pore.source_' + source_mode) + '_s2_') + prop)][loc] = s_regen[:, 1][map_pores_loc]
                    if (source_mode is not 'linear'):
                        self['pore.source_maxiter'][loc] = maxiter
                        self['pore.source_tol'][loc] = tol
    else:
        raise Exception((('No source_name has been sent for set_source_' + 'term method in the algorithm ') + self.name))

dataset/ETHPy150Open PMEAL/OpenPNM/OpenPNM/Algorithms/__GenericLinearTransport__.py GenericLinearTransport.set_source_term/VarMisuse@799/1652 `source_mode`->`matching_physics`