Ejemplo n.º 1
0
    def solve(self, state0=None, nls_status=None, ls_conf=None, nls_conf=None, force_values=None, var_data=None):
        """Solve self.equations in current time step.

        Parameters
        ----------
        var_data : dict
            A dictionary of {variable_name : data vector} used to initialize
            parameter variables.
        """
        solvers = self.get_solvers()
        if solvers is None:
            self.init_solvers(nls_status, ls_conf, nls_conf)
            solvers = self.get_solvers()

        if state0 is None:
            state0 = State(self.equations.variables)

        else:
            if isinstance(state0, nm.ndarray):
                state0 = State(self.equations.variables, vec=state0)

        self.equations.set_data(var_data, ignore_unknown=True)

        self.update_materials()
        state0.apply_ebc(force_values=force_values)

        vec0 = state0.get_reduced()

        self.nls_status = get_default(nls_status, self.nls_status)
        vec = solvers.nls(vec0, status=self.nls_status)

        state = state0.copy(preserve_caches=True)
        state.set_reduced(vec, preserve_caches=True)

        return state
Ejemplo n.º 2
0
    def __init__(self, conf, problem, **kwargs):
        from sfepy.discrete.state import State

        ScipyDirect.__init__(self, conf, **kwargs)

        equations = problem.equations
        aux_state = State(equations.variables)

        conf.idxs = {}
        for bk, bv in conf.blocks.iteritems():
            aux_state.fill(0.0)
            for jj in bv:
                idx = equations.variables.di.indx[jj]
                aux_state.vec[idx] = nm.nan

            aux_state.apply_ebc()
            vec0 = aux_state.get_reduced()
            conf.idxs[bk] = nm.where(nm.isnan(vec0))[0]
Ejemplo n.º 3
0
Archivo: ls.py Proyecto: LeiDai/sfepy
    def __init__(self, conf, **kwargs):
        from sfepy.discrete.state import State

        ScipyDirect.__init__(self, conf, **kwargs)

        equations = self.problem.equations
        aux_state = State(equations.variables)

        conf.idxs = {}
        for bk, bv in conf.blocks.iteritems():
            aux_state.fill(0.0)
            for jj in bv:
                idx = equations.variables.di.indx[jj]
                aux_state.vec[idx] = nm.nan

            aux_state.apply_ebc()
            vec0 = aux_state.get_reduced()
            conf.idxs[bk] = nm.where(nm.isnan(vec0))[0]
Ejemplo n.º 4
0
    def solve(self, state0=None, nls_status=None,
              ls_conf=None, nls_conf=None, force_values=None,
              var_data=None):
        """Solve self.equations in current time step.

        Parameters
        ----------
        var_data : dict
            A dictionary of {variable_name : data vector} used to initialize
            parameter variables.
        """
        solvers = self.get_solvers()
        if solvers is None:
            self.init_solvers(nls_status, ls_conf, nls_conf)
            solvers = self.get_solvers()

        if state0 is None:
            state0 = State(self.equations.variables)

        else:
            if isinstance(state0, nm.ndarray):
                state0 = State(self.equations.variables, vec=state0)

        self.equations.set_data(var_data, ignore_unknown=True)

        self.update_materials()
        state0.apply_ebc(force_values=force_values)

        vec0 = state0.get_reduced()

        self.nls_status = get_default(nls_status, self.nls_status)
        vec = solvers.nls(vec0, status=self.nls_status)

        state = state0.copy(preserve_caches=True)
        state.set_reduced(vec, preserve_caches=True)

        return state
