xref: /petsc/src/dm/impls/stag/stagintern.c (revision 6d8694c4fbab79f9439f1ad13c0386ba7ee1ca4b)
1 /* DMStag dimension-independent internal functions. If added to the public API, these would move to stagutils.c */
2 
3 #include <petsc/private/dmstagimpl.h> /*I  "petscdmstag.h"   I*/
4 
5 /*
6   DMStagDuplicateWithoutSetup - duplicate a `DMSTAG` object without setting it up
7 
8   Collective
9 
10   Input Parameters:
11 + dm   - The original `DM` object
12 - comm - the MPI communicator for the new DM (`MPI_COMM_NULL` to use the same communicator as `dm`)
13 
14   Output Parameter:
15 . newdm - The new `DM` object
16 
17   Level: developer
18 
19   Developer Notes:
20   Copies over all of the state for a `DMSTAG` object, except that which is
21   populated during `DMSetUp()`.  This function is used within (all) other
22   functions that require an un-setup clone, which is common when duplicating,
23   coarsening, refining, or creating compatible `DM`s with different fields.  For
24   this reason it also accepts an MPI communicator as an argument (though note
25   that at the time of this writing, implementations of `DMCoarsen()` and `DMRefine()`
26   don't usually seem to respect their "comm" arguments). This function could be
27   pushed up to the general `DM` API (and perhaps given a different name).
28 
29 .seealso: [](ch_stag), `DMSTAG`, `DM`, `DMClone()`, `DMStagCreateCompatibleDMStag()`, `DMCoarsen()`, `DMRefine()`
30 */
DMStagDuplicateWithoutSetup(DM dm,MPI_Comm comm,DM * newdm)31 PetscErrorCode DMStagDuplicateWithoutSetup(DM dm, MPI_Comm comm, DM *newdm)
32 {
33   DM_Stag *const stag = (DM_Stag *)dm->data;
34   DM_Stag       *newstag;
35   PetscInt       dim;
36   MPI_Comm       newcomm;
37 
38   PetscFunctionBegin;
39   PetscValidHeaderSpecificType(dm, DM_CLASSID, 1, DMSTAG);
40   newcomm = (comm == MPI_COMM_NULL) ? PetscObjectComm((PetscObject)dm) : comm;
41   PetscCall(DMCreate(newcomm, newdm));
42   PetscCall(DMGetDimension(dm, &dim));
43   PetscCall(DMSetDimension(*newdm, dim));
44 
45   /* Call routine to define all data required for setup */
46   PetscCall(DMStagInitialize(stag->boundaryType[0], stag->boundaryType[1], stag->boundaryType[2], stag->N[0], stag->N[1], stag->N[2], stag->nRanks[0], stag->nRanks[1], stag->nRanks[2], stag->dof[0], stag->dof[1], stag->dof[2], stag->dof[3], stag->stencilType,
47                              stag->stencilWidth, stag->l[0], stag->l[1], stag->l[2], *newdm));
48 
49   /* Copy all data unrelated to setup */
50   newstag = (DM_Stag *)(*newdm)->data;
51   PetscCall(PetscStrallocpy(stag->coordinateDMType, (char **)&newstag->coordinateDMType));
52   PetscCall(PetscArraycpy(newstag->refineFactor, stag->refineFactor, DMSTAG_MAX_DIM));
53 
54   /* Copy vectype and mattype from original DM */
55   PetscCall(DMSetVecType(*newdm, dm->vectype));
56   PetscCall(DMSetMatType(*newdm, dm->mattype));
57   PetscFunctionReturn(PETSC_SUCCESS);
58 }
59 
60 /* Populate data created after DMCreate_Stag() is called, which is used by DMSetUp_Stag(),
61    such as the grid dimensions and dof information. Arguments are ignored for dimensions
62    less than three. */
DMStagInitialize(DMBoundaryType bndx,DMBoundaryType bndy,DMBoundaryType bndz,PetscInt M,PetscInt N,PetscInt P,PetscInt m,PetscInt n,PetscInt p,PetscInt dof0,PetscInt dof1,PetscInt dof2,PetscInt dof3,DMStagStencilType stencilType,PetscInt stencilWidth,const PetscInt lx[],const PetscInt ly[],const PetscInt lz[],DM dm)63 PetscErrorCode DMStagInitialize(DMBoundaryType bndx, DMBoundaryType bndy, DMBoundaryType bndz, PetscInt M, PetscInt N, PetscInt P, PetscInt m, PetscInt n, PetscInt p, PetscInt dof0, PetscInt dof1, PetscInt dof2, PetscInt dof3, DMStagStencilType stencilType, PetscInt stencilWidth, const PetscInt lx[], const PetscInt ly[], const PetscInt lz[], DM dm)
64 {
65   PetscFunctionBegin;
66   PetscCall(DMSetType(dm, DMSTAG));
67   PetscCall(DMStagSetBoundaryTypes(dm, bndx, bndy, bndz));
68   PetscCall(DMStagSetGlobalSizes(dm, M, N, P));
69   PetscCall(DMStagSetNumRanks(dm, m, n, p));
70   PetscCall(DMStagSetStencilType(dm, stencilType));
71   PetscCall(DMStagSetStencilWidth(dm, stencilWidth));
72   PetscCall(DMStagSetDOF(dm, dof0, dof1, dof2, dof3));
73   PetscCall(DMStagSetOwnershipRanges(dm, lx, ly, lz));
74   PetscFunctionReturn(PETSC_SUCCESS);
75 }
76