Actual source code: ex2.c

  1: static char help[] = "Test DMCreateCoordinateDM_Network, and related functions \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: static PetscErrorCode StarGraphCreateEdgeList(PetscInt k, PetscBool directin, PetscInt *ne, PetscInt *edgelist[])
 21: {
 22:   PetscFunctionBegin;
 23:   *ne = k;
 24:   PetscCall(PetscCalloc1(2 * k, edgelist));

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

 40: /* Simple Circular embedding of the star graph */
 41: static PetscErrorCode StarGraphSetCoordinates(DM dm, PetscReal *vcolor)
 42: {
 43:   DM           cdm;
 44:   Vec          Coord;
 45:   PetscScalar *coord;
 46:   PetscInt     vStart, vEnd, v, vglobal, compkey = 0, off, NVert;
 47:   PetscReal    theta;

 49:   PetscFunctionBegin;
 50:   PetscCall(DMSetCoordinateDim(dm, 2));
 51:   PetscCall(DMGetCoordinateDM(dm, &cdm));

 53:   PetscCall(DMNetworkGetVertexRange(cdm, &vStart, &vEnd));
 54:   PetscCall(DMNetworkRegisterComponent(cdm, "coordinate", sizeof(PetscReal), &compkey));
 55:   for (v = vStart; v < vEnd; v++) {
 56:     PetscCall(DMNetworkGetGlobalVertexIndex(cdm, v, &vglobal));
 57:     vcolor[v - vStart] = vglobal;
 58:     PetscCall(DMNetworkAddComponent(cdm, v, compkey, &vcolor[v - vStart], 2));
 59:   }
 60:   PetscCall(DMNetworkFinalizeComponents(cdm));

 62:   PetscCall(DMCreateLocalVector(cdm, &Coord));
 63:   PetscCall(VecGetArray(Coord, &coord));
 64:   PetscCall(DMNetworkGetNumVertices(cdm, NULL, &NVert));
 65:   theta = 2 * PETSC_PI / (NVert - 1);
 66:   for (v = vStart; v < vEnd; v++) {
 67:     PetscCall(DMNetworkGetGlobalVertexIndex(cdm, v, &vglobal));
 68:     PetscCall(DMNetworkGetLocalVecOffset(cdm, v, 0, &off));
 69:     if (vglobal == 0) {
 70:       coord[off]     = 0.0;
 71:       coord[off + 1] = 0.0;
 72:     } else {
 73:       /* embed on the unit circle */
 74:       coord[off]     = PetscCosReal(theta * (vglobal - 1));
 75:       coord[off + 1] = PetscSinReal(theta * (vglobal - 1));
 76:     }
 77:   }
 78:   PetscCall(VecRestoreArray(Coord, &coord));
 79:   PetscCall(DMSetCoordinatesLocal(dm, Coord));
 80:   PetscCall(VecDestroy(&Coord));
 81:   PetscFunctionReturn(PETSC_SUCCESS);
 82: }

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

 88:   Input Parameters:
 89: . comm       - the communicator of the dm
 90: . numdofvert - number of degrees of freedom (dofs) on vertices
 91: . numdofedge - number of degrees of freedom (dofs) on edges
 92: . k          - order of the star graph (number of edges)
 93: . directin   - if true direction of edges is towards the center vertex, otherwise they are directed out of the center vertex

 95:   Output Parameters:
 96: . newdm       - The created and distributed simple Star Graph
 97: */
 98: static PetscErrorCode StarGraphCreate(MPI_Comm comm, PetscInt numdofvert, PetscInt numdofedge, PetscInt k, PetscBool directin, DM *newdm)
 99: {
100:   DM          dm;
101:   PetscMPIInt rank;
102:   PetscInt    ne       = 0, compkey, eStart, eEnd, vStart, vEnd, e, v;
103:   PetscInt   *edgelist = NULL, *compedge, *compvert;
104:   PetscReal  *vcolor;

106:   PetscFunctionBegin;
107:   PetscCall(DMNetworkCreate(comm, &dm));
108:   PetscCall(DMNetworkSetNumSubNetworks(dm, PETSC_DECIDE, 1));
109:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
110:   if (rank == 0) PetscCall(StarGraphCreateEdgeList(k, directin, &ne, &edgelist));
111:   PetscCall(DMNetworkAddSubnetwork(dm, "Main", ne, edgelist, NULL));
112:   PetscCall(DMNetworkRegisterComponent(dm, "unused", sizeof(PetscInt), &compkey));
113:   PetscCall(DMNetworkLayoutSetUp(dm));
114:   PetscCall(PetscFree(edgelist));
115:   PetscCall(DMNetworkGetEdgeRange(dm, &eStart, &eEnd));
116:   PetscCall(DMNetworkGetVertexRange(dm, &vStart, &vEnd));
117:   PetscCall(PetscCalloc3(eEnd - eStart, &compedge, vEnd - vStart, &compvert, vEnd - vStart, &vcolor));
118:   for (e = eStart; e < eEnd; e++) {
119:     compedge[e - eStart] = e;
120:     PetscCall(DMNetworkAddComponent(dm, e, compkey, &compedge[e - eStart], numdofedge));
121:   }
122:   for (v = vStart; v < vEnd; v++) {
123:     compvert[v - vStart] = v;
124:     PetscCall(DMNetworkAddComponent(dm, v, compkey, &compvert[v - vStart], numdofvert));
125:   }

127:   PetscCall(StarGraphSetCoordinates(dm, vcolor));

129:   PetscCall(DMSetFromOptions(dm));
130:   PetscCall(DMSetUp(dm));
131:   PetscCall(PetscFree3(compedge, compvert, vcolor));

133:   *newdm = dm;
134:   PetscFunctionReturn(PETSC_SUCCESS);
135: }

137: /* This subroutine is used in petsc/src/snes/tutorials/network/ex1.c */
138: static PetscErrorCode CoordinatePrint(DM dm)
139: {
140:   DM                 dmclone;
141:   PetscInt           cdim, v, off, vglobal, vStart, vEnd;
142:   const PetscScalar *carray;
143:   Vec                coords;
144:   MPI_Comm           comm;
145:   PetscMPIInt        rank;

147:   PetscFunctionBegin;
148:   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
149:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

151:   PetscCall(DMGetCoordinateDM(dm, &dmclone));
152:   PetscCall(DMNetworkGetVertexRange(dm, &vStart, &vEnd));
153:   PetscCall(DMGetCoordinatesLocal(dm, &coords));

155:   PetscCall(DMGetCoordinateDim(dm, &cdim));
156:   PetscCall(VecGetArrayRead(coords, &carray));

158:   PetscCall(PetscPrintf(MPI_COMM_WORLD, "\nCoordinatePrint, cdim %" PetscInt_FMT ":\n", cdim));
159:   PetscCall(PetscSynchronizedPrintf(MPI_COMM_WORLD, "[%d]\n", rank));
160:   for (v = vStart; v < vEnd; v++) {
161:     PetscCall(DMNetworkGetLocalVecOffset(dmclone, v, 0, &off));
162:     PetscCall(DMNetworkGetGlobalVertexIndex(dmclone, v, &vglobal));
163:     switch (cdim) {
164:     case 2:
165:       PetscCall(PetscSynchronizedPrintf(MPI_COMM_WORLD, "Vertex: %" PetscInt_FMT ", x =  %f y = %f \n", vglobal, (double)PetscRealPart(carray[off]), (double)PetscRealPart(carray[off + 1])));
166:       break;
167:     default:
168:       PetscCheck(cdim == 2, MPI_COMM_WORLD, PETSC_ERR_SUP, "Only supports Network embedding dimension of 2, not supplied  %" PetscInt_FMT, cdim);
169:       break;
170:     }
171:   }
172:   PetscCall(PetscSynchronizedFlush(MPI_COMM_WORLD, NULL));
173:   PetscCall(VecRestoreArrayRead(coords, &carray));
174:   PetscFunctionReturn(PETSC_SUCCESS);
175: }

177: int main(int argc, char **argv)
178: {
179:   DM          dm;
180:   PetscInt    dofv = 1, dofe = 1, ne = 1;
181:   PetscMPIInt rank;
182:   PetscBool   viewCSV = PETSC_FALSE;

184:   PetscFunctionBeginUser;
185:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
186:   PetscCallMPI(MPI_Comm_rank(MPI_COMM_WORLD, &rank));

188:   /* create a distributed k-Star graph DMNetwork */
189:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dofv", &dofv, NULL));
190:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dofe", &dofe, NULL));
191:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-ne", &ne, NULL));
192:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-viewCSV", &viewCSV, NULL));

194:   /* create and setup a quick R^2 embedding of the star graph */
195:   PetscCall(StarGraphCreate(PETSC_COMM_WORLD, dofv, dofe, ne, PETSC_TRUE, &dm));

197:   PetscCall(DMNetworkDistribute(&dm, 0));
198:   if (viewCSV) { /* CSV View of network with coordinates */
199:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_CSV));
200:     PetscCall(DMView(dm, PETSC_VIEWER_STDOUT_WORLD));
201:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
202:   } else {
203:     PetscCall(DMView(dm, PETSC_VIEWER_STDOUT_WORLD));
204:   }

206:   /* print or view the coordinates of each vertex */
207:   PetscCall(CoordinatePrint(dm));

209:   PetscCall(DMDestroy(&dm));
210:   PetscCall(PetscFinalize());
211: }

213: /*TEST

215:   test:
216:     suffix: 0
217:     args: -ne 4

219:   test:
220:     suffix: 1
221:     nsize: 2
222:     args: -ne 4 -petscpartitioner_type simple

224:  TEST*/