[petsc-users] Using operators in KSP

Sun, Hui hus003 at ucsd.edu
Mon Jun 16 00:40:40 CDT 2014

Thank you Barry and Jed for answering my question. Here is another question: If I have a nonlinear PDE, for example, Poisson Boltzmann with an inhomogeneous dielectric constant epsilon. I want to solve for psi, but not epsilon. But to form the SetFunctionLocal, one needs to provide psi as well as epsilon. 

The user defined function which is passed to DMDASNESSetFunctionLocal should be of the format 
PetscErrorCode (*func)(DMDALocalInfo*,void*,void*,void*), 
where the first void* corresponds to x, the dimensional pointer to state at which to evaluate residual; and the second void* corresponds to f, the dimensional pointer to residual, write the residual here; and the third void* is the optional context passed above. 

The scalar field epsilon is not part of x, neither is it part of f, it seems that the only choice is to pass it in the third void*. However, if I have some other parameters, should I set up a struct, which includes those other parameters, and a distributed Vec for epsilon, and pass it in the third void*? 


From: Barry Smith [bsmith at mcs.anl.gov]
Sent: Saturday, June 14, 2014 3:43 PM
To: Sun, Hui
Cc: petsc-users at mcs.anl.gov
Subject: Re: [petsc-users] Using operators in KSP

   MatCreateShell() is what I think you need.  You provide a “matrix-free” linear operator with MatShellSetOperation(mat,MATOP_MULT,yourfunction) and yourfunction applies the linear operator any way it likes.

   You then provide this matrix as the first Mat argument to KSPSetOperators().


On Jun 14, 2014, at 5:18 PM, Sun, Hui <hus003 at ucsd.edu> wrote:

> I try to program 2D Stokes equation solver, so it is a linear PDE, and there are u, v, p on every grid point. One way on my mind is to form a matrix-free block matrix. In that way, if the discretization is n by n, then the matrix is 3n by 3n. However, I'm also thinking if it is possible to define the PDE operator as what DMDASNESSetFunctionLocal does in SNES example ex19? In that example, the unknowns (u, v, omega, T) are defined as a struct of four PestsScalar on every grid point, and then the interface converts PestcScalar** to Vec, and an operator instead of a matrix is formed.
> Is there a function in KSP similar to DMDASNESSetFunctionLocal in SNES?

More information about the petsc-users mailing list