xref: /petsc/doc/manual/dmnetwork.md (revision b11d9968bc79904c690b122f9399be46447eb113)
1*7f296bb3SBarry Smith(ch_network)=
2*7f296bb3SBarry Smith
3*7f296bb3SBarry Smith# Networks
4*7f296bb3SBarry Smith
5*7f296bb3SBarry SmithThe `DMNETWORK` class provides
6*7f296bb3SBarry Smithabstractions for representing general unstructured networks such as
7*7f296bb3SBarry Smithcommunication networks, power grid, computer networks, transportation
8*7f296bb3SBarry Smithnetworks, electrical circuits, graphs, and others.
9*7f296bb3SBarry Smith
10*7f296bb3SBarry Smith## Application flow
11*7f296bb3SBarry Smith
12*7f296bb3SBarry SmithThe general flow of an application code using `DMNETWORK` is as
13*7f296bb3SBarry Smithfollows:
14*7f296bb3SBarry Smith
15*7f296bb3SBarry Smith1. Create a network object.
16*7f296bb3SBarry Smith
17*7f296bb3SBarry Smith   ```
18*7f296bb3SBarry Smith   DMNetworkCreate(MPI_Comm comm, DM *dm);
19*7f296bb3SBarry Smith   ```
20*7f296bb3SBarry Smith
21*7f296bb3SBarry Smith2. Create components and register them with the network. A “component”
22*7f296bb3SBarry Smith   is specific application data at a vertex/edge of the network required
23*7f296bb3SBarry Smith   for its residual evaluation. For example, components could be
24*7f296bb3SBarry Smith   resistor/inductor data for circuit applications, edge weights for
25*7f296bb3SBarry Smith   graph problems, or generator/transmission line data for power grids.
26*7f296bb3SBarry Smith   Components are registered by calling
27*7f296bb3SBarry Smith
28*7f296bb3SBarry Smith   ```
29*7f296bb3SBarry Smith   DMNetworkRegisterComponent(DM dm, const char *name, size_t size, PetscInt *compkey);
30*7f296bb3SBarry Smith   ```
31*7f296bb3SBarry Smith
32*7f296bb3SBarry Smith   Here, `name` is the component name, `size` is the size of
33*7f296bb3SBarry Smith   component data, and `compkey` is an integer key that can be
34*7f296bb3SBarry Smith   used for setting/getting the component at a vertex or an edge.
35*7f296bb3SBarry Smith
36*7f296bb3SBarry Smith3. A `DMNETWORK` can consist of one or more physical subnetworks. Each subnetwork has its own mathematical model. When
37*7f296bb3SBarry Smith   multiple subnetworks are used one can (optionally) provide
38*7f296bb3SBarry Smith   coupling information between subnetworks. That is vertices that are *shared* between multiple subnetworks; edges can only belong to a single subnetwork. The
39*7f296bb3SBarry Smith   number of subnetwork is set by calling
40*7f296bb3SBarry Smith
41*7f296bb3SBarry Smith   ```
42*7f296bb3SBarry Smith   DMNetworkSetNumSubNetworks(DM dm, PetscInt nsubnet, PetscInt Nsubnet);
43*7f296bb3SBarry Smith   ```
44*7f296bb3SBarry Smith
45*7f296bb3SBarry Smith   Here, `nsubnet` and `Nsubnet` are the local and global number of subnetworks.
46*7f296bb3SBarry Smith
47*7f296bb3SBarry Smith4. A subnetwork is added to the network by calling
48*7f296bb3SBarry Smith
49*7f296bb3SBarry Smith   ```
50*7f296bb3SBarry Smith   DMNetworkAddSubnetwork(DM dm, const char* name, PetscInt ne, PetscInt edgelist[], PetscInt *netnum);
51*7f296bb3SBarry Smith   ```
52*7f296bb3SBarry Smith
53*7f296bb3SBarry Smith   Here `name` is the subnetwork name, `ne` is the number of local edges on the subnetwork, and `edgelist` is the connectivity for the subnetwork.
54*7f296bb3SBarry Smith   The output `netnum` is the global numbering of the subnetwork in the network.
55*7f296bb3SBarry Smith   Each element of `edgelist` is an integer array of size `2*ne`
56*7f296bb3SBarry Smith   containing the edge connectivity for the subnetwork.
57*7f296bb3SBarry Smith
58*7f296bb3SBarry Smith   As an example, consider a network comprised of 2 subnetworks that
59*7f296bb3SBarry Smith   are coupled. The topological information for the network is as
60*7f296bb3SBarry Smith   follows:
61*7f296bb3SBarry Smith
62*7f296bb3SBarry Smith   subnetwork 0: v0 — v1 — v2 — v3
63*7f296bb3SBarry Smith
64*7f296bb3SBarry Smith   subnetwork 1: v1 — v2 — v0
65*7f296bb3SBarry Smith
66*7f296bb3SBarry Smith   The two subnetworks are coupled by merging vertex 0 from subnetwork 0 with vertex 2 from subnetwork 1.
67*7f296bb3SBarry Smith
68*7f296bb3SBarry Smith   The
69*7f296bb3SBarry Smith
70*7f296bb3SBarry Smith   `edgelist`
71*7f296bb3SBarry Smith
72*7f296bb3SBarry Smith    of this network is
73*7f296bb3SBarry Smith
74*7f296bb3SBarry Smith   edgelist[0] = {0,1,1,2,2,3}
75*7f296bb3SBarry Smith
76*7f296bb3SBarry Smith   edgelist[1] = {1,2,2,0}
77*7f296bb3SBarry Smith
78*7f296bb3SBarry Smith   The coupling is done by calling
79*7f296bb3SBarry Smith
80*7f296bb3SBarry Smith   ```
81*7f296bb3SBarry Smith   DMNetworkAddSharedVertices(DM dm, PetscInt anet, PetscInt bnet, PetscInt nsv, PetscInt asv[], PetscInt bsv[]);
82*7f296bb3SBarry Smith   ```
83*7f296bb3SBarry Smith
84*7f296bb3SBarry Smith   Here `anet` and `bnet` are the first and second subnetwork global numberings returned by `DMNetworkAddSubnetwork()`,
85*7f296bb3SBarry Smith   `nsv` is the number of vertices shared by the two subnetworks, `asv` and `bsv` are the vertex indices in the subnetwork `anet` and `bnet` .
86*7f296bb3SBarry Smith
87*7f296bb3SBarry Smith5. The next step is to have `DMNETWORK` create a bare layout (graph) of
88*7f296bb3SBarry Smith   the network by calling
89*7f296bb3SBarry Smith
90*7f296bb3SBarry Smith   ```
91*7f296bb3SBarry Smith   DMNetworkLayoutSetUp(DM dm);
92*7f296bb3SBarry Smith   ```
93*7f296bb3SBarry Smith
94*7f296bb3SBarry Smith6. After completing the previous steps, the network graph is set up, but
95*7f296bb3SBarry Smith   no physics is associated yet. This is done by adding the components
96*7f296bb3SBarry Smith   and setting the number of variables to the vertices and edges.
97*7f296bb3SBarry Smith
98*7f296bb3SBarry Smith   A component and number of variables are added to a vertex/edge by calling
99*7f296bb3SBarry Smith
100*7f296bb3SBarry Smith   ```
101*7f296bb3SBarry Smith   DMNetworkAddComponent(DM dm, PetscInt p, PetscInt compkey, void* compdata, PetscInt nvar)
102*7f296bb3SBarry Smith   ```
103*7f296bb3SBarry Smith
104*7f296bb3SBarry Smith   where `p` is the network vertex/edge point in the range obtained by
105*7f296bb3SBarry Smith   either `DMNetworkGetVertexRange()`/`DMNetworkGetEdgeRange()`, `DMNetworkGetSubnetwork()`, or `DMNetworkGetSharedVertices()`;
106*7f296bb3SBarry Smith   `compkey` is the component key returned when registering the component
107*7f296bb3SBarry Smith   (`DMNetworkRegisterComponent()`); `compdata` holds the data for the
108*7f296bb3SBarry Smith   component; and `nvar` is the number of variables associated to the added component at this network point. `DMNETWORK` supports setting multiple components
109*7f296bb3SBarry Smith   at a vertex/edge. At a shared vertex, `DMNETWORK` currently requires the owner process of the vertex adds all the components and number of variables.
110*7f296bb3SBarry Smith
111*7f296bb3SBarry Smith   `DMNETWORK` currently assumes the component data to be stored in a
112*7f296bb3SBarry Smith   contiguous chunk of memory. As such, it does not do any
113*7f296bb3SBarry Smith   packing/unpacking before/after the component data gets distributed.
114*7f296bb3SBarry Smith   Any such serialization (packing/unpacking) should be done by the
115*7f296bb3SBarry Smith   application.
116*7f296bb3SBarry Smith
117*7f296bb3SBarry Smith7. Set up network internal data structures.
118*7f296bb3SBarry Smith
119*7f296bb3SBarry Smith   ```
120*7f296bb3SBarry Smith   DMSetUp(DM dm);
121*7f296bb3SBarry Smith   ```
122*7f296bb3SBarry Smith
123*7f296bb3SBarry Smith8. Distribute the network (also moves components attached with
124*7f296bb3SBarry Smith   vertices/edges) to multiple processors.
125*7f296bb3SBarry Smith
126*7f296bb3SBarry Smith   ```
127*7f296bb3SBarry Smith   DMNetworkDistribute(DM dm, const char partitioner[], PetscInt overlap, DM *distDM);
128*7f296bb3SBarry Smith   ```
129*7f296bb3SBarry Smith
130*7f296bb3SBarry Smith9. Associate the `DM` with a PETSc solver:
131*7f296bb3SBarry Smith
132*7f296bb3SBarry Smith   ```
133*7f296bb3SBarry Smith   KSPSetDM(KSP ksp, DM dm) or SNESSetDM(SNES snes, DM dm) or TSSetDM(TS ts, DM dm).
134*7f296bb3SBarry Smith   ```
135*7f296bb3SBarry Smith
136*7f296bb3SBarry Smith## Utility functions
137*7f296bb3SBarry Smith
138*7f296bb3SBarry Smith`DMNETWORK` provides several utility functions for operations on the
139*7f296bb3SBarry Smithnetwork. The most commonly used functions are: obtaining iterators for
140*7f296bb3SBarry Smithvertices/edges,
141*7f296bb3SBarry Smith
142*7f296bb3SBarry Smith```
143*7f296bb3SBarry SmithDMNetworkGetEdgeRange(DM dm, PetscInt *eStart, PetscInt *eEnd);
144*7f296bb3SBarry Smith```
145*7f296bb3SBarry Smith
146*7f296bb3SBarry Smith```
147*7f296bb3SBarry SmithDMNetworkGetVertexRange(DM dm, PetscInt *vStart, PetscInt *vEnd);
148*7f296bb3SBarry Smith```
149*7f296bb3SBarry Smith
150*7f296bb3SBarry Smith```
151*7f296bb3SBarry SmithDMNetworkGetSubnetwork(DM dm, PetscInt netnum, PetscInt *nv, PetscInt *ne, const PetscInt **vtx, const PetscInt **edge);
152*7f296bb3SBarry Smith```
153*7f296bb3SBarry Smith
154*7f296bb3SBarry Smithchecking the status of a vertex,
155*7f296bb3SBarry Smith
156*7f296bb3SBarry Smith```
157*7f296bb3SBarry SmithDMNetworkIsGhostVertex(DM dm, PetscInt p, PetscBool *isghost);
158*7f296bb3SBarry Smith```
159*7f296bb3SBarry Smith
160*7f296bb3SBarry Smith```
161*7f296bb3SBarry SmithDMNetworkIsSharedVertex(DM dm, PetscInt p, PetscBool *isshared);
162*7f296bb3SBarry Smith```
163*7f296bb3SBarry Smith
164*7f296bb3SBarry Smithand retrieving local/global indices of vertex/edge component variables for
165*7f296bb3SBarry Smithinserting elements in vectors/matrices,
166*7f296bb3SBarry Smith
167*7f296bb3SBarry Smith```
168*7f296bb3SBarry SmithDMNetworkGetLocalVecOffset(DM dm, PetscInt p, PetscInt compnum, PetscInt *offset);
169*7f296bb3SBarry Smith```
170*7f296bb3SBarry Smith
171*7f296bb3SBarry Smith```
172*7f296bb3SBarry SmithDMNetworkGetGlobalVecOffset(DM dm, PetscInt p, PetscInt compnum, PetscInt *offsetg).
173*7f296bb3SBarry Smith```
174*7f296bb3SBarry Smith
175*7f296bb3SBarry SmithIn network applications, one frequently needs to find the supporting
176*7f296bb3SBarry Smithedges for a vertex or the connecting vertices covering an edge. These
177*7f296bb3SBarry Smithcan be obtained by the following two routines.
178*7f296bb3SBarry Smith
179*7f296bb3SBarry Smith```
180*7f296bb3SBarry SmithDMNetworkGetConnectedVertices(DM dm, PetscInt edge, const PetscInt *vertices[]);
181*7f296bb3SBarry Smith```
182*7f296bb3SBarry Smith
183*7f296bb3SBarry Smith```
184*7f296bb3SBarry SmithDMNetworkGetSupportingEdges(DM dm, PetscInt vertex, PetscInt *nedges, const PetscInt *edges[]).
185*7f296bb3SBarry Smith```
186*7f296bb3SBarry Smith
187*7f296bb3SBarry Smith## Retrieving components and number of variables
188*7f296bb3SBarry Smith
189*7f296bb3SBarry SmithThe components and the corresponding number of variables set at a vertex/edge can be accessed by
190*7f296bb3SBarry Smith
191*7f296bb3SBarry Smith```
192*7f296bb3SBarry SmithDMNetworkGetComponent(DM dm, PetscInt p, PetscInt compnum, PetscInt *compkey, void **component, PetscInt *nvar)
193*7f296bb3SBarry Smith```
194*7f296bb3SBarry Smith
195*7f296bb3SBarry Smithinput `compnum` is the component number, output `compkey` is the key set by `DMNetworkRegisterComponent()`. An example
196*7f296bb3SBarry Smithof accessing and retrieving the components and number of variables at vertices is:
197*7f296bb3SBarry Smith
198*7f296bb3SBarry Smith```
199*7f296bb3SBarry SmithPetscInt Start,End,numcomps,key,v,compnum;
200*7f296bb3SBarry Smithvoid *component;
201*7f296bb3SBarry Smith
202*7f296bb3SBarry SmithDMNetworkGetVertexRange(dm, &Start, &End);
203*7f296bb3SBarry Smithfor (v = Start; v < End; v++) {
204*7f296bb3SBarry Smith  DMNetworkGetNumComponents(dm, v, &numcomps);
205*7f296bb3SBarry Smith  for (compnum=0; compnum < numcomps; compnum++) {
206*7f296bb3SBarry Smith    DMNetworkGetComponent(dm, v, compnum, &key, &component, &nvar);
207*7f296bb3SBarry Smith    compdata = (UserCompDataType)(component);
208*7f296bb3SBarry Smith  }
209*7f296bb3SBarry Smith}
210*7f296bb3SBarry Smith```
211*7f296bb3SBarry Smith
212*7f296bb3SBarry SmithThe above example does not explicitly use the component key. It is
213*7f296bb3SBarry Smithused when different component types are set at different vertices. In
214*7f296bb3SBarry Smiththis case, `compkey` is used to differentiate the component type.
215*7f296bb3SBarry Smith
216*7f296bb3SBarry Smith```{eval-rst}
217*7f296bb3SBarry Smith.. bibliography:: /petsc.bib
218*7f296bb3SBarry Smith    :filter: docname in docnames
219*7f296bb3SBarry Smith
220*7f296bb3SBarry Smith```
221