Actual source code: ex1.c

  1: static char help[] = "Check if DMClone for DMNetwork Correctly Shallow Clones Topology Only \n\n";

  3: #include <petscdmnetwork.h>

  5: /*
  6: CreateStarGraphEdgeList - Create a k-Star Graph Edgelist on current processor
  7:   Not Collective

  9:   Input Parameters:
 10: . k    - order of the star graph (number of edges)
 11: . directin - if true direction of edges is towards the center vertex, otherwise they are directed out of the center vertex.

 13:   Output Parameters:
 14: .  ne - number of edges of this star graph
 15: .  edgelist - list of edges for this star graph, this is a one dimensional array with pairs of entries being the two vertices (in global numbering of the vertices) of each edge,
 16:               [first vertex of first edge, second vertex of first edge, first vertex of second edge, second vertex of second edge, etc].

 18:               User is responsible for deallocating this memory.
 19: */
 20: PetscErrorCode CreateStarGraphEdgeList(PetscInt k, PetscBool directin, PetscInt *ne, PetscInt *edgelist[])
 21: {
 22:   PetscInt i;

 24:   *ne = k;
 25:   PetscCalloc1(2 * k, edgelist);

 27:   if (directin) {
 28:     for (i = 0; i < k; i++) {
 29:       (*edgelist)[2 * i]     = i + 1;
 30:       (*edgelist)[2 * i + 1] = 0;
 31:     }
 32:   } else {
 33:     for (i = 0; i < k; i++) {
 34:       (*edgelist)[2 * i]     = 0;
 35:       (*edgelist)[2 * i + 1] = i + 1;
 36:     }
 37:   }
 38:   return 0;
 39: }

 41: /*
 42: CreateSimpleStarGraph - Create a Distributed k-Star Graph DMNetwork with a single PetscInt component on
 43: all edges and vertices, aselectable number of dofs on vertices and edges. Intended mostly to be used for testing purposes.

 45:   Input Parameters:
 46: . comm       - the communicator of the dm
 47: . numdofvert - number of degrees of freedom (dofs) on vertices
 48: . numdofedge - number of degrees of freedom (dofs) on edges
 49: . k          - order of the star graph (number of edges)
 50: . directin   - if true direction of edges is towards the center vertex, otherwise they are directed out of the center vertex

 52:   Output Parameters:
 53: . newdm       - The created and distributed simple Star Graph
 54: */
 55: PetscErrorCode CreateSimpleStarGraph(MPI_Comm comm, PetscInt numdofvert, PetscInt numdofedge, PetscInt k, PetscBool directin, DM *newdm)
 56: {
 57:   DM          dm;
 58:   PetscMPIInt rank;
 59:   PetscInt    ne       = 0, compkey, eStart, eEnd, vStart, vEnd, e, v;
 60:   PetscInt   *edgelist = NULL, *compedge, *compvert;

 62:   DMNetworkCreate(comm, &dm);
 63:   DMNetworkSetNumSubNetworks(dm, PETSC_DECIDE, 1);
 64:   MPI_Comm_rank(comm, &rank);
 65:   if (rank == 0) CreateStarGraphEdgeList(k, directin, &ne, &edgelist);
 66:   DMNetworkAddSubnetwork(dm, "Main", ne, edgelist, NULL);
 67:   DMNetworkRegisterComponent(dm, "dummy", sizeof(PetscInt), &compkey);
 68:   DMNetworkLayoutSetUp(dm);
 69:   PetscFree(edgelist);
 70:   DMNetworkGetEdgeRange(dm, &eStart, &eEnd);
 71:   DMNetworkGetVertexRange(dm, &vStart, &vEnd);
 72:   PetscMalloc2(eEnd - eStart, &compedge, vEnd - vStart, &compvert);
 73:   for (e = eStart; e < eEnd; e++) {
 74:     compedge[e - eStart] = e;
 75:     DMNetworkAddComponent(dm, e, compkey, &compedge[e - eStart], numdofedge);
 76:   }
 77:   for (v = vStart; v < vEnd; v++) {
 78:     compvert[v - vStart] = v;
 79:     DMNetworkAddComponent(dm, v, compkey, &compvert[v - vStart], numdofvert);
 80:   }
 81:   DMSetFromOptions(dm);
 82:   DMViewFromOptions(dm, NULL, "-dm_view");
 83:   DMSetUp(dm);
 84:   PetscFree2(compedge, compvert);
 85:   DMNetworkDistribute(&dm, 0);
 86:   *newdm = dm;
 87:   return 0;
 88: }

 90: int main(int argc, char **argv)
 91: {
 92:   DM           dm, dmclone, plex;
 93:   PetscInt     e, eStart, eEnd, ndofs, ndofsprev;
 94:   PetscInt    *compprev, *comp, compkey;
 95:   PetscInt     dofv = 1, dofe = 1, ne = 1;
 96:   PetscSection sec;
 97:   Vec          vec;

100:   PetscInitialize(&argc, &argv, NULL, help);
101:   /* create a distributed k-Star graph DMNetwork */
102:   PetscOptionsGetInt(NULL, NULL, "-dofv", &dofv, NULL);
103:   PetscOptionsGetInt(NULL, NULL, "-dofe", &dofe, NULL);
104:   PetscOptionsGetInt(NULL, NULL, "-ne", &ne, NULL);
105:   CreateSimpleStarGraph(PETSC_COMM_WORLD, dofv, dofe, ne, PETSC_TRUE, &dm);
106:   DMNetworkGetEdgeRange(dm, &eStart, &eEnd);

108:   /* check if cloning changed any component */
109:   if (eStart < eEnd) DMNetworkGetComponent(dm, eStart, 0, NULL, (void **)&compprev, &ndofsprev);
110:   DMClone(dm, &dmclone);
111:   if (eStart < eEnd) {
112:     DMNetworkGetComponent(dm, eStart, 0, NULL, (void **)&comp, &ndofs);
115:   }

117:   /* register new components to the clone and add a dummy component to every point */
118:   DMNetworkRegisterComponent(dmclone, "dummyclone", sizeof(PetscInt), &compkey);
119:   DMNetworkGetEdgeRange(dmclone, &eStart, &eEnd);
120:   PetscMalloc1(eEnd - eStart, &comp);
121:   for (e = eStart; e < eEnd; e++) {
122:     comp[e - eStart] = e;
123:     DMNetworkAddComponent(dmclone, e, compkey, &comp[e - eStart], 2);
124:   }
125:   DMNetworkFinalizeComponents(dmclone);
126:   PetscFree(comp);

128:   PetscPrintf(PETSC_COMM_WORLD, " dm: \n");
129:   DMView(dm, PETSC_VIEWER_STDOUT_WORLD);
130:   DMNetworkGetPlex(dm, &plex);
131:   DMGetLocalSection(plex, &sec);
132:   PetscSectionView(sec, PETSC_VIEWER_STDOUT_WORLD);

134:   PetscPrintf(PETSC_COMM_WORLD, "\n dmclone: \n");
135:   DMView(dmclone, PETSC_VIEWER_STDOUT_WORLD);
136:   DMNetworkGetPlex(dmclone, &plex);
137:   DMGetLocalSection(plex, &sec);
138:   PetscSectionView(sec, PETSC_VIEWER_STDOUT_WORLD);

140:   /* create Vectors */
141:   DMCreateGlobalVector(dm, &vec);
142:   VecSet(vec, 1.0);
143:   PetscPrintf(PETSC_COMM_WORLD, "\n dm vec:\n");
144:   VecView(vec, PETSC_VIEWER_STDOUT_WORLD);
145:   VecDestroy(&vec);

147:   DMCreateGlobalVector(dmclone, &vec);
148:   VecSet(vec, 2.0);
149:   PetscPrintf(PETSC_COMM_WORLD, "\n dmclone vec:\n");
150:   VecView(vec, PETSC_VIEWER_STDOUT_WORLD);
151:   VecDestroy(&vec);

153:   DMDestroy(&dm);
154:   DMDestroy(&dmclone);
155:   PetscFinalize();
156: }

158: /*TEST

160:   test:
161:     suffix: 0
162:     args:

164:   test:
165:     suffix: 1
166:     nsize: 2
167:     args: -dofv 2 -dofe 2 -ne 2

169:  TEST*/