2 solvers with different parameters

Asked by Sander Rhebergen

Dear,

I am solving a heat advection-diffusion equation coupled to Stokes equations with a temperature dependent viscosity. Currently I am first solving Stokes, updating velocity and pressure, then the heat advection-diffusion equation. I continue iterating until I reach some tolerance. This works fine when I'm using a direct solver. I'm having some trouble with the iterative solvers though. For the Stokes system I am using command line petsc commands. For example, on the command line I use:

python testcase.py --petsc.ksp_type bcgs --petsc.pc_type hypre

(I know FEniCS has bicgstab and hypre_amg available, but this is just for example). In my testcase.py file I get these commands and make my Stokes solver and solve:

parameters.parse()
solver_Stokes = PETScKrylovSolver()
A, bb = assemble_system(a, L, bcs)
P, btmp = assemble_system(b, L, bcs)
solver_Stokes.set_operators(A,P)
solver_Stokes.solve(U.vector(),bb)

This all works. My problem now is that I want a different solver for my heat advection-diffusion equation. What I do is:

solver_heat = PETScKrylovSolver("gmres","ilu")
At, bt = assemble_system(at, Lt, bcst)
solver_heat.solve(At, T.vector(), bt)

What happens is that the heat advection-diffusion equation is being solved by the --petsc.ksp_type bcgs --petsc.pc_type hypre commands instead of by gmres and ilu. I'm assuming this is because bcgs and hypre are now parameters also of solver_heat and that gmres and ilu did not overwrite this. Is there a way to fix this so that I can use command-line PETSc commands for the Stokes solver and a different set of Krylov solver/preconditioner for the heat solver?

Thanks!

Question information

Language:
English Edit question
Status:
Answered
For:
FEniCS Project Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Anders Logg (logg) said :
#1

You can only pass global parameters to PETSc from the command-line.

To set the parameters locally for each equation, you need to set the
parameters as part of your program, or possibly handle the parsing
yourself. Note that there is support for reading parameters from the
command line (parameters.parse()) so you can still work with
command-line parameters if you want to.

--
Anders

On Thu, Oct 25, 2012 at 08:36:14AM -0000, Sander Rhebergen wrote:
> New question #212268 on FEniCS Project:
> https://answers.launchpad.net/fenics/+question/212268
>
> Dear,
>
> I am solving a heat advection-diffusion equation coupled to Stokes equations with a temperature dependent viscosity. Currently I am first solving Stokes, updating velocity and pressure, then the heat advection-diffusion equation. I continue iterating until I reach some tolerance. This works fine when I'm using a direct solver. I'm having some trouble with the iterative solvers though. For the Stokes system I am using command line petsc commands. For example, on the command line I use:
>
> python testcase.py --petsc.ksp_type bcgs --petsc.pc_type hypre
>
> (I know FEniCS has bicgstab and hypre_amg available, but this is just for example). In my testcase.py file I get these commands and make my Stokes solver and solve:
>
> parameters.parse()
> solver_Stokes = PETScKrylovSolver()
> A, bb = assemble_system(a, L, bcs)
> P, btmp = assemble_system(b, L, bcs)
> solver_Stokes.set_operators(A,P)
> solver_Stokes.solve(U.vector(),bb)
>
> This all works. My problem now is that I want a different solver for my heat advection-diffusion equation. What I do is:
>
> solver_heat = PETScKrylovSolver("gmres","ilu")
> At, bt = assemble_system(at, Lt, bcst)
> solver_heat.solve(At, T.vector(), bt)
>
> What happens is that the heat advection-diffusion equation is being solved by the --petsc.ksp_type bcgs --petsc.pc_type hypre commands instead of by gmres and ilu. I'm assuming this is because bcgs and hypre are now parameters also of solver_heat and that gmres and ilu did not overwrite this. Is there a way to fix this so that I can use command-line PETSc commands for the Stokes solver and a different set of Krylov solver/preconditioner for the heat solver?
>
> Thanks!
>
>
>
>
>
>

Revision history for this message
Sander Rhebergen (sanderrhebergen) said :
#2

Dear Anders,

Could you explain to me what you mean by setting the parameters as part of my program or handling the parsing myself?

I created a new set of parameters and parse values to it from commandline:

myPar = Parameters("myPar")
myPar.add("ksp","gmres")
myPar.add("pc","ilu")

parameters.parse()
myPar.parse()

solver1 = PETScKrylovSolver()
solver2 = PETScKrylovSolver(myPar.ksp,myPar.pc)

Here parameters.parse() takes the petsc parameters for solver 1 (I hoped) and myPar.parse() parameters for solver 2. I then use solver1 to solve my Stokes equations and solver2 to solve my advection equation:

solver1.solve(A1,U1.vector(), b1)
solver2.solve(A2,U2.vector(), b2)

So on the commandline I have something like:

python wedge.py --petsc.ksp_type bcgs --petsc.pc_type hypre --ksp gmres --pc ilu

but solver2 uses the global parameters (that I set for solver1), like you mentioned.

regards,
Sander

Revision history for this message
Anders Logg (logg) said :
#3

On Tue, Nov 06, 2012 at 09:45:56AM -0000, Sander Rhebergen wrote:
> Question #212268 on FEniCS Project changed:
> https://answers.launchpad.net/fenics/+question/212268
>
> Status: Answered => Open
>
> Sander Rhebergen is still having a problem:
> Dear Anders,
>
> Could you explain to me what you mean by setting the parameters as part
> of my program or handling the parsing myself?
>
> I created a new set of parameters and parse values to it from
> commandline:
>
> myPar = Parameters("myPar")
> myPar.add("ksp","gmres")
> myPar.add("pc","ilu")
>
> parameters.parse()
> myPar.parse()

