How to use the petsc4py.PETSc.SNES function in petsc4py

To help you get started, we’ve selected a few petsc4py examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tarcisiofischer / lid_driven_cavity_problem / experiments / test_petsc4py.py View on Github external
for j in range(N):
            mat.setValue(i, j, 0)

    mat.setUp()
    mat.assemble()

    # print(mat.getValuesCSR())

    dm.setMatrix(mat)

    def residual_function(snes, x, f):
        f[0] = (x[0] * x[0] + x[0] * x[1] - 3.0).item()
        f[1] = (x[0] * x[1] + x[1] * x[1] - 6.0).item()
        f.assemble()

    snes = PETSc.SNES().create(comm=COMM)

    r = PETSc.Vec().createSeq(N)
    x = PETSc.Vec().createSeq(N)
    b = PETSc.Vec().createSeq(N)

    snes.setFunction(residual_function, r)

    snes.setDM(dm)
    snes.setUseFD(True)

    x.setArray([2, 3])
    b.set(0)

    snes.setConvergenceHistory()
    snes.setFromOptions()
    snes.solve(b, x)
github sfepy / sfepy / sfepy / solvers / nls.py View on Github external
def __init__(self, conf, pmtx=None, prhs=None, comm=None, **kwargs):
        if comm is None:
            try:
                import petsc4py
                petsc4py.init([])
            except ImportError:
                msg = 'cannot import petsc4py!'
                raise ImportError(msg)

        from petsc4py import PETSc as petsc

        converged_reasons = {}
        for key, val in six.iteritems(petsc.SNES.ConvergedReason.__dict__):
            if isinstance(val, int):
                converged_reasons[val] = key

        ksp_converged_reasons = {}
        for key, val in six.iteritems(petsc.KSP.ConvergedReason.__dict__):
            if isinstance(val, int):
                ksp_converged_reasons[val] = key

        NonlinearSolver.__init__(self, conf, petsc=petsc,
                                 pmtx=pmtx, prhs=prhs, comm=comm,
                                 converged_reasons=converged_reasons,
                                 ksp_converged_reasons=ksp_converged_reasons,
                                 **kwargs)
github tarcisiofischer / lid_driven_cavity_problem / experiments / petsc4py_experiments.py View on Github external
f.assemble()

COMM = PETSc.COMM_WORLD
N = 4
J = PETSc.Mat().createAIJ(N, comm=COMM)
J.setPreallocationNNZ(N)
for i in range(N):
    for j in range(N):
        J.setValue(i, j, 0.0)
J.setUp()
J.assemble()

dm = PETSc.DMShell().create(comm=COMM)
dm.setMatrix(J)

snes = PETSc.SNES().create(comm=COMM)
r = PETSc.Vec().createSeq(N)
x = PETSc.Vec().createSeq(N)
b = PETSc.Vec().createSeq(N)
snes.setFunction(f, r)
snes.setDM(dm)

# This enables coloring
snes.setUseFD(True)

x.setArray([1.1] * N)
b.set(0)

snes.setConvergenceHistory()
snes.setFromOptions()
snes.solve(b, x)
github Parallel-in-Time / pySDC / pySDC / implementations / problem_classes / GeneralizedFisher_1D_PETSc.py View on Github external
# setup linear solver
        self.ksp = PETSc.KSP()
        self.ksp.create(comm=self.params.comm)
        self.ksp.setType('cg')
        pc = self.ksp.getPC()
        pc.setType('ilu')
        self.ksp.setInitialGuessNonzero(True)
        self.ksp.setFromOptions()
        self.ksp.setTolerances(rtol=self.params.lsol_tol, atol=self.params.lsol_tol,
                               max_it=self.params.lsol_maxiter)
        self.ksp_itercount = 0
        self.ksp_ncalls = 0

        # setup nonlinear solver
        self.snes = PETSc.SNES()
        self.snes.create(comm=self.params.comm)
        if self.params.nlsol_maxiter <= 1:
            self.snes.setType('ksponly')
        self.snes.getKSP().setType('cg')
        pc = self.snes.getKSP().getPC()
        pc.setType('ilu')
        # self.snes.setType('ngmres')
        self.snes.setFromOptions()
        self.snes.setTolerances(rtol=self.params.nlsol_tol, atol=self.params.nlsol_tol, stol=self.params.nlsol_tol,
                                max_it=self.params.nlsol_maxiter)
        self.snes_itercount = 0
        self.snes_ncalls = 0
        self.F = self.init.createGlobalVec()
        self.J = self.init.createMatrix()
