[petsc-users] passing variables into (*func)(KSP, Vec, void*) in the interface of KSPSetComputeRHS

Barry Smith bsmith at mcs.anl.gov
Sun Aug 4 17:17:59 CDT 2013


   Sure, just caste it to a void* and then caste it back in the subroutine.  Note that in parallel you will only want to store on each process the "local" values for the field, not all the values.

    Barry

On Aug 4, 2013, at 3:35 PM, Roc Wang <pengxwang at hotmail.com> wrote:

> Thanks a lot, Dave,
> 
>     I can pass an 1-D array into ComputeRHS via *void ctx. 
> 
>     I am using the following procedure:
> 
>   in main()
> {//-------
>   ...
>   double array_1d[array_size];
> 
>   ComputeRHS(array_1d);
>   ierr = KSPSetComputeRHS(ksp,ComputeRHS, array_1d);
>   ...
> //--------
> }
> 
>   in ComputeRHS(KSP,Vec,void* ctx )
> {
>     int size_in_func
>     double* arr1d=static_cast<double *>(ctx);
>     for (i=0;i<size_in_func; i++)
>       cout<<" ComputeRHS="<<arr1d[i] <<endl;  
> }
> 
>   Definitely, a 3d array can be passed by converting it to 1d in main() before calling KSPSetComputeRHS and back to 3d in ComputeRHS().  However, is it possible to pass 3d array directly? Thanks.
> 
> Date: Sun, 4 Aug 2013 18:18:54 +0200
> Subject: Re: [petsc-users] passing variables into (*func)(KSP, Vec, void*) in the interface of KSPSetComputeRHS
> From: dave.mayhem23 at gmail.com
> To: pengxwang at hotmail.com
> CC: petsc-users at mcs.anl.gov
> 
> The last argument you give to KSPSetComputeRHS() will be passed into your user defined function ComputeRHS(). All data needed to evaluate the rhs should be made available through that pointer (void *ctx)
> 
> 
> On Sunday, 4 August 2013, Roc Wang wrote:
> Hello, I am developing a poisson solver based on the example code petsc-3.4.0/src/ksp/ksp/examples/tutorials/ex45.c.
> 
> I have a question on utilizing functions
>  KSPSetComputeRHS (KSP ksp,PetscErrorCode (*func)(KSP,Vec,void*),void *ctx) and ComputeRHS(KSP,Vec,void*) which is the (*func)(KSP,Vec,void*) in the interface.  
> 
> 
> Since I have source term generated on the right hand side of Poisson equation and they are computed outside KSPSetComputeRHS() and ComputeRHS(), an array of source term variables must be passed into ComuteRHS() function. From the manual online, it seems the interface of ComputeRHS is only for passing variables from inside of ComputeRHS to KSPSetComputeRHS. There is not a dummy for passing into ComputeRHS. Can anybody give me suggestions on it? Thanks in advance.
> 
> 
> 



More information about the petsc-users mailing list