How to use petsc - 10 common examples

To help you get started, we’ve selected a few petsc 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 firedrakeproject / firedrake / firedrake / slate / slac.py View on Github external
klist = []
    transformkernel = Transformer()
    oriented = False
    for v in kernel_exprs.values():
        for ks in v:
            oriented = oriented or ks.kinfo.oriented
            # TODO: Think about this. Is this true for SLATE?
            assert ks.kinfo.subdomain_id == "otherwise"
            kast = transformkernel.visit(ks.kinfo.kernel._ast)
            klist.append(kast)

    klist.append(kernel)
    kernelast = ast.Node(klist)

    # Need to get the correct PETSc directory
    inc.append(petsc.get_petsc_dir() + '/include/eigen3/')

    # Produce the op2 kernel object for assembly
    op2kernel = op2.Kernel(kernelast,
                           "slac_compile_slate",
                           cpp=True,
                           include_dirs=inc,
                           headers=['#include ', '#define restrict __restrict'])

    # TODO: What happens for multiple ufl domains?
    assert len(slate_expr.ufl_domains()) == 1
    kinfo = KernelInfo(kernel=op2kernel,
                       integral_type="cell",
                       oriented=oriented,
                       subdomain_id="otherwise",
                       domain_number=0,
                       coefficient_map=range(len(coeffs)),
github firedrakeproject / firedrake / setup.py View on Github external
def get_petsc_dir():
    try:
        petsc_arch = env.get('PETSC_ARCH', '')
        petsc_dir = env['PETSC_DIR']
        if petsc_arch:
            return (petsc_dir, path.join(petsc_dir, petsc_arch))
        return (petsc_dir,)
    except KeyError:
        try:
            import petsc
            return (petsc.get_petsc_dir(), )
        except ImportError:
            sys.exit("""Error: Could not find PETSc library.
github OP2 / PyOP2 / setup.py View on Github external
def get_petsc_dir():
    try:
        arch = '/' + env.get('PETSC_ARCH', '')
        dir = env['PETSC_DIR']
        return (dir, dir + arch)
    except KeyError:
        try:
            import petsc
            return (petsc.get_petsc_dir(), )
        except ImportError:
            sys.exit("""Error: Could not find PETSc library.
github OP2 / PyOP2 / pyop2 / utils.py View on Github external
def get_petsc_dir():
    try:
        arch = '/' + os.environ.get('PETSC_ARCH', '')
        dir = os.environ['PETSC_DIR']
        return (dir, dir + arch)
    except KeyError:
        try:
            import petsc
            return (petsc.get_petsc_dir(), )
        except ImportError:
            sys.exit("""Error: Could not find PETSc library.
github firedrakeproject / firedrake / setup.py View on Github external
def get_petsc_dir():
    try:
        petsc_arch = env.get('PETSC_ARCH', '')
        petsc_dir = env['PETSC_DIR']
        if petsc_arch:
            return (petsc_dir, path.join(petsc_dir, petsc_arch))
        return (petsc_dir,)
    except KeyError:
        try:
            import petsc
            return (petsc.get_petsc_dir(), )
        except ImportError:
            sys.exit("""Error: Could not find PETSc library.
github firedrakeproject / firedrake / firedrake / solving_utils.py View on Github external
def update_parameters(obj, petsc_obj):
    """Update parameters on a petsc object

    :arg obj: An object with a parameters dict (mapping to petsc options).
    :arg petsc_obj: The PETSc object to set parameters on."""
    # Skip if parameters haven't changed
    if hasattr(obj, '_set_parameters') and obj.parameters == obj._set_parameters:
        return
    opts = PETSc.Options(obj._opt_prefix)
    for k, v in obj.parameters.iteritems():
        if type(v) is bool:
            if v:
                opts[k] = None
        else:
            opts[k] = v
    petsc_obj.setFromOptions()
    obj._set_parameters = obj.parameters.copy()
github firedrakeproject / firedrake / firedrake / dmplex.py View on Github external
def _from_cell_list(dim, cells, coords, comm=None):
    """
    Create a DMPlex from a list of cells and coords.

    :arg dim: The topological dimension of the mesh
    :arg cells: The vertices of each cell
    :arg coords: The coordinates of each vertex
    :arg comm: An optional communicator to build the plex on (defaults to COMM_WORLD)
    """

    if comm is None:
        comm = MPI.comm
    if comm.rank == 0:
        # Provide the actual data on rank 0.
        return PETSc.DMPlex().createFromCellList(dim, cells, coords, comm=comm)

    # Provide empty plex on other ranks
    # A subsequent call to plex.distribute() takes care of parallel partitioning
    return PETSc.DMPlex().createFromCellList(dim,
                                             np.zeros((0, 0), dtype=np.int32),
                                             np.zeros((0, 0), dtype=np.int32),
                                             comm=comm)
github firedrakeproject / firedrake / firedrake / solving_utils.py View on Github external
opts[k] = None
        else:
            opts[k] = v
    petsc_obj.setFromOptions()
    obj._set_parameters = obj.parameters.copy()


def _make_reasons(reasons):
    return dict([(getattr(reasons, r), r)
                 for r in dir(reasons) if not r.startswith('_')])


KSPReasons = _make_reasons(PETSc.KSP.ConvergedReason())


SNESReasons = _make_reasons(PETSc.SNES.ConvergedReason())


def check_snes_convergence(snes):
    r = snes.getConvergedReason()
    try:
        reason = SNESReasons[r]
        inner = False
    except KeyError:
        r = snes.getKSP().getConvergedReason()
        try:
            reason = KSPReasons[r]
            inner = True
        except KeyError:
            reason = 'unknown reason (petsc4py enum incomplete?)'
    if r < 0:
        if inner:
github firedrakeproject / firedrake / firedrake / solving_utils.py View on Github external
for k, v in obj.parameters.iteritems():
        if type(v) is bool:
            if v:
                opts[k] = None
        else:
            opts[k] = v
    petsc_obj.setFromOptions()
    obj._set_parameters = obj.parameters.copy()


def _make_reasons(reasons):
    return dict([(getattr(reasons, r), r)
                 for r in dir(reasons) if not r.startswith('_')])


KSPReasons = _make_reasons(PETSc.KSP.ConvergedReason())


SNESReasons = _make_reasons(PETSc.SNES.ConvergedReason())


def check_snes_convergence(snes):
    r = snes.getConvergedReason()
    try:
        reason = SNESReasons[r]
        inner = False
    except KeyError:
        r = snes.getKSP().getConvergedReason()
        try:
            reason = KSPReasons[r]
            inner = True
        except KeyError:
github firedrakeproject / firedrake / firedrake / dmplex.py View on Github external
def permute_global_numbering(plex):
    """Permute the global/universal DoF numbering according to a
    depth-first traversal of the Plex graph."""
    dim = plex.getDimension()
    glbl = plex.getDefaultSection()
    univ = plex.getDefaultGlobalSection()
    pStart, pEnd = glbl.getChart()

    entity_classes = [0, 0, 0, 0]
    permutation = -1 * np.ones(pEnd-pStart, dtype=np.int)
    glbl_num = 0

    # Create new numbering sections
    glbl_new = PETSc.Section().create()
    glbl_new.setChart(pStart, pEnd)
    glbl_new.setUp()
    univ_new = PETSc.Section().create()
    univ_new.setChart(pStart, pEnd)
    univ_new.setUp()

    # Get a list of current universal DoFs
    universal_dofs = []
    for p in range(pStart, pEnd):
        for c in range(univ.getDof(p)):
            universal_dofs.append(univ.getOffset(p)+c)

    # Renumber core DoFs
    seen = set()
    if plex.getStratumSize("op2_core", dim) > 0:
        for cell in plex.getStratumIS("op2_core", dim).getIndices():

petsc

PETSc: Portable, Extensible Toolkit for Scientific Computation

BSD-2-Clause
Latest version published 21 days ago

Package Health Score

60 / 100
Full package analysis