Ejemplo n.º 5
0
    def __call__(self,
                 rhs,
                 x0=None,
                 conf=None,
                 eps_a=None,
                 eps_r=None,
                 i_max=None,
                 mtx=None,
                 status=None,
                 **kwargs):
        from sfepy.discrete.state import State

        equations = self.context.equations
        aux_state = State(equations.variables)

        mtxi = {}
        for bk, bv in six.iteritems(conf.blocks):
            aux_state.fill(0.0)
            for jj in bv:
                idx = equations.variables.di.indx[jj]
                aux_state.vec[idx] = nm.nan

            aux_state.apply_ebc()
            vec0 = aux_state.get_reduced()
            mtxi[bk] = nm.where(nm.isnan(vec0))[0]

        mtxslc_s = {}
        mtxslc_f = {}
        nn = {}

        for ik, iv in six.iteritems(mtxi):
            ptr = 0
            nn[ik] = len(iv)
            mtxslc_s[ik] = []
            mtxslc_f[ik] = []
            while ptr < nn[ik]:
                idx0 = iv[ptr:]
                idxrange = nm.arange(idx0[0], idx0[0] + len(idx0))
                aux = nm.where(idx0 == idxrange)[0]
                mtxslc_s[ik].append(slice(ptr + aux[0], ptr + aux[-1] + 1))
                mtxslc_f[ik].append(slice(idx0[aux][0], idx0[aux][-1] + 1))
                ptr += aux[-1] + 1

        mtxs = {}
        rhss = {}
        ress = {}
        get_sub = mtx._get_submatrix
        for ir in six.iterkeys(mtxi):
            rhss[ir] = nm.zeros((nn[ir], ), dtype=nm.float64)
            ress[ir] = nm.zeros((nn[ir], ), dtype=nm.float64)
            for jr, idxr in enumerate(mtxslc_f[ir]):
                rhss[ir][mtxslc_s[ir][jr]] = rhs[idxr]

            for ic in six.iterkeys(mtxi):
                mtxid = '%s%s' % (ir, ic)
                mtxs[mtxid] = nm.zeros((nn[ir], nn[ic]), dtype=nm.float64)
                for jr, idxr in enumerate(mtxslc_f[ir]):
                    for jc, idxc in enumerate(mtxslc_f[ic]):
                        iir = mtxslc_s[ir][jr]
                        iic = mtxslc_s[ic][jc]
                        mtxs[mtxid][iir, iic] = get_sub(idxr, idxc).todense()

        self.orig_conf.function(ress, mtxs, rhss, nn)

        res = nm.zeros_like(rhs)
        for ir in six.iterkeys(mtxi):
            for jr, idxr in enumerate(mtxslc_f[ir]):
                res[idxr] = ress[ir][mtxslc_s[ir][jr]]

        return res
Ejemplo n.º 6
0
    def test_ls_reuse(self):
        import numpy as nm
        from sfepy.solvers import Solver
        from sfepy.discrete.state import State

        self.problem.init_solvers(ls_conf=self.problem.solver_confs['d00'])
        nls = self.problem.get_nls()

        state0 = State(self.problem.equations.variables)
        state0.apply_ebc()
        vec0 = state0.get_reduced()

        self.problem.update_materials()

        rhs = nls.fun(vec0)
        mtx = nls.fun_grad(vec0)

        ok = True
        for name in ['i12', 'i01']:
            solver_conf = self.problem.solver_confs[name]
            method = solver_conf.get('method', '')
            precond = solver_conf.get('precond', '')
            name = ' '.join((solver_conf.name, solver_conf.kind, method,
                             precond)).rstrip()
            self.report(name)
            try:
                ls = Solver.any_from_conf(solver_conf)

            except:
                self.report('skipped!')
                continue

            conf = ls.conf.copy()
            conf.force_reuse = True

            sol00 = ls(rhs, mtx=mtx, conf=conf)
            digest00 = ls.mtx_digest

            sol0 = ls(rhs, mtx=mtx)
            digest0 = ls.mtx_digest

            sol1 = ls(rhs, mtx=2 * mtx, conf=conf)
            digest1 = ls.mtx_digest

            sol2 = ls(rhs, mtx=2 * mtx)
            digest2 = ls.mtx_digest
            ls(rhs, mtx=2 * mtx)
            digest3 = ls.mtx_digest

            _ok = digest00 != digest0
            self.report(digest00, '!=', digest0, ':', _ok)
            ok = ok and _ok
            _ok = digest0 == digest1
            self.report(digest0, '==', digest1, ':', _ok)
            ok = ok and _ok
            _ok = digest1 != digest2
            self.report(digest1, '!=', digest2, ':', _ok)
            ok = ok and _ok
            _ok = digest2[1] == digest3[1]
            self.report(digest2[1], '==', digest3[1], ':', _ok)
            ok = ok and _ok
            _ok = nm.allclose(sol00, sol0, atol=1e-12, rtol=0.0)
            self.report('sol00 == sol0:', _ok)
            ok = ok and _ok
            _ok = nm.allclose(sol0, sol1, atol=1e-12, rtol=0.0)
            self.report('sol0 == sol1:', _ok)
            ok = ok and _ok
            _ok = nm.allclose(sol0, 2 * sol2, atol=1e-12, rtol=0.0)
            self.report('sol0 == 2 * sol2:', _ok)
            ok = ok and _ok

        return ok
