Exemple #1
0
def DCoeff(*args, **kwargs):
    class DCoeff(MatrixPhysCoefficient):
       def __init__(self, *args, **kwargs):
           self.conj = kwargs.pop('conj', False)       
           self.space_dim = args[0]
           super(DCoeff, self).__init__(*args, **kwargs)

       def EvalValue(self, x):
           from petram.phys.phys_model import Coefficient_Evaluator
           val = Coefficient_Evaluator.EvalValue(self, x)
           val = np.diag(val)
           if self.conj: val=np.conj(val)
           return val
    e = args[1]
    if any([isinstance(ee, str) for ee in e]):
        return DCoeff(*args, **kwargs)
    else:
        e = np.diag(e)       
        conj = kwargs.get('conj', False)
        real = kwargs.get('real', True)
        if np.iscomplexobj(e):
            if conj:  e = np.conj(e)
            if real:  e = e.real
            else: e = e.imag
        else:
            e = np.array(e, dtype=float, copy=False)
        return PhysMatrixConstant(e)
def DCoeff(dim, exprs, ind_vars, l, g, **kwargs):
    if isinstance(exprs, str):
        exprs = [exprs]
    if isinstance(exprs, NativeCoefficientGenBase):
        exprs = [exprs]

    class DCoeff(MatrixPhysCoefficient):
        def __init__(self, *args, **kwargs):
            self.conj = kwargs.pop('conj', False)
            self.scale = kwargs.pop('scale', 1.0)
            self.space_dim = args[0]
            super(DCoeff, self).__init__(*args, **kwargs)

        def EvalValue(self, x):
            from petram.phys.phys_model import Coefficient_Evaluator
            val = Coefficient_Evaluator.EvalValue(self, x)
            val = np.diag(val)
            val = val * self.scale
            if self.conj:
                val = np.conj(val)

            if np.iscomplexobj(val):
                if self.real:
                    return val.real
                else:
                    return val.imag
            elif not self.real:
                return val * 0.0
            else:
                return val

    conj = kwargs.get('conj', False)
    real = kwargs.get('real', True)
    scale = kwargs.get('scale', 1.0)

    #print("matrix exprs", exprs)

    if any([isinstance(ee, str) for ee in exprs]):
        return DCoeff(dim, exprs, ind_vars, l, g, **kwargs)
    else:
        e = exprs

        if isinstance(e[0], NativeCoefficientGenBase):
            return call_nativegen(e[0], l, g, real, conj, scale)

        e = e * scale
        e = np.diag(e)
        if np.iscomplexobj(e):
            if conj:
                e = np.conj(e)
            if real:
                e = e.real
            else:
                e = e.imag
        elif not real:
            e = np.array(e * 0.0, dtype=float, copy=False)
        else:
            e = np.array(e, dtype=float, copy=False)
        return PhysMatrixConstant(e)
Exemple #3
0
def MCoeff(*args, **kwargs):
    class MCoeff(MatrixPhysCoefficient):
       def __init__(self, *args, **kwargs):
           self.conj = kwargs.pop('conj', False)
           super(MCoeff, self).__init__(*args, **kwargs)
       def EvalValue(self, x):
           val = super(MCoeff, self).EvalValue(x)
           if self.conj: val=np.conj(val)
           return val
    e = args[1]
    if any([isinstance(ee, str) for ee in e]):
        return MCoeff(*args, **kwargs)
    else:
        conj = kwargs.get('conj', False)
        real = kwargs.get('real', True)
        if np.iscomplexobj(e):
            if conj:  e = np.conj(e)
            if real:  e = e.real
            else: e = e.imag
        else:
            e = np.array(e, dtype=float, copy=False)
        return PhysMatrixConstant(e)
 def get_imag_coefficient(self):
     return PhysMatrixConstant(self.value.imag)
 def get_real_coefficient(self):
     return PhysMatrixConstant(self.value.real)
def MCoeff(dim, exprs, ind_vars, l, g, return_complex=False, **kwargs):
    if isinstance(exprs, str):
        exprs = [exprs]
    if isinstance(exprs, NativeCoefficientGenBase):
        exprs = [exprs]

    class MCoeff_Base(object):
        def __init__(self, conj=False, scale=1.0):
            self.conj = conj
            self.scale = scale

        def proc_value(self, val):
            val = val * self.scale
            if self.conj:
                val = np.conj(val)
            return val

    class MCoeff(MatrixPhysCoefficient, MCoeff_Base):
        def __init__(self,
                     sdim,
                     exprs,
                     ind_vars,
                     l,
                     g,
                     scale=1.0,
                     conj=False,
                     **kwargs):
            MCoeff_Base.__init__(self, conj=conj, scale=scale)
            MatrixPhysCoefficient.__init__(self, sdim, exprs, ind_vars, l, g,
                                           **kwargs)

        def EvalValue(self, x):
            val = super(MCoeff, self).EvalValue(x)
            val = self.proc_value(val)

            if np.iscomplexobj(val):
                if self.real:
                    return val.real
                else:
                    return val.imag
            elif not self.real:
                return val * 0.0
            else:
                return val

    class MCoeffCC(Coefficient_Evaluator, MCoeff_Base,
                   PyComplexMatrixCoefficient):
        def __init__(self,
                     c1,
                     c2,
                     exprs,
                     ind_vars,
                     l,
                     g,
                     conj=False,
                     scale=1.0,
                     **kwargs):
            MCoeff_Base.__init__(self, conj=conj, scale=scale)
            # real is not used...
            Coefficient_Evaluator.__init__(self,
                                           exprs,
                                           ind_vars,
                                           l,
                                           g,
                                           real=True)
            PyComplexMatrixCoefficient.__init__(self, c1, c2)

        def Eval(self, K, T, ip):
            for n, v in self.variables:
                v.set_point(T, ip, self.g, self.l)
            x = T.Transform(ip)
            val = Coefficient_Evaluator.EvalValue(self, x)
            val = val.reshape(self.height, self.width)
            return self.proc_value(val)

    conj = kwargs.get('conj', False)
    real = kwargs.get('real', True)
    scale = kwargs.get('scale', 1.0)

    if any([isinstance(ee, str) for ee in exprs]):
        if return_complex:
            kwargs['real'] = True
            c1 = MCoeff(dim, exprs, ind_vars, l, g, **kwargs)
            kwargs['real'] = False
            c2 = MCoeff(dim, exprs, ind_vars, l, g, **kwargs)
            return MCoeffCC(c1, c2, exprs, ind_vars, l, g, **kwargs)
        else:
            return MCoeff(dim, exprs, ind_vars, l, g, **kwargs)
    else:
        e = exprs

        if isinstance(e[0], NativeCoefficientGenBase):
            if return_complex:
                c1 = call_nativegen(e[0], l, g, True, conj, scale)
                c2 = call_nativegen(e[0], l, g, False, conj, scale)
                return complex_coefficient_from_real_and_imag(c1, c2)
            else:
                return call_nativegen(e[0], l, g, real, conj, scale)

        e = np.array(e, copy=False).reshape(dim, dim)
        e = e * scale
        if conj:
            e = np.conj(e)

        if return_complex:
            e = e.astype(complex)
            return PyComplexMatrixConstant(e)
        else:
            if np.iscomplexobj(e):
                if real:
                    e = e.real
                else:
                    e = e.imag
            elif not real:
                e = np.array(e * 0.0, dtype=float, copy=False)
            else:
                e = np.array(e, dtype=float, copy=False)

            return PhysMatrixConstant(e)