Beispiel #1
0
    def solve_general(self, ctx, eq, fixed):
        def conc(Ef, numeric_parameters):
            for s in eq.with_dos:
                yield s, s.dos.concentrationv(ctx, s, None, Ef, numeric_parameters=numeric_parameters)
        nfixed = nvalue(fixed)

        def f(Ef, numeric_parameters=True):
            if numeric_parameters:
                charge = nfixed
            else:
                charge = fixed
            for eq, c in conc(Ef, numeric_parameters):
                charge = charge + c*eq.z
            return charge
        bandv = np.asarray([nvalue(ctx.varsOf(s)['Ebandv'])
                            for s in eq.with_dos])
        a = np.amax(bandv)
        b = np.amin(bandv)
        Ef_value = brent(f, a, b, xtol=self.etol, maxiter=50)
        g = f(Ef_value, False)
        if isinstance(g, forward.value):
            raise NotImplementedError('not tested')
            dg = f(forward.seed(Ef_value), True).deriv
            if not isscalar(dg):
                dg = dg.tocsr().diagonal()
            # must create value : (Ef_value, 1/dg*g')
            Ef = custom_function(lambda *args: Ef_value, lambda *args: dg)(g)
        else:
            Ef = Ef_value
        info = dict((id(eq), c) for eq, c in conc(Ef, False))
        return Ef, info
Beispiel #2
0
def OnsagerFunction(b):
    """
    Onsager function of real variable J1(2 \sqrt(-2b))/sqrt(-2b), b>=0
    """
    def f(x):
        return scipy.special.i1(2. * x) / x

    def df(args, f):
        x, = args
        u = 2. * x
        yield lambda: 2. * (scipy.special.i0(u) - f) / x

    return ad.custom_function(f, df)(np.sqrt(2. * b + 1e-10))
Beispiel #3
0
    def _value(self, a, b):
        v = self._nevaluate(ad.nvalue(a),
                            ad.nvalue(b),
                            need_value=True,
                            need_da=isinstance(a, ad.forward.value),
                            need_db=isinstance(b, ad.forward.value))

        def _I(a, b):
            return v['value']

        def _I_deriv(args, value):
            yield lambda: v['da']
            yield lambda: v['db']

        return ad.custom_function(_I, _I_deriv)(a, b)
Beispiel #4
0
    def _dvdb(self, a, b):
        v = self._nevaluate(ad.nvalue(a),
                            ad.nvalue(b),
                            need_value=False,
                            need_db=True,
                            need_d2ab=isinstance(a, ad.forward.value),
                            need_d2bb=isinstance(b, ad.forward.value))

        def _dIdb(a, b):
            return v['db']

        def _dIdb_deriv(args, value):
            yield lambda: v['d2ab']
            yield lambda: v['d2bb']

        return ad.custom_function(_dIdb, _dIdb_deriv)(a, b)
Beispiel #5
0
    def dIdb(self, a, b):
        v = self._nevaluate(ad.nvalue(a),
                            ad.nvalue(b),
                            need_value=True,
                            need_da=isinstance(a, ad.forward.value),
                            need_db=True,
                            need_d2ab=isinstance(a, ad.forward.value),
                            need_d2bb=isinstance(b, ad.forward.value))
        i = ad.exp(v['value'])

        def _dIdb(a, _):
            return i * v['db']

        def _dIdb_deriv(args, value):
            yield lambda: i * (v['d2ab'] + v['da'] * v['db'])
            yield lambda: i * (v['d2bb'] + v['db'] * v['db'])

        return ad.custom_function(_dIdb, _dIdb_deriv)(a, b)
Beispiel #6
0
    def _b(self, a, v, b0=None):
        b = self._nsolve_b(ad.nvalue(a), ad.nvalue(v), b0=b0)

        def _b(a, v):
            return b

        def _b_deriv(args, value):
            a_, i_ = args
            b = value
            values = self._nevaluate(a_,
                                     b,
                                     need_da=isinstance(a, ad.forward.value),
                                     need_db=True)
            dbdI = 1. / values['db']
            yield lambda: -dbdI * values['da']
            yield lambda: dbdI

        return ad.custom_function(_b, _b_deriv)(a, v)