Ejemplo n.º 7
0
Archivo: ls.py Proyecto: lokik/sfepy
    def __call__(self, rhs, x0=None, conf=None, eps_a=None, eps_r=None,
                 i_max=None, mtx=None, status=None, **kwargs):
        from sfepy.discrete.state import State

        equations = self.context.equations
        aux_state = State(equations.variables)

        mtxi = {}
        for bk, bv in six.iteritems(conf.blocks):
            aux_state.fill(0.0)
            for jj in bv:
                idx = equations.variables.di.indx[jj]
                aux_state.vec[idx] = nm.nan

            aux_state.apply_ebc()
            vec0 = aux_state.get_reduced()
            mtxi[bk] = nm.where(nm.isnan(vec0))[0]

        mtxslc_s = {}
        mtxslc_f = {}
        nn = {}

        for ik, iv in six.iteritems(mtxi):
            ptr = 0
            nn[ik] = len(iv)
            mtxslc_s[ik] = []
            mtxslc_f[ik] = []
            while ptr < nn[ik]:
                idx0 = iv[ptr:]
                idxrange = nm.arange(idx0[0], idx0[0] + len(idx0))
                aux = nm.where(idx0 == idxrange)[0]
                mtxslc_s[ik].append(slice(ptr + aux[0], ptr + aux[-1] + 1))
                mtxslc_f[ik].append(slice(idx0[aux][0], idx0[aux][-1] + 1))
                ptr += aux[-1] + 1

        mtxs = {}
        rhss = {}
        ress = {}
        get_sub = mtx._get_submatrix
        for ir in six.iterkeys(mtxi):
            rhss[ir] = nm.zeros((nn[ir],), dtype=nm.float64)
            ress[ir] = nm.zeros((nn[ir],), dtype=nm.float64)
            for jr, idxr in enumerate(mtxslc_f[ir]):
                rhss[ir][mtxslc_s[ir][jr]] = rhs[idxr]

            for ic in six.iterkeys(mtxi):
                mtxid = '%s%s' % (ir, ic)
                mtxs[mtxid] = nm.zeros((nn[ir], nn[ic]), dtype=nm.float64)
                for jr, idxr in enumerate(mtxslc_f[ir]):
                    for jc, idxc in enumerate(mtxslc_f[ic]):
                        iir = mtxslc_s[ir][jr]
                        iic = mtxslc_s[ic][jc]
                        mtxs[mtxid][iir, iic] = get_sub(idxr, idxc).todense()

        self.orig_conf.function(ress, mtxs, rhss, nn)

        res = nm.zeros_like(rhs)
        for ir in six.iterkeys(mtxi):
            for jr, idxr in enumerate(mtxslc_f[ir]):
                res[idxr] = ress[ir][mtxslc_s[ir][jr]]

        return res
Ejemplo n.º 8
0
    def test_ls_reuse(self):
        import numpy as nm
        from sfepy.solvers import Solver
        from sfepy.discrete.state import State

        self.problem.init_solvers(ls_conf=self.problem.solver_confs['d00'])
        nls = self.problem.get_nls()

        state0 = State(self.problem.equations.variables)
        state0.apply_ebc()
        vec0 = state0.get_reduced()

        self.problem.update_materials()

        rhs = nls.fun(vec0)
        mtx = nls.fun_grad(vec0)

        ok = True
        for name in ['i12', 'i01']:
            solver_conf = self.problem.solver_confs[name]
            method = solver_conf.get('method', '')
            precond = solver_conf.get('precond', '')
            name = ' '.join((solver_conf.name, solver_conf.kind,
                             method, precond)).rstrip()
            self.report(name)
            try:
                ls = Solver.any_from_conf(solver_conf)

            except:
                self.report('skipped!')
                continue

            conf = ls.conf.copy()
            conf.force_reuse = True

            sol00 = ls(rhs, mtx=mtx, conf=conf)
            digest00 = ls.mtx_digest

            sol0 = ls(rhs, mtx=mtx)
            digest0 = ls.mtx_digest

            sol1 = ls(rhs, mtx=2*mtx, conf=conf)
            digest1 = ls.mtx_digest

            sol2 = ls(rhs, mtx=2*mtx)
            digest2 = ls.mtx_digest
            ls(rhs, mtx=2*mtx)
            digest3 = ls.mtx_digest

            _ok = digest00 != digest0
            self.report(digest00, '!=', digest0, ':', _ok); ok = ok and _ok
            _ok = digest0 == digest1
            self.report(digest0, '==', digest1, ':', _ok); ok = ok and _ok
            _ok = digest1 != digest2
            self.report(digest1, '!=', digest2, ':', _ok); ok = ok and _ok
            _ok = digest2[1] == digest3[1]
            self.report(digest2[1], '==', digest3[1], ':', _ok); ok = ok and _ok
            _ok = nm.allclose(sol00, sol0, atol=1e-12, rtol=0.0)
            self.report('sol00 == sol0:', _ok); ok = ok and _ok
            _ok = nm.allclose(sol0, sol1, atol=1e-12, rtol=0.0)
            self.report('sol0 == sol1:', _ok); ok = ok and _ok
            _ok = nm.allclose(sol0, 2 * sol2, atol=1e-12, rtol=0.0)
            self.report('sol0 == 2 * sol2:', _ok); ok = ok and _ok

        return ok