This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to set Petsc Options from command line?

+2 votes

Hello everyone,

How can I set command-line options for Petsc with Fenics (python-version)

I tried parameters.parse() at the start of my program and doing e.g. python bb.py --petsc.ksp_monitor <some_more_arguments_for_my_program>. But I see no output on the screen. Instead, my input-file (which is one of the other arguments) gets overwritten and has some ksp-residuals in it (so at least something seems to work, but it's not exactly the behaviour i'd expect). Btw, I am using argparse to handle my other arguments.

--petsc.ksp_type gmres simply has no effect at all

Ultimately, I want to monitor the condition numbers of my linear systems by --petsc.ksp_monitor_singular_value. So if there is any other way to do that in fenics, I'd be fine with it. (I know I can just use the array() method, but handling nonsparse arrays in python takes hours for the systems that I am solving.

EDIT: I just found the PETScOptions.set() method. However, this also does not work properly. My code has a time-loop, so many Sytems are solved inside that loop and then some more before the time-loop. The PetscOptions are only applied to the solves at the start of the code, but not to the ones inside the time-loop.....

regards

asked Jul 21, 2015 by multigrid202 FEniCS User (3,780 points)
edited Jul 21, 2015 by multigrid202

1 Answer

+4 votes
 
Best answer

The following works for me:

PETScOptions().set("ksp_type", "cg")
PETScOptions().set("pc_type", "none")
PETScOptions().set("ksp_monitor_singular_value", "")

solver = PETScKrylovSolver()
solver.set_operator(A)
solver.solve(x, b)

If dolfin was compiled with petsc4py, you can update the solver parameters with

solver.ksp().setFromOptions()

You can inspect the petsc options with

import petsc4py
print petsc4py.PETSc.Options().getAll()
answered Jul 22, 2015 by Magne Nordaas FEniCS Expert (13,820 points)
selected Jul 25, 2015 by multigrid202

Thanks for the advice. Btw: The PETScOption()... method also seems works with the standard solver = LinearVariationalProblem(...) as long as the solver there is from the PETSc Library. (The Problem from before turned out to be, that some of the solvers were set to cg and others to lu. But lu wasn't from PETSc package, so the options I set with PETScOptions().... didn't apply)

...