xref: /petsc/src/dm/impls/plex/plexnatural.c (revision c3b366b11f3ec31f57fee5376535cdadac340a0d)
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;
1113a350576SJunchao Zhang   PetscLayout    map;
112fa534816SMatthew G. Knepley   PetscErrorCode ierr;
113fa534816SMatthew G. Knepley 
114fa534816SMatthew G. Knepley   PetscFunctionBegin;
115fa534816SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
116e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Point migration SF\n");CHKERRQ(ierr);
117e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfMigration, 0);CHKERRQ(ierr); */
118e5b44f4fSMatthew G. Knepley   /* Create a new section from distributing the original section */
119e5b44f4fSMatthew G. Knepley   ierr = PetscSectionCreate(comm, &sectionDist);CHKERRQ(ierr);
120e5b44f4fSMatthew G. Knepley   ierr = PetscSFDistributeSection(sfMigration, section, &remoteOffsets, sectionDist);CHKERRQ(ierr);
121e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Distributed Section\n");CHKERRQ(ierr);
122e5b44f4fSMatthew G. Knepley    ierr = PetscSectionView(sectionDist, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); */
12392fd8e1eSJed Brown   ierr = DMSetLocalSection(dm, sectionDist);CHKERRQ(ierr);
124fa534816SMatthew G. Knepley   /* Get a pruned version of migration SF */
125e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
126fa534816SMatthew G. Knepley   ierr = PetscSectionGetChart(gSection, &pStart, &pEnd);CHKERRQ(ierr);
127fa534816SMatthew G. Knepley   for (p = pStart, ssize = 0; p < pEnd; ++p) {
128fa534816SMatthew G. Knepley     PetscInt dof, off;
129fa534816SMatthew G. Knepley 
130fa534816SMatthew G. Knepley     ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr);
131fa534816SMatthew G. Knepley     ierr = PetscSectionGetOffset(gSection, p, &off);CHKERRQ(ierr);
132fa534816SMatthew G. Knepley     if ((dof > 0) && (off >= 0)) ++ssize;
133fa534816SMatthew G. Knepley   }
134fa534816SMatthew G. Knepley   ierr = PetscMalloc1(ssize, &spoints);CHKERRQ(ierr);
135fa534816SMatthew G. Knepley   for (p = pStart, ssize = 0; p < pEnd; ++p) {
136fa534816SMatthew G. Knepley     PetscInt dof, off;
137fa534816SMatthew G. Knepley 
138fa534816SMatthew G. Knepley     ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr);
139fa534816SMatthew G. Knepley     ierr = PetscSectionGetOffset(gSection, p, &off);CHKERRQ(ierr);
140fa534816SMatthew G. Knepley     if ((dof > 0) && (off >= 0)) spoints[ssize++] = p;
141fa534816SMatthew G. Knepley   }
142fa534816SMatthew G. Knepley   ierr = PetscSFCreateEmbeddedLeafSF(sfMigration, ssize, spoints, &sfEmbed);CHKERRQ(ierr);
143e5b44f4fSMatthew G. Knepley   ierr = PetscFree(spoints);CHKERRQ(ierr);
144e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Embedded SF\n");CHKERRQ(ierr);
145e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfEmbed, 0);CHKERRQ(ierr); */
146fa534816SMatthew G. Knepley   /* Create the SF for seq to natural */
147fa534816SMatthew G. Knepley   ierr = DMGetGlobalVector(dm, &gv);CHKERRQ(ierr);
1483a350576SJunchao Zhang   ierr = VecGetLayout(gv,&map);CHKERRQ(ierr);
1493a350576SJunchao Zhang   /* Note that entries of gv are leaves in sfSeqToNatural, entries of the seq vec are roots */
1503a350576SJunchao Zhang   ierr = PetscSFCreate(comm, &sfSeqToNatural);CHKERRQ(ierr);
1513a350576SJunchao Zhang   ierr = PetscSFSetGraphWithPattern(sfSeqToNatural, map, PETSCSF_PATTERN_GATHER);CHKERRQ(ierr);
152fa534816SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dm, &gv);CHKERRQ(ierr);
153e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Seq-to-Natural SF\n");CHKERRQ(ierr);
154e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfSeqToNatural, 0);CHKERRQ(ierr); */
155fa534816SMatthew G. Knepley   /* Create the SF associated with this section */
156e5b44f4fSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
157e5b44f4fSMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(sectionDist, sf, PETSC_FALSE, PETSC_TRUE, &gLocSection);CHKERRQ(ierr);
158e5b44f4fSMatthew G. Knepley   ierr = PetscSFCreateSectionSF(sfEmbed, section, remoteOffsets, gLocSection, &sfField);CHKERRQ(ierr);
1590c374c54SMatthew G. Knepley   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
160fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfEmbed);CHKERRQ(ierr);
161e5b44f4fSMatthew G. Knepley   ierr = PetscSectionDestroy(&gLocSection);CHKERRQ(ierr);
162e5b44f4fSMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionDist);CHKERRQ(ierr);
163e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Field SF\n");CHKERRQ(ierr);
164e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfField, 0);CHKERRQ(ierr); */
165fa534816SMatthew G. Knepley   /* Invert the field SF so it's now from distributed to sequential */
166fa534816SMatthew G. Knepley   ierr = PetscSFCreateInverseSF(sfField, &sfFieldInv);CHKERRQ(ierr);
167fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfField);CHKERRQ(ierr);
168e5b44f4fSMatthew G. Knepley   /* ierr = PetscPrintf(comm, "Inverse Field SF\n");CHKERRQ(ierr);
169e5b44f4fSMatthew G. Knepley    ierr = PetscSFView(sfFieldInv, 0);CHKERRQ(ierr); */
170fa534816SMatthew G. Knepley   /* Multiply the sfFieldInv with the */
1713a350576SJunchao Zhang   ierr = PetscSFComposeInverse(sfFieldInv, sfSeqToNatural, sfNatural);CHKERRQ(ierr);
172e5b44f4fSMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) *sfNatural, NULL, "-globaltonatural_sf_view");CHKERRQ(ierr);
173fa534816SMatthew G. Knepley   /* Clean up */
174fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfFieldInv);CHKERRQ(ierr);
175fa534816SMatthew G. Knepley   ierr = PetscSFDestroy(&sfSeqToNatural);CHKERRQ(ierr);
176fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
177fa534816SMatthew G. Knepley }
178fa534816SMatthew G. Knepley 
179fa534816SMatthew G. Knepley /*@
180fa534816SMatthew G. Knepley   DMPlexGlobalToNaturalBegin - Rearranges a global Vector in the natural order.
181fa534816SMatthew G. Knepley 
182fa534816SMatthew G. Knepley   Collective on dm
183fa534816SMatthew G. Knepley 
184fa534816SMatthew G. Knepley   Input Parameters:
185fa534816SMatthew G. Knepley + dm - The distributed DMPlex
186fa534816SMatthew G. Knepley - gv - The global Vec
187fa534816SMatthew G. Knepley 
188fa534816SMatthew G. Knepley   Output Parameters:
189fa534816SMatthew G. Knepley . nv - Vec in the canonical ordering distributed over all processors associated with gv
190fa534816SMatthew G. Knepley 
19142ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
192fa534816SMatthew G. Knepley 
193fa534816SMatthew G. Knepley   Level: intermediate
194fa534816SMatthew G. Knepley 
195fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(), DMPlexGlobalToNaturalEnd()
196fa534816SMatthew G. Knepley @*/
197fa534816SMatthew G. Knepley PetscErrorCode DMPlexGlobalToNaturalBegin(DM dm, Vec gv, Vec nv)
198fa534816SMatthew G. Knepley {
199fa534816SMatthew G. Knepley   const PetscScalar *inarray;
200fa534816SMatthew G. Knepley   PetscScalar       *outarray;
201fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
202fa534816SMatthew G. Knepley 
203fa534816SMatthew G. Knepley   PetscFunctionBegin;
204fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_GlobalToNaturalBegin,dm,0,0,0);CHKERRQ(ierr);
205fa534816SMatthew G. Knepley   if (dm->sfNatural) {
206fa534816SMatthew G. Knepley     ierr = VecGetArray(nv, &outarray);CHKERRQ(ierr);
207fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(gv, &inarray);CHKERRQ(ierr);
208fa534816SMatthew G. Knepley     ierr = PetscSFBcastBegin(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray);CHKERRQ(ierr);
209fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(gv, &inarray);CHKERRQ(ierr);
210fa534816SMatthew G. Knepley     ierr = VecRestoreArray(nv, &outarray);CHKERRQ(ierr);
211e5b44f4fSMatthew 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");
212fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_GlobalToNaturalBegin,dm,0,0,0);CHKERRQ(ierr);
213fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
214fa534816SMatthew G. Knepley }
215fa534816SMatthew G. Knepley 
216fa534816SMatthew G. Knepley /*@
217fa534816SMatthew G. Knepley   DMPlexGlobalToNaturalEnd - Rearranges a global Vector in the natural order.
218fa534816SMatthew G. Knepley 
219fa534816SMatthew G. Knepley   Collective on dm
220fa534816SMatthew G. Knepley 
221fa534816SMatthew G. Knepley   Input Parameters:
222fa534816SMatthew G. Knepley + dm - The distributed DMPlex
223fa534816SMatthew G. Knepley - gv - The global Vec
224fa534816SMatthew G. Knepley 
225fa534816SMatthew G. Knepley   Output Parameters:
226fa534816SMatthew G. Knepley . nv - The natural Vec
227fa534816SMatthew G. Knepley 
22842ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
229fa534816SMatthew G. Knepley 
230fa534816SMatthew G. Knepley   Level: intermediate
231fa534816SMatthew G. Knepley 
232fa534816SMatthew G. Knepley  .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(), DMPlexGlobalToNaturalBegin()
233fa534816SMatthew G. Knepley  @*/
234fa534816SMatthew G. Knepley PetscErrorCode DMPlexGlobalToNaturalEnd(DM dm, Vec gv, Vec nv)
235fa534816SMatthew G. Knepley {
236fa534816SMatthew G. Knepley   const PetscScalar *inarray;
237fa534816SMatthew G. Knepley   PetscScalar       *outarray;
238fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
239fa534816SMatthew G. Knepley 
240fa534816SMatthew G. Knepley   PetscFunctionBegin;
241fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_GlobalToNaturalEnd,dm,0,0,0);CHKERRQ(ierr);
242fa534816SMatthew G. Knepley   if (dm->sfNatural) {
243fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(gv, &inarray);CHKERRQ(ierr);
244*c3b366b1Sprj-     ierr = VecGetArray(nv, &outarray);CHKERRQ(ierr);
245fa534816SMatthew G. Knepley     ierr = PetscSFBcastEnd(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray);CHKERRQ(ierr);
246fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(gv, &inarray);CHKERRQ(ierr);
247fa534816SMatthew G. Knepley     ierr = VecRestoreArray(nv, &outarray);CHKERRQ(ierr);
248fa534816SMatthew G. Knepley   }
249fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_GlobalToNaturalEnd,dm,0,0,0);CHKERRQ(ierr);
250fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
251fa534816SMatthew G. Knepley }
252fa534816SMatthew G. Knepley 
253fa534816SMatthew G. Knepley /*@
254fa534816SMatthew G. Knepley   DMPlexNaturalToGlobalBegin - Rearranges a Vector in the natural order to the Global order.
255fa534816SMatthew G. Knepley 
256fa534816SMatthew G. Knepley   Collective on dm
257fa534816SMatthew G. Knepley 
258fa534816SMatthew G. Knepley   Input Parameters:
259fa534816SMatthew G. Knepley + dm - The distributed DMPlex
260fa534816SMatthew G. Knepley - nv - The natural Vec
261fa534816SMatthew G. Knepley 
262fa534816SMatthew G. Knepley   Output Parameters:
263fa534816SMatthew G. Knepley . gv - The global Vec
264fa534816SMatthew G. Knepley 
26542ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
266fa534816SMatthew G. Knepley 
267fa534816SMatthew G. Knepley   Level: intermediate
268fa534816SMatthew G. Knepley 
269fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(),DMPlexGlobalToNaturalEnd()
270fa534816SMatthew G. Knepley @*/
271fa534816SMatthew G. Knepley PetscErrorCode DMPlexNaturalToGlobalBegin(DM dm, Vec nv, Vec gv)
272fa534816SMatthew G. Knepley {
273fa534816SMatthew G. Knepley   const PetscScalar *inarray;
274fa534816SMatthew G. Knepley   PetscScalar       *outarray;
275fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
276fa534816SMatthew G. Knepley 
277fa534816SMatthew G. Knepley   PetscFunctionBegin;
278fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_NaturalToGlobalBegin,dm,0,0,0);CHKERRQ(ierr);
279fa534816SMatthew G. Knepley   if (dm->sfNatural) {
280b64f75a9SBlaise Bourdin     /* We only have acces to the SF that goes from Global to Natural.
281b64f75a9SBlaise Bourdin        Instead of inverting dm->sfNatural, we can call PetscSFReduceBegin/End with MPI_Op MPI_SUM.
282b64f75a9SBlaise Bourdin        Here the SUM really does nothing since sfNatural is one to one, as long as gV is set to zero first. */
283b64f75a9SBlaise Bourdin     ierr = VecZeroEntries(gv);CHKERRQ(ierr);
284fa534816SMatthew G. Knepley     ierr = VecGetArray(gv, &outarray);CHKERRQ(ierr);
285fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(nv, &inarray);CHKERRQ(ierr);
286fa534816SMatthew G. Knepley     ierr = PetscSFReduceBegin(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray, MPI_SUM);CHKERRQ(ierr);
287fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(nv, &inarray);CHKERRQ(ierr);
288fa534816SMatthew G. Knepley     ierr = VecRestoreArray(gv, &outarray);CHKERRQ(ierr);
28942ea106eSTristan 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");
290fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_NaturalToGlobalBegin,dm,0,0,0);CHKERRQ(ierr);
291fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
292fa534816SMatthew G. Knepley }
293fa534816SMatthew G. Knepley 
294fa534816SMatthew G. Knepley /*@
295fa534816SMatthew G. Knepley   DMPlexNaturalToGlobalEnd - Rearranges a Vector in the natural order to the Global order.
296fa534816SMatthew G. Knepley 
297fa534816SMatthew G. Knepley   Collective on dm
298fa534816SMatthew G. Knepley 
299fa534816SMatthew G. Knepley   Input Parameters:
300fa534816SMatthew G. Knepley + dm - The distributed DMPlex
301fa534816SMatthew G. Knepley - nv - The natural Vec
302fa534816SMatthew G. Knepley 
303fa534816SMatthew G. Knepley   Output Parameters:
304fa534816SMatthew G. Knepley . gv - The global Vec
305fa534816SMatthew G. Knepley 
30642ea106eSTristan Konolige   Note: The user must call DMSetUseNatural(dm, PETSC_TRUE) before DMPlexDistribute().
307fa534816SMatthew G. Knepley 
308fa534816SMatthew G. Knepley   Level: intermediate
309fa534816SMatthew G. Knepley 
310fa534816SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField(), DMPlexNaturalToGlobalBegin(), DMPlexGlobalToNaturalBegin()
311fa534816SMatthew G. Knepley  @*/
312fa534816SMatthew G. Knepley PetscErrorCode DMPlexNaturalToGlobalEnd(DM dm, Vec nv, Vec gv)
313fa534816SMatthew G. Knepley {
314fa534816SMatthew G. Knepley   const PetscScalar *inarray;
315fa534816SMatthew G. Knepley   PetscScalar       *outarray;
316fa534816SMatthew G. Knepley   PetscErrorCode     ierr;
317fa534816SMatthew G. Knepley 
318fa534816SMatthew G. Knepley   PetscFunctionBegin;
319fa534816SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_NaturalToGlobalEnd,dm,0,0,0);CHKERRQ(ierr);
320fa534816SMatthew G. Knepley   if (dm->sfNatural) {
321fa534816SMatthew G. Knepley     ierr = VecGetArrayRead(nv, &inarray);CHKERRQ(ierr);
322*c3b366b1Sprj-     ierr = VecGetArray(gv, &outarray);CHKERRQ(ierr);
323fa534816SMatthew G. Knepley     ierr = PetscSFReduceEnd(dm->sfNatural, MPIU_SCALAR, (PetscScalar *) inarray, outarray, MPI_SUM);CHKERRQ(ierr);
324fa534816SMatthew G. Knepley     ierr = VecRestoreArrayRead(nv, &inarray);CHKERRQ(ierr);
325fa534816SMatthew G. Knepley     ierr = VecRestoreArray(gv, &outarray);CHKERRQ(ierr);
326fa534816SMatthew G. Knepley   }
327fa534816SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_NaturalToGlobalEnd,dm,0,0,0);CHKERRQ(ierr);
328fa534816SMatthew G. Knepley   PetscFunctionReturn(0);
329fa534816SMatthew G. Knepley }
330