1 static char help[] = "This example demonstrates the use of DMNetwork interface for solving a steady-state water network model.\n\ 2 The water network equations follow those used for the package EPANET\n\ 3 The data file format used is from the EPANET package (https://www.epa.gov/water-research/epanet).\n\ 4 Run this program: mpiexec -n <n> ./water\n\\n"; 5 6 #include "water.h" 7 #include <petscdmnetwork.h> 8 9 int main(int argc, char **argv) { 10 char waterdata_file[PETSC_MAX_PATH_LEN] = "sample1.inp"; 11 WATERDATA *waterdata; 12 AppCtx_Water appctx; 13 #if defined(PETSC_USE_LOG) 14 PetscLogStage stage1, stage2; 15 #endif 16 PetscMPIInt crank; 17 DM networkdm; 18 PetscInt *edgelist = NULL; 19 PetscInt nv, ne, i; 20 const PetscInt *vtx, *edges; 21 Vec X, F; 22 SNES snes; 23 SNESConvergedReason reason; 24 25 PetscFunctionBeginUser; 26 PetscCall(PetscInitialize(&argc, &argv, "wateroptions", help)); 27 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &crank)); 28 29 /* Create an empty network object */ 30 PetscCall(DMNetworkCreate(PETSC_COMM_WORLD, &networkdm)); 31 32 /* Register the components in the network */ 33 PetscCall(DMNetworkRegisterComponent(networkdm, "edgestruct", sizeof(struct _p_EDGE_Water), &appctx.compkey_edge)); 34 PetscCall(DMNetworkRegisterComponent(networkdm, "busstruct", sizeof(struct _p_VERTEX_Water), &appctx.compkey_vtx)); 35 36 PetscCall(PetscLogStageRegister("Read Data", &stage1)); 37 PetscCall(PetscLogStagePush(stage1)); 38 PetscCall(PetscNew(&waterdata)); 39 40 /* READ THE DATA */ 41 if (!crank) { 42 /* READ DATA. Only rank 0 reads the data */ 43 PetscCall(PetscOptionsGetString(NULL, NULL, "-waterdata", waterdata_file, sizeof(waterdata_file), NULL)); 44 PetscCall(WaterReadData(waterdata, waterdata_file)); 45 46 PetscCall(PetscCalloc1(2 * waterdata->nedge, &edgelist)); 47 PetscCall(GetListofEdges_Water(waterdata, edgelist)); 48 } 49 PetscCall(PetscLogStagePop()); 50 51 PetscCall(PetscLogStageRegister("Create network", &stage2)); 52 PetscCall(PetscLogStagePush(stage2)); 53 54 /* Set numbers of nodes and edges */ 55 PetscCall(DMNetworkSetNumSubNetworks(networkdm, PETSC_DECIDE, 1)); 56 PetscCall(DMNetworkAddSubnetwork(networkdm, "", waterdata->nedge, edgelist, NULL)); 57 if (!crank) { PetscCall(PetscPrintf(PETSC_COMM_SELF, "water nvertices %" PetscInt_FMT ", nedges %" PetscInt_FMT "\n", waterdata->nvertex, waterdata->nedge)); } 58 59 /* Set up the network layout */ 60 PetscCall(DMNetworkLayoutSetUp(networkdm)); 61 62 if (!crank) { PetscCall(PetscFree(edgelist)); } 63 64 /* ADD VARIABLES AND COMPONENTS FOR THE NETWORK */ 65 PetscCall(DMNetworkGetSubnetwork(networkdm, 0, &nv, &ne, &vtx, &edges)); 66 67 for (i = 0; i < ne; i++) { PetscCall(DMNetworkAddComponent(networkdm, edges[i], appctx.compkey_edge, &waterdata->edge[i], 0)); } 68 69 for (i = 0; i < nv; i++) { PetscCall(DMNetworkAddComponent(networkdm, vtx[i], appctx.compkey_vtx, &waterdata->vertex[i], 1)); } 70 71 /* Set up DM for use */ 72 PetscCall(DMSetUp(networkdm)); 73 74 if (!crank) { 75 PetscCall(PetscFree(waterdata->vertex)); 76 PetscCall(PetscFree(waterdata->edge)); 77 } 78 PetscCall(PetscFree(waterdata)); 79 80 /* Distribute networkdm to multiple processes */ 81 PetscCall(DMNetworkDistribute(&networkdm, 0)); 82 83 PetscCall(PetscLogStagePop()); 84 85 PetscCall(DMCreateGlobalVector(networkdm, &X)); 86 PetscCall(VecDuplicate(X, &F)); 87 88 /* HOOK UP SOLVER */ 89 PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes)); 90 PetscCall(SNESSetDM(snes, networkdm)); 91 PetscCall(SNESSetOptionsPrefix(snes, "water_")); 92 PetscCall(SNESSetFunction(snes, F, WaterFormFunction, NULL)); 93 PetscCall(SNESSetFromOptions(snes)); 94 95 PetscCall(WaterSetInitialGuess(networkdm, X)); 96 /* PetscCall(VecView(X,PETSC_VIEWER_STDOUT_WORLD)); */ 97 98 PetscCall(SNESSolve(snes, NULL, X)); 99 PetscCall(SNESGetConvergedReason(snes, &reason)); 100 101 PetscCheck(reason >= 0, PETSC_COMM_SELF, PETSC_ERR_CONV_FAILED, "No solution found for the water network"); 102 /* PetscCall(VecView(X,PETSC_VIEWER_STDOUT_WORLD)); */ 103 104 PetscCall(VecDestroy(&X)); 105 PetscCall(VecDestroy(&F)); 106 PetscCall(SNESDestroy(&snes)); 107 PetscCall(DMDestroy(&networkdm)); 108 PetscCall(PetscFinalize()); 109 return 0; 110 } 111 112 /*TEST 113 114 build: 115 depends: waterreaddata.c waterfunctions.c 116 requires: !complex double defined(PETSC_HAVE_ATTRIBUTEALIGNED) 117 118 test: 119 args: -water_snes_converged_reason -options_left no 120 localrunfiles: wateroptions sample1.inp 121 output_file: output/water.out 122 requires: double !complex defined(PETSC_HAVE_ATTRIBUTEALIGNED) 123 124 test: 125 suffix: 2 126 nsize: 3 127 args: -water_snes_converged_reason -options_left no 128 localrunfiles: wateroptions sample1.inp 129 output_file: output/water.out 130 requires: double !complex defined(PETSC_HAVE_ATTRIBUTEALIGNED) 131 132 TEST*/ 133