xref: /petsc/src/dm/impls/plex/plexnatural.c (revision 42ea106ed169eccaf0b280d95bd7b47dd20a71de)
1fa534816SMatthew G. Knepley #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2fa534816SMatthew G. Knepley 
3fa534816SMatthew G. Knepley /*@
4f94b4a02SBlaise Bourdin   DMPlexSetMigrationSF - Sets the SF for migrating from a parent DM into this DM
5f94b4a02SBlaise Bourdin 
65d3b26e6SMatthew G. Knepley   Input Parameters:
7f94b4a02SBlaise Bourdin + dm        - The DM
85d3b26e6SMatthew G. Knepley - naturalSF - The PetscSF
95d3b26e6SMatthew G. Knepley 
105d3b26e6SMatthew G. Knepley   Note: It is necessary to call this in order to have DMCreateSubDM() or DMCreateSuperDM() build the Global-To-Natural map
115d3b26e6SMatthew G. Knepley 
12f94b4a02SBlaise Bourdin   Level: intermediate
13f94b4a02SBlaise Bourdin 
14f94b4a02SBlaise Bourdin .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexCreateMigrationSF(), DMPlexGetMigrationSF()
15f94b4a02SBlaise Bourdin @*/
16f94b4a02SBlaise Bourdin PetscErrorCode DMPlexSetMigrationSF(DM dm, PetscSF migrationSF)
17f94b4a02SBlaise Bourdin {
18736995cdSBlaise Bourdin   PetscErrorCode ierr;
19f94b4a02SBlaise Bourdin   PetscFunctionBegin;
20f94b4a02SBlaise Bourdin   dm->sfMigration = migrationSF;
21736995cdSBlaise Bourdin   ierr = PetscObjectReference((PetscObject) migrationSF);CHKERRQ(ierr);
22f94b4a02SBlaise Bourdin   PetscFunctionReturn(0);
23f94b4a02SBlaise Bourdin }
24f94b4a02SBlaise Bourdin 
25f94b4a02SBlaise Bourdin /*@
26f94b4a02SBlaise Bourdin   DMPlexGetMigrationSF - Gets the SF for migrating from a parent DM into this DM
27f94b4a02SBlaise Bourdin 
285d3b26e6SMatthew G. Knepley   Input Parameter:
295d3b26e6SMatthew G. Knepley . dm          - The DM
305d3b26e6SMatthew G. Knepley 
315d3b26e6SMatthew G. Knepley   Output Parameter:
325d3b26e6SMatthew G. Knepley . migrationSF - The PetscSF
335d3b26e6SMatthew G. Knepley 
34f94b4a02SBlaise Bourdin   Level: intermediate
35f94b4a02SBlaise Bourdin 
36f94b4a02SBlaise Bourdin .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexCreateMigrationSF(), DMPlexSetMigrationSF
37f94b4a02SBlaise Bourdin @*/
38f94b4a02SBlaise Bourdin PetscErrorCode DMPlexGetMigrationSF(DM dm, PetscSF *migrationSF)
39f94b4a02SBlaise Bourdin {
40f94b4a02SBlaise Bourdin   PetscFunctionBegin;
41f94b4a02SBlaise Bourdin   *migrationSF = dm->sfMigration;
42f94b4a02SBlaise Bourdin   PetscFunctionReturn(0);
43f94b4a02SBlaise Bourdin }
44f94b4a02SBlaise Bourdin 
45f94b4a02SBlaise Bourdin /*@
46f94b4a02SBlaise Bourdin   DMPlexSetGlobalToNaturalSF - Sets the SF for mapping Global Vec to the Natural Vec
47f94b4a02SBlaise Bourdin 
485d3b26e6SMatthew G. Knepley   Input Parameters:
49f94b4a02SBlaise Bourdin + dm          - The DM
505d3b26e6SMatthew G. Knepley - naturalSF   - The PetscSF
515d3b26e6SMatthew G. Knepley 
52f94b4a02SBlaise Bourdin   Level: intermediate
53f94b4a02SBlaise Bourdin 
54f94b4a02SBlaise Bourdin .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexCreateGlobalToNaturalSF(), DMPlexGetGlobaltoNaturalSF()
55f94b4a02SBlaise Bourdin @*/
56f94b4a02SBlaise Bourdin PetscErrorCode DMPlexSetGlobalToNaturalSF(DM dm, PetscSF naturalSF)
57f94b4a02SBlaise Bourdin {
58736995cdSBlaise Bourdin   PetscErrorCode ierr;
59f94b4a02SBlaise Bourdin   PetscFunctionBegin;
60f94b4a02SBlaise Bourdin   dm->sfNatural = naturalSF;
61736995cdSBlaise Bourdin   ierr = PetscObjectReference((PetscObject) naturalSF);CHKERRQ(ierr);
62f94b4a02SBlaise Bourdin   dm->useNatural = PETSC_TRUE;
63f94b4a02SBlaise Bourdin   PetscFunctionReturn(0);
64f94b4a02SBlaise Bourdin }
65f94b4a02SBlaise Bourdin 
66f94b4a02SBlaise Bourdin /*@
67f94b4a02SBlaise Bourdin   DMPlexGetGlobalToNaturalSF - Gets the SF for mapping Global Vec to the Natural Vec
68f94b4a02SBlaise Bourdin 
695d3b26e6SMatthew G. Knepley   Input Parameter:
705d3b26e6SMatthew G. Knepley . dm          - The DM
715d3b26e6SMatthew G. Knepley 
725d3b26e6SMatthew G. Knepley   Output Parameter:
735d3b26e6SMatthew G. Knepley . naturalSF   - The PetscSF
745d3b26e6SMatthew G. Knepley 
75f94b4a02SBlaise Bourdin   Level: intermediate
76f94b4a02SBlaise Bourdin 
77f94b4a02SBlaise Bourdin .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexCreateGlobalToNaturalSF(), DMPlexSetGlobaltoNaturalSF
78f94b4a02SBlaise Bourdin @*/
79f94b4a02SBlaise Bourdin PetscErrorCode DMPlexGetGlobalToNaturalSF(DM dm, PetscSF *naturalSF)
80f94b4a02SBlaise Bourdin {
81f94b4a02SBlaise Bourdin   PetscFunctionBegin;
82f94b4a02SBlaise Bourdin   *naturalSF = dm->sfNatural;
83f94b4a02SBlaise Bourdin   PetscFunctionReturn(0);
84f94b4a02SBlaise Bourdin }
85f94b4a02SBlaise Bourdin 
86f94b4a02SBlaise Bourdin /*@
87fa534816SMatthew G. Knepley   DMPlexCreateGlobalToNaturalSF - Creates the SF for mapping Global Vec to the Natural Vec
88fa534816SMatthew G. Knepley 
89fa534816SMatthew G. Knepley   Input Parameters:
90fa534816SMatthew G. Knepley + dm          - The DM
915d3b26e6SMatthew G. Knepley . section     - The PetscSection describing the Vec before the mesh was distributed
92fa534816SMatthew G. Knepley - sfMigration - The PetscSF used to distribute the mesh
93fa534816SMatthew G. Knepley 
945d3b26e6SMatthew G. Knepley   Output Parameter:
95fa534816SMatthew G. Knepley . sfNatural   - PetscSF for mapping the Vec in PETSc ordering to the canonical ordering
96fa534816SMatthew G. Knepley 
975d3b26e6SMatthew G. Knepley   Note: This is not typically called by the user.
985d3b26e6SMatthew G. Knepley 
99fa534816SMatthew G. Knepley   Level: intermediate
100fa534816SMatthew G. Knepley 
101fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
102fa534816SMatthew G. Knepley  @*/
103fa534816SMatthew G. Knepley PetscErrorCode DMPlexCreateGlobalToNaturalSF(DM dm, PetscSection section, PetscSF sfMigration, PetscSF *sfNatural)
104fa534816SMatthew G. Knepley {
105fa534816SMatthew G. Knepley   MPI_Comm       comm;
106fa534816SMatthew G. Knepley   Vec            gv;
107e5b44f4fSMatthew G. Knepley   PetscSF        sf, sfEmbed, sfSeqToNatural, sfField, sfFieldInv;
108e5b44f4fSMatthew G. Knepley   PetscSection   gSection, sectionDist, gLocSection;
109fa534816SMatthew G. Knepley   PetscInt      *spoints, *remoteOffsets;
110fa534816SMatthew G. Knepley   PetscInt       ssize, pStart, pEnd, p;
111fa534816SMatthew G. Knepley   PetscErrorCode ierr;
112fa534816SMatthew G. Knepley 
113fa534816SMatthew G. Knepley   PetscFunctionBegin;
114fa534816SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
115e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Point migration SF\n");CHKERRQ(ierr);
116e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfMigration, 0);CHKERRQ(ierr); */
117e5b44f4fSMatthew G. Knepley   /* Create a new section from distributing the original section */
118e5b44f4fSMatthew G. Knepley   ierr = PetscSectionCreate(comm, &sectionDist);CHKERRQ(ierr);
119e5b44f4fSMatthew G. Knepley   ierr = PetscSFDistributeSection(sfMigration, section, &remoteOffsets, sectionDist);CHKERRQ(ierr);
120e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Distributed Section\n");CHKERRQ(ierr);
121e5b44f4fSMatthew G. Knepley    ierr = PetscSectionView(sectionDist, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); */
122e87a4003SBarry Smith   ierr = DMSetSection(dm, sectionDist);CHKERRQ(ierr);
123fa534816SMatthew G. Knepley   /* Get a pruned version of migration SF */
124e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
125fa534816SMatthew G. Knepley   ierr = PetscSectionGetChart(gSection, &pStart, &pEnd);CHKERRQ(ierr);
126fa534816SMatthew G. Knepley   for (p = pStart, ssize = 0; p < pEnd; ++p) {
127fa534816SMatthew G. Knepley     PetscInt dof, off;
128fa534816SMatthew G. Knepley 
129fa534816SMatthew G. Knepley     ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr);
130fa534816SMatthew G. Knepley     ierr = PetscSectionGetOffset(gSection, p, &off);CHKERRQ(ierr);
131fa534816SMatthew G. Knepley     if ((dof > 0) && (off >= 0)) ++ssize;
132fa534816SMatthew G. Knepley   }
133fa534816SMatthew G. Knepley   ierr = PetscMalloc1(ssize, &spoints);CHKERRQ(ierr);
134fa534816SMatthew G. Knepley   for (p = pStart, ssize = 0; p < pEnd; ++p) {
135fa534816SMatthew G. Knepley     PetscInt dof, off;
136fa534816SMatthew G. Knepley 
137fa534816SMatthew G. Knepley     ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr);
138fa534816SMatthew G. Knepley     ierr = PetscSectionGetOffset(gSection, p, &off);CHKERRQ(ierr);
139fa534816SMatthew G. Knepley     if ((dof > 0) && (off >= 0)) spoints[ssize++] = p;
140fa534816SMatthew G. Knepley   }
141fa534816SMatthew G. Knepley   ierr = PetscSFCreateEmbeddedLeafSF(sfMigration, ssize, spoints, &sfEmbed);CHKERRQ(ierr);
142e5b44f4fSMatthew G. Knepley   ierr = PetscFree(spoints);CHKERRQ(ierr);
143e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Embedded SF\n");CHKERRQ(ierr);
144e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfEmbed, 0);CHKERRQ(ierr); */
145fa534816SMatthew G. Knepley   /* Create the SF for seq to natural */
146fa534816SMatthew G. Knepley   ierr = DMGetGlobalVector(dm, &gv);CHKERRQ(ierr);
147fa534816SMatthew G. Knepley   ierr = PetscSFCreateFromZero(comm, gv, &sfSeqToNatural);CHKERRQ(ierr);
148fa534816SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dm, &gv);CHKERRQ(ierr);
149e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Seq-to-Natural SF\n");CHKERRQ(ierr);
150e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfSeqToNatural, 0);CHKERRQ(ierr); */
151fa534816SMatthew G. Knepley   /* Create the SF associated with this section */
152e5b44f4fSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
153e5b44f4fSMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(sectionDist, sf, PETSC_FALSE, PETSC_TRUE, &gLocSection);CHKERRQ(ierr);
154e5b44f4fSMatthew G. Knepley   ierr = PetscSFCreateSectionSF(sfEmbed, section, remoteOffsets, gLocSection, &sfField);CHKERRQ(ierr);
1550c374c54SMatthew G. Knepley   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
156fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfEmbed);CHKERRQ(ierr);
157e5b44f4fSMatthew G. Knepley   ierr = PetscSectionDestroy(&gLocSection);CHKERRQ(ierr);
158e5b44f4fSMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionDist);CHKERRQ(ierr);
159e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Field SF\n");CHKERRQ(ierr);
160e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfField, 0);CHKERRQ(ierr); */
161fa534816SMatthew G. Knepley   /* Invert the field SF so it's now from distributed to sequential */
162fa534816SMatthew G. Knepley   ierr = PetscSFCreateInverseSF(sfField, &sfFieldInv);CHKERRQ(ierr);
163fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfField);CHKERRQ(ierr);
164e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Inverse Field SF\n");CHKERRQ(ierr);
165e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfFieldInv, 0);CHKERRQ(ierr); */
166fa534816SMatthew G. Knepley   /* Multiply the sfFieldInv with the */
167fa534816SMatthew G. Knepley   ierr = PetscSFCompose(sfFieldInv, sfSeqToNatural, sfNatural);CHKERRQ(ierr);
168e5b44f4fSMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) *sfNatural, NULL, "-globaltonatural_sf_view");CHKERRQ(ierr);
169fa534816SMatthew G. Knepley   /* Clean up */
170fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfFieldInv);CHKERRQ(ierr);
171fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfSeqToNatural);CHKERRQ(ierr);
172fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
173fa534816SMatthew G. Knepley }
174fa534816SMatthew G. Knepley 
175fa534816SMatthew G. Knepley /*@
176fa534816SMatthew G. Knepley   DMPlexGlobalToNaturalBegin - Rearranges a global Vector in the natural order.
177fa534816SMatthew G. Knepley 
178fa534816SMatthew G. Knepley   Collective on dm
179fa534816SMatthew G. Knepley 
180fa534816SMatthew G. Knepley   Input Parameters:
181fa534816SMatthew G. Knepley + dm - The distributed DMPlex
182fa534816SMatthew G. Knepley - gv - The global Vec
183fa534816SMatthew G. Knepley 
184fa534816SMatthew G. Knepley   Output Parameters:
185fa534816SMatthew G. Knepley . nv - Vec in the canonical ordering distributed over all processors associated with gv
186fa534816SMatthew G. Knepley 
187*42ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
188fa534816SMatthew G. Knepley 
189fa534816SMatthew G. Knepley   Level: intermediate
190fa534816SMatthew G. Knepley 
191fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(), DMPlexGlobalToNaturalEnd()
192fa534816SMatthew G. Knepley @*/
193fa534816SMatthew G. Knepley PetscErrorCode DMPlexGlobalToNaturalBegin(DM dm, Vec gv, Vec nv)
194fa534816SMatthew G. Knepley {
195fa534816SMatthew G. Knepley   const PetscScalar *inarray;
196fa534816SMatthew G. Knepley   PetscScalar       *outarray;
197fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
198fa534816SMatthew G. Knepley 
199fa534816SMatthew G. Knepley   PetscFunctionBegin;
200fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_GlobalToNaturalBegin,dm,0,0,0);CHKERRQ(ierr);
201fa534816SMatthew G. Knepley   if (dm->sfNatural) {
202fa534816SMatthew G. Knepley     ierr = VecGetArray(nv, &outarray);CHKERRQ(ierr);
203fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(gv, &inarray);CHKERRQ(ierr);
204fa534816SMatthew G. Knepley     ierr = PetscSFBcastBegin(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray);CHKERRQ(ierr);
205fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(gv, &inarray);CHKERRQ(ierr);
206fa534816SMatthew G. Knepley     ierr = VecRestoreArray(nv, &outarray);CHKERRQ(ierr);
207e5b44f4fSMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created.\nYou must call DMSetUseNatural() before DMPlexDistribute().\n");
208fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_GlobalToNaturalBegin,dm,0,0,0);CHKERRQ(ierr);
209fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
210fa534816SMatthew G. Knepley }
211fa534816SMatthew G. Knepley 
212fa534816SMatthew G. Knepley /*@
213fa534816SMatthew G. Knepley   DMPlexGlobalToNaturalEnd - Rearranges a global Vector in the natural order.
214fa534816SMatthew G. Knepley 
215fa534816SMatthew G. Knepley   Collective on dm
216fa534816SMatthew G. Knepley 
217fa534816SMatthew G. Knepley   Input Parameters:
218fa534816SMatthew G. Knepley + dm - The distributed DMPlex
219fa534816SMatthew G. Knepley - gv - The global Vec
220fa534816SMatthew G. Knepley 
221fa534816SMatthew G. Knepley   Output Parameters:
222fa534816SMatthew G. Knepley . nv - The natural Vec
223fa534816SMatthew G. Knepley 
224*42ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
225fa534816SMatthew G. Knepley 
226fa534816SMatthew G. Knepley   Level: intermediate
227fa534816SMatthew G. Knepley 
228fa534816SMatthew G. Knepley  .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(), DMPlexGlobalToNaturalBegin()
229fa534816SMatthew G. Knepley  @*/
230fa534816SMatthew G. Knepley PetscErrorCode DMPlexGlobalToNaturalEnd(DM dm, Vec gv, Vec nv)
231fa534816SMatthew G. Knepley {
232fa534816SMatthew G. Knepley   const PetscScalar *inarray;
233fa534816SMatthew G. Knepley   PetscScalar       *outarray;
234fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
235fa534816SMatthew G. Knepley 
236fa534816SMatthew G. Knepley   PetscFunctionBegin;
237fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_GlobalToNaturalEnd,dm,0,0,0);CHKERRQ(ierr);
238fa534816SMatthew G. Knepley   if (dm->sfNatural) {
239fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(gv, &inarray);CHKERRQ(ierr);
240fa534816SMatthew G. Knepley     ierr = VecGetArray(nv, &outarray);CHKERRQ(ierr);CHKERRQ(ierr);
241fa534816SMatthew G. Knepley     ierr = PetscSFBcastEnd(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray);CHKERRQ(ierr);
242fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(gv, &inarray);CHKERRQ(ierr);
243fa534816SMatthew G. Knepley     ierr = VecRestoreArray(nv, &outarray);CHKERRQ(ierr);
244fa534816SMatthew G. Knepley   }
245fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_GlobalToNaturalEnd,dm,0,0,0);CHKERRQ(ierr);
246fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
247fa534816SMatthew G. Knepley }
248fa534816SMatthew G. Knepley 
249fa534816SMatthew G. Knepley /*@
250fa534816SMatthew G. Knepley   DMPlexNaturalToGlobalBegin - Rearranges a Vector in the natural order to the Global order.
251fa534816SMatthew G. Knepley 
252fa534816SMatthew G. Knepley   Collective on dm
253fa534816SMatthew G. Knepley 
254fa534816SMatthew G. Knepley   Input Parameters:
255fa534816SMatthew G. Knepley + dm - The distributed DMPlex
256fa534816SMatthew G. Knepley - nv - The natural Vec
257fa534816SMatthew G. Knepley 
258fa534816SMatthew G. Knepley   Output Parameters:
259fa534816SMatthew G. Knepley . gv - The global Vec
260fa534816SMatthew G. Knepley 
261*42ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
262fa534816SMatthew G. Knepley 
263fa534816SMatthew G. Knepley   Level: intermediate
264fa534816SMatthew G. Knepley 
265fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(),DMPlexGlobalToNaturalEnd()
266fa534816SMatthew G. Knepley @*/
267fa534816SMatthew G. Knepley PetscErrorCode DMPlexNaturalToGlobalBegin(DM dm, Vec nv, Vec gv)
268fa534816SMatthew G. Knepley {
269fa534816SMatthew G. Knepley   const PetscScalar *inarray;
270fa534816SMatthew G. Knepley   PetscScalar       *outarray;
271fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
272fa534816SMatthew G. Knepley 
273fa534816SMatthew G. Knepley   PetscFunctionBegin;
274fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_NaturalToGlobalBegin,dm,0,0,0);CHKERRQ(ierr);
275fa534816SMatthew G. Knepley   if (dm->sfNatural) {
276b64f75a9SBlaise Bourdin     /* We only have acces to the SF that goes from Global to Natural.
277b64f75a9SBlaise Bourdin        Instead of inverting dm->sfNatural, we can call PetscSFReduceBegin/End with MPI_Op MPI_SUM.
278b64f75a9SBlaise Bourdin        Here the SUM really does nothing since sfNatural is one to one, as long as gV is set to zero first. */
279b64f75a9SBlaise Bourdin     ierr = VecZeroEntries(gv);CHKERRQ(ierr);
280fa534816SMatthew G. Knepley     ierr = VecGetArray(gv, &outarray);CHKERRQ(ierr);
281fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(nv, &inarray);CHKERRQ(ierr);
282fa534816SMatthew G. Knepley     ierr = PetscSFReduceBegin(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray, MPI_SUM);CHKERRQ(ierr);
283fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(nv, &inarray);CHKERRQ(ierr);
284fa534816SMatthew G. Knepley     ierr = VecRestoreArray(gv, &outarray);CHKERRQ(ierr);
285*42ea106eSTristan Konolige   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created.\nYou must call DMSetUseNatural() before DMPlexDistribute().\n");
286fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_NaturalToGlobalBegin,dm,0,0,0);CHKERRQ(ierr);
287fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
288fa534816SMatthew G. Knepley }
289fa534816SMatthew G. Knepley 
290fa534816SMatthew G. Knepley /*@
291fa534816SMatthew G. Knepley   DMPlexNaturalToGlobalEnd - Rearranges a Vector in the natural order to the Global order.
292fa534816SMatthew G. Knepley 
293fa534816SMatthew G. Knepley   Collective on dm
294fa534816SMatthew G. Knepley 
295fa534816SMatthew G. Knepley   Input Parameters:
296fa534816SMatthew G. Knepley + dm - The distributed DMPlex
297fa534816SMatthew G. Knepley - nv - The natural Vec
298fa534816SMatthew G. Knepley 
299fa534816SMatthew G. Knepley   Output Parameters:
300fa534816SMatthew G. Knepley . gv - The global Vec
301fa534816SMatthew G. Knepley 
302*42ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
303fa534816SMatthew G. Knepley 
304fa534816SMatthew G. Knepley   Level: intermediate
305fa534816SMatthew G. Knepley 
306fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(), DMPlexGlobalToNaturalBegin()
307fa534816SMatthew G. Knepley  @*/
308fa534816SMatthew G. Knepley PetscErrorCode DMPlexNaturalToGlobalEnd(DM dm, Vec nv, Vec gv)
309fa534816SMatthew G. Knepley {
310fa534816SMatthew G. Knepley   const PetscScalar *inarray;
311fa534816SMatthew G. Knepley   PetscScalar       *outarray;
312fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
313fa534816SMatthew G. Knepley 
314fa534816SMatthew G. Knepley   PetscFunctionBegin;
315fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_NaturalToGlobalEnd,dm,0,0,0);CHKERRQ(ierr);
316fa534816SMatthew G. Knepley   if (dm->sfNatural) {
317fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(nv, &inarray);CHKERRQ(ierr);
318fa534816SMatthew G. Knepley     ierr = VecGetArray(gv, &outarray);CHKERRQ(ierr);CHKERRQ(ierr);
319fa534816SMatthew G. Knepley     ierr = PetscSFReduceEnd(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray, MPI_SUM);CHKERRQ(ierr);
320fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(nv, &inarray);CHKERRQ(ierr);
321fa534816SMatthew G. Knepley     ierr = VecRestoreArray(gv, &outarray);CHKERRQ(ierr);
322fa534816SMatthew G. Knepley   }
323fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_NaturalToGlobalEnd,dm,0,0,0);CHKERRQ(ierr);
324fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
325fa534816SMatthew G. Knepley }
326