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

Roc Wang pengxwang at hotmail.com
Sun Aug 4 19:54:45 CDT 2013


Thanks, Barry,          I meant  in ComputeRHS(KSP,Vec,void* ctx),  I can only has one ponter array like double* arr1d which can onle be casted from void *ctx to 1d array directly.  If it is a 3d array outside, I have to use 3 for-iterations to convert the 1d array ( arr1d ) to a 3d array (arr3d) inside,  like            int i,j,k,cnt;      cnt=0;      double* arr1d=static_cast<double *>(ctx);      for (i=0; i<3; i++)      { for(j=0; j<3; j++)        { for(k=0; k<3;k++)           arr3d[i][j][k]=arr1d[cnt];           cnt++;        }      }
  but cannot use the arr3d directly inside the function. Is there anyway to cast *ctx to a 3d array directly?   Thanks again.

> > int size_in_func
> > double* arr1d=static_cast<double *>(ctx);

> Subject: Re: [petsc-users] passing variables into (*func)(KSP, Vec, void*) in the interface of KSPSetComputeRHS
> From: bsmith at mcs.anl.gov
> Date: Sun, 4 Aug 2013 17:17:59 -0500
> CC: dave.mayhem23 at gmail.com; petsc-users at mcs.anl.gov
> To: pengxwang at hotmail.com
> 
> 
>    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.
> > 
> > 
> > 
> 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20130804/1ffcc606/attachment.html>


More information about the petsc-users mailing list