<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Thanks, Barry,<div> </div><div> I meant in ComputeRHS(KSP,Vec,void* ctx), I can only has one ponter array like <i>double* arr1d</i> which can onle be casted from <i>void</i> <i>*ctx</i> to 1d array directly. If it is a 3d array outside, I have to use 3 for-iterations to convert the 1d array (<i> arr1d</i> ) to a 3d array (<i>arr3d</i>) inside, like</div><div> </div><div><span style="font-size: 12pt;"><i> int i,j,k,cnt;</i></span></div><div><span style="font-size: 12pt;"><i> cnt=0;</i></span></div><i> double* arr1d=static_cast<double *>(ctx);</i><div><i> for (i=0; i<3; i++)</i></div><div><i> { for(j=0; j<3; j++)</i></div><div><i> { for(k=0; k<3;k++)</i></div><div><i> arr3d[i][j][k]=arr1d[cnt];</i></div><div><i> cnt++;</i></div><div><i> }</i></div><div><i> }</i></div><div><br></div><div> <span style="font-size: 12pt;">but cannot use the arr3d directly inside the function. </span><span style="font-size: 12pt;">Is there anyway to cast *ctx to a 3d array directly? Thanks again.</span></div><div><br></div><div><br>> > int size_in_func<br>> > double* arr1d=static_cast<double *>(ctx);<br><br><div>> Subject: Re: [petsc-users] passing variables into (*func)(KSP, Vec, void*) in the interface of KSPSetComputeRHS<br>> From: bsmith@mcs.anl.gov<br>> Date: Sun, 4 Aug 2013 17:17:59 -0500<br>> CC: dave.mayhem23@gmail.com; petsc-users@mcs.anl.gov<br>> To: pengxwang@hotmail.com<br>> <br>> <br>> 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.<br>> <br>> Barry<br>> <br>> On Aug 4, 2013, at 3:35 PM, Roc Wang <pengxwang@hotmail.com> wrote:<br>> <br>> > Thanks a lot, Dave,<br>> > <br>> > I can pass an 1-D array into ComputeRHS via *void ctx. <br>> > <br>> > I am using the following procedure:<br>> > <br>> > in main()<br>> > {//-------<br>> > ...<br>> > double array_1d[array_size];<br>> > <br>> > ComputeRHS(array_1d);<br>> > ierr = KSPSetComputeRHS(ksp,ComputeRHS, array_1d);<br>> > ...<br>> > //--------<br>> > }<br>> > <br>> > in ComputeRHS(KSP,Vec,void* ctx )<br>> > {<br>> > int size_in_func<br>> > double* arr1d=static_cast<double *>(ctx);<br>> > for (i=0;i<size_in_func; i++)<br>> > cout<<" ComputeRHS="<<arr1d[i] <<endl; <br>> > }<br>> > <br>> > 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.<br>> > <br>> > Date: Sun, 4 Aug 2013 18:18:54 +0200<br>> > Subject: Re: [petsc-users] passing variables into (*func)(KSP, Vec, void*) in the interface of KSPSetComputeRHS<br>> > From: dave.mayhem23@gmail.com<br>> > To: pengxwang@hotmail.com<br>> > CC: petsc-users@mcs.anl.gov<br>> > <br>> > 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)<br>> > <br>> > <br>> > On Sunday, 4 August 2013, Roc Wang wrote:<br>> > Hello, I am developing a poisson solver based on the example code petsc-3.4.0/src/ksp/ksp/examples/tutorials/ex45.c.<br>> > <br>> > I have a question on utilizing functions<br>> > 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. <br>> > <br>> > <br>> > 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.<br>> > <br>> > <br>> > <br>> <br></div></div> </div></body>
</html>