Several years ago I asked a similar question and this is what Barry told me:


>>>>>>>>>>
 Randy,

    Take a look at DMDAGetRay() in src/dm/impls/da/dasub.c (now called 
DMDACreateRay()) this takes a row or column from a 2d DADM. You can use the 
same kind of approach to get a slice from a 3d DMDA.

  Barry
<<<<<<<<<<


Maybe this would help you?


Randy


> On Jan 23, 2021, at 4:45 PM, Sajid Ali <sajidsyed2...@u.northwestern.edu> 
> wrote:
> 
> Hi PETSc-developers, 
> 
> For an application, I'd like to extract a 2D slice from a 3D DMDA vector, 
> perform a `MatMult` on it (a discretized rotation on the 2D vector) and place 
> the resulting vector back into the 3D vector. The approach I'd taken was to 
> use `DMDACreatePatchIS` to create an IS that selects the slice (say this 
> slice is [:,:,n_z] with no loss of generality). This IS is then used to 
> extract the 2D slice via the `VecGetSubVector` utility. The issue that arises 
> from this scheme is that the extracted 2D vector does not represent a 
> flattened 2D vector, it is instead ordered as a DMDA vector (aka each rank 
> having the portion of vector it owns in a column major ordering). How do I 
> convert this DMDA ordered 2D subvector to a vector that represents a 
> flattened 2D array? Since I can't predict how the extracted sub-vector 
> 
> While PETSc provides `DMDAGlobalToNatural` routines, those don't apply to 
> extracted sub-vectors and extracting the 2D slice from a natural vector 
> (filled by a scatter from the global vector) does not work either. On the 
> other hand, converting the output of DMDACreatePatchIS to a natural IS via 
> `AOPetscToApplicationIS` before extracting a subvector extracts the wrong 
> indices from the 3D vector. 
> 
> Could someone tell me how I can achieve the desired goal (converting a 2D 
> vector ordered as per a 3D DMDA grid onto a natural flattened index) ? Thanks 
> in advance for the help!
> 
> PS : Should it help, I'm attaching a slightly modified version of 
> `src/dm/tests/ex53.c` to demonstrate the issue. The array I wish to obtain 
> (the [:,:,2]) slice is : `[196., 194., 192., 190., 198., 197., 196., 195., 
> 200., 200., 200.,200., 202., 203., 204., 205.]` (a row-major flattening of 
> the 2D slice or alternatively a column-major flattening). What I obtain from 
> the program (by creating the PatchIS and extracting the subvector) instead is 
> : `[0 (A)] 196,198,194,197 [1 (B)] 200,202,200,203 [2 (C)] 192,196,190,195 [3 
> (D)] 200,204,200,205` (where each square bracket represents the rank and 
> portion of the 2D vector being flattened via a column-major formatting with 
> the layout shown below)
> |---------|
> | A | C  |
> | B | D  |
> |---------|
> 
> 
> Thank You,
> Sajid Ali (he/him) | PhD Candidate
> Applied Physics
> Northwestern University
> s-sajid-ali.github.io <http://s-sajid-ali.github.io/><ex53.c>

Reply via email to