[petsc-users] Questions about vec filter and recover

Mark Adams mfadams at lbl.gov
Fri Mar 10 09:32:27 CST 2023


On Fri, Mar 10, 2023 at 9:23 AM 胡清元 <qingyuanhu at jiangnan.edu.cn> wrote:

> Hi there,
>
> I am a fresh user of Petsc, from Jiangnan University. Now I am trying to
> use Petsc for FEM and topology optimization.
> Since I use the background pixel elements, some of elements I don't want
> them to be calculated, so I have to filter them out. Then after my
> calculation, I want to have them back.
>
> For example, in the context of "mpiexec -np 2":
> I have a Vec xPassive=[1, 1, 0, 0, 0 | 1, 1, 1, 1, 1] showing the
> design-able elements (1) and the not-design-able elements (0) to be
> filtered out. This vec is auto sliced into 5+5  by the 2 threads.
> At the same time, I have a Vec density=[0.0, 0.1, 1.0, 1.0, 1.0 | 0.5,
> 0.6, 0.7, 0.8, 0.9].
> In order to narrow down the density, I make an array and count, like
> resarray=[0.0, 0.1] with count=2 and resarray=[0.5, 0.6, 0.7, 0.8, 0.9]
> with count=5, then by the method VecCreateMPIWithArray(PETSC_COMM_WORLD,
> 1, count, PETSC_DECIDE, resarray, &density_new),  I get Vec density_new =
> [0.0, 0.1, 0.5, 0.6, 0.7, 0.8, 0.9] successfully.
> Next, I put the density_new into some methods to get the new values like
> density_new=[0.01, 0.11, 0.51, 0.61 | 0.71, 0.81, 0.91], note that since
> the density_new is of size 7, it becomes 4+3 for the 2 threads.
>

So the method changes the paralelled decompostion but not the order
(strange, but that is fine)


> Finally, I have to recover them as Vec density_recover=[0.01, 0.11, 1.0,
> 1.0, 1.0 | 0.51, 0.61, 0.71, 0.81, 0.91], in this process I fill the
> default 1.0 for the place where xPassive value=0.
>
> In the last step, when I try to recover the density vector, I tried to use
> VecGetValues but it seems can only get local values, cannot cross threads.
>

Yes, you can only get local values with VecGetValues.


>
> I tried also to use VecScatterCreate(density_new, NULL, density_recover,
> idx_to, &scatter), however, my idx_to=[0, 1 | 5, 6, 7, 8, 9] and not
> works well like normal [0, 1, 5, 6, 7, 8, 9].
> Could you help me with this please? Thank you soooooo much for your time!
>

You have 4+3 so you want your IS to be of that size. One IS can be NULL
because you are scattering all values.
I think you want:
[ 0 1 5 6  | 7  8 9 ]
And set the values to 1.0 before the scatter to get your 1.0, 1.0, 1.0 in
there.

I always just have to play around with this kind of stuff to get it right.

Good luck,
Mark



>
> Best regards,
> Qingyuan HU
> School of Science, Jiangnan University
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230310/b7475c61/attachment.html>


More information about the petsc-users mailing list