Actual source code: pipes1.c
petsc-3.9.0 2018-04-07
1: static char help[] = "This example demonstrates the use of DMNetwork \n\\n";
3: /*
4: Example: mpiexec -n <np> ./pipes1 -ts_max_steps 10
5: */
7: #include "wash.h"
8: #include <petscdmnetwork.h>
10: PetscErrorCode WASHIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void* ctx)
11: {
12: PetscErrorCode ierr;
13: Wash wash=(Wash)ctx;
14: DM networkdm;
15: Vec localX,localXdot,localF;
16: const PetscInt *cone;
17: PetscInt vfrom,vto,offsetfrom,offsetto,type,varoffset;
18: PetscInt v,vStart,vEnd,e,eStart,eEnd;
19: PetscBool ghost;
20: PetscScalar *farr,*vf,*juncx,*juncf;
21: Pipe pipe;
22: PipeField *pipex,*pipexdot,*pipef;
23: DMDALocalInfo info;
24: Junction junction;
25: MPI_Comm comm;
26: PetscMPIInt rank,size;
27: const PetscScalar *xarr,*xdotarr;
31: PetscObjectGetComm((PetscObject)ts,&comm);
32: MPI_Comm_rank(comm,&rank);
33: MPI_Comm_size(comm,&size);
35: VecSet(F,0.0);
37: localX = wash->localX;
38: localXdot = wash->localXdot;
40: TSGetDM(ts,&networkdm);
41: DMGetLocalVector(networkdm,&localF);
42: VecSet(localF,0.0);
44: /* update ghost values of locaX and locaXdot */
45: DMGlobalToLocalBegin(networkdm,X,INSERT_VALUES,localX);
46: DMGlobalToLocalEnd(networkdm,X,INSERT_VALUES,localX);
48: DMGlobalToLocalBegin(networkdm,Xdot,INSERT_VALUES,localXdot);
49: DMGlobalToLocalEnd(networkdm,Xdot,INSERT_VALUES,localXdot);
51: VecGetArrayRead(localX,&xarr);
52: VecGetArrayRead(localXdot,&xdotarr);
53: VecGetArray(localF,&farr);
55: /* Initialize localF = localX at non-ghost vertices */
56: DMNetworkGetVertexRange(networkdm,&vStart,&vEnd);
57: for (v=vStart; v<vEnd; v++) {
58: DMNetworkIsGhostVertex(networkdm,v,&ghost);
59: if (!ghost) {
60: DMNetworkGetVariableOffset(networkdm,v,&varoffset);
61: juncx = (PetscScalar*)(xarr+varoffset);
62: juncf = (PetscScalar*)(farr+varoffset);
63: juncf[0] = juncx[0];
64: juncf[1] = juncx[1];
65: }
66: }
68: /* Edge */
69: DMNetworkGetEdgeRange(networkdm,&eStart,&eEnd);
70: for (e=eStart; e<eEnd; e++) {
71: DMNetworkGetComponent(networkdm,e,0,&type,(void**)&pipe);
72: DMNetworkGetVariableOffset(networkdm,e,&varoffset);
73: pipex = (PipeField*)(xarr + varoffset);
74: pipexdot = (PipeField*)(xdotarr + varoffset);
75: pipef = (PipeField*)(farr + varoffset);
77: /* Get boundary values H0 and QL from connected vertices */
78: DMNetworkGetConnectedVertices(networkdm,e,&cone);
79: vfrom = cone[0]; /* local ordering */
80: vto = cone[1];
81: DMNetworkGetVariableOffset(networkdm,vfrom,&offsetfrom);
82: DMNetworkGetVariableOffset(networkdm,vto,&offsetto);
83: if (pipe->boundary.Q0 == PIPE_CHARACTERISTIC) {
84: pipe->boundary.H0 = (xarr+offsetfrom)[1]; /* h_from */
85: } else {
86: pipe->boundary.Q0 = (xarr+offsetfrom)[0]; /* q_from */
87: }
88: if (pipe->boundary.HL == PIPE_CHARACTERISTIC) {
89: pipe->boundary.QL = (xarr+offsetto)[0]; /* q_to */
90: } else {
91: pipe->boundary.HL = (xarr+offsetto)[1]; /* h_to */
92: }
94: /* Evaluate PipeIFunctionLocal() */
95: DMDAGetLocalInfo(pipe->da,&info);
96: PipeIFunctionLocal(&info, t, pipex, pipexdot, pipef, pipe);
98: /* Set F at vfrom */
99: vf = (PetscScalar*)(farr+offsetfrom);
100: if (pipe->boundary.Q0 == PIPE_CHARACTERISTIC) {
101: vf[0] -= pipex[0].q; /* q_vfrom - q[0] */
102: } else {
103: vf[1] -= pipex[0].h; /* h_vfrom - h[0] */
104: }
106: /* Set F at vto */
107: vf = (PetscScalar*)(farr+offsetto);
108: if (pipe->boundary.HL == PIPE_CHARACTERISTIC) {
109: vf[1] -= pipex[pipe->nnodes-1].h; /* h_vto - h[last] */
110: } else {
111: vf[0] -= pipex[pipe->nnodes-1].q; /* q_vto - q[last] */
112: }
113: }
115: /* Set F at boundary vertices */
116: for (v=vStart; v<vEnd; v++) {
117: DMNetworkGetComponent(networkdm,v,0,&type,(void**)&junction);
118: DMNetworkGetVariableOffset(networkdm,v,&varoffset);
119: juncf = (PetscScalar *)(farr + varoffset);
120: if (junction->isEnd == -1) {
121: juncf[1] -= wash->H0;
122: } else if (junction->isEnd == 1) {
123: juncf[0] -= wash->QL;
124: }
125: }
127: VecRestoreArrayRead(localX,&xarr);
128: VecRestoreArrayRead(localXdot,&xdotarr);
129: VecRestoreArray(localF,&farr);
131: DMLocalToGlobalBegin(networkdm,localF,ADD_VALUES,F);
132: DMLocalToGlobalEnd(networkdm,localF,ADD_VALUES,F);
133: DMRestoreLocalVector(networkdm,&localF);
134: /* VecView(F,PETSC_VIEWER_STDOUT_WORLD); */
135: return(0);
136: }
138: PetscErrorCode WASHSetInitialSolution(DM networkdm,Vec X,Wash wash)
139: {
141: PetscInt k,nx,vkey,vfrom,vto,offsetfrom,offsetto;
142: PetscInt type,varoffset;
143: PetscInt e,eStart,eEnd;
144: Vec localX;
145: PetscScalar *xarr;
146: Pipe pipe;
147: Junction junction;
148: const PetscInt *cone;
149: const PetscScalar *xarray;
152: VecSet(X,0.0);
153: DMGetLocalVector(networkdm,&localX);
154: VecGetArray(localX,&xarr);
156: /* Edge */
157: DMNetworkGetEdgeRange(networkdm,&eStart,&eEnd);
158: for (e=eStart; e<eEnd; e++) {
159: DMNetworkGetVariableOffset(networkdm,e,&varoffset);
160: DMNetworkGetComponent(networkdm,e,0,&type,(void**)&pipe);
162: /* get from and to vertices */
163: DMNetworkGetConnectedVertices(networkdm,e,&cone);
164: vfrom = cone[0]; /* local ordering */
165: vto = cone[1];
167: DMNetworkGetVariableOffset(networkdm,vfrom,&offsetfrom);
168: DMNetworkGetVariableOffset(networkdm,vto,&offsetto);
170: /* set initial values for this pipe */
171: /* Q0=0.477432; H0=150.0; needs to be updated by its succeeding pipe. Use SNESSolve()? */
172: PipeComputeSteadyState(pipe, 0.477432, wash->H0);
173: VecGetSize(pipe->x,&nx);
175: VecGetArrayRead(pipe->x,&xarray);
176: /* copy pipe->x to xarray */
177: for (k=0; k<nx; k++) {
178: (xarr+varoffset)[k] = xarray[k];
179: }
181: /* set boundary values into vfrom and vto */
182: if (pipe->boundary.Q0 == PIPE_CHARACTERISTIC) {
183: (xarr+offsetfrom)[0] += xarray[0]; /* Q0 -> vfrom[0] */
184: } else {
185: (xarr+offsetfrom)[1] += xarray[1]; /* H0 -> vfrom[1] */
186: }
188: if (pipe->boundary.HL == PIPE_CHARACTERISTIC) {
189: (xarr+offsetto)[1] += xarray[nx-1]; /* HL -> vto[1] */
190: } else {
191: (xarr+offsetto)[0] += xarray[nx-2]; /* QL -> vto[0] */
192: }
194: /* if vform is a head vertex: */
195: DMNetworkGetComponent(networkdm,vfrom,0,&vkey,(void**)&junction);
196: if (junction->isEnd == -1) { /* head junction */
197: (xarr+offsetfrom)[0] = 0.0; /* 1st Q -- not used */
198: (xarr+offsetfrom)[1] = wash->H0; /* 1st H */
199: }
201: /* if vto is an end vertex: */
202: DMNetworkGetComponent(networkdm,vto,0,&vkey,(void**)&junction);
203: if (junction->isEnd == 1) { /* end junction */
204: (xarr+offsetto)[0] = wash->QL; /* last Q */
205: (xarr+offsetto)[1] = 0.0; /* last H -- not used */
206: }
207: VecRestoreArrayRead(pipe->x,&xarray);
208: }
210: VecRestoreArray(localX,&xarr);
211: DMLocalToGlobalBegin(networkdm,localX,ADD_VALUES,X);
212: DMLocalToGlobalEnd(networkdm,localX,ADD_VALUES,X);
213: DMRestoreLocalVector(networkdm,&localX);
215: #if 0
216: PetscInt N;
217: VecGetSize(X,&N);
218: printf("initial solution %d:\n",N);
219: VecView(X,PETSC_VIEWER_STDOUT_WORLD);
220: #endif
221: return(0);
222: }
224: PetscErrorCode TSDMNetworkMonitor(TS ts, PetscInt step, PetscReal t, Vec x, void *context)
225: {
226: PetscErrorCode ierr;
227: DMNetworkMonitor monitor;
230: monitor = (DMNetworkMonitor)context;
231: DMNetworkMonitorView(monitor,x);
232: return(0);
233: }
235: PetscErrorCode PipesView(Vec X,DM networkdm,Wash wash)
236: {
237: PetscErrorCode ierr;
238: Pipe pipe;
239: PetscInt key,Start,End;
240: PetscMPIInt rank;
241: PetscInt nx,nnodes,nidx,*idx1,*idx2,*idx1_h,*idx2_h,idx_start,i,k,k1,xstart,j1;
242: Vec Xq,Xh,localX;
243: IS is1_q,is2_q,is1_h,is2_h;
244: VecScatter ctx_q,ctx_h;
247: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
249: /* get num of local and global total nnodes */
250: nidx = wash->nnodes_loc;
251: MPIU_Allreduce(&nidx,&nx,1,MPIU_INT,MPI_SUM,PETSC_COMM_WORLD);
253: VecCreate(PETSC_COMM_WORLD,&Xq);
254: if (rank == 0) { /* all entries of Xq are in proc[0] */
255: VecSetSizes(Xq,nx,PETSC_DECIDE);
256: } else {
257: VecSetSizes(Xq,0,PETSC_DECIDE);
258: }
259: VecSetFromOptions(Xq);
260: VecSet(Xq,0.0);
261: VecDuplicate(Xq,&Xh);
263: DMGetLocalVector(networkdm,&localX);
265: /* set idx1 and idx2 */
266: PetscCalloc4(nidx,&idx1,nidx,&idx2,nidx,&idx1_h,nidx,&idx2_h);
268: DMNetworkGetEdgeRange(networkdm,&Start, &End);
270: VecGetOwnershipRange(X,&xstart,NULL);
271: k1 = 0;
272: j1 = 0;
273: for (i = Start; i < End; i++) {
274: DMNetworkGetComponent(networkdm,i,0,&key,(void**)&pipe);
275: nnodes = pipe->nnodes;
276: idx_start = pipe->id*nnodes;
277: for (k=0; k<nnodes; k++) {
278: idx1[k1] = xstart + j1*2*nnodes + 2*k;
279: idx2[k1] = idx_start + k;
281: idx1_h[k1] = xstart + j1*2*nnodes + 2*k + 1;
282: idx2_h[k1] = idx_start + k;
283: k1++;
284: }
285: j1++;
286: }
288: ISCreateGeneral(PETSC_COMM_SELF,nidx,idx1,PETSC_COPY_VALUES,&is1_q);
289: ISCreateGeneral(PETSC_COMM_SELF,nidx,idx2,PETSC_COPY_VALUES,&is2_q);
290: VecScatterCreate(X,is1_q,Xq,is2_q,&ctx_q);
291: VecScatterBegin(ctx_q,X,Xq,INSERT_VALUES,SCATTER_FORWARD);
292: VecScatterEnd(ctx_q,X,Xq,INSERT_VALUES,SCATTER_FORWARD);
294: ISCreateGeneral(PETSC_COMM_SELF,nidx,idx1_h,PETSC_COPY_VALUES,&is1_h);
295: ISCreateGeneral(PETSC_COMM_SELF,nidx,idx2_h,PETSC_COPY_VALUES,&is2_h);
296: VecScatterCreate(X,is1_h,Xh,is2_h,&ctx_h);
297: VecScatterBegin(ctx_h,X,Xh,INSERT_VALUES,SCATTER_FORWARD);
298: VecScatterEnd(ctx_h,X,Xh,INSERT_VALUES,SCATTER_FORWARD);
300: if (!rank) printf("Xq: \n");
301: VecView(Xq,PETSC_VIEWER_STDOUT_WORLD);
302: if (!rank) printf("Xh: \n");
303: VecView(Xh,PETSC_VIEWER_STDOUT_WORLD);
305: VecScatterDestroy(&ctx_q);
306: PetscFree4(idx1,idx2,idx1_h,idx2_h);
307: ISDestroy(&is1_q);
308: ISDestroy(&is2_q);
310: VecScatterDestroy(&ctx_h);
311: ISDestroy(&is1_h);
312: ISDestroy(&is2_h);
314: VecDestroy(&Xq);
315: VecDestroy(&Xh);
316: DMRestoreLocalVector(networkdm,&localX);
317: return(0);
318: }
320: PetscErrorCode WashNetworkCleanUp(Wash wash,PetscInt *edgelist)
321: {
323: PetscMPIInt rank;
326: MPI_Comm_rank(wash->comm,&rank);
327: if (!rank) {
328: PetscFree(edgelist);
329: PetscFree2(wash->junction,wash->pipe);
330: }
331: return(0);
332: }
334: PetscErrorCode WashNetworkCreate(MPI_Comm comm,PetscInt pipesCase,Wash *wash_ptr,PetscInt **elist)
335: {
337: PetscInt nnodes,npipes;
338: PetscMPIInt rank;
339: Wash wash;
340: PetscInt i,numVertices,numEdges;
341: PetscInt *edgelist;
342: Junction junctions=NULL;
343: Pipe pipes=NULL;
346: MPI_Comm_rank(comm,&rank);
348: PetscCalloc1(1,&wash);
349: wash->comm = comm;
350: *wash_ptr = wash;
351: wash->Q0 = 0.477432; /* copied from initial soluiton */
352: wash->H0 = 150.0;
353: wash->HL = 143.488; /* copied from initial soluiton */
354: wash->nnodes_loc = 0;
356: numVertices = 0;
357: numEdges = 0;
358: edgelist = NULL;
360: if (!rank) {
361: PetscPrintf(PETSC_COMM_SELF,"Setup pipesCase %D\n",pipesCase);
362: }
363: nnodes = 6;
364: PetscOptionsGetInt(NULL,NULL, "-npipenodes", &nnodes, NULL);
366: /* Set global number of pipes, edges, and junctions */
367: /*-------------------------------------------------*/
368: switch (pipesCase) {
369: case 0:
370: /* pipeCase 0: */
371: /* =============================
372: v0 --E0--> v1--E1--> v2 --E2-->v3
373: ================================ */
374: npipes = 3;
375: PetscOptionsGetInt(NULL,NULL, "-npipes", &npipes, NULL);
376: wash->nedge = npipes;
377: wash->nvertex = npipes + 1;
379: /* Set local edges and vertices -- proc[0] sets entire network, then distributes */
380: numVertices = 0;
381: numEdges = 0;
382: edgelist = NULL;
383: if (!rank) {
384: numVertices = wash->nvertex;
385: numEdges = wash->nedge;
387: PetscCalloc1(2*numEdges,&edgelist);
388: for (i=0; i<numEdges; i++) {
389: edgelist[2*i] = i; edgelist[2*i+1] = i+1;
390: }
392: /* Add network components */
393: /*------------------------*/
394: PetscCalloc2(numVertices,&junctions,numEdges,&pipes);
395: /* vertex */
396: for (i=0; i<numVertices; i++) {
397: junctions[i].id = i;
398: junctions[i].isEnd = 0;
399: junctions[i].nedges_in = 1; junctions[i].nedges_out = 1;
401: /* Set GPS data */
402: junctions[i].latitude = 0.0;
403: junctions[i].longitude = 0.0;
404: }
405: junctions[0].isEnd = -1;
406: junctions[0].nedges_in = 0;
407: junctions[numVertices-1].isEnd = 1;
408: junctions[numVertices-1].nedges_out = 0;
410: /* edge and pipe */
411: for (i=0; i<numEdges; i++) {
412: pipes[i].id = i;
413: pipes[i].nnodes = nnodes;
414: }
415: }
416: break;
417: case 1:
418: /* pipeCase 1: */
419: /* ==========================
420: v2
421: ^
422: |
423: E2
424: |
425: v0 --E0--> v3--E1--> v1
426: ============================= */
427: npipes = 3;
428: wash->nedge = npipes;
429: wash->nvertex = npipes + 1;
431: /* Set local edges and vertices -- proc[0] sets entire network, then distributes */
432: if (!rank) {
433: numVertices = wash->nvertex;
434: numEdges = wash->nedge;
436: PetscCalloc1(2*numEdges,&edgelist);
437: edgelist[0] = 0; edgelist[1] = 3; /* edge[0] */
438: edgelist[2] = 3; edgelist[3] = 1; /* edge[1] */
439: edgelist[4] = 3; edgelist[5] = 2; /* edge[2] */
441: /* Add network components */
442: /*------------------------*/
443: PetscCalloc2(numVertices,&junctions,numEdges,&pipes);
444: /* vertex */
445: for (i=0; i<numVertices; i++) {
446: junctions[i].id = i;
448: /* Set GPS data */
449: junctions[i].latitude = 0.0;
450: junctions[i].longitude = 0.0;
451: }
452: junctions[0].isEnd = -1; junctions[0].nedges_in = 0; junctions[0].nedges_out = 1;
453: junctions[1].isEnd = 1; junctions[1].nedges_in = 1; junctions[1].nedges_out = 0;
454: junctions[2].isEnd = 1; junctions[2].nedges_in = 1; junctions[2].nedges_out = 0;
455: junctions[3].isEnd = 0; junctions[3].nedges_in = 1; junctions[3].nedges_out = 2;
457: /* edge and pipe */
458: for (i=0; i<numEdges; i++) {
459: pipes[i].id = i;
460: pipes[i].nnodes = nnodes;
461: }
462: }
463: break;
464: case 2:
465: /* pipeCase 2: */
466: /* ==========================
467: v2--> E2
468: |
469: v0 --E0--> v3--E1--> v1
470: ============================= */
472: /* Set application parameters -- to be used in function evalutions */
473: npipes = 3;
474: wash->nedge = npipes;
475: wash->nvertex = npipes + 1;
477: /* Set local edges and vertices -- proc[0] sets entire network, then distributes */
478: if (!rank) {
479: numVertices = wash->nvertex;
480: numEdges = wash->nedge;
482: PetscCalloc1(2*numEdges,&edgelist);
483: edgelist[0] = 0; edgelist[1] = 3; /* edge[0] */
484: edgelist[2] = 3; edgelist[3] = 1; /* edge[1] */
485: edgelist[4] = 2; edgelist[5] = 3; /* edge[2] */
487: /* Add network components */
488: /*------------------------*/
489: PetscCalloc2(numVertices,&junctions,numEdges,&pipes);
490: /* vertex */
491: for (i=0; i<numVertices; i++) {
492: junctions[i].id = i;
494: /* Set GPS data */
495: junctions[i].latitude = 0.0;
496: junctions[i].longitude = 0.0;
497: }
498: junctions[0].isEnd = -1; junctions[0].nedges_in = 0; junctions[0].nedges_out = 1;
499: junctions[1].isEnd = 1; junctions[1].nedges_in = 1; junctions[1].nedges_out = 0;
500: junctions[2].isEnd = -1; junctions[2].nedges_in = 0; junctions[2].nedges_out = 1;
501: junctions[3].isEnd = 0; junctions[3].nedges_in = 2; junctions[3].nedges_out = 1;
503: /* edge and pipe */
504: for (i=0; i<numEdges; i++) {
505: pipes[i].id = i;
506: pipes[i].nnodes = nnodes;
507: }
508: }
509: break;
510: default:
511: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"not done yet");
512: }
514: *wash_ptr = wash;
515: wash->nedge = numEdges;
516: wash->nvertex = numVertices;
517: *elist = edgelist;
518: wash->junction = junctions;
519: wash->pipe = pipes;
520: return(0);
521: }
522: /* ------------------------------------------------------- */
523: int main(int argc,char ** argv)
524: {
525: PetscErrorCode ierr;
526: Wash wash;
527: Junction junctions,junction;
528: Pipe pipe,pipes;
529: PetscInt numEdges,numVertices,KeyPipe,KeyJunction;
530: PetscInt *edgelist = NULL,*edgelists[1];
531: PetscInt i,e,v,eStart,eEnd,vStart,vEnd,key,frombType,tobType;
532: PetscInt vfrom,vto,vkey,type,varoffset;
533: PetscInt from_nedge_in,from_nedge_out,to_nedge_in;
534: const PetscInt *cone;
535: DM networkdm;
536: PetscMPIInt size,rank;
537: PetscReal ftime = 2500.0;
538: Vec X;
539: TS ts;
540: PetscInt steps;
541: TSConvergedReason reason;
542: PetscBool viewpipes;
543: PetscInt pipesCase;
544: DMNetworkMonitor monitor;
546: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
547: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
548: MPI_Comm_size(PETSC_COMM_WORLD,&size);
550: /* Create and setup network */
551: /*--------------------------*/
552: DMNetworkCreate(PETSC_COMM_WORLD,&networkdm);
553: if (size == 1) {
554: DMNetworkMonitorCreate(networkdm,&monitor);
555: }
556: /* Register the components in the network */
557: DMNetworkRegisterComponent(networkdm,"junctionstruct",sizeof(struct _p_Junction),&KeyJunction);
558: DMNetworkRegisterComponent(networkdm,"pipestruct",sizeof(struct _p_Pipe),&KeyPipe);
560: /* Set global number of pipes, edges, and vertices */
561: pipesCase = 2;
562: PetscOptionsGetInt(NULL,NULL, "-case", &pipesCase, NULL);
564: WashNetworkCreate(PETSC_COMM_WORLD,pipesCase,&wash,&edgelist);
565: numEdges = wash->nedge;
566: numVertices = wash->nvertex;
567: junctions = wash->junction;
568: pipes = wash->pipe;
570: /* Set number of vertices and edges */
571: DMNetworkSetSizes(networkdm,1,0,&numVertices,&numEdges,NULL,NULL);
572: /* Add edge connectivity */
573: edgelists[0] = edgelist;
574: DMNetworkSetEdgeList(networkdm,edgelists,NULL);
575: /* Set up the network layout */
576: DMNetworkLayoutSetUp(networkdm);
578: /* Add EDGEDATA component to all edges -- currently networkdm is a sequential network */
579: DMNetworkGetEdgeRange(networkdm,&eStart,&eEnd);
580: DMNetworkGetVertexRange(networkdm,&vStart,&vEnd);
582: for (e = eStart; e < eEnd; e++) {
583: /* Add Pipe component to all edges -- create pipe here */
584: DMNetworkAddComponent(networkdm,e,KeyPipe,&pipes[e-eStart]);
586: /* Add number of variables to each edge */
587: DMNetworkAddNumVariables(networkdm,e,2*pipes[e-eStart].nnodes);
589: if (size == 1) { /* Add monitor -- show Q_{pipes[e-eStart].id}? */
590: DMNetworkMonitorAdd(monitor, "Pipe Q", e, pipes[e-eStart].nnodes, 0, 2, -0.8, 0.8, PETSC_TRUE);
591: DMNetworkMonitorAdd(monitor, "Pipe H", e, pipes[e-eStart].nnodes, 1, 2, -400.0, 800.0, PETSC_TRUE);
592: }
593: }
595: /* Add Junction component to all vertices */
596: for (v = vStart; v < vEnd; v++) {
597: DMNetworkAddComponent(networkdm,v,KeyJunction,&junctions[v-vStart]);
599: /* Add number of variables to vertex */
600: DMNetworkAddNumVariables(networkdm,v,2);
601: }
603: /* Set up DM for use */
604: DMSetUp(networkdm);
605: WashNetworkCleanUp(wash,edgelist);
607: /* Network partitioning and distribution of data */
608: DMNetworkDistribute(&networkdm,0);
610: /* PipeSetUp -- each process only sets its own pipes */
611: /*---------------------------------------------------*/
612: DMNetworkGetEdgeRange(networkdm,&eStart,&eEnd);
613: for (e=eStart; e<eEnd; e++) { /* each edge has only one component, pipe */
614: DMNetworkGetComponent(networkdm,e,0,&type,(void**)&pipe);
615: DMNetworkGetVariableOffset(networkdm,e,&varoffset);
617: /* Setup conntected vertices */
618: DMNetworkGetConnectedVertices(networkdm,e,&cone);
619: vfrom = cone[0]; /* local ordering */
620: vto = cone[1];
622: /* vfrom */
623: DMNetworkGetComponent(networkdm,vfrom,0,&vkey,(void**)&junction);
624: from_nedge_in = junction->nedges_in;
625: from_nedge_out = junction->nedges_out;
627: /* vto */
628: DMNetworkGetComponent(networkdm,vto,0,&vkey,(void**)&junction);
629: to_nedge_in = junction->nedges_in;
631: pipe->comm = PETSC_COMM_SELF; /* must be set here, otherwise crashes in my mac??? */
632: wash->nnodes_loc += pipe->nnodes; /* local total num of nodes, will be used by PipesView() */
633: PipeSetParameters(pipe,
634: 600.0, /* length */
635: pipe->nnodes, /* nnodes -- rm from PipeSetParameters */
636: 0.5, /* diameter */
637: 1200.0, /* a */
638: 0.018); /* friction */
640: /* set boundary conditions for this pipe */
641: if (from_nedge_in <= 1 && from_nedge_out > 0) {
642: frombType = 0;
643: } else {
644: frombType = 1;
645: }
647: if (to_nedge_in == 1) {
648: tobType = 0;
649: } else {
650: tobType = 1;
651: }
653: if (frombType == 0) {
654: pipe->boundary.Q0 = PIPE_CHARACTERISTIC; /* will be obtained from characteristic */
655: pipe->boundary.H0 = wash->H0;
656: } else {
657: pipe->boundary.Q0 = wash->Q0;
658: pipe->boundary.H0 = PIPE_CHARACTERISTIC; /* will be obtained from characteristic */
659: }
660: if (tobType == 0) {
661: pipe->boundary.QL = wash->QL;
662: pipe->boundary.HL = PIPE_CHARACTERISTIC; /* will be obtained from characteristic */
663: } else {
664: pipe->boundary.QL = PIPE_CHARACTERISTIC; /* will be obtained from characteristic */
665: pipe->boundary.HL = wash->HL;
666: }
668: PipeSetUp(pipe);
669: }
671: /* create vectors */
672: DMCreateGlobalVector(networkdm,&X);
673: DMCreateLocalVector(networkdm,&wash->localX);
674: DMCreateLocalVector(networkdm,&wash->localXdot);
676: /* Setup solver */
677: /*--------------------------------------------------------*/
678: TSCreate(PETSC_COMM_WORLD,&ts);
680: TSSetDM(ts,(DM)networkdm);
681: TSSetIFunction(ts,NULL,WASHIFunction,wash);
683: TSSetMaxTime(ts,ftime);
684: TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);
685: TSSetTimeStep(ts,0.1);
686: TSSetType(ts,TSBEULER);
687: if (size == 1) {
688: TSMonitorSet(ts, TSDMNetworkMonitor, monitor, NULL);
689: }
690: TSSetFromOptions(ts);
692: WASHSetInitialSolution(networkdm,X,wash);
694: TSSolve(ts,X);
696: TSGetSolveTime(ts,&ftime);
697: TSGetStepNumber(ts,&steps);
698: TSGetConvergedReason(ts,&reason);
699: PetscPrintf(PETSC_COMM_WORLD,"%s at time %g after %D steps\n",TSConvergedReasons[reason],(double)ftime,steps);
700: /* VecView(X,PETSC_VIEWER_STDOUT_WORLD); */
702: /* View solution q and h */
703: /* --------------------- */
704: viewpipes = PETSC_FALSE;
705: PetscOptionsGetBool(NULL,NULL, "-pipe_view", &viewpipes,NULL);
706: if (viewpipes) {
707: PipesView(X,networkdm,wash);
708: }
710: /* Free spaces */
711: /* ----------- */
712: TSDestroy(&ts);
713: VecDestroy(&X);
714: VecDestroy(&wash->localX);
715: VecDestroy(&wash->localXdot);
717: /* Destroy objects from each pipe that are created in PipeSetUp() */
718: DMNetworkGetEdgeRange(networkdm,&eStart, &eEnd);
719: for (i = eStart; i < eEnd; i++) {
720: DMNetworkGetComponent(networkdm,i,0,&key,(void**)&pipe);
721: DMDestroy(&(pipe->da));
722: VecDestroy(&pipe->x);
723: }
724: if (size == 1) {
725: DMNetworkMonitorDestroy(&monitor);
726: }
727: DMDestroy(&networkdm);
728: PetscFree(wash);
729: PetscFinalize();
730: return ierr;
731: }
734: /*TEST
736: build:
737: depends: pipeInterface.c pipeImpls.c
739: test:
740: args: -ts_monitor -case 1 -ts_max_steps 1 -nox
742: test:
743: suffix: 2
744: nsize: 2
745: args: -ts_monitor -case 1 -ts_max_steps 1 -petscpartitioner_type simple -nox
746: output_file: output/pipes1_1.out
748: test:
749: suffix: 3
750: nsize: 4
751: args: -ts_monitor -case 1 -ts_max_steps 1 -petscpartitioner_type simple -nox
752: output_file: output/pipes1_1.out
754: test:
755: suffix: 4
756: args: -ts_monitor -case 0 -ts_max_steps 1 -nox
758: test:
759: suffix: 5
760: nsize: 2
761: args: -ts_monitor -case 0 -ts_max_steps 1 -petscpartitioner_type simple -nox
762: output_file: output/pipes1_4.out
764: test:
765: suffix: 6
766: nsize: 4
767: args: -ts_monitor -case 0 -ts_max_steps 1 -nox -petscpartitioner_type simple
768: output_file: output/pipes1_4.out
770: test:
771: suffix: 7
772: args: -ts_monitor -case 2 -ts_max_steps 1 -nox
774: test:
775: suffix: 8
776: nsize: 2
777: args: -ts_monitor -case 2 -ts_max_steps 1 -petscpartitioner_type simple -nox
778: output_file: output/pipes1_7.out
780: test:
781: suffix: 9
782: nsize: 4
783: args: -ts_monitor -case 2 -ts_max_steps 1 -petscpartitioner_type simple -nox
784: output_file: output/pipes1_7.out
786: TEST*/