github mathLab / RBniCS / rbnics / backends / basic / wrapping / petsc_snes_solver.py View on Github external
def __init__(self, problem, solution):
            self.problem = problem
            self.solution = solution
            self.monitor = None
            # Create SNES object
            self.snes = PETSc.SNES().create(wrapping.get_mpi_comm(solution))
            # ... and associate residual and jacobian
            self.snes.setFunction(problem.residual_vector_eval, wrapping.to_petsc4py(problem.residual_vector))
            self.snes.setJacobian(problem.jacobian_matrix_eval, wrapping.to_petsc4py(problem.jacobian_matrix))
            # Set sensible default values to parameters
            self._report = None
            self.set_parameters({
                "report": True
            })
github Parallel-in-Time / pySDC / pySDC / implementations / problem_classes / GrayScott_2D_PETSc_periodic.py View on Github external
self.localX = self.init.createLocalVec()

        # setup linear solver
        self.ksp = PETSc.KSP()
        self.ksp.create(comm=self.params.comm)
        self.ksp.setType('cg')
        pc = self.ksp.getPC()
        pc.setType('none')
        self.ksp.setInitialGuessNonzero(True)
        self.ksp.setFromOptions()
        self.ksp.setTolerances(rtol=self.params.lsol_tol, atol=self.params.lsol_tol, max_it=self.params.lsol_maxiter)
        self.ksp_itercount = 0
        self.ksp_ncalls = 0

        # setup nonlinear solver
        self.snes = PETSc.SNES()
        self.snes.create(comm=self.params.comm)
        # self.snes.getKSP().setType('cg')
        # self.snes.setType('ngmres')
        self.snes.setFromOptions()
        self.snes.setTolerances(rtol=self.params.nlsol_tol, atol=self.params.nlsol_tol, stol=self.params.nlsol_tol,
                                max_it=self.params.nlsol_maxiter)
        self.snes_itercount = 0
        self.snes_ncalls = 0
github Parallel-in-Time / pySDC / pySDC / implementations / problem_classes / GrayScott_2D_PETSc_implicit_periodic.py View on Github external
stencil_width=1, comm=problem_params['comm'])

        # invoke super init, passing number of dofs, dtype_u and dtype_f
        super(petsc_grayscott, self).__init__(init=da, dtype_u=dtype_u, dtype_f=dtype_f, params=problem_params)

        # compute dx, dy and get local ranges
        self.dx = 100.0 / (self.params.nvars[0])
        self.dy = 100.0 / (self.params.nvars[1])
        (self.xs, self.xe), (self.ys, self.ye) = self.init.getRanges()

        # compute discretization matrix A and identity
        self.A = self.__get_A()
        self.localX = self.init.createLocalVec()

        # setup nonlinear solver
        self.snes = PETSc.SNES()
        self.snes.create(comm=self.params.comm)
        # self.snes.getKSP().setType('cg')
        # self.snes.setType('ngmres')
        self.snes.setFromOptions()
        self.snes.setTolerances(rtol=self.params.sol_tol, atol=self.params.sol_tol, stol=self.params.sol_tol,
                                max_it=self.params.sol_maxiter)
github Parallel-in-Time / pySDC / pySDC / implementations / problem_classes / GeneralizedFisher_1D_PETSc_implicit.py View on Github external
super(petsc_fisher, self).__init__(init=da, dtype_u=dtype_u, dtype_f=dtype_f, params=problem_params)

        # compute dx, dy and get local ranges
        self.dx = (self.params.interval[1] - self.params.interval[0]) / (self.params.nvars - 1)
        # print(self.init.getRanges())
        (self.xs, self.xe) = self.init.getRanges()[0]

        # compute discretization matrix A and identity
        self.A = self.__get_A()
        self.localX = self.init.createLocalVec()

        self.ksp_itercount = 0
        self.ksp_ncalls = 0

        # setup nonlinear solver
        self.snes = PETSc.SNES()
        self.snes.create(comm=self.params.comm)
        # self.snes.getKSP().setType('cg')
        # self.snes.setType('ngmres')
        self.snes.setFromOptions()
        self.snes.setTolerances(rtol=self.params.sol_tol, atol=self.params.sol_tol, stol=self.params.sol_tol,
                                max_it=self.params.sol_maxiter)
        self.snes_itercount = 0
        self.snes_ncalls = 0