Create one single list of parameters that you parse and name the
parameters for the two solvers differently.

> solver1 = PETScKrylovSolver()
> solver2 = PETScKrylovSolver(myPar.ksp,myPar.pc)

> Here parameters.parse() takes the petsc parameters for solver 1 (I
> hoped) and myPar.parse() parameters for solver 2. I then use solver1 to
> solve my Stokes equations and solver2 to solve my advection equation:
>
> solver1.solve(A1,U1.vector(), b1)
> solver2.solve(A2,U2.vector(), b2)
>
> So on the commandline I have something like:
>
> python wedge.py --petsc.ksp_type bcgs --petsc.pc_type hypre --ksp gmres
> --pc ilu
>
> but solver2 uses the global parameters (that I set for solver1), like
> you mentioned.

Which parameters are those? Is it not using the ksp and ps you
specify?

--
Anders

Revision history for this message
Sander Rhebergen (sanderrhebergen) said :
#4

Dear Anders,

When I create a single list of parameters, will I still be able to parse PETSc specific commandline parameters, so for example
--petsc.pc_type fieldsplit ?
As far as I understood, I could only use for example --petsc.pc_type fieldsplit by changing the global parameters list.

Regarding your second question: In my python program I have:

myPar = Parameters("myPar")
myPar.add("ksp","gmres")
myPar.add("pc","ilu")
parameters.parse()
myPar.parse()

and I use the global parameters to solve my Stokes equations and the "myPar" parameters to solve the advection equation:

solverStokes = PETScKrylovSolver()
solverAdvection = PETScKrylovSolver(myPar.ksp,myPar.pc)

Now let's say I switch off the Stokes solver and just solve the advection equation. If on the command-line I type:

python demo.py --petsc.ksp_type bcgs --petsc.pc_type hypre --ksp gmres --pc ilu

the solverAdvection does not use gmres and ilu, but instead the global parameters bcgs and hypre even though I have

solverAdvection = PETScKrylovSolver(myPar.ksp,myPar.pc)

On the other hand, if on the command-line I type:

python demo.py --ksp gmres --pc ilu

my solver does use gmres and ilu. So the problem is, as soon as I specify --petsc.ksp_type bcgs --petsc.pc_type hypre for my Stokes solver, my other parameters --ksp gmres --pc ilu for the advection solver are ignored.

Revision history for this message
Anders Logg (logg) said :
#5

On Tue, Nov 06, 2012 at 06:11:15PM -0000, Sander Rhebergen wrote:
> Question #212268 on FEniCS Project changed:
> https://answers.launchpad.net/fenics/+question/212268
>
> Status: Answered => Open
>
> Sander Rhebergen is still having a problem:
> Dear Anders,
>
> When I create a single list of parameters, will I still be able to parse PETSc specific commandline parameters, so for example
> --petsc.pc_type fieldsplit ?
> As far as I understood, I could only use for example --petsc.pc_type fieldsplit by changing the global parameters list.
>
>
> Regarding your second question: In my python program I have:
>
> myPar = Parameters("myPar")
> myPar.add("ksp","gmres")
> myPar.add("pc","ilu")
> parameters.parse()
> myPar.parse()
>
> and I use the global parameters to solve my Stokes equations and the
> "myPar" parameters to solve the advection equation:
>
> solverStokes = PETScKrylovSolver()
> solverAdvection = PETScKrylovSolver(myPar.ksp,myPar.pc)
>
> Now let's say I switch off the Stokes solver and just solve the
> advection equation. If on the command-line I type:
>
> python demo.py --petsc.ksp_type bcgs --petsc.pc_type hypre --ksp gmres
> --pc ilu
>
> the solverAdvection does not use gmres and ilu, but instead the global
> parameters bcgs and hypre even though I have
>
> solverAdvection = PETScKrylovSolver(myPar.ksp,myPar.pc)
>
> On the other hand, if on the command-line I type:
>
> python demo.py --ksp gmres --pc ilu
>
> my solver does use gmres and ilu. So the problem is, as soon as I
> specify --petsc.ksp_type bcgs --petsc.pc_type hypre for my Stokes
> solver, my other parameters --ksp gmres --pc ilu for the advection
> solver are ignored.

Then the problem is PETSc is overriding the arguments.

What DOLFIN does is to:

1. Provide a way to parse command-line arguments

2. Provide a way to feed all command-line arguments to PETSc

So you will have to figure out the logic to make it work, for example,
don't pass the arguments to PETSc, but instead parse the arguments
explicitly in DOLFIN, then explicitly set the parameters for the two
solvers based on the command-line arguments.

--
Anders

Revision history for this message
Felix Ospald (felix-ospald) said :
#6

http://www.diveintopython.net/scripts_and_streams/command_line_arguments.html

for example

program.py:

import sys
...
solverStokes = PETScKrylovSolver(sys.argv[1], sys.argv[2])
solverAdvection = PETScKrylovSolver(sys.argv[3], sys.argv[4])

then run

program.py bcgs hypre gmres ilu

to give you an idea...

Can you help with this problem?

Provide an answer of your own, or ask Sander Rhebergen for more information if necessary.

To post a message you must log in.